blob: a4ef6371810106bef15aa649badef4ff9d8ac18d [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivity00b27a32011-11-23 16:30:32 +020021#include "cpuid.h"
Andrey Smetanind62caab2015-11-10 15:36:33 +030022#include "lapic.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivityedf88412007-12-16 11:02:48 +020024#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020026#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/mm.h>
28#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040029#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020030#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070031#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040032#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040034#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010035#include <linux/hrtimer.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030036#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030037#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040038
Feng Wu28b835d2015-09-18 22:29:54 +080039#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080040#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080041#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020042#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020043#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080044#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020045#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020046#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010047#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080048#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010049#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080050#include <asm/irq_remapping.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080051
Marcelo Tosatti229456f2009-06-17 09:22:14 -030052#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020053#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030054
Avi Kivity4ecac3f2008-05-13 13:23:38 +030055#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040056#define __ex_clear(x, reg) \
57 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030058
Avi Kivity6aa8b732006-12-10 02:21:36 -080059MODULE_AUTHOR("Qumranet");
60MODULE_LICENSE("GPL");
61
Josh Triplette9bda3b2012-03-20 23:33:51 -070062static const struct x86_cpu_id vmx_cpu_id[] = {
63 X86_FEATURE_MATCH(X86_FEATURE_VMX),
64 {}
65};
66MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
67
Rusty Russell476bc002012-01-13 09:32:18 +103068static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020069module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080070
Rusty Russell476bc002012-01-13 09:32:18 +103071static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020072module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020073
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020075module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080076
Rusty Russell476bc002012-01-13 09:32:18 +103077static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070078module_param_named(unrestricted_guest,
79 enable_unrestricted_guest, bool, S_IRUGO);
80
Xudong Hao83c3a332012-05-28 19:33:35 +080081static bool __read_mostly enable_ept_ad_bits = 1;
82module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
83
Avi Kivitya27685c2012-06-12 20:30:18 +030084static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020085module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030086
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030088module_param(fasteoi, bool, S_IRUGO);
89
Yang Zhang5a717852013-04-11 19:25:16 +080090static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080091module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080092
Abel Gordonabc4fc52013-04-18 14:35:25 +030093static bool __read_mostly enable_shadow_vmcs = 1;
94module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +030095/*
96 * If nested=1, nested virtualization is supported, i.e., guests may use
97 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
98 * use VMX instructions.
99 */
Rusty Russell476bc002012-01-13 09:32:18 +1030100static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300101module_param(nested, bool, S_IRUGO);
102
Wanpeng Li20300092014-12-02 19:14:59 +0800103static u64 __read_mostly host_xss;
104
Kai Huang843e4332015-01-28 10:54:28 +0800105static bool __read_mostly enable_pml = 1;
106module_param_named(pml, enable_pml, bool, S_IRUGO);
107
Haozhong Zhang64903d62015-10-20 15:39:09 +0800108#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
109
Yunhong Jiang64672c92016-06-13 14:19:59 -0700110/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
111static int __read_mostly cpu_preemption_timer_multi;
112static bool __read_mostly enable_preemption_timer = 1;
113#ifdef CONFIG_X86_64
114module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
115#endif
116
Gleb Natapov50378782013-02-04 16:00:28 +0200117#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
118#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200119#define KVM_VM_CR0_ALWAYS_ON \
120 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200121#define KVM_CR4_GUEST_OWNED_BITS \
122 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700123 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200124
Avi Kivitycdc0e242009-12-06 17:21:14 +0200125#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
126#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
127
Avi Kivity78ac8b42010-04-08 18:19:35 +0300128#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
129
Jan Kiszkaf41245002014-03-07 20:03:13 +0100130#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
131
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800132/*
Jan Dakinevich16c2aec2016-10-28 07:00:30 +0300133 * Hyper-V requires all of these, so mark them as supported even though
134 * they are just treated the same as all-context.
135 */
136#define VMX_VPID_EXTENT_SUPPORTED_MASK \
137 (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \
138 VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \
139 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \
140 VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT)
141
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800142/*
143 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
144 * ple_gap: upper bound on the amount of time between two successive
145 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500146 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800147 * ple_window: upper bound on the amount of time a guest is allowed to execute
148 * in a PAUSE loop. Tests indicate that most spinlocks are held for
149 * less than 2^12 cycles
150 * Time is measured based on a counter that runs at the same rate as the TSC,
151 * refer SDM volume 3b section 21.6.13 & 22.1.3.
152 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200153#define KVM_VMX_DEFAULT_PLE_GAP 128
154#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
155#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
156#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
157#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
158 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
159
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800160static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
161module_param(ple_gap, int, S_IRUGO);
162
163static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
164module_param(ple_window, int, S_IRUGO);
165
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200166/* Default doubles per-vcpu window every exit. */
167static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
168module_param(ple_window_grow, int, S_IRUGO);
169
170/* Default resets per-vcpu window every exit to ple_window. */
171static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
172module_param(ple_window_shrink, int, S_IRUGO);
173
174/* Default is to compute the maximum so we can never overflow. */
175static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
176static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
177module_param(ple_window_max, int, S_IRUGO);
178
Avi Kivity83287ea422012-09-16 15:10:57 +0300179extern const ulong vmx_return;
180
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200181#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300182#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300183
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400184struct vmcs {
185 u32 revision_id;
186 u32 abort;
187 char data[0];
188};
189
Nadav Har'Eld462b812011-05-24 15:26:10 +0300190/*
191 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
192 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
193 * loaded on this CPU (so we can clear them if the CPU goes down).
194 */
195struct loaded_vmcs {
196 struct vmcs *vmcs;
Jim Mattson355f4fb2016-10-28 08:29:39 -0700197 struct vmcs *shadow_vmcs;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300198 int cpu;
199 int launched;
200 struct list_head loaded_vmcss_on_cpu_link;
201};
202
Avi Kivity26bb0982009-09-07 11:14:12 +0300203struct shared_msr_entry {
204 unsigned index;
205 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200206 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300207};
208
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300209/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300210 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
211 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
212 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
213 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
214 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
215 * More than one of these structures may exist, if L1 runs multiple L2 guests.
216 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
217 * underlying hardware which will be used to run L2.
218 * This structure is packed to ensure that its layout is identical across
219 * machines (necessary for live migration).
220 * If there are changes in this struct, VMCS12_REVISION must be changed.
221 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300222typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300223struct __packed vmcs12 {
224 /* According to the Intel spec, a VMCS region must start with the
225 * following two fields. Then follow implementation-specific data.
226 */
227 u32 revision_id;
228 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300229
Nadav Har'El27d6c862011-05-25 23:06:59 +0300230 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
231 u32 padding[7]; /* room for future expansion */
232
Nadav Har'El22bd0352011-05-25 23:05:57 +0300233 u64 io_bitmap_a;
234 u64 io_bitmap_b;
235 u64 msr_bitmap;
236 u64 vm_exit_msr_store_addr;
237 u64 vm_exit_msr_load_addr;
238 u64 vm_entry_msr_load_addr;
239 u64 tsc_offset;
240 u64 virtual_apic_page_addr;
241 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800242 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300243 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800244 u64 eoi_exit_bitmap0;
245 u64 eoi_exit_bitmap1;
246 u64 eoi_exit_bitmap2;
247 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800248 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300249 u64 guest_physical_address;
250 u64 vmcs_link_pointer;
251 u64 guest_ia32_debugctl;
252 u64 guest_ia32_pat;
253 u64 guest_ia32_efer;
254 u64 guest_ia32_perf_global_ctrl;
255 u64 guest_pdptr0;
256 u64 guest_pdptr1;
257 u64 guest_pdptr2;
258 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100259 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300260 u64 host_ia32_pat;
261 u64 host_ia32_efer;
262 u64 host_ia32_perf_global_ctrl;
263 u64 padding64[8]; /* room for future expansion */
264 /*
265 * To allow migration of L1 (complete with its L2 guests) between
266 * machines of different natural widths (32 or 64 bit), we cannot have
267 * unsigned long fields with no explict size. We use u64 (aliased
268 * natural_width) instead. Luckily, x86 is little-endian.
269 */
270 natural_width cr0_guest_host_mask;
271 natural_width cr4_guest_host_mask;
272 natural_width cr0_read_shadow;
273 natural_width cr4_read_shadow;
274 natural_width cr3_target_value0;
275 natural_width cr3_target_value1;
276 natural_width cr3_target_value2;
277 natural_width cr3_target_value3;
278 natural_width exit_qualification;
279 natural_width guest_linear_address;
280 natural_width guest_cr0;
281 natural_width guest_cr3;
282 natural_width guest_cr4;
283 natural_width guest_es_base;
284 natural_width guest_cs_base;
285 natural_width guest_ss_base;
286 natural_width guest_ds_base;
287 natural_width guest_fs_base;
288 natural_width guest_gs_base;
289 natural_width guest_ldtr_base;
290 natural_width guest_tr_base;
291 natural_width guest_gdtr_base;
292 natural_width guest_idtr_base;
293 natural_width guest_dr7;
294 natural_width guest_rsp;
295 natural_width guest_rip;
296 natural_width guest_rflags;
297 natural_width guest_pending_dbg_exceptions;
298 natural_width guest_sysenter_esp;
299 natural_width guest_sysenter_eip;
300 natural_width host_cr0;
301 natural_width host_cr3;
302 natural_width host_cr4;
303 natural_width host_fs_base;
304 natural_width host_gs_base;
305 natural_width host_tr_base;
306 natural_width host_gdtr_base;
307 natural_width host_idtr_base;
308 natural_width host_ia32_sysenter_esp;
309 natural_width host_ia32_sysenter_eip;
310 natural_width host_rsp;
311 natural_width host_rip;
312 natural_width paddingl[8]; /* room for future expansion */
313 u32 pin_based_vm_exec_control;
314 u32 cpu_based_vm_exec_control;
315 u32 exception_bitmap;
316 u32 page_fault_error_code_mask;
317 u32 page_fault_error_code_match;
318 u32 cr3_target_count;
319 u32 vm_exit_controls;
320 u32 vm_exit_msr_store_count;
321 u32 vm_exit_msr_load_count;
322 u32 vm_entry_controls;
323 u32 vm_entry_msr_load_count;
324 u32 vm_entry_intr_info_field;
325 u32 vm_entry_exception_error_code;
326 u32 vm_entry_instruction_len;
327 u32 tpr_threshold;
328 u32 secondary_vm_exec_control;
329 u32 vm_instruction_error;
330 u32 vm_exit_reason;
331 u32 vm_exit_intr_info;
332 u32 vm_exit_intr_error_code;
333 u32 idt_vectoring_info_field;
334 u32 idt_vectoring_error_code;
335 u32 vm_exit_instruction_len;
336 u32 vmx_instruction_info;
337 u32 guest_es_limit;
338 u32 guest_cs_limit;
339 u32 guest_ss_limit;
340 u32 guest_ds_limit;
341 u32 guest_fs_limit;
342 u32 guest_gs_limit;
343 u32 guest_ldtr_limit;
344 u32 guest_tr_limit;
345 u32 guest_gdtr_limit;
346 u32 guest_idtr_limit;
347 u32 guest_es_ar_bytes;
348 u32 guest_cs_ar_bytes;
349 u32 guest_ss_ar_bytes;
350 u32 guest_ds_ar_bytes;
351 u32 guest_fs_ar_bytes;
352 u32 guest_gs_ar_bytes;
353 u32 guest_ldtr_ar_bytes;
354 u32 guest_tr_ar_bytes;
355 u32 guest_interruptibility_info;
356 u32 guest_activity_state;
357 u32 guest_sysenter_cs;
358 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100359 u32 vmx_preemption_timer_value;
360 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300361 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800362 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300363 u16 guest_es_selector;
364 u16 guest_cs_selector;
365 u16 guest_ss_selector;
366 u16 guest_ds_selector;
367 u16 guest_fs_selector;
368 u16 guest_gs_selector;
369 u16 guest_ldtr_selector;
370 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800371 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300372 u16 host_es_selector;
373 u16 host_cs_selector;
374 u16 host_ss_selector;
375 u16 host_ds_selector;
376 u16 host_fs_selector;
377 u16 host_gs_selector;
378 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300379};
380
381/*
382 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
383 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
384 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
385 */
386#define VMCS12_REVISION 0x11e57ed0
387
388/*
389 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
390 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
391 * current implementation, 4K are reserved to avoid future complications.
392 */
393#define VMCS12_SIZE 0x1000
394
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300395/* Used to remember the last vmcs02 used for some recently used vmcs12s */
396struct vmcs02_list {
397 struct list_head list;
398 gpa_t vmptr;
399 struct loaded_vmcs vmcs02;
400};
401
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300402/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300403 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
404 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
405 */
406struct nested_vmx {
407 /* Has the level1 guest done vmxon? */
408 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400409 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300410
411 /* The guest-physical address of the current VMCS L1 keeps for L2 */
412 gpa_t current_vmptr;
413 /* The host-usable pointer to the above */
414 struct page *current_vmcs12_page;
415 struct vmcs12 *current_vmcs12;
David Matlack4f2777b2016-07-13 17:16:37 -0700416 /*
417 * Cache of the guest's VMCS, existing outside of guest memory.
418 * Loaded from guest memory during VMPTRLD. Flushed to guest
419 * memory during VMXOFF, VMCLEAR, VMPTRLD.
420 */
421 struct vmcs12 *cached_vmcs12;
Abel Gordon012f83c2013-04-18 14:39:25 +0300422 /*
423 * Indicates if the shadow vmcs must be updated with the
424 * data hold by vmcs12
425 */
426 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300427
428 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
429 struct list_head vmcs02_pool;
430 int vmcs02_num;
Radim Krčmářdccbfcf2016-08-08 20:16:23 +0200431 bool change_vmcs01_virtual_x2apic_mode;
Nadav Har'El644d7112011-05-25 23:12:35 +0300432 /* L2 must run next, and mustn't decide to exit to L1. */
433 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300434 /*
435 * Guest pages referred to in vmcs02 with host-physical pointers, so
436 * we must keep them pinned while L2 runs.
437 */
438 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800439 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800440 struct page *pi_desc_page;
441 struct pi_desc *pi_desc;
442 bool pi_pending;
443 u16 posted_intr_nv;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100444
Radim Krčmářd048c092016-08-08 20:16:22 +0200445 unsigned long *msr_bitmap;
446
Jan Kiszkaf41245002014-03-07 20:03:13 +0100447 struct hrtimer preemption_timer;
448 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200449
450 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
451 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800452
Wanpeng Li5c614b32015-10-13 09:18:36 -0700453 u16 vpid02;
454 u16 last_vpid;
455
David Matlack0115f9c2016-11-29 18:14:06 -0800456 /*
457 * We only store the "true" versions of the VMX capability MSRs. We
458 * generate the "non-true" versions by setting the must-be-1 bits
459 * according to the SDM.
460 */
Wincy Vanb9c237b2015-02-03 23:56:30 +0800461 u32 nested_vmx_procbased_ctls_low;
462 u32 nested_vmx_procbased_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800463 u32 nested_vmx_secondary_ctls_low;
464 u32 nested_vmx_secondary_ctls_high;
465 u32 nested_vmx_pinbased_ctls_low;
466 u32 nested_vmx_pinbased_ctls_high;
467 u32 nested_vmx_exit_ctls_low;
468 u32 nested_vmx_exit_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800469 u32 nested_vmx_entry_ctls_low;
470 u32 nested_vmx_entry_ctls_high;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800471 u32 nested_vmx_misc_low;
472 u32 nested_vmx_misc_high;
473 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700474 u32 nested_vmx_vpid_caps;
David Matlack62cc6b9d2016-11-29 18:14:07 -0800475 u64 nested_vmx_basic;
476 u64 nested_vmx_cr0_fixed0;
477 u64 nested_vmx_cr0_fixed1;
478 u64 nested_vmx_cr4_fixed0;
479 u64 nested_vmx_cr4_fixed1;
480 u64 nested_vmx_vmcs_enum;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300481};
482
Yang Zhang01e439b2013-04-11 19:25:12 +0800483#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800484#define POSTED_INTR_SN 1
485
Yang Zhang01e439b2013-04-11 19:25:12 +0800486/* Posted-Interrupt Descriptor */
487struct pi_desc {
488 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800489 union {
490 struct {
491 /* bit 256 - Outstanding Notification */
492 u16 on : 1,
493 /* bit 257 - Suppress Notification */
494 sn : 1,
495 /* bit 271:258 - Reserved */
496 rsvd_1 : 14;
497 /* bit 279:272 - Notification Vector */
498 u8 nv;
499 /* bit 287:280 - Reserved */
500 u8 rsvd_2;
501 /* bit 319:288 - Notification Destination */
502 u32 ndst;
503 };
504 u64 control;
505 };
506 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800507} __aligned(64);
508
Yang Zhanga20ed542013-04-11 19:25:15 +0800509static bool pi_test_and_set_on(struct pi_desc *pi_desc)
510{
511 return test_and_set_bit(POSTED_INTR_ON,
512 (unsigned long *)&pi_desc->control);
513}
514
515static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
516{
517 return test_and_clear_bit(POSTED_INTR_ON,
518 (unsigned long *)&pi_desc->control);
519}
520
521static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
522{
523 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
524}
525
Feng Wuebbfc762015-09-18 22:29:46 +0800526static inline void pi_clear_sn(struct pi_desc *pi_desc)
527{
528 return clear_bit(POSTED_INTR_SN,
529 (unsigned long *)&pi_desc->control);
530}
531
532static inline void pi_set_sn(struct pi_desc *pi_desc)
533{
534 return set_bit(POSTED_INTR_SN,
535 (unsigned long *)&pi_desc->control);
536}
537
Paolo Bonziniad361092016-09-20 16:15:05 +0200538static inline void pi_clear_on(struct pi_desc *pi_desc)
539{
540 clear_bit(POSTED_INTR_ON,
541 (unsigned long *)&pi_desc->control);
542}
543
Feng Wuebbfc762015-09-18 22:29:46 +0800544static inline int pi_test_on(struct pi_desc *pi_desc)
545{
546 return test_bit(POSTED_INTR_ON,
547 (unsigned long *)&pi_desc->control);
548}
549
550static inline int pi_test_sn(struct pi_desc *pi_desc)
551{
552 return test_bit(POSTED_INTR_SN,
553 (unsigned long *)&pi_desc->control);
554}
555
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400556struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000557 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300558 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300559 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200560 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300561 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200562 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200563 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300564 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400565 int nmsrs;
566 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800567 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400568#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300569 u64 msr_host_kernel_gs_base;
570 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400571#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200572 u32 vm_entry_controls_shadow;
573 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300574 /*
575 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
576 * non-nested (L1) guest, it always points to vmcs01. For a nested
577 * guest (L2), it points to a different VMCS.
578 */
579 struct loaded_vmcs vmcs01;
580 struct loaded_vmcs *loaded_vmcs;
581 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300582 struct msr_autoload {
583 unsigned nr;
584 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
585 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
586 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400587 struct {
588 int loaded;
589 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300590#ifdef CONFIG_X86_64
591 u16 ds_sel, es_sel;
592#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200593 int gs_ldt_reload_needed;
594 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000595 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700596 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400597 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200598 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300599 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300600 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300601 struct kvm_segment segs[8];
602 } rmode;
603 struct {
604 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300605 struct kvm_save_segment {
606 u16 selector;
607 unsigned long base;
608 u32 limit;
609 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300610 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300611 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800612 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300613 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200614
Andi Kleena0861c02009-06-08 17:37:09 +0800615 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800616
Yang Zhang01e439b2013-04-11 19:25:12 +0800617 /* Posted interrupt descriptor */
618 struct pi_desc pi_desc;
619
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300620 /* Support for a guest hypervisor (nested VMX) */
621 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200622
623 /* Dynamic PLE window. */
624 int ple_window;
625 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800626
627 /* Support for PML */
628#define PML_ENTITY_NUM 512
629 struct page *pml_pg;
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800630
Yunhong Jiang64672c92016-06-13 14:19:59 -0700631 /* apic deadline value in host tsc */
632 u64 hv_deadline_tsc;
633
Owen Hofmann2680d6d2016-03-01 13:36:13 -0800634 u64 current_tsc_ratio;
Xiao Guangrong1be0e612016-03-22 16:51:18 +0800635
636 bool guest_pkru_valid;
637 u32 guest_pkru;
638 u32 host_pkru;
Haozhong Zhang3b840802016-06-22 14:59:54 +0800639
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800640 /*
641 * Only bits masked by msr_ia32_feature_control_valid_bits can be set in
642 * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included
643 * in msr_ia32_feature_control_valid_bits.
644 */
Haozhong Zhang3b840802016-06-22 14:59:54 +0800645 u64 msr_ia32_feature_control;
Haozhong Zhang37e4c992016-06-22 14:59:55 +0800646 u64 msr_ia32_feature_control_valid_bits;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400647};
648
Avi Kivity2fb92db2011-04-27 19:42:18 +0300649enum segment_cache_field {
650 SEG_FIELD_SEL = 0,
651 SEG_FIELD_BASE = 1,
652 SEG_FIELD_LIMIT = 2,
653 SEG_FIELD_AR = 3,
654
655 SEG_FIELD_NR = 4
656};
657
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400658static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
659{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000660 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400661}
662
Feng Wuefc64402015-09-18 22:29:51 +0800663static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
664{
665 return &(to_vmx(vcpu)->pi_desc);
666}
667
Nadav Har'El22bd0352011-05-25 23:05:57 +0300668#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
669#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
670#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
671 [number##_HIGH] = VMCS12_OFFSET(name)+4
672
Abel Gordon4607c2d2013-04-18 14:35:55 +0300673
Bandan Dasfe2b2012014-04-21 15:20:14 -0400674static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300675 /*
676 * We do NOT shadow fields that are modified when L0
677 * traps and emulates any vmx instruction (e.g. VMPTRLD,
678 * VMXON...) executed by L1.
679 * For example, VM_INSTRUCTION_ERROR is read
680 * by L1 if a vmx instruction fails (part of the error path).
681 * Note the code assumes this logic. If for some reason
682 * we start shadowing these fields then we need to
683 * force a shadow sync when L0 emulates vmx instructions
684 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
685 * by nested_vmx_failValid)
686 */
687 VM_EXIT_REASON,
688 VM_EXIT_INTR_INFO,
689 VM_EXIT_INSTRUCTION_LEN,
690 IDT_VECTORING_INFO_FIELD,
691 IDT_VECTORING_ERROR_CODE,
692 VM_EXIT_INTR_ERROR_CODE,
693 EXIT_QUALIFICATION,
694 GUEST_LINEAR_ADDRESS,
695 GUEST_PHYSICAL_ADDRESS
696};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400697static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300698 ARRAY_SIZE(shadow_read_only_fields);
699
Bandan Dasfe2b2012014-04-21 15:20:14 -0400700static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800701 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300702 GUEST_RIP,
703 GUEST_RSP,
704 GUEST_CR0,
705 GUEST_CR3,
706 GUEST_CR4,
707 GUEST_INTERRUPTIBILITY_INFO,
708 GUEST_RFLAGS,
709 GUEST_CS_SELECTOR,
710 GUEST_CS_AR_BYTES,
711 GUEST_CS_LIMIT,
712 GUEST_CS_BASE,
713 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100714 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300715 CR0_GUEST_HOST_MASK,
716 CR0_READ_SHADOW,
717 CR4_READ_SHADOW,
718 TSC_OFFSET,
719 EXCEPTION_BITMAP,
720 CPU_BASED_VM_EXEC_CONTROL,
721 VM_ENTRY_EXCEPTION_ERROR_CODE,
722 VM_ENTRY_INTR_INFO_FIELD,
723 VM_ENTRY_INSTRUCTION_LEN,
724 VM_ENTRY_EXCEPTION_ERROR_CODE,
725 HOST_FS_BASE,
726 HOST_GS_BASE,
727 HOST_FS_SELECTOR,
728 HOST_GS_SELECTOR
729};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400730static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300731 ARRAY_SIZE(shadow_read_write_fields);
732
Mathias Krause772e0312012-08-30 01:30:19 +0200733static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300734 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800735 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300736 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
737 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
738 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
739 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
740 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
741 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
742 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
743 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800744 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300745 FIELD(HOST_ES_SELECTOR, host_es_selector),
746 FIELD(HOST_CS_SELECTOR, host_cs_selector),
747 FIELD(HOST_SS_SELECTOR, host_ss_selector),
748 FIELD(HOST_DS_SELECTOR, host_ds_selector),
749 FIELD(HOST_FS_SELECTOR, host_fs_selector),
750 FIELD(HOST_GS_SELECTOR, host_gs_selector),
751 FIELD(HOST_TR_SELECTOR, host_tr_selector),
752 FIELD64(IO_BITMAP_A, io_bitmap_a),
753 FIELD64(IO_BITMAP_B, io_bitmap_b),
754 FIELD64(MSR_BITMAP, msr_bitmap),
755 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
756 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
757 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
758 FIELD64(TSC_OFFSET, tsc_offset),
759 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
760 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800761 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300762 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800763 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
764 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
765 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
766 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800767 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300768 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
769 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
770 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
771 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
772 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
773 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
774 FIELD64(GUEST_PDPTR0, guest_pdptr0),
775 FIELD64(GUEST_PDPTR1, guest_pdptr1),
776 FIELD64(GUEST_PDPTR2, guest_pdptr2),
777 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100778 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300779 FIELD64(HOST_IA32_PAT, host_ia32_pat),
780 FIELD64(HOST_IA32_EFER, host_ia32_efer),
781 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
782 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
783 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
784 FIELD(EXCEPTION_BITMAP, exception_bitmap),
785 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
786 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
787 FIELD(CR3_TARGET_COUNT, cr3_target_count),
788 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
789 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
790 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
791 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
792 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
793 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
794 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
795 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
796 FIELD(TPR_THRESHOLD, tpr_threshold),
797 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
798 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
799 FIELD(VM_EXIT_REASON, vm_exit_reason),
800 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
801 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
802 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
803 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
804 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
805 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
806 FIELD(GUEST_ES_LIMIT, guest_es_limit),
807 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
808 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
809 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
810 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
811 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
812 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
813 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
814 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
815 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
816 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
817 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
818 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
819 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
820 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
821 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
822 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
823 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
824 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
825 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
826 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
827 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100828 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300829 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
830 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
831 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
832 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
833 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
834 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
835 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
836 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
837 FIELD(EXIT_QUALIFICATION, exit_qualification),
838 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
839 FIELD(GUEST_CR0, guest_cr0),
840 FIELD(GUEST_CR3, guest_cr3),
841 FIELD(GUEST_CR4, guest_cr4),
842 FIELD(GUEST_ES_BASE, guest_es_base),
843 FIELD(GUEST_CS_BASE, guest_cs_base),
844 FIELD(GUEST_SS_BASE, guest_ss_base),
845 FIELD(GUEST_DS_BASE, guest_ds_base),
846 FIELD(GUEST_FS_BASE, guest_fs_base),
847 FIELD(GUEST_GS_BASE, guest_gs_base),
848 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
849 FIELD(GUEST_TR_BASE, guest_tr_base),
850 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
851 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
852 FIELD(GUEST_DR7, guest_dr7),
853 FIELD(GUEST_RSP, guest_rsp),
854 FIELD(GUEST_RIP, guest_rip),
855 FIELD(GUEST_RFLAGS, guest_rflags),
856 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
857 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
858 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
859 FIELD(HOST_CR0, host_cr0),
860 FIELD(HOST_CR3, host_cr3),
861 FIELD(HOST_CR4, host_cr4),
862 FIELD(HOST_FS_BASE, host_fs_base),
863 FIELD(HOST_GS_BASE, host_gs_base),
864 FIELD(HOST_TR_BASE, host_tr_base),
865 FIELD(HOST_GDTR_BASE, host_gdtr_base),
866 FIELD(HOST_IDTR_BASE, host_idtr_base),
867 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
868 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
869 FIELD(HOST_RSP, host_rsp),
870 FIELD(HOST_RIP, host_rip),
871};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300872
873static inline short vmcs_field_to_offset(unsigned long field)
874{
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100875 BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
876
877 if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
878 vmcs_field_to_offset_table[field] == 0)
879 return -ENOENT;
880
Nadav Har'El22bd0352011-05-25 23:05:57 +0300881 return vmcs_field_to_offset_table[field];
882}
883
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300884static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
885{
David Matlack4f2777b2016-07-13 17:16:37 -0700886 return to_vmx(vcpu)->nested.cached_vmcs12;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300887}
888
889static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
890{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200891 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800892 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300893 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800894
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300895 return page;
896}
897
898static void nested_release_page(struct page *page)
899{
900 kvm_release_page_dirty(page);
901}
902
903static void nested_release_page_clean(struct page *page)
904{
905 kvm_release_page_clean(page);
906}
907
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300908static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800909static u64 construct_eptp(unsigned long root_hpa);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800910static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200911static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300912static void vmx_set_segment(struct kvm_vcpu *vcpu,
913 struct kvm_segment *var, int seg);
914static void vmx_get_segment(struct kvm_vcpu *vcpu,
915 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200916static bool guest_state_valid(struct kvm_vcpu *vcpu);
917static u32 vmx_segment_access_rights(struct kvm_segment *var);
Abel Gordonc3114422013-04-18 14:38:55 +0300918static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300919static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +0800920static int alloc_identity_pagetable(struct kvm *kvm);
Avi Kivity75880a02007-06-20 11:20:04 +0300921
Avi Kivity6aa8b732006-12-10 02:21:36 -0800922static DEFINE_PER_CPU(struct vmcs *, vmxarea);
923static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300924/*
925 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
926 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
927 */
928static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300929static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800930
Feng Wubf9f6ac2015-09-18 22:29:55 +0800931/*
932 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
933 * can find which vCPU should be waken up.
934 */
935static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
936static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
937
Radim Krčmář23611332016-09-29 22:41:33 +0200938enum {
939 VMX_IO_BITMAP_A,
940 VMX_IO_BITMAP_B,
941 VMX_MSR_BITMAP_LEGACY,
942 VMX_MSR_BITMAP_LONGMODE,
943 VMX_MSR_BITMAP_LEGACY_X2APIC_APICV,
944 VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV,
945 VMX_MSR_BITMAP_LEGACY_X2APIC,
946 VMX_MSR_BITMAP_LONGMODE_X2APIC,
947 VMX_VMREAD_BITMAP,
948 VMX_VMWRITE_BITMAP,
949 VMX_BITMAP_NR
950};
951
952static unsigned long *vmx_bitmap[VMX_BITMAP_NR];
953
954#define vmx_io_bitmap_a (vmx_bitmap[VMX_IO_BITMAP_A])
955#define vmx_io_bitmap_b (vmx_bitmap[VMX_IO_BITMAP_B])
956#define vmx_msr_bitmap_legacy (vmx_bitmap[VMX_MSR_BITMAP_LEGACY])
957#define vmx_msr_bitmap_longmode (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE])
958#define vmx_msr_bitmap_legacy_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC_APICV])
959#define vmx_msr_bitmap_longmode_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV])
960#define vmx_msr_bitmap_legacy_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC])
961#define vmx_msr_bitmap_longmode_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC])
962#define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP])
963#define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP])
He, Qingfdef3ad2007-04-30 09:45:24 +0300964
Avi Kivity110312c2010-12-21 12:54:20 +0200965static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200966static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200967
Sheng Yang2384d2b2008-01-17 15:14:33 +0800968static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
969static DEFINE_SPINLOCK(vmx_vpid_lock);
970
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300971static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972 int size;
973 int order;
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +0300974 u32 basic_cap;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300976 u32 pin_based_exec_ctrl;
977 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800978 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300979 u32 vmexit_ctrl;
980 u32 vmentry_ctrl;
981} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982
Hannes Ederefff9e52008-11-28 17:02:06 +0100983static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800984 u32 ept;
985 u32 vpid;
986} vmx_capability;
987
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988#define VMX_SEGMENT_FIELD(seg) \
989 [VCPU_SREG_##seg] = { \
990 .selector = GUEST_##seg##_SELECTOR, \
991 .base = GUEST_##seg##_BASE, \
992 .limit = GUEST_##seg##_LIMIT, \
993 .ar_bytes = GUEST_##seg##_AR_BYTES, \
994 }
995
Mathias Krause772e0312012-08-30 01:30:19 +0200996static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 unsigned selector;
998 unsigned base;
999 unsigned limit;
1000 unsigned ar_bytes;
1001} kvm_vmx_segment_fields[] = {
1002 VMX_SEGMENT_FIELD(CS),
1003 VMX_SEGMENT_FIELD(DS),
1004 VMX_SEGMENT_FIELD(ES),
1005 VMX_SEGMENT_FIELD(FS),
1006 VMX_SEGMENT_FIELD(GS),
1007 VMX_SEGMENT_FIELD(SS),
1008 VMX_SEGMENT_FIELD(TR),
1009 VMX_SEGMENT_FIELD(LDTR),
1010};
1011
Avi Kivity26bb0982009-09-07 11:14:12 +03001012static u64 host_efer;
1013
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001014static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
1015
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001016/*
Brian Gerst8c065852010-07-17 09:03:26 -04001017 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001018 * away by decrementing the array size.
1019 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001020static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001021#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +03001022 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001023#endif
Brian Gerst8c065852010-07-17 09:03:26 -04001024 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025};
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026
Jan Kiszka5bb16012016-02-09 20:14:21 +01001027static inline bool is_exception_n(u32 intr_info, u8 vector)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028{
1029 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1030 INTR_INFO_VALID_MASK)) ==
Jan Kiszka5bb16012016-02-09 20:14:21 +01001031 (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK);
1032}
1033
Jan Kiszka6f054852016-02-09 20:15:18 +01001034static inline bool is_debug(u32 intr_info)
1035{
1036 return is_exception_n(intr_info, DB_VECTOR);
1037}
1038
1039static inline bool is_breakpoint(u32 intr_info)
1040{
1041 return is_exception_n(intr_info, BP_VECTOR);
1042}
1043
Jan Kiszka5bb16012016-02-09 20:14:21 +01001044static inline bool is_page_fault(u32 intr_info)
1045{
1046 return is_exception_n(intr_info, PF_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047}
1048
Gui Jianfeng31299942010-03-15 17:29:09 +08001049static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001050{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001051 return is_exception_n(intr_info, NM_VECTOR);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001052}
1053
Gui Jianfeng31299942010-03-15 17:29:09 +08001054static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001055{
Jan Kiszka5bb16012016-02-09 20:14:21 +01001056 return is_exception_n(intr_info, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001057}
1058
Gui Jianfeng31299942010-03-15 17:29:09 +08001059static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060{
1061 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1062 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1063}
1064
Gui Jianfeng31299942010-03-15 17:29:09 +08001065static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +08001066{
1067 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
1068 INTR_INFO_VALID_MASK)) ==
1069 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
1070}
1071
Gui Jianfeng31299942010-03-15 17:29:09 +08001072static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001073{
Sheng Yang04547152009-04-01 15:52:31 +08001074 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001075}
1076
Gui Jianfeng31299942010-03-15 17:29:09 +08001077static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001078{
Sheng Yang04547152009-04-01 15:52:31 +08001079 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001080}
1081
Paolo Bonzini35754c92015-07-29 12:05:37 +02001082static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001083{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001084 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001085}
1086
Gui Jianfeng31299942010-03-15 17:29:09 +08001087static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001088{
Sheng Yang04547152009-04-01 15:52:31 +08001089 return vmcs_config.cpu_based_exec_ctrl &
1090 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001091}
1092
Avi Kivity774ead32007-12-26 13:57:04 +02001093static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001094{
Sheng Yang04547152009-04-01 15:52:31 +08001095 return vmcs_config.cpu_based_2nd_exec_ctrl &
1096 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1097}
1098
Yang Zhang8d146952013-01-25 10:18:50 +08001099static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1100{
1101 return vmcs_config.cpu_based_2nd_exec_ctrl &
1102 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1103}
1104
Yang Zhang83d4c282013-01-25 10:18:49 +08001105static inline bool cpu_has_vmx_apic_register_virt(void)
1106{
1107 return vmcs_config.cpu_based_2nd_exec_ctrl &
1108 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1109}
1110
Yang Zhangc7c9c562013-01-25 10:18:51 +08001111static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1112{
1113 return vmcs_config.cpu_based_2nd_exec_ctrl &
1114 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1115}
1116
Yunhong Jiang64672c92016-06-13 14:19:59 -07001117/*
1118 * Comment's format: document - errata name - stepping - processor name.
1119 * Refer from
1120 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1121 */
1122static u32 vmx_preemption_cpu_tfms[] = {
1123/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
11240x000206E6,
1125/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1126/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1127/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
11280x00020652,
1129/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
11300x00020655,
1131/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1132/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1133/*
1134 * 320767.pdf - AAP86 - B1 -
1135 * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1136 */
11370x000106E5,
1138/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
11390x000106A0,
1140/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
11410x000106A1,
1142/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
11430x000106A4,
1144 /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1145 /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1146 /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
11470x000106A5,
1148};
1149
1150static inline bool cpu_has_broken_vmx_preemption_timer(void)
1151{
1152 u32 eax = cpuid_eax(0x00000001), i;
1153
1154 /* Clear the reserved bits */
1155 eax &= ~(0x3U << 14 | 0xfU << 28);
Wei Yongjun03f6a222016-07-04 15:13:07 +00001156 for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
Yunhong Jiang64672c92016-06-13 14:19:59 -07001157 if (eax == vmx_preemption_cpu_tfms[i])
1158 return true;
1159
1160 return false;
1161}
1162
1163static inline bool cpu_has_vmx_preemption_timer(void)
1164{
Yunhong Jiang64672c92016-06-13 14:19:59 -07001165 return vmcs_config.pin_based_exec_ctrl &
1166 PIN_BASED_VMX_PREEMPTION_TIMER;
1167}
1168
Yang Zhang01e439b2013-04-11 19:25:12 +08001169static inline bool cpu_has_vmx_posted_intr(void)
1170{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001171 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1172 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001173}
1174
1175static inline bool cpu_has_vmx_apicv(void)
1176{
1177 return cpu_has_vmx_apic_register_virt() &&
1178 cpu_has_vmx_virtual_intr_delivery() &&
1179 cpu_has_vmx_posted_intr();
1180}
1181
Sheng Yang04547152009-04-01 15:52:31 +08001182static inline bool cpu_has_vmx_flexpriority(void)
1183{
1184 return cpu_has_vmx_tpr_shadow() &&
1185 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001186}
1187
Marcelo Tosattie7997942009-06-11 12:07:40 -03001188static inline bool cpu_has_vmx_ept_execute_only(void)
1189{
Gui Jianfeng31299942010-03-15 17:29:09 +08001190 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001191}
1192
Marcelo Tosattie7997942009-06-11 12:07:40 -03001193static inline bool cpu_has_vmx_ept_2m_page(void)
1194{
Gui Jianfeng31299942010-03-15 17:29:09 +08001195 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001196}
1197
Sheng Yang878403b2010-01-05 19:02:29 +08001198static inline bool cpu_has_vmx_ept_1g_page(void)
1199{
Gui Jianfeng31299942010-03-15 17:29:09 +08001200 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001201}
1202
Sheng Yang4bc9b982010-06-02 14:05:24 +08001203static inline bool cpu_has_vmx_ept_4levels(void)
1204{
1205 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1206}
1207
Xudong Hao83c3a332012-05-28 19:33:35 +08001208static inline bool cpu_has_vmx_ept_ad_bits(void)
1209{
1210 return vmx_capability.ept & VMX_EPT_AD_BIT;
1211}
1212
Gui Jianfeng31299942010-03-15 17:29:09 +08001213static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001214{
Gui Jianfeng31299942010-03-15 17:29:09 +08001215 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001216}
1217
Gui Jianfeng31299942010-03-15 17:29:09 +08001218static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001219{
Gui Jianfeng31299942010-03-15 17:29:09 +08001220 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001221}
1222
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001223static inline bool cpu_has_vmx_invvpid_single(void)
1224{
1225 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1226}
1227
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001228static inline bool cpu_has_vmx_invvpid_global(void)
1229{
1230 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1231}
1232
Wanpeng Li08d839c2017-03-23 05:30:08 -07001233static inline bool cpu_has_vmx_invvpid(void)
1234{
1235 return vmx_capability.vpid & VMX_VPID_INVVPID_BIT;
1236}
1237
Gui Jianfeng31299942010-03-15 17:29:09 +08001238static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001239{
Sheng Yang04547152009-04-01 15:52:31 +08001240 return vmcs_config.cpu_based_2nd_exec_ctrl &
1241 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001242}
1243
Gui Jianfeng31299942010-03-15 17:29:09 +08001244static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001245{
1246 return vmcs_config.cpu_based_2nd_exec_ctrl &
1247 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1248}
1249
Gui Jianfeng31299942010-03-15 17:29:09 +08001250static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001251{
1252 return vmcs_config.cpu_based_2nd_exec_ctrl &
1253 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1254}
1255
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03001256static inline bool cpu_has_vmx_basic_inout(void)
1257{
1258 return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT);
1259}
1260
Paolo Bonzini35754c92015-07-29 12:05:37 +02001261static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001262{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001263 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001264}
1265
Gui Jianfeng31299942010-03-15 17:29:09 +08001266static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001267{
Sheng Yang04547152009-04-01 15:52:31 +08001268 return vmcs_config.cpu_based_2nd_exec_ctrl &
1269 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001270}
1271
Gui Jianfeng31299942010-03-15 17:29:09 +08001272static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001273{
1274 return vmcs_config.cpu_based_2nd_exec_ctrl &
1275 SECONDARY_EXEC_RDTSCP;
1276}
1277
Mao, Junjiead756a12012-07-02 01:18:48 +00001278static inline bool cpu_has_vmx_invpcid(void)
1279{
1280 return vmcs_config.cpu_based_2nd_exec_ctrl &
1281 SECONDARY_EXEC_ENABLE_INVPCID;
1282}
1283
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001284static inline bool cpu_has_vmx_wbinvd_exit(void)
1285{
1286 return vmcs_config.cpu_based_2nd_exec_ctrl &
1287 SECONDARY_EXEC_WBINVD_EXITING;
1288}
1289
Abel Gordonabc4fc52013-04-18 14:35:25 +03001290static inline bool cpu_has_vmx_shadow_vmcs(void)
1291{
1292 u64 vmx_msr;
1293 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1294 /* check if the cpu supports writing r/o exit information fields */
1295 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1296 return false;
1297
1298 return vmcs_config.cpu_based_2nd_exec_ctrl &
1299 SECONDARY_EXEC_SHADOW_VMCS;
1300}
1301
Kai Huang843e4332015-01-28 10:54:28 +08001302static inline bool cpu_has_vmx_pml(void)
1303{
1304 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1305}
1306
Haozhong Zhang64903d62015-10-20 15:39:09 +08001307static inline bool cpu_has_vmx_tsc_scaling(void)
1308{
1309 return vmcs_config.cpu_based_2nd_exec_ctrl &
1310 SECONDARY_EXEC_TSC_SCALING;
1311}
1312
Sheng Yang04547152009-04-01 15:52:31 +08001313static inline bool report_flexpriority(void)
1314{
1315 return flexpriority_enabled;
1316}
1317
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001318static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1319{
1320 return vmcs12->cpu_based_vm_exec_control & bit;
1321}
1322
1323static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1324{
1325 return (vmcs12->cpu_based_vm_exec_control &
1326 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1327 (vmcs12->secondary_vm_exec_control & bit);
1328}
1329
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001330static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001331{
1332 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1333}
1334
Jan Kiszkaf41245002014-03-07 20:03:13 +01001335static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1336{
1337 return vmcs12->pin_based_vm_exec_control &
1338 PIN_BASED_VMX_PREEMPTION_TIMER;
1339}
1340
Nadav Har'El155a97a2013-08-05 11:07:16 +03001341static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1342{
1343 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1344}
1345
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001346static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1347{
1348 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1349 vmx_xsaves_supported();
1350}
1351
Wincy Vanf2b93282015-02-03 23:56:03 +08001352static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1353{
1354 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1355}
1356
Wanpeng Li5c614b32015-10-13 09:18:36 -07001357static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1358{
1359 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1360}
1361
Wincy Van82f0dd42015-02-03 23:57:18 +08001362static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1363{
1364 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1365}
1366
Wincy Van608406e2015-02-03 23:57:51 +08001367static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1368{
1369 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1370}
1371
Wincy Van705699a2015-02-03 23:58:17 +08001372static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1373{
1374 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1375}
1376
Jim Mattsonef85b672016-12-12 11:01:37 -08001377static inline bool is_nmi(u32 intr_info)
Nadav Har'El644d7112011-05-25 23:12:35 +03001378{
1379 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
Jim Mattsonef85b672016-12-12 11:01:37 -08001380 == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK);
Nadav Har'El644d7112011-05-25 23:12:35 +03001381}
1382
Jan Kiszka533558b2014-01-04 18:47:20 +01001383static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1384 u32 exit_intr_info,
1385 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001386static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1387 struct vmcs12 *vmcs12,
1388 u32 reason, unsigned long qualification);
1389
Rusty Russell8b9cf982007-07-30 16:31:43 +10001390static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001391{
1392 int i;
1393
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001394 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001395 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001396 return i;
1397 return -1;
1398}
1399
Sheng Yang2384d2b2008-01-17 15:14:33 +08001400static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1401{
1402 struct {
1403 u64 vpid : 16;
1404 u64 rsvd : 48;
1405 u64 gva;
1406 } operand = { vpid, 0, gva };
1407
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001408 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001409 /* CF==1 or ZF==1 --> rc = -1 */
1410 "; ja 1f ; ud2 ; 1:"
1411 : : "a"(&operand), "c"(ext) : "cc", "memory");
1412}
1413
Sheng Yang14394422008-04-28 12:24:45 +08001414static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1415{
1416 struct {
1417 u64 eptp, gpa;
1418 } operand = {eptp, gpa};
1419
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001420 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001421 /* CF==1 or ZF==1 --> rc = -1 */
1422 "; ja 1f ; ud2 ; 1:\n"
1423 : : "a" (&operand), "c" (ext) : "cc", "memory");
1424}
1425
Avi Kivity26bb0982009-09-07 11:14:12 +03001426static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001427{
1428 int i;
1429
Rusty Russell8b9cf982007-07-30 16:31:43 +10001430 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001431 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001432 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001433 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001434}
1435
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436static void vmcs_clear(struct vmcs *vmcs)
1437{
1438 u64 phys_addr = __pa(vmcs);
1439 u8 error;
1440
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001441 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001442 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443 : "cc", "memory");
1444 if (error)
1445 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1446 vmcs, phys_addr);
1447}
1448
Nadav Har'Eld462b812011-05-24 15:26:10 +03001449static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1450{
1451 vmcs_clear(loaded_vmcs->vmcs);
Jim Mattson355f4fb2016-10-28 08:29:39 -07001452 if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1453 vmcs_clear(loaded_vmcs->shadow_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001454 loaded_vmcs->cpu = -1;
1455 loaded_vmcs->launched = 0;
1456}
1457
Dongxiao Xu7725b892010-05-11 18:29:38 +08001458static void vmcs_load(struct vmcs *vmcs)
1459{
1460 u64 phys_addr = __pa(vmcs);
1461 u8 error;
1462
1463 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001464 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001465 : "cc", "memory");
1466 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001467 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001468 vmcs, phys_addr);
1469}
1470
Dave Young2965faa2015-09-09 15:38:55 -07001471#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001472/*
1473 * This bitmap is used to indicate whether the vmclear
1474 * operation is enabled on all cpus. All disabled by
1475 * default.
1476 */
1477static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1478
1479static inline void crash_enable_local_vmclear(int cpu)
1480{
1481 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1482}
1483
1484static inline void crash_disable_local_vmclear(int cpu)
1485{
1486 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1487}
1488
1489static inline int crash_local_vmclear_enabled(int cpu)
1490{
1491 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1492}
1493
1494static void crash_vmclear_local_loaded_vmcss(void)
1495{
1496 int cpu = raw_smp_processor_id();
1497 struct loaded_vmcs *v;
1498
1499 if (!crash_local_vmclear_enabled(cpu))
1500 return;
1501
1502 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1503 loaded_vmcss_on_cpu_link)
1504 vmcs_clear(v->vmcs);
1505}
1506#else
1507static inline void crash_enable_local_vmclear(int cpu) { }
1508static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001509#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001510
Nadav Har'Eld462b812011-05-24 15:26:10 +03001511static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001513 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001514 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515
Nadav Har'Eld462b812011-05-24 15:26:10 +03001516 if (loaded_vmcs->cpu != cpu)
1517 return; /* vcpu migration can race with cpu offline */
1518 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001520 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001521 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001522
1523 /*
1524 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1525 * is before setting loaded_vmcs->vcpu to -1 which is done in
1526 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1527 * then adds the vmcs into percpu list before it is deleted.
1528 */
1529 smp_wmb();
1530
Nadav Har'Eld462b812011-05-24 15:26:10 +03001531 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001532 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533}
1534
Nadav Har'Eld462b812011-05-24 15:26:10 +03001535static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001536{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001537 int cpu = loaded_vmcs->cpu;
1538
1539 if (cpu != -1)
1540 smp_call_function_single(cpu,
1541 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001542}
1543
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001544static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001545{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001546 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001547 return;
1548
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001549 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001550 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001551}
1552
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001553static inline void vpid_sync_vcpu_global(void)
1554{
1555 if (cpu_has_vmx_invvpid_global())
1556 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1557}
1558
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001559static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001560{
1561 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001562 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001563 else
1564 vpid_sync_vcpu_global();
1565}
1566
Sheng Yang14394422008-04-28 12:24:45 +08001567static inline void ept_sync_global(void)
1568{
1569 if (cpu_has_vmx_invept_global())
1570 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1571}
1572
1573static inline void ept_sync_context(u64 eptp)
1574{
Avi Kivity089d0342009-03-23 18:26:32 +02001575 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001576 if (cpu_has_vmx_invept_context())
1577 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1578 else
1579 ept_sync_global();
1580 }
1581}
1582
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001583static __always_inline void vmcs_check16(unsigned long field)
1584{
1585 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1586 "16-bit accessor invalid for 64-bit field");
1587 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1588 "16-bit accessor invalid for 64-bit high field");
1589 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1590 "16-bit accessor invalid for 32-bit high field");
1591 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1592 "16-bit accessor invalid for natural width field");
1593}
1594
1595static __always_inline void vmcs_check32(unsigned long field)
1596{
1597 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1598 "32-bit accessor invalid for 16-bit field");
1599 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1600 "32-bit accessor invalid for natural width field");
1601}
1602
1603static __always_inline void vmcs_check64(unsigned long field)
1604{
1605 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1606 "64-bit accessor invalid for 16-bit field");
1607 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1608 "64-bit accessor invalid for 64-bit high field");
1609 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1610 "64-bit accessor invalid for 32-bit field");
1611 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000,
1612 "64-bit accessor invalid for natural width field");
1613}
1614
1615static __always_inline void vmcs_checkl(unsigned long field)
1616{
1617 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0,
1618 "Natural width accessor invalid for 16-bit field");
1619 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000,
1620 "Natural width accessor invalid for 64-bit field");
1621 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001,
1622 "Natural width accessor invalid for 64-bit high field");
1623 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000,
1624 "Natural width accessor invalid for 32-bit field");
1625}
1626
1627static __always_inline unsigned long __vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628{
Avi Kivity5e520e62011-05-15 10:13:12 -04001629 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001630
Avi Kivity5e520e62011-05-15 10:13:12 -04001631 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1632 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001633 return value;
1634}
1635
Avi Kivity96304212011-05-15 10:13:13 -04001636static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001637{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001638 vmcs_check16(field);
1639 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640}
1641
Avi Kivity96304212011-05-15 10:13:13 -04001642static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001644 vmcs_check32(field);
1645 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646}
1647
Avi Kivity96304212011-05-15 10:13:13 -04001648static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001649{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001650 vmcs_check64(field);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001651#ifdef CONFIG_X86_64
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001652 return __vmcs_readl(field);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653#else
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001654 return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655#endif
1656}
1657
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001658static __always_inline unsigned long vmcs_readl(unsigned long field)
1659{
1660 vmcs_checkl(field);
1661 return __vmcs_readl(field);
1662}
1663
Avi Kivitye52de1b2007-01-05 16:36:56 -08001664static noinline void vmwrite_error(unsigned long field, unsigned long value)
1665{
1666 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1667 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1668 dump_stack();
1669}
1670
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001671static __always_inline void __vmcs_writel(unsigned long field, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001672{
1673 u8 error;
1674
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001675 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001676 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001677 if (unlikely(error))
1678 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679}
1680
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001681static __always_inline void vmcs_write16(unsigned long field, u16 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001683 vmcs_check16(field);
1684 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685}
1686
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001687static __always_inline void vmcs_write32(unsigned long field, u32 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001689 vmcs_check32(field);
1690 __vmcs_writel(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691}
1692
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001693static __always_inline void vmcs_write64(unsigned long field, u64 value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001695 vmcs_check64(field);
1696 __vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001697#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698 asm volatile ("");
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001699 __vmcs_writel(field+1, value >> 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700#endif
1701}
1702
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001703static __always_inline void vmcs_writel(unsigned long field, unsigned long value)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001704{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001705 vmcs_checkl(field);
1706 __vmcs_writel(field, value);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001707}
1708
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001709static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask)
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001710{
Paolo Bonzini8a86aea92015-12-03 15:56:55 +01001711 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1712 "vmcs_clear_bits does not support 64-bit fields");
1713 __vmcs_writel(field, __vmcs_readl(field) & ~mask);
1714}
1715
1716static __always_inline void vmcs_set_bits(unsigned long field, u32 mask)
1717{
1718 BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000,
1719 "vmcs_set_bits does not support 64-bit fields");
1720 __vmcs_writel(field, __vmcs_readl(field) | mask);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001721}
1722
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001723static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx)
1724{
1725 vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS);
1726}
1727
Gleb Natapov2961e8762013-11-25 15:37:13 +02001728static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1729{
1730 vmcs_write32(VM_ENTRY_CONTROLS, val);
1731 vmx->vm_entry_controls_shadow = val;
1732}
1733
1734static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1735{
1736 if (vmx->vm_entry_controls_shadow != val)
1737 vm_entry_controls_init(vmx, val);
1738}
1739
1740static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1741{
1742 return vmx->vm_entry_controls_shadow;
1743}
1744
1745
1746static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1747{
1748 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1749}
1750
1751static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1752{
1753 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1754}
1755
Paolo Bonzini8391ce42016-07-07 14:58:33 +02001756static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx)
1757{
1758 vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS);
1759}
1760
Gleb Natapov2961e8762013-11-25 15:37:13 +02001761static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1762{
1763 vmcs_write32(VM_EXIT_CONTROLS, val);
1764 vmx->vm_exit_controls_shadow = val;
1765}
1766
1767static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1768{
1769 if (vmx->vm_exit_controls_shadow != val)
1770 vm_exit_controls_init(vmx, val);
1771}
1772
1773static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1774{
1775 return vmx->vm_exit_controls_shadow;
1776}
1777
1778
1779static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1780{
1781 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1782}
1783
1784static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1785{
1786 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1787}
1788
Avi Kivity2fb92db2011-04-27 19:42:18 +03001789static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1790{
1791 vmx->segment_cache.bitmask = 0;
1792}
1793
1794static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1795 unsigned field)
1796{
1797 bool ret;
1798 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1799
1800 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1801 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1802 vmx->segment_cache.bitmask = 0;
1803 }
1804 ret = vmx->segment_cache.bitmask & mask;
1805 vmx->segment_cache.bitmask |= mask;
1806 return ret;
1807}
1808
1809static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1810{
1811 u16 *p = &vmx->segment_cache.seg[seg].selector;
1812
1813 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1814 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1815 return *p;
1816}
1817
1818static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1819{
1820 ulong *p = &vmx->segment_cache.seg[seg].base;
1821
1822 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1823 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1824 return *p;
1825}
1826
1827static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1828{
1829 u32 *p = &vmx->segment_cache.seg[seg].limit;
1830
1831 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1832 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1833 return *p;
1834}
1835
1836static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1837{
1838 u32 *p = &vmx->segment_cache.seg[seg].ar;
1839
1840 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1841 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1842 return *p;
1843}
1844
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001845static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1846{
1847 u32 eb;
1848
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001849 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001850 (1u << DB_VECTOR) | (1u << AC_VECTOR);
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001851 if ((vcpu->guest_debug &
1852 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1853 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1854 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001855 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001856 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001857 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001858 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001859
1860 /* When we are running a nested L2 guest and L1 specified for it a
1861 * certain exception bitmap, we must trap the same exceptions and pass
1862 * them to L1. When running L2, we will only handle the exceptions
1863 * specified above if L1 did not want them.
1864 */
1865 if (is_guest_mode(vcpu))
1866 eb |= get_vmcs12(vcpu)->exception_bitmap;
1867
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001868 vmcs_write32(EXCEPTION_BITMAP, eb);
1869}
1870
Gleb Natapov2961e8762013-11-25 15:37:13 +02001871static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1872 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001873{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001874 vm_entry_controls_clearbit(vmx, entry);
1875 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001876}
1877
Avi Kivity61d2ef22010-04-28 16:40:38 +03001878static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1879{
1880 unsigned i;
1881 struct msr_autoload *m = &vmx->msr_autoload;
1882
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001883 switch (msr) {
1884 case MSR_EFER:
1885 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001886 clear_atomic_switch_msr_special(vmx,
1887 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001888 VM_EXIT_LOAD_IA32_EFER);
1889 return;
1890 }
1891 break;
1892 case MSR_CORE_PERF_GLOBAL_CTRL:
1893 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001894 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001895 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1896 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1897 return;
1898 }
1899 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001900 }
1901
Avi Kivity61d2ef22010-04-28 16:40:38 +03001902 for (i = 0; i < m->nr; ++i)
1903 if (m->guest[i].index == msr)
1904 break;
1905
1906 if (i == m->nr)
1907 return;
1908 --m->nr;
1909 m->guest[i] = m->guest[m->nr];
1910 m->host[i] = m->host[m->nr];
1911 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1912 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1913}
1914
Gleb Natapov2961e8762013-11-25 15:37:13 +02001915static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1916 unsigned long entry, unsigned long exit,
1917 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1918 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001919{
1920 vmcs_write64(guest_val_vmcs, guest_val);
1921 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001922 vm_entry_controls_setbit(vmx, entry);
1923 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001924}
1925
Avi Kivity61d2ef22010-04-28 16:40:38 +03001926static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1927 u64 guest_val, u64 host_val)
1928{
1929 unsigned i;
1930 struct msr_autoload *m = &vmx->msr_autoload;
1931
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001932 switch (msr) {
1933 case MSR_EFER:
1934 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001935 add_atomic_switch_msr_special(vmx,
1936 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001937 VM_EXIT_LOAD_IA32_EFER,
1938 GUEST_IA32_EFER,
1939 HOST_IA32_EFER,
1940 guest_val, host_val);
1941 return;
1942 }
1943 break;
1944 case MSR_CORE_PERF_GLOBAL_CTRL:
1945 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001946 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001947 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1948 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1949 GUEST_IA32_PERF_GLOBAL_CTRL,
1950 HOST_IA32_PERF_GLOBAL_CTRL,
1951 guest_val, host_val);
1952 return;
1953 }
1954 break;
Radim Krčmář7099e2e2016-03-04 15:08:42 +01001955 case MSR_IA32_PEBS_ENABLE:
1956 /* PEBS needs a quiescent period after being disabled (to write
1957 * a record). Disabling PEBS through VMX MSR swapping doesn't
1958 * provide that period, so a CPU could write host's record into
1959 * guest's memory.
1960 */
1961 wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
Avi Kivity110312c2010-12-21 12:54:20 +02001962 }
1963
Avi Kivity61d2ef22010-04-28 16:40:38 +03001964 for (i = 0; i < m->nr; ++i)
1965 if (m->guest[i].index == msr)
1966 break;
1967
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001968 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001969 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001970 "Can't add msr %x\n", msr);
1971 return;
1972 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001973 ++m->nr;
1974 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1975 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1976 }
1977
1978 m->guest[i].index = msr;
1979 m->guest[i].value = guest_val;
1980 m->host[i].index = msr;
1981 m->host[i].value = host_val;
1982}
1983
Avi Kivity92c0d902009-10-29 11:00:16 +02001984static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001985{
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001986 u64 guest_efer = vmx->vcpu.arch.efer;
1987 u64 ignore_bits = 0;
Eddie Dong2cc51562007-05-21 07:28:09 +03001988
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01001989 if (!enable_ept) {
1990 /*
1991 * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing
1992 * host CPUID is more efficient than testing guest CPUID
1993 * or CR4. Host SMEP is anyway a requirement for guest SMEP.
1994 */
1995 if (boot_cpu_has(X86_FEATURE_SMEP))
1996 guest_efer |= EFER_NX;
1997 else if (!(guest_efer & EFER_NX))
1998 ignore_bits |= EFER_NX;
1999 }
Roel Kluin3a34a882009-08-04 02:08:45 -07002000
Avi Kivity51c6cf62007-08-29 03:48:05 +03002001 /*
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002002 * LMA and LME handled by hardware; SCE meaningless outside long mode.
Avi Kivity51c6cf62007-08-29 03:48:05 +03002003 */
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002004 ignore_bits |= EFER_SCE;
Avi Kivity51c6cf62007-08-29 03:48:05 +03002005#ifdef CONFIG_X86_64
2006 ignore_bits |= EFER_LMA | EFER_LME;
2007 /* SCE is meaningful only in long mode on Intel */
2008 if (guest_efer & EFER_LMA)
2009 ignore_bits &= ~(u64)EFER_SCE;
2010#endif
Avi Kivity84ad33e2010-04-28 16:42:29 +03002011
2012 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08002013
2014 /*
2015 * On EPT, we can't emulate NX, so we must switch EFER atomically.
2016 * On CPUs that support "load IA32_EFER", always switch EFER
2017 * atomically, since it's faster than switching it manually.
2018 */
2019 if (cpu_has_load_ia32_efer ||
2020 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03002021 if (!(guest_efer & EFER_LMA))
2022 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08002023 if (guest_efer != host_efer)
2024 add_atomic_switch_msr(vmx, MSR_EFER,
2025 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03002026 return false;
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002027 } else {
2028 guest_efer &= ~ignore_bits;
2029 guest_efer |= host_efer & ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03002030
Paolo Bonzini844a5fe2016-03-08 12:13:39 +01002031 vmx->guest_msrs[efer_offset].data = guest_efer;
2032 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
2033
2034 return true;
2035 }
Avi Kivity51c6cf62007-08-29 03:48:05 +03002036}
2037
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002038#ifdef CONFIG_X86_32
2039/*
2040 * On 32-bit kernels, VM exits still load the FS and GS bases from the
2041 * VMCS rather than the segment table. KVM uses this helper to figure
2042 * out the current bases to poke them into the VMCS before entry.
2043 */
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002044static unsigned long segment_base(u16 selector)
2045{
Christoph Lameter89cbc762014-08-17 12:30:40 -05002046 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002047 struct desc_struct *table;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002048 unsigned long v;
2049
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002050 if (!(selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002051 return 0;
2052
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002053 table = (struct desc_struct *)gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002054
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002055 if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002056 u16 ldt_selector = kvm_read_ldt();
2057
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002058 if (!(ldt_selector & ~SEGMENT_RPL_MASK))
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002059 return 0;
2060
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002061 table = (struct desc_struct *)segment_base(ldt_selector);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002062 }
Andy Lutomirski8c2e41f2017-02-20 08:56:12 -08002063 v = get_desc_base(&table[selector >> 3]);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002064 return v;
2065}
Andy Lutomirskie28baea2017-02-20 08:56:11 -08002066#endif
Gleb Natapov2d49ec72010-02-25 12:43:09 +02002067
Avi Kivity04d2cc72007-09-10 18:10:54 +03002068static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03002069{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002070 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002071 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03002072
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002073 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002074 return;
2075
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002076 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002077 /*
2078 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2079 * allow segment selectors with cpl > 0 or ti == 1.
2080 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002081 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02002082 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02002083 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002084 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002085 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002086 vmx->host_state.fs_reload_needed = 0;
2087 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03002088 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002089 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002090 }
Avi Kivity9581d442010-10-19 16:46:55 +02002091 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002092 if (!(vmx->host_state.gs_sel & 7))
2093 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002094 else {
2095 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02002096 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03002097 }
2098
2099#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03002100 savesegment(ds, vmx->host_state.ds_sel);
2101 savesegment(es, vmx->host_state.es_sel);
2102#endif
2103
2104#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03002105 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
2106 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
2107#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002108 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
2109 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03002110#endif
Avi Kivity707c0872007-05-02 17:33:43 +03002111
2112#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002113 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2114 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03002115 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03002116#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002117 if (boot_cpu_has(X86_FEATURE_MPX))
2118 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03002119 for (i = 0; i < vmx->save_nmsrs; ++i)
2120 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02002121 vmx->guest_msrs[i].data,
2122 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03002123}
2124
Avi Kivitya9b21b62008-06-24 11:48:49 +03002125static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03002126{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002127 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03002128 return;
2129
Avi Kivitye1beb1d2007-11-18 13:50:24 +02002130 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002131 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02002132#ifdef CONFIG_X86_64
2133 if (is_long_mode(&vmx->vcpu))
2134 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2135#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02002136 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002137 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002138#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02002139 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02002140#else
2141 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03002142#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03002143 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02002144 if (vmx->host_state.fs_reload_needed)
2145 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03002146#ifdef CONFIG_X86_64
2147 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
2148 loadsegment(ds, vmx->host_state.ds_sel);
2149 loadsegment(es, vmx->host_state.es_sel);
2150 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03002151#endif
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002152 invalidate_tss_limit();
Avi Kivity44ea2b12009-09-06 15:55:37 +03002153#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02002154 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03002155#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002156 if (vmx->host_state.msr_host_bndcfgs)
2157 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Christoph Lameter89cbc762014-08-17 12:30:40 -05002158 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03002159}
2160
Avi Kivitya9b21b62008-06-24 11:48:49 +03002161static void vmx_load_host_state(struct vcpu_vmx *vmx)
2162{
2163 preempt_disable();
2164 __vmx_load_host_state(vmx);
2165 preempt_enable();
2166}
2167
Feng Wu28b835d2015-09-18 22:29:54 +08002168static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2169{
2170 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2171 struct pi_desc old, new;
2172 unsigned int dest;
2173
2174 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002175 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2176 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002177 return;
2178
2179 do {
2180 old.control = new.control = pi_desc->control;
2181
2182 /*
2183 * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there
2184 * are two possible cases:
2185 * 1. After running 'pre_block', context switch
2186 * happened. For this case, 'sn' was set in
2187 * vmx_vcpu_put(), so we need to clear it here.
2188 * 2. After running 'pre_block', we were blocked,
2189 * and woken up by some other guy. For this case,
2190 * we don't need to do anything, 'pi_post_block'
2191 * will do everything for us. However, we cannot
2192 * check whether it is case #1 or case #2 here
2193 * (maybe, not needed), so we also clear sn here,
2194 * I think it is not a big deal.
2195 */
2196 if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) {
2197 if (vcpu->cpu != cpu) {
2198 dest = cpu_physical_id(cpu);
2199
2200 if (x2apic_enabled())
2201 new.ndst = dest;
2202 else
2203 new.ndst = (dest << 8) & 0xFF00;
2204 }
2205
2206 /* set 'NV' to 'notification vector' */
2207 new.nv = POSTED_INTR_VECTOR;
2208 }
2209
2210 /* Allow posting non-urgent interrupts */
2211 new.sn = 0;
2212 } while (cmpxchg(&pi_desc->control, old.control,
2213 new.control) != old.control);
2214}
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002215
Peter Feinerc95ba922016-08-17 09:36:47 -07002216static void decache_tsc_multiplier(struct vcpu_vmx *vmx)
2217{
2218 vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio;
2219 vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio);
2220}
2221
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222/*
2223 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2224 * vcpu mutex is already taken.
2225 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002226static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002228 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002229 bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002231 if (!already_loaded) {
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01002232 loaded_vmcs_clear(vmx->loaded_vmcs);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002233 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002234 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002235
2236 /*
2237 * Read loaded_vmcs->cpu should be before fetching
2238 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2239 * See the comments in __loaded_vmcs_clear().
2240 */
2241 smp_rmb();
2242
Nadav Har'Eld462b812011-05-24 15:26:10 +03002243 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2244 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002245 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002246 local_irq_enable();
Jim Mattsonb80c76e2016-07-29 18:56:53 -07002247 }
2248
2249 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2250 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2251 vmcs_load(vmx->loaded_vmcs->vmcs);
2252 }
2253
2254 if (!already_loaded) {
2255 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
2256 unsigned long sysenter_esp;
2257
2258 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002259
Avi Kivity6aa8b732006-12-10 02:21:36 -08002260 /*
2261 * Linux uses per-cpu TSS and GDT, so set these when switching
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002262 * processors. See 22.2.4.
Avi Kivity6aa8b732006-12-10 02:21:36 -08002263 */
Andy Lutomirskie0c23062017-02-20 08:56:10 -08002264 vmcs_writel(HOST_TR_BASE,
2265 (unsigned long)this_cpu_ptr(&cpu_tss));
2266 vmcs_writel(HOST_GDTR_BASE, gdt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267
Andy Lutomirskib7ffc442017-02-20 08:56:14 -08002268 /*
2269 * VM exits change the host TR limit to 0x67 after a VM
2270 * exit. This is okay, since 0x67 covers everything except
2271 * the IO bitmap and have have code to handle the IO bitmap
2272 * being lost after a VM exit.
2273 */
2274 BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67);
2275
Avi Kivity6aa8b732006-12-10 02:21:36 -08002276 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2277 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002278
Nadav Har'Eld462b812011-05-24 15:26:10 +03002279 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280 }
Feng Wu28b835d2015-09-18 22:29:54 +08002281
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002282 /* Setup TSC multiplier */
2283 if (kvm_has_tsc_control &&
Peter Feinerc95ba922016-08-17 09:36:47 -07002284 vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2285 decache_tsc_multiplier(vmx);
Owen Hofmann2680d6d2016-03-01 13:36:13 -08002286
Feng Wu28b835d2015-09-18 22:29:54 +08002287 vmx_vcpu_pi_load(vcpu, cpu);
Xiao Guangrong1be0e612016-03-22 16:51:18 +08002288 vmx->host_pkru = read_pkru();
Feng Wu28b835d2015-09-18 22:29:54 +08002289}
2290
2291static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2292{
2293 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2294
2295 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +08002296 !irq_remapping_cap(IRQ_POSTING_CAP) ||
2297 !kvm_vcpu_apicv_active(vcpu))
Feng Wu28b835d2015-09-18 22:29:54 +08002298 return;
2299
2300 /* Set SN when the vCPU is preempted */
2301 if (vcpu->preempted)
2302 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303}
2304
2305static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2306{
Feng Wu28b835d2015-09-18 22:29:54 +08002307 vmx_vcpu_pi_put(vcpu);
2308
Avi Kivitya9b21b62008-06-24 11:48:49 +03002309 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310}
2311
Avi Kivityedcafe32009-12-30 18:07:40 +02002312static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2313
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002314/*
2315 * Return the cr0 value that a nested guest would read. This is a combination
2316 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2317 * its hypervisor (cr0_read_shadow).
2318 */
2319static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2320{
2321 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2322 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2323}
2324static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2325{
2326 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2327 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2328}
2329
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2331{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002332 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002333
Avi Kivity6de12732011-03-07 12:51:22 +02002334 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2335 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2336 rflags = vmcs_readl(GUEST_RFLAGS);
2337 if (to_vmx(vcpu)->rmode.vm86_active) {
2338 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2339 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2340 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2341 }
2342 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002343 }
Avi Kivity6de12732011-03-07 12:51:22 +02002344 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345}
2346
2347static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2348{
Avi Kivity6de12732011-03-07 12:51:22 +02002349 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2350 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002351 if (to_vmx(vcpu)->rmode.vm86_active) {
2352 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002353 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002354 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355 vmcs_writel(GUEST_RFLAGS, rflags);
2356}
2357
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08002358static u32 vmx_get_pkru(struct kvm_vcpu *vcpu)
2359{
2360 return to_vmx(vcpu)->guest_pkru;
2361}
2362
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002363static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002364{
2365 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2366 int ret = 0;
2367
2368 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002369 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002370 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002371 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002372
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002373 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002374}
2375
2376static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2377{
2378 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2379 u32 interruptibility = interruptibility_old;
2380
2381 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2382
Jan Kiszka48005f62010-02-19 19:38:07 +01002383 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002384 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002385 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002386 interruptibility |= GUEST_INTR_STATE_STI;
2387
2388 if ((interruptibility != interruptibility_old))
2389 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2390}
2391
Avi Kivity6aa8b732006-12-10 02:21:36 -08002392static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2393{
2394 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002396 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002398 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399
Glauber Costa2809f5d2009-05-12 16:21:05 -04002400 /* skipping an emulated instruction also counts */
2401 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402}
2403
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002404/*
2405 * KVM wants to inject page-faults which it got to the guest. This function
2406 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002407 */
Gleb Natapove011c662013-09-25 12:51:35 +03002408static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002409{
2410 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2411
Gleb Natapove011c662013-09-25 12:51:35 +03002412 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002413 return 0;
2414
Jan Kiszka533558b2014-01-04 18:47:20 +01002415 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
2416 vmcs_read32(VM_EXIT_INTR_INFO),
2417 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002418 return 1;
2419}
2420
Avi Kivity298101d2007-11-25 13:41:11 +02002421static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002422 bool has_error_code, u32 error_code,
2423 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002424{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002425 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002426 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002427
Gleb Natapove011c662013-09-25 12:51:35 +03002428 if (!reinject && is_guest_mode(vcpu) &&
2429 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002430 return;
2431
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002432 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002433 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002434 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2435 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002436
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002437 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002438 int inc_eip = 0;
2439 if (kvm_exception_is_soft(nr))
2440 inc_eip = vcpu->arch.event_exit_inst_len;
2441 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002442 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002443 return;
2444 }
2445
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002446 if (kvm_exception_is_soft(nr)) {
2447 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2448 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002449 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2450 } else
2451 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2452
2453 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002454}
2455
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002456static bool vmx_rdtscp_supported(void)
2457{
2458 return cpu_has_vmx_rdtscp();
2459}
2460
Mao, Junjiead756a12012-07-02 01:18:48 +00002461static bool vmx_invpcid_supported(void)
2462{
2463 return cpu_has_vmx_invpcid() && enable_ept;
2464}
2465
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466/*
Eddie Donga75beee2007-05-17 18:55:15 +03002467 * Swap MSR entry in host/guest MSR entry array.
2468 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002469static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002470{
Avi Kivity26bb0982009-09-07 11:14:12 +03002471 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002472
2473 tmp = vmx->guest_msrs[to];
2474 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2475 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002476}
2477
Yang Zhang8d146952013-01-25 10:18:50 +08002478static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2479{
2480 unsigned long *msr_bitmap;
2481
Wincy Van670125b2015-03-04 14:31:56 +08002482 if (is_guest_mode(vcpu))
Radim Krčmářd048c092016-08-08 20:16:22 +02002483 msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap;
Roman Kagan3ce424e2016-05-18 17:48:20 +03002484 else if (cpu_has_secondary_exec_ctrls() &&
2485 (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) &
2486 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002487 if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) {
2488 if (is_long_mode(vcpu))
Wanpeng Lic63e4562016-09-23 19:17:16 +08002489 msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv;
2490 else
2491 msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv;
2492 } else {
2493 if (is_long_mode(vcpu))
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002494 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2495 else
2496 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
Wanpeng Lif6e90f92016-09-22 07:43:25 +08002497 }
Yang Zhang8d146952013-01-25 10:18:50 +08002498 } else {
2499 if (is_long_mode(vcpu))
2500 msr_bitmap = vmx_msr_bitmap_longmode;
2501 else
2502 msr_bitmap = vmx_msr_bitmap_legacy;
2503 }
2504
2505 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2506}
2507
Eddie Donga75beee2007-05-17 18:55:15 +03002508/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002509 * Set up the vmcs to automatically save and restore system
2510 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2511 * mode, as fiddling with msrs is very expensive.
2512 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002513static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002514{
Avi Kivity26bb0982009-09-07 11:14:12 +03002515 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002516
Eddie Donga75beee2007-05-17 18:55:15 +03002517 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002518#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002519 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002520 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002521 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002522 move_msr_up(vmx, index, save_nmsrs++);
2523 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002524 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002525 move_msr_up(vmx, index, save_nmsrs++);
2526 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002527 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002528 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002529 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002530 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002531 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002532 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002533 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002534 * if efer.sce is enabled.
2535 */
Brian Gerst8c065852010-07-17 09:03:26 -04002536 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002537 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002538 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002539 }
Eddie Donga75beee2007-05-17 18:55:15 +03002540#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002541 index = __find_msr_index(vmx, MSR_EFER);
2542 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002543 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002544
Avi Kivity26bb0982009-09-07 11:14:12 +03002545 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002546
Yang Zhang8d146952013-01-25 10:18:50 +08002547 if (cpu_has_vmx_msr_bitmap())
2548 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002549}
2550
2551/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002553 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2554 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002556static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557{
2558 u64 host_tsc, tsc_offset;
2559
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002560 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002562 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002563}
2564
2565/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002566 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002568static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002570 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002571 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002572 * We're here if L1 chose not to trap WRMSR to TSC. According
2573 * to the spec, this should set L1's TSC; The offset that L1
2574 * set for L2 remains unchanged, and still needs to be added
2575 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002576 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002577 struct vmcs12 *vmcs12;
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002578 /* recalculate vmcs02.TSC_OFFSET: */
2579 vmcs12 = get_vmcs12(vcpu);
2580 vmcs_write64(TSC_OFFSET, offset +
2581 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2582 vmcs12->tsc_offset : 0));
2583 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002584 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2585 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002586 vmcs_write64(TSC_OFFSET, offset);
2587 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588}
2589
Nadav Har'El801d3422011-05-25 23:02:23 +03002590static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2591{
2592 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2593 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2594}
2595
2596/*
2597 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2598 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2599 * all guests if the "nested" module option is off, and can also be disabled
2600 * for a single guest by disabling its VMX cpuid bit.
2601 */
2602static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2603{
2604 return nested && guest_cpuid_has_vmx(vcpu);
2605}
2606
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002608 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2609 * returned for the various VMX controls MSRs when nested VMX is enabled.
2610 * The same values should also be used to verify that vmcs12 control fields are
2611 * valid during nested entry from L1 to L2.
2612 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2613 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2614 * bit in the high half is on if the corresponding bit in the control field
2615 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002616 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002617static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002618{
2619 /*
2620 * Note that as a general rule, the high half of the MSRs (bits in
2621 * the control fields which may be 1) should be initialized by the
2622 * intersection of the underlying hardware's MSR (i.e., features which
2623 * can be supported) and the list of features we want to expose -
2624 * because they are known to be properly supported in our code.
2625 * Also, usually, the low half of the MSRs (bits which must be 1) can
2626 * be set to 0, meaning that L1 may turn off any of these bits. The
2627 * reason is that if one of these bits is necessary, it will appear
2628 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2629 * fields of vmcs01 and vmcs02, will turn these bits off - and
2630 * nested_vmx_exit_handled() will not pass related exits to L1.
2631 * These rules have exceptions below.
2632 */
2633
2634 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002635 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002636 vmx->nested.nested_vmx_pinbased_ctls_low,
2637 vmx->nested.nested_vmx_pinbased_ctls_high);
2638 vmx->nested.nested_vmx_pinbased_ctls_low |=
2639 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2640 vmx->nested.nested_vmx_pinbased_ctls_high &=
2641 PIN_BASED_EXT_INTR_MASK |
2642 PIN_BASED_NMI_EXITING |
2643 PIN_BASED_VIRTUAL_NMIS;
2644 vmx->nested.nested_vmx_pinbased_ctls_high |=
2645 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002646 PIN_BASED_VMX_PREEMPTION_TIMER;
Andrey Smetanind62caab2015-11-10 15:36:33 +03002647 if (kvm_vcpu_apicv_active(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002648 vmx->nested.nested_vmx_pinbased_ctls_high |=
2649 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002650
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002651 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002652 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002653 vmx->nested.nested_vmx_exit_ctls_low,
2654 vmx->nested.nested_vmx_exit_ctls_high);
2655 vmx->nested.nested_vmx_exit_ctls_low =
2656 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002657
Wincy Vanb9c237b2015-02-03 23:56:30 +08002658 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002659#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002660 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002661#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002662 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002663 vmx->nested.nested_vmx_exit_ctls_high |=
2664 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002665 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002666 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2667
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002668 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002669 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002670
Jan Kiszka2996fca2014-06-16 13:59:43 +02002671 /* We support free control of debug control saving. */
David Matlack0115f9c2016-11-29 18:14:06 -08002672 vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002673
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002674 /* entry controls */
2675 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002676 vmx->nested.nested_vmx_entry_ctls_low,
2677 vmx->nested.nested_vmx_entry_ctls_high);
2678 vmx->nested.nested_vmx_entry_ctls_low =
2679 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2680 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002681#ifdef CONFIG_X86_64
2682 VM_ENTRY_IA32E_MODE |
2683#endif
2684 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002685 vmx->nested.nested_vmx_entry_ctls_high |=
2686 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzinia87036a2016-03-08 09:52:13 +01002687 if (kvm_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002688 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002689
Jan Kiszka2996fca2014-06-16 13:59:43 +02002690 /* We support free control of debug control loading. */
David Matlack0115f9c2016-11-29 18:14:06 -08002691 vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002692
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002693 /* cpu-based controls */
2694 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002695 vmx->nested.nested_vmx_procbased_ctls_low,
2696 vmx->nested.nested_vmx_procbased_ctls_high);
2697 vmx->nested.nested_vmx_procbased_ctls_low =
2698 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2699 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002700 CPU_BASED_VIRTUAL_INTR_PENDING |
2701 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002702 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2703 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2704 CPU_BASED_CR3_STORE_EXITING |
2705#ifdef CONFIG_X86_64
2706 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2707#endif
2708 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002709 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2710 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2711 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2712 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002713 /*
2714 * We can allow some features even when not supported by the
2715 * hardware. For example, L1 can specify an MSR bitmap - and we
2716 * can use it to avoid exits to L1 - even when L0 runs L2
2717 * without MSR bitmaps.
2718 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002719 vmx->nested.nested_vmx_procbased_ctls_high |=
2720 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002721 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002722
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002723 /* We support free control of CR3 access interception. */
David Matlack0115f9c2016-11-29 18:14:06 -08002724 vmx->nested.nested_vmx_procbased_ctls_low &=
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002725 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2726
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002727 /* secondary cpu-based controls */
2728 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002729 vmx->nested.nested_vmx_secondary_ctls_low,
2730 vmx->nested.nested_vmx_secondary_ctls_high);
2731 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2732 vmx->nested.nested_vmx_secondary_ctls_high &=
Paolo Bonzinia5f46452017-03-30 11:55:32 +02002733 SECONDARY_EXEC_RDRAND | SECONDARY_EXEC_RDSEED |
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002734 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002735 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini1b073042016-10-25 16:06:30 +02002736 SECONDARY_EXEC_DESC |
Wincy Vanf2b93282015-02-03 23:56:03 +08002737 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08002738 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002739 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002740 SECONDARY_EXEC_WBINVD_EXITING |
Dan Williamsdfa169b2016-06-02 11:17:24 -07002741 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002742
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002743 if (enable_ept) {
2744 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002745 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002746 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002747 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002748 VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT;
Bandan Das02120c42016-07-12 18:18:52 -04002749 if (cpu_has_vmx_ept_execute_only())
2750 vmx->nested.nested_vmx_ept_caps |=
2751 VMX_EPT_EXECUTE_ONLY_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002752 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Bandan Das45e11812016-08-02 16:32:36 -04002753 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
Paolo Bonzini7db74262017-03-08 10:49:19 +01002754 VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT |
2755 VMX_EPT_1GB_PAGE_BIT;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02002756 if (enable_ept_ad_bits)
2757 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002758 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002759 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002760
Paolo Bonzinief697a72016-03-18 16:58:38 +01002761 /*
2762 * Old versions of KVM use the single-context version without
2763 * checking for support, so declare that it is supported even
2764 * though it is treated as global context. The alternative is
2765 * not failing the single-context invvpid, and it is worse.
2766 */
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002767 if (enable_vpid) {
2768 vmx->nested.nested_vmx_secondary_ctls_high |=
2769 SECONDARY_EXEC_ENABLE_VPID;
Wanpeng Li089d7b62015-10-13 09:18:37 -07002770 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
Jan Dakinevichbcdde302016-10-28 07:00:30 +03002771 VMX_VPID_EXTENT_SUPPORTED_MASK;
Wanpeng Li63cb6d52017-03-20 21:18:53 -07002772 } else
Wanpeng Li089d7b62015-10-13 09:18:37 -07002773 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002774
Radim Krčmář0790ec12015-03-17 14:02:32 +01002775 if (enable_unrestricted_guest)
2776 vmx->nested.nested_vmx_secondary_ctls_high |=
2777 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2778
Jan Kiszkac18911a2013-03-13 16:06:41 +01002779 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002780 rdmsr(MSR_IA32_VMX_MISC,
2781 vmx->nested.nested_vmx_misc_low,
2782 vmx->nested.nested_vmx_misc_high);
2783 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2784 vmx->nested.nested_vmx_misc_low |=
2785 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002786 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002787 vmx->nested.nested_vmx_misc_high = 0;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002788
2789 /*
2790 * This MSR reports some information about VMX support. We
2791 * should return information about the VMX we emulate for the
2792 * guest, and the VMCS structure we give it - not about the
2793 * VMX support of the underlying hardware.
2794 */
2795 vmx->nested.nested_vmx_basic =
2796 VMCS12_REVISION |
2797 VMX_BASIC_TRUE_CTLS |
2798 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2799 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2800
2801 if (cpu_has_vmx_basic_inout())
2802 vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT;
2803
2804 /*
David Matlack8322ebb2016-11-29 18:14:09 -08002805 * These MSRs specify bits which the guest must keep fixed on
David Matlack62cc6b9d2016-11-29 18:14:07 -08002806 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2807 * We picked the standard core2 setting.
2808 */
2809#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2810#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2811 vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON;
David Matlack62cc6b9d2016-11-29 18:14:07 -08002812 vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON;
David Matlack8322ebb2016-11-29 18:14:09 -08002813
2814 /* These MSRs specify bits which the guest must keep fixed off. */
2815 rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1);
2816 rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1);
David Matlack62cc6b9d2016-11-29 18:14:07 -08002817
2818 /* highest index: VMX_PREEMPTION_TIMER_VALUE */
2819 vmx->nested.nested_vmx_vmcs_enum = 0x2e;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002820}
2821
David Matlack38991522016-11-29 18:14:08 -08002822/*
2823 * if fixed0[i] == 1: val[i] must be 1
2824 * if fixed1[i] == 0: val[i] must be 0
2825 */
2826static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1)
2827{
2828 return ((val & fixed1) | fixed0) == val;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002829}
2830
2831static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2832{
David Matlack38991522016-11-29 18:14:08 -08002833 return fixed_bits_valid(control, low, high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002834}
2835
2836static inline u64 vmx_control_msr(u32 low, u32 high)
2837{
2838 return low | ((u64)high << 32);
2839}
2840
David Matlack62cc6b9d2016-11-29 18:14:07 -08002841static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
2842{
2843 superset &= mask;
2844 subset &= mask;
2845
2846 return (superset | subset) == superset;
2847}
2848
2849static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
2850{
2851 const u64 feature_and_reserved =
2852 /* feature (except bit 48; see below) */
2853 BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
2854 /* reserved */
2855 BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
2856 u64 vmx_basic = vmx->nested.nested_vmx_basic;
2857
2858 if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
2859 return -EINVAL;
2860
2861 /*
2862 * KVM does not emulate a version of VMX that constrains physical
2863 * addresses of VMX structures (e.g. VMCS) to 32-bits.
2864 */
2865 if (data & BIT_ULL(48))
2866 return -EINVAL;
2867
2868 if (vmx_basic_vmcs_revision_id(vmx_basic) !=
2869 vmx_basic_vmcs_revision_id(data))
2870 return -EINVAL;
2871
2872 if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data))
2873 return -EINVAL;
2874
2875 vmx->nested.nested_vmx_basic = data;
2876 return 0;
2877}
2878
2879static int
2880vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2881{
2882 u64 supported;
2883 u32 *lowp, *highp;
2884
2885 switch (msr_index) {
2886 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2887 lowp = &vmx->nested.nested_vmx_pinbased_ctls_low;
2888 highp = &vmx->nested.nested_vmx_pinbased_ctls_high;
2889 break;
2890 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2891 lowp = &vmx->nested.nested_vmx_procbased_ctls_low;
2892 highp = &vmx->nested.nested_vmx_procbased_ctls_high;
2893 break;
2894 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2895 lowp = &vmx->nested.nested_vmx_exit_ctls_low;
2896 highp = &vmx->nested.nested_vmx_exit_ctls_high;
2897 break;
2898 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2899 lowp = &vmx->nested.nested_vmx_entry_ctls_low;
2900 highp = &vmx->nested.nested_vmx_entry_ctls_high;
2901 break;
2902 case MSR_IA32_VMX_PROCBASED_CTLS2:
2903 lowp = &vmx->nested.nested_vmx_secondary_ctls_low;
2904 highp = &vmx->nested.nested_vmx_secondary_ctls_high;
2905 break;
2906 default:
2907 BUG();
2908 }
2909
2910 supported = vmx_control_msr(*lowp, *highp);
2911
2912 /* Check must-be-1 bits are still 1. */
2913 if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0)))
2914 return -EINVAL;
2915
2916 /* Check must-be-0 bits are still 0. */
2917 if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32)))
2918 return -EINVAL;
2919
2920 *lowp = data;
2921 *highp = data >> 32;
2922 return 0;
2923}
2924
2925static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data)
2926{
2927 const u64 feature_and_reserved_bits =
2928 /* feature */
2929 BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) |
2930 BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) |
2931 /* reserved */
2932 GENMASK_ULL(13, 9) | BIT_ULL(31);
2933 u64 vmx_misc;
2934
2935 vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low,
2936 vmx->nested.nested_vmx_misc_high);
2937
2938 if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits))
2939 return -EINVAL;
2940
2941 if ((vmx->nested.nested_vmx_pinbased_ctls_high &
2942 PIN_BASED_VMX_PREEMPTION_TIMER) &&
2943 vmx_misc_preemption_timer_rate(data) !=
2944 vmx_misc_preemption_timer_rate(vmx_misc))
2945 return -EINVAL;
2946
2947 if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc))
2948 return -EINVAL;
2949
2950 if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc))
2951 return -EINVAL;
2952
2953 if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc))
2954 return -EINVAL;
2955
2956 vmx->nested.nested_vmx_misc_low = data;
2957 vmx->nested.nested_vmx_misc_high = data >> 32;
2958 return 0;
2959}
2960
2961static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data)
2962{
2963 u64 vmx_ept_vpid_cap;
2964
2965 vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps,
2966 vmx->nested.nested_vmx_vpid_caps);
2967
2968 /* Every bit is either reserved or a feature bit. */
2969 if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL))
2970 return -EINVAL;
2971
2972 vmx->nested.nested_vmx_ept_caps = data;
2973 vmx->nested.nested_vmx_vpid_caps = data >> 32;
2974 return 0;
2975}
2976
2977static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data)
2978{
2979 u64 *msr;
2980
2981 switch (msr_index) {
2982 case MSR_IA32_VMX_CR0_FIXED0:
2983 msr = &vmx->nested.nested_vmx_cr0_fixed0;
2984 break;
2985 case MSR_IA32_VMX_CR4_FIXED0:
2986 msr = &vmx->nested.nested_vmx_cr4_fixed0;
2987 break;
2988 default:
2989 BUG();
2990 }
2991
2992 /*
2993 * 1 bits (which indicates bits which "must-be-1" during VMX operation)
2994 * must be 1 in the restored value.
2995 */
2996 if (!is_bitwise_subset(data, *msr, -1ULL))
2997 return -EINVAL;
2998
2999 *msr = data;
3000 return 0;
3001}
3002
3003/*
3004 * Called when userspace is restoring VMX MSRs.
3005 *
3006 * Returns 0 on success, non-0 otherwise.
3007 */
3008static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
3009{
3010 struct vcpu_vmx *vmx = to_vmx(vcpu);
3011
3012 switch (msr_index) {
3013 case MSR_IA32_VMX_BASIC:
3014 return vmx_restore_vmx_basic(vmx, data);
3015 case MSR_IA32_VMX_PINBASED_CTLS:
3016 case MSR_IA32_VMX_PROCBASED_CTLS:
3017 case MSR_IA32_VMX_EXIT_CTLS:
3018 case MSR_IA32_VMX_ENTRY_CTLS:
3019 /*
3020 * The "non-true" VMX capability MSRs are generated from the
3021 * "true" MSRs, so we do not support restoring them directly.
3022 *
3023 * If userspace wants to emulate VMX_BASIC[55]=0, userspace
3024 * should restore the "true" MSRs with the must-be-1 bits
3025 * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND
3026 * DEFAULT SETTINGS".
3027 */
3028 return -EINVAL;
3029 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3030 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3031 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3032 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3033 case MSR_IA32_VMX_PROCBASED_CTLS2:
3034 return vmx_restore_control_msr(vmx, msr_index, data);
3035 case MSR_IA32_VMX_MISC:
3036 return vmx_restore_vmx_misc(vmx, data);
3037 case MSR_IA32_VMX_CR0_FIXED0:
3038 case MSR_IA32_VMX_CR4_FIXED0:
3039 return vmx_restore_fixed0_msr(vmx, msr_index, data);
3040 case MSR_IA32_VMX_CR0_FIXED1:
3041 case MSR_IA32_VMX_CR4_FIXED1:
3042 /*
3043 * These MSRs are generated based on the vCPU's CPUID, so we
3044 * do not support restoring them directly.
3045 */
3046 return -EINVAL;
3047 case MSR_IA32_VMX_EPT_VPID_CAP:
3048 return vmx_restore_vmx_ept_vpid_cap(vmx, data);
3049 case MSR_IA32_VMX_VMCS_ENUM:
3050 vmx->nested.nested_vmx_vmcs_enum = data;
3051 return 0;
3052 default:
3053 /*
3054 * The rest of the VMX capability MSRs do not support restore.
3055 */
3056 return -EINVAL;
3057 }
3058}
3059
Jan Kiszkacae50132014-01-04 18:47:22 +01003060/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003061static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
3062{
Wincy Vanb9c237b2015-02-03 23:56:30 +08003063 struct vcpu_vmx *vmx = to_vmx(vcpu);
3064
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003065 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003066 case MSR_IA32_VMX_BASIC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003067 *pdata = vmx->nested.nested_vmx_basic;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003068 break;
3069 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
3070 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003071 *pdata = vmx_control_msr(
3072 vmx->nested.nested_vmx_pinbased_ctls_low,
3073 vmx->nested.nested_vmx_pinbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003074 if (msr_index == MSR_IA32_VMX_PINBASED_CTLS)
3075 *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003076 break;
3077 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
3078 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003079 *pdata = vmx_control_msr(
3080 vmx->nested.nested_vmx_procbased_ctls_low,
3081 vmx->nested.nested_vmx_procbased_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003082 if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS)
3083 *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003084 break;
3085 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
3086 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003087 *pdata = vmx_control_msr(
3088 vmx->nested.nested_vmx_exit_ctls_low,
3089 vmx->nested.nested_vmx_exit_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003090 if (msr_index == MSR_IA32_VMX_EXIT_CTLS)
3091 *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003092 break;
3093 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
3094 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003095 *pdata = vmx_control_msr(
3096 vmx->nested.nested_vmx_entry_ctls_low,
3097 vmx->nested.nested_vmx_entry_ctls_high);
David Matlack0115f9c2016-11-29 18:14:06 -08003098 if (msr_index == MSR_IA32_VMX_ENTRY_CTLS)
3099 *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003100 break;
3101 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003102 *pdata = vmx_control_msr(
3103 vmx->nested.nested_vmx_misc_low,
3104 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003105 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003106 case MSR_IA32_VMX_CR0_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003107 *pdata = vmx->nested.nested_vmx_cr0_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003108 break;
3109 case MSR_IA32_VMX_CR0_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003110 *pdata = vmx->nested.nested_vmx_cr0_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003111 break;
3112 case MSR_IA32_VMX_CR4_FIXED0:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003113 *pdata = vmx->nested.nested_vmx_cr4_fixed0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003114 break;
3115 case MSR_IA32_VMX_CR4_FIXED1:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003116 *pdata = vmx->nested.nested_vmx_cr4_fixed1;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003117 break;
3118 case MSR_IA32_VMX_VMCS_ENUM:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003119 *pdata = vmx->nested.nested_vmx_vmcs_enum;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003120 break;
3121 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08003122 *pdata = vmx_control_msr(
3123 vmx->nested.nested_vmx_secondary_ctls_low,
3124 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003125 break;
3126 case MSR_IA32_VMX_EPT_VPID_CAP:
Wanpeng Li089d7b62015-10-13 09:18:37 -07003127 *pdata = vmx->nested.nested_vmx_ept_caps |
3128 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003129 break;
3130 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003131 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08003132 }
3133
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003134 return 0;
3135}
3136
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003137static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
3138 uint64_t val)
3139{
3140 uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
3141
3142 return !(val & ~valid_bits);
3143}
3144
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003145/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003146 * Reads an msr value (of 'msr_index') into 'pdata'.
3147 * Returns 0 on success, non-0 otherwise.
3148 * Assumes vcpu_load() was already called.
3149 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003150static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151{
Avi Kivity26bb0982009-09-07 11:14:12 +03003152 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003154 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003155#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003157 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158 break;
3159 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003160 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003162 case MSR_KERNEL_GS_BASE:
3163 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003164 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003165 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03003166#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003168 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303169 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08003170 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171 break;
3172 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003173 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174 break;
3175 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003176 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177 break;
3178 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003179 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003180 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003181 case MSR_IA32_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003182 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003183 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003184 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003185 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003186 case MSR_IA32_MCG_EXT_CTL:
3187 if (!msr_info->host_initiated &&
3188 !(to_vmx(vcpu)->msr_ia32_feature_control &
3189 FEATURE_CONTROL_LMCE))
Jan Kiszkacae50132014-01-04 18:47:22 +01003190 return 1;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003191 msr_info->data = vcpu->arch.mcg_ext_ctl;
3192 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003193 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang3b840802016-06-22 14:59:54 +08003194 msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01003195 break;
3196 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3197 if (!nested_vmx_allowed(vcpu))
3198 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003199 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08003200 case MSR_IA32_XSS:
3201 if (!vmx_xsaves_supported())
3202 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003203 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08003204 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003205 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003206 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003207 return 1;
3208 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003210 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003211 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003212 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003213 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02003215 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216 }
3217
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218 return 0;
3219}
3220
Jan Kiszkacae50132014-01-04 18:47:22 +01003221static void vmx_leave_nested(struct kvm_vcpu *vcpu);
3222
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223/*
3224 * Writes msr value into into the appropriate "register".
3225 * Returns 0 on success, non-0 otherwise.
3226 * Assumes vcpu_load() was already called.
3227 */
Will Auld8fe8ab42012-11-29 12:42:12 -08003228static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003230 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003231 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03003232 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08003233 u32 msr_index = msr_info->index;
3234 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03003235
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08003237 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08003238 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03003239 break;
Avi Kivity16175a72009-03-23 22:13:44 +02003240#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003242 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243 vmcs_writel(GUEST_FS_BASE, data);
3244 break;
3245 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03003246 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003247 vmcs_writel(GUEST_GS_BASE, data);
3248 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03003249 case MSR_KERNEL_GS_BASE:
3250 vmx_load_host_state(vmx);
3251 vmx->msr_guest_kernel_gs_base = data;
3252 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003253#endif
3254 case MSR_IA32_SYSENTER_CS:
3255 vmcs_write32(GUEST_SYSENTER_CS, data);
3256 break;
3257 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003258 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003259 break;
3260 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02003261 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003262 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003263 case MSR_IA32_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003264 if (!kvm_mpx_supported())
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01003265 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00003266 vmcs_write64(GUEST_BNDCFGS, data);
3267 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05303268 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08003269 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003270 break;
Sheng Yang468d4722008-10-09 16:01:55 +08003271 case MSR_IA32_CR_PAT:
3272 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03003273 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
3274 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08003275 vmcs_write64(GUEST_IA32_PAT, data);
3276 vcpu->arch.pat = data;
3277 break;
3278 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003279 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003280 break;
Will Auldba904632012-11-29 12:42:50 -08003281 case MSR_IA32_TSC_ADJUST:
3282 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003283 break;
Ashok Rajc45dcc72016-06-22 14:59:56 +08003284 case MSR_IA32_MCG_EXT_CTL:
3285 if ((!msr_info->host_initiated &&
3286 !(to_vmx(vcpu)->msr_ia32_feature_control &
3287 FEATURE_CONTROL_LMCE)) ||
3288 (data & ~MCG_EXT_CTL_LMCE_EN))
3289 return 1;
3290 vcpu->arch.mcg_ext_ctl = data;
3291 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01003292 case MSR_IA32_FEATURE_CONTROL:
Haozhong Zhang37e4c992016-06-22 14:59:55 +08003293 if (!vmx_feature_control_msr_valid(vcpu, data) ||
Haozhong Zhang3b840802016-06-22 14:59:54 +08003294 (to_vmx(vcpu)->msr_ia32_feature_control &
Jan Kiszkacae50132014-01-04 18:47:22 +01003295 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
3296 return 1;
Haozhong Zhang3b840802016-06-22 14:59:54 +08003297 vmx->msr_ia32_feature_control = data;
Jan Kiszkacae50132014-01-04 18:47:22 +01003298 if (msr_info->host_initiated && data == 0)
3299 vmx_leave_nested(vcpu);
3300 break;
3301 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
David Matlack62cc6b9d2016-11-29 18:14:07 -08003302 if (!msr_info->host_initiated)
3303 return 1; /* they are read-only */
3304 if (!nested_vmx_allowed(vcpu))
3305 return 1;
3306 return vmx_set_vmx_msr(vcpu, msr_index, data);
Wanpeng Li20300092014-12-02 19:14:59 +08003307 case MSR_IA32_XSS:
3308 if (!vmx_xsaves_supported())
3309 return 1;
3310 /*
3311 * The only supported bit as of Skylake is bit 8, but
3312 * it is not supported on KVM.
3313 */
3314 if (data != 0)
3315 return 1;
3316 vcpu->arch.ia32_xss = data;
3317 if (vcpu->arch.ia32_xss != host_xss)
3318 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
3319 vcpu->arch.ia32_xss, host_xss);
3320 else
3321 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
3322 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003323 case MSR_TSC_AUX:
Haozhong Zhang81b1b9c2015-12-14 23:13:38 +08003324 if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003325 return 1;
3326 /* Check reserved bit, higher 32 bits should be zero */
3327 if ((data >> 32) != 0)
3328 return 1;
3329 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003330 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10003331 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08003332 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07003333 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08003334 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003335 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
3336 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003337 ret = kvm_set_shared_msr(msr->index, msr->data,
3338 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03003339 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07003340 if (ret)
3341 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03003342 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08003343 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003344 }
Will Auld8fe8ab42012-11-29 12:42:12 -08003345 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003346 }
3347
Eddie Dong2cc51562007-05-21 07:28:09 +03003348 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003349}
3350
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003351static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003352{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003353 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
3354 switch (reg) {
3355 case VCPU_REGS_RSP:
3356 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3357 break;
3358 case VCPU_REGS_RIP:
3359 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3360 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003361 case VCPU_EXREG_PDPTR:
3362 if (enable_ept)
3363 ept_save_pdptrs(vcpu);
3364 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003365 default:
3366 break;
3367 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003368}
3369
Avi Kivity6aa8b732006-12-10 02:21:36 -08003370static __init int cpu_has_kvm_support(void)
3371{
Eduardo Habkost6210e372008-11-17 19:03:16 -02003372 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003373}
3374
3375static __init int vmx_disabled_by_bios(void)
3376{
3377 u64 msr;
3378
3379 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04003380 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08003381 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04003382 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3383 && tboot_enabled())
3384 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08003385 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04003386 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08003387 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08003388 && !tboot_enabled()) {
3389 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08003390 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04003391 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08003392 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08003393 /* launched w/o TXT and VMX disabled */
3394 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3395 && !tboot_enabled())
3396 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04003397 }
3398
3399 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003400}
3401
Dongxiao Xu7725b892010-05-11 18:29:38 +08003402static void kvm_cpu_vmxon(u64 addr)
3403{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003404 cr4_set_bits(X86_CR4_VMXE);
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003405 intel_pt_handle_vmx(1);
3406
Dongxiao Xu7725b892010-05-11 18:29:38 +08003407 asm volatile (ASM_VMX_VMXON_RAX
3408 : : "a"(&addr), "m"(addr)
3409 : "memory", "cc");
3410}
3411
Radim Krčmář13a34e02014-08-28 15:13:03 +02003412static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003413{
3414 int cpu = raw_smp_processor_id();
3415 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003416 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003418 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003419 return -EBUSY;
3420
Nadav Har'Eld462b812011-05-24 15:26:10 +03003421 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003422 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3423 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003424
3425 /*
3426 * Now we can enable the vmclear operation in kdump
3427 * since the loaded_vmcss_on_cpu list on this cpu
3428 * has been initialized.
3429 *
3430 * Though the cpu is not in VMX operation now, there
3431 * is no problem to enable the vmclear operation
3432 * for the loaded_vmcss_on_cpu list is empty!
3433 */
3434 crash_enable_local_vmclear(cpu);
3435
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003437
3438 test_bits = FEATURE_CONTROL_LOCKED;
3439 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3440 if (tboot_enabled())
3441 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3442
3443 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003444 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003445 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3446 }
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003447 kvm_cpu_vmxon(phys_addr);
3448 ept_sync_global();
Alexander Graf10474ae2009-09-15 11:37:46 +02003449
Christoph Lameter89cbc762014-08-17 12:30:40 -05003450 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003451
Alexander Graf10474ae2009-09-15 11:37:46 +02003452 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003453}
3454
Nadav Har'Eld462b812011-05-24 15:26:10 +03003455static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003456{
3457 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003458 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003459
Nadav Har'Eld462b812011-05-24 15:26:10 +03003460 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3461 loaded_vmcss_on_cpu_link)
3462 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003463}
3464
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003465
3466/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3467 * tricks.
3468 */
3469static void kvm_cpu_vmxoff(void)
3470{
3471 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Alexander Shishkin1c5ac212016-03-29 17:43:10 +03003472
3473 intel_pt_handle_vmx(0);
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003474 cr4_clear_bits(X86_CR4_VMXE);
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003475}
3476
Radim Krčmář13a34e02014-08-28 15:13:03 +02003477static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003478{
David Hildenbrandfe0e80b2017-03-10 12:47:13 +01003479 vmclear_local_loaded_vmcss();
3480 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003481}
3482
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003483static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003484 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003485{
3486 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003487 u32 ctl = ctl_min | ctl_opt;
3488
3489 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3490
3491 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3492 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3493
3494 /* Ensure minimum (required) set of control bits are supported. */
3495 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003496 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003497
3498 *result = ctl;
3499 return 0;
3500}
3501
Avi Kivity110312c2010-12-21 12:54:20 +02003502static __init bool allow_1_setting(u32 msr, u32 ctl)
3503{
3504 u32 vmx_msr_low, vmx_msr_high;
3505
3506 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3507 return vmx_msr_high & ctl;
3508}
3509
Yang, Sheng002c7f72007-07-31 14:23:01 +03003510static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003511{
3512 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003513 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003514 u32 _pin_based_exec_control = 0;
3515 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003516 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003517 u32 _vmexit_control = 0;
3518 u32 _vmentry_control = 0;
3519
Raghavendra K T10166742012-02-07 23:19:20 +05303520 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003521#ifdef CONFIG_X86_64
3522 CPU_BASED_CR8_LOAD_EXITING |
3523 CPU_BASED_CR8_STORE_EXITING |
3524#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003525 CPU_BASED_CR3_LOAD_EXITING |
3526 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003527 CPU_BASED_USE_IO_BITMAPS |
3528 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003529 CPU_BASED_USE_TSC_OFFSETING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003530 CPU_BASED_INVLPG_EXITING |
3531 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003532
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02003533 if (!kvm_mwait_in_guest())
3534 min |= CPU_BASED_MWAIT_EXITING |
3535 CPU_BASED_MONITOR_EXITING;
3536
Sheng Yangf78e0e22007-10-29 09:40:42 +08003537 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003538 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003539 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003540 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3541 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003542 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003543#ifdef CONFIG_X86_64
3544 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3545 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3546 ~CPU_BASED_CR8_STORE_EXITING;
3547#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003548 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003549 min2 = 0;
3550 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003551 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003552 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003553 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003554 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003555 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003556 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003557 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003558 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003559 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003560 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003561 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003562 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003563 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003564 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003565 if (adjust_vmx_controls(min2, opt2,
3566 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003567 &_cpu_based_2nd_exec_control) < 0)
3568 return -EIO;
3569 }
3570#ifndef CONFIG_X86_64
3571 if (!(_cpu_based_2nd_exec_control &
3572 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3573 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3574#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003575
3576 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3577 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003578 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003579 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3580 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003581
Sheng Yangd56f5462008-04-25 10:13:16 +08003582 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003583 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3584 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003585 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3586 CPU_BASED_CR3_STORE_EXITING |
3587 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003588 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3589 vmx_capability.ept, vmx_capability.vpid);
3590 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003591
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003592 min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003593#ifdef CONFIG_X86_64
3594 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3595#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003596 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003597 VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003598 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3599 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003600 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003601
Paolo Bonzini2c828782017-03-27 14:37:28 +02003602 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING |
3603 PIN_BASED_VIRTUAL_NMIS;
3604 opt = PIN_BASED_POSTED_INTR | PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003605 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3606 &_pin_based_exec_control) < 0)
3607 return -EIO;
3608
Paolo Bonzini1c17c3e2016-07-08 11:53:38 +02003609 if (cpu_has_broken_vmx_preemption_timer())
3610 _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08003611 if (!(_cpu_based_2nd_exec_control &
Paolo Bonzini91fa0f82016-06-15 20:55:08 +02003612 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
Yang Zhang01e439b2013-04-11 19:25:12 +08003613 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3614
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003615 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003616 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003617 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3618 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003619 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003620
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003621 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003622
3623 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3624 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003625 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003626
3627#ifdef CONFIG_X86_64
3628 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3629 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003630 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003631#endif
3632
3633 /* Require Write-Back (WB) memory type for VMCS accesses. */
3634 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003635 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003636
Yang, Sheng002c7f72007-07-31 14:23:01 +03003637 vmcs_conf->size = vmx_msr_high & 0x1fff;
Paolo Bonzini16cb0252016-09-05 15:57:00 +02003638 vmcs_conf->order = get_order(vmcs_conf->size);
Jan Dakinevich9ac7e3e2016-09-04 21:23:15 +03003639 vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003640 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003641
Yang, Sheng002c7f72007-07-31 14:23:01 +03003642 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3643 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003644 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003645 vmcs_conf->vmexit_ctrl = _vmexit_control;
3646 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003647
Avi Kivity110312c2010-12-21 12:54:20 +02003648 cpu_has_load_ia32_efer =
3649 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3650 VM_ENTRY_LOAD_IA32_EFER)
3651 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3652 VM_EXIT_LOAD_IA32_EFER);
3653
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003654 cpu_has_load_perf_global_ctrl =
3655 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3656 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3657 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3658 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3659
3660 /*
3661 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
Andrea Gelminibb3541f2016-05-21 14:14:44 +02003662 * but due to errata below it can't be used. Workaround is to use
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003663 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3664 *
3665 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3666 *
3667 * AAK155 (model 26)
3668 * AAP115 (model 30)
3669 * AAT100 (model 37)
3670 * BC86,AAY89,BD102 (model 44)
3671 * BA97 (model 46)
3672 *
3673 */
3674 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3675 switch (boot_cpu_data.x86_model) {
3676 case 26:
3677 case 30:
3678 case 37:
3679 case 44:
3680 case 46:
3681 cpu_has_load_perf_global_ctrl = false;
3682 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3683 "does not work properly. Using workaround\n");
3684 break;
3685 default:
3686 break;
3687 }
3688 }
3689
Borislav Petkov782511b2016-04-04 22:25:03 +02003690 if (boot_cpu_has(X86_FEATURE_XSAVES))
Wanpeng Li20300092014-12-02 19:14:59 +08003691 rdmsrl(MSR_IA32_XSS, host_xss);
3692
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003693 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003694}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003695
3696static struct vmcs *alloc_vmcs_cpu(int cpu)
3697{
3698 int node = cpu_to_node(cpu);
3699 struct page *pages;
3700 struct vmcs *vmcs;
3701
Vlastimil Babka96db8002015-09-08 15:03:50 -07003702 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003703 if (!pages)
3704 return NULL;
3705 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003706 memset(vmcs, 0, vmcs_config.size);
3707 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003708 return vmcs;
3709}
3710
3711static struct vmcs *alloc_vmcs(void)
3712{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003713 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003714}
3715
3716static void free_vmcs(struct vmcs *vmcs)
3717{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003718 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003719}
3720
Nadav Har'Eld462b812011-05-24 15:26:10 +03003721/*
3722 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3723 */
3724static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3725{
3726 if (!loaded_vmcs->vmcs)
3727 return;
3728 loaded_vmcs_clear(loaded_vmcs);
3729 free_vmcs(loaded_vmcs->vmcs);
3730 loaded_vmcs->vmcs = NULL;
Jim Mattson355f4fb2016-10-28 08:29:39 -07003731 WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
Nadav Har'Eld462b812011-05-24 15:26:10 +03003732}
3733
Sam Ravnborg39959582007-06-01 00:47:13 -07003734static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003735{
3736 int cpu;
3737
Zachary Amsden3230bb42009-09-29 11:38:37 -10003738 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003739 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003740 per_cpu(vmxarea, cpu) = NULL;
3741 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003742}
3743
Bandan Dasfe2b2012014-04-21 15:20:14 -04003744static void init_vmcs_shadow_fields(void)
3745{
3746 int i, j;
3747
3748 /* No checks for read only fields yet */
3749
3750 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3751 switch (shadow_read_write_fields[i]) {
3752 case GUEST_BNDCFGS:
Paolo Bonzinia87036a2016-03-08 09:52:13 +01003753 if (!kvm_mpx_supported())
Bandan Dasfe2b2012014-04-21 15:20:14 -04003754 continue;
3755 break;
3756 default:
3757 break;
3758 }
3759
3760 if (j < i)
3761 shadow_read_write_fields[j] =
3762 shadow_read_write_fields[i];
3763 j++;
3764 }
3765 max_shadow_read_write_fields = j;
3766
3767 /* shadowed fields guest access without vmexit */
3768 for (i = 0; i < max_shadow_read_write_fields; i++) {
3769 clear_bit(shadow_read_write_fields[i],
3770 vmx_vmwrite_bitmap);
3771 clear_bit(shadow_read_write_fields[i],
3772 vmx_vmread_bitmap);
3773 }
3774 for (i = 0; i < max_shadow_read_only_fields; i++)
3775 clear_bit(shadow_read_only_fields[i],
3776 vmx_vmread_bitmap);
3777}
3778
Avi Kivity6aa8b732006-12-10 02:21:36 -08003779static __init int alloc_kvm_area(void)
3780{
3781 int cpu;
3782
Zachary Amsden3230bb42009-09-29 11:38:37 -10003783 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003784 struct vmcs *vmcs;
3785
3786 vmcs = alloc_vmcs_cpu(cpu);
3787 if (!vmcs) {
3788 free_kvm_area();
3789 return -ENOMEM;
3790 }
3791
3792 per_cpu(vmxarea, cpu) = vmcs;
3793 }
3794 return 0;
3795}
3796
Gleb Natapov14168782013-01-21 15:36:49 +02003797static bool emulation_required(struct kvm_vcpu *vcpu)
3798{
3799 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3800}
3801
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003802static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003803 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003805 if (!emulate_invalid_guest_state) {
3806 /*
3807 * CS and SS RPL should be equal during guest entry according
3808 * to VMX spec, but in reality it is not always so. Since vcpu
3809 * is in the middle of the transition from real mode to
3810 * protected mode it is safe to assume that RPL 0 is a good
3811 * default value.
3812 */
3813 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003814 save->selector &= ~SEGMENT_RPL_MASK;
3815 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003816 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003817 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003818 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003819}
3820
3821static void enter_pmode(struct kvm_vcpu *vcpu)
3822{
3823 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003824 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003825
Gleb Natapovd99e4152012-12-20 16:57:45 +02003826 /*
3827 * Update real mode segment cache. It may be not up-to-date if sement
3828 * register was written while vcpu was in a guest mode.
3829 */
3830 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3831 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3832 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3833 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3834 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3835 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3836
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003837 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003838
Avi Kivity2fb92db2011-04-27 19:42:18 +03003839 vmx_segment_cache_clear(vmx);
3840
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003841 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003842
3843 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003844 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3845 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003846 vmcs_writel(GUEST_RFLAGS, flags);
3847
Rusty Russell66aee912007-07-17 23:34:16 +10003848 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3849 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850
3851 update_exception_bitmap(vcpu);
3852
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003853 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3854 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3855 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3856 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3857 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3858 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003859}
3860
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003861static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003862{
Mathias Krause772e0312012-08-30 01:30:19 +02003863 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003864 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003865
Gleb Natapovd99e4152012-12-20 16:57:45 +02003866 var.dpl = 0x3;
3867 if (seg == VCPU_SREG_CS)
3868 var.type = 0x3;
3869
3870 if (!emulate_invalid_guest_state) {
3871 var.selector = var.base >> 4;
3872 var.base = var.base & 0xffff0;
3873 var.limit = 0xffff;
3874 var.g = 0;
3875 var.db = 0;
3876 var.present = 1;
3877 var.s = 1;
3878 var.l = 0;
3879 var.unusable = 0;
3880 var.type = 0x3;
3881 var.avl = 0;
3882 if (save->base & 0xf)
3883 printk_once(KERN_WARNING "kvm: segment base is not "
3884 "paragraph aligned when entering "
3885 "protected mode (seg=%d)", seg);
3886 }
3887
3888 vmcs_write16(sf->selector, var.selector);
Chao Peng96794e42017-02-21 03:50:01 -05003889 vmcs_writel(sf->base, var.base);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003890 vmcs_write32(sf->limit, var.limit);
3891 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003892}
3893
3894static void enter_rmode(struct kvm_vcpu *vcpu)
3895{
3896 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003897 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003898
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003899 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3900 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3901 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3902 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3903 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003904 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3905 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003906
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003907 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003908
Gleb Natapov776e58e2011-03-13 12:34:27 +02003909 /*
3910 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003911 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003912 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003913 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003914 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3915 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003916
Avi Kivity2fb92db2011-04-27 19:42:18 +03003917 vmx_segment_cache_clear(vmx);
3918
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003919 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003920 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003921 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3922
3923 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003924 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003925
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003926 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003927
3928 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003929 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003930 update_exception_bitmap(vcpu);
3931
Gleb Natapovd99e4152012-12-20 16:57:45 +02003932 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3933 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3934 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3935 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3936 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3937 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003938
Eddie Dong8668a3c2007-10-10 14:26:45 +08003939 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003940}
3941
Amit Shah401d10d2009-02-20 22:53:37 +05303942static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3943{
3944 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003945 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3946
3947 if (!msr)
3948 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303949
Avi Kivity44ea2b12009-09-06 15:55:37 +03003950 /*
3951 * Force kernel_gs_base reloading before EFER changes, as control
3952 * of this msr depends on is_long_mode().
3953 */
3954 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003955 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303956 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003957 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303958 msr->data = efer;
3959 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003960 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303961
3962 msr->data = efer & ~EFER_LME;
3963 }
3964 setup_msrs(vmx);
3965}
3966
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003967#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003968
3969static void enter_lmode(struct kvm_vcpu *vcpu)
3970{
3971 u32 guest_tr_ar;
3972
Avi Kivity2fb92db2011-04-27 19:42:18 +03003973 vmx_segment_cache_clear(to_vmx(vcpu));
3974
Avi Kivity6aa8b732006-12-10 02:21:36 -08003975 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003976 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003977 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3978 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003979 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003980 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3981 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003982 }
Avi Kivityda38f432010-07-06 11:30:49 +03003983 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003984}
3985
3986static void exit_lmode(struct kvm_vcpu *vcpu)
3987{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003988 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003989 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003990}
3991
3992#endif
3993
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003994static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003995{
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003996 if (enable_ept) {
3997 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3998 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003999 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Jim Mattsonf0b98c02017-03-15 07:56:11 -07004000 } else {
4001 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08004002 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08004003}
4004
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004005static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
4006{
4007 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
4008}
4009
Jim Mattsonfb6c8192017-03-16 13:53:59 -07004010static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu)
4011{
4012 if (enable_ept)
4013 vmx_flush_tlb(vcpu);
4014}
4015
Avi Kivitye8467fd2009-12-29 18:43:06 +02004016static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
4017{
4018 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
4019
4020 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
4021 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
4022}
4023
Avi Kivityaff48ba2010-12-05 18:56:11 +02004024static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
4025{
4026 if (enable_ept && is_paging(vcpu))
4027 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
4028 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
4029}
4030
Anthony Liguori25c4c272007-04-27 09:29:21 +03004031static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08004032{
Avi Kivityfc78f512009-12-07 12:16:48 +02004033 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
4034
4035 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
4036 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08004037}
4038
Sheng Yang14394422008-04-28 12:24:45 +08004039static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
4040{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004041 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4042
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004043 if (!test_bit(VCPU_EXREG_PDPTR,
4044 (unsigned long *)&vcpu->arch.regs_dirty))
4045 return;
4046
Sheng Yang14394422008-04-28 12:24:45 +08004047 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004048 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
4049 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
4050 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
4051 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08004052 }
4053}
4054
Avi Kivity8f5d5492009-05-31 18:41:29 +03004055static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
4056{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004057 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
4058
Avi Kivity8f5d5492009-05-31 18:41:29 +03004059 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03004060 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
4061 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
4062 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
4063 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004064 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004065
4066 __set_bit(VCPU_EXREG_PDPTR,
4067 (unsigned long *)&vcpu->arch.regs_avail);
4068 __set_bit(VCPU_EXREG_PDPTR,
4069 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03004070}
4071
David Matlack38991522016-11-29 18:14:08 -08004072static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4073{
4074 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4075 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4076 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4077
4078 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
4079 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4080 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4081 fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4082
4083 return fixed_bits_valid(val, fixed0, fixed1);
4084}
4085
4086static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
4087{
4088 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0;
4089 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1;
4090
4091 return fixed_bits_valid(val, fixed0, fixed1);
4092}
4093
4094static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val)
4095{
4096 u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0;
4097 u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1;
4098
4099 return fixed_bits_valid(val, fixed0, fixed1);
4100}
4101
4102/* No difference in the restrictions on guest and host CR4 in VMX operation. */
4103#define nested_guest_cr4_valid nested_cr4_valid
4104#define nested_host_cr4_valid nested_cr4_valid
4105
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004106static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08004107
4108static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
4109 unsigned long cr0,
4110 struct kvm_vcpu *vcpu)
4111{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03004112 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4113 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004114 if (!(cr0 & X86_CR0_PG)) {
4115 /* From paging/starting to nonpaging */
4116 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004117 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08004118 (CPU_BASED_CR3_LOAD_EXITING |
4119 CPU_BASED_CR3_STORE_EXITING));
4120 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004121 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004122 } else if (!is_paging(vcpu)) {
4123 /* From nonpaging to paging */
4124 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08004125 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08004126 ~(CPU_BASED_CR3_LOAD_EXITING |
4127 CPU_BASED_CR3_STORE_EXITING));
4128 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02004129 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08004130 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08004131
4132 if (!(cr0 & X86_CR0_WP))
4133 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08004134}
4135
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
4137{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004138 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004139 unsigned long hw_cr0;
4140
Gleb Natapov50378782013-02-04 16:00:28 +02004141 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004142 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02004143 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02004144 else {
Gleb Natapov50378782013-02-04 16:00:28 +02004145 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08004146
Gleb Natapov218e7632013-01-21 15:36:45 +02004147 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
4148 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004149
Gleb Natapov218e7632013-01-21 15:36:45 +02004150 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
4151 enter_rmode(vcpu);
4152 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004153
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004154#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02004155 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10004156 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004157 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10004158 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08004159 exit_lmode(vcpu);
4160 }
4161#endif
4162
Avi Kivity089d0342009-03-23 18:26:32 +02004163 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08004164 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
4165
Avi Kivity6aa8b732006-12-10 02:21:36 -08004166 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08004167 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004168 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02004169
4170 /* depends on vcpu->arch.cr0 to be set to a new value */
4171 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172}
4173
Sheng Yang14394422008-04-28 12:24:45 +08004174static u64 construct_eptp(unsigned long root_hpa)
4175{
4176 u64 eptp;
4177
4178 /* TODO write the value reading from MSR */
4179 eptp = VMX_EPT_DEFAULT_MT |
4180 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08004181 if (enable_ept_ad_bits)
4182 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08004183 eptp |= (root_hpa & PAGE_MASK);
4184
4185 return eptp;
4186}
4187
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
4189{
Sheng Yang14394422008-04-28 12:24:45 +08004190 unsigned long guest_cr3;
4191 u64 eptp;
4192
4193 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02004194 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004195 eptp = construct_eptp(cr3);
4196 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02004197 if (is_paging(vcpu) || is_guest_mode(vcpu))
4198 guest_cr3 = kvm_read_cr3(vcpu);
4199 else
4200 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02004201 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004202 }
4203
Sheng Yang2384d2b2008-01-17 15:14:33 +08004204 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08004205 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004206}
4207
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004208static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004209{
Ben Serebrin085e68e2015-04-16 11:58:05 -07004210 /*
4211 * Pass through host's Machine Check Enable value to hw_cr4, which
4212 * is in force while we are in guest mode. Do not let guests control
4213 * this bit, even if host CR4.MCE == 0.
4214 */
4215 unsigned long hw_cr4 =
4216 (cr4_read_shadow() & X86_CR4_MCE) |
4217 (cr4 & ~X86_CR4_MCE) |
4218 (to_vmx(vcpu)->rmode.vm86_active ?
4219 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08004220
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004221 if (cr4 & X86_CR4_VMXE) {
4222 /*
4223 * To use VMXON (and later other VMX instructions), a guest
4224 * must first be able to turn on cr4.VMXE (see handle_vmon()).
4225 * So basically the check on whether to allow nested VMX
4226 * is here.
4227 */
4228 if (!nested_vmx_allowed(vcpu))
4229 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004230 }
David Matlack38991522016-11-29 18:14:08 -08004231
4232 if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004233 return 1;
4234
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004235 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02004236 if (enable_ept) {
4237 if (!is_paging(vcpu)) {
4238 hw_cr4 &= ~X86_CR4_PAE;
4239 hw_cr4 |= X86_CR4_PSE;
4240 } else if (!(cr4 & X86_CR4_PAE)) {
4241 hw_cr4 &= ~X86_CR4_PAE;
4242 }
4243 }
Sheng Yang14394422008-04-28 12:24:45 +08004244
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004245 if (!enable_unrestricted_guest && !is_paging(vcpu))
4246 /*
Huaitong Handdba2622016-03-22 16:51:15 +08004247 * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
4248 * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
4249 * to be manually disabled when guest switches to non-paging
4250 * mode.
4251 *
4252 * If !enable_unrestricted_guest, the CPU is always running
4253 * with CR0.PG=1 and CR4 needs to be modified.
4254 * If enable_unrestricted_guest, the CPU automatically
4255 * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004256 */
Huaitong Handdba2622016-03-22 16:51:15 +08004257 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
Radim Krčmář656ec4a2015-11-02 22:20:00 +01004258
Sheng Yang14394422008-04-28 12:24:45 +08004259 vmcs_writel(CR4_READ_SHADOW, cr4);
4260 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03004261 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004262}
4263
Avi Kivity6aa8b732006-12-10 02:21:36 -08004264static void vmx_get_segment(struct kvm_vcpu *vcpu,
4265 struct kvm_segment *var, int seg)
4266{
Avi Kivitya9179492011-01-03 14:28:52 +02004267 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004268 u32 ar;
4269
Gleb Natapovc6ad11532012-12-12 19:10:51 +02004270 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004271 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02004272 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03004273 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03004274 return;
Avi Kivity1390a282012-08-21 17:07:08 +03004275 var->base = vmx_read_guest_seg_base(vmx, seg);
4276 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4277 return;
Avi Kivitya9179492011-01-03 14:28:52 +02004278 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004279 var->base = vmx_read_guest_seg_base(vmx, seg);
4280 var->limit = vmx_read_guest_seg_limit(vmx, seg);
4281 var->selector = vmx_read_guest_seg_selector(vmx, seg);
4282 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03004283 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004284 var->type = ar & 15;
4285 var->s = (ar >> 4) & 1;
4286 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03004287 /*
4288 * Some userspaces do not preserve unusable property. Since usable
4289 * segment has to be present according to VMX spec we can use present
4290 * property to amend userspace bug by making unusable segment always
4291 * nonpresent. vmx_segment_access_rights() already marks nonpresent
4292 * segment as unusable.
4293 */
4294 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004295 var->avl = (ar >> 12) & 1;
4296 var->l = (ar >> 13) & 1;
4297 var->db = (ar >> 14) & 1;
4298 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004299}
4300
Avi Kivitya9179492011-01-03 14:28:52 +02004301static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
4302{
Avi Kivitya9179492011-01-03 14:28:52 +02004303 struct kvm_segment s;
4304
4305 if (to_vmx(vcpu)->rmode.vm86_active) {
4306 vmx_get_segment(vcpu, &s, seg);
4307 return s.base;
4308 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03004309 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02004310}
4311
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004312static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02004313{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02004314 struct vcpu_vmx *vmx = to_vmx(vcpu);
4315
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004316 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02004317 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02004318 else {
4319 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004320 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02004321 }
Avi Kivity69c73022011-03-07 15:26:44 +02004322}
4323
Avi Kivity653e3102007-05-07 10:55:37 +03004324static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004325{
Avi Kivity6aa8b732006-12-10 02:21:36 -08004326 u32 ar;
4327
Avi Kivityf0495f92012-06-07 17:06:10 +03004328 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004329 ar = 1 << 16;
4330 else {
4331 ar = var->type & 15;
4332 ar |= (var->s & 1) << 4;
4333 ar |= (var->dpl & 3) << 5;
4334 ar |= (var->present & 1) << 7;
4335 ar |= (var->avl & 1) << 12;
4336 ar |= (var->l & 1) << 13;
4337 ar |= (var->db & 1) << 14;
4338 ar |= (var->g & 1) << 15;
4339 }
Avi Kivity653e3102007-05-07 10:55:37 +03004340
4341 return ar;
4342}
4343
4344static void vmx_set_segment(struct kvm_vcpu *vcpu,
4345 struct kvm_segment *var, int seg)
4346{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004347 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02004348 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03004349
Avi Kivity2fb92db2011-04-27 19:42:18 +03004350 vmx_segment_cache_clear(vmx);
4351
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004352 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4353 vmx->rmode.segs[seg] = *var;
4354 if (seg == VCPU_SREG_TR)
4355 vmcs_write16(sf->selector, var->selector);
4356 else if (var->s)
4357 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02004358 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03004359 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02004360
Avi Kivity653e3102007-05-07 10:55:37 +03004361 vmcs_writel(sf->base, var->base);
4362 vmcs_write32(sf->limit, var->limit);
4363 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004364
4365 /*
4366 * Fix the "Accessed" bit in AR field of segment registers for older
4367 * qemu binaries.
4368 * IA32 arch specifies that at the time of processor reset the
4369 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08004370 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004371 * state vmexit when "unrestricted guest" mode is turned on.
4372 * Fix for this setup issue in cpu_reset is being pushed in the qemu
4373 * tree. Newer qemu binaries with that qemu fix would not need this
4374 * kvm hack.
4375 */
4376 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02004377 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004378
Gleb Natapovf924d662012-12-12 19:10:55 +02004379 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02004380
4381out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01004382 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004383}
4384
Avi Kivity6aa8b732006-12-10 02:21:36 -08004385static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4386{
Avi Kivity2fb92db2011-04-27 19:42:18 +03004387 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004388
4389 *db = (ar >> 14) & 1;
4390 *l = (ar >> 13) & 1;
4391}
4392
Gleb Natapov89a27f42010-02-16 10:51:48 +02004393static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004394{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004395 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4396 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397}
4398
Gleb Natapov89a27f42010-02-16 10:51:48 +02004399static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004400{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004401 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4402 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004403}
4404
Gleb Natapov89a27f42010-02-16 10:51:48 +02004405static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004406{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004407 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4408 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409}
4410
Gleb Natapov89a27f42010-02-16 10:51:48 +02004411static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004412{
Gleb Natapov89a27f42010-02-16 10:51:48 +02004413 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4414 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004415}
4416
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004417static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4418{
4419 struct kvm_segment var;
4420 u32 ar;
4421
4422 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02004423 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02004424 if (seg == VCPU_SREG_CS)
4425 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004426 ar = vmx_segment_access_rights(&var);
4427
4428 if (var.base != (var.selector << 4))
4429 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02004430 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004431 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02004432 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004433 return false;
4434
4435 return true;
4436}
4437
4438static bool code_segment_valid(struct kvm_vcpu *vcpu)
4439{
4440 struct kvm_segment cs;
4441 unsigned int cs_rpl;
4442
4443 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004444 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004445
Avi Kivity1872a3f2009-01-04 23:26:52 +02004446 if (cs.unusable)
4447 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004448 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004449 return false;
4450 if (!cs.s)
4451 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004452 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004453 if (cs.dpl > cs_rpl)
4454 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004455 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004456 if (cs.dpl != cs_rpl)
4457 return false;
4458 }
4459 if (!cs.present)
4460 return false;
4461
4462 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4463 return true;
4464}
4465
4466static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4467{
4468 struct kvm_segment ss;
4469 unsigned int ss_rpl;
4470
4471 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004472 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004473
Avi Kivity1872a3f2009-01-04 23:26:52 +02004474 if (ss.unusable)
4475 return true;
4476 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004477 return false;
4478 if (!ss.s)
4479 return false;
4480 if (ss.dpl != ss_rpl) /* DPL != RPL */
4481 return false;
4482 if (!ss.present)
4483 return false;
4484
4485 return true;
4486}
4487
4488static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4489{
4490 struct kvm_segment var;
4491 unsigned int rpl;
4492
4493 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004494 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004495
Avi Kivity1872a3f2009-01-04 23:26:52 +02004496 if (var.unusable)
4497 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004498 if (!var.s)
4499 return false;
4500 if (!var.present)
4501 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004502 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004503 if (var.dpl < rpl) /* DPL < RPL */
4504 return false;
4505 }
4506
4507 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4508 * rights flags
4509 */
4510 return true;
4511}
4512
4513static bool tr_valid(struct kvm_vcpu *vcpu)
4514{
4515 struct kvm_segment tr;
4516
4517 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4518
Avi Kivity1872a3f2009-01-04 23:26:52 +02004519 if (tr.unusable)
4520 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004521 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004522 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004523 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004524 return false;
4525 if (!tr.present)
4526 return false;
4527
4528 return true;
4529}
4530
4531static bool ldtr_valid(struct kvm_vcpu *vcpu)
4532{
4533 struct kvm_segment ldtr;
4534
4535 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4536
Avi Kivity1872a3f2009-01-04 23:26:52 +02004537 if (ldtr.unusable)
4538 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004539 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004540 return false;
4541 if (ldtr.type != 2)
4542 return false;
4543 if (!ldtr.present)
4544 return false;
4545
4546 return true;
4547}
4548
4549static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4550{
4551 struct kvm_segment cs, ss;
4552
4553 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4554 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4555
Nadav Amitb32a9912015-03-29 16:33:04 +03004556 return ((cs.selector & SEGMENT_RPL_MASK) ==
4557 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004558}
4559
4560/*
4561 * Check if guest state is valid. Returns true if valid, false if
4562 * not.
4563 * We assume that registers are always usable
4564 */
4565static bool guest_state_valid(struct kvm_vcpu *vcpu)
4566{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004567 if (enable_unrestricted_guest)
4568 return true;
4569
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004570 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004571 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004572 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4573 return false;
4574 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4575 return false;
4576 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4577 return false;
4578 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4579 return false;
4580 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4581 return false;
4582 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4583 return false;
4584 } else {
4585 /* protected mode guest state checks */
4586 if (!cs_ss_rpl_check(vcpu))
4587 return false;
4588 if (!code_segment_valid(vcpu))
4589 return false;
4590 if (!stack_segment_valid(vcpu))
4591 return false;
4592 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4593 return false;
4594 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4595 return false;
4596 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4597 return false;
4598 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4599 return false;
4600 if (!tr_valid(vcpu))
4601 return false;
4602 if (!ldtr_valid(vcpu))
4603 return false;
4604 }
4605 /* TODO:
4606 * - Add checks on RIP
4607 * - Add checks on RFLAGS
4608 */
4609
4610 return true;
4611}
4612
Mike Dayd77c26f2007-10-08 09:02:08 -04004613static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004614{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004615 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004616 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004617 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004618
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004619 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004620 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004621 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4622 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004623 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004624 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004625 r = kvm_write_guest_page(kvm, fn++, &data,
4626 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004627 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004628 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004629 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4630 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004631 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004632 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4633 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004634 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004635 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004636 r = kvm_write_guest_page(kvm, fn, &data,
4637 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4638 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004639out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004640 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004641 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004642}
4643
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004644static int init_rmode_identity_map(struct kvm *kvm)
4645{
Tang Chenf51770e2014-09-16 18:41:59 +08004646 int i, idx, r = 0;
Dan Williamsba049e92016-01-15 16:56:11 -08004647 kvm_pfn_t identity_map_pfn;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004648 u32 tmp;
4649
Avi Kivity089d0342009-03-23 18:26:32 +02004650 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004651 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004652
4653 /* Protect kvm->arch.ept_identity_pagetable_done. */
4654 mutex_lock(&kvm->slots_lock);
4655
Tang Chenf51770e2014-09-16 18:41:59 +08004656 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004657 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004658
Sheng Yangb927a3c2009-07-21 10:42:48 +08004659 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004660
4661 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004662 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004663 goto out2;
4664
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004665 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004666 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4667 if (r < 0)
4668 goto out;
4669 /* Set up identity-mapping pagetable for EPT in real mode */
4670 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4671 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4672 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4673 r = kvm_write_guest_page(kvm, identity_map_pfn,
4674 &tmp, i * sizeof(tmp), sizeof(tmp));
4675 if (r < 0)
4676 goto out;
4677 }
4678 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004679
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004680out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004681 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004682
4683out2:
4684 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004685 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004686}
4687
Avi Kivity6aa8b732006-12-10 02:21:36 -08004688static void seg_setup(int seg)
4689{
Mathias Krause772e0312012-08-30 01:30:19 +02004690 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004691 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004692
4693 vmcs_write16(sf->selector, 0);
4694 vmcs_writel(sf->base, 0);
4695 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004696 ar = 0x93;
4697 if (seg == VCPU_SREG_CS)
4698 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004699
4700 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004701}
4702
Sheng Yangf78e0e22007-10-29 09:40:42 +08004703static int alloc_apic_access_page(struct kvm *kvm)
4704{
Xiao Guangrong44841412012-09-07 14:14:20 +08004705 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004706 int r = 0;
4707
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004708 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004709 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004710 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004711 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4712 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004713 if (r)
4714 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004715
Tang Chen73a6d942014-09-11 13:38:00 +08004716 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004717 if (is_error_page(page)) {
4718 r = -EFAULT;
4719 goto out;
4720 }
4721
Tang Chenc24ae0d2014-09-24 15:57:58 +08004722 /*
4723 * Do not pin the page in memory, so that memory hot-unplug
4724 * is able to migrate it.
4725 */
4726 put_page(page);
4727 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004728out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004729 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004730 return r;
4731}
4732
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004733static int alloc_identity_pagetable(struct kvm *kvm)
4734{
Tang Chena255d472014-09-16 18:41:58 +08004735 /* Called with kvm->slots_lock held. */
4736
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004737 int r = 0;
4738
Tang Chena255d472014-09-16 18:41:58 +08004739 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4740
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004741 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4742 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004743
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004744 return r;
4745}
4746
Wanpeng Li991e7a02015-09-16 17:30:05 +08004747static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004748{
4749 int vpid;
4750
Avi Kivity919818a2009-03-23 18:01:29 +02004751 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004752 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004753 spin_lock(&vmx_vpid_lock);
4754 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004755 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004756 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004757 else
4758 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004759 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004760 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004761}
4762
Wanpeng Li991e7a02015-09-16 17:30:05 +08004763static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004764{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004765 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004766 return;
4767 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004768 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004769 spin_unlock(&vmx_vpid_lock);
4770}
4771
Yang Zhang8d146952013-01-25 10:18:50 +08004772#define MSR_TYPE_R 1
4773#define MSR_TYPE_W 2
4774static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4775 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004776{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004777 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004778
4779 if (!cpu_has_vmx_msr_bitmap())
4780 return;
4781
4782 /*
4783 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4784 * have the write-low and read-high bitmap offsets the wrong way round.
4785 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4786 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004787 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004788 if (type & MSR_TYPE_R)
4789 /* read-low */
4790 __clear_bit(msr, msr_bitmap + 0x000 / f);
4791
4792 if (type & MSR_TYPE_W)
4793 /* write-low */
4794 __clear_bit(msr, msr_bitmap + 0x800 / f);
4795
Sheng Yang25c5f222008-03-28 13:18:56 +08004796 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4797 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004798 if (type & MSR_TYPE_R)
4799 /* read-high */
4800 __clear_bit(msr, msr_bitmap + 0x400 / f);
4801
4802 if (type & MSR_TYPE_W)
4803 /* write-high */
4804 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4805
4806 }
4807}
4808
Wincy Vanf2b93282015-02-03 23:56:03 +08004809/*
4810 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4811 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4812 */
4813static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4814 unsigned long *msr_bitmap_nested,
4815 u32 msr, int type)
4816{
4817 int f = sizeof(unsigned long);
4818
4819 if (!cpu_has_vmx_msr_bitmap()) {
4820 WARN_ON(1);
4821 return;
4822 }
4823
4824 /*
4825 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4826 * have the write-low and read-high bitmap offsets the wrong way round.
4827 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4828 */
4829 if (msr <= 0x1fff) {
4830 if (type & MSR_TYPE_R &&
4831 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4832 /* read-low */
4833 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4834
4835 if (type & MSR_TYPE_W &&
4836 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4837 /* write-low */
4838 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4839
4840 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4841 msr &= 0x1fff;
4842 if (type & MSR_TYPE_R &&
4843 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4844 /* read-high */
4845 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4846
4847 if (type & MSR_TYPE_W &&
4848 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4849 /* write-high */
4850 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4851
4852 }
4853}
4854
Avi Kivity58972972009-02-24 22:26:47 +02004855static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4856{
4857 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004858 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4859 msr, MSR_TYPE_R | MSR_TYPE_W);
4860 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4861 msr, MSR_TYPE_R | MSR_TYPE_W);
4862}
4863
Radim Krčmář2e69f862016-09-29 22:41:32 +02004864static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_active)
Yang Zhang8d146952013-01-25 10:18:50 +08004865{
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004866 if (apicv_active) {
Wanpeng Lic63e4562016-09-23 19:17:16 +08004867 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv,
Radim Krčmář2e69f862016-09-29 22:41:32 +02004868 msr, type);
Wanpeng Lic63e4562016-09-23 19:17:16 +08004869 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv,
Radim Krčmář2e69f862016-09-29 22:41:32 +02004870 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004871 } else {
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004872 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02004873 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004874 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
Radim Krčmář2e69f862016-09-29 22:41:32 +02004875 msr, type);
Wanpeng Lif6e90f92016-09-22 07:43:25 +08004876 }
Avi Kivity58972972009-02-24 22:26:47 +02004877}
4878
Andrey Smetanind62caab2015-11-10 15:36:33 +03004879static bool vmx_get_enable_apicv(void)
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004880{
Andrey Smetanind62caab2015-11-10 15:36:33 +03004881 return enable_apicv;
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004882}
4883
David Hildenbrand6342c502017-01-25 11:58:58 +01004884static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
Wincy Van705699a2015-02-03 23:58:17 +08004885{
4886 struct vcpu_vmx *vmx = to_vmx(vcpu);
4887 int max_irr;
4888 void *vapic_page;
4889 u16 status;
4890
4891 if (vmx->nested.pi_desc &&
4892 vmx->nested.pi_pending) {
4893 vmx->nested.pi_pending = false;
4894 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
David Hildenbrand6342c502017-01-25 11:58:58 +01004895 return;
Wincy Van705699a2015-02-03 23:58:17 +08004896
4897 max_irr = find_last_bit(
4898 (unsigned long *)vmx->nested.pi_desc->pir, 256);
4899
4900 if (max_irr == 256)
David Hildenbrand6342c502017-01-25 11:58:58 +01004901 return;
Wincy Van705699a2015-02-03 23:58:17 +08004902
4903 vapic_page = kmap(vmx->nested.virtual_apic_page);
Wincy Van705699a2015-02-03 23:58:17 +08004904 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4905 kunmap(vmx->nested.virtual_apic_page);
4906
4907 status = vmcs_read16(GUEST_INTR_STATUS);
4908 if ((u8)max_irr > ((u8)status & 0xff)) {
4909 status &= ~0xff;
4910 status |= (u8)max_irr;
4911 vmcs_write16(GUEST_INTR_STATUS, status);
4912 }
4913 }
Wincy Van705699a2015-02-03 23:58:17 +08004914}
4915
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004916static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
4917{
4918#ifdef CONFIG_SMP
4919 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08004920 struct vcpu_vmx *vmx = to_vmx(vcpu);
4921
4922 /*
4923 * Currently, we don't support urgent interrupt,
4924 * all interrupts are recognized as non-urgent
4925 * interrupt, so we cannot post interrupts when
4926 * 'SN' is set.
4927 *
4928 * If the vcpu is in guest mode, it means it is
4929 * running instead of being scheduled out and
4930 * waiting in the run queue, and that's the only
4931 * case when 'SN' is set currently, warning if
4932 * 'SN' is set.
4933 */
4934 WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc));
4935
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004936 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4937 POSTED_INTR_VECTOR);
4938 return true;
4939 }
4940#endif
4941 return false;
4942}
4943
Wincy Van705699a2015-02-03 23:58:17 +08004944static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4945 int vector)
4946{
4947 struct vcpu_vmx *vmx = to_vmx(vcpu);
4948
4949 if (is_guest_mode(vcpu) &&
4950 vector == vmx->nested.posted_intr_nv) {
4951 /* the PIR and ON have been set by L1. */
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004952 kvm_vcpu_trigger_posted_interrupt(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004953 /*
4954 * If a posted intr is not recognized by hardware,
4955 * we will accomplish it in the next vmentry.
4956 */
4957 vmx->nested.pi_pending = true;
4958 kvm_make_request(KVM_REQ_EVENT, vcpu);
4959 return 0;
4960 }
4961 return -1;
4962}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004963/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004964 * Send interrupt to vcpu via posted interrupt way.
4965 * 1. If target vcpu is running(non-root mode), send posted interrupt
4966 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4967 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4968 * interrupt from PIR in next vmentry.
4969 */
4970static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4971{
4972 struct vcpu_vmx *vmx = to_vmx(vcpu);
4973 int r;
4974
Wincy Van705699a2015-02-03 23:58:17 +08004975 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4976 if (!r)
4977 return;
4978
Yang Zhanga20ed542013-04-11 19:25:15 +08004979 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4980 return;
4981
Paolo Bonzinib95234c2016-12-19 13:57:33 +01004982 /* If a previous notification has sent the IPI, nothing to do. */
4983 if (pi_test_and_set_on(&vmx->pi_desc))
4984 return;
4985
4986 if (!kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08004987 kvm_vcpu_kick(vcpu);
4988}
4989
Avi Kivity6aa8b732006-12-10 02:21:36 -08004990/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004991 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4992 * will not change in the lifetime of the guest.
4993 * Note that host-state that does change is set elsewhere. E.g., host-state
4994 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4995 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004996static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004997{
4998 u32 low32, high32;
4999 unsigned long tmpl;
5000 struct desc_ptr dt;
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005001 unsigned long cr0, cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005002
Andy Lutomirski04ac88a2016-10-31 15:18:45 -07005003 cr0 = read_cr0();
5004 WARN_ON(cr0 & X86_CR0_TS);
5005 vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005006 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
5007
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005008 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07005009 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07005010 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
5011 vmx->host_state.vmcs_host_cr4 = cr4;
5012
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005013 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005014#ifdef CONFIG_X86_64
5015 /*
5016 * Load null selectors, so we can avoid reloading them in
5017 * __vmx_load_host_state(), in case userspace uses the null selectors
5018 * too (the expected case).
5019 */
5020 vmcs_write16(HOST_DS_SELECTOR, 0);
5021 vmcs_write16(HOST_ES_SELECTOR, 0);
5022#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005023 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5024 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03005025#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005026 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
5027 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
5028
5029 native_store_idt(&dt);
5030 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005031 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005032
Avi Kivity83287ea422012-09-16 15:10:57 +03005033 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005034
5035 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
5036 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
5037 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
5038 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
5039
5040 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
5041 rdmsr(MSR_IA32_CR_PAT, low32, high32);
5042 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
5043 }
5044}
5045
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005046static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
5047{
5048 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
5049 if (enable_ept)
5050 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005051 if (is_guest_mode(&vmx->vcpu))
5052 vmx->vcpu.arch.cr4_guest_owned_bits &=
5053 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005054 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
5055}
5056
Yang Zhang01e439b2013-04-11 19:25:12 +08005057static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
5058{
5059 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
5060
Andrey Smetanind62caab2015-11-10 15:36:33 +03005061 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005062 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
Yunhong Jiang64672c92016-06-13 14:19:59 -07005063 /* Enable the preemption timer dynamically */
5064 pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
Yang Zhang01e439b2013-04-11 19:25:12 +08005065 return pin_based_exec_ctrl;
5066}
5067
Andrey Smetanind62caab2015-11-10 15:36:33 +03005068static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
5069{
5070 struct vcpu_vmx *vmx = to_vmx(vcpu);
5071
5072 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Roman Kagan3ce424e2016-05-18 17:48:20 +03005073 if (cpu_has_secondary_exec_ctrls()) {
5074 if (kvm_vcpu_apicv_active(vcpu))
5075 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
5076 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5077 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5078 else
5079 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
5080 SECONDARY_EXEC_APIC_REGISTER_VIRT |
5081 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
5082 }
5083
5084 if (cpu_has_vmx_msr_bitmap())
5085 vmx_set_msr_bitmap(vcpu);
Andrey Smetanind62caab2015-11-10 15:36:33 +03005086}
5087
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005088static u32 vmx_exec_control(struct vcpu_vmx *vmx)
5089{
5090 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01005091
5092 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
5093 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5094
Paolo Bonzini35754c92015-07-29 12:05:37 +02005095 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005096 exec_control &= ~CPU_BASED_TPR_SHADOW;
5097#ifdef CONFIG_X86_64
5098 exec_control |= CPU_BASED_CR8_STORE_EXITING |
5099 CPU_BASED_CR8_LOAD_EXITING;
5100#endif
5101 }
5102 if (!enable_ept)
5103 exec_control |= CPU_BASED_CR3_STORE_EXITING |
5104 CPU_BASED_CR3_LOAD_EXITING |
5105 CPU_BASED_INVLPG_EXITING;
5106 return exec_control;
5107}
5108
5109static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
5110{
5111 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02005112 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005113 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
5114 if (vmx->vpid == 0)
5115 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
5116 if (!enable_ept) {
5117 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
5118 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00005119 /* Enable INVPCID for non-ept guests may cause performance regression. */
5120 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005121 }
5122 if (!enable_unrestricted_guest)
5123 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
5124 if (!ple_gap)
5125 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Andrey Smetanind62caab2015-11-10 15:36:33 +03005126 if (!kvm_vcpu_apicv_active(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08005127 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
5128 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08005129 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03005130 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
5131 (handle_vmptrld).
5132 We can NOT enable shadow_vmcs here because we don't have yet
5133 a current VMCS12
5134 */
5135 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08005136
5137 if (!enable_pml)
5138 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08005139
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005140 return exec_control;
5141}
5142
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005143static void ept_set_mmio_spte_mask(void)
5144{
5145 /*
5146 * EPT Misconfigurations can be generated if the value of bits 2:0
5147 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005148 */
Junaid Shahid312b6162016-12-21 20:29:29 -08005149 kvm_mmu_set_mmio_spte_mask(VMX_EPT_MISCONFIG_WX_VALUE);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005150}
5151
Wanpeng Lif53cd632014-12-02 19:14:58 +08005152#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03005153/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005154 * Sets up the vmcs for emulated real mode.
5155 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10005156static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005157{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005158#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005159 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02005160#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08005161 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005162
Avi Kivity6aa8b732006-12-10 02:21:36 -08005163 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02005164 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
5165 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005166
Abel Gordon4607c2d2013-04-18 14:35:55 +03005167 if (enable_shadow_vmcs) {
5168 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
5169 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
5170 }
Sheng Yang25c5f222008-03-28 13:18:56 +08005171 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02005172 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08005173
Avi Kivity6aa8b732006-12-10 02:21:36 -08005174 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5175
Avi Kivity6aa8b732006-12-10 02:21:36 -08005176 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08005177 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yunhong Jiang64672c92016-06-13 14:19:59 -07005178 vmx->hv_deadline_tsc = -1;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005179
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005180 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005181
Dan Williamsdfa169b2016-06-02 11:17:24 -07005182 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005183 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
5184 vmx_secondary_exec_control(vmx));
Dan Williamsdfa169b2016-06-02 11:17:24 -07005185 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08005186
Andrey Smetanind62caab2015-11-10 15:36:33 +03005187 if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08005188 vmcs_write64(EOI_EXIT_BITMAP0, 0);
5189 vmcs_write64(EOI_EXIT_BITMAP1, 0);
5190 vmcs_write64(EOI_EXIT_BITMAP2, 0);
5191 vmcs_write64(EOI_EXIT_BITMAP3, 0);
5192
5193 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08005194
Li RongQing0bcf2612015-12-03 13:29:34 +08005195 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Yang Zhang01e439b2013-04-11 19:25:12 +08005196 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08005197 }
5198
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005199 if (ple_gap) {
5200 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02005201 vmx->ple_window = ple_window;
5202 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005203 }
5204
Xiao Guangrongc3707952011-07-12 03:28:04 +08005205 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5206 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005207 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5208
Avi Kivity9581d442010-10-19 16:46:55 +02005209 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5210 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08005211 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08005212#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08005213 rdmsrl(MSR_FS_BASE, a);
5214 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
5215 rdmsrl(MSR_GS_BASE, a);
5216 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
5217#else
5218 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5219 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5220#endif
5221
Eddie Dong2cc51562007-05-21 07:28:09 +03005222 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5223 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005224 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03005225 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03005226 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08005227
Radim Krčmář74545702015-04-27 15:11:25 +02005228 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5229 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08005230
Paolo Bonzini03916db2014-07-24 14:21:57 +02005231 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005232 u32 index = vmx_msr_index[i];
5233 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005234 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005235
5236 if (rdmsr_safe(index, &data_low, &data_high) < 0)
5237 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08005238 if (wrmsr_safe(index, data_low, data_high) < 0)
5239 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03005240 vmx->guest_msrs[j].index = i;
5241 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02005242 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04005243 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005244 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005245
Gleb Natapov2961e8762013-11-25 15:37:13 +02005246
5247 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005248
5249 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02005250 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03005251
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005252 vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5253 vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5254
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03005255 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005256
Wanpeng Lif53cd632014-12-02 19:14:58 +08005257 if (vmx_xsaves_supported())
5258 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5259
Peter Feiner4e595162016-07-07 14:49:58 -07005260 if (enable_pml) {
5261 ASSERT(vmx->pml_pg);
5262 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5263 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5264 }
5265
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005266 return 0;
5267}
5268
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005269static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005270{
5271 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01005272 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005273 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005274
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005275 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005276
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005277 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005278 kvm_set_cr8(vcpu, 0);
5279
5280 if (!init_event) {
5281 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5282 MSR_IA32_APICBASE_ENABLE;
5283 if (kvm_vcpu_is_reset_bsp(vcpu))
5284 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5285 apic_base_msr.host_initiated = true;
5286 kvm_set_apic_base(vcpu, &apic_base_msr);
5287 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005288
Avi Kivity2fb92db2011-04-27 19:42:18 +03005289 vmx_segment_cache_clear(vmx);
5290
Avi Kivity5706be02008-08-20 15:07:31 +03005291 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01005292 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005293 vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005294
5295 seg_setup(VCPU_SREG_DS);
5296 seg_setup(VCPU_SREG_ES);
5297 seg_setup(VCPU_SREG_FS);
5298 seg_setup(VCPU_SREG_GS);
5299 seg_setup(VCPU_SREG_SS);
5300
5301 vmcs_write16(GUEST_TR_SELECTOR, 0);
5302 vmcs_writel(GUEST_TR_BASE, 0);
5303 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5304 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5305
5306 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5307 vmcs_writel(GUEST_LDTR_BASE, 0);
5308 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5309 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5310
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005311 if (!init_event) {
5312 vmcs_write32(GUEST_SYSENTER_CS, 0);
5313 vmcs_writel(GUEST_SYSENTER_ESP, 0);
5314 vmcs_writel(GUEST_SYSENTER_EIP, 0);
5315 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5316 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005317
5318 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01005319 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005320
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005321 vmcs_writel(GUEST_GDTR_BASE, 0);
5322 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5323
5324 vmcs_writel(GUEST_IDTR_BASE, 0);
5325 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5326
Anthony Liguori443381a2010-12-06 10:53:38 -06005327 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005328 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
Paolo Bonzinif3531052015-12-03 15:49:56 +01005329 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005330
Avi Kivitye00c8cf2007-10-21 11:00:39 +02005331 setup_msrs(vmx);
5332
Avi Kivity6aa8b732006-12-10 02:21:36 -08005333 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5334
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005335 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08005336 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005337 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08005338 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005339 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08005340 vmcs_write32(TPR_THRESHOLD, 0);
5341 }
5342
Paolo Bonzinia73896c2014-11-02 07:54:30 +01005343 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005344
Andrey Smetanind62caab2015-11-10 15:36:33 +03005345 if (kvm_vcpu_apicv_active(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08005346 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
5347
Sheng Yang2384d2b2008-01-17 15:14:33 +08005348 if (vmx->vpid != 0)
5349 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5350
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005351 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005352 vmx->vcpu.arch.cr0 = cr0;
Bruce Rogersf2463242016-04-28 14:49:21 -06005353 vmx_set_cr0(vcpu, cr0); /* enter rmode */
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005354 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02005355 vmx_set_efer(vcpu, 0);
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005356
Nadav Amitd28bc9d2015-04-13 14:34:08 +03005357 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005358
Wanpeng Lidd5f5342015-09-23 18:26:57 +08005359 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005360}
5361
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005362/*
5363 * In nested virtualization, check if L1 asked to exit on external interrupts.
5364 * For most existing hypervisors, this will always return true.
5365 */
5366static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
5367{
5368 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5369 PIN_BASED_EXT_INTR_MASK;
5370}
5371
Bandan Das77b0f5d2014-04-19 18:17:45 -04005372/*
5373 * In nested virtualization, check if L1 has set
5374 * VM_EXIT_ACK_INTR_ON_EXIT
5375 */
5376static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
5377{
5378 return get_vmcs12(vcpu)->vm_exit_controls &
5379 VM_EXIT_ACK_INTR_ON_EXIT;
5380}
5381
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005382static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
5383{
5384 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
5385 PIN_BASED_NMI_EXITING;
5386}
5387
Jan Kiszkac9a79532014-03-07 20:03:15 +01005388static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005389{
Paolo Bonzini47c01522016-12-19 11:44:07 +01005390 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5391 CPU_BASED_VIRTUAL_INTR_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005392}
5393
Jan Kiszkac9a79532014-03-07 20:03:15 +01005394static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005395{
Paolo Bonzini2c828782017-03-27 14:37:28 +02005396 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
Jan Kiszkac9a79532014-03-07 20:03:15 +01005397 enable_irq_window(vcpu);
5398 return;
5399 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02005400
Paolo Bonzini47c01522016-12-19 11:44:07 +01005401 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL,
5402 CPU_BASED_VIRTUAL_NMI_PENDING);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005403}
5404
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005405static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03005406{
Avi Kivity9c8cba32007-11-22 11:42:59 +02005407 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005408 uint32_t intr;
5409 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02005410
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005411 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005412
Avi Kivityfa89a812008-09-01 15:57:51 +03005413 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005414 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005415 int inc_eip = 0;
5416 if (vcpu->arch.interrupt.soft)
5417 inc_eip = vcpu->arch.event_exit_inst_len;
5418 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005419 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005420 return;
5421 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005422 intr = irq | INTR_INFO_VALID_MASK;
5423 if (vcpu->arch.interrupt.soft) {
5424 intr |= INTR_TYPE_SOFT_INTR;
5425 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5426 vmx->vcpu.arch.event_exit_inst_len);
5427 } else
5428 intr |= INTR_TYPE_EXT_INTR;
5429 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005430}
5431
Sheng Yangf08864b2008-05-15 18:23:25 +08005432static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5433{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005434 struct vcpu_vmx *vmx = to_vmx(vcpu);
5435
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005436 if (!is_guest_mode(vcpu)) {
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005437 ++vcpu->stat.nmi_injections;
5438 vmx->nmi_known_unmasked = false;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005439 }
5440
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005441 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005442 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005443 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005444 return;
5445 }
Wanpeng Lic5a6d5f2016-09-22 17:55:54 +08005446
Sheng Yangf08864b2008-05-15 18:23:25 +08005447 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5448 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005449}
5450
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005451static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5452{
Avi Kivity9d58b932011-03-07 16:52:07 +02005453 if (to_vmx(vcpu)->nmi_known_unmasked)
5454 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005455 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005456}
5457
5458static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5459{
5460 struct vcpu_vmx *vmx = to_vmx(vcpu);
5461
Paolo Bonzini2c828782017-03-27 14:37:28 +02005462 vmx->nmi_known_unmasked = !masked;
5463 if (masked)
5464 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5465 GUEST_INTR_STATE_NMI);
5466 else
5467 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5468 GUEST_INTR_STATE_NMI);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005469}
5470
Jan Kiszka2505dc92013-04-14 12:12:47 +02005471static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5472{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005473 if (to_vmx(vcpu)->nested.nested_run_pending)
5474 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005475
Jan Kiszka2505dc92013-04-14 12:12:47 +02005476 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5477 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5478 | GUEST_INTR_STATE_NMI));
5479}
5480
Gleb Natapov78646122009-03-23 12:12:11 +02005481static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5482{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005483 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5484 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005485 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5486 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005487}
5488
Izik Eiduscbc94022007-10-25 00:29:55 +02005489static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5490{
5491 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005492
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005493 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5494 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005495 if (ret)
5496 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005497 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005498 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005499}
5500
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005501static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005502{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005503 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005504 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01005505 /*
5506 * Update instruction length as we may reinject the exception
5507 * from user space while in guest debugging mode.
5508 */
5509 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5510 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005511 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005512 return false;
5513 /* fall through */
5514 case DB_VECTOR:
5515 if (vcpu->guest_debug &
5516 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5517 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005518 /* fall through */
5519 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005520 case OF_VECTOR:
5521 case BR_VECTOR:
5522 case UD_VECTOR:
5523 case DF_VECTOR:
5524 case SS_VECTOR:
5525 case GP_VECTOR:
5526 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005527 return true;
5528 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005529 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005530 return false;
5531}
5532
5533static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5534 int vec, u32 err_code)
5535{
5536 /*
5537 * Instruction with address size override prefix opcode 0x67
5538 * Cause the #SS fault with 0 error code in VM86 mode.
5539 */
5540 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5541 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5542 if (vcpu->arch.halt_request) {
5543 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005544 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005545 }
5546 return 1;
5547 }
5548 return 0;
5549 }
5550
5551 /*
5552 * Forward all other exceptions that are valid in real mode.
5553 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5554 * the required debugging infrastructure rework.
5555 */
5556 kvm_queue_exception(vcpu, vec);
5557 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005558}
5559
Andi Kleena0861c02009-06-08 17:37:09 +08005560/*
5561 * Trigger machine check on the host. We assume all the MSRs are already set up
5562 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5563 * We pass a fake environment to the machine check handler because we want
5564 * the guest to be always treated like user space, no matter what context
5565 * it used internally.
5566 */
5567static void kvm_machine_check(void)
5568{
5569#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5570 struct pt_regs regs = {
5571 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5572 .flags = X86_EFLAGS_IF,
5573 };
5574
5575 do_machine_check(&regs, 0);
5576#endif
5577}
5578
Avi Kivity851ba692009-08-24 11:10:17 +03005579static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005580{
5581 /* already handled by vcpu_run */
5582 return 1;
5583}
5584
Avi Kivity851ba692009-08-24 11:10:17 +03005585static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005586{
Avi Kivity1155f762007-11-22 11:30:47 +02005587 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005588 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005589 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005590 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005591 u32 vect_info;
5592 enum emulation_result er;
5593
Avi Kivity1155f762007-11-22 11:30:47 +02005594 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005595 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005596
Andi Kleena0861c02009-06-08 17:37:09 +08005597 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005598 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005599
Jim Mattsonef85b672016-12-12 11:01:37 -08005600 if (is_nmi(intr_info))
Avi Kivity1b6269d2007-10-09 12:12:19 +02005601 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005602
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005603 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005604 if (is_guest_mode(vcpu)) {
5605 kvm_queue_exception(vcpu, UD_VECTOR);
5606 return 1;
5607 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005608 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005609 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005610 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005611 return 1;
5612 }
5613
Avi Kivity6aa8b732006-12-10 02:21:36 -08005614 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005615 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005616 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005617
5618 /*
5619 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5620 * MMIO, it is better to report an internal error.
5621 * See the comments in vmx_handle_exit.
5622 */
5623 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5624 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5625 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5626 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005627 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005628 vcpu->run->internal.data[0] = vect_info;
5629 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005630 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005631 return 0;
5632 }
5633
Avi Kivity6aa8b732006-12-10 02:21:36 -08005634 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005635 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005636 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005637 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005638 trace_kvm_page_fault(cr2, error_code);
5639
Gleb Natapov3298b752009-05-11 13:35:46 +03005640 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005641 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005642 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005643 }
5644
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005645 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005646
5647 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5648 return handle_rmode_exception(vcpu, ex_no, error_code);
5649
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005650 switch (ex_no) {
Eric Northup54a20552015-11-03 18:03:53 +01005651 case AC_VECTOR:
5652 kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5653 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005654 case DB_VECTOR:
5655 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5656 if (!(vcpu->guest_debug &
5657 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005658 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005659 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01005660 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5661 skip_emulated_instruction(vcpu);
5662
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005663 kvm_queue_exception(vcpu, DB_VECTOR);
5664 return 1;
5665 }
5666 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5667 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5668 /* fall through */
5669 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01005670 /*
5671 * Update instruction length as we may reinject #BP from
5672 * user space while in guest debugging mode. Reading it for
5673 * #DB as well causes no harm, it is not used in that case.
5674 */
5675 vmx->vcpu.arch.event_exit_inst_len =
5676 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005677 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005678 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005679 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5680 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005681 break;
5682 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005683 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5684 kvm_run->ex.exception = ex_no;
5685 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005686 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005687 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005688 return 0;
5689}
5690
Avi Kivity851ba692009-08-24 11:10:17 +03005691static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005692{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005693 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005694 return 1;
5695}
5696
Avi Kivity851ba692009-08-24 11:10:17 +03005697static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005698{
Avi Kivity851ba692009-08-24 11:10:17 +03005699 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005700 return 0;
5701}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005702
Avi Kivity851ba692009-08-24 11:10:17 +03005703static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005704{
He, Qingbfdaab02007-09-12 14:18:28 +08005705 unsigned long exit_qualification;
Kyle Huey6affcbe2016-11-29 12:40:40 -08005706 int size, in, string, ret;
Avi Kivity039576c2007-03-20 12:46:50 +02005707 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005708
He, Qingbfdaab02007-09-12 14:18:28 +08005709 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005710 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005711 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005712
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005713 ++vcpu->stat.io_exits;
5714
5715 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005716 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005717
5718 port = exit_qualification >> 16;
5719 size = (exit_qualification & 7) + 1;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005720
Kyle Huey6affcbe2016-11-29 12:40:40 -08005721 ret = kvm_skip_emulated_instruction(vcpu);
5722
5723 /*
5724 * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered
5725 * KVM_EXIT_DEBUG here.
5726 */
5727 return kvm_fast_pio_out(vcpu, size, port) && ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005728}
5729
Ingo Molnar102d8322007-02-19 14:37:47 +02005730static void
5731vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5732{
5733 /*
5734 * Patch in the VMCALL instruction:
5735 */
5736 hypercall[0] = 0x0f;
5737 hypercall[1] = 0x01;
5738 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005739}
5740
Guo Chao0fa06072012-06-28 15:16:19 +08005741/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005742static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5743{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005744 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005745 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5746 unsigned long orig_val = val;
5747
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005748 /*
5749 * We get here when L2 changed cr0 in a way that did not change
5750 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005751 * but did change L0 shadowed bits. So we first calculate the
5752 * effective cr0 value that L1 would like to write into the
5753 * hardware. It consists of the L2-owned bits from the new
5754 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005755 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005756 val = (val & ~vmcs12->cr0_guest_host_mask) |
5757 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5758
David Matlack38991522016-11-29 18:14:08 -08005759 if (!nested_guest_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005760 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005761
5762 if (kvm_set_cr0(vcpu, val))
5763 return 1;
5764 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005765 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005766 } else {
5767 if (to_vmx(vcpu)->nested.vmxon &&
David Matlack38991522016-11-29 18:14:08 -08005768 !nested_host_cr0_valid(vcpu, val))
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005769 return 1;
David Matlack38991522016-11-29 18:14:08 -08005770
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005771 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005772 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005773}
5774
5775static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5776{
5777 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005778 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5779 unsigned long orig_val = val;
5780
5781 /* analogously to handle_set_cr0 */
5782 val = (val & ~vmcs12->cr4_guest_host_mask) |
5783 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5784 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005785 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005786 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005787 return 0;
5788 } else
5789 return kvm_set_cr4(vcpu, val);
5790}
5791
Avi Kivity851ba692009-08-24 11:10:17 +03005792static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005793{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005794 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005795 int cr;
5796 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005797 int err;
Kyle Huey6affcbe2016-11-29 12:40:40 -08005798 int ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005799
He, Qingbfdaab02007-09-12 14:18:28 +08005800 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005801 cr = exit_qualification & 15;
5802 reg = (exit_qualification >> 8) & 15;
5803 switch ((exit_qualification >> 4) & 3) {
5804 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005805 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005806 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005807 switch (cr) {
5808 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005809 err = handle_set_cr0(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005810 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005811 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005812 err = kvm_set_cr3(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005813 return kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005814 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005815 err = handle_set_cr4(vcpu, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005816 return kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005817 case 8: {
5818 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005819 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005820 err = kvm_set_cr8(vcpu, cr8);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005821 ret = kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005822 if (lapic_in_kernel(vcpu))
Kyle Huey6affcbe2016-11-29 12:40:40 -08005823 return ret;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005824 if (cr8_prev <= cr8)
Kyle Huey6affcbe2016-11-29 12:40:40 -08005825 return ret;
5826 /*
5827 * TODO: we might be squashing a
5828 * KVM_GUESTDBG_SINGLESTEP-triggered
5829 * KVM_EXIT_DEBUG here.
5830 */
Avi Kivity851ba692009-08-24 11:10:17 +03005831 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005832 return 0;
5833 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005834 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005835 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005836 case 2: /* clts */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08005837 WARN_ONCE(1, "Guest should always own CR0.TS");
5838 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02005839 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Kyle Huey6affcbe2016-11-29 12:40:40 -08005840 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005841 case 1: /*mov from cr*/
5842 switch (cr) {
5843 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005844 val = kvm_read_cr3(vcpu);
5845 kvm_register_write(vcpu, reg, val);
5846 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005847 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005848 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005849 val = kvm_get_cr8(vcpu);
5850 kvm_register_write(vcpu, reg, val);
5851 trace_kvm_cr_read(cr, val);
Kyle Huey6affcbe2016-11-29 12:40:40 -08005852 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005853 }
5854 break;
5855 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005856 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005857 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005858 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005859
Kyle Huey6affcbe2016-11-29 12:40:40 -08005860 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005861 default:
5862 break;
5863 }
Avi Kivity851ba692009-08-24 11:10:17 +03005864 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005865 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005866 (int)(exit_qualification >> 4) & 3, cr);
5867 return 0;
5868}
5869
Avi Kivity851ba692009-08-24 11:10:17 +03005870static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005871{
He, Qingbfdaab02007-09-12 14:18:28 +08005872 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005873 int dr, dr7, reg;
5874
5875 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5876 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5877
5878 /* First, if DR does not exist, trigger UD */
5879 if (!kvm_require_dr(vcpu, dr))
5880 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005881
Jan Kiszkaf2483412010-01-20 18:20:20 +01005882 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005883 if (!kvm_require_cpl(vcpu, 0))
5884 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005885 dr7 = vmcs_readl(GUEST_DR7);
5886 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005887 /*
5888 * As the vm-exit takes precedence over the debug trap, we
5889 * need to emulate the latter, either for the host or the
5890 * guest debugging itself.
5891 */
5892 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005893 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005894 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005895 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005896 vcpu->run->debug.arch.exception = DB_VECTOR;
5897 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005898 return 0;
5899 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02005900 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005901 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005902 kvm_queue_exception(vcpu, DB_VECTOR);
5903 return 1;
5904 }
5905 }
5906
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005907 if (vcpu->guest_debug == 0) {
Paolo Bonzini8f223722016-02-26 12:09:49 +01005908 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
5909 CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005910
5911 /*
5912 * No more DR vmexits; force a reload of the debug registers
5913 * and reenter on this instruction. The next vmexit will
5914 * retrieve the full state of the debug registers.
5915 */
5916 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5917 return 1;
5918 }
5919
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005920 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5921 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005922 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005923
5924 if (kvm_get_dr(vcpu, dr, &val))
5925 return 1;
5926 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005927 } else
Nadav Amit57773922014-06-18 17:19:23 +03005928 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005929 return 1;
5930
Kyle Huey6affcbe2016-11-29 12:40:40 -08005931 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005932}
5933
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005934static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5935{
5936 return vcpu->arch.dr6;
5937}
5938
5939static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5940{
5941}
5942
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005943static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5944{
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005945 get_debugreg(vcpu->arch.db[0], 0);
5946 get_debugreg(vcpu->arch.db[1], 1);
5947 get_debugreg(vcpu->arch.db[2], 2);
5948 get_debugreg(vcpu->arch.db[3], 3);
5949 get_debugreg(vcpu->arch.dr6, 6);
5950 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5951
5952 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzini8f223722016-02-26 12:09:49 +01005953 vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING);
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005954}
5955
Gleb Natapov020df072010-04-13 10:05:23 +03005956static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5957{
5958 vmcs_writel(GUEST_DR7, val);
5959}
5960
Avi Kivity851ba692009-08-24 11:10:17 +03005961static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005962{
Kyle Huey6a908b62016-11-29 12:40:37 -08005963 return kvm_emulate_cpuid(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005964}
5965
Avi Kivity851ba692009-08-24 11:10:17 +03005966static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005967{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005968 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005969 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005970
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005971 msr_info.index = ecx;
5972 msr_info.host_initiated = false;
5973 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02005974 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005975 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005976 return 1;
5977 }
5978
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005979 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005980
Avi Kivity6aa8b732006-12-10 02:21:36 -08005981 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005982 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
5983 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Kyle Huey6affcbe2016-11-29 12:40:40 -08005984 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005985}
5986
Avi Kivity851ba692009-08-24 11:10:17 +03005987static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005988{
Will Auld8fe8ab42012-11-29 12:42:12 -08005989 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005990 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5991 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5992 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005993
Will Auld8fe8ab42012-11-29 12:42:12 -08005994 msr.data = data;
5995 msr.index = ecx;
5996 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03005997 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005998 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005999 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006000 return 1;
6001 }
6002
Avi Kivity59200272010-01-25 19:47:02 +02006003 trace_kvm_msr_write(ecx, data);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006004 return kvm_skip_emulated_instruction(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006005}
6006
Avi Kivity851ba692009-08-24 11:10:17 +03006007static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006008{
Paolo Bonzinieb90f342016-12-18 14:02:21 +01006009 kvm_apic_update_ppr(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006010 return 1;
6011}
6012
Avi Kivity851ba692009-08-24 11:10:17 +03006013static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006014{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006015 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6016 CPU_BASED_VIRTUAL_INTR_PENDING);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04006017
Avi Kivity3842d132010-07-27 12:30:24 +03006018 kvm_make_request(KVM_REQ_EVENT, vcpu);
6019
Jan Kiszkaa26bf122008-09-26 09:30:45 +02006020 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006021 return 1;
6022}
6023
Avi Kivity851ba692009-08-24 11:10:17 +03006024static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006025{
Avi Kivityd3bef152007-06-05 15:53:05 +03006026 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006027}
6028
Avi Kivity851ba692009-08-24 11:10:17 +03006029static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02006030{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03006031 return kvm_emulate_hypercall(vcpu);
Ingo Molnarc21415e2007-02-19 14:37:47 +02006032}
6033
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006034static int handle_invd(struct kvm_vcpu *vcpu)
6035{
Andre Przywara51d8b662010-12-21 11:12:02 +01006036 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006037}
6038
Avi Kivity851ba692009-08-24 11:10:17 +03006039static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03006040{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006041 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006042
6043 kvm_mmu_invlpg(vcpu, exit_qualification);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006044 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03006045}
6046
Avi Kivityfee84b02011-11-10 14:57:25 +02006047static int handle_rdpmc(struct kvm_vcpu *vcpu)
6048{
6049 int err;
6050
6051 err = kvm_rdpmc(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006052 return kvm_complete_insn_gp(vcpu, err);
Avi Kivityfee84b02011-11-10 14:57:25 +02006053}
6054
Avi Kivity851ba692009-08-24 11:10:17 +03006055static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02006056{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006057 return kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02006058}
6059
Dexuan Cui2acf9232010-06-10 11:27:12 +08006060static int handle_xsetbv(struct kvm_vcpu *vcpu)
6061{
6062 u64 new_bv = kvm_read_edx_eax(vcpu);
6063 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
6064
6065 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
Kyle Huey6affcbe2016-11-29 12:40:40 -08006066 return kvm_skip_emulated_instruction(vcpu);
Dexuan Cui2acf9232010-06-10 11:27:12 +08006067 return 1;
6068}
6069
Wanpeng Lif53cd632014-12-02 19:14:58 +08006070static int handle_xsaves(struct kvm_vcpu *vcpu)
6071{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006072 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006073 WARN(1, "this should never happen\n");
6074 return 1;
6075}
6076
6077static int handle_xrstors(struct kvm_vcpu *vcpu)
6078{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006079 kvm_skip_emulated_instruction(vcpu);
Wanpeng Lif53cd632014-12-02 19:14:58 +08006080 WARN(1, "this should never happen\n");
6081 return 1;
6082}
6083
Avi Kivity851ba692009-08-24 11:10:17 +03006084static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08006085{
Kevin Tian58fbbf22011-08-30 13:56:17 +03006086 if (likely(fasteoi)) {
6087 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6088 int access_type, offset;
6089
6090 access_type = exit_qualification & APIC_ACCESS_TYPE;
6091 offset = exit_qualification & APIC_ACCESS_OFFSET;
6092 /*
6093 * Sane guest uses MOV to write EOI, with written value
6094 * not cared. So make a short-circuit here by avoiding
6095 * heavy instruction emulation.
6096 */
6097 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
6098 (offset == APIC_EOI)) {
6099 kvm_lapic_set_eoi(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006100 return kvm_skip_emulated_instruction(vcpu);
Kevin Tian58fbbf22011-08-30 13:56:17 +03006101 }
6102 }
Andre Przywara51d8b662010-12-21 11:12:02 +01006103 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08006104}
6105
Yang Zhangc7c9c562013-01-25 10:18:51 +08006106static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
6107{
6108 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6109 int vector = exit_qualification & 0xff;
6110
6111 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
6112 kvm_apic_set_eoi_accelerated(vcpu, vector);
6113 return 1;
6114}
6115
Yang Zhang83d4c282013-01-25 10:18:49 +08006116static int handle_apic_write(struct kvm_vcpu *vcpu)
6117{
6118 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6119 u32 offset = exit_qualification & 0xfff;
6120
6121 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
6122 kvm_apic_write_nodecode(vcpu, offset);
6123 return 1;
6124}
6125
Avi Kivity851ba692009-08-24 11:10:17 +03006126static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02006127{
Jan Kiszka60637aa2008-09-26 09:30:47 +02006128 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02006129 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02006130 bool has_error_code = false;
6131 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02006132 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006133 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006134
6135 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006136 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006137 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02006138
6139 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6140
6141 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006142 if (reason == TASK_SWITCH_GATE && idt_v) {
6143 switch (type) {
6144 case INTR_TYPE_NMI_INTR:
6145 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02006146 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006147 break;
6148 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006149 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006150 kvm_clear_interrupt_queue(vcpu);
6151 break;
6152 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02006153 if (vmx->idt_vectoring_info &
6154 VECTORING_INFO_DELIVER_CODE_MASK) {
6155 has_error_code = true;
6156 error_code =
6157 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6158 }
6159 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006160 case INTR_TYPE_SOFT_EXCEPTION:
6161 kvm_clear_exception_queue(vcpu);
6162 break;
6163 default:
6164 break;
6165 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02006166 }
Izik Eidus37817f22008-03-24 23:14:53 +02006167 tss_selector = exit_qualification;
6168
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006169 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
6170 type != INTR_TYPE_EXT_INTR &&
6171 type != INTR_TYPE_NMI_INTR))
6172 skip_emulated_instruction(vcpu);
6173
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01006174 if (kvm_task_switch(vcpu, tss_selector,
6175 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
6176 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03006177 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6178 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6179 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006180 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03006181 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006182
Jan Kiszka42dbaa52008-12-15 13:52:10 +01006183 /*
6184 * TODO: What about debug traps on tss switch?
6185 * Are we supposed to inject them and update dr6?
6186 */
6187
6188 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02006189}
6190
Avi Kivity851ba692009-08-24 11:10:17 +03006191static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08006192{
Sheng Yangf9c617f2009-03-25 10:08:52 +08006193 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08006194 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006195 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08006196
Sheng Yangf9c617f2009-03-25 10:08:52 +08006197 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08006198
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02006199 if (is_guest_mode(vcpu)
6200 && !(exit_qualification & EPT_VIOLATION_GVA_TRANSLATED)) {
6201 /*
6202 * Fix up exit_qualification according to whether guest
6203 * page table accesses are reads or writes.
6204 */
6205 u64 eptp = nested_ept_get_cr3(vcpu);
Radim Krčmář33251872017-04-13 18:39:18 +02006206 if (!(eptp & VMX_EPT_AD_ENABLE_BIT))
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02006207 exit_qualification &= ~EPT_VIOLATION_ACC_WRITE;
6208 }
6209
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006210 /*
6211 * EPT violation happened while executing iret from NMI,
6212 * "blocked by NMI" bit has to be set before next VM entry.
6213 * There are errata that may cause this bit to not be set:
6214 * AAK134, BY25.
6215 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006216 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Gleb Natapovbcd1c292013-09-25 10:58:22 +03006217 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03006218 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
6219
Sheng Yang14394422008-04-28 12:24:45 +08006220 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03006221 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006222
Junaid Shahid27959a42016-12-06 16:46:10 -08006223 /* Is it a read fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006224 error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
Junaid Shahid27959a42016-12-06 16:46:10 -08006225 ? PFERR_USER_MASK : 0;
6226 /* Is it a write fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006227 error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
Junaid Shahid27959a42016-12-06 16:46:10 -08006228 ? PFERR_WRITE_MASK : 0;
6229 /* Is it a fetch fault? */
Junaid Shahidab22a472016-12-21 20:29:28 -08006230 error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
Junaid Shahid27959a42016-12-06 16:46:10 -08006231 ? PFERR_FETCH_MASK : 0;
6232 /* ept page table entry is present? */
6233 error_code |= (exit_qualification &
6234 (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6235 EPT_VIOLATION_EXECUTABLE))
6236 ? PFERR_PRESENT_MASK : 0;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006237
Paolo Bonzinidb1c0562016-12-08 15:31:41 +01006238 vcpu->arch.gpa_available = true;
Yang Zhang25d92082013-08-06 12:00:32 +03006239 vcpu->arch.exit_qualification = exit_qualification;
6240
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08006241 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08006242}
6243
Avi Kivity851ba692009-08-24 11:10:17 +03006244static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006245{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006246 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006247 gpa_t gpa;
6248
6249 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00006250 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Jason Wang931c33b2015-09-15 14:41:58 +08006251 trace_kvm_fast_mmio(gpa);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006252 return kvm_skip_emulated_instruction(vcpu);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03006253 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006254
Paolo Bonzini450869d2015-11-04 13:41:21 +01006255 ret = handle_mmio_page_fault(vcpu, gpa, true);
Paolo Bonzinidb1c0562016-12-08 15:31:41 +01006256 vcpu->arch.gpa_available = true;
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006257 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006258 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
6259 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08006260
6261 if (unlikely(ret == RET_MMIO_PF_INVALID))
6262 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
6263
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08006264 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08006265 return 1;
6266
6267 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08006268 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006269
Avi Kivity851ba692009-08-24 11:10:17 +03006270 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6271 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006272
6273 return 0;
6274}
6275
Avi Kivity851ba692009-08-24 11:10:17 +03006276static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08006277{
Paolo Bonzini47c01522016-12-19 11:44:07 +01006278 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
6279 CPU_BASED_VIRTUAL_NMI_PENDING);
Sheng Yangf08864b2008-05-15 18:23:25 +08006280 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03006281 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08006282
6283 return 1;
6284}
6285
Mohammed Gamal80ced182009-09-01 12:48:18 +02006286static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006287{
Avi Kivity8b3079a2009-01-05 12:10:54 +02006288 struct vcpu_vmx *vmx = to_vmx(vcpu);
6289 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006290 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02006291 u32 cpu_exec_ctrl;
6292 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03006293 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02006294
6295 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6296 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006297
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01006298 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03006299 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02006300 return handle_interrupt_window(&vmx->vcpu);
6301
Avi Kivityde87dcdd2012-06-12 20:21:38 +03006302 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
6303 return 1;
6304
Gleb Natapov991eebf2013-04-11 12:10:51 +03006305 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006306
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02006307 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02006308 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02006309 ret = 0;
6310 goto out;
6311 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006312
Avi Kivityde5f70e2012-06-12 20:22:28 +03006313 if (err != EMULATE_DONE) {
6314 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6315 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6316 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03006317 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03006318 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006319
Gleb Natapov8d76c492013-05-08 18:38:44 +03006320 if (vcpu->arch.halt_request) {
6321 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06006322 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03006323 goto out;
6324 }
6325
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006326 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02006327 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006328 if (need_resched())
6329 schedule();
6330 }
6331
Mohammed Gamal80ced182009-09-01 12:48:18 +02006332out:
6333 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03006334}
6335
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006336static int __grow_ple_window(int val)
6337{
6338 if (ple_window_grow < 1)
6339 return ple_window;
6340
6341 val = min(val, ple_window_actual_max);
6342
6343 if (ple_window_grow < ple_window)
6344 val *= ple_window_grow;
6345 else
6346 val += ple_window_grow;
6347
6348 return val;
6349}
6350
6351static int __shrink_ple_window(int val, int modifier, int minimum)
6352{
6353 if (modifier < 1)
6354 return ple_window;
6355
6356 if (modifier < ple_window)
6357 val /= modifier;
6358 else
6359 val -= modifier;
6360
6361 return max(val, minimum);
6362}
6363
6364static void grow_ple_window(struct kvm_vcpu *vcpu)
6365{
6366 struct vcpu_vmx *vmx = to_vmx(vcpu);
6367 int old = vmx->ple_window;
6368
6369 vmx->ple_window = __grow_ple_window(old);
6370
6371 if (vmx->ple_window != old)
6372 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006373
6374 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006375}
6376
6377static void shrink_ple_window(struct kvm_vcpu *vcpu)
6378{
6379 struct vcpu_vmx *vmx = to_vmx(vcpu);
6380 int old = vmx->ple_window;
6381
6382 vmx->ple_window = __shrink_ple_window(old,
6383 ple_window_shrink, ple_window);
6384
6385 if (vmx->ple_window != old)
6386 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006387
6388 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006389}
6390
6391/*
6392 * ple_window_actual_max is computed to be one grow_ple_window() below
6393 * ple_window_max. (See __grow_ple_window for the reason.)
6394 * This prevents overflows, because ple_window_max is int.
6395 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6396 * this process.
6397 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6398 */
6399static void update_ple_window_actual_max(void)
6400{
6401 ple_window_actual_max =
6402 __shrink_ple_window(max(ple_window_max, ple_window),
6403 ple_window_grow, INT_MIN);
6404}
6405
Feng Wubf9f6ac2015-09-18 22:29:55 +08006406/*
6407 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6408 */
6409static void wakeup_handler(void)
6410{
6411 struct kvm_vcpu *vcpu;
6412 int cpu = smp_processor_id();
6413
6414 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6415 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6416 blocked_vcpu_list) {
6417 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6418
6419 if (pi_test_on(pi_desc) == 1)
6420 kvm_vcpu_kick(vcpu);
6421 }
6422 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6423}
6424
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006425void vmx_enable_tdp(void)
6426{
6427 kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6428 enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6429 enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6430 0ull, VMX_EPT_EXECUTABLE_MASK,
6431 cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
Junaid Shahid312b6162016-12-21 20:29:29 -08006432 enable_ept_ad_bits ? 0ull : VMX_EPT_RWX_MASK);
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006433
6434 ept_set_mmio_spte_mask();
6435 kvm_enable_tdp();
6436}
6437
Tiejun Chenf2c76482014-10-28 10:14:47 +08006438static __init int hardware_setup(void)
6439{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006440 int r = -ENOMEM, i, msr;
6441
6442 rdmsrl_safe(MSR_EFER, &host_efer);
6443
6444 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6445 kvm_define_shared_msr(i, vmx_msr_index[i]);
6446
Radim Krčmář23611332016-09-29 22:41:33 +02006447 for (i = 0; i < VMX_BITMAP_NR; i++) {
6448 vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL);
6449 if (!vmx_bitmap[i])
6450 goto out;
6451 }
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006452
6453 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006454 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6455 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6456
6457 /*
6458 * Allow direct access to the PC debug port (it is often used for I/O
6459 * delays, but the vmexits simply slow things down).
6460 */
6461 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
6462 clear_bit(0x80, vmx_io_bitmap_a);
6463
6464 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6465
6466 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6467 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
6468
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006469 if (setup_vmcs_config(&vmcs_config) < 0) {
6470 r = -EIO;
Radim Krčmář23611332016-09-29 22:41:33 +02006471 goto out;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006472 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006473
6474 if (boot_cpu_has(X86_FEATURE_NX))
6475 kvm_enable_efer_bits(EFER_NX);
6476
Wanpeng Li08d839c2017-03-23 05:30:08 -07006477 if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
6478 !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
Tiejun Chenf2c76482014-10-28 10:14:47 +08006479 enable_vpid = 0;
Wanpeng Li08d839c2017-03-23 05:30:08 -07006480
Tiejun Chenf2c76482014-10-28 10:14:47 +08006481 if (!cpu_has_vmx_shadow_vmcs())
6482 enable_shadow_vmcs = 0;
6483 if (enable_shadow_vmcs)
6484 init_vmcs_shadow_fields();
6485
6486 if (!cpu_has_vmx_ept() ||
6487 !cpu_has_vmx_ept_4levels()) {
6488 enable_ept = 0;
6489 enable_unrestricted_guest = 0;
6490 enable_ept_ad_bits = 0;
6491 }
6492
6493 if (!cpu_has_vmx_ept_ad_bits())
6494 enable_ept_ad_bits = 0;
6495
6496 if (!cpu_has_vmx_unrestricted_guest())
6497 enable_unrestricted_guest = 0;
6498
Paolo Bonziniad15a292015-01-30 16:18:49 +01006499 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006500 flexpriority_enabled = 0;
6501
Paolo Bonziniad15a292015-01-30 16:18:49 +01006502 /*
6503 * set_apic_access_page_addr() is used to reload apic access
6504 * page upon invalidation. No need to do anything if not
6505 * using the APIC_ACCESS_ADDR VMCS field.
6506 */
6507 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006508 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006509
6510 if (!cpu_has_vmx_tpr_shadow())
6511 kvm_x86_ops->update_cr8_intercept = NULL;
6512
6513 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6514 kvm_disable_largepages();
6515
6516 if (!cpu_has_vmx_ple())
6517 ple_gap = 0;
6518
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006519 if (!cpu_has_vmx_apicv()) {
Tiejun Chenf2c76482014-10-28 10:14:47 +08006520 enable_apicv = 0;
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01006521 kvm_x86_ops->sync_pir_to_irr = NULL;
6522 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006523
Haozhong Zhang64903d62015-10-20 15:39:09 +08006524 if (cpu_has_vmx_tsc_scaling()) {
6525 kvm_has_tsc_control = true;
6526 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6527 kvm_tsc_scaling_ratio_frac_bits = 48;
6528 }
6529
Tiejun Chenbaa03522014-12-23 16:21:11 +08006530 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6531 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6532 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6533 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6534 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6535 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
6536 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
6537
Wanpeng Lic63e4562016-09-23 19:17:16 +08006538 memcpy(vmx_msr_bitmap_legacy_x2apic_apicv,
6539 vmx_msr_bitmap_legacy, PAGE_SIZE);
6540 memcpy(vmx_msr_bitmap_longmode_x2apic_apicv,
6541 vmx_msr_bitmap_longmode, PAGE_SIZE);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006542 memcpy(vmx_msr_bitmap_legacy_x2apic,
6543 vmx_msr_bitmap_legacy, PAGE_SIZE);
6544 memcpy(vmx_msr_bitmap_longmode_x2apic,
6545 vmx_msr_bitmap_longmode, PAGE_SIZE);
6546
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006547 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6548
Radim Krčmář40d83382016-09-29 22:41:31 +02006549 for (msr = 0x800; msr <= 0x8ff; msr++) {
6550 if (msr == 0x839 /* TMCCT */)
6551 continue;
Radim Krčmář2e69f862016-09-29 22:41:32 +02006552 vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true);
Radim Krčmář40d83382016-09-29 22:41:31 +02006553 }
Tiejun Chenbaa03522014-12-23 16:21:11 +08006554
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006555 /*
Radim Krčmář2e69f862016-09-29 22:41:32 +02006556 * TPR reads and writes can be virtualized even if virtual interrupt
6557 * delivery is not in use.
Wanpeng Lif6e90f92016-09-22 07:43:25 +08006558 */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006559 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_W, true);
6560 vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_R | MSR_TYPE_W, false);
6561
Roman Kagan3ce424e2016-05-18 17:48:20 +03006562 /* EOI */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006563 vmx_disable_intercept_msr_x2apic(0x80b, MSR_TYPE_W, true);
Roman Kagan3ce424e2016-05-18 17:48:20 +03006564 /* SELF-IPI */
Radim Krčmář2e69f862016-09-29 22:41:32 +02006565 vmx_disable_intercept_msr_x2apic(0x83f, MSR_TYPE_W, true);
Tiejun Chenbaa03522014-12-23 16:21:11 +08006566
Junaid Shahidf160c7b2016-12-06 16:46:16 -08006567 if (enable_ept)
6568 vmx_enable_tdp();
6569 else
Tiejun Chenbaa03522014-12-23 16:21:11 +08006570 kvm_disable_tdp();
6571
6572 update_ple_window_actual_max();
6573
Kai Huang843e4332015-01-28 10:54:28 +08006574 /*
6575 * Only enable PML when hardware supports PML feature, and both EPT
6576 * and EPT A/D bit features are enabled -- PML depends on them to work.
6577 */
6578 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6579 enable_pml = 0;
6580
6581 if (!enable_pml) {
6582 kvm_x86_ops->slot_enable_log_dirty = NULL;
6583 kvm_x86_ops->slot_disable_log_dirty = NULL;
6584 kvm_x86_ops->flush_log_dirty = NULL;
6585 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6586 }
6587
Yunhong Jiang64672c92016-06-13 14:19:59 -07006588 if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) {
6589 u64 vmx_msr;
6590
6591 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
6592 cpu_preemption_timer_multi =
6593 vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
6594 } else {
6595 kvm_x86_ops->set_hv_timer = NULL;
6596 kvm_x86_ops->cancel_hv_timer = NULL;
6597 }
6598
Feng Wubf9f6ac2015-09-18 22:29:55 +08006599 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6600
Ashok Rajc45dcc72016-06-22 14:59:56 +08006601 kvm_mce_cap_supported |= MCG_LMCE_P;
6602
Tiejun Chenf2c76482014-10-28 10:14:47 +08006603 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006604
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006605out:
Radim Krčmář23611332016-09-29 22:41:33 +02006606 for (i = 0; i < VMX_BITMAP_NR; i++)
6607 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006608
6609 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006610}
6611
6612static __exit void hardware_unsetup(void)
6613{
Radim Krčmář23611332016-09-29 22:41:33 +02006614 int i;
6615
6616 for (i = 0; i < VMX_BITMAP_NR; i++)
6617 free_page((unsigned long)vmx_bitmap[i]);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006618
Tiejun Chenf2c76482014-10-28 10:14:47 +08006619 free_kvm_area();
6620}
6621
Avi Kivity6aa8b732006-12-10 02:21:36 -08006622/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006623 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6624 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6625 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006626static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006627{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006628 if (ple_gap)
6629 grow_ple_window(vcpu);
6630
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006631 kvm_vcpu_on_spin(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006632 return kvm_skip_emulated_instruction(vcpu);
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006633}
6634
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006635static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006636{
Kyle Huey6affcbe2016-11-29 12:40:40 -08006637 return kvm_skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006638}
6639
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006640static int handle_mwait(struct kvm_vcpu *vcpu)
6641{
6642 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6643 return handle_nop(vcpu);
6644}
6645
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006646static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6647{
6648 return 1;
6649}
6650
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006651static int handle_monitor(struct kvm_vcpu *vcpu)
6652{
6653 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6654 return handle_nop(vcpu);
6655}
6656
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006657/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006658 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6659 * We could reuse a single VMCS for all the L2 guests, but we also want the
6660 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6661 * allows keeping them loaded on the processor, and in the future will allow
6662 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6663 * every entry if they never change.
6664 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6665 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6666 *
6667 * The following functions allocate and free a vmcs02 in this pool.
6668 */
6669
6670/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6671static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6672{
6673 struct vmcs02_list *item;
6674 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6675 if (item->vmptr == vmx->nested.current_vmptr) {
6676 list_move(&item->list, &vmx->nested.vmcs02_pool);
6677 return &item->vmcs02;
6678 }
6679
6680 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
6681 /* Recycle the least recently used VMCS. */
Geliang Tangd74c0e62016-01-01 19:47:14 +08006682 item = list_last_entry(&vmx->nested.vmcs02_pool,
6683 struct vmcs02_list, list);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006684 item->vmptr = vmx->nested.current_vmptr;
6685 list_move(&item->list, &vmx->nested.vmcs02_pool);
6686 return &item->vmcs02;
6687 }
6688
6689 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02006690 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006691 if (!item)
6692 return NULL;
6693 item->vmcs02.vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07006694 item->vmcs02.shadow_vmcs = NULL;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006695 if (!item->vmcs02.vmcs) {
6696 kfree(item);
6697 return NULL;
6698 }
6699 loaded_vmcs_init(&item->vmcs02);
6700 item->vmptr = vmx->nested.current_vmptr;
6701 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
6702 vmx->nested.vmcs02_num++;
6703 return &item->vmcs02;
6704}
6705
6706/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
6707static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
6708{
6709 struct vmcs02_list *item;
6710 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6711 if (item->vmptr == vmptr) {
6712 free_loaded_vmcs(&item->vmcs02);
6713 list_del(&item->list);
6714 kfree(item);
6715 vmx->nested.vmcs02_num--;
6716 return;
6717 }
6718}
6719
6720/*
6721 * Free all VMCSs saved for this vcpu, except the one pointed by
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006722 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
6723 * must be &vmx->vmcs01.
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006724 */
6725static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
6726{
6727 struct vmcs02_list *item, *n;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006728
6729 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006730 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006731 /*
6732 * Something will leak if the above WARN triggers. Better than
6733 * a use-after-free.
6734 */
6735 if (vmx->loaded_vmcs == &item->vmcs02)
6736 continue;
6737
6738 free_loaded_vmcs(&item->vmcs02);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006739 list_del(&item->list);
6740 kfree(item);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006741 vmx->nested.vmcs02_num--;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006742 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006743}
6744
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006745/*
6746 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6747 * set the success or error code of an emulated VMX instruction, as specified
6748 * by Vol 2B, VMX Instruction Reference, "Conventions".
6749 */
6750static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6751{
6752 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6753 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6754 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6755}
6756
6757static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6758{
6759 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6760 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6761 X86_EFLAGS_SF | X86_EFLAGS_OF))
6762 | X86_EFLAGS_CF);
6763}
6764
Abel Gordon145c28d2013-04-18 14:36:55 +03006765static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006766 u32 vm_instruction_error)
6767{
6768 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6769 /*
6770 * failValid writes the error number to the current VMCS, which
6771 * can't be done there isn't a current VMCS.
6772 */
6773 nested_vmx_failInvalid(vcpu);
6774 return;
6775 }
6776 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6777 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6778 X86_EFLAGS_SF | X86_EFLAGS_OF))
6779 | X86_EFLAGS_ZF);
6780 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6781 /*
6782 * We don't need to force a shadow sync because
6783 * VM_INSTRUCTION_ERROR is not shadowed
6784 */
6785}
Abel Gordon145c28d2013-04-18 14:36:55 +03006786
Wincy Vanff651cb2014-12-11 08:52:58 +03006787static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6788{
6789 /* TODO: not to reset guest simply here. */
6790 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02006791 pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator);
Wincy Vanff651cb2014-12-11 08:52:58 +03006792}
6793
Jan Kiszkaf41245002014-03-07 20:03:13 +01006794static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6795{
6796 struct vcpu_vmx *vmx =
6797 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6798
6799 vmx->nested.preemption_timer_expired = true;
6800 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6801 kvm_vcpu_kick(&vmx->vcpu);
6802
6803 return HRTIMER_NORESTART;
6804}
6805
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006806/*
Bandan Das19677e32014-05-06 02:19:15 -04006807 * Decode the memory-address operand of a vmx instruction, as recorded on an
6808 * exit caused by such an instruction (run by a guest hypervisor).
6809 * On success, returns 0. When the operand is invalid, returns 1 and throws
6810 * #UD or #GP.
6811 */
6812static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6813 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006814 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006815{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006816 gva_t off;
6817 bool exn;
6818 struct kvm_segment s;
6819
Bandan Das19677e32014-05-06 02:19:15 -04006820 /*
6821 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6822 * Execution", on an exit, vmx_instruction_info holds most of the
6823 * addressing components of the operand. Only the displacement part
6824 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6825 * For how an actual address is calculated from all these components,
6826 * refer to Vol. 1, "Operand Addressing".
6827 */
6828 int scaling = vmx_instruction_info & 3;
6829 int addr_size = (vmx_instruction_info >> 7) & 7;
6830 bool is_reg = vmx_instruction_info & (1u << 10);
6831 int seg_reg = (vmx_instruction_info >> 15) & 7;
6832 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6833 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6834 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6835 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6836
6837 if (is_reg) {
6838 kvm_queue_exception(vcpu, UD_VECTOR);
6839 return 1;
6840 }
6841
6842 /* Addr = segment_base + offset */
6843 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006844 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006845 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006846 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006847 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006848 off += kvm_register_read(vcpu, index_reg)<<scaling;
6849 vmx_get_segment(vcpu, &s, seg_reg);
6850 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006851
6852 if (addr_size == 1) /* 32 bit */
6853 *ret &= 0xffffffff;
6854
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006855 /* Checks for #GP/#SS exceptions. */
6856 exn = false;
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006857 if (is_long_mode(vcpu)) {
6858 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6859 * non-canonical form. This is the only check on the memory
6860 * destination for long mode!
6861 */
6862 exn = is_noncanonical_address(*ret);
6863 } else if (is_protmode(vcpu)) {
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006864 /* Protected mode: apply checks for segment validity in the
6865 * following order:
6866 * - segment type check (#GP(0) may be thrown)
6867 * - usability check (#GP(0)/#SS(0))
6868 * - limit check (#GP(0)/#SS(0))
6869 */
6870 if (wr)
6871 /* #GP(0) if the destination operand is located in a
6872 * read-only data segment or any code segment.
6873 */
6874 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6875 else
6876 /* #GP(0) if the source operand is located in an
6877 * execute-only code segment
6878 */
6879 exn = ((s.type & 0xa) == 8);
Quentin Casasnovasff30ef42016-06-18 11:01:05 +02006880 if (exn) {
6881 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6882 return 1;
6883 }
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006884 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6885 */
6886 exn = (s.unusable != 0);
6887 /* Protected mode: #GP(0)/#SS(0) if the memory
6888 * operand is outside the segment limit.
6889 */
6890 exn = exn || (off + sizeof(u64) > s.limit);
6891 }
6892 if (exn) {
6893 kvm_queue_exception_e(vcpu,
6894 seg_reg == VCPU_SREG_SS ?
6895 SS_VECTOR : GP_VECTOR,
6896 0);
6897 return 1;
6898 }
6899
Bandan Das19677e32014-05-06 02:19:15 -04006900 return 0;
6901}
6902
6903/*
Bandan Das3573e222014-05-06 02:19:16 -04006904 * This function performs the various checks including
6905 * - if it's 4KB aligned
6906 * - No bits beyond the physical address width are set
6907 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006908 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006909 */
Bandan Das4291b582014-05-06 02:19:18 -04006910static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6911 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006912{
6913 gva_t gva;
6914 gpa_t vmptr;
6915 struct x86_exception e;
6916 struct page *page;
6917 struct vcpu_vmx *vmx = to_vmx(vcpu);
6918 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6919
6920 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006921 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04006922 return 1;
6923
6924 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6925 sizeof(vmptr), &e)) {
6926 kvm_inject_page_fault(vcpu, &e);
6927 return 1;
6928 }
6929
6930 switch (exit_reason) {
6931 case EXIT_REASON_VMON:
6932 /*
6933 * SDM 3: 24.11.5
6934 * The first 4 bytes of VMXON region contain the supported
6935 * VMCS revision identifier
6936 *
6937 * Note - IA32_VMX_BASIC[48] will never be 1
6938 * for the nested case;
6939 * which replaces physical address width with 32
6940 *
6941 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006942 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04006943 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006944 return kvm_skip_emulated_instruction(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04006945 }
6946
6947 page = nested_get_page(vcpu, vmptr);
Paolo Bonzini06ce5212017-01-24 11:56:21 +01006948 if (page == NULL) {
Bandan Das3573e222014-05-06 02:19:16 -04006949 nested_vmx_failInvalid(vcpu);
Paolo Bonzini06ce5212017-01-24 11:56:21 +01006950 return kvm_skip_emulated_instruction(vcpu);
6951 }
6952 if (*(u32 *)kmap(page) != VMCS12_REVISION) {
Bandan Das3573e222014-05-06 02:19:16 -04006953 kunmap(page);
Paolo Bonzini06ce5212017-01-24 11:56:21 +01006954 nested_release_page_clean(page);
6955 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006956 return kvm_skip_emulated_instruction(vcpu);
Bandan Das3573e222014-05-06 02:19:16 -04006957 }
6958 kunmap(page);
Paolo Bonzini06ce5212017-01-24 11:56:21 +01006959 nested_release_page_clean(page);
Bandan Das3573e222014-05-06 02:19:16 -04006960 vmx->nested.vmxon_ptr = vmptr;
6961 break;
Bandan Das4291b582014-05-06 02:19:18 -04006962 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006963 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006964 nested_vmx_failValid(vcpu,
6965 VMXERR_VMCLEAR_INVALID_ADDRESS);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006966 return kvm_skip_emulated_instruction(vcpu);
Bandan Das4291b582014-05-06 02:19:18 -04006967 }
Bandan Das3573e222014-05-06 02:19:16 -04006968
Bandan Das4291b582014-05-06 02:19:18 -04006969 if (vmptr == vmx->nested.vmxon_ptr) {
6970 nested_vmx_failValid(vcpu,
6971 VMXERR_VMCLEAR_VMXON_POINTER);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006972 return kvm_skip_emulated_instruction(vcpu);
Bandan Das4291b582014-05-06 02:19:18 -04006973 }
6974 break;
6975 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006976 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006977 nested_vmx_failValid(vcpu,
6978 VMXERR_VMPTRLD_INVALID_ADDRESS);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006979 return kvm_skip_emulated_instruction(vcpu);
Bandan Das4291b582014-05-06 02:19:18 -04006980 }
6981
6982 if (vmptr == vmx->nested.vmxon_ptr) {
6983 nested_vmx_failValid(vcpu,
GanShun37b9a672016-11-30 10:28:19 -08006984 VMXERR_VMPTRLD_VMXON_POINTER);
Kyle Huey6affcbe2016-11-29 12:40:40 -08006985 return kvm_skip_emulated_instruction(vcpu);
Bandan Das4291b582014-05-06 02:19:18 -04006986 }
6987 break;
Bandan Das3573e222014-05-06 02:19:16 -04006988 default:
6989 return 1; /* shouldn't happen */
6990 }
6991
Bandan Das4291b582014-05-06 02:19:18 -04006992 if (vmpointer)
6993 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04006994 return 0;
6995}
6996
Jim Mattsone29acc52016-11-30 12:03:43 -08006997static int enter_vmx_operation(struct kvm_vcpu *vcpu)
6998{
6999 struct vcpu_vmx *vmx = to_vmx(vcpu);
7000 struct vmcs *shadow_vmcs;
7001
7002 if (cpu_has_vmx_msr_bitmap()) {
7003 vmx->nested.msr_bitmap =
7004 (unsigned long *)__get_free_page(GFP_KERNEL);
7005 if (!vmx->nested.msr_bitmap)
7006 goto out_msr_bitmap;
7007 }
7008
7009 vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL);
7010 if (!vmx->nested.cached_vmcs12)
7011 goto out_cached_vmcs12;
7012
7013 if (enable_shadow_vmcs) {
7014 shadow_vmcs = alloc_vmcs();
7015 if (!shadow_vmcs)
7016 goto out_shadow_vmcs;
7017 /* mark vmcs as shadow */
7018 shadow_vmcs->revision_id |= (1u << 31);
7019 /* init shadow vmcs */
7020 vmcs_clear(shadow_vmcs);
7021 vmx->vmcs01.shadow_vmcs = shadow_vmcs;
7022 }
7023
7024 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
7025 vmx->nested.vmcs02_num = 0;
7026
7027 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
7028 HRTIMER_MODE_REL_PINNED);
7029 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
7030
7031 vmx->nested.vmxon = true;
7032 return 0;
7033
7034out_shadow_vmcs:
7035 kfree(vmx->nested.cached_vmcs12);
7036
7037out_cached_vmcs12:
7038 free_page((unsigned long)vmx->nested.msr_bitmap);
7039
7040out_msr_bitmap:
7041 return -ENOMEM;
7042}
7043
Bandan Das3573e222014-05-06 02:19:16 -04007044/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007045 * Emulate the VMXON instruction.
7046 * Currently, we just remember that VMX is active, and do not save or even
7047 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
7048 * do not currently need to store anything in that guest-allocated memory
7049 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
7050 * argument is different from the VMXON pointer (which the spec says they do).
7051 */
7052static int handle_vmon(struct kvm_vcpu *vcpu)
7053{
Jim Mattsone29acc52016-11-30 12:03:43 -08007054 int ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007055 struct kvm_segment cs;
7056 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007057 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
7058 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007059
7060 /* The Intel VMX Instruction Reference lists a bunch of bits that
7061 * are prerequisite to running VMXON, most notably cr4.VMXE must be
7062 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
7063 * Otherwise, we should fail with #UD. We test these now:
7064 */
7065 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
7066 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
7067 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
7068 kvm_queue_exception(vcpu, UD_VECTOR);
7069 return 1;
7070 }
7071
7072 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7073 if (is_long_mode(vcpu) && !cs.l) {
7074 kvm_queue_exception(vcpu, UD_VECTOR);
7075 return 1;
7076 }
7077
7078 if (vmx_get_cpl(vcpu)) {
7079 kvm_inject_gp(vcpu, 0);
7080 return 1;
7081 }
Bandan Das3573e222014-05-06 02:19:16 -04007082
Abel Gordon145c28d2013-04-18 14:36:55 +03007083 if (vmx->nested.vmxon) {
7084 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007085 return kvm_skip_emulated_instruction(vcpu);
Abel Gordon145c28d2013-04-18 14:36:55 +03007086 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007087
Haozhong Zhang3b840802016-06-22 14:59:54 +08007088 if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
Nadav Har'Elb3897a42013-07-08 19:12:35 +08007089 != VMXON_NEEDED_FEATURES) {
7090 kvm_inject_gp(vcpu, 0);
7091 return 1;
7092 }
7093
Jim Mattson21e7fbe2016-12-22 15:49:55 -08007094 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
7095 return 1;
Jim Mattsone29acc52016-11-30 12:03:43 -08007096
7097 ret = enter_vmx_operation(vcpu);
7098 if (ret)
7099 return ret;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007100
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007101 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007102 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007103}
7104
7105/*
7106 * Intel's VMX Instruction Reference specifies a common set of prerequisites
7107 * for running VMX instructions (except VMXON, whose prerequisites are
7108 * slightly different). It also specifies what exception to inject otherwise.
7109 */
7110static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
7111{
7112 struct kvm_segment cs;
7113 struct vcpu_vmx *vmx = to_vmx(vcpu);
7114
7115 if (!vmx->nested.vmxon) {
7116 kvm_queue_exception(vcpu, UD_VECTOR);
7117 return 0;
7118 }
7119
7120 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
7121 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
7122 (is_long_mode(vcpu) && !cs.l)) {
7123 kvm_queue_exception(vcpu, UD_VECTOR);
7124 return 0;
7125 }
7126
7127 if (vmx_get_cpl(vcpu)) {
7128 kvm_inject_gp(vcpu, 0);
7129 return 0;
7130 }
7131
7132 return 1;
7133}
7134
Abel Gordone7953d72013-04-18 14:37:55 +03007135static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
7136{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007137 if (vmx->nested.current_vmptr == -1ull)
7138 return;
7139
7140 /* current_vmptr and current_vmcs12 are always set/reset together */
7141 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
7142 return;
7143
Abel Gordon012f83c2013-04-18 14:39:25 +03007144 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007145 /* copy to memory all shadowed fields in case
7146 they were modified */
7147 copy_shadow_to_vmcs12(vmx);
7148 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007149 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
7150 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007151 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03007152 }
Wincy Van705699a2015-02-03 23:58:17 +08007153 vmx->nested.posted_intr_nv = -1;
David Matlack4f2777b2016-07-13 17:16:37 -07007154
7155 /* Flush VMCS12 to guest memory */
7156 memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12,
7157 VMCS12_SIZE);
7158
Abel Gordone7953d72013-04-18 14:37:55 +03007159 kunmap(vmx->nested.current_vmcs12_page);
7160 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007161 vmx->nested.current_vmptr = -1ull;
7162 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03007163}
7164
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007165/*
7166 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
7167 * just stops using VMX.
7168 */
7169static void free_nested(struct vcpu_vmx *vmx)
7170{
7171 if (!vmx->nested.vmxon)
7172 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007173
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007174 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07007175 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007176 nested_release_vmcs12(vmx);
Radim Krčmářd048c092016-08-08 20:16:22 +02007177 if (vmx->nested.msr_bitmap) {
7178 free_page((unsigned long)vmx->nested.msr_bitmap);
7179 vmx->nested.msr_bitmap = NULL;
7180 }
Jim Mattson355f4fb2016-10-28 08:29:39 -07007181 if (enable_shadow_vmcs) {
7182 vmcs_clear(vmx->vmcs01.shadow_vmcs);
7183 free_vmcs(vmx->vmcs01.shadow_vmcs);
7184 vmx->vmcs01.shadow_vmcs = NULL;
7185 }
David Matlack4f2777b2016-07-13 17:16:37 -07007186 kfree(vmx->nested.cached_vmcs12);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007187 /* Unpin physical memory we referred to in current vmcs02 */
7188 if (vmx->nested.apic_access_page) {
7189 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007190 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007191 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007192 if (vmx->nested.virtual_apic_page) {
7193 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02007194 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007195 }
Wincy Van705699a2015-02-03 23:58:17 +08007196 if (vmx->nested.pi_desc_page) {
7197 kunmap(vmx->nested.pi_desc_page);
7198 nested_release_page(vmx->nested.pi_desc_page);
7199 vmx->nested.pi_desc_page = NULL;
7200 vmx->nested.pi_desc = NULL;
7201 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03007202
7203 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007204}
7205
7206/* Emulate the VMXOFF instruction */
7207static int handle_vmoff(struct kvm_vcpu *vcpu)
7208{
7209 if (!nested_vmx_check_permission(vcpu))
7210 return 1;
7211 free_nested(to_vmx(vcpu));
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08007212 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007213 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007214}
7215
Nadav Har'El27d6c862011-05-25 23:06:59 +03007216/* Emulate the VMCLEAR instruction */
7217static int handle_vmclear(struct kvm_vcpu *vcpu)
7218{
7219 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson587d7e722017-03-02 12:41:48 -08007220 u32 zero = 0;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007221 gpa_t vmptr;
Nadav Har'El27d6c862011-05-25 23:06:59 +03007222
7223 if (!nested_vmx_check_permission(vcpu))
7224 return 1;
7225
Bandan Das4291b582014-05-06 02:19:18 -04007226 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03007227 return 1;
7228
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007229 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03007230 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007231
Jim Mattson587d7e722017-03-02 12:41:48 -08007232 kvm_vcpu_write_guest(vcpu,
7233 vmptr + offsetof(struct vmcs12, launch_state),
7234 &zero, sizeof(zero));
Nadav Har'El27d6c862011-05-25 23:06:59 +03007235
7236 nested_free_vmcs02(vmx, vmptr);
7237
Nadav Har'El27d6c862011-05-25 23:06:59 +03007238 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007239 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03007240}
7241
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007242static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
7243
7244/* Emulate the VMLAUNCH instruction */
7245static int handle_vmlaunch(struct kvm_vcpu *vcpu)
7246{
7247 return nested_vmx_run(vcpu, true);
7248}
7249
7250/* Emulate the VMRESUME instruction */
7251static int handle_vmresume(struct kvm_vcpu *vcpu)
7252{
7253
7254 return nested_vmx_run(vcpu, false);
7255}
7256
Nadav Har'El49f705c2011-05-25 23:08:30 +03007257enum vmcs_field_type {
7258 VMCS_FIELD_TYPE_U16 = 0,
7259 VMCS_FIELD_TYPE_U64 = 1,
7260 VMCS_FIELD_TYPE_U32 = 2,
7261 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
7262};
7263
7264static inline int vmcs_field_type(unsigned long field)
7265{
7266 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
7267 return VMCS_FIELD_TYPE_U32;
7268 return (field >> 13) & 0x3 ;
7269}
7270
7271static inline int vmcs_field_readonly(unsigned long field)
7272{
7273 return (((field >> 10) & 0x3) == 1);
7274}
7275
7276/*
7277 * Read a vmcs12 field. Since these can have varying lengths and we return
7278 * one type, we chose the biggest type (u64) and zero-extend the return value
7279 * to that size. Note that the caller, handle_vmread, might need to use only
7280 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
7281 * 64-bit fields are to be returned).
7282 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007283static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
7284 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03007285{
7286 short offset = vmcs_field_to_offset(field);
7287 char *p;
7288
7289 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007290 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007291
7292 p = ((char *)(get_vmcs12(vcpu))) + offset;
7293
7294 switch (vmcs_field_type(field)) {
7295 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7296 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007297 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007298 case VMCS_FIELD_TYPE_U16:
7299 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007300 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007301 case VMCS_FIELD_TYPE_U32:
7302 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007303 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007304 case VMCS_FIELD_TYPE_U64:
7305 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007306 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007307 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007308 WARN_ON(1);
7309 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03007310 }
7311}
7312
Abel Gordon20b97fe2013-04-18 14:36:25 +03007313
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007314static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
7315 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03007316 short offset = vmcs_field_to_offset(field);
7317 char *p = ((char *) get_vmcs12(vcpu)) + offset;
7318 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007319 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007320
7321 switch (vmcs_field_type(field)) {
7322 case VMCS_FIELD_TYPE_U16:
7323 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007324 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007325 case VMCS_FIELD_TYPE_U32:
7326 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007327 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007328 case VMCS_FIELD_TYPE_U64:
7329 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007330 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007331 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7332 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007333 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007334 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007335 WARN_ON(1);
7336 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007337 }
7338
7339}
7340
Abel Gordon16f5b902013-04-18 14:38:25 +03007341static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7342{
7343 int i;
7344 unsigned long field;
7345 u64 field_value;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007346 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007347 const unsigned long *fields = shadow_read_write_fields;
7348 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007349
Jan Kiszka282da872014-10-08 18:05:39 +02007350 preempt_disable();
7351
Abel Gordon16f5b902013-04-18 14:38:25 +03007352 vmcs_load(shadow_vmcs);
7353
7354 for (i = 0; i < num_fields; i++) {
7355 field = fields[i];
7356 switch (vmcs_field_type(field)) {
7357 case VMCS_FIELD_TYPE_U16:
7358 field_value = vmcs_read16(field);
7359 break;
7360 case VMCS_FIELD_TYPE_U32:
7361 field_value = vmcs_read32(field);
7362 break;
7363 case VMCS_FIELD_TYPE_U64:
7364 field_value = vmcs_read64(field);
7365 break;
7366 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7367 field_value = vmcs_readl(field);
7368 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007369 default:
7370 WARN_ON(1);
7371 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007372 }
7373 vmcs12_write_any(&vmx->vcpu, field, field_value);
7374 }
7375
7376 vmcs_clear(shadow_vmcs);
7377 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007378
7379 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007380}
7381
Abel Gordonc3114422013-04-18 14:38:55 +03007382static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7383{
Mathias Krausec2bae892013-06-26 20:36:21 +02007384 const unsigned long *fields[] = {
7385 shadow_read_write_fields,
7386 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007387 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007388 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007389 max_shadow_read_write_fields,
7390 max_shadow_read_only_fields
7391 };
7392 int i, q;
7393 unsigned long field;
7394 u64 field_value = 0;
Jim Mattson355f4fb2016-10-28 08:29:39 -07007395 struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs;
Abel Gordonc3114422013-04-18 14:38:55 +03007396
7397 vmcs_load(shadow_vmcs);
7398
Mathias Krausec2bae892013-06-26 20:36:21 +02007399 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007400 for (i = 0; i < max_fields[q]; i++) {
7401 field = fields[q][i];
7402 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7403
7404 switch (vmcs_field_type(field)) {
7405 case VMCS_FIELD_TYPE_U16:
7406 vmcs_write16(field, (u16)field_value);
7407 break;
7408 case VMCS_FIELD_TYPE_U32:
7409 vmcs_write32(field, (u32)field_value);
7410 break;
7411 case VMCS_FIELD_TYPE_U64:
7412 vmcs_write64(field, (u64)field_value);
7413 break;
7414 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7415 vmcs_writel(field, (long)field_value);
7416 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007417 default:
7418 WARN_ON(1);
7419 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007420 }
7421 }
7422 }
7423
7424 vmcs_clear(shadow_vmcs);
7425 vmcs_load(vmx->loaded_vmcs->vmcs);
7426}
7427
Nadav Har'El49f705c2011-05-25 23:08:30 +03007428/*
7429 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7430 * used before) all generate the same failure when it is missing.
7431 */
7432static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7433{
7434 struct vcpu_vmx *vmx = to_vmx(vcpu);
7435 if (vmx->nested.current_vmptr == -1ull) {
7436 nested_vmx_failInvalid(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007437 return 0;
7438 }
7439 return 1;
7440}
7441
7442static int handle_vmread(struct kvm_vcpu *vcpu)
7443{
7444 unsigned long field;
7445 u64 field_value;
7446 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7447 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7448 gva_t gva = 0;
7449
Kyle Hueyeb277562016-11-29 12:40:39 -08007450 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007451 return 1;
7452
Kyle Huey6affcbe2016-11-29 12:40:40 -08007453 if (!nested_vmx_check_vmcs12(vcpu))
7454 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007455
Nadav Har'El49f705c2011-05-25 23:08:30 +03007456 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007457 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007458 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007459 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007460 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007461 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007462 }
7463 /*
7464 * Now copy part of this value to register or memory, as requested.
7465 * Note that the number of bits actually copied is 32 or 64 depending
7466 * on the guest's mode (32 or 64 bit), not on the given field's length.
7467 */
7468 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007469 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007470 field_value);
7471 } else {
7472 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007473 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007474 return 1;
7475 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
7476 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7477 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7478 }
7479
7480 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007481 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007482}
7483
7484
7485static int handle_vmwrite(struct kvm_vcpu *vcpu)
7486{
7487 unsigned long field;
7488 gva_t gva;
7489 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7490 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007491 /* The value to write might be 32 or 64 bits, depending on L1's long
7492 * mode, and eventually we need to write that into a field of several
7493 * possible lengths. The code below first zero-extends the value to 64
Adam Buchbinder6a6256f2016-02-23 15:34:30 -08007494 * bit (field_value), and then copies only the appropriate number of
Nadav Har'El49f705c2011-05-25 23:08:30 +03007495 * bits into the vmcs12 field.
7496 */
7497 u64 field_value = 0;
7498 struct x86_exception e;
7499
Kyle Hueyeb277562016-11-29 12:40:39 -08007500 if (!nested_vmx_check_permission(vcpu))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007501 return 1;
7502
Kyle Huey6affcbe2016-11-29 12:40:40 -08007503 if (!nested_vmx_check_vmcs12(vcpu))
7504 return kvm_skip_emulated_instruction(vcpu);
Kyle Hueyeb277562016-11-29 12:40:39 -08007505
Nadav Har'El49f705c2011-05-25 23:08:30 +03007506 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007507 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007508 (((vmx_instruction_info) >> 3) & 0xf));
7509 else {
7510 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007511 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007512 return 1;
7513 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007514 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007515 kvm_inject_page_fault(vcpu, &e);
7516 return 1;
7517 }
7518 }
7519
7520
Nadav Amit27e6fb52014-06-18 17:19:26 +03007521 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007522 if (vmcs_field_readonly(field)) {
7523 nested_vmx_failValid(vcpu,
7524 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007525 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007526 }
7527
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007528 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007529 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007530 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007531 }
7532
7533 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007534 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007535}
7536
Jim Mattsona8bc2842016-11-30 12:03:44 -08007537static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr)
7538{
7539 vmx->nested.current_vmptr = vmptr;
7540 if (enable_shadow_vmcs) {
7541 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7542 SECONDARY_EXEC_SHADOW_VMCS);
7543 vmcs_write64(VMCS_LINK_POINTER,
7544 __pa(vmx->vmcs01.shadow_vmcs));
7545 vmx->nested.sync_shadow_vmcs = true;
7546 }
7547}
7548
Nadav Har'El63846662011-05-25 23:07:29 +03007549/* Emulate the VMPTRLD instruction */
7550static int handle_vmptrld(struct kvm_vcpu *vcpu)
7551{
7552 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007553 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007554
7555 if (!nested_vmx_check_permission(vcpu))
7556 return 1;
7557
Bandan Das4291b582014-05-06 02:19:18 -04007558 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007559 return 1;
7560
Nadav Har'El63846662011-05-25 23:07:29 +03007561 if (vmx->nested.current_vmptr != vmptr) {
7562 struct vmcs12 *new_vmcs12;
7563 struct page *page;
7564 page = nested_get_page(vcpu, vmptr);
7565 if (page == NULL) {
7566 nested_vmx_failInvalid(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007567 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007568 }
7569 new_vmcs12 = kmap(page);
7570 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7571 kunmap(page);
7572 nested_release_page_clean(page);
7573 nested_vmx_failValid(vcpu,
7574 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007575 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007576 }
Nadav Har'El63846662011-05-25 23:07:29 +03007577
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007578 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007579 vmx->nested.current_vmcs12 = new_vmcs12;
7580 vmx->nested.current_vmcs12_page = page;
David Matlack4f2777b2016-07-13 17:16:37 -07007581 /*
7582 * Load VMCS12 from guest memory since it is not already
7583 * cached.
7584 */
7585 memcpy(vmx->nested.cached_vmcs12,
7586 vmx->nested.current_vmcs12, VMCS12_SIZE);
Jim Mattsona8bc2842016-11-30 12:03:44 -08007587 set_current_vmptr(vmx, vmptr);
Nadav Har'El63846662011-05-25 23:07:29 +03007588 }
7589
7590 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007591 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007592}
7593
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007594/* Emulate the VMPTRST instruction */
7595static int handle_vmptrst(struct kvm_vcpu *vcpu)
7596{
7597 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7598 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7599 gva_t vmcs_gva;
7600 struct x86_exception e;
7601
7602 if (!nested_vmx_check_permission(vcpu))
7603 return 1;
7604
7605 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007606 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007607 return 1;
7608 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
7609 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7610 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7611 sizeof(u64), &e)) {
7612 kvm_inject_page_fault(vcpu, &e);
7613 return 1;
7614 }
7615 nested_vmx_succeed(vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007616 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007617}
7618
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007619/* Emulate the INVEPT instruction */
7620static int handle_invept(struct kvm_vcpu *vcpu)
7621{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007622 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007623 u32 vmx_instruction_info, types;
7624 unsigned long type;
7625 gva_t gva;
7626 struct x86_exception e;
7627 struct {
7628 u64 eptp, gpa;
7629 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007630
Wincy Vanb9c237b2015-02-03 23:56:30 +08007631 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7632 SECONDARY_EXEC_ENABLE_EPT) ||
7633 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007634 kvm_queue_exception(vcpu, UD_VECTOR);
7635 return 1;
7636 }
7637
7638 if (!nested_vmx_check_permission(vcpu))
7639 return 1;
7640
7641 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7642 kvm_queue_exception(vcpu, UD_VECTOR);
7643 return 1;
7644 }
7645
7646 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007647 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007648
Wincy Vanb9c237b2015-02-03 23:56:30 +08007649 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007650
Jim Mattson85c856b2016-10-26 08:38:38 -07007651 if (type >= 32 || !(types & (1 << type))) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007652 nested_vmx_failValid(vcpu,
7653 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007654 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007655 }
7656
7657 /* According to the Intel VMX instruction reference, the memory
7658 * operand is read even if it isn't needed (e.g., for type==global)
7659 */
7660 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007661 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007662 return 1;
7663 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7664 sizeof(operand), &e)) {
7665 kvm_inject_page_fault(vcpu, &e);
7666 return 1;
7667 }
7668
7669 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007670 case VMX_EPT_EXTENT_GLOBAL:
Bandan Das45e11812016-08-02 16:32:36 -04007671 /*
7672 * TODO: track mappings and invalidate
7673 * single context requests appropriately
7674 */
7675 case VMX_EPT_EXTENT_CONTEXT:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007676 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007677 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007678 nested_vmx_succeed(vcpu);
7679 break;
7680 default:
7681 BUG_ON(1);
7682 break;
7683 }
7684
Kyle Huey6affcbe2016-11-29 12:40:40 -08007685 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007686}
7687
Petr Matouseka642fc32014-09-23 20:22:30 +02007688static int handle_invvpid(struct kvm_vcpu *vcpu)
7689{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007690 struct vcpu_vmx *vmx = to_vmx(vcpu);
7691 u32 vmx_instruction_info;
7692 unsigned long type, types;
7693 gva_t gva;
7694 struct x86_exception e;
7695 int vpid;
7696
7697 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7698 SECONDARY_EXEC_ENABLE_VPID) ||
7699 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7700 kvm_queue_exception(vcpu, UD_VECTOR);
7701 return 1;
7702 }
7703
7704 if (!nested_vmx_check_permission(vcpu))
7705 return 1;
7706
7707 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7708 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7709
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007710 types = (vmx->nested.nested_vmx_vpid_caps &
7711 VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007712
Jim Mattson85c856b2016-10-26 08:38:38 -07007713 if (type >= 32 || !(types & (1 << type))) {
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007714 nested_vmx_failValid(vcpu,
7715 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007716 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007717 }
7718
7719 /* according to the intel vmx instruction reference, the memory
7720 * operand is read even if it isn't needed (e.g., for type==global)
7721 */
7722 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7723 vmx_instruction_info, false, &gva))
7724 return 1;
7725 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid,
7726 sizeof(u32), &e)) {
7727 kvm_inject_page_fault(vcpu, &e);
7728 return 1;
7729 }
7730
7731 switch (type) {
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007732 case VMX_VPID_EXTENT_INDIVIDUAL_ADDR:
Paolo Bonzinief697a72016-03-18 16:58:38 +01007733 case VMX_VPID_EXTENT_SINGLE_CONTEXT:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007734 case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL:
7735 if (!vpid) {
7736 nested_vmx_failValid(vcpu,
7737 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007738 return kvm_skip_emulated_instruction(vcpu);
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007739 }
7740 break;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007741 case VMX_VPID_EXTENT_ALL_CONTEXT:
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007742 break;
7743 default:
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007744 WARN_ON_ONCE(1);
Kyle Huey6affcbe2016-11-29 12:40:40 -08007745 return kvm_skip_emulated_instruction(vcpu);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007746 }
7747
Jan Dakinevichbcdde302016-10-28 07:00:30 +03007748 __vmx_flush_tlb(vcpu, vmx->nested.vpid02);
7749 nested_vmx_succeed(vcpu);
7750
Kyle Huey6affcbe2016-11-29 12:40:40 -08007751 return kvm_skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007752}
7753
Kai Huang843e4332015-01-28 10:54:28 +08007754static int handle_pml_full(struct kvm_vcpu *vcpu)
7755{
7756 unsigned long exit_qualification;
7757
7758 trace_kvm_pml_full(vcpu->vcpu_id);
7759
7760 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7761
7762 /*
7763 * PML buffer FULL happened while executing iret from NMI,
7764 * "blocked by NMI" bit has to be set before next VM entry.
7765 */
7766 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
Kai Huang843e4332015-01-28 10:54:28 +08007767 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7768 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7769 GUEST_INTR_STATE_NMI);
7770
7771 /*
7772 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7773 * here.., and there's no userspace involvement needed for PML.
7774 */
7775 return 1;
7776}
7777
Yunhong Jiang64672c92016-06-13 14:19:59 -07007778static int handle_preemption_timer(struct kvm_vcpu *vcpu)
7779{
7780 kvm_lapic_expired_hv_timer(vcpu);
7781 return 1;
7782}
7783
Nadav Har'El0140cae2011-05-25 23:06:28 +03007784/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007785 * The exit handlers return 1 if the exit was handled fully and guest execution
7786 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7787 * to be done to userspace and return 0.
7788 */
Mathias Krause772e0312012-08-30 01:30:19 +02007789static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007790 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7791 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007792 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007793 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007794 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007795 [EXIT_REASON_CR_ACCESS] = handle_cr,
7796 [EXIT_REASON_DR_ACCESS] = handle_dr,
7797 [EXIT_REASON_CPUID] = handle_cpuid,
7798 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7799 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7800 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7801 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007802 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007803 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007804 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007805 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007806 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007807 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007808 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007809 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007810 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007811 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007812 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007813 [EXIT_REASON_VMOFF] = handle_vmoff,
7814 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007815 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7816 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007817 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007818 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007819 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007820 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007821 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007822 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007823 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7824 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007825 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007826 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007827 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007828 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007829 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007830 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007831 [EXIT_REASON_XSAVES] = handle_xsaves,
7832 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007833 [EXIT_REASON_PML_FULL] = handle_pml_full,
Yunhong Jiang64672c92016-06-13 14:19:59 -07007834 [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007835};
7836
7837static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007838 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007839
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007840static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7841 struct vmcs12 *vmcs12)
7842{
7843 unsigned long exit_qualification;
7844 gpa_t bitmap, last_bitmap;
7845 unsigned int port;
7846 int size;
7847 u8 b;
7848
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007849 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007850 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007851
7852 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7853
7854 port = exit_qualification >> 16;
7855 size = (exit_qualification & 7) + 1;
7856
7857 last_bitmap = (gpa_t)-1;
7858 b = -1;
7859
7860 while (size > 0) {
7861 if (port < 0x8000)
7862 bitmap = vmcs12->io_bitmap_a;
7863 else if (port < 0x10000)
7864 bitmap = vmcs12->io_bitmap_b;
7865 else
Joe Perches1d804d02015-03-30 16:46:09 -07007866 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007867 bitmap += (port & 0x7fff) / 8;
7868
7869 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007870 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007871 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007872 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007873 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007874
7875 port++;
7876 size--;
7877 last_bitmap = bitmap;
7878 }
7879
Joe Perches1d804d02015-03-30 16:46:09 -07007880 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007881}
7882
Nadav Har'El644d7112011-05-25 23:12:35 +03007883/*
7884 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7885 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7886 * disinterest in the current event (read or write a specific MSR) by using an
7887 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7888 */
7889static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7890 struct vmcs12 *vmcs12, u32 exit_reason)
7891{
7892 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7893 gpa_t bitmap;
7894
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007895 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007896 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007897
7898 /*
7899 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7900 * for the four combinations of read/write and low/high MSR numbers.
7901 * First we need to figure out which of the four to use:
7902 */
7903 bitmap = vmcs12->msr_bitmap;
7904 if (exit_reason == EXIT_REASON_MSR_WRITE)
7905 bitmap += 2048;
7906 if (msr_index >= 0xc0000000) {
7907 msr_index -= 0xc0000000;
7908 bitmap += 1024;
7909 }
7910
7911 /* Then read the msr_index'th bit from this bitmap: */
7912 if (msr_index < 1024*8) {
7913 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007914 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007915 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007916 return 1 & (b >> (msr_index & 7));
7917 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007918 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03007919}
7920
7921/*
7922 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7923 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7924 * intercept (via guest_host_mask etc.) the current event.
7925 */
7926static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7927 struct vmcs12 *vmcs12)
7928{
7929 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7930 int cr = exit_qualification & 15;
7931 int reg = (exit_qualification >> 8) & 15;
Nadav Amit1e32c072014-06-18 17:19:25 +03007932 unsigned long val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03007933
7934 switch ((exit_qualification >> 4) & 3) {
7935 case 0: /* mov to cr */
7936 switch (cr) {
7937 case 0:
7938 if (vmcs12->cr0_guest_host_mask &
7939 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007940 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007941 break;
7942 case 3:
7943 if ((vmcs12->cr3_target_count >= 1 &&
7944 vmcs12->cr3_target_value0 == val) ||
7945 (vmcs12->cr3_target_count >= 2 &&
7946 vmcs12->cr3_target_value1 == val) ||
7947 (vmcs12->cr3_target_count >= 3 &&
7948 vmcs12->cr3_target_value2 == val) ||
7949 (vmcs12->cr3_target_count >= 4 &&
7950 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07007951 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007952 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007953 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007954 break;
7955 case 4:
7956 if (vmcs12->cr4_guest_host_mask &
7957 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07007958 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007959 break;
7960 case 8:
7961 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007962 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007963 break;
7964 }
7965 break;
7966 case 2: /* clts */
7967 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
7968 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007969 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007970 break;
7971 case 1: /* mov from cr */
7972 switch (cr) {
7973 case 3:
7974 if (vmcs12->cpu_based_vm_exec_control &
7975 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007976 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007977 break;
7978 case 8:
7979 if (vmcs12->cpu_based_vm_exec_control &
7980 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007981 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007982 break;
7983 }
7984 break;
7985 case 3: /* lmsw */
7986 /*
7987 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
7988 * cr0. Other attempted changes are ignored, with no exit.
7989 */
7990 if (vmcs12->cr0_guest_host_mask & 0xe &
7991 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007992 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007993 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
7994 !(vmcs12->cr0_read_shadow & 0x1) &&
7995 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07007996 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007997 break;
7998 }
Joe Perches1d804d02015-03-30 16:46:09 -07007999 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008000}
8001
8002/*
8003 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
8004 * should handle it ourselves in L0 (and then continue L2). Only call this
8005 * when in is_guest_mode (L2).
8006 */
8007static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
8008{
Nadav Har'El644d7112011-05-25 23:12:35 +03008009 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8010 struct vcpu_vmx *vmx = to_vmx(vcpu);
8011 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01008012 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03008013
Jan Kiszka542060e2014-01-04 18:47:21 +01008014 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
8015 vmcs_readl(EXIT_QUALIFICATION),
8016 vmx->idt_vectoring_info,
8017 intr_info,
8018 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8019 KVM_ISA_VMX);
8020
Nadav Har'El644d7112011-05-25 23:12:35 +03008021 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07008022 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008023
8024 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008025 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
8026 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07008027 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008028 }
8029
8030 switch (exit_reason) {
8031 case EXIT_REASON_EXCEPTION_NMI:
Jim Mattsonef85b672016-12-12 11:01:37 -08008032 if (is_nmi(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07008033 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008034 else if (is_page_fault(intr_info))
8035 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01008036 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01008037 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07008038 return false;
Jan Kiszka6f054852016-02-09 20:15:18 +01008039 else if (is_debug(intr_info) &&
8040 vcpu->guest_debug &
8041 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
8042 return false;
8043 else if (is_breakpoint(intr_info) &&
8044 vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
8045 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008046 return vmcs12->exception_bitmap &
8047 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
8048 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07008049 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008050 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07008051 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008052 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008053 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008054 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02008055 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03008056 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07008057 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008058 case EXIT_REASON_CPUID:
Joe Perches1d804d02015-03-30 16:46:09 -07008059 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008060 case EXIT_REASON_HLT:
8061 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
8062 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07008063 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008064 case EXIT_REASON_INVLPG:
8065 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
8066 case EXIT_REASON_RDPMC:
8067 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Paolo Bonzinia5f46452017-03-30 11:55:32 +02008068 case EXIT_REASON_RDRAND:
8069 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND);
8070 case EXIT_REASON_RDSEED:
8071 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01008072 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03008073 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
8074 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
8075 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
8076 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
8077 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
8078 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02008079 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03008080 /*
8081 * VMX instructions trap unconditionally. This allows L1 to
8082 * emulate them for its L2 guest, i.e., allows 3-level nesting!
8083 */
Joe Perches1d804d02015-03-30 16:46:09 -07008084 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008085 case EXIT_REASON_CR_ACCESS:
8086 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
8087 case EXIT_REASON_DR_ACCESS:
8088 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
8089 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01008090 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Paolo Bonzini1b073042016-10-25 16:06:30 +02008091 case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR:
8092 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC);
Nadav Har'El644d7112011-05-25 23:12:35 +03008093 case EXIT_REASON_MSR_READ:
8094 case EXIT_REASON_MSR_WRITE:
8095 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
8096 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07008097 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008098 case EXIT_REASON_MWAIT_INSTRUCTION:
8099 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03008100 case EXIT_REASON_MONITOR_TRAP_FLAG:
8101 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03008102 case EXIT_REASON_MONITOR_INSTRUCTION:
8103 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
8104 case EXIT_REASON_PAUSE_INSTRUCTION:
8105 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
8106 nested_cpu_has2(vmcs12,
8107 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
8108 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07008109 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008110 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008111 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03008112 case EXIT_REASON_APIC_ACCESS:
8113 return nested_cpu_has2(vmcs12,
8114 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08008115 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08008116 case EXIT_REASON_EOI_INDUCED:
8117 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07008118 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008119 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008120 /*
8121 * L0 always deals with the EPT violation. If nested EPT is
8122 * used, and the nested mmu code discovers that the address is
8123 * missing in the guest EPT table (EPT12), the EPT violation
8124 * will be injected with nested_ept_inject_page_fault()
8125 */
Joe Perches1d804d02015-03-30 16:46:09 -07008126 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008127 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03008128 /*
8129 * L2 never uses directly L1's EPT, but rather L0's own EPT
8130 * table (shadow on EPT) or a merged EPT table that L0 built
8131 * (EPT on EPT). So any problems with the structure of the
8132 * table is L0's fault.
8133 */
Joe Perches1d804d02015-03-30 16:46:09 -07008134 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008135 case EXIT_REASON_WBINVD:
8136 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
8137 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07008138 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008139 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
8140 /*
8141 * This should never happen, since it is not possible to
8142 * set XSS to a non-zero value---neither in L1 nor in L2.
8143 * If if it were, XSS would have to be checked against
8144 * the XSS exit bitmap in vmcs12.
8145 */
8146 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Wanpeng Li55123e32016-07-06 18:29:58 +08008147 case EXIT_REASON_PREEMPTION_TIMER:
8148 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03008149 default:
Joe Perches1d804d02015-03-30 16:46:09 -07008150 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03008151 }
8152}
8153
Avi Kivity586f9602010-11-18 13:09:54 +02008154static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
8155{
8156 *info1 = vmcs_readl(EXIT_QUALIFICATION);
8157 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
8158}
8159
Kai Huanga3eaa862015-11-04 13:46:05 +08008160static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08008161{
Kai Huanga3eaa862015-11-04 13:46:05 +08008162 if (vmx->pml_pg) {
8163 __free_page(vmx->pml_pg);
8164 vmx->pml_pg = NULL;
8165 }
Kai Huang843e4332015-01-28 10:54:28 +08008166}
8167
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008168static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08008169{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008170 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008171 u64 *pml_buf;
8172 u16 pml_idx;
8173
8174 pml_idx = vmcs_read16(GUEST_PML_INDEX);
8175
8176 /* Do nothing if PML buffer is empty */
8177 if (pml_idx == (PML_ENTITY_NUM - 1))
8178 return;
8179
8180 /* PML index always points to next available PML buffer entity */
8181 if (pml_idx >= PML_ENTITY_NUM)
8182 pml_idx = 0;
8183 else
8184 pml_idx++;
8185
8186 pml_buf = page_address(vmx->pml_pg);
8187 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
8188 u64 gpa;
8189
8190 gpa = pml_buf[pml_idx];
8191 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008192 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08008193 }
8194
8195 /* reset PML index */
8196 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
8197}
8198
8199/*
8200 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
8201 * Called before reporting dirty_bitmap to userspace.
8202 */
8203static void kvm_flush_pml_buffers(struct kvm *kvm)
8204{
8205 int i;
8206 struct kvm_vcpu *vcpu;
8207 /*
8208 * We only need to kick vcpu out of guest mode here, as PML buffer
8209 * is flushed at beginning of all VMEXITs, and it's obvious that only
8210 * vcpus running in guest are possible to have unflushed GPAs in PML
8211 * buffer.
8212 */
8213 kvm_for_each_vcpu(i, vcpu, kvm)
8214 kvm_vcpu_kick(vcpu);
8215}
8216
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008217static void vmx_dump_sel(char *name, uint32_t sel)
8218{
8219 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
Chao Peng96794e42017-02-21 03:50:01 -05008220 name, vmcs_read16(sel),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008221 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
8222 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
8223 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
8224}
8225
8226static void vmx_dump_dtsel(char *name, uint32_t limit)
8227{
8228 pr_err("%s limit=0x%08x, base=0x%016lx\n",
8229 name, vmcs_read32(limit),
8230 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
8231}
8232
8233static void dump_vmcs(void)
8234{
8235 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
8236 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
8237 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
8238 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
8239 u32 secondary_exec_control = 0;
8240 unsigned long cr4 = vmcs_readl(GUEST_CR4);
Paolo Bonzinif3531052015-12-03 15:49:56 +01008241 u64 efer = vmcs_read64(GUEST_IA32_EFER);
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008242 int i, n;
8243
8244 if (cpu_has_secondary_exec_ctrls())
8245 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8246
8247 pr_err("*** Guest State ***\n");
8248 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8249 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
8250 vmcs_readl(CR0_GUEST_HOST_MASK));
8251 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
8252 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
8253 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
8254 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
8255 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
8256 {
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008257 pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
8258 vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
8259 pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
8260 vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008261 }
8262 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
8263 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
8264 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
8265 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
8266 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8267 vmcs_readl(GUEST_SYSENTER_ESP),
8268 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
8269 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
8270 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
8271 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
8272 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
8273 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
8274 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
8275 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
8276 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
8277 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
8278 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
8279 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
8280 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008281 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8282 efer, vmcs_read64(GUEST_IA32_PAT));
8283 pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
8284 vmcs_read64(GUEST_IA32_DEBUGCTL),
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008285 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
8286 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008287 pr_err("PerfGlobCtl = 0x%016llx\n",
8288 vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008289 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008290 pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008291 pr_err("Interruptibility = %08x ActivityState = %08x\n",
8292 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
8293 vmcs_read32(GUEST_ACTIVITY_STATE));
8294 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
8295 pr_err("InterruptStatus = %04x\n",
8296 vmcs_read16(GUEST_INTR_STATUS));
8297
8298 pr_err("*** Host State ***\n");
8299 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
8300 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
8301 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
8302 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
8303 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
8304 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
8305 vmcs_read16(HOST_TR_SELECTOR));
8306 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
8307 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
8308 vmcs_readl(HOST_TR_BASE));
8309 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
8310 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
8311 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
8312 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
8313 vmcs_readl(HOST_CR4));
8314 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
8315 vmcs_readl(HOST_IA32_SYSENTER_ESP),
8316 vmcs_read32(HOST_IA32_SYSENTER_CS),
8317 vmcs_readl(HOST_IA32_SYSENTER_EIP));
8318 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008319 pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
8320 vmcs_read64(HOST_IA32_EFER),
8321 vmcs_read64(HOST_IA32_PAT));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008322 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008323 pr_err("PerfGlobCtl = 0x%016llx\n",
8324 vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008325
8326 pr_err("*** Control State ***\n");
8327 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
8328 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
8329 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
8330 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
8331 vmcs_read32(EXCEPTION_BITMAP),
8332 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
8333 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
8334 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
8335 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8336 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8337 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8338 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8339 vmcs_read32(VM_EXIT_INTR_INFO),
8340 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8341 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8342 pr_err(" reason=%08x qualification=%016lx\n",
8343 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8344 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8345 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8346 vmcs_read32(IDT_VECTORING_ERROR_CODE));
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008347 pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
Haozhong Zhang8cfe9862015-10-20 15:39:12 +08008348 if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008349 pr_err("TSC Multiplier = 0x%016llx\n",
8350 vmcs_read64(TSC_MULTIPLIER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008351 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8352 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8353 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8354 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8355 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
Paolo Bonzini845c5b402015-12-03 15:51:00 +01008356 pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008357 n = vmcs_read32(CR3_TARGET_COUNT);
8358 for (i = 0; i + 1 < n; i += 4)
8359 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8360 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8361 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8362 if (i < n)
8363 pr_err("CR3 target%u=%016lx\n",
8364 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8365 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8366 pr_err("PLE Gap=%08x Window=%08x\n",
8367 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8368 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8369 pr_err("Virtual processor ID = 0x%04x\n",
8370 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8371}
8372
Avi Kivity6aa8b732006-12-10 02:21:36 -08008373/*
8374 * The guest has exited. See if we can fix it or if we need userspace
8375 * assistance.
8376 */
Avi Kivity851ba692009-08-24 11:10:17 +03008377static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008378{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008379 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008380 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008381 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008382
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008383 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
Paolo Bonzinidb1c0562016-12-08 15:31:41 +01008384 vcpu->arch.gpa_available = false;
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01008385
Kai Huang843e4332015-01-28 10:54:28 +08008386 /*
8387 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8388 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8389 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8390 * mode as if vcpus is in root mode, the PML buffer must has been
8391 * flushed already.
8392 */
8393 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008394 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008395
Mohammed Gamal80ced182009-09-01 12:48:18 +02008396 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008397 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008398 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008399
Nadav Har'El644d7112011-05-25 23:12:35 +03008400 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008401 nested_vmx_vmexit(vcpu, exit_reason,
8402 vmcs_read32(VM_EXIT_INTR_INFO),
8403 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008404 return 1;
8405 }
8406
Mohammed Gamal51207022010-05-31 22:40:54 +03008407 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008408 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008409 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8410 vcpu->run->fail_entry.hardware_entry_failure_reason
8411 = exit_reason;
8412 return 0;
8413 }
8414
Avi Kivity29bd8a72007-09-10 17:27:03 +03008415 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008416 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8417 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008418 = vmcs_read32(VM_INSTRUCTION_ERROR);
8419 return 0;
8420 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008421
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008422 /*
8423 * Note:
8424 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8425 * delivery event since it indicates guest is accessing MMIO.
8426 * The vm-exit can be triggered again after return to guest that
8427 * will cause infinite loop.
8428 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008429 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008430 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008431 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Cao, Leib244c9f2016-07-15 13:54:04 +00008432 exit_reason != EXIT_REASON_PML_FULL &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008433 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8434 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8435 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8436 vcpu->run->internal.ndata = 2;
8437 vcpu->run->internal.data[0] = vectoring_info;
8438 vcpu->run->internal.data[1] = exit_reason;
8439 return 0;
8440 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008441
Avi Kivity6aa8b732006-12-10 02:21:36 -08008442 if (exit_reason < kvm_vmx_max_exit_handlers
8443 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008444 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008445 else {
Radim Krčmář6c6c5e02017-01-13 18:59:04 +01008446 vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
8447 exit_reason);
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008448 kvm_queue_exception(vcpu, UD_VECTOR);
8449 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008450 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008451}
8452
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008453static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008454{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008455 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8456
8457 if (is_guest_mode(vcpu) &&
8458 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8459 return;
8460
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008461 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008462 vmcs_write32(TPR_THRESHOLD, 0);
8463 return;
8464 }
8465
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008466 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008467}
8468
Yang Zhang8d146952013-01-25 10:18:50 +08008469static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8470{
8471 u32 sec_exec_control;
8472
Radim Krčmářdccbfcf2016-08-08 20:16:23 +02008473 /* Postpone execution until vmcs01 is the current VMCS. */
8474 if (is_guest_mode(vcpu)) {
8475 to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true;
8476 return;
8477 }
8478
Wanpeng Lif6e90f92016-09-22 07:43:25 +08008479 if (!cpu_has_vmx_virtualize_x2apic_mode())
Yang Zhang8d146952013-01-25 10:18:50 +08008480 return;
8481
Paolo Bonzini35754c92015-07-29 12:05:37 +02008482 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008483 return;
8484
8485 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8486
8487 if (set) {
8488 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8489 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8490 } else {
8491 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8492 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008493 vmx_flush_tlb_ept_only(vcpu);
Yang Zhang8d146952013-01-25 10:18:50 +08008494 }
8495 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8496
8497 vmx_set_msr_bitmap(vcpu);
8498}
8499
Tang Chen38b99172014-09-24 15:57:54 +08008500static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8501{
8502 struct vcpu_vmx *vmx = to_vmx(vcpu);
8503
8504 /*
8505 * Currently we do not handle the nested case where L2 has an
8506 * APIC access page of its own; that page is still pinned.
8507 * Hence, we skip the case where the VCPU is in guest mode _and_
8508 * L1 prepared an APIC access page for L2.
8509 *
8510 * For the case where L1 and L2 share the same APIC access page
8511 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8512 * in the vmcs12), this function will only update either the vmcs01
8513 * or the vmcs02. If the former, the vmcs02 will be updated by
8514 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8515 * the next L2->L1 exit.
8516 */
8517 if (!is_guest_mode(vcpu) ||
David Matlack4f2777b2016-07-13 17:16:37 -07008518 !nested_cpu_has2(get_vmcs12(&vmx->vcpu),
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008519 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Tang Chen38b99172014-09-24 15:57:54 +08008520 vmcs_write64(APIC_ACCESS_ADDR, hpa);
Jim Mattsonfb6c8192017-03-16 13:53:59 -07008521 vmx_flush_tlb_ept_only(vcpu);
8522 }
Tang Chen38b99172014-09-24 15:57:54 +08008523}
8524
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008525static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008526{
8527 u16 status;
8528 u8 old;
8529
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008530 if (max_isr == -1)
8531 max_isr = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008532
8533 status = vmcs_read16(GUEST_INTR_STATUS);
8534 old = status >> 8;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008535 if (max_isr != old) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08008536 status &= 0xff;
Paolo Bonzini67c9ddd2016-05-10 17:01:23 +02008537 status |= max_isr << 8;
Yang Zhangc7c9c562013-01-25 10:18:51 +08008538 vmcs_write16(GUEST_INTR_STATUS, status);
8539 }
8540}
8541
8542static void vmx_set_rvi(int vector)
8543{
8544 u16 status;
8545 u8 old;
8546
Wei Wang4114c272014-11-05 10:53:43 +08008547 if (vector == -1)
8548 vector = 0;
8549
Yang Zhangc7c9c562013-01-25 10:18:51 +08008550 status = vmcs_read16(GUEST_INTR_STATUS);
8551 old = (u8)status & 0xff;
8552 if ((u8)vector != old) {
8553 status &= ~0xff;
8554 status |= (u8)vector;
8555 vmcs_write16(GUEST_INTR_STATUS, status);
8556 }
8557}
8558
8559static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8560{
Wanpeng Li963fee12014-07-17 19:03:00 +08008561 if (!is_guest_mode(vcpu)) {
8562 vmx_set_rvi(max_irr);
8563 return;
8564 }
8565
Wei Wang4114c272014-11-05 10:53:43 +08008566 if (max_irr == -1)
8567 return;
8568
Wanpeng Li963fee12014-07-17 19:03:00 +08008569 /*
Wei Wang4114c272014-11-05 10:53:43 +08008570 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8571 * handles it.
8572 */
8573 if (nested_exit_on_intr(vcpu))
8574 return;
8575
8576 /*
8577 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008578 * is run without virtual interrupt delivery.
8579 */
8580 if (!kvm_event_needs_reinjection(vcpu) &&
8581 vmx_interrupt_allowed(vcpu)) {
8582 kvm_queue_interrupt(vcpu, max_irr, false);
8583 vmx_inject_irq(vcpu);
8584 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008585}
8586
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008587static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008588{
8589 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008590 int max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008591
Paolo Bonzini76dfafd52016-12-19 17:17:11 +01008592 WARN_ON(!vcpu->arch.apicv_active);
8593 if (pi_test_on(&vmx->pi_desc)) {
8594 pi_clear_on(&vmx->pi_desc);
8595 /*
8596 * IOMMU can write to PIR.ON, so the barrier matters even on UP.
8597 * But on x86 this is just a compiler barrier anyway.
8598 */
8599 smp_mb__after_atomic();
8600 max_irr = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
8601 } else {
8602 max_irr = kvm_lapic_find_highest_irr(vcpu);
8603 }
8604 vmx_hwapic_irr_update(vcpu, max_irr);
8605 return max_irr;
Paolo Bonzini810e6de2016-12-19 13:05:46 +01008606}
8607
Andrey Smetanin63086302015-11-10 15:36:32 +03008608static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008609{
Andrey Smetanind62caab2015-11-10 15:36:33 +03008610 if (!kvm_vcpu_apicv_active(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008611 return;
8612
Yang Zhangc7c9c562013-01-25 10:18:51 +08008613 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8614 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8615 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8616 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8617}
8618
Paolo Bonzini967235d2016-12-19 14:03:45 +01008619static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
8620{
8621 struct vcpu_vmx *vmx = to_vmx(vcpu);
8622
8623 pi_clear_on(&vmx->pi_desc);
8624 memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
8625}
8626
Avi Kivity51aa01d2010-07-20 14:31:20 +03008627static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008628{
Avi Kivity00eba012011-03-07 17:24:54 +02008629 u32 exit_intr_info;
8630
8631 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8632 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8633 return;
8634
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008635 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008636 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008637
8638 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008639 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008640 kvm_machine_check();
8641
Gleb Natapov20f65982009-05-11 13:35:55 +03008642 /* We need to handle NMIs before interrupts are enabled */
Jim Mattsonef85b672016-12-12 11:01:37 -08008643 if (is_nmi(exit_intr_info)) {
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008644 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008645 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008646 kvm_after_handle_nmi(&vmx->vcpu);
8647 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008648}
Gleb Natapov20f65982009-05-11 13:35:55 +03008649
Yang Zhanga547c6d2013-04-11 19:25:10 +08008650static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8651{
8652 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Chris J Arges3f62de52016-01-22 15:44:38 -06008653 register void *__sp asm(_ASM_SP);
Yang Zhanga547c6d2013-04-11 19:25:10 +08008654
Yang Zhanga547c6d2013-04-11 19:25:10 +08008655 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8656 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8657 unsigned int vector;
8658 unsigned long entry;
8659 gate_desc *desc;
8660 struct vcpu_vmx *vmx = to_vmx(vcpu);
8661#ifdef CONFIG_X86_64
8662 unsigned long tmp;
8663#endif
8664
8665 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8666 desc = (gate_desc *)vmx->host_idt_base + vector;
8667 entry = gate_offset(*desc);
8668 asm volatile(
8669#ifdef CONFIG_X86_64
8670 "mov %%" _ASM_SP ", %[sp]\n\t"
8671 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8672 "push $%c[ss]\n\t"
8673 "push %[sp]\n\t"
8674#endif
8675 "pushf\n\t"
Yang Zhanga547c6d2013-04-11 19:25:10 +08008676 __ASM_SIZE(push) " $%c[cs]\n\t"
8677 "call *%[entry]\n\t"
8678 :
8679#ifdef CONFIG_X86_64
Chris J Arges3f62de52016-01-22 15:44:38 -06008680 [sp]"=&r"(tmp),
Yang Zhanga547c6d2013-04-11 19:25:10 +08008681#endif
Chris J Arges3f62de52016-01-22 15:44:38 -06008682 "+r"(__sp)
Yang Zhanga547c6d2013-04-11 19:25:10 +08008683 :
8684 [entry]"r"(entry),
8685 [ss]"i"(__KERNEL_DS),
8686 [cs]"i"(__KERNEL_CS)
8687 );
Paolo Bonzinif2485b32016-06-15 15:23:11 +02008688 }
Yang Zhanga547c6d2013-04-11 19:25:10 +08008689}
8690
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008691static bool vmx_has_high_real_mode_segbase(void)
8692{
8693 return enable_unrestricted_guest || emulate_invalid_guest_state;
8694}
8695
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008696static bool vmx_mpx_supported(void)
8697{
8698 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8699 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8700}
8701
Wanpeng Li55412b22014-12-02 19:21:30 +08008702static bool vmx_xsaves_supported(void)
8703{
8704 return vmcs_config.cpu_based_2nd_exec_ctrl &
8705 SECONDARY_EXEC_XSAVES;
8706}
8707
Avi Kivity51aa01d2010-07-20 14:31:20 +03008708static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8709{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008710 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008711 bool unblock_nmi;
8712 u8 vector;
8713 bool idtv_info_valid;
8714
8715 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008716
Paolo Bonzini2c828782017-03-27 14:37:28 +02008717 if (vmx->nmi_known_unmasked)
8718 return;
8719 /*
8720 * Can't use vmx->exit_intr_info since we're not sure what
8721 * the exit reason is.
8722 */
8723 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8724 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8725 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8726 /*
8727 * SDM 3: 27.7.1.2 (September 2008)
8728 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8729 * a guest IRET fault.
8730 * SDM 3: 23.2.2 (September 2008)
8731 * Bit 12 is undefined in any of the following cases:
8732 * If the VM exit sets the valid bit in the IDT-vectoring
8733 * information field.
8734 * If the VM exit is due to a double fault.
8735 */
8736 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8737 vector != DF_VECTOR && !idtv_info_valid)
8738 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8739 GUEST_INTR_STATE_NMI);
8740 else
8741 vmx->nmi_known_unmasked =
8742 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8743 & GUEST_INTR_STATE_NMI);
Avi Kivity51aa01d2010-07-20 14:31:20 +03008744}
8745
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008746static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008747 u32 idt_vectoring_info,
8748 int instr_len_field,
8749 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008750{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008751 u8 vector;
8752 int type;
8753 bool idtv_info_valid;
8754
8755 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008756
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008757 vcpu->arch.nmi_injected = false;
8758 kvm_clear_exception_queue(vcpu);
8759 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008760
8761 if (!idtv_info_valid)
8762 return;
8763
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008764 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008765
Avi Kivity668f6122008-07-02 09:28:55 +03008766 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8767 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008768
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008769 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008770 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008771 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008772 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008773 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008774 * Clear bit "block by NMI" before VM entry if a NMI
8775 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008776 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008777 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008778 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008779 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008780 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008781 /* fall through */
8782 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008783 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008784 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008785 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008786 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008787 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008788 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008789 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008790 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008791 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008792 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008793 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008794 break;
8795 default:
8796 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008797 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008798}
8799
Avi Kivity83422e12010-07-20 14:43:23 +03008800static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8801{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008802 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008803 VM_EXIT_INSTRUCTION_LEN,
8804 IDT_VECTORING_ERROR_CODE);
8805}
8806
Avi Kivityb463a6f2010-07-20 15:06:17 +03008807static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8808{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008809 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008810 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8811 VM_ENTRY_INSTRUCTION_LEN,
8812 VM_ENTRY_EXCEPTION_ERROR_CODE);
8813
8814 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8815}
8816
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008817static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8818{
8819 int i, nr_msrs;
8820 struct perf_guest_switch_msr *msrs;
8821
8822 msrs = perf_guest_get_msrs(&nr_msrs);
8823
8824 if (!msrs)
8825 return;
8826
8827 for (i = 0; i < nr_msrs; i++)
8828 if (msrs[i].host == msrs[i].guest)
8829 clear_atomic_switch_msr(vmx, msrs[i].msr);
8830 else
8831 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8832 msrs[i].host);
8833}
8834
Jiang Biao33365e72016-11-03 15:03:37 +08008835static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu)
Yunhong Jiang64672c92016-06-13 14:19:59 -07008836{
8837 struct vcpu_vmx *vmx = to_vmx(vcpu);
8838 u64 tscl;
8839 u32 delta_tsc;
8840
8841 if (vmx->hv_deadline_tsc == -1)
8842 return;
8843
8844 tscl = rdtsc();
8845 if (vmx->hv_deadline_tsc > tscl)
8846 /* sure to be 32 bit only because checked on set_hv_timer */
8847 delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
8848 cpu_preemption_timer_multi);
8849 else
8850 delta_tsc = 0;
8851
8852 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
8853}
8854
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08008855static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008856{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008857 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008858 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02008859
Avi Kivity104f2262010-11-18 13:12:52 +02008860 /* Don't enter VMX if guest state is invalid, let the exit handler
8861 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02008862 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02008863 return;
8864
Radim Krčmářa7653ec2014-08-21 18:08:07 +02008865 if (vmx->ple_window_dirty) {
8866 vmx->ple_window_dirty = false;
8867 vmcs_write32(PLE_WINDOW, vmx->ple_window);
8868 }
8869
Abel Gordon012f83c2013-04-18 14:39:25 +03008870 if (vmx->nested.sync_shadow_vmcs) {
8871 copy_vmcs12_to_shadow(vmx);
8872 vmx->nested.sync_shadow_vmcs = false;
8873 }
8874
Avi Kivity104f2262010-11-18 13:12:52 +02008875 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
8876 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
8877 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
8878 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
8879
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07008880 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008881 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
8882 vmcs_writel(HOST_CR4, cr4);
8883 vmx->host_state.vmcs_host_cr4 = cr4;
8884 }
8885
Avi Kivity104f2262010-11-18 13:12:52 +02008886 /* When single-stepping over STI and MOV SS, we must clear the
8887 * corresponding interruptibility bits in the guest state. Otherwise
8888 * vmentry fails as it then expects bit 14 (BS) in pending debug
8889 * exceptions being set, but that's not correct for the guest debugging
8890 * case. */
8891 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8892 vmx_set_interrupt_shadow(vcpu, 0);
8893
Xiao Guangrong1be0e612016-03-22 16:51:18 +08008894 if (vmx->guest_pkru_valid)
8895 __write_pkru(vmx->guest_pkru);
8896
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008897 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008898 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008899
Yunhong Jiang64672c92016-06-13 14:19:59 -07008900 vmx_arm_hv_timer(vcpu);
8901
Nadav Har'Eld462b812011-05-24 15:26:10 +03008902 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02008903 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08008904 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008905 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
8906 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
8907 "push %%" _ASM_CX " \n\t"
8908 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008909 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008910 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008911 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008912 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008913 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008914 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
8915 "mov %%cr2, %%" _ASM_DX " \n\t"
8916 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008917 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008918 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008919 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008920 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02008921 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008922 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008923 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
8924 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
8925 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
8926 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
8927 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
8928 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008929#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008930 "mov %c[r8](%0), %%r8 \n\t"
8931 "mov %c[r9](%0), %%r9 \n\t"
8932 "mov %c[r10](%0), %%r10 \n\t"
8933 "mov %c[r11](%0), %%r11 \n\t"
8934 "mov %c[r12](%0), %%r12 \n\t"
8935 "mov %c[r13](%0), %%r13 \n\t"
8936 "mov %c[r14](%0), %%r14 \n\t"
8937 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008938#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008939 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03008940
Avi Kivity6aa8b732006-12-10 02:21:36 -08008941 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03008942 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008943 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008944 "jmp 2f \n\t"
8945 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
8946 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08008947 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008948 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02008949 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008950 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
8951 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
8952 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
8953 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
8954 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
8955 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
8956 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008957#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008958 "mov %%r8, %c[r8](%0) \n\t"
8959 "mov %%r9, %c[r9](%0) \n\t"
8960 "mov %%r10, %c[r10](%0) \n\t"
8961 "mov %%r11, %c[r11](%0) \n\t"
8962 "mov %%r12, %c[r12](%0) \n\t"
8963 "mov %%r13, %c[r13](%0) \n\t"
8964 "mov %%r14, %c[r14](%0) \n\t"
8965 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008966#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008967 "mov %%cr2, %%" _ASM_AX " \n\t"
8968 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03008969
Avi Kivityb188c81f2012-09-16 15:10:58 +03008970 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02008971 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008972 ".pushsection .rodata \n\t"
8973 ".global vmx_return \n\t"
8974 "vmx_return: " _ASM_PTR " 2b \n\t"
8975 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02008976 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03008977 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02008978 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03008979 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008980 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
8981 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
8982 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
8983 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
8984 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
8985 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
8986 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008987#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008988 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
8989 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
8990 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
8991 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
8992 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
8993 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
8994 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
8995 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008996#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02008997 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
8998 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02008999 : "cc", "memory"
9000#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03009001 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009002 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03009003#else
9004 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02009005#endif
9006 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08009007
Gleb Natapov2a7921b2012-08-12 16:12:29 +03009008 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
9009 if (debugctlmsr)
9010 update_debugctlmsr(debugctlmsr);
9011
Avi Kivityaa67f602012-08-01 16:48:03 +03009012#ifndef CONFIG_X86_64
9013 /*
9014 * The sysexit path does not restore ds/es, so we must set them to
9015 * a reasonable value ourselves.
9016 *
9017 * We can't defer this to vmx_load_host_state() since that function
9018 * may be executed in interrupt context, which saves and restore segments
9019 * around it, nullifying its effect.
9020 */
9021 loadsegment(ds, __USER_DS);
9022 loadsegment(es, __USER_DS);
9023#endif
9024
Avi Kivity6de4f3a2009-05-31 22:58:47 +03009025 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02009026 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009027 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03009028 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02009029 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009030 vcpu->arch.regs_dirty = 0;
9031
Avi Kivity1155f762007-11-22 11:30:47 +02009032 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
9033
Nadav Har'Eld462b812011-05-24 15:26:10 +03009034 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02009035
Avi Kivity51aa01d2010-07-20 14:31:20 +03009036 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03009037
Gleb Natapove0b890d2013-09-25 12:51:33 +03009038 /*
Xiao Guangrong1be0e612016-03-22 16:51:18 +08009039 * eager fpu is enabled if PKEY is supported and CR4 is switched
9040 * back on host, so it is safe to read guest PKRU from current
9041 * XSAVE.
9042 */
9043 if (boot_cpu_has(X86_FEATURE_OSPKE)) {
9044 vmx->guest_pkru = __read_pkru();
9045 if (vmx->guest_pkru != vmx->host_pkru) {
9046 vmx->guest_pkru_valid = true;
9047 __write_pkru(vmx->host_pkru);
9048 } else
9049 vmx->guest_pkru_valid = false;
9050 }
9051
9052 /*
Gleb Natapove0b890d2013-09-25 12:51:33 +03009053 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
9054 * we did not inject a still-pending event to L1 now because of
9055 * nested_run_pending, we need to re-enable this bit.
9056 */
9057 if (vmx->nested.nested_run_pending)
9058 kvm_make_request(KVM_REQ_EVENT, vcpu);
9059
9060 vmx->nested.nested_run_pending = 0;
9061
Avi Kivity51aa01d2010-07-20 14:31:20 +03009062 vmx_complete_atomic_exit(vmx);
9063 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03009064 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009065}
9066
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009067static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009068{
9069 struct vcpu_vmx *vmx = to_vmx(vcpu);
9070 int cpu;
9071
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009072 if (vmx->loaded_vmcs == vmcs)
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009073 return;
9074
9075 cpu = get_cpu();
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009076 vmx->loaded_vmcs = vmcs;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009077 vmx_vcpu_put(vcpu);
9078 vmx_vcpu_load(vcpu, cpu);
9079 vcpu->cpu = cpu;
9080 put_cpu();
9081}
9082
Jim Mattson2f1fe812016-07-08 15:36:06 -07009083/*
9084 * Ensure that the current vmcs of the logical processor is the
9085 * vmcs01 of the vcpu before calling free_nested().
9086 */
9087static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
9088{
9089 struct vcpu_vmx *vmx = to_vmx(vcpu);
9090 int r;
9091
9092 r = vcpu_load(vcpu);
9093 BUG_ON(r);
David Hildenbrand1279a6b12017-03-20 10:00:08 +01009094 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009095 free_nested(vmx);
9096 vcpu_put(vcpu);
9097}
9098
Avi Kivity6aa8b732006-12-10 02:21:36 -08009099static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
9100{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009101 struct vcpu_vmx *vmx = to_vmx(vcpu);
9102
Kai Huang843e4332015-01-28 10:54:28 +08009103 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08009104 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08009105 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009106 leave_guest_mode(vcpu);
Jim Mattson2f1fe812016-07-08 15:36:06 -07009107 vmx_free_vcpu_nested(vcpu);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02009108 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009109 kfree(vmx->guest_msrs);
9110 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10009111 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009112}
9113
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009114static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08009115{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009116 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10009117 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03009118 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009119
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009120 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009121 return ERR_PTR(-ENOMEM);
9122
Wanpeng Li991e7a02015-09-16 17:30:05 +08009123 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08009124
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009125 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
9126 if (err)
9127 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009128
Peter Feiner4e595162016-07-07 14:49:58 -07009129 err = -ENOMEM;
9130
9131 /*
9132 * If PML is turned on, failure on enabling PML just results in failure
9133 * of creating the vcpu, therefore we can simplify PML logic (by
9134 * avoiding dealing with cases, such as enabling PML partially on vcpus
9135 * for the guest, etc.
9136 */
9137 if (enable_pml) {
9138 vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
9139 if (!vmx->pml_pg)
9140 goto uninit_vcpu;
9141 }
9142
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009143 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02009144 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
9145 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03009146
Peter Feiner4e595162016-07-07 14:49:58 -07009147 if (!vmx->guest_msrs)
9148 goto free_pml;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009149
Nadav Har'Eld462b812011-05-24 15:26:10 +03009150 vmx->loaded_vmcs = &vmx->vmcs01;
9151 vmx->loaded_vmcs->vmcs = alloc_vmcs();
Jim Mattson355f4fb2016-10-28 08:29:39 -07009152 vmx->loaded_vmcs->shadow_vmcs = NULL;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009153 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009154 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03009155 loaded_vmcs_init(vmx->loaded_vmcs);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04009156
Avi Kivity15ad7142007-07-11 18:17:21 +03009157 cpu = get_cpu();
9158 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10009159 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10009160 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009161 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03009162 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009163 if (err)
9164 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02009165 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02009166 err = alloc_apic_access_page(kvm);
9167 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02009168 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02009169 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08009170
Sheng Yangb927a3c2009-07-21 10:42:48 +08009171 if (enable_ept) {
9172 if (!kvm->arch.ept_identity_map_addr)
9173 kvm->arch.ept_identity_map_addr =
9174 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08009175 err = init_rmode_identity_map(kvm);
9176 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02009177 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08009178 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08009179
Wanpeng Li5c614b32015-10-13 09:18:36 -07009180 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08009181 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07009182 vmx->nested.vpid02 = allocate_vpid();
9183 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08009184
Wincy Van705699a2015-02-03 23:58:17 +08009185 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03009186 vmx->nested.current_vmptr = -1ull;
9187 vmx->nested.current_vmcs12 = NULL;
9188
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009189 vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
9190
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009191 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08009192
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009193free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07009194 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08009195 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009196free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009197 kfree(vmx->guest_msrs);
Peter Feiner4e595162016-07-07 14:49:58 -07009198free_pml:
9199 vmx_destroy_pml_buffer(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009200uninit_vcpu:
9201 kvm_vcpu_uninit(&vmx->vcpu);
9202free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08009203 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10009204 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10009205 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08009206}
9207
Yang, Sheng002c7f72007-07-31 14:23:01 +03009208static void __init vmx_check_processor_compat(void *rtn)
9209{
9210 struct vmcs_config vmcs_conf;
9211
9212 *(int *)rtn = 0;
9213 if (setup_vmcs_config(&vmcs_conf) < 0)
9214 *(int *)rtn = -EIO;
9215 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
9216 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
9217 smp_processor_id());
9218 *(int *)rtn = -EIO;
9219 }
9220}
9221
Sheng Yang67253af2008-04-25 10:20:22 +08009222static int get_ept_level(void)
9223{
9224 return VMX_EPT_DEFAULT_GAW + 1;
9225}
9226
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009227static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08009228{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009229 u8 cache;
9230 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009231
Sheng Yang522c68c2009-04-27 20:35:43 +08009232 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02009233 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08009234 * 2. EPT with VT-d:
9235 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02009236 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08009237 * b. VT-d with snooping control feature: snooping control feature of
9238 * VT-d engine can guarantee the cache correctness. Just set it
9239 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08009240 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08009241 * consistent with host MTRR
9242 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02009243 if (is_mmio) {
9244 cache = MTRR_TYPE_UNCACHABLE;
9245 goto exit;
9246 }
9247
9248 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009249 ipat = VMX_EPT_IPAT_BIT;
9250 cache = MTRR_TYPE_WRBACK;
9251 goto exit;
9252 }
9253
9254 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
9255 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02009256 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08009257 cache = MTRR_TYPE_WRBACK;
9258 else
9259 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009260 goto exit;
9261 }
9262
Xiao Guangrongff536042015-06-15 16:55:22 +08009263 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08009264
9265exit:
9266 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08009267}
9268
Sheng Yang17cc3932010-01-05 19:02:27 +08009269static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02009270{
Sheng Yang878403b2010-01-05 19:02:29 +08009271 if (enable_ept && !cpu_has_vmx_ept_1g_page())
9272 return PT_DIRECTORY_LEVEL;
9273 else
9274 /* For shadow and EPT supported 1GB page */
9275 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02009276}
9277
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009278static void vmcs_set_secondary_exec_control(u32 new_ctl)
9279{
9280 /*
9281 * These bits in the secondary execution controls field
9282 * are dynamic, the others are mostly based on the hypervisor
9283 * architecture and the guest's CPUID. Do not touch the
9284 * dynamic bits.
9285 */
9286 u32 mask =
9287 SECONDARY_EXEC_SHADOW_VMCS |
9288 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
9289 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9290
9291 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
9292
9293 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
9294 (new_ctl & ~mask) | (cur_ctl & mask));
9295}
9296
David Matlack8322ebb2016-11-29 18:14:09 -08009297/*
9298 * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
9299 * (indicating "allowed-1") if they are supported in the guest's CPUID.
9300 */
9301static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
9302{
9303 struct vcpu_vmx *vmx = to_vmx(vcpu);
9304 struct kvm_cpuid_entry2 *entry;
9305
9306 vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff;
9307 vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE;
9308
9309#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
9310 if (entry && (entry->_reg & (_cpuid_mask))) \
9311 vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \
9312} while (0)
9313
9314 entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
9315 cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
9316 cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
9317 cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
9318 cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
9319 cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
9320 cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
9321 cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
9322 cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
9323 cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
9324 cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
9325 cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
9326 cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
9327 cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
9328 cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
9329
9330 entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9331 cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
9332 cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
9333 cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
9334 cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
9335 /* TODO: Use X86_CR4_UMIP and X86_FEATURE_UMIP macros */
9336 cr4_fixed1_update(bit(11), ecx, bit(2));
9337
9338#undef cr4_fixed1_update
9339}
9340
Sheng Yang0e851882009-12-18 16:48:46 +08009341static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
9342{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009343 struct kvm_cpuid_entry2 *best;
9344 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009345 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009346
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009347 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009348 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
9349 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009350 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08009351
Paolo Bonzini8b972652015-09-15 17:34:42 +02009352 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08009353 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02009354 vmx->nested.nested_vmx_secondary_ctls_high |=
9355 SECONDARY_EXEC_RDTSCP;
9356 else
9357 vmx->nested.nested_vmx_secondary_ctls_high &=
9358 ~SECONDARY_EXEC_RDTSCP;
9359 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009360 }
Mao, Junjiead756a12012-07-02 01:18:48 +00009361
Mao, Junjiead756a12012-07-02 01:18:48 +00009362 /* Exposing INVPCID only when PCID is exposed */
9363 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
9364 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009365 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
9366 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009367 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08009368
Mao, Junjiead756a12012-07-02 01:18:48 +00009369 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00009370 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00009371 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009372
Huaitong Han45bdbcf2016-01-12 16:04:20 +08009373 if (cpu_has_secondary_exec_ctrls())
9374 vmcs_set_secondary_exec_control(secondary_exec_ctl);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08009375
Haozhong Zhang37e4c992016-06-22 14:59:55 +08009376 if (nested_vmx_allowed(vcpu))
9377 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
9378 FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
9379 else
9380 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
9381 ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
David Matlack8322ebb2016-11-29 18:14:09 -08009382
9383 if (nested_vmx_allowed(vcpu))
9384 nested_vmx_cr_fixed1_bits_update(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08009385}
9386
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009387static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
9388{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03009389 if (func == 1 && nested)
9390 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009391}
9392
Yang Zhang25d92082013-08-06 12:00:32 +03009393static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
9394 struct x86_exception *fault)
9395{
Jan Kiszka533558b2014-01-04 18:47:20 +01009396 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9397 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03009398
9399 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01009400 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03009401 else
Jan Kiszka533558b2014-01-04 18:47:20 +01009402 exit_reason = EXIT_REASON_EPT_VIOLATION;
9403 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03009404 vmcs12->guest_physical_address = fault->address;
9405}
9406
Nadav Har'El155a97a2013-08-05 11:07:16 +03009407/* Callbacks for nested_ept_init_mmu_context: */
9408
9409static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
9410{
9411 /* return the page table to be shadowed - in our case, EPT12 */
9412 return get_vmcs12(vcpu)->ept_pointer;
9413}
9414
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009415static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03009416{
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009417 u64 eptp;
9418
Paolo Bonziniad896af2013-10-02 16:56:14 +02009419 WARN_ON(mmu_is_nested(vcpu));
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009420 eptp = nested_ept_get_cr3(vcpu);
9421 if ((eptp & VMX_EPT_AD_ENABLE_BIT) && !enable_ept_ad_bits)
9422 return 1;
9423
9424 kvm_mmu_unload(vcpu);
Paolo Bonziniad896af2013-10-02 16:56:14 +02009425 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009426 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009427 VMX_EPT_EXECUTE_ONLY_BIT,
9428 eptp & VMX_EPT_AD_ENABLE_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009429 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
9430 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
9431 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
9432
9433 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Paolo Bonziniae1e2d12017-03-30 11:55:30 +02009434 return 0;
Nadav Har'El155a97a2013-08-05 11:07:16 +03009435}
9436
9437static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
9438{
9439 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
9440}
9441
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009442static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
9443 u16 error_code)
9444{
9445 bool inequality, bit;
9446
9447 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
9448 inequality =
9449 (error_code & vmcs12->page_fault_error_code_mask) !=
9450 vmcs12->page_fault_error_code_match;
9451 return inequality ^ bit;
9452}
9453
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009454static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9455 struct x86_exception *fault)
9456{
9457 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9458
9459 WARN_ON(!is_guest_mode(vcpu));
9460
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009461 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009462 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9463 vmcs_read32(VM_EXIT_INTR_INFO),
9464 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009465 else
9466 kvm_inject_page_fault(vcpu, fault);
9467}
9468
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009469static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9470 struct vmcs12 *vmcs12);
9471
9472static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009473 struct vmcs12 *vmcs12)
9474{
9475 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009476 u64 hpa;
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009477
9478 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009479 /*
9480 * Translate L1 physical address to host physical
9481 * address for vmcs02. Keep the page pinned, so this
9482 * physical address remains valid. We keep a reference
9483 * to it so we can release it later.
9484 */
9485 if (vmx->nested.apic_access_page) /* shouldn't happen */
9486 nested_release_page(vmx->nested.apic_access_page);
9487 vmx->nested.apic_access_page =
9488 nested_get_page(vcpu, vmcs12->apic_access_addr);
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009489 /*
9490 * If translation failed, no matter: This feature asks
9491 * to exit when accessing the given address, and if it
9492 * can never be accessed, this feature won't do
9493 * anything anyway.
9494 */
9495 if (vmx->nested.apic_access_page) {
9496 hpa = page_to_phys(vmx->nested.apic_access_page);
9497 vmcs_write64(APIC_ACCESS_ADDR, hpa);
9498 } else {
9499 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
9500 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9501 }
9502 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
9503 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
9504 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
9505 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
9506 kvm_vcpu_reload_apic_access_page(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009507 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009508
9509 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009510 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9511 nested_release_page(vmx->nested.virtual_apic_page);
9512 vmx->nested.virtual_apic_page =
9513 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9514
9515 /*
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009516 * If translation failed, VM entry will fail because
9517 * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull.
9518 * Failing the vm entry is _not_ what the processor
9519 * does but it's basically the only possibility we
9520 * have. We could still enter the guest if CR8 load
9521 * exits are enabled, CR8 store exits are enabled, and
9522 * virtualize APIC access is disabled; in this case
9523 * the processor would never use the TPR shadow and we
9524 * could simply clear the bit from the execution
9525 * control. But such a configuration is useless, so
9526 * let's keep the code simple.
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009527 */
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009528 if (vmx->nested.virtual_apic_page) {
9529 hpa = page_to_phys(vmx->nested.virtual_apic_page);
9530 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa);
9531 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009532 }
9533
Wincy Van705699a2015-02-03 23:58:17 +08009534 if (nested_cpu_has_posted_intr(vmcs12)) {
Wincy Van705699a2015-02-03 23:58:17 +08009535 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9536 kunmap(vmx->nested.pi_desc_page);
9537 nested_release_page(vmx->nested.pi_desc_page);
9538 }
9539 vmx->nested.pi_desc_page =
9540 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
Wincy Van705699a2015-02-03 23:58:17 +08009541 vmx->nested.pi_desc =
9542 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9543 if (!vmx->nested.pi_desc) {
9544 nested_release_page_clean(vmx->nested.pi_desc_page);
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009545 return;
Wincy Van705699a2015-02-03 23:58:17 +08009546 }
9547 vmx->nested.pi_desc =
9548 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9549 (unsigned long)(vmcs12->posted_intr_desc_addr &
9550 (PAGE_SIZE - 1)));
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009551 vmcs_write64(POSTED_INTR_DESC_ADDR,
9552 page_to_phys(vmx->nested.pi_desc_page) +
9553 (unsigned long)(vmcs12->posted_intr_desc_addr &
9554 (PAGE_SIZE - 1)));
Wincy Van705699a2015-02-03 23:58:17 +08009555 }
Jim Mattson6beb7bd2016-11-30 12:03:45 -08009556 if (cpu_has_vmx_msr_bitmap() &&
9557 nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) &&
9558 nested_vmx_merge_msr_bitmap(vcpu, vmcs12))
9559 ;
9560 else
9561 vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL,
9562 CPU_BASED_USE_MSR_BITMAPS);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009563}
9564
Jan Kiszkaf41245002014-03-07 20:03:13 +01009565static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9566{
9567 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9568 struct vcpu_vmx *vmx = to_vmx(vcpu);
9569
9570 if (vcpu->arch.virtual_tsc_khz == 0)
9571 return;
9572
9573 /* Make sure short timeouts reliably trigger an immediate vmexit.
9574 * hrtimer_start does not guarantee this. */
9575 if (preemption_timeout <= 1) {
9576 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9577 return;
9578 }
9579
9580 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9581 preemption_timeout *= 1000000;
9582 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9583 hrtimer_start(&vmx->nested.preemption_timer,
9584 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9585}
9586
Wincy Van3af18d92015-02-03 23:49:31 +08009587static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9588 struct vmcs12 *vmcs12)
9589{
9590 int maxphyaddr;
9591 u64 addr;
9592
9593 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9594 return 0;
9595
9596 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9597 WARN_ON(1);
9598 return -EINVAL;
9599 }
9600 maxphyaddr = cpuid_maxphyaddr(vcpu);
9601
9602 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9603 ((addr + PAGE_SIZE) >> maxphyaddr))
9604 return -EINVAL;
9605
9606 return 0;
9607}
9608
9609/*
9610 * Merge L0's and L1's MSR bitmap, return false to indicate that
9611 * we do not use the hardware.
9612 */
9613static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9614 struct vmcs12 *vmcs12)
9615{
Wincy Van82f0dd42015-02-03 23:57:18 +08009616 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009617 struct page *page;
Radim Krčmářd048c092016-08-08 20:16:22 +02009618 unsigned long *msr_bitmap_l1;
9619 unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap;
Wincy Vanf2b93282015-02-03 23:56:03 +08009620
Radim Krčmářd048c092016-08-08 20:16:22 +02009621 /* This shortcut is ok because we support only x2APIC MSRs so far. */
Wincy Vanf2b93282015-02-03 23:56:03 +08009622 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
9623 return false;
9624
9625 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
Radim Krčmář05d8d342017-03-07 17:51:49 +01009626 if (!page)
Wincy Vanf2b93282015-02-03 23:56:03 +08009627 return false;
Radim Krčmářd048c092016-08-08 20:16:22 +02009628 msr_bitmap_l1 = (unsigned long *)kmap(page);
Wincy Vanf2b93282015-02-03 23:56:03 +08009629
Radim Krčmářd048c092016-08-08 20:16:22 +02009630 memset(msr_bitmap_l0, 0xff, PAGE_SIZE);
9631
Wincy Vanf2b93282015-02-03 23:56:03 +08009632 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009633 if (nested_cpu_has_apic_reg_virt(vmcs12))
9634 for (msr = 0x800; msr <= 0x8ff; msr++)
9635 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009636 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van82f0dd42015-02-03 23:57:18 +08009637 msr, MSR_TYPE_R);
Radim Krčmářd048c092016-08-08 20:16:22 +02009638
9639 nested_vmx_disable_intercept_for_msr(
9640 msr_bitmap_l1, msr_bitmap_l0,
Wincy Vanf2b93282015-02-03 23:56:03 +08009641 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9642 MSR_TYPE_R | MSR_TYPE_W);
Radim Krčmářd048c092016-08-08 20:16:22 +02009643
Wincy Van608406e2015-02-03 23:57:51 +08009644 if (nested_cpu_has_vid(vmcs12)) {
Wincy Van608406e2015-02-03 23:57:51 +08009645 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009646 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009647 APIC_BASE_MSR + (APIC_EOI >> 4),
9648 MSR_TYPE_W);
9649 nested_vmx_disable_intercept_for_msr(
Radim Krčmářd048c092016-08-08 20:16:22 +02009650 msr_bitmap_l1, msr_bitmap_l0,
Wincy Van608406e2015-02-03 23:57:51 +08009651 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9652 MSR_TYPE_W);
9653 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009654 }
Wincy Vanf2b93282015-02-03 23:56:03 +08009655 kunmap(page);
9656 nested_release_page_clean(page);
9657
9658 return true;
9659}
9660
9661static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9662 struct vmcs12 *vmcs12)
9663{
Wincy Van82f0dd42015-02-03 23:57:18 +08009664 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009665 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009666 !nested_cpu_has_vid(vmcs12) &&
9667 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009668 return 0;
9669
9670 /*
9671 * If virtualize x2apic mode is enabled,
9672 * virtualize apic access must be disabled.
9673 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009674 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9675 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009676 return -EINVAL;
9677
Wincy Van608406e2015-02-03 23:57:51 +08009678 /*
9679 * If virtual interrupt delivery is enabled,
9680 * we must exit on external interrupts.
9681 */
9682 if (nested_cpu_has_vid(vmcs12) &&
9683 !nested_exit_on_intr(vcpu))
9684 return -EINVAL;
9685
Wincy Van705699a2015-02-03 23:58:17 +08009686 /*
9687 * bits 15:8 should be zero in posted_intr_nv,
9688 * the descriptor address has been already checked
9689 * in nested_get_vmcs12_pages.
9690 */
9691 if (nested_cpu_has_posted_intr(vmcs12) &&
9692 (!nested_cpu_has_vid(vmcs12) ||
9693 !nested_exit_intr_ack_set(vcpu) ||
9694 vmcs12->posted_intr_nv & 0xff00))
9695 return -EINVAL;
9696
Wincy Vanf2b93282015-02-03 23:56:03 +08009697 /* tpr shadow is needed by all apicv features. */
9698 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9699 return -EINVAL;
9700
9701 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009702}
9703
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009704static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9705 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009706 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009707{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009708 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009709 u64 count, addr;
9710
9711 if (vmcs12_read_any(vcpu, count_field, &count) ||
9712 vmcs12_read_any(vcpu, addr_field, &addr)) {
9713 WARN_ON(1);
9714 return -EINVAL;
9715 }
9716 if (count == 0)
9717 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009718 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009719 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9720 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009721 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009722 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9723 addr_field, maxphyaddr, count, addr);
9724 return -EINVAL;
9725 }
9726 return 0;
9727}
9728
9729static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9730 struct vmcs12 *vmcs12)
9731{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009732 if (vmcs12->vm_exit_msr_load_count == 0 &&
9733 vmcs12->vm_exit_msr_store_count == 0 &&
9734 vmcs12->vm_entry_msr_load_count == 0)
9735 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009736 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009737 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009738 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009739 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009740 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009741 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009742 return -EINVAL;
9743 return 0;
9744}
9745
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009746static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9747 struct vmx_msr_entry *e)
9748{
9749 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009750 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009751 return -EINVAL;
9752 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9753 e->index == MSR_IA32_UCODE_REV)
9754 return -EINVAL;
9755 if (e->reserved != 0)
9756 return -EINVAL;
9757 return 0;
9758}
9759
9760static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9761 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009762{
9763 if (e->index == MSR_FS_BASE ||
9764 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009765 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9766 nested_vmx_msr_check_common(vcpu, e))
9767 return -EINVAL;
9768 return 0;
9769}
9770
9771static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9772 struct vmx_msr_entry *e)
9773{
9774 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9775 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009776 return -EINVAL;
9777 return 0;
9778}
9779
9780/*
9781 * Load guest's/host's msr at nested entry/exit.
9782 * return 0 for success, entry index for failure.
9783 */
9784static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9785{
9786 u32 i;
9787 struct vmx_msr_entry e;
9788 struct msr_data msr;
9789
9790 msr.host_initiated = false;
9791 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009792 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9793 &e, sizeof(e))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009794 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009795 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9796 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009797 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009798 }
9799 if (nested_vmx_load_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009800 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009801 "%s check failed (%u, 0x%x, 0x%x)\n",
9802 __func__, i, e.index, e.reserved);
9803 goto fail;
9804 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009805 msr.index = e.index;
9806 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009807 if (kvm_set_msr(vcpu, &msr)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009808 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009809 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9810 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +03009811 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009812 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009813 }
9814 return 0;
9815fail:
9816 return i + 1;
9817}
9818
9819static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9820{
9821 u32 i;
9822 struct vmx_msr_entry e;
9823
9824 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009825 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009826 if (kvm_vcpu_read_guest(vcpu,
9827 gpa + i * sizeof(e),
9828 &e, 2 * sizeof(u32))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009829 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009830 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9831 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009832 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009833 }
9834 if (nested_vmx_store_msr_check(vcpu, &e)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009835 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009836 "%s check failed (%u, 0x%x, 0x%x)\n",
9837 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +03009838 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009839 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009840 msr_info.host_initiated = false;
9841 msr_info.index = e.index;
9842 if (kvm_get_msr(vcpu, &msr_info)) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009843 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009844 "%s cannot read MSR (%u, 0x%x)\n",
9845 __func__, i, e.index);
9846 return -EINVAL;
9847 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009848 if (kvm_vcpu_write_guest(vcpu,
9849 gpa + i * sizeof(e) +
9850 offsetof(struct vmx_msr_entry, value),
9851 &msr_info.data, sizeof(msr_info.data))) {
Paolo Bonzinibbe41b92016-08-19 17:51:20 +02009852 pr_debug_ratelimited(
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009853 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009854 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009855 return -EINVAL;
9856 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009857 }
9858 return 0;
9859}
9860
Ladi Prosek1dc35da2016-11-30 16:03:11 +01009861static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val)
9862{
9863 unsigned long invalid_mask;
9864
9865 invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu);
9866 return (val & invalid_mask) == 0;
9867}
9868
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009869/*
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +01009870 * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are
9871 * emulating VM entry into a guest with EPT enabled.
9872 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
9873 * is assigned to entry_failure_code on failure.
9874 */
9875static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept,
Jim Mattsonca0bde22016-11-30 12:03:46 -08009876 u32 *entry_failure_code)
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +01009877{
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +01009878 if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) {
Ladi Prosek1dc35da2016-11-30 16:03:11 +01009879 if (!nested_cr3_valid(vcpu, cr3)) {
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +01009880 *entry_failure_code = ENTRY_FAIL_DEFAULT;
9881 return 1;
9882 }
9883
9884 /*
9885 * If PAE paging and EPT are both on, CR3 is not used by the CPU and
9886 * must not be dereferenced.
9887 */
9888 if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) &&
9889 !nested_ept) {
9890 if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) {
9891 *entry_failure_code = ENTRY_FAIL_PDPTE;
9892 return 1;
9893 }
9894 }
9895
9896 vcpu->arch.cr3 = cr3;
9897 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
9898 }
9899
9900 kvm_mmu_reset_context(vcpu);
9901 return 0;
9902}
9903
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009904/*
9905 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
9906 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +08009907 * 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 +03009908 * guest in a way that will both be appropriate to L1's requests, and our
9909 * needs. In addition to modifying the active vmcs (which is vmcs02), this
9910 * function also has additional necessary side-effects, like setting various
9911 * vcpu->arch fields.
Ladi Prosekee146c12016-11-30 16:03:09 +01009912 * Returns 0 on success, 1 on failure. Invalid state exit qualification code
9913 * is assigned to entry_failure_code on failure.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009914 */
Ladi Prosekee146c12016-11-30 16:03:09 +01009915static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
Jim Mattsonca0bde22016-11-30 12:03:46 -08009916 bool from_vmentry, u32 *entry_failure_code)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009917{
9918 struct vcpu_vmx *vmx = to_vmx(vcpu);
9919 u32 exec_control;
9920
9921 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
9922 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
9923 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
9924 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
9925 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
9926 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
9927 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
9928 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
9929 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
9930 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
9931 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
9932 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
9933 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
9934 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
9935 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
9936 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
9937 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
9938 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
9939 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
9940 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
9941 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
9942 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
9943 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
9944 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
9945 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
9946 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
9947 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
9948 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
9949 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
9950 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
9951 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
9952 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
9953 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
9954 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
9955 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
9956 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
9957
Jim Mattsoncf8b84f2016-11-30 12:03:42 -08009958 if (from_vmentry &&
9959 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) {
Jan Kiszka2996fca2014-06-16 13:59:43 +02009960 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
9961 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
9962 } else {
9963 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
9964 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
9965 }
Jim Mattsoncf8b84f2016-11-30 12:03:42 -08009966 if (from_vmentry) {
9967 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
9968 vmcs12->vm_entry_intr_info_field);
9969 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
9970 vmcs12->vm_entry_exception_error_code);
9971 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
9972 vmcs12->vm_entry_instruction_len);
9973 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
9974 vmcs12->guest_interruptibility_info);
9975 } else {
9976 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
9977 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009978 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +03009979 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009980 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
9981 vmcs12->guest_pending_dbg_exceptions);
9982 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
9983 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
9984
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009985 if (nested_cpu_has_xsaves(vmcs12))
9986 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009987 vmcs_write64(VMCS_LINK_POINTER, -1ull);
9988
Jan Kiszkaf41245002014-03-07 20:03:13 +01009989 exec_control = vmcs12->pin_based_vm_exec_control;
Wincy Van705699a2015-02-03 23:58:17 +08009990
Paolo Bonzini9314006db2016-07-06 13:23:51 +02009991 /* Preemption timer setting is only taken from vmcs01. */
9992 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9993 exec_control |= vmcs_config.pin_based_exec_ctrl;
9994 if (vmx->hv_deadline_tsc == -1)
9995 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9996
9997 /* Posted interrupts setting is only taken from vmcs12. */
Wincy Van705699a2015-02-03 23:58:17 +08009998 if (nested_cpu_has_posted_intr(vmcs12)) {
9999 /*
10000 * Note that we use L0's vector here and in
10001 * vmx_deliver_nested_posted_interrupt.
10002 */
10003 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
10004 vmx->nested.pi_pending = false;
Li RongQing0bcf2612015-12-03 13:29:34 +080010005 vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010006 } else {
Wincy Van705699a2015-02-03 23:58:17 +080010007 exec_control &= ~PIN_BASED_POSTED_INTR;
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010008 }
Wincy Van705699a2015-02-03 23:58:17 +080010009
Jan Kiszkaf41245002014-03-07 20:03:13 +010010010 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010011
Jan Kiszkaf41245002014-03-07 20:03:13 +010010012 vmx->nested.preemption_timer_expired = false;
10013 if (nested_cpu_has_preemption_timer(vmcs12))
10014 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +010010015
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010016 /*
10017 * Whether page-faults are trapped is determined by a combination of
10018 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
10019 * If enable_ept, L0 doesn't care about page faults and we should
10020 * set all of these to L1's desires. However, if !enable_ept, L0 does
10021 * care about (at least some) page faults, and because it is not easy
10022 * (if at all possible?) to merge L0 and L1's desires, we simply ask
10023 * to exit on each and every L2 page fault. This is done by setting
10024 * MASK=MATCH=0 and (see below) EB.PF=1.
10025 * Note that below we don't need special code to set EB.PF beyond the
10026 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
10027 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
10028 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
10029 *
10030 * A problem with this approach (when !enable_ept) is that L1 may be
10031 * injected with more page faults than it asked for. This could have
10032 * caused problems, but in practice existing hypervisors don't care.
10033 * To fix this, we will need to emulate the PFEC checking (on the L1
10034 * page tables), using walk_addr(), when injecting PFs to L1.
10035 */
10036 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
10037 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
10038 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
10039 enable_ept ? vmcs12->page_fault_error_code_match : 0);
10040
10041 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf41245002014-03-07 20:03:13 +010010042 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +080010043
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010044 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010045 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +010010046 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +020010047 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Dan Williamsdfa169b2016-06-02 11:17:24 -070010048 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010049 if (nested_cpu_has(vmcs12,
10050 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
10051 exec_control |= vmcs12->secondary_vm_exec_control;
10052
Wincy Van608406e2015-02-03 23:57:51 +080010053 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
10054 vmcs_write64(EOI_EXIT_BITMAP0,
10055 vmcs12->eoi_exit_bitmap0);
10056 vmcs_write64(EOI_EXIT_BITMAP1,
10057 vmcs12->eoi_exit_bitmap1);
10058 vmcs_write64(EOI_EXIT_BITMAP2,
10059 vmcs12->eoi_exit_bitmap2);
10060 vmcs_write64(EOI_EXIT_BITMAP3,
10061 vmcs12->eoi_exit_bitmap3);
10062 vmcs_write16(GUEST_INTR_STATUS,
10063 vmcs12->guest_intr_status);
10064 }
10065
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010066 /*
10067 * Write an illegal value to APIC_ACCESS_ADDR. Later,
10068 * nested_get_vmcs12_pages will either fix it up or
10069 * remove the VM execution control.
10070 */
10071 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
10072 vmcs_write64(APIC_ACCESS_ADDR, -1ull);
10073
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010074 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
10075 }
10076
10077
10078 /*
10079 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
10080 * Some constant fields are set here by vmx_set_constant_host_state().
10081 * Other fields are different per CPU, and will be set later when
10082 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
10083 */
Yang Zhanga547c6d2013-04-11 19:25:10 +080010084 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010085
10086 /*
Jim Mattson83bafef2016-10-04 10:48:38 -070010087 * Set the MSR load/store lists to match L0's settings.
10088 */
10089 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
10090 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10091 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
10092 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10093 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
10094
10095 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010096 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
10097 * entry, but only if the current (host) sp changed from the value
10098 * we wrote last (vmx->host_rsp). This cache is no longer relevant
10099 * if we switch vmcs, and rather than hold a separate cache per vmcs,
10100 * here we just force the write to happen on entry.
10101 */
10102 vmx->host_rsp = 0;
10103
10104 exec_control = vmx_exec_control(vmx); /* L0's desires */
10105 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
10106 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
10107 exec_control &= ~CPU_BASED_TPR_SHADOW;
10108 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010109
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010110 /*
10111 * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if
10112 * nested_get_vmcs12_pages can't fix it up, the illegal value
10113 * will result in a VM entry failure.
10114 */
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010115 if (exec_control & CPU_BASED_TPR_SHADOW) {
Jim Mattson6beb7bd2016-11-30 12:03:45 -080010116 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull);
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010117 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
10118 }
10119
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010120 /*
Wincy Van3af18d92015-02-03 23:49:31 +080010121 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010122 * Rather, exit every time.
10123 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010124 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
10125 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
10126
10127 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
10128
10129 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
10130 * bitwise-or of what L1 wants to trap for L2, and what we want to
10131 * trap. Note that CR0.TS also needs updating - we do this later.
10132 */
10133 update_exception_bitmap(vcpu);
10134 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
10135 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10136
Nadav Har'El8049d652013-08-05 11:07:06 +030010137 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
10138 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
10139 * bits are further modified by vmx_set_efer() below.
10140 */
Jan Kiszkaf41245002014-03-07 20:03:13 +010010141 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +030010142
10143 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
10144 * emulated by vmx_set_efer(), below.
10145 */
Gleb Natapov2961e8762013-11-25 15:37:13 +020010146 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +030010147 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
10148 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010149 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
10150
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010151 if (from_vmentry &&
10152 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010153 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010154 vcpu->arch.pat = vmcs12->guest_ia32_pat;
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010155 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010156 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010157 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010158
10159 set_cr4_guest_host_mask(vmx);
10160
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010161 if (from_vmentry &&
10162 vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010163 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
10164
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010165 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
10166 vmcs_write64(TSC_OFFSET,
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010167 vcpu->arch.tsc_offset + vmcs12->tsc_offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010168 else
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010169 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Peter Feinerc95ba922016-08-17 09:36:47 -070010170 if (kvm_has_tsc_control)
10171 decache_tsc_multiplier(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010172
10173 if (enable_vpid) {
10174 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -070010175 * There is no direct mapping between vpid02 and vpid12, the
10176 * vpid02 is per-vCPU for L0 and reused while the value of
10177 * vpid12 is changed w/ one invvpid during nested vmentry.
10178 * The vpid12 is allocated by L1 for L2, so it will not
10179 * influence global bitmap(for vpid01 and vpid02 allocation)
10180 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010181 */
Wanpeng Li5c614b32015-10-13 09:18:36 -070010182 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
10183 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
10184 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
10185 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
10186 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
10187 }
10188 } else {
10189 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
10190 vmx_flush_tlb(vcpu);
10191 }
10192
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010193 }
10194
Nadav Har'El155a97a2013-08-05 11:07:16 +030010195 if (nested_cpu_has_ept(vmcs12)) {
Paolo Bonziniae1e2d12017-03-30 11:55:30 +020010196 if (nested_ept_init_mmu_context(vcpu)) {
10197 *entry_failure_code = ENTRY_FAIL_DEFAULT;
10198 return 1;
10199 }
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010200 } else if (nested_cpu_has2(vmcs12,
10201 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10202 vmx_flush_tlb_ept_only(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010203 }
10204
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010205 /*
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010206 * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those
10207 * bits which we consider mandatory enabled.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010208 * The CR0_READ_SHADOW is what L2 should have expected to read given
10209 * the specifications by L1; It's not enough to take
10210 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
10211 * have more bits than L1 expected.
10212 */
10213 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
10214 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
10215
10216 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
10217 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
10218
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010219 if (from_vmentry &&
10220 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER))
David Matlack5a6a9742016-11-29 18:14:10 -080010221 vcpu->arch.efer = vmcs12->guest_ia32_efer;
10222 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
10223 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10224 else
10225 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10226 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
10227 vmx_set_efer(vcpu, vcpu->arch.efer);
10228
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010229 /* Shadow page tables on either EPT or shadow page tables. */
Ladi Prosek7ad658b2017-03-23 07:18:08 +010010230 if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12),
Ladi Prosek9ed38ffa2016-11-30 16:03:10 +010010231 entry_failure_code))
10232 return 1;
Ladi Prosek7ca29de2016-11-30 16:03:08 +010010233
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010234 if (!enable_ept)
10235 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
10236
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010237 /*
10238 * L1 may access the L2's PDPTR, so save them to construct vmcs12
10239 */
10240 if (enable_ept) {
10241 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
10242 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
10243 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
10244 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
10245 }
10246
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010247 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
10248 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
Ladi Prosekee146c12016-11-30 16:03:09 +010010249 return 0;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +030010250}
10251
Jim Mattsonca0bde22016-11-30 12:03:46 -080010252static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10253{
10254 struct vcpu_vmx *vmx = to_vmx(vcpu);
10255
10256 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
10257 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT)
10258 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10259
10260 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12))
10261 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10262
10263 if (nested_vmx_check_apicv_controls(vcpu, vmcs12))
10264 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10265
10266 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12))
10267 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10268
10269 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
10270 vmx->nested.nested_vmx_procbased_ctls_low,
10271 vmx->nested.nested_vmx_procbased_ctls_high) ||
10272 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
10273 vmx->nested.nested_vmx_secondary_ctls_low,
10274 vmx->nested.nested_vmx_secondary_ctls_high) ||
10275 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
10276 vmx->nested.nested_vmx_pinbased_ctls_low,
10277 vmx->nested.nested_vmx_pinbased_ctls_high) ||
10278 !vmx_control_verify(vmcs12->vm_exit_controls,
10279 vmx->nested.nested_vmx_exit_ctls_low,
10280 vmx->nested.nested_vmx_exit_ctls_high) ||
10281 !vmx_control_verify(vmcs12->vm_entry_controls,
10282 vmx->nested.nested_vmx_entry_ctls_low,
10283 vmx->nested.nested_vmx_entry_ctls_high))
10284 return VMXERR_ENTRY_INVALID_CONTROL_FIELD;
10285
10286 if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) ||
10287 !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) ||
10288 !nested_cr3_valid(vcpu, vmcs12->host_cr3))
10289 return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD;
10290
10291 return 0;
10292}
10293
10294static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10295 u32 *exit_qual)
10296{
10297 bool ia32e;
10298
10299 *exit_qual = ENTRY_FAIL_DEFAULT;
10300
10301 if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) ||
10302 !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4))
10303 return 1;
10304
10305 if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) &&
10306 vmcs12->vmcs_link_pointer != -1ull) {
10307 *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR;
10308 return 1;
10309 }
10310
10311 /*
10312 * If the load IA32_EFER VM-entry control is 1, the following checks
10313 * are performed on the field for the IA32_EFER MSR:
10314 * - Bits reserved in the IA32_EFER MSR must be 0.
10315 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
10316 * the IA-32e mode guest VM-exit control. It must also be identical
10317 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
10318 * CR0.PG) is 1.
10319 */
10320 if (to_vmx(vcpu)->nested.nested_run_pending &&
10321 (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) {
10322 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
10323 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
10324 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
10325 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
10326 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME)))
10327 return 1;
10328 }
10329
10330 /*
10331 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
10332 * IA32_EFER MSR must be 0 in the field for that register. In addition,
10333 * the values of the LMA and LME bits in the field must each be that of
10334 * the host address-space size VM-exit control.
10335 */
10336 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
10337 ia32e = (vmcs12->vm_exit_controls &
10338 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
10339 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
10340 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
10341 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME))
10342 return 1;
10343 }
10344
10345 return 0;
10346}
10347
Jim Mattson858e25c2016-11-30 12:03:47 -080010348static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry)
10349{
10350 struct vcpu_vmx *vmx = to_vmx(vcpu);
10351 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10352 struct loaded_vmcs *vmcs02;
Jim Mattson858e25c2016-11-30 12:03:47 -080010353 u32 msr_entry_idx;
10354 u32 exit_qual;
10355
10356 vmcs02 = nested_get_current_vmcs02(vmx);
10357 if (!vmcs02)
10358 return -ENOMEM;
10359
10360 enter_guest_mode(vcpu);
10361
10362 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
10363 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10364
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010365 vmx_switch_vmcs(vcpu, vmcs02);
Jim Mattson858e25c2016-11-30 12:03:47 -080010366 vmx_segment_cache_clear(vmx);
10367
10368 if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) {
10369 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010370 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010371 nested_vmx_entry_failure(vcpu, vmcs12,
10372 EXIT_REASON_INVALID_STATE, exit_qual);
10373 return 1;
10374 }
10375
10376 nested_get_vmcs12_pages(vcpu, vmcs12);
10377
10378 msr_entry_idx = nested_vmx_load_msr(vcpu,
10379 vmcs12->vm_entry_msr_load_addr,
10380 vmcs12->vm_entry_msr_load_count);
10381 if (msr_entry_idx) {
10382 leave_guest_mode(vcpu);
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010383 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Jim Mattson858e25c2016-11-30 12:03:47 -080010384 nested_vmx_entry_failure(vcpu, vmcs12,
10385 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
10386 return 1;
10387 }
10388
10389 vmcs12->launch_state = 1;
10390
10391 /*
10392 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
10393 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
10394 * returned as far as L1 is concerned. It will only return (and set
10395 * the success flag) when L2 exits (see nested_vmx_vmexit()).
10396 */
10397 return 0;
10398}
10399
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010400/*
10401 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
10402 * for running an L2 nested guest.
10403 */
10404static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
10405{
10406 struct vmcs12 *vmcs12;
10407 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jim Mattsonca0bde22016-11-30 12:03:46 -080010408 u32 exit_qual;
10409 int ret;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010410
Kyle Hueyeb277562016-11-29 12:40:39 -080010411 if (!nested_vmx_check_permission(vcpu))
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010412 return 1;
10413
Kyle Hueyeb277562016-11-29 12:40:39 -080010414 if (!nested_vmx_check_vmcs12(vcpu))
10415 goto out;
10416
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010417 vmcs12 = get_vmcs12(vcpu);
10418
Abel Gordon012f83c2013-04-18 14:39:25 +030010419 if (enable_shadow_vmcs)
10420 copy_shadow_to_vmcs12(vmx);
10421
Nadav Har'El7c177932011-05-25 23:12:04 +030010422 /*
10423 * The nested entry process starts with enforcing various prerequisites
10424 * on vmcs12 as required by the Intel SDM, and act appropriately when
10425 * they fail: As the SDM explains, some conditions should cause the
10426 * instruction to fail, while others will cause the instruction to seem
10427 * to succeed, but return an EXIT_REASON_INVALID_STATE.
10428 * To speed up the normal (success) code path, we should avoid checking
10429 * for misconfigurations which will anyway be caught by the processor
10430 * when using the merged vmcs02.
10431 */
10432 if (vmcs12->launch_state == launch) {
10433 nested_vmx_failValid(vcpu,
10434 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
10435 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
Kyle Hueyeb277562016-11-29 12:40:39 -080010436 goto out;
Nadav Har'El7c177932011-05-25 23:12:04 +030010437 }
10438
Jim Mattsonca0bde22016-11-30 12:03:46 -080010439 ret = check_vmentry_prereqs(vcpu, vmcs12);
10440 if (ret) {
10441 nested_vmx_failValid(vcpu, ret);
Kyle Hueyeb277562016-11-29 12:40:39 -080010442 goto out;
Paolo Bonzini26539bd2013-04-15 15:00:27 +020010443 }
10444
Nadav Har'El7c177932011-05-25 23:12:04 +030010445 /*
Jim Mattsonca0bde22016-11-30 12:03:46 -080010446 * After this point, the trap flag no longer triggers a singlestep trap
10447 * on the vm entry instructions; don't call kvm_skip_emulated_instruction.
10448 * This is not 100% correct; for performance reasons, we delegate most
10449 * of the checks on host state to the processor. If those fail,
10450 * the singlestep trap is missed.
Jan Kiszka384bb782013-04-20 10:52:36 +020010451 */
Jim Mattsonca0bde22016-11-30 12:03:46 -080010452 skip_emulated_instruction(vcpu);
Jan Kiszka384bb782013-04-20 10:52:36 +020010453
Jim Mattsonca0bde22016-11-30 12:03:46 -080010454 ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual);
10455 if (ret) {
10456 nested_vmx_entry_failure(vcpu, vmcs12,
10457 EXIT_REASON_INVALID_STATE, exit_qual);
10458 return 1;
Jan Kiszka384bb782013-04-20 10:52:36 +020010459 }
10460
10461 /*
Nadav Har'El7c177932011-05-25 23:12:04 +030010462 * We're finally done with prerequisite checking, and can start with
10463 * the nested entry.
10464 */
10465
Jim Mattson858e25c2016-11-30 12:03:47 -080010466 ret = enter_vmx_non_root_mode(vcpu, true);
10467 if (ret)
10468 return ret;
Wincy Vanff651cb2014-12-11 08:52:58 +030010469
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010470 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -060010471 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +010010472
Jan Kiszka7af40ad32014-01-04 18:47:23 +010010473 vmx->nested.nested_run_pending = 1;
10474
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010475 return 1;
Kyle Hueyeb277562016-11-29 12:40:39 -080010476
10477out:
Kyle Huey6affcbe2016-11-29 12:40:40 -080010478 return kvm_skip_emulated_instruction(vcpu);
Nadav Har'Elcd232ad2011-05-25 23:10:33 +030010479}
10480
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010481/*
10482 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
10483 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
10484 * This function returns the new value we should put in vmcs12.guest_cr0.
10485 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
10486 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
10487 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
10488 * didn't trap the bit, because if L1 did, so would L0).
10489 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
10490 * been modified by L2, and L1 knows it. So just leave the old value of
10491 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
10492 * isn't relevant, because if L0 traps this bit it can set it to anything.
10493 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
10494 * changed these bits, and therefore they need to be updated, but L0
10495 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
10496 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
10497 */
10498static inline unsigned long
10499vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10500{
10501 return
10502 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
10503 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
10504 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
10505 vcpu->arch.cr0_guest_owned_bits));
10506}
10507
10508static inline unsigned long
10509vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
10510{
10511 return
10512 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
10513 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
10514 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
10515 vcpu->arch.cr4_guest_owned_bits));
10516}
10517
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010518static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
10519 struct vmcs12 *vmcs12)
10520{
10521 u32 idt_vectoring;
10522 unsigned int nr;
10523
Gleb Natapov851eb6672013-09-25 12:51:34 +030010524 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010525 nr = vcpu->arch.exception.nr;
10526 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10527
10528 if (kvm_exception_is_soft(nr)) {
10529 vmcs12->vm_exit_instruction_len =
10530 vcpu->arch.event_exit_inst_len;
10531 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10532 } else
10533 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10534
10535 if (vcpu->arch.exception.has_error_code) {
10536 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10537 vmcs12->idt_vectoring_error_code =
10538 vcpu->arch.exception.error_code;
10539 }
10540
10541 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010542 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010543 vmcs12->idt_vectoring_info_field =
10544 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10545 } else if (vcpu->arch.interrupt.pending) {
10546 nr = vcpu->arch.interrupt.nr;
10547 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10548
10549 if (vcpu->arch.interrupt.soft) {
10550 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10551 vmcs12->vm_entry_instruction_len =
10552 vcpu->arch.event_exit_inst_len;
10553 } else
10554 idt_vectoring |= INTR_TYPE_EXT_INTR;
10555
10556 vmcs12->idt_vectoring_info_field = idt_vectoring;
10557 }
10558}
10559
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010560static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10561{
10562 struct vcpu_vmx *vmx = to_vmx(vcpu);
10563
Wanpeng Liacc9ab62017-02-27 04:24:39 -080010564 if (vcpu->arch.exception.pending ||
10565 vcpu->arch.nmi_injected ||
10566 vcpu->arch.interrupt.pending)
10567 return -EBUSY;
10568
Jan Kiszkaf41245002014-03-07 20:03:13 +010010569 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10570 vmx->nested.preemption_timer_expired) {
10571 if (vmx->nested.nested_run_pending)
10572 return -EBUSY;
10573 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10574 return 0;
10575 }
10576
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010577 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Wanpeng Liacc9ab62017-02-27 04:24:39 -080010578 if (vmx->nested.nested_run_pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010579 return -EBUSY;
10580 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10581 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10582 INTR_INFO_VALID_MASK, 0);
10583 /*
10584 * The NMI-triggered VM exit counts as injection:
10585 * clear this one and block further NMIs.
10586 */
10587 vcpu->arch.nmi_pending = 0;
10588 vmx_set_nmi_mask(vcpu, true);
10589 return 0;
10590 }
10591
10592 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10593 nested_exit_on_intr(vcpu)) {
10594 if (vmx->nested.nested_run_pending)
10595 return -EBUSY;
10596 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010597 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010598 }
10599
David Hildenbrand6342c502017-01-25 11:58:58 +010010600 vmx_complete_nested_posted_interrupt(vcpu);
10601 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010602}
10603
Jan Kiszkaf41245002014-03-07 20:03:13 +010010604static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10605{
10606 ktime_t remaining =
10607 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10608 u64 value;
10609
10610 if (ktime_to_ns(remaining) <= 0)
10611 return 0;
10612
10613 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10614 do_div(value, 1000000);
10615 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10616}
10617
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010618/*
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010619 * Update the guest state fields of vmcs12 to reflect changes that
10620 * occurred while L2 was running. (The "IA-32e mode guest" bit of the
10621 * VM-entry controls is also updated, since this is really a guest
10622 * state bit.)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010623 */
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010624static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010625{
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010626 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10627 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10628
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010629 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10630 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10631 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10632
10633 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10634 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10635 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10636 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10637 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10638 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10639 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10640 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10641 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10642 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10643 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10644 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10645 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10646 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10647 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10648 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10649 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10650 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10651 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10652 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10653 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10654 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10655 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10656 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10657 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10658 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10659 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10660 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10661 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10662 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10663 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10664 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10665 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10666 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10667 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10668 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10669
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010670 vmcs12->guest_interruptibility_info =
10671 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10672 vmcs12->guest_pending_dbg_exceptions =
10673 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010674 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10675 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10676 else
10677 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010678
Jan Kiszkaf41245002014-03-07 20:03:13 +010010679 if (nested_cpu_has_preemption_timer(vmcs12)) {
10680 if (vmcs12->vm_exit_controls &
10681 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10682 vmcs12->vmx_preemption_timer_value =
10683 vmx_get_preemption_timer_value(vcpu);
10684 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10685 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010686
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010687 /*
10688 * In some cases (usually, nested EPT), L2 is allowed to change its
10689 * own CR3 without exiting. If it has changed it, we must keep it.
10690 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10691 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10692 *
10693 * Additionally, restore L2's PDPTR to vmcs12.
10694 */
10695 if (enable_ept) {
Paolo Bonzinif3531052015-12-03 15:49:56 +010010696 vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3);
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010697 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10698 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10699 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10700 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10701 }
10702
Jan Dakinevich119a9c02016-09-04 21:22:47 +030010703 if (nested_cpu_has_ept(vmcs12))
10704 vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS);
10705
Wincy Van608406e2015-02-03 23:57:51 +080010706 if (nested_cpu_has_vid(vmcs12))
10707 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10708
Jan Kiszkac18911a2013-03-13 16:06:41 +010010709 vmcs12->vm_entry_controls =
10710 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010711 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010712
Jan Kiszka2996fca2014-06-16 13:59:43 +020010713 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10714 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10715 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10716 }
10717
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010718 /* TODO: These cannot have changed unless we have MSR bitmaps and
10719 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010720 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010721 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010722 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10723 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010724 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10725 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10726 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzinia87036a2016-03-08 09:52:13 +010010727 if (kvm_mpx_supported())
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010728 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010729 if (nested_cpu_has_xsaves(vmcs12))
10730 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Jim Mattsoncf8b84f2016-11-30 12:03:42 -080010731}
10732
10733/*
10734 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10735 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10736 * and this function updates it to reflect the changes to the guest state while
10737 * L2 was running (and perhaps made some exits which were handled directly by L0
10738 * without going back to L1), and to reflect the exit reason.
10739 * Note that we do not have to copy here all VMCS fields, just those that
10740 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10741 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10742 * which already writes to vmcs12 directly.
10743 */
10744static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10745 u32 exit_reason, u32 exit_intr_info,
10746 unsigned long exit_qualification)
10747{
10748 /* update guest state fields: */
10749 sync_vmcs12(vcpu, vmcs12);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010750
10751 /* update exit information fields: */
10752
Jan Kiszka533558b2014-01-04 18:47:20 +010010753 vmcs12->vm_exit_reason = exit_reason;
10754 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010755
Jan Kiszka533558b2014-01-04 18:47:20 +010010756 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020010757 if ((vmcs12->vm_exit_intr_info &
10758 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10759 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10760 vmcs12->vm_exit_intr_error_code =
10761 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010762 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010763 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10764 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10765
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010766 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10767 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10768 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010769 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010770
10771 /*
10772 * Transfer the event that L0 or L1 may wanted to inject into
10773 * L2 to IDT_VECTORING_INFO_FIELD.
10774 */
10775 vmcs12_save_pending_event(vcpu, vmcs12);
10776 }
10777
10778 /*
10779 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10780 * preserved above and would only end up incorrectly in L1.
10781 */
10782 vcpu->arch.nmi_injected = false;
10783 kvm_clear_exception_queue(vcpu);
10784 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010785}
10786
10787/*
10788 * A part of what we need to when the nested L2 guest exits and we want to
10789 * run its L1 parent, is to reset L1's guest state to the host state specified
10790 * in vmcs12.
10791 * This function is to be called not only on normal nested exit, but also on
10792 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10793 * Failures During or After Loading Guest State").
10794 * This function should be called when the active VMCS is L1's (vmcs01).
10795 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010796static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10797 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010798{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010799 struct kvm_segment seg;
Jim Mattsonca0bde22016-11-30 12:03:46 -080010800 u32 entry_failure_code;
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010801
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010802 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10803 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010804 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010805 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10806 else
10807 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10808 vmx_set_efer(vcpu, vcpu->arch.efer);
10809
10810 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10811 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010812 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010813 /*
10814 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010815 * actually changed, because vmx_set_cr0 refers to efer set above.
10816 *
10817 * CR0_GUEST_HOST_MASK is already set in the original vmcs01
10818 * (KVM doesn't change it);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010819 */
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010820 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020010821 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010822
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -080010823 /* Same as above - no reason to call set_cr4_guest_host_mask(). */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010824 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
10825 kvm_set_cr4(vcpu, vmcs12->host_cr4);
10826
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010827 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010828
Ladi Prosek1dc35da2016-11-30 16:03:11 +010010829 /*
10830 * Only PDPTE load can fail as the value of cr3 was checked on entry and
10831 * couldn't have changed.
10832 */
10833 if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code))
10834 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010835
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010836 if (!enable_ept)
10837 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10838
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010839 if (enable_vpid) {
10840 /*
10841 * Trivially support vpid by letting L2s share their parent
10842 * L1's vpid. TODO: move to a more elaborate solution, giving
10843 * each L2 its own vpid and exposing the vpid feature to L1.
10844 */
10845 vmx_flush_tlb(vcpu);
10846 }
10847
10848
10849 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10850 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10851 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10852 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10853 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010854
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010855 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10856 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10857 vmcs_write64(GUEST_BNDCFGS, 0);
10858
Jan Kiszka44811c02013-08-04 17:17:27 +020010859 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010860 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010861 vcpu->arch.pat = vmcs12->host_ia32_pat;
10862 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010863 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10864 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10865 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010866
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010867 /* Set L1 segment info according to Intel SDM
10868 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10869 seg = (struct kvm_segment) {
10870 .base = 0,
10871 .limit = 0xFFFFFFFF,
10872 .selector = vmcs12->host_cs_selector,
10873 .type = 11,
10874 .present = 1,
10875 .s = 1,
10876 .g = 1
10877 };
10878 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10879 seg.l = 1;
10880 else
10881 seg.db = 1;
10882 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
10883 seg = (struct kvm_segment) {
10884 .base = 0,
10885 .limit = 0xFFFFFFFF,
10886 .type = 3,
10887 .present = 1,
10888 .s = 1,
10889 .db = 1,
10890 .g = 1
10891 };
10892 seg.selector = vmcs12->host_ds_selector;
10893 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
10894 seg.selector = vmcs12->host_es_selector;
10895 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
10896 seg.selector = vmcs12->host_ss_selector;
10897 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
10898 seg.selector = vmcs12->host_fs_selector;
10899 seg.base = vmcs12->host_fs_base;
10900 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
10901 seg.selector = vmcs12->host_gs_selector;
10902 seg.base = vmcs12->host_gs_base;
10903 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
10904 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030010905 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010906 .limit = 0x67,
10907 .selector = vmcs12->host_tr_selector,
10908 .type = 11,
10909 .present = 1
10910 };
10911 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
10912
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010913 kvm_set_dr(vcpu, 7, 0x400);
10914 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030010915
Wincy Van3af18d92015-02-03 23:49:31 +080010916 if (cpu_has_vmx_msr_bitmap())
10917 vmx_set_msr_bitmap(vcpu);
10918
Wincy Vanff651cb2014-12-11 08:52:58 +030010919 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
10920 vmcs12->vm_exit_msr_load_count))
10921 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010922}
10923
10924/*
10925 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
10926 * and modify vmcs12 to make it see what it would expect to see there if
10927 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
10928 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010929static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
10930 u32 exit_intr_info,
10931 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010932{
10933 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010934 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jim Mattsoncf3215d2016-09-06 09:33:21 -070010935 u32 vm_inst_error = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010936
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010937 /* trying to cancel vmlaunch/vmresume is a bug */
10938 WARN_ON_ONCE(vmx->nested.nested_run_pending);
10939
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010940 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010941 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
10942 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010943
Wincy Vanff651cb2014-12-11 08:52:58 +030010944 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
10945 vmcs12->vm_exit_msr_store_count))
10946 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
10947
Jim Mattsoncf3215d2016-09-06 09:33:21 -070010948 if (unlikely(vmx->fail))
10949 vm_inst_error = vmcs_read32(VM_INSTRUCTION_ERROR);
10950
David Hildenbrand1279a6b12017-03-20 10:00:08 +010010951 vmx_switch_vmcs(vcpu, &vmx->vmcs01);
Wanpeng Lif3380ca52014-08-05 12:42:23 +080010952
Bandan Das77b0f5d2014-04-19 18:17:45 -040010953 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
10954 && nested_exit_intr_ack_set(vcpu)) {
10955 int irq = kvm_cpu_get_interrupt(vcpu);
10956 WARN_ON(irq < 0);
10957 vmcs12->vm_exit_intr_info = irq |
10958 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
10959 }
10960
Jan Kiszka542060e2014-01-04 18:47:21 +010010961 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
10962 vmcs12->exit_qualification,
10963 vmcs12->idt_vectoring_info_field,
10964 vmcs12->vm_exit_intr_info,
10965 vmcs12->vm_exit_intr_error_code,
10966 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010967
Paolo Bonzini8391ce42016-07-07 14:58:33 +020010968 vm_entry_controls_reset_shadow(vmx);
10969 vm_exit_controls_reset_shadow(vmx);
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010970 vmx_segment_cache_clear(vmx);
10971
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010972 /* if no vmcs02 cache requested, remove the one we used */
10973 if (VMCS02_POOL_SIZE == 0)
10974 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
10975
10976 load_vmcs12_host_state(vcpu, vmcs12);
10977
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010978 /* Update any VMCS fields that might have changed while L2 ran */
Jim Mattson83bafef2016-10-04 10:48:38 -070010979 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
10980 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr);
Paolo Bonziniea26e4e2016-11-01 00:39:48 +010010981 vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset);
Paolo Bonzini9314006db2016-07-06 13:23:51 +020010982 if (vmx->hv_deadline_tsc == -1)
10983 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
10984 PIN_BASED_VMX_PREEMPTION_TIMER);
10985 else
10986 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
10987 PIN_BASED_VMX_PREEMPTION_TIMER);
Peter Feinerc95ba922016-08-17 09:36:47 -070010988 if (kvm_has_tsc_control)
10989 decache_tsc_multiplier(vmx);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010990
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010991 if (vmx->nested.change_vmcs01_virtual_x2apic_mode) {
10992 vmx->nested.change_vmcs01_virtual_x2apic_mode = false;
10993 vmx_set_virtual_x2apic_mode(vcpu,
10994 vcpu->arch.apic_base & X2APIC_ENABLE);
Jim Mattsonfb6c8192017-03-16 13:53:59 -070010995 } else if (!nested_cpu_has_ept(vmcs12) &&
10996 nested_cpu_has2(vmcs12,
10997 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
10998 vmx_flush_tlb_ept_only(vcpu);
Radim Krčmářdccbfcf2016-08-08 20:16:23 +020010999 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011000
11001 /* This is needed for same reason as it was needed in prepare_vmcs02 */
11002 vmx->host_rsp = 0;
11003
11004 /* Unpin physical memory we referred to in vmcs02 */
11005 if (vmx->nested.apic_access_page) {
11006 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011007 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011008 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011009 if (vmx->nested.virtual_apic_page) {
11010 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011011 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080011012 }
Wincy Van705699a2015-02-03 23:58:17 +080011013 if (vmx->nested.pi_desc_page) {
11014 kunmap(vmx->nested.pi_desc_page);
11015 nested_release_page(vmx->nested.pi_desc_page);
11016 vmx->nested.pi_desc_page = NULL;
11017 vmx->nested.pi_desc = NULL;
11018 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011019
11020 /*
Tang Chen38b99172014-09-24 15:57:54 +080011021 * We are now running in L2, mmu_notifier will force to reload the
11022 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
11023 */
Wanpeng Lic83b6d12016-09-06 17:20:33 +080011024 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Tang Chen38b99172014-09-24 15:57:54 +080011025
11026 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011027 * Exiting from L2 to L1, we're now back to L1 which thinks it just
11028 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
11029 * success or failure flag accordingly.
11030 */
11031 if (unlikely(vmx->fail)) {
11032 vmx->fail = 0;
Jim Mattsoncf3215d2016-09-06 09:33:21 -070011033 nested_vmx_failValid(vcpu, vm_inst_error);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011034 } else
11035 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011036 if (enable_shadow_vmcs)
11037 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011038
11039 /* in case we halted in L2 */
11040 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030011041}
11042
Nadav Har'El7c177932011-05-25 23:12:04 +030011043/*
Jan Kiszka42124922014-01-04 18:47:19 +010011044 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
11045 */
11046static void vmx_leave_nested(struct kvm_vcpu *vcpu)
11047{
Wanpeng Li2f707d92017-03-06 04:03:28 -080011048 if (is_guest_mode(vcpu)) {
11049 to_vmx(vcpu)->nested.nested_run_pending = 0;
Jan Kiszka533558b2014-01-04 18:47:20 +010011050 nested_vmx_vmexit(vcpu, -1, 0, 0);
Wanpeng Li2f707d92017-03-06 04:03:28 -080011051 }
Jan Kiszka42124922014-01-04 18:47:19 +010011052 free_nested(to_vmx(vcpu));
11053}
11054
11055/*
Nadav Har'El7c177932011-05-25 23:12:04 +030011056 * L1's failure to enter L2 is a subset of a normal exit, as explained in
11057 * 23.7 "VM-entry failures during or after loading guest state" (this also
11058 * lists the acceptable exit-reason and exit-qualification parameters).
11059 * It should only be called before L2 actually succeeded to run, and when
11060 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
11061 */
11062static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
11063 struct vmcs12 *vmcs12,
11064 u32 reason, unsigned long qualification)
11065{
11066 load_vmcs12_host_state(vcpu, vmcs12);
11067 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
11068 vmcs12->exit_qualification = qualification;
11069 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030011070 if (enable_shadow_vmcs)
11071 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030011072}
11073
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011074static int vmx_check_intercept(struct kvm_vcpu *vcpu,
11075 struct x86_instruction_info *info,
11076 enum x86_intercept_stage stage)
11077{
11078 return X86EMUL_CONTINUE;
11079}
11080
Yunhong Jiang64672c92016-06-13 14:19:59 -070011081#ifdef CONFIG_X86_64
11082/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
11083static inline int u64_shl_div_u64(u64 a, unsigned int shift,
11084 u64 divisor, u64 *result)
11085{
11086 u64 low = a << shift, high = a >> (64 - shift);
11087
11088 /* To avoid the overflow on divq */
11089 if (high >= divisor)
11090 return 1;
11091
11092 /* Low hold the result, high hold rem which is discarded */
11093 asm("divq %2\n\t" : "=a" (low), "=d" (high) :
11094 "rm" (divisor), "0" (low), "1" (high));
11095 *result = low;
11096
11097 return 0;
11098}
11099
11100static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
11101{
11102 struct vcpu_vmx *vmx = to_vmx(vcpu);
Paolo Bonzini9175d2e2016-06-27 15:08:01 +020011103 u64 tscl = rdtsc();
11104 u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
11105 u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
Yunhong Jiang64672c92016-06-13 14:19:59 -070011106
11107 /* Convert to host delta tsc if tsc scaling is enabled */
11108 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
11109 u64_shl_div_u64(delta_tsc,
11110 kvm_tsc_scaling_ratio_frac_bits,
11111 vcpu->arch.tsc_scaling_ratio,
11112 &delta_tsc))
11113 return -ERANGE;
11114
11115 /*
11116 * If the delta tsc can't fit in the 32 bit after the multi shift,
11117 * we can't use the preemption timer.
11118 * It's possible that it fits on later vmentries, but checking
11119 * on every vmentry is costly so we just use an hrtimer.
11120 */
11121 if (delta_tsc >> (cpu_preemption_timer_multi + 32))
11122 return -ERANGE;
11123
11124 vmx->hv_deadline_tsc = tscl + delta_tsc;
11125 vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
11126 PIN_BASED_VMX_PREEMPTION_TIMER);
11127 return 0;
11128}
11129
11130static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
11131{
11132 struct vcpu_vmx *vmx = to_vmx(vcpu);
11133 vmx->hv_deadline_tsc = -1;
11134 vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL,
11135 PIN_BASED_VMX_PREEMPTION_TIMER);
11136}
11137#endif
11138
Paolo Bonzini48d89b92014-08-26 13:27:46 +020011139static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011140{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020011141 if (ple_gap)
11142 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011143}
11144
Kai Huang843e4332015-01-28 10:54:28 +080011145static void vmx_slot_enable_log_dirty(struct kvm *kvm,
11146 struct kvm_memory_slot *slot)
11147{
11148 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
11149 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
11150}
11151
11152static void vmx_slot_disable_log_dirty(struct kvm *kvm,
11153 struct kvm_memory_slot *slot)
11154{
11155 kvm_mmu_slot_set_dirty(kvm, slot);
11156}
11157
11158static void vmx_flush_log_dirty(struct kvm *kvm)
11159{
11160 kvm_flush_pml_buffers(kvm);
11161}
11162
11163static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
11164 struct kvm_memory_slot *memslot,
11165 gfn_t offset, unsigned long mask)
11166{
11167 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
11168}
11169
Feng Wuefc64402015-09-18 22:29:51 +080011170/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080011171 * This routine does the following things for vCPU which is going
11172 * to be blocked if VT-d PI is enabled.
11173 * - Store the vCPU to the wakeup list, so when interrupts happen
11174 * we can find the right vCPU to wake up.
11175 * - Change the Posted-interrupt descriptor as below:
11176 * 'NDST' <-- vcpu->pre_pcpu
11177 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
11178 * - If 'ON' is set during this process, which means at least one
11179 * interrupt is posted for this vCPU, we cannot block it, in
11180 * this case, return 1, otherwise, return 0.
11181 *
11182 */
Yunhong Jiangbc225122016-06-13 14:19:58 -070011183static int pi_pre_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011184{
11185 unsigned long flags;
11186 unsigned int dest;
11187 struct pi_desc old, new;
11188 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11189
11190 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011191 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11192 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011193 return 0;
11194
11195 vcpu->pre_pcpu = vcpu->cpu;
11196 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
11197 vcpu->pre_pcpu), flags);
11198 list_add_tail(&vcpu->blocked_vcpu_list,
11199 &per_cpu(blocked_vcpu_on_cpu,
11200 vcpu->pre_pcpu));
11201 spin_unlock_irqrestore(&per_cpu(blocked_vcpu_on_cpu_lock,
11202 vcpu->pre_pcpu), flags);
11203
11204 do {
11205 old.control = new.control = pi_desc->control;
11206
11207 /*
11208 * We should not block the vCPU if
11209 * an interrupt is posted for it.
11210 */
11211 if (pi_test_on(pi_desc) == 1) {
11212 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
11213 vcpu->pre_pcpu), flags);
11214 list_del(&vcpu->blocked_vcpu_list);
11215 spin_unlock_irqrestore(
11216 &per_cpu(blocked_vcpu_on_cpu_lock,
11217 vcpu->pre_pcpu), flags);
11218 vcpu->pre_pcpu = -1;
11219
11220 return 1;
11221 }
11222
11223 WARN((pi_desc->sn == 1),
11224 "Warning: SN field of posted-interrupts "
11225 "is set before blocking\n");
11226
11227 /*
11228 * Since vCPU can be preempted during this process,
11229 * vcpu->cpu could be different with pre_pcpu, we
11230 * need to set pre_pcpu as the destination of wakeup
11231 * notification event, then we can find the right vCPU
11232 * to wakeup in wakeup handler if interrupts happen
11233 * when the vCPU is in blocked state.
11234 */
11235 dest = cpu_physical_id(vcpu->pre_pcpu);
11236
11237 if (x2apic_enabled())
11238 new.ndst = dest;
11239 else
11240 new.ndst = (dest << 8) & 0xFF00;
11241
11242 /* set 'NV' to 'wakeup vector' */
11243 new.nv = POSTED_INTR_WAKEUP_VECTOR;
11244 } while (cmpxchg(&pi_desc->control, old.control,
11245 new.control) != old.control);
11246
11247 return 0;
11248}
11249
Yunhong Jiangbc225122016-06-13 14:19:58 -070011250static int vmx_pre_block(struct kvm_vcpu *vcpu)
11251{
11252 if (pi_pre_block(vcpu))
11253 return 1;
11254
Yunhong Jiang64672c92016-06-13 14:19:59 -070011255 if (kvm_lapic_hv_timer_in_use(vcpu))
11256 kvm_lapic_switch_to_sw_timer(vcpu);
11257
Yunhong Jiangbc225122016-06-13 14:19:58 -070011258 return 0;
11259}
11260
11261static void pi_post_block(struct kvm_vcpu *vcpu)
Feng Wubf9f6ac2015-09-18 22:29:55 +080011262{
11263 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
11264 struct pi_desc old, new;
11265 unsigned int dest;
11266 unsigned long flags;
11267
11268 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011269 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11270 !kvm_vcpu_apicv_active(vcpu))
Feng Wubf9f6ac2015-09-18 22:29:55 +080011271 return;
11272
11273 do {
11274 old.control = new.control = pi_desc->control;
11275
11276 dest = cpu_physical_id(vcpu->cpu);
11277
11278 if (x2apic_enabled())
11279 new.ndst = dest;
11280 else
11281 new.ndst = (dest << 8) & 0xFF00;
11282
11283 /* Allow posting non-urgent interrupts */
11284 new.sn = 0;
11285
11286 /* set 'NV' to 'notification vector' */
11287 new.nv = POSTED_INTR_VECTOR;
11288 } while (cmpxchg(&pi_desc->control, old.control,
11289 new.control) != old.control);
11290
11291 if(vcpu->pre_pcpu != -1) {
11292 spin_lock_irqsave(
11293 &per_cpu(blocked_vcpu_on_cpu_lock,
11294 vcpu->pre_pcpu), flags);
11295 list_del(&vcpu->blocked_vcpu_list);
11296 spin_unlock_irqrestore(
11297 &per_cpu(blocked_vcpu_on_cpu_lock,
11298 vcpu->pre_pcpu), flags);
11299 vcpu->pre_pcpu = -1;
11300 }
11301}
11302
Yunhong Jiangbc225122016-06-13 14:19:58 -070011303static void vmx_post_block(struct kvm_vcpu *vcpu)
11304{
Yunhong Jiang64672c92016-06-13 14:19:59 -070011305 if (kvm_x86_ops->set_hv_timer)
11306 kvm_lapic_switch_to_hv_timer(vcpu);
11307
Yunhong Jiangbc225122016-06-13 14:19:58 -070011308 pi_post_block(vcpu);
11309}
11310
Feng Wubf9f6ac2015-09-18 22:29:55 +080011311/*
Feng Wuefc64402015-09-18 22:29:51 +080011312 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
11313 *
11314 * @kvm: kvm
11315 * @host_irq: host irq of the interrupt
11316 * @guest_irq: gsi of the interrupt
11317 * @set: set or unset PI
11318 * returns 0 on success, < 0 on failure
11319 */
11320static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
11321 uint32_t guest_irq, bool set)
11322{
11323 struct kvm_kernel_irq_routing_entry *e;
11324 struct kvm_irq_routing_table *irq_rt;
11325 struct kvm_lapic_irq irq;
11326 struct kvm_vcpu *vcpu;
11327 struct vcpu_data vcpu_info;
11328 int idx, ret = -EINVAL;
11329
11330 if (!kvm_arch_has_assigned_device(kvm) ||
Yang Zhanga0052192016-06-13 09:56:56 +080011331 !irq_remapping_cap(IRQ_POSTING_CAP) ||
11332 !kvm_vcpu_apicv_active(kvm->vcpus[0]))
Feng Wuefc64402015-09-18 22:29:51 +080011333 return 0;
11334
11335 idx = srcu_read_lock(&kvm->irq_srcu);
11336 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
11337 BUG_ON(guest_irq >= irq_rt->nr_rt_entries);
11338
11339 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
11340 if (e->type != KVM_IRQ_ROUTING_MSI)
11341 continue;
11342 /*
11343 * VT-d PI cannot support posting multicast/broadcast
11344 * interrupts to a vCPU, we still use interrupt remapping
11345 * for these kind of interrupts.
11346 *
11347 * For lowest-priority interrupts, we only support
11348 * those with single CPU as the destination, e.g. user
11349 * configures the interrupts via /proc/irq or uses
11350 * irqbalance to make the interrupts single-CPU.
11351 *
11352 * We will support full lowest-priority interrupt later.
11353 */
11354
Radim Krčmář371313132016-07-12 22:09:27 +020011355 kvm_set_msi_irq(kvm, e, &irq);
Feng Wu23a1c252016-01-25 16:53:32 +080011356 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) {
11357 /*
11358 * Make sure the IRTE is in remapped mode if
11359 * we don't handle it in posted mode.
11360 */
11361 ret = irq_set_vcpu_affinity(host_irq, NULL);
11362 if (ret < 0) {
11363 printk(KERN_INFO
11364 "failed to back to remapped mode, irq: %u\n",
11365 host_irq);
11366 goto out;
11367 }
11368
Feng Wuefc64402015-09-18 22:29:51 +080011369 continue;
Feng Wu23a1c252016-01-25 16:53:32 +080011370 }
Feng Wuefc64402015-09-18 22:29:51 +080011371
11372 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
11373 vcpu_info.vector = irq.vector;
11374
Feng Wub6ce9782016-01-25 16:53:35 +080011375 trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi,
Feng Wuefc64402015-09-18 22:29:51 +080011376 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
11377
11378 if (set)
11379 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
11380 else {
11381 /* suppress notification event before unposting */
11382 pi_set_sn(vcpu_to_pi_desc(vcpu));
11383 ret = irq_set_vcpu_affinity(host_irq, NULL);
11384 pi_clear_sn(vcpu_to_pi_desc(vcpu));
11385 }
11386
11387 if (ret < 0) {
11388 printk(KERN_INFO "%s: failed to update PI IRTE\n",
11389 __func__);
11390 goto out;
11391 }
11392 }
11393
11394 ret = 0;
11395out:
11396 srcu_read_unlock(&kvm->irq_srcu, idx);
11397 return ret;
11398}
11399
Ashok Rajc45dcc72016-06-22 14:59:56 +080011400static void vmx_setup_mce(struct kvm_vcpu *vcpu)
11401{
11402 if (vcpu->arch.mcg_cap & MCG_LMCE_P)
11403 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
11404 FEATURE_CONTROL_LMCE;
11405 else
11406 to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
11407 ~FEATURE_CONTROL_LMCE;
11408}
11409
Kees Cook404f6aa2016-08-08 16:29:06 -070011410static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080011411 .cpu_has_kvm_support = cpu_has_kvm_support,
11412 .disabled_by_bios = vmx_disabled_by_bios,
11413 .hardware_setup = hardware_setup,
11414 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030011415 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011416 .hardware_enable = hardware_enable,
11417 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080011418 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020011419 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011420
11421 .vcpu_create = vmx_create_vcpu,
11422 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030011423 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011424
Avi Kivity04d2cc72007-09-10 18:10:54 +030011425 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011426 .vcpu_load = vmx_vcpu_load,
11427 .vcpu_put = vmx_vcpu_put,
11428
Paolo Bonzinia96036b2015-11-10 11:55:36 +010011429 .update_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011430 .get_msr = vmx_get_msr,
11431 .set_msr = vmx_set_msr,
11432 .get_segment_base = vmx_get_segment_base,
11433 .get_segment = vmx_get_segment,
11434 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020011435 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011436 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020011437 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020011438 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030011439 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011440 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011441 .set_cr3 = vmx_set_cr3,
11442 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011443 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011444 .get_idt = vmx_get_idt,
11445 .set_idt = vmx_set_idt,
11446 .get_gdt = vmx_get_gdt,
11447 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010011448 .get_dr6 = vmx_get_dr6,
11449 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030011450 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010011451 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030011452 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011453 .get_rflags = vmx_get_rflags,
11454 .set_rflags = vmx_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +080011455
11456 .get_pkru = vmx_get_pkru,
11457
Avi Kivity6aa8b732006-12-10 02:21:36 -080011458 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011459
Avi Kivity6aa8b732006-12-10 02:21:36 -080011460 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020011461 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011462 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040011463 .set_interrupt_shadow = vmx_set_interrupt_shadow,
11464 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020011465 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030011466 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011467 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020011468 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030011469 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020011470 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011471 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010011472 .get_nmi_mask = vmx_get_nmi_mask,
11473 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011474 .enable_nmi_window = enable_nmi_window,
11475 .enable_irq_window = enable_irq_window,
11476 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080011477 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080011478 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Andrey Smetanind62caab2015-11-10 15:36:33 +030011479 .get_enable_apicv = vmx_get_enable_apicv,
11480 .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011481 .load_eoi_exitmap = vmx_load_eoi_exitmap,
Paolo Bonzini967235d2016-12-19 14:03:45 +010011482 .apicv_post_state_restore = vmx_apicv_post_state_restore,
Yang Zhangc7c9c562013-01-25 10:18:51 +080011483 .hwapic_irr_update = vmx_hwapic_irr_update,
11484 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080011485 .sync_pir_to_irr = vmx_sync_pir_to_irr,
11486 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030011487
Izik Eiduscbc94022007-10-25 00:29:55 +020011488 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080011489 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080011490 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030011491
Avi Kivity586f9602010-11-18 13:09:54 +020011492 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020011493
Sheng Yang17cc3932010-01-05 19:02:27 +080011494 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080011495
11496 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080011497
11498 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000011499 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020011500
11501 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080011502
11503 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100011504
11505 .write_tsc_offset = vmx_write_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020011506
11507 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020011508
11509 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080011510 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000011511 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080011512 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010011513
11514 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020011515
11516 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080011517
11518 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
11519 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
11520 .flush_log_dirty = vmx_flush_log_dirty,
11521 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020011522
Feng Wubf9f6ac2015-09-18 22:29:55 +080011523 .pre_block = vmx_pre_block,
11524 .post_block = vmx_post_block,
11525
Wei Huang25462f72015-06-19 15:45:05 +020011526 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080011527
11528 .update_pi_irte = vmx_update_pi_irte,
Yunhong Jiang64672c92016-06-13 14:19:59 -070011529
11530#ifdef CONFIG_X86_64
11531 .set_hv_timer = vmx_set_hv_timer,
11532 .cancel_hv_timer = vmx_cancel_hv_timer,
11533#endif
Ashok Rajc45dcc72016-06-22 14:59:56 +080011534
11535 .setup_mce = vmx_setup_mce,
Avi Kivity6aa8b732006-12-10 02:21:36 -080011536};
11537
11538static int __init vmx_init(void)
11539{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011540 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
11541 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030011542 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080011543 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080011544
Dave Young2965faa2015-09-09 15:38:55 -070011545#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011546 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
11547 crash_vmclear_local_loaded_vmcss);
11548#endif
11549
He, Qingfdef3ad2007-04-30 09:45:24 +030011550 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080011551}
11552
11553static void __exit vmx_exit(void)
11554{
Dave Young2965faa2015-09-09 15:38:55 -070011555#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053011556 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080011557 synchronize_rcu();
11558#endif
11559
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080011560 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080011561}
11562
11563module_init(vmx_init)
11564module_exit(vmx_exit)