blob: c6016e4a5af0042c70aa5f27d8473e0a42873429 [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"
Avi Kivitye4956062007-06-28 14:15:57 -040022
Avi Kivityedf88412007-12-16 11:02:48 +020023#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020025#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/mm.h>
27#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040028#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020029#include <linux/moduleparam.h>
Josh Triplette9bda3b2012-03-20 23:33:51 -070030#include <linux/mod_devicetable.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040031#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040033#include <linux/tboot.h>
Jan Kiszkaf41245002014-03-07 20:03:13 +010034#include <linux/hrtimer.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030035#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030036#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040037
Feng Wu28b835d2015-09-18 22:29:54 +080038#include <asm/cpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080039#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080040#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020041#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020042#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080043#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020044#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020045#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010046#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080047#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010048#include <asm/apic.h>
Feng Wuefc64402015-09-18 22:29:51 +080049#include <asm/irq_remapping.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080050
Marcelo Tosatti229456f2009-06-17 09:22:14 -030051#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020052#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030053
Avi Kivity4ecac3f2008-05-13 13:23:38 +030054#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040055#define __ex_clear(x, reg) \
56 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030057
Avi Kivity6aa8b732006-12-10 02:21:36 -080058MODULE_AUTHOR("Qumranet");
59MODULE_LICENSE("GPL");
60
Josh Triplette9bda3b2012-03-20 23:33:51 -070061static const struct x86_cpu_id vmx_cpu_id[] = {
62 X86_FEATURE_MATCH(X86_FEATURE_VMX),
63 {}
64};
65MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
66
Rusty Russell476bc002012-01-13 09:32:18 +103067static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020068module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080069
Rusty Russell476bc002012-01-13 09:32:18 +103070static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020071module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020072
Rusty Russell476bc002012-01-13 09:32:18 +103073static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020074module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080075
Rusty Russell476bc002012-01-13 09:32:18 +103076static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070077module_param_named(unrestricted_guest,
78 enable_unrestricted_guest, bool, S_IRUGO);
79
Xudong Hao83c3a332012-05-28 19:33:35 +080080static bool __read_mostly enable_ept_ad_bits = 1;
81module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
82
Avi Kivitya27685c2012-06-12 20:30:18 +030083static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020084module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030085
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080087module_param(vmm_exclusive, bool, S_IRUGO);
88
Rusty Russell476bc002012-01-13 09:32:18 +103089static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030090module_param(fasteoi, bool, S_IRUGO);
91
Yang Zhang5a717852013-04-11 19:25:16 +080092static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080093module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080094
Abel Gordonabc4fc52013-04-18 14:35:25 +030095static bool __read_mostly enable_shadow_vmcs = 1;
96module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +030097/*
98 * If nested=1, nested virtualization is supported, i.e., guests may use
99 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
100 * use VMX instructions.
101 */
Rusty Russell476bc002012-01-13 09:32:18 +1030102static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300103module_param(nested, bool, S_IRUGO);
104
Wanpeng Li20300092014-12-02 19:14:59 +0800105static u64 __read_mostly host_xss;
106
Kai Huang843e4332015-01-28 10:54:28 +0800107static bool __read_mostly enable_pml = 1;
108module_param_named(pml, enable_pml, bool, S_IRUGO);
109
Haozhong Zhang64903d62015-10-20 15:39:09 +0800110#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
111
Gleb Natapov50378782013-02-04 16:00:28 +0200112#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
113#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200114#define KVM_VM_CR0_ALWAYS_ON \
115 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200116#define KVM_CR4_GUEST_OWNED_BITS \
117 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700118 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200119
Avi Kivitycdc0e242009-12-06 17:21:14 +0200120#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
121#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
122
Avi Kivity78ac8b42010-04-08 18:19:35 +0300123#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
124
Jan Kiszkaf41245002014-03-07 20:03:13 +0100125#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
126
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800127/*
128 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
129 * ple_gap: upper bound on the amount of time between two successive
130 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500131 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800132 * ple_window: upper bound on the amount of time a guest is allowed to execute
133 * in a PAUSE loop. Tests indicate that most spinlocks are held for
134 * less than 2^12 cycles
135 * Time is measured based on a counter that runs at the same rate as the TSC,
136 * refer SDM volume 3b section 21.6.13 & 22.1.3.
137 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200138#define KVM_VMX_DEFAULT_PLE_GAP 128
139#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
140#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
141#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
142#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
143 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
144
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800145static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
146module_param(ple_gap, int, S_IRUGO);
147
148static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
149module_param(ple_window, int, S_IRUGO);
150
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200151/* Default doubles per-vcpu window every exit. */
152static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
153module_param(ple_window_grow, int, S_IRUGO);
154
155/* Default resets per-vcpu window every exit to ple_window. */
156static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
157module_param(ple_window_shrink, int, S_IRUGO);
158
159/* Default is to compute the maximum so we can never overflow. */
160static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
161static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
162module_param(ple_window_max, int, S_IRUGO);
163
Avi Kivity83287ea422012-09-16 15:10:57 +0300164extern const ulong vmx_return;
165
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200166#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300167#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300168
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400169struct vmcs {
170 u32 revision_id;
171 u32 abort;
172 char data[0];
173};
174
Nadav Har'Eld462b812011-05-24 15:26:10 +0300175/*
176 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
177 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
178 * loaded on this CPU (so we can clear them if the CPU goes down).
179 */
180struct loaded_vmcs {
181 struct vmcs *vmcs;
182 int cpu;
183 int launched;
184 struct list_head loaded_vmcss_on_cpu_link;
185};
186
Avi Kivity26bb0982009-09-07 11:14:12 +0300187struct shared_msr_entry {
188 unsigned index;
189 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200190 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300191};
192
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300193/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300194 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
195 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
196 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
197 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
198 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
199 * More than one of these structures may exist, if L1 runs multiple L2 guests.
200 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
201 * underlying hardware which will be used to run L2.
202 * This structure is packed to ensure that its layout is identical across
203 * machines (necessary for live migration).
204 * If there are changes in this struct, VMCS12_REVISION must be changed.
205 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300206typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300207struct __packed vmcs12 {
208 /* According to the Intel spec, a VMCS region must start with the
209 * following two fields. Then follow implementation-specific data.
210 */
211 u32 revision_id;
212 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300213
Nadav Har'El27d6c862011-05-25 23:06:59 +0300214 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
215 u32 padding[7]; /* room for future expansion */
216
Nadav Har'El22bd0352011-05-25 23:05:57 +0300217 u64 io_bitmap_a;
218 u64 io_bitmap_b;
219 u64 msr_bitmap;
220 u64 vm_exit_msr_store_addr;
221 u64 vm_exit_msr_load_addr;
222 u64 vm_entry_msr_load_addr;
223 u64 tsc_offset;
224 u64 virtual_apic_page_addr;
225 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800226 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300227 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800228 u64 eoi_exit_bitmap0;
229 u64 eoi_exit_bitmap1;
230 u64 eoi_exit_bitmap2;
231 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800232 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300233 u64 guest_physical_address;
234 u64 vmcs_link_pointer;
235 u64 guest_ia32_debugctl;
236 u64 guest_ia32_pat;
237 u64 guest_ia32_efer;
238 u64 guest_ia32_perf_global_ctrl;
239 u64 guest_pdptr0;
240 u64 guest_pdptr1;
241 u64 guest_pdptr2;
242 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100243 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300244 u64 host_ia32_pat;
245 u64 host_ia32_efer;
246 u64 host_ia32_perf_global_ctrl;
247 u64 padding64[8]; /* room for future expansion */
248 /*
249 * To allow migration of L1 (complete with its L2 guests) between
250 * machines of different natural widths (32 or 64 bit), we cannot have
251 * unsigned long fields with no explict size. We use u64 (aliased
252 * natural_width) instead. Luckily, x86 is little-endian.
253 */
254 natural_width cr0_guest_host_mask;
255 natural_width cr4_guest_host_mask;
256 natural_width cr0_read_shadow;
257 natural_width cr4_read_shadow;
258 natural_width cr3_target_value0;
259 natural_width cr3_target_value1;
260 natural_width cr3_target_value2;
261 natural_width cr3_target_value3;
262 natural_width exit_qualification;
263 natural_width guest_linear_address;
264 natural_width guest_cr0;
265 natural_width guest_cr3;
266 natural_width guest_cr4;
267 natural_width guest_es_base;
268 natural_width guest_cs_base;
269 natural_width guest_ss_base;
270 natural_width guest_ds_base;
271 natural_width guest_fs_base;
272 natural_width guest_gs_base;
273 natural_width guest_ldtr_base;
274 natural_width guest_tr_base;
275 natural_width guest_gdtr_base;
276 natural_width guest_idtr_base;
277 natural_width guest_dr7;
278 natural_width guest_rsp;
279 natural_width guest_rip;
280 natural_width guest_rflags;
281 natural_width guest_pending_dbg_exceptions;
282 natural_width guest_sysenter_esp;
283 natural_width guest_sysenter_eip;
284 natural_width host_cr0;
285 natural_width host_cr3;
286 natural_width host_cr4;
287 natural_width host_fs_base;
288 natural_width host_gs_base;
289 natural_width host_tr_base;
290 natural_width host_gdtr_base;
291 natural_width host_idtr_base;
292 natural_width host_ia32_sysenter_esp;
293 natural_width host_ia32_sysenter_eip;
294 natural_width host_rsp;
295 natural_width host_rip;
296 natural_width paddingl[8]; /* room for future expansion */
297 u32 pin_based_vm_exec_control;
298 u32 cpu_based_vm_exec_control;
299 u32 exception_bitmap;
300 u32 page_fault_error_code_mask;
301 u32 page_fault_error_code_match;
302 u32 cr3_target_count;
303 u32 vm_exit_controls;
304 u32 vm_exit_msr_store_count;
305 u32 vm_exit_msr_load_count;
306 u32 vm_entry_controls;
307 u32 vm_entry_msr_load_count;
308 u32 vm_entry_intr_info_field;
309 u32 vm_entry_exception_error_code;
310 u32 vm_entry_instruction_len;
311 u32 tpr_threshold;
312 u32 secondary_vm_exec_control;
313 u32 vm_instruction_error;
314 u32 vm_exit_reason;
315 u32 vm_exit_intr_info;
316 u32 vm_exit_intr_error_code;
317 u32 idt_vectoring_info_field;
318 u32 idt_vectoring_error_code;
319 u32 vm_exit_instruction_len;
320 u32 vmx_instruction_info;
321 u32 guest_es_limit;
322 u32 guest_cs_limit;
323 u32 guest_ss_limit;
324 u32 guest_ds_limit;
325 u32 guest_fs_limit;
326 u32 guest_gs_limit;
327 u32 guest_ldtr_limit;
328 u32 guest_tr_limit;
329 u32 guest_gdtr_limit;
330 u32 guest_idtr_limit;
331 u32 guest_es_ar_bytes;
332 u32 guest_cs_ar_bytes;
333 u32 guest_ss_ar_bytes;
334 u32 guest_ds_ar_bytes;
335 u32 guest_fs_ar_bytes;
336 u32 guest_gs_ar_bytes;
337 u32 guest_ldtr_ar_bytes;
338 u32 guest_tr_ar_bytes;
339 u32 guest_interruptibility_info;
340 u32 guest_activity_state;
341 u32 guest_sysenter_cs;
342 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100343 u32 vmx_preemption_timer_value;
344 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300345 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800346 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300347 u16 guest_es_selector;
348 u16 guest_cs_selector;
349 u16 guest_ss_selector;
350 u16 guest_ds_selector;
351 u16 guest_fs_selector;
352 u16 guest_gs_selector;
353 u16 guest_ldtr_selector;
354 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800355 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300356 u16 host_es_selector;
357 u16 host_cs_selector;
358 u16 host_ss_selector;
359 u16 host_ds_selector;
360 u16 host_fs_selector;
361 u16 host_gs_selector;
362 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300363};
364
365/*
366 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
367 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
368 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
369 */
370#define VMCS12_REVISION 0x11e57ed0
371
372/*
373 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
374 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
375 * current implementation, 4K are reserved to avoid future complications.
376 */
377#define VMCS12_SIZE 0x1000
378
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300379/* Used to remember the last vmcs02 used for some recently used vmcs12s */
380struct vmcs02_list {
381 struct list_head list;
382 gpa_t vmptr;
383 struct loaded_vmcs vmcs02;
384};
385
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300386/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300387 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
388 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
389 */
390struct nested_vmx {
391 /* Has the level1 guest done vmxon? */
392 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400393 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300394
395 /* The guest-physical address of the current VMCS L1 keeps for L2 */
396 gpa_t current_vmptr;
397 /* The host-usable pointer to the above */
398 struct page *current_vmcs12_page;
399 struct vmcs12 *current_vmcs12;
Abel Gordon8de48832013-04-18 14:37:25 +0300400 struct vmcs *current_shadow_vmcs;
Abel Gordon012f83c2013-04-18 14:39:25 +0300401 /*
402 * Indicates if the shadow vmcs must be updated with the
403 * data hold by vmcs12
404 */
405 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300406
407 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
408 struct list_head vmcs02_pool;
409 int vmcs02_num;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300410 u64 vmcs01_tsc_offset;
Nadav Har'El644d7112011-05-25 23:12:35 +0300411 /* L2 must run next, and mustn't decide to exit to L1. */
412 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300413 /*
414 * Guest pages referred to in vmcs02 with host-physical pointers, so
415 * we must keep them pinned while L2 runs.
416 */
417 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800418 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800419 struct page *pi_desc_page;
420 struct pi_desc *pi_desc;
421 bool pi_pending;
422 u16 posted_intr_nv;
Nadav Har'Elb3897a42013-07-08 19:12:35 +0800423 u64 msr_ia32_feature_control;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100424
425 struct hrtimer preemption_timer;
426 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200427
428 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
429 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800430
Wanpeng Li5c614b32015-10-13 09:18:36 -0700431 u16 vpid02;
432 u16 last_vpid;
433
Wincy Vanb9c237b2015-02-03 23:56:30 +0800434 u32 nested_vmx_procbased_ctls_low;
435 u32 nested_vmx_procbased_ctls_high;
436 u32 nested_vmx_true_procbased_ctls_low;
437 u32 nested_vmx_secondary_ctls_low;
438 u32 nested_vmx_secondary_ctls_high;
439 u32 nested_vmx_pinbased_ctls_low;
440 u32 nested_vmx_pinbased_ctls_high;
441 u32 nested_vmx_exit_ctls_low;
442 u32 nested_vmx_exit_ctls_high;
443 u32 nested_vmx_true_exit_ctls_low;
444 u32 nested_vmx_entry_ctls_low;
445 u32 nested_vmx_entry_ctls_high;
446 u32 nested_vmx_true_entry_ctls_low;
447 u32 nested_vmx_misc_low;
448 u32 nested_vmx_misc_high;
449 u32 nested_vmx_ept_caps;
Wanpeng Li99b83ac2015-10-13 09:12:21 -0700450 u32 nested_vmx_vpid_caps;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300451};
452
Yang Zhang01e439b2013-04-11 19:25:12 +0800453#define POSTED_INTR_ON 0
Feng Wuebbfc762015-09-18 22:29:46 +0800454#define POSTED_INTR_SN 1
455
Yang Zhang01e439b2013-04-11 19:25:12 +0800456/* Posted-Interrupt Descriptor */
457struct pi_desc {
458 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800459 union {
460 struct {
461 /* bit 256 - Outstanding Notification */
462 u16 on : 1,
463 /* bit 257 - Suppress Notification */
464 sn : 1,
465 /* bit 271:258 - Reserved */
466 rsvd_1 : 14;
467 /* bit 279:272 - Notification Vector */
468 u8 nv;
469 /* bit 287:280 - Reserved */
470 u8 rsvd_2;
471 /* bit 319:288 - Notification Destination */
472 u32 ndst;
473 };
474 u64 control;
475 };
476 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800477} __aligned(64);
478
Yang Zhanga20ed542013-04-11 19:25:15 +0800479static bool pi_test_and_set_on(struct pi_desc *pi_desc)
480{
481 return test_and_set_bit(POSTED_INTR_ON,
482 (unsigned long *)&pi_desc->control);
483}
484
485static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
486{
487 return test_and_clear_bit(POSTED_INTR_ON,
488 (unsigned long *)&pi_desc->control);
489}
490
491static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
492{
493 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
494}
495
Feng Wuebbfc762015-09-18 22:29:46 +0800496static inline void pi_clear_sn(struct pi_desc *pi_desc)
497{
498 return clear_bit(POSTED_INTR_SN,
499 (unsigned long *)&pi_desc->control);
500}
501
502static inline void pi_set_sn(struct pi_desc *pi_desc)
503{
504 return set_bit(POSTED_INTR_SN,
505 (unsigned long *)&pi_desc->control);
506}
507
508static inline int pi_test_on(struct pi_desc *pi_desc)
509{
510 return test_bit(POSTED_INTR_ON,
511 (unsigned long *)&pi_desc->control);
512}
513
514static inline int pi_test_sn(struct pi_desc *pi_desc)
515{
516 return test_bit(POSTED_INTR_SN,
517 (unsigned long *)&pi_desc->control);
518}
519
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400520struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000521 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300522 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300523 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200524 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300525 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200526 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200527 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300528 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400529 int nmsrs;
530 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800531 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400532#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300533 u64 msr_host_kernel_gs_base;
534 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400535#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200536 u32 vm_entry_controls_shadow;
537 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300538 /*
539 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
540 * non-nested (L1) guest, it always points to vmcs01. For a nested
541 * guest (L2), it points to a different VMCS.
542 */
543 struct loaded_vmcs vmcs01;
544 struct loaded_vmcs *loaded_vmcs;
545 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300546 struct msr_autoload {
547 unsigned nr;
548 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
549 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
550 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400551 struct {
552 int loaded;
553 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300554#ifdef CONFIG_X86_64
555 u16 ds_sel, es_sel;
556#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200557 int gs_ldt_reload_needed;
558 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000559 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700560 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400561 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200562 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300563 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300564 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300565 struct kvm_segment segs[8];
566 } rmode;
567 struct {
568 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300569 struct kvm_save_segment {
570 u16 selector;
571 unsigned long base;
572 u32 limit;
573 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300574 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300575 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800576 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300577 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200578
579 /* Support for vnmi-less CPUs */
580 int soft_vnmi_blocked;
581 ktime_t entry_time;
582 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800583 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800584
Yang Zhang01e439b2013-04-11 19:25:12 +0800585 /* Posted interrupt descriptor */
586 struct pi_desc pi_desc;
587
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300588 /* Support for a guest hypervisor (nested VMX) */
589 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200590
591 /* Dynamic PLE window. */
592 int ple_window;
593 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800594
595 /* Support for PML */
596#define PML_ENTITY_NUM 512
597 struct page *pml_pg;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400598};
599
Avi Kivity2fb92db2011-04-27 19:42:18 +0300600enum segment_cache_field {
601 SEG_FIELD_SEL = 0,
602 SEG_FIELD_BASE = 1,
603 SEG_FIELD_LIMIT = 2,
604 SEG_FIELD_AR = 3,
605
606 SEG_FIELD_NR = 4
607};
608
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400609static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
610{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000611 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400612}
613
Feng Wuefc64402015-09-18 22:29:51 +0800614static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
615{
616 return &(to_vmx(vcpu)->pi_desc);
617}
618
Nadav Har'El22bd0352011-05-25 23:05:57 +0300619#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
620#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
621#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
622 [number##_HIGH] = VMCS12_OFFSET(name)+4
623
Abel Gordon4607c2d2013-04-18 14:35:55 +0300624
Bandan Dasfe2b2012014-04-21 15:20:14 -0400625static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300626 /*
627 * We do NOT shadow fields that are modified when L0
628 * traps and emulates any vmx instruction (e.g. VMPTRLD,
629 * VMXON...) executed by L1.
630 * For example, VM_INSTRUCTION_ERROR is read
631 * by L1 if a vmx instruction fails (part of the error path).
632 * Note the code assumes this logic. If for some reason
633 * we start shadowing these fields then we need to
634 * force a shadow sync when L0 emulates vmx instructions
635 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
636 * by nested_vmx_failValid)
637 */
638 VM_EXIT_REASON,
639 VM_EXIT_INTR_INFO,
640 VM_EXIT_INSTRUCTION_LEN,
641 IDT_VECTORING_INFO_FIELD,
642 IDT_VECTORING_ERROR_CODE,
643 VM_EXIT_INTR_ERROR_CODE,
644 EXIT_QUALIFICATION,
645 GUEST_LINEAR_ADDRESS,
646 GUEST_PHYSICAL_ADDRESS
647};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400648static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300649 ARRAY_SIZE(shadow_read_only_fields);
650
Bandan Dasfe2b2012014-04-21 15:20:14 -0400651static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800652 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300653 GUEST_RIP,
654 GUEST_RSP,
655 GUEST_CR0,
656 GUEST_CR3,
657 GUEST_CR4,
658 GUEST_INTERRUPTIBILITY_INFO,
659 GUEST_RFLAGS,
660 GUEST_CS_SELECTOR,
661 GUEST_CS_AR_BYTES,
662 GUEST_CS_LIMIT,
663 GUEST_CS_BASE,
664 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100665 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300666 CR0_GUEST_HOST_MASK,
667 CR0_READ_SHADOW,
668 CR4_READ_SHADOW,
669 TSC_OFFSET,
670 EXCEPTION_BITMAP,
671 CPU_BASED_VM_EXEC_CONTROL,
672 VM_ENTRY_EXCEPTION_ERROR_CODE,
673 VM_ENTRY_INTR_INFO_FIELD,
674 VM_ENTRY_INSTRUCTION_LEN,
675 VM_ENTRY_EXCEPTION_ERROR_CODE,
676 HOST_FS_BASE,
677 HOST_GS_BASE,
678 HOST_FS_SELECTOR,
679 HOST_GS_SELECTOR
680};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400681static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300682 ARRAY_SIZE(shadow_read_write_fields);
683
Mathias Krause772e0312012-08-30 01:30:19 +0200684static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300685 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800686 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300687 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
688 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
689 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
690 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
691 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
692 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
693 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
694 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800695 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300696 FIELD(HOST_ES_SELECTOR, host_es_selector),
697 FIELD(HOST_CS_SELECTOR, host_cs_selector),
698 FIELD(HOST_SS_SELECTOR, host_ss_selector),
699 FIELD(HOST_DS_SELECTOR, host_ds_selector),
700 FIELD(HOST_FS_SELECTOR, host_fs_selector),
701 FIELD(HOST_GS_SELECTOR, host_gs_selector),
702 FIELD(HOST_TR_SELECTOR, host_tr_selector),
703 FIELD64(IO_BITMAP_A, io_bitmap_a),
704 FIELD64(IO_BITMAP_B, io_bitmap_b),
705 FIELD64(MSR_BITMAP, msr_bitmap),
706 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
707 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
708 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
709 FIELD64(TSC_OFFSET, tsc_offset),
710 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
711 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800712 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300713 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800714 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
715 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
716 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
717 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800718 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300719 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
720 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
721 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
722 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
723 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
724 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
725 FIELD64(GUEST_PDPTR0, guest_pdptr0),
726 FIELD64(GUEST_PDPTR1, guest_pdptr1),
727 FIELD64(GUEST_PDPTR2, guest_pdptr2),
728 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100729 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300730 FIELD64(HOST_IA32_PAT, host_ia32_pat),
731 FIELD64(HOST_IA32_EFER, host_ia32_efer),
732 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
733 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
734 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
735 FIELD(EXCEPTION_BITMAP, exception_bitmap),
736 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
737 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
738 FIELD(CR3_TARGET_COUNT, cr3_target_count),
739 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
740 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
741 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
742 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
743 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
744 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
745 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
746 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
747 FIELD(TPR_THRESHOLD, tpr_threshold),
748 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
749 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
750 FIELD(VM_EXIT_REASON, vm_exit_reason),
751 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
752 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
753 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
754 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
755 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
756 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
757 FIELD(GUEST_ES_LIMIT, guest_es_limit),
758 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
759 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
760 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
761 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
762 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
763 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
764 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
765 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
766 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
767 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
768 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
769 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
770 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
771 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
772 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
773 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
774 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
775 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
776 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
777 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
778 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100779 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300780 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
781 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
782 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
783 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
784 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
785 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
786 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
787 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
788 FIELD(EXIT_QUALIFICATION, exit_qualification),
789 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
790 FIELD(GUEST_CR0, guest_cr0),
791 FIELD(GUEST_CR3, guest_cr3),
792 FIELD(GUEST_CR4, guest_cr4),
793 FIELD(GUEST_ES_BASE, guest_es_base),
794 FIELD(GUEST_CS_BASE, guest_cs_base),
795 FIELD(GUEST_SS_BASE, guest_ss_base),
796 FIELD(GUEST_DS_BASE, guest_ds_base),
797 FIELD(GUEST_FS_BASE, guest_fs_base),
798 FIELD(GUEST_GS_BASE, guest_gs_base),
799 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
800 FIELD(GUEST_TR_BASE, guest_tr_base),
801 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
802 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
803 FIELD(GUEST_DR7, guest_dr7),
804 FIELD(GUEST_RSP, guest_rsp),
805 FIELD(GUEST_RIP, guest_rip),
806 FIELD(GUEST_RFLAGS, guest_rflags),
807 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
808 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
809 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
810 FIELD(HOST_CR0, host_cr0),
811 FIELD(HOST_CR3, host_cr3),
812 FIELD(HOST_CR4, host_cr4),
813 FIELD(HOST_FS_BASE, host_fs_base),
814 FIELD(HOST_GS_BASE, host_gs_base),
815 FIELD(HOST_TR_BASE, host_tr_base),
816 FIELD(HOST_GDTR_BASE, host_gdtr_base),
817 FIELD(HOST_IDTR_BASE, host_idtr_base),
818 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
819 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
820 FIELD(HOST_RSP, host_rsp),
821 FIELD(HOST_RIP, host_rip),
822};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300823
824static inline short vmcs_field_to_offset(unsigned long field)
825{
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100826 BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
827
828 if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
829 vmcs_field_to_offset_table[field] == 0)
830 return -ENOENT;
831
Nadav Har'El22bd0352011-05-25 23:05:57 +0300832 return vmcs_field_to_offset_table[field];
833}
834
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300835static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
836{
837 return to_vmx(vcpu)->nested.current_vmcs12;
838}
839
840static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
841{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200842 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800843 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300844 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800845
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300846 return page;
847}
848
849static void nested_release_page(struct page *page)
850{
851 kvm_release_page_dirty(page);
852}
853
854static void nested_release_page_clean(struct page *page)
855{
856 kvm_release_page_clean(page);
857}
858
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300859static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800860static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800861static void kvm_cpu_vmxon(u64 addr);
862static void kvm_cpu_vmxoff(void);
Paolo Bonzini93c4adc2014-03-05 23:19:52 +0100863static bool vmx_mpx_supported(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800864static bool vmx_xsaves_supported(void);
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +0200865static int vmx_cpu_uses_apicv(struct kvm_vcpu *vcpu);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200866static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300867static void vmx_set_segment(struct kvm_vcpu *vcpu,
868 struct kvm_segment *var, int seg);
869static void vmx_get_segment(struct kvm_vcpu *vcpu,
870 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200871static bool guest_state_valid(struct kvm_vcpu *vcpu);
872static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yang Zhanga20ed542013-04-11 19:25:15 +0800873static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu);
Abel Gordonc3114422013-04-18 14:38:55 +0300874static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300875static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +0800876static int alloc_identity_pagetable(struct kvm *kvm);
Avi Kivity75880a02007-06-20 11:20:04 +0300877
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878static DEFINE_PER_CPU(struct vmcs *, vmxarea);
879static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300880/*
881 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
882 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
883 */
884static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300885static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886
Feng Wubf9f6ac2015-09-18 22:29:55 +0800887/*
888 * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
889 * can find which vCPU should be waken up.
890 */
891static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
892static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
893
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200894static unsigned long *vmx_io_bitmap_a;
895static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200896static unsigned long *vmx_msr_bitmap_legacy;
897static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800898static unsigned long *vmx_msr_bitmap_legacy_x2apic;
899static unsigned long *vmx_msr_bitmap_longmode_x2apic;
Wincy Van3af18d92015-02-03 23:49:31 +0800900static unsigned long *vmx_msr_bitmap_nested;
Abel Gordon4607c2d2013-04-18 14:35:55 +0300901static unsigned long *vmx_vmread_bitmap;
902static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300903
Avi Kivity110312c2010-12-21 12:54:20 +0200904static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200905static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200906
Sheng Yang2384d2b2008-01-17 15:14:33 +0800907static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
908static DEFINE_SPINLOCK(vmx_vpid_lock);
909
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300910static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800911 int size;
912 int order;
913 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300914 u32 pin_based_exec_ctrl;
915 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800916 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300917 u32 vmexit_ctrl;
918 u32 vmentry_ctrl;
919} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920
Hannes Ederefff9e52008-11-28 17:02:06 +0100921static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800922 u32 ept;
923 u32 vpid;
924} vmx_capability;
925
Avi Kivity6aa8b732006-12-10 02:21:36 -0800926#define VMX_SEGMENT_FIELD(seg) \
927 [VCPU_SREG_##seg] = { \
928 .selector = GUEST_##seg##_SELECTOR, \
929 .base = GUEST_##seg##_BASE, \
930 .limit = GUEST_##seg##_LIMIT, \
931 .ar_bytes = GUEST_##seg##_AR_BYTES, \
932 }
933
Mathias Krause772e0312012-08-30 01:30:19 +0200934static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800935 unsigned selector;
936 unsigned base;
937 unsigned limit;
938 unsigned ar_bytes;
939} kvm_vmx_segment_fields[] = {
940 VMX_SEGMENT_FIELD(CS),
941 VMX_SEGMENT_FIELD(DS),
942 VMX_SEGMENT_FIELD(ES),
943 VMX_SEGMENT_FIELD(FS),
944 VMX_SEGMENT_FIELD(GS),
945 VMX_SEGMENT_FIELD(SS),
946 VMX_SEGMENT_FIELD(TR),
947 VMX_SEGMENT_FIELD(LDTR),
948};
949
Avi Kivity26bb0982009-09-07 11:14:12 +0300950static u64 host_efer;
951
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300952static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
953
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300954/*
Brian Gerst8c065852010-07-17 09:03:26 -0400955 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300956 * away by decrementing the array size.
957 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800959#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300960 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400962 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964
Gui Jianfeng31299942010-03-15 17:29:09 +0800965static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966{
967 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
968 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100969 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970}
971
Gui Jianfeng31299942010-03-15 17:29:09 +0800972static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300973{
974 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
975 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100976 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300977}
978
Gui Jianfeng31299942010-03-15 17:29:09 +0800979static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500980{
981 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
982 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100983 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500984}
985
Gui Jianfeng31299942010-03-15 17:29:09 +0800986static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987{
988 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
989 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
990}
991
Gui Jianfeng31299942010-03-15 17:29:09 +0800992static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800993{
994 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
995 INTR_INFO_VALID_MASK)) ==
996 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
997}
998
Gui Jianfeng31299942010-03-15 17:29:09 +0800999static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +08001000{
Sheng Yang04547152009-04-01 15:52:31 +08001001 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +08001002}
1003
Gui Jianfeng31299942010-03-15 17:29:09 +08001004static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001005{
Sheng Yang04547152009-04-01 15:52:31 +08001006 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001007}
1008
Paolo Bonzini35754c92015-07-29 12:05:37 +02001009static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001010{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001011 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001012}
1013
Gui Jianfeng31299942010-03-15 17:29:09 +08001014static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001015{
Sheng Yang04547152009-04-01 15:52:31 +08001016 return vmcs_config.cpu_based_exec_ctrl &
1017 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001018}
1019
Avi Kivity774ead32007-12-26 13:57:04 +02001020static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001021{
Sheng Yang04547152009-04-01 15:52:31 +08001022 return vmcs_config.cpu_based_2nd_exec_ctrl &
1023 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
1024}
1025
Yang Zhang8d146952013-01-25 10:18:50 +08001026static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
1027{
1028 return vmcs_config.cpu_based_2nd_exec_ctrl &
1029 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
1030}
1031
Yang Zhang83d4c282013-01-25 10:18:49 +08001032static inline bool cpu_has_vmx_apic_register_virt(void)
1033{
1034 return vmcs_config.cpu_based_2nd_exec_ctrl &
1035 SECONDARY_EXEC_APIC_REGISTER_VIRT;
1036}
1037
Yang Zhangc7c9c562013-01-25 10:18:51 +08001038static inline bool cpu_has_vmx_virtual_intr_delivery(void)
1039{
1040 return vmcs_config.cpu_based_2nd_exec_ctrl &
1041 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
1042}
1043
Yang Zhang01e439b2013-04-11 19:25:12 +08001044static inline bool cpu_has_vmx_posted_intr(void)
1045{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001046 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1047 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001048}
1049
1050static inline bool cpu_has_vmx_apicv(void)
1051{
1052 return cpu_has_vmx_apic_register_virt() &&
1053 cpu_has_vmx_virtual_intr_delivery() &&
1054 cpu_has_vmx_posted_intr();
1055}
1056
Sheng Yang04547152009-04-01 15:52:31 +08001057static inline bool cpu_has_vmx_flexpriority(void)
1058{
1059 return cpu_has_vmx_tpr_shadow() &&
1060 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001061}
1062
Marcelo Tosattie7997942009-06-11 12:07:40 -03001063static inline bool cpu_has_vmx_ept_execute_only(void)
1064{
Gui Jianfeng31299942010-03-15 17:29:09 +08001065 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001066}
1067
Marcelo Tosattie7997942009-06-11 12:07:40 -03001068static inline bool cpu_has_vmx_ept_2m_page(void)
1069{
Gui Jianfeng31299942010-03-15 17:29:09 +08001070 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001071}
1072
Sheng Yang878403b2010-01-05 19:02:29 +08001073static inline bool cpu_has_vmx_ept_1g_page(void)
1074{
Gui Jianfeng31299942010-03-15 17:29:09 +08001075 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001076}
1077
Sheng Yang4bc9b982010-06-02 14:05:24 +08001078static inline bool cpu_has_vmx_ept_4levels(void)
1079{
1080 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1081}
1082
Xudong Hao83c3a332012-05-28 19:33:35 +08001083static inline bool cpu_has_vmx_ept_ad_bits(void)
1084{
1085 return vmx_capability.ept & VMX_EPT_AD_BIT;
1086}
1087
Gui Jianfeng31299942010-03-15 17:29:09 +08001088static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001089{
Gui Jianfeng31299942010-03-15 17:29:09 +08001090 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001091}
1092
Gui Jianfeng31299942010-03-15 17:29:09 +08001093static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001094{
Gui Jianfeng31299942010-03-15 17:29:09 +08001095 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001096}
1097
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001098static inline bool cpu_has_vmx_invvpid_single(void)
1099{
1100 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1101}
1102
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001103static inline bool cpu_has_vmx_invvpid_global(void)
1104{
1105 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1106}
1107
Gui Jianfeng31299942010-03-15 17:29:09 +08001108static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001109{
Sheng Yang04547152009-04-01 15:52:31 +08001110 return vmcs_config.cpu_based_2nd_exec_ctrl &
1111 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001112}
1113
Gui Jianfeng31299942010-03-15 17:29:09 +08001114static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001115{
1116 return vmcs_config.cpu_based_2nd_exec_ctrl &
1117 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1118}
1119
Gui Jianfeng31299942010-03-15 17:29:09 +08001120static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001121{
1122 return vmcs_config.cpu_based_2nd_exec_ctrl &
1123 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1124}
1125
Paolo Bonzini35754c92015-07-29 12:05:37 +02001126static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001127{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001128 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001129}
1130
Gui Jianfeng31299942010-03-15 17:29:09 +08001131static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001132{
Sheng Yang04547152009-04-01 15:52:31 +08001133 return vmcs_config.cpu_based_2nd_exec_ctrl &
1134 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001135}
1136
Gui Jianfeng31299942010-03-15 17:29:09 +08001137static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001138{
1139 return vmcs_config.cpu_based_2nd_exec_ctrl &
1140 SECONDARY_EXEC_RDTSCP;
1141}
1142
Mao, Junjiead756a12012-07-02 01:18:48 +00001143static inline bool cpu_has_vmx_invpcid(void)
1144{
1145 return vmcs_config.cpu_based_2nd_exec_ctrl &
1146 SECONDARY_EXEC_ENABLE_INVPCID;
1147}
1148
Gui Jianfeng31299942010-03-15 17:29:09 +08001149static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001150{
1151 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1152}
1153
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001154static inline bool cpu_has_vmx_wbinvd_exit(void)
1155{
1156 return vmcs_config.cpu_based_2nd_exec_ctrl &
1157 SECONDARY_EXEC_WBINVD_EXITING;
1158}
1159
Abel Gordonabc4fc52013-04-18 14:35:25 +03001160static inline bool cpu_has_vmx_shadow_vmcs(void)
1161{
1162 u64 vmx_msr;
1163 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1164 /* check if the cpu supports writing r/o exit information fields */
1165 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1166 return false;
1167
1168 return vmcs_config.cpu_based_2nd_exec_ctrl &
1169 SECONDARY_EXEC_SHADOW_VMCS;
1170}
1171
Kai Huang843e4332015-01-28 10:54:28 +08001172static inline bool cpu_has_vmx_pml(void)
1173{
1174 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1175}
1176
Haozhong Zhang64903d62015-10-20 15:39:09 +08001177static inline bool cpu_has_vmx_tsc_scaling(void)
1178{
1179 return vmcs_config.cpu_based_2nd_exec_ctrl &
1180 SECONDARY_EXEC_TSC_SCALING;
1181}
1182
Sheng Yang04547152009-04-01 15:52:31 +08001183static inline bool report_flexpriority(void)
1184{
1185 return flexpriority_enabled;
1186}
1187
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001188static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1189{
1190 return vmcs12->cpu_based_vm_exec_control & bit;
1191}
1192
1193static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1194{
1195 return (vmcs12->cpu_based_vm_exec_control &
1196 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1197 (vmcs12->secondary_vm_exec_control & bit);
1198}
1199
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001200static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001201{
1202 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1203}
1204
Jan Kiszkaf41245002014-03-07 20:03:13 +01001205static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1206{
1207 return vmcs12->pin_based_vm_exec_control &
1208 PIN_BASED_VMX_PREEMPTION_TIMER;
1209}
1210
Nadav Har'El155a97a2013-08-05 11:07:16 +03001211static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1212{
1213 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1214}
1215
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001216static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1217{
1218 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1219 vmx_xsaves_supported();
1220}
1221
Wincy Vanf2b93282015-02-03 23:56:03 +08001222static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1223{
1224 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1225}
1226
Wanpeng Li5c614b32015-10-13 09:18:36 -07001227static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12)
1228{
1229 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID);
1230}
1231
Wincy Van82f0dd42015-02-03 23:57:18 +08001232static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1233{
1234 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1235}
1236
Wincy Van608406e2015-02-03 23:57:51 +08001237static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1238{
1239 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1240}
1241
Wincy Van705699a2015-02-03 23:58:17 +08001242static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1243{
1244 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1245}
1246
Nadav Har'El644d7112011-05-25 23:12:35 +03001247static inline bool is_exception(u32 intr_info)
1248{
1249 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1250 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
1251}
1252
Jan Kiszka533558b2014-01-04 18:47:20 +01001253static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1254 u32 exit_intr_info,
1255 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001256static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1257 struct vmcs12 *vmcs12,
1258 u32 reason, unsigned long qualification);
1259
Rusty Russell8b9cf982007-07-30 16:31:43 +10001260static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001261{
1262 int i;
1263
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001264 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001265 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001266 return i;
1267 return -1;
1268}
1269
Sheng Yang2384d2b2008-01-17 15:14:33 +08001270static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1271{
1272 struct {
1273 u64 vpid : 16;
1274 u64 rsvd : 48;
1275 u64 gva;
1276 } operand = { vpid, 0, gva };
1277
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001278 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001279 /* CF==1 or ZF==1 --> rc = -1 */
1280 "; ja 1f ; ud2 ; 1:"
1281 : : "a"(&operand), "c"(ext) : "cc", "memory");
1282}
1283
Sheng Yang14394422008-04-28 12:24:45 +08001284static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1285{
1286 struct {
1287 u64 eptp, gpa;
1288 } operand = {eptp, gpa};
1289
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001290 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001291 /* CF==1 or ZF==1 --> rc = -1 */
1292 "; ja 1f ; ud2 ; 1:\n"
1293 : : "a" (&operand), "c" (ext) : "cc", "memory");
1294}
1295
Avi Kivity26bb0982009-09-07 11:14:12 +03001296static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001297{
1298 int i;
1299
Rusty Russell8b9cf982007-07-30 16:31:43 +10001300 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001301 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001302 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001303 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001304}
1305
Avi Kivity6aa8b732006-12-10 02:21:36 -08001306static void vmcs_clear(struct vmcs *vmcs)
1307{
1308 u64 phys_addr = __pa(vmcs);
1309 u8 error;
1310
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001311 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001312 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313 : "cc", "memory");
1314 if (error)
1315 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1316 vmcs, phys_addr);
1317}
1318
Nadav Har'Eld462b812011-05-24 15:26:10 +03001319static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1320{
1321 vmcs_clear(loaded_vmcs->vmcs);
1322 loaded_vmcs->cpu = -1;
1323 loaded_vmcs->launched = 0;
1324}
1325
Dongxiao Xu7725b892010-05-11 18:29:38 +08001326static void vmcs_load(struct vmcs *vmcs)
1327{
1328 u64 phys_addr = __pa(vmcs);
1329 u8 error;
1330
1331 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001332 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001333 : "cc", "memory");
1334 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001335 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001336 vmcs, phys_addr);
1337}
1338
Dave Young2965faa2015-09-09 15:38:55 -07001339#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001340/*
1341 * This bitmap is used to indicate whether the vmclear
1342 * operation is enabled on all cpus. All disabled by
1343 * default.
1344 */
1345static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1346
1347static inline void crash_enable_local_vmclear(int cpu)
1348{
1349 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1350}
1351
1352static inline void crash_disable_local_vmclear(int cpu)
1353{
1354 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1355}
1356
1357static inline int crash_local_vmclear_enabled(int cpu)
1358{
1359 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1360}
1361
1362static void crash_vmclear_local_loaded_vmcss(void)
1363{
1364 int cpu = raw_smp_processor_id();
1365 struct loaded_vmcs *v;
1366
1367 if (!crash_local_vmclear_enabled(cpu))
1368 return;
1369
1370 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1371 loaded_vmcss_on_cpu_link)
1372 vmcs_clear(v->vmcs);
1373}
1374#else
1375static inline void crash_enable_local_vmclear(int cpu) { }
1376static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001377#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001378
Nadav Har'Eld462b812011-05-24 15:26:10 +03001379static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001381 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001382 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383
Nadav Har'Eld462b812011-05-24 15:26:10 +03001384 if (loaded_vmcs->cpu != cpu)
1385 return; /* vcpu migration can race with cpu offline */
1386 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001388 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001389 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001390
1391 /*
1392 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1393 * is before setting loaded_vmcs->vcpu to -1 which is done in
1394 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1395 * then adds the vmcs into percpu list before it is deleted.
1396 */
1397 smp_wmb();
1398
Nadav Har'Eld462b812011-05-24 15:26:10 +03001399 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001400 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401}
1402
Nadav Har'Eld462b812011-05-24 15:26:10 +03001403static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001404{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001405 int cpu = loaded_vmcs->cpu;
1406
1407 if (cpu != -1)
1408 smp_call_function_single(cpu,
1409 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001410}
1411
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001412static inline void vpid_sync_vcpu_single(int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001413{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001414 if (vpid == 0)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001415 return;
1416
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001417 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001418 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001419}
1420
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001421static inline void vpid_sync_vcpu_global(void)
1422{
1423 if (cpu_has_vmx_invvpid_global())
1424 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1425}
1426
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001427static inline void vpid_sync_context(int vpid)
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001428{
1429 if (cpu_has_vmx_invvpid_single())
Wanpeng Lidd5f5342015-09-23 18:26:57 +08001430 vpid_sync_vcpu_single(vpid);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001431 else
1432 vpid_sync_vcpu_global();
1433}
1434
Sheng Yang14394422008-04-28 12:24:45 +08001435static inline void ept_sync_global(void)
1436{
1437 if (cpu_has_vmx_invept_global())
1438 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1439}
1440
1441static inline void ept_sync_context(u64 eptp)
1442{
Avi Kivity089d0342009-03-23 18:26:32 +02001443 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001444 if (cpu_has_vmx_invept_context())
1445 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1446 else
1447 ept_sync_global();
1448 }
1449}
1450
Avi Kivity96304212011-05-15 10:13:13 -04001451static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452{
Avi Kivity5e520e62011-05-15 10:13:12 -04001453 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454
Avi Kivity5e520e62011-05-15 10:13:12 -04001455 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1456 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457 return value;
1458}
1459
Avi Kivity96304212011-05-15 10:13:13 -04001460static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001461{
1462 return vmcs_readl(field);
1463}
1464
Avi Kivity96304212011-05-15 10:13:13 -04001465static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466{
1467 return vmcs_readl(field);
1468}
1469
Avi Kivity96304212011-05-15 10:13:13 -04001470static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471{
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001472#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473 return vmcs_readl(field);
1474#else
1475 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
1476#endif
1477}
1478
Avi Kivitye52de1b2007-01-05 16:36:56 -08001479static noinline void vmwrite_error(unsigned long field, unsigned long value)
1480{
1481 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1482 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1483 dump_stack();
1484}
1485
Avi Kivity6aa8b732006-12-10 02:21:36 -08001486static void vmcs_writel(unsigned long field, unsigned long value)
1487{
1488 u8 error;
1489
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001490 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001491 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001492 if (unlikely(error))
1493 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494}
1495
1496static void vmcs_write16(unsigned long field, u16 value)
1497{
1498 vmcs_writel(field, value);
1499}
1500
1501static void vmcs_write32(unsigned long field, u32 value)
1502{
1503 vmcs_writel(field, value);
1504}
1505
1506static void vmcs_write64(unsigned long field, u64 value)
1507{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001509#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510 asm volatile ("");
1511 vmcs_writel(field+1, value >> 32);
1512#endif
1513}
1514
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001515static void vmcs_clear_bits(unsigned long field, u32 mask)
1516{
1517 vmcs_writel(field, vmcs_readl(field) & ~mask);
1518}
1519
1520static void vmcs_set_bits(unsigned long field, u32 mask)
1521{
1522 vmcs_writel(field, vmcs_readl(field) | mask);
1523}
1524
Gleb Natapov2961e8762013-11-25 15:37:13 +02001525static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1526{
1527 vmcs_write32(VM_ENTRY_CONTROLS, val);
1528 vmx->vm_entry_controls_shadow = val;
1529}
1530
1531static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1532{
1533 if (vmx->vm_entry_controls_shadow != val)
1534 vm_entry_controls_init(vmx, val);
1535}
1536
1537static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1538{
1539 return vmx->vm_entry_controls_shadow;
1540}
1541
1542
1543static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1544{
1545 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1546}
1547
1548static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1549{
1550 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1551}
1552
1553static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1554{
1555 vmcs_write32(VM_EXIT_CONTROLS, val);
1556 vmx->vm_exit_controls_shadow = val;
1557}
1558
1559static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1560{
1561 if (vmx->vm_exit_controls_shadow != val)
1562 vm_exit_controls_init(vmx, val);
1563}
1564
1565static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1566{
1567 return vmx->vm_exit_controls_shadow;
1568}
1569
1570
1571static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1572{
1573 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1574}
1575
1576static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1577{
1578 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1579}
1580
Avi Kivity2fb92db2011-04-27 19:42:18 +03001581static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1582{
1583 vmx->segment_cache.bitmask = 0;
1584}
1585
1586static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1587 unsigned field)
1588{
1589 bool ret;
1590 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1591
1592 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1593 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1594 vmx->segment_cache.bitmask = 0;
1595 }
1596 ret = vmx->segment_cache.bitmask & mask;
1597 vmx->segment_cache.bitmask |= mask;
1598 return ret;
1599}
1600
1601static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1602{
1603 u16 *p = &vmx->segment_cache.seg[seg].selector;
1604
1605 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1606 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1607 return *p;
1608}
1609
1610static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1611{
1612 ulong *p = &vmx->segment_cache.seg[seg].base;
1613
1614 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1615 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1616 return *p;
1617}
1618
1619static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1620{
1621 u32 *p = &vmx->segment_cache.seg[seg].limit;
1622
1623 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1624 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1625 return *p;
1626}
1627
1628static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1629{
1630 u32 *p = &vmx->segment_cache.seg[seg].ar;
1631
1632 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1633 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1634 return *p;
1635}
1636
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001637static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1638{
1639 u32 eb;
1640
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001641 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1642 (1u << NM_VECTOR) | (1u << DB_VECTOR);
1643 if ((vcpu->guest_debug &
1644 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1645 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1646 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001647 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001648 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001649 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001650 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001651 if (vcpu->fpu_active)
1652 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001653
1654 /* When we are running a nested L2 guest and L1 specified for it a
1655 * certain exception bitmap, we must trap the same exceptions and pass
1656 * them to L1. When running L2, we will only handle the exceptions
1657 * specified above if L1 did not want them.
1658 */
1659 if (is_guest_mode(vcpu))
1660 eb |= get_vmcs12(vcpu)->exception_bitmap;
1661
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001662 vmcs_write32(EXCEPTION_BITMAP, eb);
1663}
1664
Gleb Natapov2961e8762013-11-25 15:37:13 +02001665static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1666 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001667{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001668 vm_entry_controls_clearbit(vmx, entry);
1669 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001670}
1671
Avi Kivity61d2ef22010-04-28 16:40:38 +03001672static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1673{
1674 unsigned i;
1675 struct msr_autoload *m = &vmx->msr_autoload;
1676
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001677 switch (msr) {
1678 case MSR_EFER:
1679 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001680 clear_atomic_switch_msr_special(vmx,
1681 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001682 VM_EXIT_LOAD_IA32_EFER);
1683 return;
1684 }
1685 break;
1686 case MSR_CORE_PERF_GLOBAL_CTRL:
1687 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001688 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001689 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1690 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1691 return;
1692 }
1693 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001694 }
1695
Avi Kivity61d2ef22010-04-28 16:40:38 +03001696 for (i = 0; i < m->nr; ++i)
1697 if (m->guest[i].index == msr)
1698 break;
1699
1700 if (i == m->nr)
1701 return;
1702 --m->nr;
1703 m->guest[i] = m->guest[m->nr];
1704 m->host[i] = m->host[m->nr];
1705 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1706 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1707}
1708
Gleb Natapov2961e8762013-11-25 15:37:13 +02001709static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1710 unsigned long entry, unsigned long exit,
1711 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1712 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001713{
1714 vmcs_write64(guest_val_vmcs, guest_val);
1715 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001716 vm_entry_controls_setbit(vmx, entry);
1717 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001718}
1719
Avi Kivity61d2ef22010-04-28 16:40:38 +03001720static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1721 u64 guest_val, u64 host_val)
1722{
1723 unsigned i;
1724 struct msr_autoload *m = &vmx->msr_autoload;
1725
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001726 switch (msr) {
1727 case MSR_EFER:
1728 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001729 add_atomic_switch_msr_special(vmx,
1730 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001731 VM_EXIT_LOAD_IA32_EFER,
1732 GUEST_IA32_EFER,
1733 HOST_IA32_EFER,
1734 guest_val, host_val);
1735 return;
1736 }
1737 break;
1738 case MSR_CORE_PERF_GLOBAL_CTRL:
1739 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001740 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001741 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1742 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1743 GUEST_IA32_PERF_GLOBAL_CTRL,
1744 HOST_IA32_PERF_GLOBAL_CTRL,
1745 guest_val, host_val);
1746 return;
1747 }
1748 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001749 }
1750
Avi Kivity61d2ef22010-04-28 16:40:38 +03001751 for (i = 0; i < m->nr; ++i)
1752 if (m->guest[i].index == msr)
1753 break;
1754
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001755 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001756 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001757 "Can't add msr %x\n", msr);
1758 return;
1759 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001760 ++m->nr;
1761 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1762 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1763 }
1764
1765 m->guest[i].index = msr;
1766 m->guest[i].value = guest_val;
1767 m->host[i].index = msr;
1768 m->host[i].value = host_val;
1769}
1770
Avi Kivity33ed6322007-05-02 16:54:03 +03001771static void reload_tss(void)
1772{
Avi Kivity33ed6322007-05-02 16:54:03 +03001773 /*
1774 * VT restores TR but not its size. Useless.
1775 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05001776 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001777 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001778
Avi Kivityd3591922010-07-26 18:32:39 +03001779 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001780 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1781 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001782}
1783
Avi Kivity92c0d902009-10-29 11:00:16 +02001784static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001785{
Roel Kluin3a34a882009-08-04 02:08:45 -07001786 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001787 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +03001788
Avi Kivityf6801df2010-01-21 15:31:50 +02001789 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -07001790
Avi Kivity51c6cf62007-08-29 03:48:05 +03001791 /*
Guo Chao0fa06072012-06-28 15:16:19 +08001792 * NX is emulated; LMA and LME handled by hardware; SCE meaningless
Avi Kivity51c6cf62007-08-29 03:48:05 +03001793 * outside long mode
1794 */
1795 ignore_bits = EFER_NX | EFER_SCE;
1796#ifdef CONFIG_X86_64
1797 ignore_bits |= EFER_LMA | EFER_LME;
1798 /* SCE is meaningful only in long mode on Intel */
1799 if (guest_efer & EFER_LMA)
1800 ignore_bits &= ~(u64)EFER_SCE;
1801#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +03001802 guest_efer &= ~ignore_bits;
1803 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +03001804 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +02001805 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03001806
1807 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001808
1809 /*
1810 * On EPT, we can't emulate NX, so we must switch EFER atomically.
1811 * On CPUs that support "load IA32_EFER", always switch EFER
1812 * atomically, since it's faster than switching it manually.
1813 */
1814 if (cpu_has_load_ia32_efer ||
1815 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03001816 guest_efer = vmx->vcpu.arch.efer;
1817 if (!(guest_efer & EFER_LMA))
1818 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08001819 if (guest_efer != host_efer)
1820 add_atomic_switch_msr(vmx, MSR_EFER,
1821 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03001822 return false;
1823 }
1824
Avi Kivity26bb0982009-09-07 11:14:12 +03001825 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001826}
1827
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001828static unsigned long segment_base(u16 selector)
1829{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001830 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001831 struct desc_struct *d;
1832 unsigned long table_base;
1833 unsigned long v;
1834
1835 if (!(selector & ~3))
1836 return 0;
1837
Avi Kivityd3591922010-07-26 18:32:39 +03001838 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001839
1840 if (selector & 4) { /* from ldt */
1841 u16 ldt_selector = kvm_read_ldt();
1842
1843 if (!(ldt_selector & ~3))
1844 return 0;
1845
1846 table_base = segment_base(ldt_selector);
1847 }
1848 d = (struct desc_struct *)(table_base + (selector & ~7));
1849 v = get_desc_base(d);
1850#ifdef CONFIG_X86_64
1851 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
1852 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
1853#endif
1854 return v;
1855}
1856
1857static inline unsigned long kvm_read_tr_base(void)
1858{
1859 u16 tr;
1860 asm("str %0" : "=g"(tr));
1861 return segment_base(tr);
1862}
1863
Avi Kivity04d2cc72007-09-10 18:10:54 +03001864static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001865{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001866 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001867 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001868
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001869 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001870 return;
1871
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001872 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001873 /*
1874 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1875 * allow segment selectors with cpl > 0 or ti == 1.
1876 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001877 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02001878 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02001879 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001880 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001881 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001882 vmx->host_state.fs_reload_needed = 0;
1883 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03001884 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001885 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001886 }
Avi Kivity9581d442010-10-19 16:46:55 +02001887 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001888 if (!(vmx->host_state.gs_sel & 7))
1889 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001890 else {
1891 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001892 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001893 }
1894
1895#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03001896 savesegment(ds, vmx->host_state.ds_sel);
1897 savesegment(es, vmx->host_state.es_sel);
1898#endif
1899
1900#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03001901 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1902 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1903#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001904 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1905 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03001906#endif
Avi Kivity707c0872007-05-02 17:33:43 +03001907
1908#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001909 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1910 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03001911 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03001912#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001913 if (boot_cpu_has(X86_FEATURE_MPX))
1914 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03001915 for (i = 0; i < vmx->save_nmsrs; ++i)
1916 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02001917 vmx->guest_msrs[i].data,
1918 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03001919}
1920
Avi Kivitya9b21b62008-06-24 11:48:49 +03001921static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001922{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001923 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001924 return;
1925
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001926 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001927 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02001928#ifdef CONFIG_X86_64
1929 if (is_long_mode(&vmx->vcpu))
1930 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1931#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001932 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001933 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001934#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02001935 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001936#else
1937 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001938#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001939 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02001940 if (vmx->host_state.fs_reload_needed)
1941 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001942#ifdef CONFIG_X86_64
1943 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
1944 loadsegment(ds, vmx->host_state.ds_sel);
1945 loadsegment(es, vmx->host_state.es_sel);
1946 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001947#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001948 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001949#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001950 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001951#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001952 if (vmx->host_state.msr_host_bndcfgs)
1953 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07001954 /*
1955 * If the FPU is not active (through the host task or
1956 * the guest vcpu), then restore the cr0.TS bit.
1957 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02001958 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07001959 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05001960 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03001961}
1962
Avi Kivitya9b21b62008-06-24 11:48:49 +03001963static void vmx_load_host_state(struct vcpu_vmx *vmx)
1964{
1965 preempt_disable();
1966 __vmx_load_host_state(vmx);
1967 preempt_enable();
1968}
1969
Feng Wu28b835d2015-09-18 22:29:54 +08001970static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
1971{
1972 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
1973 struct pi_desc old, new;
1974 unsigned int dest;
1975
1976 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
1977 !irq_remapping_cap(IRQ_POSTING_CAP))
1978 return;
1979
1980 do {
1981 old.control = new.control = pi_desc->control;
1982
1983 /*
1984 * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there
1985 * are two possible cases:
1986 * 1. After running 'pre_block', context switch
1987 * happened. For this case, 'sn' was set in
1988 * vmx_vcpu_put(), so we need to clear it here.
1989 * 2. After running 'pre_block', we were blocked,
1990 * and woken up by some other guy. For this case,
1991 * we don't need to do anything, 'pi_post_block'
1992 * will do everything for us. However, we cannot
1993 * check whether it is case #1 or case #2 here
1994 * (maybe, not needed), so we also clear sn here,
1995 * I think it is not a big deal.
1996 */
1997 if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) {
1998 if (vcpu->cpu != cpu) {
1999 dest = cpu_physical_id(cpu);
2000
2001 if (x2apic_enabled())
2002 new.ndst = dest;
2003 else
2004 new.ndst = (dest << 8) & 0xFF00;
2005 }
2006
2007 /* set 'NV' to 'notification vector' */
2008 new.nv = POSTED_INTR_VECTOR;
2009 }
2010
2011 /* Allow posting non-urgent interrupts */
2012 new.sn = 0;
2013 } while (cmpxchg(&pi_desc->control, old.control,
2014 new.control) != old.control);
2015}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002016/*
2017 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2018 * vcpu mutex is already taken.
2019 */
Avi Kivity15ad7142007-07-11 18:17:21 +03002020static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002022 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002023 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002024
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002025 if (!vmm_exclusive)
2026 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03002027 else if (vmx->loaded_vmcs->cpu != cpu)
2028 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002029
Nadav Har'Eld462b812011-05-24 15:26:10 +03002030 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2031 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2032 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002033 }
2034
Nadav Har'Eld462b812011-05-24 15:26:10 +03002035 if (vmx->loaded_vmcs->cpu != cpu) {
Christoph Lameter89cbc762014-08-17 12:30:40 -05002036 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037 unsigned long sysenter_esp;
2038
Avi Kivitya8eeb042010-05-10 12:34:53 +03002039 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002040 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002041 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08002042
2043 /*
2044 * Read loaded_vmcs->cpu should be before fetching
2045 * loaded_vmcs->loaded_vmcss_on_cpu_link.
2046 * See the comments in __loaded_vmcs_clear().
2047 */
2048 smp_rmb();
2049
Nadav Har'Eld462b812011-05-24 15:26:10 +03002050 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2051 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002052 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08002053 local_irq_enable();
2054
Avi Kivity6aa8b732006-12-10 02:21:36 -08002055 /*
2056 * Linux uses per-cpu TSS and GDT, so set these when switching
2057 * processors.
2058 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002059 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03002060 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002061
2062 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2063 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Haozhong Zhangff2c3a12015-10-20 15:39:10 +08002064
2065 /* Setup TSC multiplier */
2066 if (cpu_has_vmx_tsc_scaling())
2067 vmcs_write64(TSC_MULTIPLIER,
2068 vcpu->arch.tsc_scaling_ratio);
2069
Nadav Har'Eld462b812011-05-24 15:26:10 +03002070 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071 }
Feng Wu28b835d2015-09-18 22:29:54 +08002072
2073 vmx_vcpu_pi_load(vcpu, cpu);
2074}
2075
2076static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2077{
2078 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2079
2080 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
2081 !irq_remapping_cap(IRQ_POSTING_CAP))
2082 return;
2083
2084 /* Set SN when the vCPU is preempted */
2085 if (vcpu->preempted)
2086 pi_set_sn(pi_desc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002087}
2088
2089static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2090{
Feng Wu28b835d2015-09-18 22:29:54 +08002091 vmx_vcpu_pi_put(vcpu);
2092
Avi Kivitya9b21b62008-06-24 11:48:49 +03002093 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002094 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002095 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
2096 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002097 kvm_cpu_vmxoff();
2098 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099}
2100
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002101static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
2102{
Avi Kivity81231c62010-01-24 16:26:40 +02002103 ulong cr0;
2104
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002105 if (vcpu->fpu_active)
2106 return;
2107 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02002108 cr0 = vmcs_readl(GUEST_CR0);
2109 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
2110 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
2111 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002112 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002113 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002114 if (is_guest_mode(vcpu))
2115 vcpu->arch.cr0_guest_owned_bits &=
2116 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02002117 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002118}
2119
Avi Kivityedcafe32009-12-30 18:07:40 +02002120static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2121
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03002122/*
2123 * Return the cr0 value that a nested guest would read. This is a combination
2124 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
2125 * its hypervisor (cr0_read_shadow).
2126 */
2127static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2128{
2129 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2130 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2131}
2132static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2133{
2134 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2135 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2136}
2137
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002138static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2139{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002140 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2141 * set this *before* calling this function.
2142 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002143 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002144 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002145 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002146 vcpu->arch.cr0_guest_owned_bits = 0;
2147 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002148 if (is_guest_mode(vcpu)) {
2149 /*
2150 * L1's specified read shadow might not contain the TS bit,
2151 * so now that we turned on shadowing of this bit, we need to
2152 * set this bit of the shadow. Like in nested_vmx_run we need
2153 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2154 * up-to-date here because we just decached cr0.TS (and we'll
2155 * only update vmcs12->guest_cr0 on nested exit).
2156 */
2157 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2158 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2159 (vcpu->arch.cr0 & X86_CR0_TS);
2160 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2161 } else
2162 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002163}
2164
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2166{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002167 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002168
Avi Kivity6de12732011-03-07 12:51:22 +02002169 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2170 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2171 rflags = vmcs_readl(GUEST_RFLAGS);
2172 if (to_vmx(vcpu)->rmode.vm86_active) {
2173 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2174 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2175 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2176 }
2177 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002178 }
Avi Kivity6de12732011-03-07 12:51:22 +02002179 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002180}
2181
2182static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2183{
Avi Kivity6de12732011-03-07 12:51:22 +02002184 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2185 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002186 if (to_vmx(vcpu)->rmode.vm86_active) {
2187 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002188 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002189 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190 vmcs_writel(GUEST_RFLAGS, rflags);
2191}
2192
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002193static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002194{
2195 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2196 int ret = 0;
2197
2198 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002199 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002200 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002201 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002202
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002203 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002204}
2205
2206static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2207{
2208 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2209 u32 interruptibility = interruptibility_old;
2210
2211 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2212
Jan Kiszka48005f62010-02-19 19:38:07 +01002213 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002214 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002215 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002216 interruptibility |= GUEST_INTR_STATE_STI;
2217
2218 if ((interruptibility != interruptibility_old))
2219 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2220}
2221
Avi Kivity6aa8b732006-12-10 02:21:36 -08002222static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2223{
2224 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002226 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002228 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002229
Glauber Costa2809f5d2009-05-12 16:21:05 -04002230 /* skipping an emulated instruction also counts */
2231 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002232}
2233
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002234/*
2235 * KVM wants to inject page-faults which it got to the guest. This function
2236 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002237 */
Gleb Natapove011c662013-09-25 12:51:35 +03002238static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002239{
2240 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2241
Gleb Natapove011c662013-09-25 12:51:35 +03002242 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002243 return 0;
2244
Jan Kiszka533558b2014-01-04 18:47:20 +01002245 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
2246 vmcs_read32(VM_EXIT_INTR_INFO),
2247 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002248 return 1;
2249}
2250
Avi Kivity298101d2007-11-25 13:41:11 +02002251static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002252 bool has_error_code, u32 error_code,
2253 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002254{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002255 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002256 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002257
Gleb Natapove011c662013-09-25 12:51:35 +03002258 if (!reinject && is_guest_mode(vcpu) &&
2259 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002260 return;
2261
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002262 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002263 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002264 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2265 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002266
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002267 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002268 int inc_eip = 0;
2269 if (kvm_exception_is_soft(nr))
2270 inc_eip = vcpu->arch.event_exit_inst_len;
2271 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002272 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002273 return;
2274 }
2275
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002276 if (kvm_exception_is_soft(nr)) {
2277 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2278 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002279 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2280 } else
2281 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2282
2283 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002284}
2285
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002286static bool vmx_rdtscp_supported(void)
2287{
2288 return cpu_has_vmx_rdtscp();
2289}
2290
Mao, Junjiead756a12012-07-02 01:18:48 +00002291static bool vmx_invpcid_supported(void)
2292{
2293 return cpu_has_vmx_invpcid() && enable_ept;
2294}
2295
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296/*
Eddie Donga75beee2007-05-17 18:55:15 +03002297 * Swap MSR entry in host/guest MSR entry array.
2298 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002299static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002300{
Avi Kivity26bb0982009-09-07 11:14:12 +03002301 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002302
2303 tmp = vmx->guest_msrs[to];
2304 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2305 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002306}
2307
Yang Zhang8d146952013-01-25 10:18:50 +08002308static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2309{
2310 unsigned long *msr_bitmap;
2311
Wincy Van670125b2015-03-04 14:31:56 +08002312 if (is_guest_mode(vcpu))
2313 msr_bitmap = vmx_msr_bitmap_nested;
Jan Kiszka8a9781f2015-05-04 08:32:32 +02002314 else if (vcpu->arch.apic_base & X2APIC_ENABLE) {
Yang Zhang8d146952013-01-25 10:18:50 +08002315 if (is_long_mode(vcpu))
2316 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2317 else
2318 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
2319 } else {
2320 if (is_long_mode(vcpu))
2321 msr_bitmap = vmx_msr_bitmap_longmode;
2322 else
2323 msr_bitmap = vmx_msr_bitmap_legacy;
2324 }
2325
2326 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2327}
2328
Eddie Donga75beee2007-05-17 18:55:15 +03002329/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002330 * Set up the vmcs to automatically save and restore system
2331 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2332 * mode, as fiddling with msrs is very expensive.
2333 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002334static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002335{
Avi Kivity26bb0982009-09-07 11:14:12 +03002336 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002337
Eddie Donga75beee2007-05-17 18:55:15 +03002338 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002339#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002340 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002341 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002342 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002343 move_msr_up(vmx, index, save_nmsrs++);
2344 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002345 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002346 move_msr_up(vmx, index, save_nmsrs++);
2347 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002348 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002349 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002350 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002351 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002352 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002353 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002354 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002355 * if efer.sce is enabled.
2356 */
Brian Gerst8c065852010-07-17 09:03:26 -04002357 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002358 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002359 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002360 }
Eddie Donga75beee2007-05-17 18:55:15 +03002361#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002362 index = __find_msr_index(vmx, MSR_EFER);
2363 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002364 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002365
Avi Kivity26bb0982009-09-07 11:14:12 +03002366 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002367
Yang Zhang8d146952013-01-25 10:18:50 +08002368 if (cpu_has_vmx_msr_bitmap())
2369 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002370}
2371
2372/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373 * reads and returns guest's timestamp counter "register"
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002374 * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset
2375 * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376 */
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002377static u64 guest_read_tsc(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378{
2379 u64 host_tsc, tsc_offset;
2380
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002381 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382 tsc_offset = vmcs_read64(TSC_OFFSET);
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002383 return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384}
2385
2386/*
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002387 * Like guest_read_tsc, but always returns L1's notion of the timestamp
2388 * counter, even if a nested guest (L2) is currently running.
2389 */
Paolo Bonzini48d89b92014-08-26 13:27:46 +02002390static u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002391{
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002392 u64 tsc_offset;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002393
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002394 tsc_offset = is_guest_mode(vcpu) ?
2395 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
2396 vmcs_read64(TSC_OFFSET);
2397 return host_tsc + tsc_offset;
2398}
2399
Will Auldba904632012-11-29 12:42:50 -08002400static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
2401{
2402 return vmcs_read64(TSC_OFFSET);
2403}
2404
Joerg Roedel4051b182011-03-25 09:44:49 +01002405/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002406 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002408static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002409{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002410 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002411 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002412 * We're here if L1 chose not to trap WRMSR to TSC. According
2413 * to the spec, this should set L1's TSC; The offset that L1
2414 * set for L2 remains unchanged, and still needs to be added
2415 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002416 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002417 struct vmcs12 *vmcs12;
2418 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
2419 /* recalculate vmcs02.TSC_OFFSET: */
2420 vmcs12 = get_vmcs12(vcpu);
2421 vmcs_write64(TSC_OFFSET, offset +
2422 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2423 vmcs12->tsc_offset : 0));
2424 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002425 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2426 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002427 vmcs_write64(TSC_OFFSET, offset);
2428 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429}
2430
Haozhong Zhang58ea6762015-10-20 15:39:06 +08002431static void vmx_adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, s64 adjustment)
Zachary Amsdene48672f2010-08-19 22:07:23 -10002432{
2433 u64 offset = vmcs_read64(TSC_OFFSET);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002434
Zachary Amsdene48672f2010-08-19 22:07:23 -10002435 vmcs_write64(TSC_OFFSET, offset + adjustment);
Nadav Har'El79918252011-05-25 23:15:39 +03002436 if (is_guest_mode(vcpu)) {
2437 /* Even when running L2, the adjustment needs to apply to L1 */
2438 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002439 } else
2440 trace_kvm_write_tsc_offset(vcpu->vcpu_id, offset,
2441 offset + adjustment);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002442}
2443
Nadav Har'El801d3422011-05-25 23:02:23 +03002444static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2445{
2446 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2447 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2448}
2449
2450/*
2451 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2452 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2453 * all guests if the "nested" module option is off, and can also be disabled
2454 * for a single guest by disabling its VMX cpuid bit.
2455 */
2456static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2457{
2458 return nested && guest_cpuid_has_vmx(vcpu);
2459}
2460
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002462 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2463 * returned for the various VMX controls MSRs when nested VMX is enabled.
2464 * The same values should also be used to verify that vmcs12 control fields are
2465 * valid during nested entry from L1 to L2.
2466 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2467 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2468 * bit in the high half is on if the corresponding bit in the control field
2469 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002470 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002471static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002472{
2473 /*
2474 * Note that as a general rule, the high half of the MSRs (bits in
2475 * the control fields which may be 1) should be initialized by the
2476 * intersection of the underlying hardware's MSR (i.e., features which
2477 * can be supported) and the list of features we want to expose -
2478 * because they are known to be properly supported in our code.
2479 * Also, usually, the low half of the MSRs (bits which must be 1) can
2480 * be set to 0, meaning that L1 may turn off any of these bits. The
2481 * reason is that if one of these bits is necessary, it will appear
2482 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2483 * fields of vmcs01 and vmcs02, will turn these bits off - and
2484 * nested_vmx_exit_handled() will not pass related exits to L1.
2485 * These rules have exceptions below.
2486 */
2487
2488 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002489 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002490 vmx->nested.nested_vmx_pinbased_ctls_low,
2491 vmx->nested.nested_vmx_pinbased_ctls_high);
2492 vmx->nested.nested_vmx_pinbased_ctls_low |=
2493 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2494 vmx->nested.nested_vmx_pinbased_ctls_high &=
2495 PIN_BASED_EXT_INTR_MASK |
2496 PIN_BASED_NMI_EXITING |
2497 PIN_BASED_VIRTUAL_NMIS;
2498 vmx->nested.nested_vmx_pinbased_ctls_high |=
2499 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002500 PIN_BASED_VMX_PREEMPTION_TIMER;
Paolo Bonzini35754c92015-07-29 12:05:37 +02002501 if (vmx_cpu_uses_apicv(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002502 vmx->nested.nested_vmx_pinbased_ctls_high |=
2503 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002504
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002505 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002506 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002507 vmx->nested.nested_vmx_exit_ctls_low,
2508 vmx->nested.nested_vmx_exit_ctls_high);
2509 vmx->nested.nested_vmx_exit_ctls_low =
2510 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002511
Wincy Vanb9c237b2015-02-03 23:56:30 +08002512 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002513#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002514 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002515#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002516 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002517 vmx->nested.nested_vmx_exit_ctls_high |=
2518 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002519 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002520 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2521
Paolo Bonzini36be0b92014-02-24 12:30:04 +01002522 if (vmx_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002523 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002524
Jan Kiszka2996fca2014-06-16 13:59:43 +02002525 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002526 vmx->nested.nested_vmx_true_exit_ctls_low =
2527 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002528 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2529
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002530 /* entry controls */
2531 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002532 vmx->nested.nested_vmx_entry_ctls_low,
2533 vmx->nested.nested_vmx_entry_ctls_high);
2534 vmx->nested.nested_vmx_entry_ctls_low =
2535 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2536 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002537#ifdef CONFIG_X86_64
2538 VM_ENTRY_IA32E_MODE |
2539#endif
2540 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002541 vmx->nested.nested_vmx_entry_ctls_high |=
2542 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzini36be0b92014-02-24 12:30:04 +01002543 if (vmx_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002544 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002545
Jan Kiszka2996fca2014-06-16 13:59:43 +02002546 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002547 vmx->nested.nested_vmx_true_entry_ctls_low =
2548 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002549 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2550
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002551 /* cpu-based controls */
2552 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002553 vmx->nested.nested_vmx_procbased_ctls_low,
2554 vmx->nested.nested_vmx_procbased_ctls_high);
2555 vmx->nested.nested_vmx_procbased_ctls_low =
2556 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2557 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002558 CPU_BASED_VIRTUAL_INTR_PENDING |
2559 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002560 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2561 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2562 CPU_BASED_CR3_STORE_EXITING |
2563#ifdef CONFIG_X86_64
2564 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2565#endif
2566 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002567 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2568 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2569 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2570 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002571 /*
2572 * We can allow some features even when not supported by the
2573 * hardware. For example, L1 can specify an MSR bitmap - and we
2574 * can use it to avoid exits to L1 - even when L0 runs L2
2575 * without MSR bitmaps.
2576 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002577 vmx->nested.nested_vmx_procbased_ctls_high |=
2578 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002579 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002580
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002581 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002582 vmx->nested.nested_vmx_true_procbased_ctls_low =
2583 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002584 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2585
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002586 /* secondary cpu-based controls */
2587 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002588 vmx->nested.nested_vmx_secondary_ctls_low,
2589 vmx->nested.nested_vmx_secondary_ctls_high);
2590 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2591 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002592 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002593 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002594 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wanpeng Li5c614b32015-10-13 09:18:36 -07002595 SECONDARY_EXEC_ENABLE_VPID |
Wincy Van82f0dd42015-02-03 23:57:18 +08002596 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002597 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002598 SECONDARY_EXEC_WBINVD_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08002599 SECONDARY_EXEC_XSAVES |
2600 SECONDARY_EXEC_PCOMMIT;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002601
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002602 if (enable_ept) {
2603 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002604 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002605 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002606 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002607 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2608 VMX_EPT_INVEPT_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002609 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002610 /*
Bandan Das4b855072014-04-19 18:17:44 -04002611 * For nested guests, we don't do anything specific
2612 * for single context invalidation. Hence, only advertise
2613 * support for global context invalidation.
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002614 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002615 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002616 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002617 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002618
Wanpeng Li089d7b62015-10-13 09:18:37 -07002619 if (enable_vpid)
2620 vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT |
2621 VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
2622 else
2623 vmx->nested.nested_vmx_vpid_caps = 0;
Wanpeng Li99b83ac2015-10-13 09:12:21 -07002624
Radim Krčmář0790ec12015-03-17 14:02:32 +01002625 if (enable_unrestricted_guest)
2626 vmx->nested.nested_vmx_secondary_ctls_high |=
2627 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2628
Jan Kiszkac18911a2013-03-13 16:06:41 +01002629 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002630 rdmsr(MSR_IA32_VMX_MISC,
2631 vmx->nested.nested_vmx_misc_low,
2632 vmx->nested.nested_vmx_misc_high);
2633 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2634 vmx->nested.nested_vmx_misc_low |=
2635 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002636 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002637 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002638}
2639
2640static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2641{
2642 /*
2643 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2644 */
2645 return ((control & high) | low) == control;
2646}
2647
2648static inline u64 vmx_control_msr(u32 low, u32 high)
2649{
2650 return low | ((u64)high << 32);
2651}
2652
Jan Kiszkacae50132014-01-04 18:47:22 +01002653/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002654static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2655{
Wincy Vanb9c237b2015-02-03 23:56:30 +08002656 struct vcpu_vmx *vmx = to_vmx(vcpu);
2657
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002658 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002659 case MSR_IA32_VMX_BASIC:
2660 /*
2661 * This MSR reports some information about VMX support. We
2662 * should return information about the VMX we emulate for the
2663 * guest, and the VMCS structure we give it - not about the
2664 * VMX support of the underlying hardware.
2665 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002666 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002667 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2668 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2669 break;
2670 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2671 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002672 *pdata = vmx_control_msr(
2673 vmx->nested.nested_vmx_pinbased_ctls_low,
2674 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002675 break;
2676 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002677 *pdata = vmx_control_msr(
2678 vmx->nested.nested_vmx_true_procbased_ctls_low,
2679 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002680 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002681 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002682 *pdata = vmx_control_msr(
2683 vmx->nested.nested_vmx_procbased_ctls_low,
2684 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002685 break;
2686 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002687 *pdata = vmx_control_msr(
2688 vmx->nested.nested_vmx_true_exit_ctls_low,
2689 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002690 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002691 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002692 *pdata = vmx_control_msr(
2693 vmx->nested.nested_vmx_exit_ctls_low,
2694 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002695 break;
2696 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002697 *pdata = vmx_control_msr(
2698 vmx->nested.nested_vmx_true_entry_ctls_low,
2699 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002700 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002701 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002702 *pdata = vmx_control_msr(
2703 vmx->nested.nested_vmx_entry_ctls_low,
2704 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002705 break;
2706 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002707 *pdata = vmx_control_msr(
2708 vmx->nested.nested_vmx_misc_low,
2709 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002710 break;
2711 /*
2712 * These MSRs specify bits which the guest must keep fixed (on or off)
2713 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2714 * We picked the standard core2 setting.
2715 */
2716#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2717#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2718 case MSR_IA32_VMX_CR0_FIXED0:
2719 *pdata = VMXON_CR0_ALWAYSON;
2720 break;
2721 case MSR_IA32_VMX_CR0_FIXED1:
2722 *pdata = -1ULL;
2723 break;
2724 case MSR_IA32_VMX_CR4_FIXED0:
2725 *pdata = VMXON_CR4_ALWAYSON;
2726 break;
2727 case MSR_IA32_VMX_CR4_FIXED1:
2728 *pdata = -1ULL;
2729 break;
2730 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02002731 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002732 break;
2733 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002734 *pdata = vmx_control_msr(
2735 vmx->nested.nested_vmx_secondary_ctls_low,
2736 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002737 break;
2738 case MSR_IA32_VMX_EPT_VPID_CAP:
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002739 /* Currently, no nested vpid support */
Wanpeng Li089d7b62015-10-13 09:18:37 -07002740 *pdata = vmx->nested.nested_vmx_ept_caps |
2741 ((u64)vmx->nested.nested_vmx_vpid_caps << 32);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002742 break;
2743 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002744 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002745 }
2746
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002747 return 0;
2748}
2749
2750/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 * Reads an msr value (of 'msr_index') into 'pdata'.
2752 * Returns 0 on success, non-0 otherwise.
2753 * Assumes vcpu_load() was already called.
2754 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002755static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756{
Avi Kivity26bb0982009-09-07 11:14:12 +03002757 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002759 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002760#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002762 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002763 break;
2764 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002765 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002767 case MSR_KERNEL_GS_BASE:
2768 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002769 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002770 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002771#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002773 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302774 case MSR_IA32_TSC:
Haozhong Zhangbe7b2632015-10-20 15:39:11 +08002775 msr_info->data = guest_read_tsc(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776 break;
2777 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002778 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779 break;
2780 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002781 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782 break;
2783 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002784 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002786 case MSR_IA32_BNDCFGS:
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002787 if (!vmx_mpx_supported())
2788 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002789 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002790 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002791 case MSR_IA32_FEATURE_CONTROL:
2792 if (!nested_vmx_allowed(vcpu))
2793 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002794 msr_info->data = to_vmx(vcpu)->nested.msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01002795 break;
2796 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2797 if (!nested_vmx_allowed(vcpu))
2798 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002799 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08002800 case MSR_IA32_XSS:
2801 if (!vmx_xsaves_supported())
2802 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002803 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08002804 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002805 case MSR_TSC_AUX:
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002806 if (!guest_cpuid_has_rdtscp(vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002807 return 1;
2808 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002810 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002811 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002812 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08002813 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002815 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816 }
2817
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 return 0;
2819}
2820
Jan Kiszkacae50132014-01-04 18:47:22 +01002821static void vmx_leave_nested(struct kvm_vcpu *vcpu);
2822
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823/*
2824 * Writes msr value into into the appropriate "register".
2825 * Returns 0 on success, non-0 otherwise.
2826 * Assumes vcpu_load() was already called.
2827 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002828static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002830 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002831 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002832 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002833 u32 msr_index = msr_info->index;
2834 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03002835
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002837 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002838 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002839 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002840#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002841 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002842 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 vmcs_writel(GUEST_FS_BASE, data);
2844 break;
2845 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002846 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847 vmcs_writel(GUEST_GS_BASE, data);
2848 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002849 case MSR_KERNEL_GS_BASE:
2850 vmx_load_host_state(vmx);
2851 vmx->msr_guest_kernel_gs_base = data;
2852 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853#endif
2854 case MSR_IA32_SYSENTER_CS:
2855 vmcs_write32(GUEST_SYSENTER_CS, data);
2856 break;
2857 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002858 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859 break;
2860 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002861 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002863 case MSR_IA32_BNDCFGS:
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002864 if (!vmx_mpx_supported())
2865 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002866 vmcs_write64(GUEST_BNDCFGS, data);
2867 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302868 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08002869 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002871 case MSR_IA32_CR_PAT:
2872 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03002873 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2874 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08002875 vmcs_write64(GUEST_IA32_PAT, data);
2876 vcpu->arch.pat = data;
2877 break;
2878 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002879 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002880 break;
Will Auldba904632012-11-29 12:42:50 -08002881 case MSR_IA32_TSC_ADJUST:
2882 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002883 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002884 case MSR_IA32_FEATURE_CONTROL:
2885 if (!nested_vmx_allowed(vcpu) ||
2886 (to_vmx(vcpu)->nested.msr_ia32_feature_control &
2887 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
2888 return 1;
2889 vmx->nested.msr_ia32_feature_control = data;
2890 if (msr_info->host_initiated && data == 0)
2891 vmx_leave_nested(vcpu);
2892 break;
2893 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2894 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08002895 case MSR_IA32_XSS:
2896 if (!vmx_xsaves_supported())
2897 return 1;
2898 /*
2899 * The only supported bit as of Skylake is bit 8, but
2900 * it is not supported on KVM.
2901 */
2902 if (data != 0)
2903 return 1;
2904 vcpu->arch.ia32_xss = data;
2905 if (vcpu->arch.ia32_xss != host_xss)
2906 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
2907 vcpu->arch.ia32_xss, host_xss);
2908 else
2909 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
2910 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002911 case MSR_TSC_AUX:
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002912 if (!guest_cpuid_has_rdtscp(vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002913 return 1;
2914 /* Check reserved bit, higher 32 bits should be zero */
2915 if ((data >> 32) != 0)
2916 return 1;
2917 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002918 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002919 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002920 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07002921 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08002922 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002923 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2924 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07002925 ret = kvm_set_shared_msr(msr->index, msr->data,
2926 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03002927 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07002928 if (ret)
2929 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002930 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002931 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002932 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002933 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934 }
2935
Eddie Dong2cc51562007-05-21 07:28:09 +03002936 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002937}
2938
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002939static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002940{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002941 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2942 switch (reg) {
2943 case VCPU_REGS_RSP:
2944 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2945 break;
2946 case VCPU_REGS_RIP:
2947 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2948 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002949 case VCPU_EXREG_PDPTR:
2950 if (enable_ept)
2951 ept_save_pdptrs(vcpu);
2952 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002953 default:
2954 break;
2955 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956}
2957
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958static __init int cpu_has_kvm_support(void)
2959{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002960 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961}
2962
2963static __init int vmx_disabled_by_bios(void)
2964{
2965 u64 msr;
2966
2967 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04002968 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08002969 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04002970 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2971 && tboot_enabled())
2972 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08002973 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04002974 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08002975 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08002976 && !tboot_enabled()) {
2977 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08002978 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04002979 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08002980 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08002981 /* launched w/o TXT and VMX disabled */
2982 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2983 && !tboot_enabled())
2984 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04002985 }
2986
2987 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002988}
2989
Dongxiao Xu7725b892010-05-11 18:29:38 +08002990static void kvm_cpu_vmxon(u64 addr)
2991{
2992 asm volatile (ASM_VMX_VMXON_RAX
2993 : : "a"(&addr), "m"(addr)
2994 : "memory", "cc");
2995}
2996
Radim Krčmář13a34e02014-08-28 15:13:03 +02002997static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998{
2999 int cpu = raw_smp_processor_id();
3000 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04003001 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003002
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07003003 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02003004 return -EBUSY;
3005
Nadav Har'Eld462b812011-05-24 15:26:10 +03003006 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Feng Wubf9f6ac2015-09-18 22:29:55 +08003007 INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3008 spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08003009
3010 /*
3011 * Now we can enable the vmclear operation in kdump
3012 * since the loaded_vmcss_on_cpu list on this cpu
3013 * has been initialized.
3014 *
3015 * Though the cpu is not in VMX operation now, there
3016 * is no problem to enable the vmclear operation
3017 * for the loaded_vmcss_on_cpu list is empty!
3018 */
3019 crash_enable_local_vmclear(cpu);
3020
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04003022
3023 test_bits = FEATURE_CONTROL_LOCKED;
3024 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3025 if (tboot_enabled())
3026 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3027
3028 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003029 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04003030 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3031 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003032 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02003033
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003034 if (vmm_exclusive) {
3035 kvm_cpu_vmxon(phys_addr);
3036 ept_sync_global();
3037 }
Alexander Graf10474ae2009-09-15 11:37:46 +02003038
Christoph Lameter89cbc762014-08-17 12:30:40 -05003039 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03003040
Alexander Graf10474ae2009-09-15 11:37:46 +02003041 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042}
3043
Nadav Har'Eld462b812011-05-24 15:26:10 +03003044static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03003045{
3046 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03003047 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03003048
Nadav Har'Eld462b812011-05-24 15:26:10 +03003049 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3050 loaded_vmcss_on_cpu_link)
3051 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03003052}
3053
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003054
3055/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3056 * tricks.
3057 */
3058static void kvm_cpu_vmxoff(void)
3059{
3060 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02003061}
3062
Radim Krčmář13a34e02014-08-28 15:13:03 +02003063static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003065 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03003066 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08003067 kvm_cpu_vmxoff();
3068 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07003069 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003070}
3071
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003072static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04003073 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003074{
3075 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003076 u32 ctl = ctl_min | ctl_opt;
3077
3078 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3079
3080 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3081 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3082
3083 /* Ensure minimum (required) set of control bits are supported. */
3084 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003085 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003086
3087 *result = ctl;
3088 return 0;
3089}
3090
Avi Kivity110312c2010-12-21 12:54:20 +02003091static __init bool allow_1_setting(u32 msr, u32 ctl)
3092{
3093 u32 vmx_msr_low, vmx_msr_high;
3094
3095 rdmsr(msr, vmx_msr_low, vmx_msr_high);
3096 return vmx_msr_high & ctl;
3097}
3098
Yang, Sheng002c7f72007-07-31 14:23:01 +03003099static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003100{
3101 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08003102 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003103 u32 _pin_based_exec_control = 0;
3104 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003105 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003106 u32 _vmexit_control = 0;
3107 u32 _vmentry_control = 0;
3108
Raghavendra K T10166742012-02-07 23:19:20 +05303109 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003110#ifdef CONFIG_X86_64
3111 CPU_BASED_CR8_LOAD_EXITING |
3112 CPU_BASED_CR8_STORE_EXITING |
3113#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08003114 CPU_BASED_CR3_LOAD_EXITING |
3115 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003116 CPU_BASED_USE_IO_BITMAPS |
3117 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003118 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003119 CPU_BASED_MWAIT_EXITING |
3120 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003121 CPU_BASED_INVLPG_EXITING |
3122 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003123
Sheng Yangf78e0e22007-10-29 09:40:42 +08003124 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003125 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003126 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003127 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3128 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003129 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003130#ifdef CONFIG_X86_64
3131 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3132 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3133 ~CPU_BASED_CR8_STORE_EXITING;
3134#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003135 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003136 min2 = 0;
3137 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003138 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003139 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003140 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003141 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003142 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003143 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003144 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003145 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003146 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003147 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003148 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003149 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003150 SECONDARY_EXEC_ENABLE_PML |
Haozhong Zhang64903d62015-10-20 15:39:09 +08003151 SECONDARY_EXEC_PCOMMIT |
3152 SECONDARY_EXEC_TSC_SCALING;
Sheng Yangd56f5462008-04-25 10:13:16 +08003153 if (adjust_vmx_controls(min2, opt2,
3154 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003155 &_cpu_based_2nd_exec_control) < 0)
3156 return -EIO;
3157 }
3158#ifndef CONFIG_X86_64
3159 if (!(_cpu_based_2nd_exec_control &
3160 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3161 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3162#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003163
3164 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3165 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003166 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003167 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3168 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003169
Sheng Yangd56f5462008-04-25 10:13:16 +08003170 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003171 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3172 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003173 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3174 CPU_BASED_CR3_STORE_EXITING |
3175 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003176 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3177 vmx_capability.ept, vmx_capability.vpid);
3178 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003179
Paolo Bonzini81908bf2014-02-21 10:32:27 +01003180 min = VM_EXIT_SAVE_DEBUG_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003181#ifdef CONFIG_X86_64
3182 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3183#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003184 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003185 VM_EXIT_ACK_INTR_ON_EXIT | VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003186 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3187 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003188 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003189
Yang Zhang01e439b2013-04-11 19:25:12 +08003190 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3191 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR;
3192 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3193 &_pin_based_exec_control) < 0)
3194 return -EIO;
3195
3196 if (!(_cpu_based_2nd_exec_control &
3197 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) ||
3198 !(_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT))
3199 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3200
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003201 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003202 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003203 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3204 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003205 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003207 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003208
3209 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3210 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003211 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003212
3213#ifdef CONFIG_X86_64
3214 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3215 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003216 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003217#endif
3218
3219 /* Require Write-Back (WB) memory type for VMCS accesses. */
3220 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003221 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003222
Yang, Sheng002c7f72007-07-31 14:23:01 +03003223 vmcs_conf->size = vmx_msr_high & 0x1fff;
3224 vmcs_conf->order = get_order(vmcs_config.size);
3225 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003226
Yang, Sheng002c7f72007-07-31 14:23:01 +03003227 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3228 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003229 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003230 vmcs_conf->vmexit_ctrl = _vmexit_control;
3231 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003232
Avi Kivity110312c2010-12-21 12:54:20 +02003233 cpu_has_load_ia32_efer =
3234 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3235 VM_ENTRY_LOAD_IA32_EFER)
3236 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3237 VM_EXIT_LOAD_IA32_EFER);
3238
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003239 cpu_has_load_perf_global_ctrl =
3240 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3241 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3242 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3243 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3244
3245 /*
3246 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
3247 * but due to arrata below it can't be used. Workaround is to use
3248 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3249 *
3250 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3251 *
3252 * AAK155 (model 26)
3253 * AAP115 (model 30)
3254 * AAT100 (model 37)
3255 * BC86,AAY89,BD102 (model 44)
3256 * BA97 (model 46)
3257 *
3258 */
3259 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3260 switch (boot_cpu_data.x86_model) {
3261 case 26:
3262 case 30:
3263 case 37:
3264 case 44:
3265 case 46:
3266 cpu_has_load_perf_global_ctrl = false;
3267 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3268 "does not work properly. Using workaround\n");
3269 break;
3270 default:
3271 break;
3272 }
3273 }
3274
Wanpeng Li20300092014-12-02 19:14:59 +08003275 if (cpu_has_xsaves)
3276 rdmsrl(MSR_IA32_XSS, host_xss);
3277
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003278 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003279}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003280
3281static struct vmcs *alloc_vmcs_cpu(int cpu)
3282{
3283 int node = cpu_to_node(cpu);
3284 struct page *pages;
3285 struct vmcs *vmcs;
3286
Vlastimil Babka96db8002015-09-08 15:03:50 -07003287 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003288 if (!pages)
3289 return NULL;
3290 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003291 memset(vmcs, 0, vmcs_config.size);
3292 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293 return vmcs;
3294}
3295
3296static struct vmcs *alloc_vmcs(void)
3297{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003298 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003299}
3300
3301static void free_vmcs(struct vmcs *vmcs)
3302{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003303 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003304}
3305
Nadav Har'Eld462b812011-05-24 15:26:10 +03003306/*
3307 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3308 */
3309static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3310{
3311 if (!loaded_vmcs->vmcs)
3312 return;
3313 loaded_vmcs_clear(loaded_vmcs);
3314 free_vmcs(loaded_vmcs->vmcs);
3315 loaded_vmcs->vmcs = NULL;
3316}
3317
Sam Ravnborg39959582007-06-01 00:47:13 -07003318static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003319{
3320 int cpu;
3321
Zachary Amsden3230bb42009-09-29 11:38:37 -10003322 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003323 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003324 per_cpu(vmxarea, cpu) = NULL;
3325 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003326}
3327
Bandan Dasfe2b2012014-04-21 15:20:14 -04003328static void init_vmcs_shadow_fields(void)
3329{
3330 int i, j;
3331
3332 /* No checks for read only fields yet */
3333
3334 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3335 switch (shadow_read_write_fields[i]) {
3336 case GUEST_BNDCFGS:
3337 if (!vmx_mpx_supported())
3338 continue;
3339 break;
3340 default:
3341 break;
3342 }
3343
3344 if (j < i)
3345 shadow_read_write_fields[j] =
3346 shadow_read_write_fields[i];
3347 j++;
3348 }
3349 max_shadow_read_write_fields = j;
3350
3351 /* shadowed fields guest access without vmexit */
3352 for (i = 0; i < max_shadow_read_write_fields; i++) {
3353 clear_bit(shadow_read_write_fields[i],
3354 vmx_vmwrite_bitmap);
3355 clear_bit(shadow_read_write_fields[i],
3356 vmx_vmread_bitmap);
3357 }
3358 for (i = 0; i < max_shadow_read_only_fields; i++)
3359 clear_bit(shadow_read_only_fields[i],
3360 vmx_vmread_bitmap);
3361}
3362
Avi Kivity6aa8b732006-12-10 02:21:36 -08003363static __init int alloc_kvm_area(void)
3364{
3365 int cpu;
3366
Zachary Amsden3230bb42009-09-29 11:38:37 -10003367 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003368 struct vmcs *vmcs;
3369
3370 vmcs = alloc_vmcs_cpu(cpu);
3371 if (!vmcs) {
3372 free_kvm_area();
3373 return -ENOMEM;
3374 }
3375
3376 per_cpu(vmxarea, cpu) = vmcs;
3377 }
3378 return 0;
3379}
3380
Gleb Natapov14168782013-01-21 15:36:49 +02003381static bool emulation_required(struct kvm_vcpu *vcpu)
3382{
3383 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3384}
3385
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003386static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003387 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003388{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003389 if (!emulate_invalid_guest_state) {
3390 /*
3391 * CS and SS RPL should be equal during guest entry according
3392 * to VMX spec, but in reality it is not always so. Since vcpu
3393 * is in the middle of the transition from real mode to
3394 * protected mode it is safe to assume that RPL 0 is a good
3395 * default value.
3396 */
3397 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003398 save->selector &= ~SEGMENT_RPL_MASK;
3399 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003400 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003401 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003402 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003403}
3404
3405static void enter_pmode(struct kvm_vcpu *vcpu)
3406{
3407 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003408 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003409
Gleb Natapovd99e4152012-12-20 16:57:45 +02003410 /*
3411 * Update real mode segment cache. It may be not up-to-date if sement
3412 * register was written while vcpu was in a guest mode.
3413 */
3414 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3415 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3416 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3417 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3418 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3419 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3420
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003421 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003422
Avi Kivity2fb92db2011-04-27 19:42:18 +03003423 vmx_segment_cache_clear(vmx);
3424
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003425 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426
3427 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003428 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3429 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003430 vmcs_writel(GUEST_RFLAGS, flags);
3431
Rusty Russell66aee912007-07-17 23:34:16 +10003432 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3433 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003434
3435 update_exception_bitmap(vcpu);
3436
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003437 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3438 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3439 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3440 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3441 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3442 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443}
3444
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003445static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003446{
Mathias Krause772e0312012-08-30 01:30:19 +02003447 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003448 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003449
Gleb Natapovd99e4152012-12-20 16:57:45 +02003450 var.dpl = 0x3;
3451 if (seg == VCPU_SREG_CS)
3452 var.type = 0x3;
3453
3454 if (!emulate_invalid_guest_state) {
3455 var.selector = var.base >> 4;
3456 var.base = var.base & 0xffff0;
3457 var.limit = 0xffff;
3458 var.g = 0;
3459 var.db = 0;
3460 var.present = 1;
3461 var.s = 1;
3462 var.l = 0;
3463 var.unusable = 0;
3464 var.type = 0x3;
3465 var.avl = 0;
3466 if (save->base & 0xf)
3467 printk_once(KERN_WARNING "kvm: segment base is not "
3468 "paragraph aligned when entering "
3469 "protected mode (seg=%d)", seg);
3470 }
3471
3472 vmcs_write16(sf->selector, var.selector);
3473 vmcs_write32(sf->base, var.base);
3474 vmcs_write32(sf->limit, var.limit);
3475 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476}
3477
3478static void enter_rmode(struct kvm_vcpu *vcpu)
3479{
3480 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003481 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003482
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003483 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3484 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3485 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3486 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3487 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003488 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3489 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003490
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003491 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492
Gleb Natapov776e58e2011-03-13 12:34:27 +02003493 /*
3494 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003495 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003496 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003497 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003498 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3499 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003500
Avi Kivity2fb92db2011-04-27 19:42:18 +03003501 vmx_segment_cache_clear(vmx);
3502
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003503 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003504 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3506
3507 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003508 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003509
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003510 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003511
3512 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003513 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003514 update_exception_bitmap(vcpu);
3515
Gleb Natapovd99e4152012-12-20 16:57:45 +02003516 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3517 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3518 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3519 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3520 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3521 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003522
Eddie Dong8668a3c2007-10-10 14:26:45 +08003523 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003524}
3525
Amit Shah401d10d2009-02-20 22:53:37 +05303526static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3527{
3528 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003529 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3530
3531 if (!msr)
3532 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303533
Avi Kivity44ea2b12009-09-06 15:55:37 +03003534 /*
3535 * Force kernel_gs_base reloading before EFER changes, as control
3536 * of this msr depends on is_long_mode().
3537 */
3538 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003539 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303540 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003541 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303542 msr->data = efer;
3543 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003544 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303545
3546 msr->data = efer & ~EFER_LME;
3547 }
3548 setup_msrs(vmx);
3549}
3550
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003551#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003552
3553static void enter_lmode(struct kvm_vcpu *vcpu)
3554{
3555 u32 guest_tr_ar;
3556
Avi Kivity2fb92db2011-04-27 19:42:18 +03003557 vmx_segment_cache_clear(to_vmx(vcpu));
3558
Avi Kivity6aa8b732006-12-10 02:21:36 -08003559 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003560 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003561 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3562 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003563 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003564 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3565 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003566 }
Avi Kivityda38f432010-07-06 11:30:49 +03003567 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003568}
3569
3570static void exit_lmode(struct kvm_vcpu *vcpu)
3571{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003572 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003573 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003574}
3575
3576#endif
3577
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003578static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003579{
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003580 vpid_sync_context(vpid);
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003581 if (enable_ept) {
3582 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3583 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003584 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003585 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003586}
3587
Wanpeng Lidd5f5342015-09-23 18:26:57 +08003588static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3589{
3590 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid);
3591}
3592
Avi Kivitye8467fd2009-12-29 18:43:06 +02003593static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3594{
3595 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3596
3597 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3598 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3599}
3600
Avi Kivityaff48ba2010-12-05 18:56:11 +02003601static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3602{
3603 if (enable_ept && is_paging(vcpu))
3604 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3605 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3606}
3607
Anthony Liguori25c4c272007-04-27 09:29:21 +03003608static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003609{
Avi Kivityfc78f512009-12-07 12:16:48 +02003610 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3611
3612 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3613 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003614}
3615
Sheng Yang14394422008-04-28 12:24:45 +08003616static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3617{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003618 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3619
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003620 if (!test_bit(VCPU_EXREG_PDPTR,
3621 (unsigned long *)&vcpu->arch.regs_dirty))
3622 return;
3623
Sheng Yang14394422008-04-28 12:24:45 +08003624 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003625 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3626 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3627 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3628 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003629 }
3630}
3631
Avi Kivity8f5d5492009-05-31 18:41:29 +03003632static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3633{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003634 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3635
Avi Kivity8f5d5492009-05-31 18:41:29 +03003636 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003637 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3638 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3639 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3640 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003641 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003642
3643 __set_bit(VCPU_EXREG_PDPTR,
3644 (unsigned long *)&vcpu->arch.regs_avail);
3645 __set_bit(VCPU_EXREG_PDPTR,
3646 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003647}
3648
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003649static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003650
3651static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3652 unsigned long cr0,
3653 struct kvm_vcpu *vcpu)
3654{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003655 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3656 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003657 if (!(cr0 & X86_CR0_PG)) {
3658 /* From paging/starting to nonpaging */
3659 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003660 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003661 (CPU_BASED_CR3_LOAD_EXITING |
3662 CPU_BASED_CR3_STORE_EXITING));
3663 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003664 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003665 } else if (!is_paging(vcpu)) {
3666 /* From nonpaging to paging */
3667 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003668 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003669 ~(CPU_BASED_CR3_LOAD_EXITING |
3670 CPU_BASED_CR3_STORE_EXITING));
3671 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003672 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003673 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003674
3675 if (!(cr0 & X86_CR0_WP))
3676 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003677}
3678
Avi Kivity6aa8b732006-12-10 02:21:36 -08003679static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3680{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003681 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003682 unsigned long hw_cr0;
3683
Gleb Natapov50378782013-02-04 16:00:28 +02003684 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003685 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003686 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003687 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003688 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003689
Gleb Natapov218e7632013-01-21 15:36:45 +02003690 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3691 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003692
Gleb Natapov218e7632013-01-21 15:36:45 +02003693 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3694 enter_rmode(vcpu);
3695 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003696
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003697#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003698 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003699 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003700 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003701 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003702 exit_lmode(vcpu);
3703 }
3704#endif
3705
Avi Kivity089d0342009-03-23 18:26:32 +02003706 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003707 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3708
Avi Kivity02daab22009-12-30 12:40:26 +02003709 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003710 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003711
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003713 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003714 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003715
3716 /* depends on vcpu->arch.cr0 to be set to a new value */
3717 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718}
3719
Sheng Yang14394422008-04-28 12:24:45 +08003720static u64 construct_eptp(unsigned long root_hpa)
3721{
3722 u64 eptp;
3723
3724 /* TODO write the value reading from MSR */
3725 eptp = VMX_EPT_DEFAULT_MT |
3726 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003727 if (enable_ept_ad_bits)
3728 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003729 eptp |= (root_hpa & PAGE_MASK);
3730
3731 return eptp;
3732}
3733
Avi Kivity6aa8b732006-12-10 02:21:36 -08003734static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3735{
Sheng Yang14394422008-04-28 12:24:45 +08003736 unsigned long guest_cr3;
3737 u64 eptp;
3738
3739 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003740 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003741 eptp = construct_eptp(cr3);
3742 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02003743 if (is_paging(vcpu) || is_guest_mode(vcpu))
3744 guest_cr3 = kvm_read_cr3(vcpu);
3745 else
3746 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02003747 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003748 }
3749
Sheng Yang2384d2b2008-01-17 15:14:33 +08003750 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003751 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003752}
3753
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003754static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003755{
Ben Serebrin085e68e2015-04-16 11:58:05 -07003756 /*
3757 * Pass through host's Machine Check Enable value to hw_cr4, which
3758 * is in force while we are in guest mode. Do not let guests control
3759 * this bit, even if host CR4.MCE == 0.
3760 */
3761 unsigned long hw_cr4 =
3762 (cr4_read_shadow() & X86_CR4_MCE) |
3763 (cr4 & ~X86_CR4_MCE) |
3764 (to_vmx(vcpu)->rmode.vm86_active ?
3765 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08003766
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003767 if (cr4 & X86_CR4_VMXE) {
3768 /*
3769 * To use VMXON (and later other VMX instructions), a guest
3770 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3771 * So basically the check on whether to allow nested VMX
3772 * is here.
3773 */
3774 if (!nested_vmx_allowed(vcpu))
3775 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003776 }
3777 if (to_vmx(vcpu)->nested.vmxon &&
3778 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003779 return 1;
3780
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003781 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02003782 if (enable_ept) {
3783 if (!is_paging(vcpu)) {
3784 hw_cr4 &= ~X86_CR4_PAE;
3785 hw_cr4 |= X86_CR4_PSE;
3786 } else if (!(cr4 & X86_CR4_PAE)) {
3787 hw_cr4 &= ~X86_CR4_PAE;
3788 }
3789 }
Sheng Yang14394422008-04-28 12:24:45 +08003790
Radim Krčmář656ec4a2015-11-02 22:20:00 +01003791 if (!enable_unrestricted_guest && !is_paging(vcpu))
3792 /*
3793 * SMEP/SMAP is disabled if CPU is in non-paging mode in
3794 * hardware. However KVM always uses paging mode without
3795 * unrestricted guest.
3796 * To emulate this behavior, SMEP/SMAP needs to be manually
3797 * disabled when guest switches to non-paging mode.
3798 */
3799 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP);
3800
Sheng Yang14394422008-04-28 12:24:45 +08003801 vmcs_writel(CR4_READ_SHADOW, cr4);
3802 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003803 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804}
3805
Avi Kivity6aa8b732006-12-10 02:21:36 -08003806static void vmx_get_segment(struct kvm_vcpu *vcpu,
3807 struct kvm_segment *var, int seg)
3808{
Avi Kivitya9179492011-01-03 14:28:52 +02003809 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003810 u32 ar;
3811
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003812 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003813 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003814 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003815 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003816 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003817 var->base = vmx_read_guest_seg_base(vmx, seg);
3818 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3819 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003820 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003821 var->base = vmx_read_guest_seg_base(vmx, seg);
3822 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3823 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3824 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003825 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003826 var->type = ar & 15;
3827 var->s = (ar >> 4) & 1;
3828 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003829 /*
3830 * Some userspaces do not preserve unusable property. Since usable
3831 * segment has to be present according to VMX spec we can use present
3832 * property to amend userspace bug by making unusable segment always
3833 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3834 * segment as unusable.
3835 */
3836 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003837 var->avl = (ar >> 12) & 1;
3838 var->l = (ar >> 13) & 1;
3839 var->db = (ar >> 14) & 1;
3840 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003841}
3842
Avi Kivitya9179492011-01-03 14:28:52 +02003843static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3844{
Avi Kivitya9179492011-01-03 14:28:52 +02003845 struct kvm_segment s;
3846
3847 if (to_vmx(vcpu)->rmode.vm86_active) {
3848 vmx_get_segment(vcpu, &s, seg);
3849 return s.base;
3850 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003851 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003852}
3853
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003854static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003855{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003856 struct vcpu_vmx *vmx = to_vmx(vcpu);
3857
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003858 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003859 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003860 else {
3861 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003862 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02003863 }
Avi Kivity69c73022011-03-07 15:26:44 +02003864}
3865
Avi Kivity653e3102007-05-07 10:55:37 +03003866static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003867{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003868 u32 ar;
3869
Avi Kivityf0495f92012-06-07 17:06:10 +03003870 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871 ar = 1 << 16;
3872 else {
3873 ar = var->type & 15;
3874 ar |= (var->s & 1) << 4;
3875 ar |= (var->dpl & 3) << 5;
3876 ar |= (var->present & 1) << 7;
3877 ar |= (var->avl & 1) << 12;
3878 ar |= (var->l & 1) << 13;
3879 ar |= (var->db & 1) << 14;
3880 ar |= (var->g & 1) << 15;
3881 }
Avi Kivity653e3102007-05-07 10:55:37 +03003882
3883 return ar;
3884}
3885
3886static void vmx_set_segment(struct kvm_vcpu *vcpu,
3887 struct kvm_segment *var, int seg)
3888{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003889 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003890 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003891
Avi Kivity2fb92db2011-04-27 19:42:18 +03003892 vmx_segment_cache_clear(vmx);
3893
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003894 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3895 vmx->rmode.segs[seg] = *var;
3896 if (seg == VCPU_SREG_TR)
3897 vmcs_write16(sf->selector, var->selector);
3898 else if (var->s)
3899 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003900 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003901 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003902
Avi Kivity653e3102007-05-07 10:55:37 +03003903 vmcs_writel(sf->base, var->base);
3904 vmcs_write32(sf->limit, var->limit);
3905 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003906
3907 /*
3908 * Fix the "Accessed" bit in AR field of segment registers for older
3909 * qemu binaries.
3910 * IA32 arch specifies that at the time of processor reset the
3911 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003912 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003913 * state vmexit when "unrestricted guest" mode is turned on.
3914 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3915 * tree. Newer qemu binaries with that qemu fix would not need this
3916 * kvm hack.
3917 */
3918 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003919 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003920
Gleb Natapovf924d662012-12-12 19:10:55 +02003921 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003922
3923out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01003924 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003925}
3926
Avi Kivity6aa8b732006-12-10 02:21:36 -08003927static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3928{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003929 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003930
3931 *db = (ar >> 14) & 1;
3932 *l = (ar >> 13) & 1;
3933}
3934
Gleb Natapov89a27f42010-02-16 10:51:48 +02003935static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003936{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003937 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3938 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003939}
3940
Gleb Natapov89a27f42010-02-16 10:51:48 +02003941static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003942{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003943 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3944 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003945}
3946
Gleb Natapov89a27f42010-02-16 10:51:48 +02003947static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003948{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003949 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3950 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003951}
3952
Gleb Natapov89a27f42010-02-16 10:51:48 +02003953static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003954{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003955 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3956 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003957}
3958
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003959static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3960{
3961 struct kvm_segment var;
3962 u32 ar;
3963
3964 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003965 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003966 if (seg == VCPU_SREG_CS)
3967 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003968 ar = vmx_segment_access_rights(&var);
3969
3970 if (var.base != (var.selector << 4))
3971 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003972 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003973 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003974 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003975 return false;
3976
3977 return true;
3978}
3979
3980static bool code_segment_valid(struct kvm_vcpu *vcpu)
3981{
3982 struct kvm_segment cs;
3983 unsigned int cs_rpl;
3984
3985 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003986 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003987
Avi Kivity1872a3f2009-01-04 23:26:52 +02003988 if (cs.unusable)
3989 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003990 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003991 return false;
3992 if (!cs.s)
3993 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003994 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003995 if (cs.dpl > cs_rpl)
3996 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003997 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003998 if (cs.dpl != cs_rpl)
3999 return false;
4000 }
4001 if (!cs.present)
4002 return false;
4003
4004 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4005 return true;
4006}
4007
4008static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4009{
4010 struct kvm_segment ss;
4011 unsigned int ss_rpl;
4012
4013 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03004014 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004015
Avi Kivity1872a3f2009-01-04 23:26:52 +02004016 if (ss.unusable)
4017 return true;
4018 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004019 return false;
4020 if (!ss.s)
4021 return false;
4022 if (ss.dpl != ss_rpl) /* DPL != RPL */
4023 return false;
4024 if (!ss.present)
4025 return false;
4026
4027 return true;
4028}
4029
4030static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4031{
4032 struct kvm_segment var;
4033 unsigned int rpl;
4034
4035 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03004036 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004037
Avi Kivity1872a3f2009-01-04 23:26:52 +02004038 if (var.unusable)
4039 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004040 if (!var.s)
4041 return false;
4042 if (!var.present)
4043 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07004044 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004045 if (var.dpl < rpl) /* DPL < RPL */
4046 return false;
4047 }
4048
4049 /* TODO: Add other members to kvm_segment_field to allow checking for other access
4050 * rights flags
4051 */
4052 return true;
4053}
4054
4055static bool tr_valid(struct kvm_vcpu *vcpu)
4056{
4057 struct kvm_segment tr;
4058
4059 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4060
Avi Kivity1872a3f2009-01-04 23:26:52 +02004061 if (tr.unusable)
4062 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03004063 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004064 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02004065 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004066 return false;
4067 if (!tr.present)
4068 return false;
4069
4070 return true;
4071}
4072
4073static bool ldtr_valid(struct kvm_vcpu *vcpu)
4074{
4075 struct kvm_segment ldtr;
4076
4077 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4078
Avi Kivity1872a3f2009-01-04 23:26:52 +02004079 if (ldtr.unusable)
4080 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03004081 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004082 return false;
4083 if (ldtr.type != 2)
4084 return false;
4085 if (!ldtr.present)
4086 return false;
4087
4088 return true;
4089}
4090
4091static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4092{
4093 struct kvm_segment cs, ss;
4094
4095 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4096 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4097
Nadav Amitb32a9912015-03-29 16:33:04 +03004098 return ((cs.selector & SEGMENT_RPL_MASK) ==
4099 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004100}
4101
4102/*
4103 * Check if guest state is valid. Returns true if valid, false if
4104 * not.
4105 * We assume that registers are always usable
4106 */
4107static bool guest_state_valid(struct kvm_vcpu *vcpu)
4108{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02004109 if (enable_unrestricted_guest)
4110 return true;
4111
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004112 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03004113 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03004114 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4115 return false;
4116 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4117 return false;
4118 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4119 return false;
4120 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4121 return false;
4122 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4123 return false;
4124 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4125 return false;
4126 } else {
4127 /* protected mode guest state checks */
4128 if (!cs_ss_rpl_check(vcpu))
4129 return false;
4130 if (!code_segment_valid(vcpu))
4131 return false;
4132 if (!stack_segment_valid(vcpu))
4133 return false;
4134 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4135 return false;
4136 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4137 return false;
4138 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4139 return false;
4140 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4141 return false;
4142 if (!tr_valid(vcpu))
4143 return false;
4144 if (!ldtr_valid(vcpu))
4145 return false;
4146 }
4147 /* TODO:
4148 * - Add checks on RIP
4149 * - Add checks on RFLAGS
4150 */
4151
4152 return true;
4153}
4154
Mike Dayd77c26f2007-10-08 09:02:08 -04004155static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004156{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004157 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004158 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004159 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004160
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004161 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004162 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004163 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4164 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004165 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004166 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004167 r = kvm_write_guest_page(kvm, fn++, &data,
4168 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004169 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004170 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004171 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4172 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004173 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004174 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4175 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004176 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004177 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004178 r = kvm_write_guest_page(kvm, fn, &data,
4179 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4180 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004181out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004182 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004183 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004184}
4185
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004186static int init_rmode_identity_map(struct kvm *kvm)
4187{
Tang Chenf51770e2014-09-16 18:41:59 +08004188 int i, idx, r = 0;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004189 pfn_t identity_map_pfn;
4190 u32 tmp;
4191
Avi Kivity089d0342009-03-23 18:26:32 +02004192 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004193 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004194
4195 /* Protect kvm->arch.ept_identity_pagetable_done. */
4196 mutex_lock(&kvm->slots_lock);
4197
Tang Chenf51770e2014-09-16 18:41:59 +08004198 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004199 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004200
Sheng Yangb927a3c2009-07-21 10:42:48 +08004201 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004202
4203 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004204 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004205 goto out2;
4206
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004207 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004208 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4209 if (r < 0)
4210 goto out;
4211 /* Set up identity-mapping pagetable for EPT in real mode */
4212 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4213 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4214 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4215 r = kvm_write_guest_page(kvm, identity_map_pfn,
4216 &tmp, i * sizeof(tmp), sizeof(tmp));
4217 if (r < 0)
4218 goto out;
4219 }
4220 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004221
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004222out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004223 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004224
4225out2:
4226 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004227 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004228}
4229
Avi Kivity6aa8b732006-12-10 02:21:36 -08004230static void seg_setup(int seg)
4231{
Mathias Krause772e0312012-08-30 01:30:19 +02004232 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004233 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004234
4235 vmcs_write16(sf->selector, 0);
4236 vmcs_writel(sf->base, 0);
4237 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004238 ar = 0x93;
4239 if (seg == VCPU_SREG_CS)
4240 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004241
4242 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004243}
4244
Sheng Yangf78e0e22007-10-29 09:40:42 +08004245static int alloc_apic_access_page(struct kvm *kvm)
4246{
Xiao Guangrong44841412012-09-07 14:14:20 +08004247 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004248 int r = 0;
4249
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004250 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004251 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004252 goto out;
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004253 r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4254 APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004255 if (r)
4256 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004257
Tang Chen73a6d942014-09-11 13:38:00 +08004258 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004259 if (is_error_page(page)) {
4260 r = -EFAULT;
4261 goto out;
4262 }
4263
Tang Chenc24ae0d2014-09-24 15:57:58 +08004264 /*
4265 * Do not pin the page in memory, so that memory hot-unplug
4266 * is able to migrate it.
4267 */
4268 put_page(page);
4269 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004270out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004271 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004272 return r;
4273}
4274
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004275static int alloc_identity_pagetable(struct kvm *kvm)
4276{
Tang Chena255d472014-09-16 18:41:58 +08004277 /* Called with kvm->slots_lock held. */
4278
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004279 int r = 0;
4280
Tang Chena255d472014-09-16 18:41:58 +08004281 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4282
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02004283 r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4284 kvm->arch.ept_identity_map_addr, PAGE_SIZE);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004285
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004286 return r;
4287}
4288
Wanpeng Li991e7a02015-09-16 17:30:05 +08004289static int allocate_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004290{
4291 int vpid;
4292
Avi Kivity919818a2009-03-23 18:01:29 +02004293 if (!enable_vpid)
Wanpeng Li991e7a02015-09-16 17:30:05 +08004294 return 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004295 spin_lock(&vmx_vpid_lock);
4296 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004297 if (vpid < VMX_NR_VPIDS)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004298 __set_bit(vpid, vmx_vpid_bitmap);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004299 else
4300 vpid = 0;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004301 spin_unlock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004302 return vpid;
Sheng Yang2384d2b2008-01-17 15:14:33 +08004303}
4304
Wanpeng Li991e7a02015-09-16 17:30:05 +08004305static void free_vpid(int vpid)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004306{
Wanpeng Li991e7a02015-09-16 17:30:05 +08004307 if (!enable_vpid || vpid == 0)
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004308 return;
4309 spin_lock(&vmx_vpid_lock);
Wanpeng Li991e7a02015-09-16 17:30:05 +08004310 __clear_bit(vpid, vmx_vpid_bitmap);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004311 spin_unlock(&vmx_vpid_lock);
4312}
4313
Yang Zhang8d146952013-01-25 10:18:50 +08004314#define MSR_TYPE_R 1
4315#define MSR_TYPE_W 2
4316static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4317 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004318{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004319 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004320
4321 if (!cpu_has_vmx_msr_bitmap())
4322 return;
4323
4324 /*
4325 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4326 * have the write-low and read-high bitmap offsets the wrong way round.
4327 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4328 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004329 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004330 if (type & MSR_TYPE_R)
4331 /* read-low */
4332 __clear_bit(msr, msr_bitmap + 0x000 / f);
4333
4334 if (type & MSR_TYPE_W)
4335 /* write-low */
4336 __clear_bit(msr, msr_bitmap + 0x800 / f);
4337
Sheng Yang25c5f222008-03-28 13:18:56 +08004338 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4339 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004340 if (type & MSR_TYPE_R)
4341 /* read-high */
4342 __clear_bit(msr, msr_bitmap + 0x400 / f);
4343
4344 if (type & MSR_TYPE_W)
4345 /* write-high */
4346 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4347
4348 }
4349}
4350
4351static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4352 u32 msr, int type)
4353{
4354 int f = sizeof(unsigned long);
4355
4356 if (!cpu_has_vmx_msr_bitmap())
4357 return;
4358
4359 /*
4360 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4361 * have the write-low and read-high bitmap offsets the wrong way round.
4362 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4363 */
4364 if (msr <= 0x1fff) {
4365 if (type & MSR_TYPE_R)
4366 /* read-low */
4367 __set_bit(msr, msr_bitmap + 0x000 / f);
4368
4369 if (type & MSR_TYPE_W)
4370 /* write-low */
4371 __set_bit(msr, msr_bitmap + 0x800 / f);
4372
4373 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4374 msr &= 0x1fff;
4375 if (type & MSR_TYPE_R)
4376 /* read-high */
4377 __set_bit(msr, msr_bitmap + 0x400 / f);
4378
4379 if (type & MSR_TYPE_W)
4380 /* write-high */
4381 __set_bit(msr, msr_bitmap + 0xc00 / f);
4382
Sheng Yang25c5f222008-03-28 13:18:56 +08004383 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004384}
4385
Wincy Vanf2b93282015-02-03 23:56:03 +08004386/*
4387 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4388 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4389 */
4390static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4391 unsigned long *msr_bitmap_nested,
4392 u32 msr, int type)
4393{
4394 int f = sizeof(unsigned long);
4395
4396 if (!cpu_has_vmx_msr_bitmap()) {
4397 WARN_ON(1);
4398 return;
4399 }
4400
4401 /*
4402 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4403 * have the write-low and read-high bitmap offsets the wrong way round.
4404 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4405 */
4406 if (msr <= 0x1fff) {
4407 if (type & MSR_TYPE_R &&
4408 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4409 /* read-low */
4410 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4411
4412 if (type & MSR_TYPE_W &&
4413 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4414 /* write-low */
4415 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4416
4417 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4418 msr &= 0x1fff;
4419 if (type & MSR_TYPE_R &&
4420 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4421 /* read-high */
4422 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4423
4424 if (type & MSR_TYPE_W &&
4425 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4426 /* write-high */
4427 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4428
4429 }
4430}
4431
Avi Kivity58972972009-02-24 22:26:47 +02004432static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4433{
4434 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004435 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4436 msr, MSR_TYPE_R | MSR_TYPE_W);
4437 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4438 msr, MSR_TYPE_R | MSR_TYPE_W);
4439}
4440
4441static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
4442{
4443 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4444 msr, MSR_TYPE_R);
4445 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4446 msr, MSR_TYPE_R);
4447}
4448
4449static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
4450{
4451 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4452 msr, MSR_TYPE_R);
4453 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4454 msr, MSR_TYPE_R);
4455}
4456
4457static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
4458{
4459 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4460 msr, MSR_TYPE_W);
4461 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4462 msr, MSR_TYPE_W);
Avi Kivity58972972009-02-24 22:26:47 +02004463}
4464
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004465static int vmx_cpu_uses_apicv(struct kvm_vcpu *vcpu)
4466{
Paolo Bonzini35754c92015-07-29 12:05:37 +02004467 return enable_apicv && lapic_in_kernel(vcpu);
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004468}
4469
Wincy Van705699a2015-02-03 23:58:17 +08004470static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
4471{
4472 struct vcpu_vmx *vmx = to_vmx(vcpu);
4473 int max_irr;
4474 void *vapic_page;
4475 u16 status;
4476
4477 if (vmx->nested.pi_desc &&
4478 vmx->nested.pi_pending) {
4479 vmx->nested.pi_pending = false;
4480 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4481 return 0;
4482
4483 max_irr = find_last_bit(
4484 (unsigned long *)vmx->nested.pi_desc->pir, 256);
4485
4486 if (max_irr == 256)
4487 return 0;
4488
4489 vapic_page = kmap(vmx->nested.virtual_apic_page);
4490 if (!vapic_page) {
4491 WARN_ON(1);
4492 return -ENOMEM;
4493 }
4494 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4495 kunmap(vmx->nested.virtual_apic_page);
4496
4497 status = vmcs_read16(GUEST_INTR_STATUS);
4498 if ((u8)max_irr > ((u8)status & 0xff)) {
4499 status &= ~0xff;
4500 status |= (u8)max_irr;
4501 vmcs_write16(GUEST_INTR_STATUS, status);
4502 }
4503 }
4504 return 0;
4505}
4506
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004507static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
4508{
4509#ifdef CONFIG_SMP
4510 if (vcpu->mode == IN_GUEST_MODE) {
Feng Wu28b835d2015-09-18 22:29:54 +08004511 struct vcpu_vmx *vmx = to_vmx(vcpu);
4512
4513 /*
4514 * Currently, we don't support urgent interrupt,
4515 * all interrupts are recognized as non-urgent
4516 * interrupt, so we cannot post interrupts when
4517 * 'SN' is set.
4518 *
4519 * If the vcpu is in guest mode, it means it is
4520 * running instead of being scheduled out and
4521 * waiting in the run queue, and that's the only
4522 * case when 'SN' is set currently, warning if
4523 * 'SN' is set.
4524 */
4525 WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc));
4526
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004527 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4528 POSTED_INTR_VECTOR);
4529 return true;
4530 }
4531#endif
4532 return false;
4533}
4534
Wincy Van705699a2015-02-03 23:58:17 +08004535static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4536 int vector)
4537{
4538 struct vcpu_vmx *vmx = to_vmx(vcpu);
4539
4540 if (is_guest_mode(vcpu) &&
4541 vector == vmx->nested.posted_intr_nv) {
4542 /* the PIR and ON have been set by L1. */
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004543 kvm_vcpu_trigger_posted_interrupt(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004544 /*
4545 * If a posted intr is not recognized by hardware,
4546 * we will accomplish it in the next vmentry.
4547 */
4548 vmx->nested.pi_pending = true;
4549 kvm_make_request(KVM_REQ_EVENT, vcpu);
4550 return 0;
4551 }
4552 return -1;
4553}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004554/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004555 * Send interrupt to vcpu via posted interrupt way.
4556 * 1. If target vcpu is running(non-root mode), send posted interrupt
4557 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4558 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4559 * interrupt from PIR in next vmentry.
4560 */
4561static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4562{
4563 struct vcpu_vmx *vmx = to_vmx(vcpu);
4564 int r;
4565
Wincy Van705699a2015-02-03 23:58:17 +08004566 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4567 if (!r)
4568 return;
4569
Yang Zhanga20ed542013-04-11 19:25:15 +08004570 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4571 return;
4572
4573 r = pi_test_and_set_on(&vmx->pi_desc);
4574 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004575 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08004576 kvm_vcpu_kick(vcpu);
4577}
4578
4579static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4580{
4581 struct vcpu_vmx *vmx = to_vmx(vcpu);
4582
4583 if (!pi_test_and_clear_on(&vmx->pi_desc))
4584 return;
4585
4586 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4587}
4588
4589static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu)
4590{
4591 return;
4592}
4593
Avi Kivity6aa8b732006-12-10 02:21:36 -08004594/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004595 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4596 * will not change in the lifetime of the guest.
4597 * Note that host-state that does change is set elsewhere. E.g., host-state
4598 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4599 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004600static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004601{
4602 u32 low32, high32;
4603 unsigned long tmpl;
4604 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004605 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004606
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07004607 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004608 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4609
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004610 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004611 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004612 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
4613 vmx->host_state.vmcs_host_cr4 = cr4;
4614
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004615 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004616#ifdef CONFIG_X86_64
4617 /*
4618 * Load null selectors, so we can avoid reloading them in
4619 * __vmx_load_host_state(), in case userspace uses the null selectors
4620 * too (the expected case).
4621 */
4622 vmcs_write16(HOST_DS_SELECTOR, 0);
4623 vmcs_write16(HOST_ES_SELECTOR, 0);
4624#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004625 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4626 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004627#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004628 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4629 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4630
4631 native_store_idt(&dt);
4632 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004633 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004634
Avi Kivity83287ea422012-09-16 15:10:57 +03004635 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004636
4637 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4638 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4639 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4640 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4641
4642 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4643 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4644 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4645 }
4646}
4647
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004648static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4649{
4650 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4651 if (enable_ept)
4652 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004653 if (is_guest_mode(&vmx->vcpu))
4654 vmx->vcpu.arch.cr4_guest_owned_bits &=
4655 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004656 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4657}
4658
Yang Zhang01e439b2013-04-11 19:25:12 +08004659static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4660{
4661 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4662
Paolo Bonzini35754c92015-07-29 12:05:37 +02004663 if (!vmx_cpu_uses_apicv(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004664 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
4665 return pin_based_exec_ctrl;
4666}
4667
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004668static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4669{
4670 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01004671
4672 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4673 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4674
Paolo Bonzini35754c92015-07-29 12:05:37 +02004675 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004676 exec_control &= ~CPU_BASED_TPR_SHADOW;
4677#ifdef CONFIG_X86_64
4678 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4679 CPU_BASED_CR8_LOAD_EXITING;
4680#endif
4681 }
4682 if (!enable_ept)
4683 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4684 CPU_BASED_CR3_LOAD_EXITING |
4685 CPU_BASED_INVLPG_EXITING;
4686 return exec_control;
4687}
4688
4689static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4690{
4691 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004692 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004693 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4694 if (vmx->vpid == 0)
4695 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4696 if (!enable_ept) {
4697 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4698 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00004699 /* Enable INVPCID for non-ept guests may cause performance regression. */
4700 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004701 }
4702 if (!enable_unrestricted_guest)
4703 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4704 if (!ple_gap)
4705 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004706 if (!vmx_cpu_uses_apicv(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004707 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4708 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004709 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03004710 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4711 (handle_vmptrld).
4712 We can NOT enable shadow_vmcs here because we don't have yet
4713 a current VMCS12
4714 */
4715 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huanga3eaa862015-11-04 13:46:05 +08004716
4717 if (!enable_pml)
4718 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
Kai Huang843e4332015-01-28 10:54:28 +08004719
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004720 /* Currently, we allow L1 guest to directly run pcommit instruction. */
4721 exec_control &= ~SECONDARY_EXEC_PCOMMIT;
4722
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004723 return exec_control;
4724}
4725
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004726static void ept_set_mmio_spte_mask(void)
4727{
4728 /*
4729 * EPT Misconfigurations can be generated if the value of bits 2:0
4730 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08004731 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004732 * spte.
4733 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08004734 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004735}
4736
Wanpeng Lif53cd632014-12-02 19:14:58 +08004737#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004738/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004739 * Sets up the vmcs for emulated real mode.
4740 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004741static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004742{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004743#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004744 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004745#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004747
Avi Kivity6aa8b732006-12-10 02:21:36 -08004748 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004749 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4750 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004751
Abel Gordon4607c2d2013-04-18 14:35:55 +03004752 if (enable_shadow_vmcs) {
4753 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
4754 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
4755 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004756 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02004757 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08004758
Avi Kivity6aa8b732006-12-10 02:21:36 -08004759 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4760
Avi Kivity6aa8b732006-12-10 02:21:36 -08004761 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08004762 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004763
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004764 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004765
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004766 if (cpu_has_secondary_exec_ctrls())
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004767 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4768 vmx_secondary_exec_control(vmx));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004769
Paolo Bonzini35754c92015-07-29 12:05:37 +02004770 if (vmx_cpu_uses_apicv(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004771 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4772 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4773 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4774 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4775
4776 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004777
4778 vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
4779 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004780 }
4781
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004782 if (ple_gap) {
4783 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02004784 vmx->ple_window = ple_window;
4785 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004786 }
4787
Xiao Guangrongc3707952011-07-12 03:28:04 +08004788 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4789 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004790 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4791
Avi Kivity9581d442010-10-19 16:46:55 +02004792 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4793 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004794 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004795#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796 rdmsrl(MSR_FS_BASE, a);
4797 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
4798 rdmsrl(MSR_GS_BASE, a);
4799 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
4800#else
4801 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4802 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
4803#endif
4804
Eddie Dong2cc51562007-05-21 07:28:09 +03004805 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4806 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004807 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03004808 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004809 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004810
Radim Krčmář74545702015-04-27 15:11:25 +02004811 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
4812 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08004813
Paolo Bonzini03916db2014-07-24 14:21:57 +02004814 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004815 u32 index = vmx_msr_index[i];
4816 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004817 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004818
4819 if (rdmsr_safe(index, &data_low, &data_high) < 0)
4820 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08004821 if (wrmsr_safe(index, data_low, data_high) < 0)
4822 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03004823 vmx->guest_msrs[j].index = i;
4824 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02004825 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004826 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004827 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004828
Gleb Natapov2961e8762013-11-25 15:37:13 +02004829
4830 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004831
4832 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02004833 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004834
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004835 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004836 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004837
Wanpeng Lif53cd632014-12-02 19:14:58 +08004838 if (vmx_xsaves_supported())
4839 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
4840
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004841 return 0;
4842}
4843
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004844static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004845{
4846 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004847 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004848 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004849
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004850 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004851
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004852 vmx->soft_vnmi_blocked = 0;
4853
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004854 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004855 kvm_set_cr8(vcpu, 0);
4856
4857 if (!init_event) {
4858 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
4859 MSR_IA32_APICBASE_ENABLE;
4860 if (kvm_vcpu_is_reset_bsp(vcpu))
4861 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4862 apic_base_msr.host_initiated = true;
4863 kvm_set_apic_base(vcpu, &apic_base_msr);
4864 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004865
Avi Kivity2fb92db2011-04-27 19:42:18 +03004866 vmx_segment_cache_clear(vmx);
4867
Avi Kivity5706be02008-08-20 15:07:31 +03004868 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004869 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzini04b66832013-03-19 16:30:26 +01004870 vmcs_write32(GUEST_CS_BASE, 0xffff0000);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004871
4872 seg_setup(VCPU_SREG_DS);
4873 seg_setup(VCPU_SREG_ES);
4874 seg_setup(VCPU_SREG_FS);
4875 seg_setup(VCPU_SREG_GS);
4876 seg_setup(VCPU_SREG_SS);
4877
4878 vmcs_write16(GUEST_TR_SELECTOR, 0);
4879 vmcs_writel(GUEST_TR_BASE, 0);
4880 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4881 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4882
4883 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4884 vmcs_writel(GUEST_LDTR_BASE, 0);
4885 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4886 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4887
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004888 if (!init_event) {
4889 vmcs_write32(GUEST_SYSENTER_CS, 0);
4890 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4891 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4892 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4893 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004894
4895 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01004896 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004897
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004898 vmcs_writel(GUEST_GDTR_BASE, 0);
4899 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4900
4901 vmcs_writel(GUEST_IDTR_BASE, 0);
4902 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4903
Anthony Liguori443381a2010-12-06 10:53:38 -06004904 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004905 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
4906 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
4907
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004908 setup_msrs(vmx);
4909
Avi Kivity6aa8b732006-12-10 02:21:36 -08004910 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4911
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004912 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08004913 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004914 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08004915 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004916 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004917 vmcs_write32(TPR_THRESHOLD, 0);
4918 }
4919
Paolo Bonzinia73896c2014-11-02 07:54:30 +01004920 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004921
Paolo Bonzini35754c92015-07-29 12:05:37 +02004922 if (vmx_cpu_uses_apicv(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004923 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
4924
Sheng Yang2384d2b2008-01-17 15:14:33 +08004925 if (vmx->vpid != 0)
4926 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4927
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004928 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
4929 vmx_set_cr0(vcpu, cr0); /* enter rmode */
4930 vmx->vcpu.arch.cr0 = cr0;
4931 vmx_set_cr4(vcpu, 0);
Paolo Bonzini56908912015-10-19 11:30:19 +02004932 vmx_set_efer(vcpu, 0);
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004933 vmx_fpu_activate(vcpu);
4934 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004935
Wanpeng Lidd5f5342015-09-23 18:26:57 +08004936 vpid_sync_context(vmx->vpid);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004937}
4938
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004939/*
4940 * In nested virtualization, check if L1 asked to exit on external interrupts.
4941 * For most existing hypervisors, this will always return true.
4942 */
4943static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4944{
4945 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4946 PIN_BASED_EXT_INTR_MASK;
4947}
4948
Bandan Das77b0f5d2014-04-19 18:17:45 -04004949/*
4950 * In nested virtualization, check if L1 has set
4951 * VM_EXIT_ACK_INTR_ON_EXIT
4952 */
4953static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
4954{
4955 return get_vmcs12(vcpu)->vm_exit_controls &
4956 VM_EXIT_ACK_INTR_ON_EXIT;
4957}
4958
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004959static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
4960{
4961 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4962 PIN_BASED_NMI_EXITING;
4963}
4964
Jan Kiszkac9a79532014-03-07 20:03:15 +01004965static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004966{
4967 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02004968
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004969 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4970 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
4971 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4972}
4973
Jan Kiszkac9a79532014-03-07 20:03:15 +01004974static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004975{
4976 u32 cpu_based_vm_exec_control;
4977
Jan Kiszkac9a79532014-03-07 20:03:15 +01004978 if (!cpu_has_virtual_nmis() ||
4979 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
4980 enable_irq_window(vcpu);
4981 return;
4982 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004983
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004984 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4985 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
4986 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4987}
4988
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004989static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004990{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004991 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004992 uint32_t intr;
4993 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004994
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004995 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004996
Avi Kivityfa89a812008-09-01 15:57:51 +03004997 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004998 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004999 int inc_eip = 0;
5000 if (vcpu->arch.interrupt.soft)
5001 inc_eip = vcpu->arch.event_exit_inst_len;
5002 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005003 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03005004 return;
5005 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005006 intr = irq | INTR_INFO_VALID_MASK;
5007 if (vcpu->arch.interrupt.soft) {
5008 intr |= INTR_TYPE_SOFT_INTR;
5009 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5010 vmx->vcpu.arch.event_exit_inst_len);
5011 } else
5012 intr |= INTR_TYPE_EXT_INTR;
5013 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03005014}
5015
Sheng Yangf08864b2008-05-15 18:23:25 +08005016static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5017{
Jan Kiszka66a5a342008-09-26 09:30:51 +02005018 struct vcpu_vmx *vmx = to_vmx(vcpu);
5019
Nadav Har'El0b6ac342011-05-25 23:13:36 +03005020 if (is_guest_mode(vcpu))
5021 return;
5022
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005023 if (!cpu_has_virtual_nmis()) {
5024 /*
5025 * Tracking the NMI-blocked state in software is built upon
5026 * finding the next open IRQ window. This, in turn, depends on
5027 * well-behaving guests: They have to keep IRQs disabled at
5028 * least as long as the NMI handler runs. Otherwise we may
5029 * cause NMI nesting, maybe breaking the guest. But as this is
5030 * highly unlikely, we can live with the residual risk.
5031 */
5032 vmx->soft_vnmi_blocked = 1;
5033 vmx->vnmi_blocked_time = 0;
5034 }
5035
Jan Kiszka487b3912008-09-26 09:30:56 +02005036 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02005037 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03005038 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05005039 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02005040 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02005041 return;
5042 }
Sheng Yangf08864b2008-05-15 18:23:25 +08005043 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5044 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08005045}
5046
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005047static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5048{
5049 if (!cpu_has_virtual_nmis())
5050 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02005051 if (to_vmx(vcpu)->nmi_known_unmasked)
5052 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03005053 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005054}
5055
5056static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5057{
5058 struct vcpu_vmx *vmx = to_vmx(vcpu);
5059
5060 if (!cpu_has_virtual_nmis()) {
5061 if (vmx->soft_vnmi_blocked != masked) {
5062 vmx->soft_vnmi_blocked = masked;
5063 vmx->vnmi_blocked_time = 0;
5064 }
5065 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02005066 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005067 if (masked)
5068 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5069 GUEST_INTR_STATE_NMI);
5070 else
5071 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5072 GUEST_INTR_STATE_NMI);
5073 }
5074}
5075
Jan Kiszka2505dc92013-04-14 12:12:47 +02005076static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5077{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005078 if (to_vmx(vcpu)->nested.nested_run_pending)
5079 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02005080
Jan Kiszka2505dc92013-04-14 12:12:47 +02005081 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
5082 return 0;
5083
5084 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5085 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5086 | GUEST_INTR_STATE_NMI));
5087}
5088
Gleb Natapov78646122009-03-23 12:12:11 +02005089static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5090{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01005091 return (!to_vmx(vcpu)->nested.nested_run_pending &&
5092 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03005093 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5094 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02005095}
5096
Izik Eiduscbc94022007-10-25 00:29:55 +02005097static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5098{
5099 int ret;
Izik Eiduscbc94022007-10-25 00:29:55 +02005100
Paolo Bonzini1d8007b2015-10-12 13:38:32 +02005101 ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5102 PAGE_SIZE * 3);
Izik Eiduscbc94022007-10-25 00:29:55 +02005103 if (ret)
5104 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08005105 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02005106 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02005107}
5108
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005109static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005110{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005111 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005112 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01005113 /*
5114 * Update instruction length as we may reinject the exception
5115 * from user space while in guest debugging mode.
5116 */
5117 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5118 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005119 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005120 return false;
5121 /* fall through */
5122 case DB_VECTOR:
5123 if (vcpu->guest_debug &
5124 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5125 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005126 /* fall through */
5127 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005128 case OF_VECTOR:
5129 case BR_VECTOR:
5130 case UD_VECTOR:
5131 case DF_VECTOR:
5132 case SS_VECTOR:
5133 case GP_VECTOR:
5134 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005135 return true;
5136 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005137 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005138 return false;
5139}
5140
5141static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5142 int vec, u32 err_code)
5143{
5144 /*
5145 * Instruction with address size override prefix opcode 0x67
5146 * Cause the #SS fault with 0 error code in VM86 mode.
5147 */
5148 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5149 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5150 if (vcpu->arch.halt_request) {
5151 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005152 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005153 }
5154 return 1;
5155 }
5156 return 0;
5157 }
5158
5159 /*
5160 * Forward all other exceptions that are valid in real mode.
5161 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5162 * the required debugging infrastructure rework.
5163 */
5164 kvm_queue_exception(vcpu, vec);
5165 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005166}
5167
Andi Kleena0861c02009-06-08 17:37:09 +08005168/*
5169 * Trigger machine check on the host. We assume all the MSRs are already set up
5170 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5171 * We pass a fake environment to the machine check handler because we want
5172 * the guest to be always treated like user space, no matter what context
5173 * it used internally.
5174 */
5175static void kvm_machine_check(void)
5176{
5177#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5178 struct pt_regs regs = {
5179 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5180 .flags = X86_EFLAGS_IF,
5181 };
5182
5183 do_machine_check(&regs, 0);
5184#endif
5185}
5186
Avi Kivity851ba692009-08-24 11:10:17 +03005187static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005188{
5189 /* already handled by vcpu_run */
5190 return 1;
5191}
5192
Avi Kivity851ba692009-08-24 11:10:17 +03005193static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005194{
Avi Kivity1155f762007-11-22 11:30:47 +02005195 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005196 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005197 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005198 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005199 u32 vect_info;
5200 enum emulation_result er;
5201
Avi Kivity1155f762007-11-22 11:30:47 +02005202 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005203 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005204
Andi Kleena0861c02009-06-08 17:37:09 +08005205 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005206 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005207
Jan Kiszkae4a41882008-09-26 09:30:46 +02005208 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02005209 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005210
5211 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005212 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005213 return 1;
5214 }
5215
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005216 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005217 if (is_guest_mode(vcpu)) {
5218 kvm_queue_exception(vcpu, UD_VECTOR);
5219 return 1;
5220 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005221 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005222 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005223 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005224 return 1;
5225 }
5226
Avi Kivity6aa8b732006-12-10 02:21:36 -08005227 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005228 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005229 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005230
5231 /*
5232 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5233 * MMIO, it is better to report an internal error.
5234 * See the comments in vmx_handle_exit.
5235 */
5236 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5237 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5238 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5239 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005240 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005241 vcpu->run->internal.data[0] = vect_info;
5242 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005243 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005244 return 0;
5245 }
5246
Avi Kivity6aa8b732006-12-10 02:21:36 -08005247 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005248 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005249 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005250 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005251 trace_kvm_page_fault(cr2, error_code);
5252
Gleb Natapov3298b752009-05-11 13:35:46 +03005253 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005254 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005255 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005256 }
5257
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005258 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005259
5260 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5261 return handle_rmode_exception(vcpu, ex_no, error_code);
5262
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005263 switch (ex_no) {
5264 case DB_VECTOR:
5265 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5266 if (!(vcpu->guest_debug &
5267 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005268 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005269 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01005270 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5271 skip_emulated_instruction(vcpu);
5272
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005273 kvm_queue_exception(vcpu, DB_VECTOR);
5274 return 1;
5275 }
5276 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5277 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5278 /* fall through */
5279 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01005280 /*
5281 * Update instruction length as we may reinject #BP from
5282 * user space while in guest debugging mode. Reading it for
5283 * #DB as well causes no harm, it is not used in that case.
5284 */
5285 vmx->vcpu.arch.event_exit_inst_len =
5286 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005287 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005288 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005289 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5290 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005291 break;
5292 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005293 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5294 kvm_run->ex.exception = ex_no;
5295 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005296 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005297 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005298 return 0;
5299}
5300
Avi Kivity851ba692009-08-24 11:10:17 +03005301static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005302{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005303 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005304 return 1;
5305}
5306
Avi Kivity851ba692009-08-24 11:10:17 +03005307static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005308{
Avi Kivity851ba692009-08-24 11:10:17 +03005309 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005310 return 0;
5311}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005312
Avi Kivity851ba692009-08-24 11:10:17 +03005313static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005314{
He, Qingbfdaab02007-09-12 14:18:28 +08005315 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005316 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005317 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005318
He, Qingbfdaab02007-09-12 14:18:28 +08005319 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005320 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005321 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005322
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005323 ++vcpu->stat.io_exits;
5324
5325 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005326 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005327
5328 port = exit_qualification >> 16;
5329 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005330 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005331
5332 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005333}
5334
Ingo Molnar102d8322007-02-19 14:37:47 +02005335static void
5336vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5337{
5338 /*
5339 * Patch in the VMCALL instruction:
5340 */
5341 hypercall[0] = 0x0f;
5342 hypercall[1] = 0x01;
5343 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005344}
5345
Wincy Vanb9c237b2015-02-03 23:56:30 +08005346static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005347{
5348 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005349 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005350
Wincy Vanb9c237b2015-02-03 23:56:30 +08005351 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005352 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5353 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5354 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5355 return (val & always_on) == always_on;
5356}
5357
Guo Chao0fa06072012-06-28 15:16:19 +08005358/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005359static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5360{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005361 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005362 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5363 unsigned long orig_val = val;
5364
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005365 /*
5366 * We get here when L2 changed cr0 in a way that did not change
5367 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005368 * but did change L0 shadowed bits. So we first calculate the
5369 * effective cr0 value that L1 would like to write into the
5370 * hardware. It consists of the L2-owned bits from the new
5371 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005372 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005373 val = (val & ~vmcs12->cr0_guest_host_mask) |
5374 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5375
Wincy Vanb9c237b2015-02-03 23:56:30 +08005376 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005377 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005378
5379 if (kvm_set_cr0(vcpu, val))
5380 return 1;
5381 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005382 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005383 } else {
5384 if (to_vmx(vcpu)->nested.vmxon &&
5385 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5386 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005387 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005388 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005389}
5390
5391static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5392{
5393 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005394 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5395 unsigned long orig_val = val;
5396
5397 /* analogously to handle_set_cr0 */
5398 val = (val & ~vmcs12->cr4_guest_host_mask) |
5399 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5400 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005401 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005402 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005403 return 0;
5404 } else
5405 return kvm_set_cr4(vcpu, val);
5406}
5407
5408/* called to set cr0 as approriate for clts instruction exit. */
5409static void handle_clts(struct kvm_vcpu *vcpu)
5410{
5411 if (is_guest_mode(vcpu)) {
5412 /*
5413 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5414 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5415 * just pretend it's off (also in arch.cr0 for fpu_activate).
5416 */
5417 vmcs_writel(CR0_READ_SHADOW,
5418 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5419 vcpu->arch.cr0 &= ~X86_CR0_TS;
5420 } else
5421 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5422}
5423
Avi Kivity851ba692009-08-24 11:10:17 +03005424static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005425{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005426 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005427 int cr;
5428 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005429 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005430
He, Qingbfdaab02007-09-12 14:18:28 +08005431 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005432 cr = exit_qualification & 15;
5433 reg = (exit_qualification >> 8) & 15;
5434 switch ((exit_qualification >> 4) & 3) {
5435 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005436 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005437 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005438 switch (cr) {
5439 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005440 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005441 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005442 return 1;
5443 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005444 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005445 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005446 return 1;
5447 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005448 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005449 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005450 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005451 case 8: {
5452 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005453 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005454 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005455 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005456 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005457 return 1;
5458 if (cr8_prev <= cr8)
5459 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005460 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005461 return 0;
5462 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005463 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005464 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005465 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005466 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005467 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005468 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005469 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005470 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005471 case 1: /*mov from cr*/
5472 switch (cr) {
5473 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005474 val = kvm_read_cr3(vcpu);
5475 kvm_register_write(vcpu, reg, val);
5476 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005477 skip_emulated_instruction(vcpu);
5478 return 1;
5479 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005480 val = kvm_get_cr8(vcpu);
5481 kvm_register_write(vcpu, reg, val);
5482 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005483 skip_emulated_instruction(vcpu);
5484 return 1;
5485 }
5486 break;
5487 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005488 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005489 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005490 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005491
5492 skip_emulated_instruction(vcpu);
5493 return 1;
5494 default:
5495 break;
5496 }
Avi Kivity851ba692009-08-24 11:10:17 +03005497 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005498 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005499 (int)(exit_qualification >> 4) & 3, cr);
5500 return 0;
5501}
5502
Avi Kivity851ba692009-08-24 11:10:17 +03005503static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005504{
He, Qingbfdaab02007-09-12 14:18:28 +08005505 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005506 int dr, dr7, reg;
5507
5508 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5509 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5510
5511 /* First, if DR does not exist, trigger UD */
5512 if (!kvm_require_dr(vcpu, dr))
5513 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005514
Jan Kiszkaf2483412010-01-20 18:20:20 +01005515 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005516 if (!kvm_require_cpl(vcpu, 0))
5517 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005518 dr7 = vmcs_readl(GUEST_DR7);
5519 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005520 /*
5521 * As the vm-exit takes precedence over the debug trap, we
5522 * need to emulate the latter, either for the host or the
5523 * guest debugging itself.
5524 */
5525 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005526 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005527 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005528 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005529 vcpu->run->debug.arch.exception = DB_VECTOR;
5530 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005531 return 0;
5532 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02005533 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005534 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005535 kvm_queue_exception(vcpu, DB_VECTOR);
5536 return 1;
5537 }
5538 }
5539
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005540 if (vcpu->guest_debug == 0) {
5541 u32 cpu_based_vm_exec_control;
5542
5543 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5544 cpu_based_vm_exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5545 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5546
5547 /*
5548 * No more DR vmexits; force a reload of the debug registers
5549 * and reenter on this instruction. The next vmexit will
5550 * retrieve the full state of the debug registers.
5551 */
5552 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5553 return 1;
5554 }
5555
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005556 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5557 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005558 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005559
5560 if (kvm_get_dr(vcpu, dr, &val))
5561 return 1;
5562 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005563 } else
Nadav Amit57773922014-06-18 17:19:23 +03005564 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005565 return 1;
5566
Avi Kivity6aa8b732006-12-10 02:21:36 -08005567 skip_emulated_instruction(vcpu);
5568 return 1;
5569}
5570
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005571static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5572{
5573 return vcpu->arch.dr6;
5574}
5575
5576static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5577{
5578}
5579
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005580static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5581{
5582 u32 cpu_based_vm_exec_control;
5583
5584 get_debugreg(vcpu->arch.db[0], 0);
5585 get_debugreg(vcpu->arch.db[1], 1);
5586 get_debugreg(vcpu->arch.db[2], 2);
5587 get_debugreg(vcpu->arch.db[3], 3);
5588 get_debugreg(vcpu->arch.dr6, 6);
5589 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5590
5591 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
5592
5593 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5594 cpu_based_vm_exec_control |= CPU_BASED_MOV_DR_EXITING;
5595 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5596}
5597
Gleb Natapov020df072010-04-13 10:05:23 +03005598static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5599{
5600 vmcs_writel(GUEST_DR7, val);
5601}
5602
Avi Kivity851ba692009-08-24 11:10:17 +03005603static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005604{
Avi Kivity06465c52007-02-28 20:46:53 +02005605 kvm_emulate_cpuid(vcpu);
5606 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005607}
5608
Avi Kivity851ba692009-08-24 11:10:17 +03005609static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005610{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005611 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005612 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005613
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005614 msr_info.index = ecx;
5615 msr_info.host_initiated = false;
5616 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02005617 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005618 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005619 return 1;
5620 }
5621
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005622 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005623
Avi Kivity6aa8b732006-12-10 02:21:36 -08005624 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005625 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
5626 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005627 skip_emulated_instruction(vcpu);
5628 return 1;
5629}
5630
Avi Kivity851ba692009-08-24 11:10:17 +03005631static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005632{
Will Auld8fe8ab42012-11-29 12:42:12 -08005633 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005634 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5635 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5636 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005637
Will Auld8fe8ab42012-11-29 12:42:12 -08005638 msr.data = data;
5639 msr.index = ecx;
5640 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03005641 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005642 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005643 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005644 return 1;
5645 }
5646
Avi Kivity59200272010-01-25 19:47:02 +02005647 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005648 skip_emulated_instruction(vcpu);
5649 return 1;
5650}
5651
Avi Kivity851ba692009-08-24 11:10:17 +03005652static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005653{
Avi Kivity3842d132010-07-27 12:30:24 +03005654 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005655 return 1;
5656}
5657
Avi Kivity851ba692009-08-24 11:10:17 +03005658static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005659{
Eddie Dong85f455f2007-07-06 12:20:49 +03005660 u32 cpu_based_vm_exec_control;
5661
5662 /* clear pending irq */
5663 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5664 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5665 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005666
Avi Kivity3842d132010-07-27 12:30:24 +03005667 kvm_make_request(KVM_REQ_EVENT, vcpu);
5668
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005669 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005670 return 1;
5671}
5672
Avi Kivity851ba692009-08-24 11:10:17 +03005673static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005674{
Avi Kivityd3bef152007-06-05 15:53:05 +03005675 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005676}
5677
Avi Kivity851ba692009-08-24 11:10:17 +03005678static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005679{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005680 kvm_emulate_hypercall(vcpu);
5681 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02005682}
5683
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005684static int handle_invd(struct kvm_vcpu *vcpu)
5685{
Andre Przywara51d8b662010-12-21 11:12:02 +01005686 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005687}
5688
Avi Kivity851ba692009-08-24 11:10:17 +03005689static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005690{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005691 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005692
5693 kvm_mmu_invlpg(vcpu, exit_qualification);
5694 skip_emulated_instruction(vcpu);
5695 return 1;
5696}
5697
Avi Kivityfee84b02011-11-10 14:57:25 +02005698static int handle_rdpmc(struct kvm_vcpu *vcpu)
5699{
5700 int err;
5701
5702 err = kvm_rdpmc(vcpu);
5703 kvm_complete_insn_gp(vcpu, err);
5704
5705 return 1;
5706}
5707
Avi Kivity851ba692009-08-24 11:10:17 +03005708static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005709{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005710 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005711 return 1;
5712}
5713
Dexuan Cui2acf9232010-06-10 11:27:12 +08005714static int handle_xsetbv(struct kvm_vcpu *vcpu)
5715{
5716 u64 new_bv = kvm_read_edx_eax(vcpu);
5717 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5718
5719 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5720 skip_emulated_instruction(vcpu);
5721 return 1;
5722}
5723
Wanpeng Lif53cd632014-12-02 19:14:58 +08005724static int handle_xsaves(struct kvm_vcpu *vcpu)
5725{
5726 skip_emulated_instruction(vcpu);
5727 WARN(1, "this should never happen\n");
5728 return 1;
5729}
5730
5731static int handle_xrstors(struct kvm_vcpu *vcpu)
5732{
5733 skip_emulated_instruction(vcpu);
5734 WARN(1, "this should never happen\n");
5735 return 1;
5736}
5737
Avi Kivity851ba692009-08-24 11:10:17 +03005738static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005739{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005740 if (likely(fasteoi)) {
5741 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5742 int access_type, offset;
5743
5744 access_type = exit_qualification & APIC_ACCESS_TYPE;
5745 offset = exit_qualification & APIC_ACCESS_OFFSET;
5746 /*
5747 * Sane guest uses MOV to write EOI, with written value
5748 * not cared. So make a short-circuit here by avoiding
5749 * heavy instruction emulation.
5750 */
5751 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5752 (offset == APIC_EOI)) {
5753 kvm_lapic_set_eoi(vcpu);
5754 skip_emulated_instruction(vcpu);
5755 return 1;
5756 }
5757 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005758 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005759}
5760
Yang Zhangc7c9c562013-01-25 10:18:51 +08005761static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5762{
5763 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5764 int vector = exit_qualification & 0xff;
5765
5766 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5767 kvm_apic_set_eoi_accelerated(vcpu, vector);
5768 return 1;
5769}
5770
Yang Zhang83d4c282013-01-25 10:18:49 +08005771static int handle_apic_write(struct kvm_vcpu *vcpu)
5772{
5773 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5774 u32 offset = exit_qualification & 0xfff;
5775
5776 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5777 kvm_apic_write_nodecode(vcpu, offset);
5778 return 1;
5779}
5780
Avi Kivity851ba692009-08-24 11:10:17 +03005781static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005782{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005783 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005784 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005785 bool has_error_code = false;
5786 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005787 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005788 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005789
5790 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005791 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005792 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005793
5794 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5795
5796 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005797 if (reason == TASK_SWITCH_GATE && idt_v) {
5798 switch (type) {
5799 case INTR_TYPE_NMI_INTR:
5800 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005801 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005802 break;
5803 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005804 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005805 kvm_clear_interrupt_queue(vcpu);
5806 break;
5807 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005808 if (vmx->idt_vectoring_info &
5809 VECTORING_INFO_DELIVER_CODE_MASK) {
5810 has_error_code = true;
5811 error_code =
5812 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5813 }
5814 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005815 case INTR_TYPE_SOFT_EXCEPTION:
5816 kvm_clear_exception_queue(vcpu);
5817 break;
5818 default:
5819 break;
5820 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005821 }
Izik Eidus37817f22008-03-24 23:14:53 +02005822 tss_selector = exit_qualification;
5823
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005824 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5825 type != INTR_TYPE_EXT_INTR &&
5826 type != INTR_TYPE_NMI_INTR))
5827 skip_emulated_instruction(vcpu);
5828
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005829 if (kvm_task_switch(vcpu, tss_selector,
5830 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
5831 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03005832 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5833 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5834 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005835 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03005836 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005837
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005838 /*
5839 * TODO: What about debug traps on tss switch?
5840 * Are we supposed to inject them and update dr6?
5841 */
5842
5843 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02005844}
5845
Avi Kivity851ba692009-08-24 11:10:17 +03005846static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005847{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005848 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005849 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005850 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005851 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08005852
Sheng Yangf9c617f2009-03-25 10:08:52 +08005853 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08005854
Sheng Yang14394422008-04-28 12:24:45 +08005855 gla_validity = (exit_qualification >> 7) & 0x3;
5856 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
5857 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
5858 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
5859 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08005860 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08005861 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
5862 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03005863 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5864 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03005865 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08005866 }
5867
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005868 /*
5869 * EPT violation happened while executing iret from NMI,
5870 * "blocked by NMI" bit has to be set before next VM entry.
5871 * There are errata that may cause this bit to not be set:
5872 * AAK134, BY25.
5873 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005874 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
5875 cpu_has_virtual_nmis() &&
5876 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005877 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5878
Sheng Yang14394422008-04-28 12:24:45 +08005879 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005880 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005881
5882 /* It is a write fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08005883 error_code = exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03005884 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08005885 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005886 /* ept page table is present? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08005887 error_code |= (exit_qualification >> 3) & PFERR_PRESENT_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005888
Yang Zhang25d92082013-08-06 12:00:32 +03005889 vcpu->arch.exit_qualification = exit_qualification;
5890
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005891 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005892}
5893
Avi Kivity851ba692009-08-24 11:10:17 +03005894static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005895{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08005896 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005897 gpa_t gpa;
5898
5899 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005900 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005901 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08005902 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005903 return 1;
5904 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005905
Paolo Bonzini450869d2015-11-04 13:41:21 +01005906 ret = handle_mmio_page_fault(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005907 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005908 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
5909 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005910
5911 if (unlikely(ret == RET_MMIO_PF_INVALID))
5912 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
5913
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005914 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005915 return 1;
5916
5917 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08005918 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005919
Avi Kivity851ba692009-08-24 11:10:17 +03005920 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5921 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005922
5923 return 0;
5924}
5925
Avi Kivity851ba692009-08-24 11:10:17 +03005926static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005927{
5928 u32 cpu_based_vm_exec_control;
5929
5930 /* clear pending NMI */
5931 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5932 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
5933 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5934 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005935 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005936
5937 return 1;
5938}
5939
Mohammed Gamal80ced182009-09-01 12:48:18 +02005940static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005941{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005942 struct vcpu_vmx *vmx = to_vmx(vcpu);
5943 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005944 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02005945 u32 cpu_exec_ctrl;
5946 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005947 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005948
5949 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5950 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005951
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005952 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03005953 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005954 return handle_interrupt_window(&vmx->vcpu);
5955
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005956 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
5957 return 1;
5958
Gleb Natapov991eebf2013-04-11 12:10:51 +03005959 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005960
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005961 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02005962 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005963 ret = 0;
5964 goto out;
5965 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005966
Avi Kivityde5f70e2012-06-12 20:22:28 +03005967 if (err != EMULATE_DONE) {
5968 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5969 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5970 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005971 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03005972 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005973
Gleb Natapov8d76c492013-05-08 18:38:44 +03005974 if (vcpu->arch.halt_request) {
5975 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005976 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03005977 goto out;
5978 }
5979
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005980 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02005981 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005982 if (need_resched())
5983 schedule();
5984 }
5985
Mohammed Gamal80ced182009-09-01 12:48:18 +02005986out:
5987 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005988}
5989
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005990static int __grow_ple_window(int val)
5991{
5992 if (ple_window_grow < 1)
5993 return ple_window;
5994
5995 val = min(val, ple_window_actual_max);
5996
5997 if (ple_window_grow < ple_window)
5998 val *= ple_window_grow;
5999 else
6000 val += ple_window_grow;
6001
6002 return val;
6003}
6004
6005static int __shrink_ple_window(int val, int modifier, int minimum)
6006{
6007 if (modifier < 1)
6008 return ple_window;
6009
6010 if (modifier < ple_window)
6011 val /= modifier;
6012 else
6013 val -= modifier;
6014
6015 return max(val, minimum);
6016}
6017
6018static void grow_ple_window(struct kvm_vcpu *vcpu)
6019{
6020 struct vcpu_vmx *vmx = to_vmx(vcpu);
6021 int old = vmx->ple_window;
6022
6023 vmx->ple_window = __grow_ple_window(old);
6024
6025 if (vmx->ple_window != old)
6026 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006027
6028 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006029}
6030
6031static void shrink_ple_window(struct kvm_vcpu *vcpu)
6032{
6033 struct vcpu_vmx *vmx = to_vmx(vcpu);
6034 int old = vmx->ple_window;
6035
6036 vmx->ple_window = __shrink_ple_window(old,
6037 ple_window_shrink, ple_window);
6038
6039 if (vmx->ple_window != old)
6040 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02006041
6042 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006043}
6044
6045/*
6046 * ple_window_actual_max is computed to be one grow_ple_window() below
6047 * ple_window_max. (See __grow_ple_window for the reason.)
6048 * This prevents overflows, because ple_window_max is int.
6049 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
6050 * this process.
6051 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
6052 */
6053static void update_ple_window_actual_max(void)
6054{
6055 ple_window_actual_max =
6056 __shrink_ple_window(max(ple_window_max, ple_window),
6057 ple_window_grow, INT_MIN);
6058}
6059
Feng Wubf9f6ac2015-09-18 22:29:55 +08006060/*
6061 * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6062 */
6063static void wakeup_handler(void)
6064{
6065 struct kvm_vcpu *vcpu;
6066 int cpu = smp_processor_id();
6067
6068 spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6069 list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6070 blocked_vcpu_list) {
6071 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6072
6073 if (pi_test_on(pi_desc) == 1)
6074 kvm_vcpu_kick(vcpu);
6075 }
6076 spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6077}
6078
Tiejun Chenf2c76482014-10-28 10:14:47 +08006079static __init int hardware_setup(void)
6080{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006081 int r = -ENOMEM, i, msr;
6082
6083 rdmsrl_safe(MSR_EFER, &host_efer);
6084
6085 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
6086 kvm_define_shared_msr(i, vmx_msr_index[i]);
6087
6088 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
6089 if (!vmx_io_bitmap_a)
6090 return r;
6091
6092 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
6093 if (!vmx_io_bitmap_b)
6094 goto out;
6095
6096 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
6097 if (!vmx_msr_bitmap_legacy)
6098 goto out1;
6099
6100 vmx_msr_bitmap_legacy_x2apic =
6101 (unsigned long *)__get_free_page(GFP_KERNEL);
6102 if (!vmx_msr_bitmap_legacy_x2apic)
6103 goto out2;
6104
6105 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
6106 if (!vmx_msr_bitmap_longmode)
6107 goto out3;
6108
6109 vmx_msr_bitmap_longmode_x2apic =
6110 (unsigned long *)__get_free_page(GFP_KERNEL);
6111 if (!vmx_msr_bitmap_longmode_x2apic)
6112 goto out4;
Wincy Van3af18d92015-02-03 23:49:31 +08006113
6114 if (nested) {
6115 vmx_msr_bitmap_nested =
6116 (unsigned long *)__get_free_page(GFP_KERNEL);
6117 if (!vmx_msr_bitmap_nested)
6118 goto out5;
6119 }
6120
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006121 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6122 if (!vmx_vmread_bitmap)
Wincy Van3af18d92015-02-03 23:49:31 +08006123 goto out6;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006124
6125 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
6126 if (!vmx_vmwrite_bitmap)
Wincy Van3af18d92015-02-03 23:49:31 +08006127 goto out7;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006128
6129 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
6130 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
6131
6132 /*
6133 * Allow direct access to the PC debug port (it is often used for I/O
6134 * delays, but the vmexits simply slow things down).
6135 */
6136 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
6137 clear_bit(0x80, vmx_io_bitmap_a);
6138
6139 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
6140
6141 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
6142 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Wincy Van3af18d92015-02-03 23:49:31 +08006143 if (nested)
6144 memset(vmx_msr_bitmap_nested, 0xff, PAGE_SIZE);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006145
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006146 if (setup_vmcs_config(&vmcs_config) < 0) {
6147 r = -EIO;
Wincy Van3af18d92015-02-03 23:49:31 +08006148 goto out8;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006149 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006150
6151 if (boot_cpu_has(X86_FEATURE_NX))
6152 kvm_enable_efer_bits(EFER_NX);
6153
6154 if (!cpu_has_vmx_vpid())
6155 enable_vpid = 0;
6156 if (!cpu_has_vmx_shadow_vmcs())
6157 enable_shadow_vmcs = 0;
6158 if (enable_shadow_vmcs)
6159 init_vmcs_shadow_fields();
6160
6161 if (!cpu_has_vmx_ept() ||
6162 !cpu_has_vmx_ept_4levels()) {
6163 enable_ept = 0;
6164 enable_unrestricted_guest = 0;
6165 enable_ept_ad_bits = 0;
6166 }
6167
6168 if (!cpu_has_vmx_ept_ad_bits())
6169 enable_ept_ad_bits = 0;
6170
6171 if (!cpu_has_vmx_unrestricted_guest())
6172 enable_unrestricted_guest = 0;
6173
Paolo Bonziniad15a292015-01-30 16:18:49 +01006174 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006175 flexpriority_enabled = 0;
6176
Paolo Bonziniad15a292015-01-30 16:18:49 +01006177 /*
6178 * set_apic_access_page_addr() is used to reload apic access
6179 * page upon invalidation. No need to do anything if not
6180 * using the APIC_ACCESS_ADDR VMCS field.
6181 */
6182 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006183 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006184
6185 if (!cpu_has_vmx_tpr_shadow())
6186 kvm_x86_ops->update_cr8_intercept = NULL;
6187
6188 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6189 kvm_disable_largepages();
6190
6191 if (!cpu_has_vmx_ple())
6192 ple_gap = 0;
6193
6194 if (!cpu_has_vmx_apicv())
6195 enable_apicv = 0;
6196
Haozhong Zhang64903d62015-10-20 15:39:09 +08006197 if (cpu_has_vmx_tsc_scaling()) {
6198 kvm_has_tsc_control = true;
6199 kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
6200 kvm_tsc_scaling_ratio_frac_bits = 48;
6201 }
6202
Tiejun Chenf2c76482014-10-28 10:14:47 +08006203 if (enable_apicv)
6204 kvm_x86_ops->update_cr8_intercept = NULL;
6205 else {
6206 kvm_x86_ops->hwapic_irr_update = NULL;
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01006207 kvm_x86_ops->hwapic_isr_update = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006208 kvm_x86_ops->deliver_posted_interrupt = NULL;
6209 kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy;
6210 }
6211
Tiejun Chenbaa03522014-12-23 16:21:11 +08006212 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6213 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6214 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6215 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6216 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6217 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
6218 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
6219
6220 memcpy(vmx_msr_bitmap_legacy_x2apic,
6221 vmx_msr_bitmap_legacy, PAGE_SIZE);
6222 memcpy(vmx_msr_bitmap_longmode_x2apic,
6223 vmx_msr_bitmap_longmode, PAGE_SIZE);
6224
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006225 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6226
Tiejun Chenbaa03522014-12-23 16:21:11 +08006227 if (enable_apicv) {
6228 for (msr = 0x800; msr <= 0x8ff; msr++)
6229 vmx_disable_intercept_msr_read_x2apic(msr);
6230
6231 /* According SDM, in x2apic mode, the whole id reg is used.
6232 * But in KVM, it only use the highest eight bits. Need to
6233 * intercept it */
6234 vmx_enable_intercept_msr_read_x2apic(0x802);
6235 /* TMCCT */
6236 vmx_enable_intercept_msr_read_x2apic(0x839);
6237 /* TPR */
6238 vmx_disable_intercept_msr_write_x2apic(0x808);
6239 /* EOI */
6240 vmx_disable_intercept_msr_write_x2apic(0x80b);
6241 /* SELF-IPI */
6242 vmx_disable_intercept_msr_write_x2apic(0x83f);
6243 }
6244
6245 if (enable_ept) {
6246 kvm_mmu_set_mask_ptes(0ull,
6247 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6248 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
6249 0ull, VMX_EPT_EXECUTABLE_MASK);
6250 ept_set_mmio_spte_mask();
6251 kvm_enable_tdp();
6252 } else
6253 kvm_disable_tdp();
6254
6255 update_ple_window_actual_max();
6256
Kai Huang843e4332015-01-28 10:54:28 +08006257 /*
6258 * Only enable PML when hardware supports PML feature, and both EPT
6259 * and EPT A/D bit features are enabled -- PML depends on them to work.
6260 */
6261 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6262 enable_pml = 0;
6263
6264 if (!enable_pml) {
6265 kvm_x86_ops->slot_enable_log_dirty = NULL;
6266 kvm_x86_ops->slot_disable_log_dirty = NULL;
6267 kvm_x86_ops->flush_log_dirty = NULL;
6268 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6269 }
6270
Feng Wubf9f6ac2015-09-18 22:29:55 +08006271 kvm_set_posted_intr_wakeup_handler(wakeup_handler);
6272
Tiejun Chenf2c76482014-10-28 10:14:47 +08006273 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006274
Wincy Van3af18d92015-02-03 23:49:31 +08006275out8:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006276 free_page((unsigned long)vmx_vmwrite_bitmap);
Wincy Van3af18d92015-02-03 23:49:31 +08006277out7:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006278 free_page((unsigned long)vmx_vmread_bitmap);
Wincy Van3af18d92015-02-03 23:49:31 +08006279out6:
6280 if (nested)
6281 free_page((unsigned long)vmx_msr_bitmap_nested);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006282out5:
6283 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
6284out4:
6285 free_page((unsigned long)vmx_msr_bitmap_longmode);
6286out3:
6287 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
6288out2:
6289 free_page((unsigned long)vmx_msr_bitmap_legacy);
6290out1:
6291 free_page((unsigned long)vmx_io_bitmap_b);
6292out:
6293 free_page((unsigned long)vmx_io_bitmap_a);
6294
6295 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006296}
6297
6298static __exit void hardware_unsetup(void)
6299{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006300 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
6301 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
6302 free_page((unsigned long)vmx_msr_bitmap_legacy);
6303 free_page((unsigned long)vmx_msr_bitmap_longmode);
6304 free_page((unsigned long)vmx_io_bitmap_b);
6305 free_page((unsigned long)vmx_io_bitmap_a);
6306 free_page((unsigned long)vmx_vmwrite_bitmap);
6307 free_page((unsigned long)vmx_vmread_bitmap);
Wincy Van3af18d92015-02-03 23:49:31 +08006308 if (nested)
6309 free_page((unsigned long)vmx_msr_bitmap_nested);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006310
Tiejun Chenf2c76482014-10-28 10:14:47 +08006311 free_kvm_area();
6312}
6313
Avi Kivity6aa8b732006-12-10 02:21:36 -08006314/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006315 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6316 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6317 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006318static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006319{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006320 if (ple_gap)
6321 grow_ple_window(vcpu);
6322
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006323 skip_emulated_instruction(vcpu);
6324 kvm_vcpu_on_spin(vcpu);
6325
6326 return 1;
6327}
6328
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006329static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006330{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006331 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006332 return 1;
6333}
6334
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006335static int handle_mwait(struct kvm_vcpu *vcpu)
6336{
6337 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6338 return handle_nop(vcpu);
6339}
6340
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006341static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6342{
6343 return 1;
6344}
6345
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006346static int handle_monitor(struct kvm_vcpu *vcpu)
6347{
6348 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6349 return handle_nop(vcpu);
6350}
6351
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006352/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006353 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6354 * We could reuse a single VMCS for all the L2 guests, but we also want the
6355 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6356 * allows keeping them loaded on the processor, and in the future will allow
6357 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6358 * every entry if they never change.
6359 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6360 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6361 *
6362 * The following functions allocate and free a vmcs02 in this pool.
6363 */
6364
6365/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6366static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6367{
6368 struct vmcs02_list *item;
6369 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6370 if (item->vmptr == vmx->nested.current_vmptr) {
6371 list_move(&item->list, &vmx->nested.vmcs02_pool);
6372 return &item->vmcs02;
6373 }
6374
6375 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
6376 /* Recycle the least recently used VMCS. */
6377 item = list_entry(vmx->nested.vmcs02_pool.prev,
6378 struct vmcs02_list, list);
6379 item->vmptr = vmx->nested.current_vmptr;
6380 list_move(&item->list, &vmx->nested.vmcs02_pool);
6381 return &item->vmcs02;
6382 }
6383
6384 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02006385 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006386 if (!item)
6387 return NULL;
6388 item->vmcs02.vmcs = alloc_vmcs();
6389 if (!item->vmcs02.vmcs) {
6390 kfree(item);
6391 return NULL;
6392 }
6393 loaded_vmcs_init(&item->vmcs02);
6394 item->vmptr = vmx->nested.current_vmptr;
6395 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
6396 vmx->nested.vmcs02_num++;
6397 return &item->vmcs02;
6398}
6399
6400/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
6401static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
6402{
6403 struct vmcs02_list *item;
6404 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6405 if (item->vmptr == vmptr) {
6406 free_loaded_vmcs(&item->vmcs02);
6407 list_del(&item->list);
6408 kfree(item);
6409 vmx->nested.vmcs02_num--;
6410 return;
6411 }
6412}
6413
6414/*
6415 * Free all VMCSs saved for this vcpu, except the one pointed by
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006416 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
6417 * must be &vmx->vmcs01.
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006418 */
6419static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
6420{
6421 struct vmcs02_list *item, *n;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006422
6423 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006424 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006425 /*
6426 * Something will leak if the above WARN triggers. Better than
6427 * a use-after-free.
6428 */
6429 if (vmx->loaded_vmcs == &item->vmcs02)
6430 continue;
6431
6432 free_loaded_vmcs(&item->vmcs02);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006433 list_del(&item->list);
6434 kfree(item);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006435 vmx->nested.vmcs02_num--;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006436 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006437}
6438
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006439/*
6440 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6441 * set the success or error code of an emulated VMX instruction, as specified
6442 * by Vol 2B, VMX Instruction Reference, "Conventions".
6443 */
6444static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6445{
6446 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6447 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6448 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6449}
6450
6451static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6452{
6453 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6454 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6455 X86_EFLAGS_SF | X86_EFLAGS_OF))
6456 | X86_EFLAGS_CF);
6457}
6458
Abel Gordon145c28d2013-04-18 14:36:55 +03006459static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006460 u32 vm_instruction_error)
6461{
6462 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6463 /*
6464 * failValid writes the error number to the current VMCS, which
6465 * can't be done there isn't a current VMCS.
6466 */
6467 nested_vmx_failInvalid(vcpu);
6468 return;
6469 }
6470 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6471 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6472 X86_EFLAGS_SF | X86_EFLAGS_OF))
6473 | X86_EFLAGS_ZF);
6474 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6475 /*
6476 * We don't need to force a shadow sync because
6477 * VM_INSTRUCTION_ERROR is not shadowed
6478 */
6479}
Abel Gordon145c28d2013-04-18 14:36:55 +03006480
Wincy Vanff651cb2014-12-11 08:52:58 +03006481static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6482{
6483 /* TODO: not to reset guest simply here. */
6484 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6485 pr_warn("kvm: nested vmx abort, indicator %d\n", indicator);
6486}
6487
Jan Kiszkaf41245002014-03-07 20:03:13 +01006488static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6489{
6490 struct vcpu_vmx *vmx =
6491 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6492
6493 vmx->nested.preemption_timer_expired = true;
6494 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6495 kvm_vcpu_kick(&vmx->vcpu);
6496
6497 return HRTIMER_NORESTART;
6498}
6499
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006500/*
Bandan Das19677e32014-05-06 02:19:15 -04006501 * Decode the memory-address operand of a vmx instruction, as recorded on an
6502 * exit caused by such an instruction (run by a guest hypervisor).
6503 * On success, returns 0. When the operand is invalid, returns 1 and throws
6504 * #UD or #GP.
6505 */
6506static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6507 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006508 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006509{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006510 gva_t off;
6511 bool exn;
6512 struct kvm_segment s;
6513
Bandan Das19677e32014-05-06 02:19:15 -04006514 /*
6515 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6516 * Execution", on an exit, vmx_instruction_info holds most of the
6517 * addressing components of the operand. Only the displacement part
6518 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6519 * For how an actual address is calculated from all these components,
6520 * refer to Vol. 1, "Operand Addressing".
6521 */
6522 int scaling = vmx_instruction_info & 3;
6523 int addr_size = (vmx_instruction_info >> 7) & 7;
6524 bool is_reg = vmx_instruction_info & (1u << 10);
6525 int seg_reg = (vmx_instruction_info >> 15) & 7;
6526 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6527 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6528 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6529 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6530
6531 if (is_reg) {
6532 kvm_queue_exception(vcpu, UD_VECTOR);
6533 return 1;
6534 }
6535
6536 /* Addr = segment_base + offset */
6537 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006538 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006539 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006540 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006541 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006542 off += kvm_register_read(vcpu, index_reg)<<scaling;
6543 vmx_get_segment(vcpu, &s, seg_reg);
6544 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006545
6546 if (addr_size == 1) /* 32 bit */
6547 *ret &= 0xffffffff;
6548
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006549 /* Checks for #GP/#SS exceptions. */
6550 exn = false;
6551 if (is_protmode(vcpu)) {
6552 /* Protected mode: apply checks for segment validity in the
6553 * following order:
6554 * - segment type check (#GP(0) may be thrown)
6555 * - usability check (#GP(0)/#SS(0))
6556 * - limit check (#GP(0)/#SS(0))
6557 */
6558 if (wr)
6559 /* #GP(0) if the destination operand is located in a
6560 * read-only data segment or any code segment.
6561 */
6562 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6563 else
6564 /* #GP(0) if the source operand is located in an
6565 * execute-only code segment
6566 */
6567 exn = ((s.type & 0xa) == 8);
6568 }
6569 if (exn) {
6570 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6571 return 1;
6572 }
6573 if (is_long_mode(vcpu)) {
6574 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6575 * non-canonical form. This is an only check for long mode.
6576 */
6577 exn = is_noncanonical_address(*ret);
6578 } else if (is_protmode(vcpu)) {
6579 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6580 */
6581 exn = (s.unusable != 0);
6582 /* Protected mode: #GP(0)/#SS(0) if the memory
6583 * operand is outside the segment limit.
6584 */
6585 exn = exn || (off + sizeof(u64) > s.limit);
6586 }
6587 if (exn) {
6588 kvm_queue_exception_e(vcpu,
6589 seg_reg == VCPU_SREG_SS ?
6590 SS_VECTOR : GP_VECTOR,
6591 0);
6592 return 1;
6593 }
6594
Bandan Das19677e32014-05-06 02:19:15 -04006595 return 0;
6596}
6597
6598/*
Bandan Das3573e222014-05-06 02:19:16 -04006599 * This function performs the various checks including
6600 * - if it's 4KB aligned
6601 * - No bits beyond the physical address width are set
6602 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006603 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006604 */
Bandan Das4291b582014-05-06 02:19:18 -04006605static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6606 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006607{
6608 gva_t gva;
6609 gpa_t vmptr;
6610 struct x86_exception e;
6611 struct page *page;
6612 struct vcpu_vmx *vmx = to_vmx(vcpu);
6613 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6614
6615 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006616 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04006617 return 1;
6618
6619 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6620 sizeof(vmptr), &e)) {
6621 kvm_inject_page_fault(vcpu, &e);
6622 return 1;
6623 }
6624
6625 switch (exit_reason) {
6626 case EXIT_REASON_VMON:
6627 /*
6628 * SDM 3: 24.11.5
6629 * The first 4 bytes of VMXON region contain the supported
6630 * VMCS revision identifier
6631 *
6632 * Note - IA32_VMX_BASIC[48] will never be 1
6633 * for the nested case;
6634 * which replaces physical address width with 32
6635 *
6636 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006637 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04006638 nested_vmx_failInvalid(vcpu);
6639 skip_emulated_instruction(vcpu);
6640 return 1;
6641 }
6642
6643 page = nested_get_page(vcpu, vmptr);
6644 if (page == NULL ||
6645 *(u32 *)kmap(page) != VMCS12_REVISION) {
6646 nested_vmx_failInvalid(vcpu);
6647 kunmap(page);
6648 skip_emulated_instruction(vcpu);
6649 return 1;
6650 }
6651 kunmap(page);
6652 vmx->nested.vmxon_ptr = vmptr;
6653 break;
Bandan Das4291b582014-05-06 02:19:18 -04006654 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006655 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006656 nested_vmx_failValid(vcpu,
6657 VMXERR_VMCLEAR_INVALID_ADDRESS);
6658 skip_emulated_instruction(vcpu);
6659 return 1;
6660 }
Bandan Das3573e222014-05-06 02:19:16 -04006661
Bandan Das4291b582014-05-06 02:19:18 -04006662 if (vmptr == vmx->nested.vmxon_ptr) {
6663 nested_vmx_failValid(vcpu,
6664 VMXERR_VMCLEAR_VMXON_POINTER);
6665 skip_emulated_instruction(vcpu);
6666 return 1;
6667 }
6668 break;
6669 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006670 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006671 nested_vmx_failValid(vcpu,
6672 VMXERR_VMPTRLD_INVALID_ADDRESS);
6673 skip_emulated_instruction(vcpu);
6674 return 1;
6675 }
6676
6677 if (vmptr == vmx->nested.vmxon_ptr) {
6678 nested_vmx_failValid(vcpu,
6679 VMXERR_VMCLEAR_VMXON_POINTER);
6680 skip_emulated_instruction(vcpu);
6681 return 1;
6682 }
6683 break;
Bandan Das3573e222014-05-06 02:19:16 -04006684 default:
6685 return 1; /* shouldn't happen */
6686 }
6687
Bandan Das4291b582014-05-06 02:19:18 -04006688 if (vmpointer)
6689 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04006690 return 0;
6691}
6692
6693/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006694 * Emulate the VMXON instruction.
6695 * Currently, we just remember that VMX is active, and do not save or even
6696 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
6697 * do not currently need to store anything in that guest-allocated memory
6698 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
6699 * argument is different from the VMXON pointer (which the spec says they do).
6700 */
6701static int handle_vmon(struct kvm_vcpu *vcpu)
6702{
6703 struct kvm_segment cs;
6704 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03006705 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006706 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
6707 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006708
6709 /* The Intel VMX Instruction Reference lists a bunch of bits that
6710 * are prerequisite to running VMXON, most notably cr4.VMXE must be
6711 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
6712 * Otherwise, we should fail with #UD. We test these now:
6713 */
6714 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
6715 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
6716 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
6717 kvm_queue_exception(vcpu, UD_VECTOR);
6718 return 1;
6719 }
6720
6721 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6722 if (is_long_mode(vcpu) && !cs.l) {
6723 kvm_queue_exception(vcpu, UD_VECTOR);
6724 return 1;
6725 }
6726
6727 if (vmx_get_cpl(vcpu)) {
6728 kvm_inject_gp(vcpu, 0);
6729 return 1;
6730 }
Bandan Das3573e222014-05-06 02:19:16 -04006731
Bandan Das4291b582014-05-06 02:19:18 -04006732 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04006733 return 1;
6734
Abel Gordon145c28d2013-04-18 14:36:55 +03006735 if (vmx->nested.vmxon) {
6736 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
6737 skip_emulated_instruction(vcpu);
6738 return 1;
6739 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006740
6741 if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
6742 != VMXON_NEEDED_FEATURES) {
6743 kvm_inject_gp(vcpu, 0);
6744 return 1;
6745 }
6746
Abel Gordon8de48832013-04-18 14:37:25 +03006747 if (enable_shadow_vmcs) {
6748 shadow_vmcs = alloc_vmcs();
6749 if (!shadow_vmcs)
6750 return -ENOMEM;
6751 /* mark vmcs as shadow */
6752 shadow_vmcs->revision_id |= (1u << 31);
6753 /* init shadow vmcs */
6754 vmcs_clear(shadow_vmcs);
6755 vmx->nested.current_shadow_vmcs = shadow_vmcs;
6756 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006757
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006758 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
6759 vmx->nested.vmcs02_num = 0;
6760
Jan Kiszkaf41245002014-03-07 20:03:13 +01006761 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
6762 HRTIMER_MODE_REL);
6763 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
6764
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006765 vmx->nested.vmxon = true;
6766
6767 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08006768 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006769 return 1;
6770}
6771
6772/*
6773 * Intel's VMX Instruction Reference specifies a common set of prerequisites
6774 * for running VMX instructions (except VMXON, whose prerequisites are
6775 * slightly different). It also specifies what exception to inject otherwise.
6776 */
6777static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
6778{
6779 struct kvm_segment cs;
6780 struct vcpu_vmx *vmx = to_vmx(vcpu);
6781
6782 if (!vmx->nested.vmxon) {
6783 kvm_queue_exception(vcpu, UD_VECTOR);
6784 return 0;
6785 }
6786
6787 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6788 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
6789 (is_long_mode(vcpu) && !cs.l)) {
6790 kvm_queue_exception(vcpu, UD_VECTOR);
6791 return 0;
6792 }
6793
6794 if (vmx_get_cpl(vcpu)) {
6795 kvm_inject_gp(vcpu, 0);
6796 return 0;
6797 }
6798
6799 return 1;
6800}
6801
Abel Gordone7953d72013-04-18 14:37:55 +03006802static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
6803{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006804 if (vmx->nested.current_vmptr == -1ull)
6805 return;
6806
6807 /* current_vmptr and current_vmcs12 are always set/reset together */
6808 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
6809 return;
6810
Abel Gordon012f83c2013-04-18 14:39:25 +03006811 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006812 /* copy to memory all shadowed fields in case
6813 they were modified */
6814 copy_shadow_to_vmcs12(vmx);
6815 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08006816 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6817 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006818 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03006819 }
Wincy Van705699a2015-02-03 23:58:17 +08006820 vmx->nested.posted_intr_nv = -1;
Abel Gordone7953d72013-04-18 14:37:55 +03006821 kunmap(vmx->nested.current_vmcs12_page);
6822 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006823 vmx->nested.current_vmptr = -1ull;
6824 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03006825}
6826
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006827/*
6828 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
6829 * just stops using VMX.
6830 */
6831static void free_nested(struct vcpu_vmx *vmx)
6832{
6833 if (!vmx->nested.vmxon)
6834 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006835
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006836 vmx->nested.vmxon = false;
Wanpeng Li5c614b32015-10-13 09:18:36 -07006837 free_vpid(vmx->nested.vpid02);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006838 nested_release_vmcs12(vmx);
Abel Gordone7953d72013-04-18 14:37:55 +03006839 if (enable_shadow_vmcs)
6840 free_vmcs(vmx->nested.current_shadow_vmcs);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006841 /* Unpin physical memory we referred to in current vmcs02 */
6842 if (vmx->nested.apic_access_page) {
6843 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02006844 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006845 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006846 if (vmx->nested.virtual_apic_page) {
6847 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02006848 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006849 }
Wincy Van705699a2015-02-03 23:58:17 +08006850 if (vmx->nested.pi_desc_page) {
6851 kunmap(vmx->nested.pi_desc_page);
6852 nested_release_page(vmx->nested.pi_desc_page);
6853 vmx->nested.pi_desc_page = NULL;
6854 vmx->nested.pi_desc = NULL;
6855 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006856
6857 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006858}
6859
6860/* Emulate the VMXOFF instruction */
6861static int handle_vmoff(struct kvm_vcpu *vcpu)
6862{
6863 if (!nested_vmx_check_permission(vcpu))
6864 return 1;
6865 free_nested(to_vmx(vcpu));
6866 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08006867 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006868 return 1;
6869}
6870
Nadav Har'El27d6c862011-05-25 23:06:59 +03006871/* Emulate the VMCLEAR instruction */
6872static int handle_vmclear(struct kvm_vcpu *vcpu)
6873{
6874 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03006875 gpa_t vmptr;
6876 struct vmcs12 *vmcs12;
6877 struct page *page;
Nadav Har'El27d6c862011-05-25 23:06:59 +03006878
6879 if (!nested_vmx_check_permission(vcpu))
6880 return 1;
6881
Bandan Das4291b582014-05-06 02:19:18 -04006882 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03006883 return 1;
6884
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006885 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03006886 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03006887
6888 page = nested_get_page(vcpu, vmptr);
6889 if (page == NULL) {
6890 /*
6891 * For accurate processor emulation, VMCLEAR beyond available
6892 * physical memory should do nothing at all. However, it is
6893 * possible that a nested vmx bug, not a guest hypervisor bug,
6894 * resulted in this case, so let's shut down before doing any
6895 * more damage:
6896 */
6897 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6898 return 1;
6899 }
6900 vmcs12 = kmap(page);
6901 vmcs12->launch_state = 0;
6902 kunmap(page);
6903 nested_release_page(page);
6904
6905 nested_free_vmcs02(vmx, vmptr);
6906
6907 skip_emulated_instruction(vcpu);
6908 nested_vmx_succeed(vcpu);
6909 return 1;
6910}
6911
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006912static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
6913
6914/* Emulate the VMLAUNCH instruction */
6915static int handle_vmlaunch(struct kvm_vcpu *vcpu)
6916{
6917 return nested_vmx_run(vcpu, true);
6918}
6919
6920/* Emulate the VMRESUME instruction */
6921static int handle_vmresume(struct kvm_vcpu *vcpu)
6922{
6923
6924 return nested_vmx_run(vcpu, false);
6925}
6926
Nadav Har'El49f705c2011-05-25 23:08:30 +03006927enum vmcs_field_type {
6928 VMCS_FIELD_TYPE_U16 = 0,
6929 VMCS_FIELD_TYPE_U64 = 1,
6930 VMCS_FIELD_TYPE_U32 = 2,
6931 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
6932};
6933
6934static inline int vmcs_field_type(unsigned long field)
6935{
6936 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
6937 return VMCS_FIELD_TYPE_U32;
6938 return (field >> 13) & 0x3 ;
6939}
6940
6941static inline int vmcs_field_readonly(unsigned long field)
6942{
6943 return (((field >> 10) & 0x3) == 1);
6944}
6945
6946/*
6947 * Read a vmcs12 field. Since these can have varying lengths and we return
6948 * one type, we chose the biggest type (u64) and zero-extend the return value
6949 * to that size. Note that the caller, handle_vmread, might need to use only
6950 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
6951 * 64-bit fields are to be returned).
6952 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006953static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
6954 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03006955{
6956 short offset = vmcs_field_to_offset(field);
6957 char *p;
6958
6959 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006960 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006961
6962 p = ((char *)(get_vmcs12(vcpu))) + offset;
6963
6964 switch (vmcs_field_type(field)) {
6965 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6966 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006967 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006968 case VMCS_FIELD_TYPE_U16:
6969 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006970 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006971 case VMCS_FIELD_TYPE_U32:
6972 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006973 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006974 case VMCS_FIELD_TYPE_U64:
6975 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006976 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006977 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006978 WARN_ON(1);
6979 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006980 }
6981}
6982
Abel Gordon20b97fe2013-04-18 14:36:25 +03006983
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006984static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
6985 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03006986 short offset = vmcs_field_to_offset(field);
6987 char *p = ((char *) get_vmcs12(vcpu)) + offset;
6988 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006989 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006990
6991 switch (vmcs_field_type(field)) {
6992 case VMCS_FIELD_TYPE_U16:
6993 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006994 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006995 case VMCS_FIELD_TYPE_U32:
6996 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006997 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006998 case VMCS_FIELD_TYPE_U64:
6999 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007000 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007001 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7002 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007003 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007004 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007005 WARN_ON(1);
7006 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03007007 }
7008
7009}
7010
Abel Gordon16f5b902013-04-18 14:38:25 +03007011static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
7012{
7013 int i;
7014 unsigned long field;
7015 u64 field_value;
7016 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02007017 const unsigned long *fields = shadow_read_write_fields;
7018 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03007019
Jan Kiszka282da872014-10-08 18:05:39 +02007020 preempt_disable();
7021
Abel Gordon16f5b902013-04-18 14:38:25 +03007022 vmcs_load(shadow_vmcs);
7023
7024 for (i = 0; i < num_fields; i++) {
7025 field = fields[i];
7026 switch (vmcs_field_type(field)) {
7027 case VMCS_FIELD_TYPE_U16:
7028 field_value = vmcs_read16(field);
7029 break;
7030 case VMCS_FIELD_TYPE_U32:
7031 field_value = vmcs_read32(field);
7032 break;
7033 case VMCS_FIELD_TYPE_U64:
7034 field_value = vmcs_read64(field);
7035 break;
7036 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7037 field_value = vmcs_readl(field);
7038 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007039 default:
7040 WARN_ON(1);
7041 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03007042 }
7043 vmcs12_write_any(&vmx->vcpu, field, field_value);
7044 }
7045
7046 vmcs_clear(shadow_vmcs);
7047 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02007048
7049 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03007050}
7051
Abel Gordonc3114422013-04-18 14:38:55 +03007052static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
7053{
Mathias Krausec2bae892013-06-26 20:36:21 +02007054 const unsigned long *fields[] = {
7055 shadow_read_write_fields,
7056 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03007057 };
Mathias Krausec2bae892013-06-26 20:36:21 +02007058 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03007059 max_shadow_read_write_fields,
7060 max_shadow_read_only_fields
7061 };
7062 int i, q;
7063 unsigned long field;
7064 u64 field_value = 0;
7065 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
7066
7067 vmcs_load(shadow_vmcs);
7068
Mathias Krausec2bae892013-06-26 20:36:21 +02007069 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03007070 for (i = 0; i < max_fields[q]; i++) {
7071 field = fields[q][i];
7072 vmcs12_read_any(&vmx->vcpu, field, &field_value);
7073
7074 switch (vmcs_field_type(field)) {
7075 case VMCS_FIELD_TYPE_U16:
7076 vmcs_write16(field, (u16)field_value);
7077 break;
7078 case VMCS_FIELD_TYPE_U32:
7079 vmcs_write32(field, (u32)field_value);
7080 break;
7081 case VMCS_FIELD_TYPE_U64:
7082 vmcs_write64(field, (u64)field_value);
7083 break;
7084 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
7085 vmcs_writel(field, (long)field_value);
7086 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007087 default:
7088 WARN_ON(1);
7089 break;
Abel Gordonc3114422013-04-18 14:38:55 +03007090 }
7091 }
7092 }
7093
7094 vmcs_clear(shadow_vmcs);
7095 vmcs_load(vmx->loaded_vmcs->vmcs);
7096}
7097
Nadav Har'El49f705c2011-05-25 23:08:30 +03007098/*
7099 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
7100 * used before) all generate the same failure when it is missing.
7101 */
7102static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
7103{
7104 struct vcpu_vmx *vmx = to_vmx(vcpu);
7105 if (vmx->nested.current_vmptr == -1ull) {
7106 nested_vmx_failInvalid(vcpu);
7107 skip_emulated_instruction(vcpu);
7108 return 0;
7109 }
7110 return 1;
7111}
7112
7113static int handle_vmread(struct kvm_vcpu *vcpu)
7114{
7115 unsigned long field;
7116 u64 field_value;
7117 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7118 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7119 gva_t gva = 0;
7120
7121 if (!nested_vmx_check_permission(vcpu) ||
7122 !nested_vmx_check_vmcs12(vcpu))
7123 return 1;
7124
7125 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03007126 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007127 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007128 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007129 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7130 skip_emulated_instruction(vcpu);
7131 return 1;
7132 }
7133 /*
7134 * Now copy part of this value to register or memory, as requested.
7135 * Note that the number of bits actually copied is 32 or 64 depending
7136 * on the guest's mode (32 or 64 bit), not on the given field's length.
7137 */
7138 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03007139 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03007140 field_value);
7141 } else {
7142 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007143 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007144 return 1;
7145 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
7146 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
7147 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
7148 }
7149
7150 nested_vmx_succeed(vcpu);
7151 skip_emulated_instruction(vcpu);
7152 return 1;
7153}
7154
7155
7156static int handle_vmwrite(struct kvm_vcpu *vcpu)
7157{
7158 unsigned long field;
7159 gva_t gva;
7160 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7161 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007162 /* The value to write might be 32 or 64 bits, depending on L1's long
7163 * mode, and eventually we need to write that into a field of several
7164 * possible lengths. The code below first zero-extends the value to 64
7165 * bit (field_value), and then copies only the approriate number of
7166 * bits into the vmcs12 field.
7167 */
7168 u64 field_value = 0;
7169 struct x86_exception e;
7170
7171 if (!nested_vmx_check_permission(vcpu) ||
7172 !nested_vmx_check_vmcs12(vcpu))
7173 return 1;
7174
7175 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007176 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007177 (((vmx_instruction_info) >> 3) & 0xf));
7178 else {
7179 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007180 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007181 return 1;
7182 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007183 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007184 kvm_inject_page_fault(vcpu, &e);
7185 return 1;
7186 }
7187 }
7188
7189
Nadav Amit27e6fb52014-06-18 17:19:26 +03007190 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007191 if (vmcs_field_readonly(field)) {
7192 nested_vmx_failValid(vcpu,
7193 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7194 skip_emulated_instruction(vcpu);
7195 return 1;
7196 }
7197
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007198 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007199 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7200 skip_emulated_instruction(vcpu);
7201 return 1;
7202 }
7203
7204 nested_vmx_succeed(vcpu);
7205 skip_emulated_instruction(vcpu);
7206 return 1;
7207}
7208
Nadav Har'El63846662011-05-25 23:07:29 +03007209/* Emulate the VMPTRLD instruction */
7210static int handle_vmptrld(struct kvm_vcpu *vcpu)
7211{
7212 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007213 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007214
7215 if (!nested_vmx_check_permission(vcpu))
7216 return 1;
7217
Bandan Das4291b582014-05-06 02:19:18 -04007218 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007219 return 1;
7220
Nadav Har'El63846662011-05-25 23:07:29 +03007221 if (vmx->nested.current_vmptr != vmptr) {
7222 struct vmcs12 *new_vmcs12;
7223 struct page *page;
7224 page = nested_get_page(vcpu, vmptr);
7225 if (page == NULL) {
7226 nested_vmx_failInvalid(vcpu);
7227 skip_emulated_instruction(vcpu);
7228 return 1;
7229 }
7230 new_vmcs12 = kmap(page);
7231 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7232 kunmap(page);
7233 nested_release_page_clean(page);
7234 nested_vmx_failValid(vcpu,
7235 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7236 skip_emulated_instruction(vcpu);
7237 return 1;
7238 }
Nadav Har'El63846662011-05-25 23:07:29 +03007239
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007240 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007241 vmx->nested.current_vmptr = vmptr;
7242 vmx->nested.current_vmcs12 = new_vmcs12;
7243 vmx->nested.current_vmcs12_page = page;
Abel Gordon012f83c2013-04-18 14:39:25 +03007244 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007245 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7246 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007247 vmcs_write64(VMCS_LINK_POINTER,
7248 __pa(vmx->nested.current_shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007249 vmx->nested.sync_shadow_vmcs = true;
7250 }
Nadav Har'El63846662011-05-25 23:07:29 +03007251 }
7252
7253 nested_vmx_succeed(vcpu);
7254 skip_emulated_instruction(vcpu);
7255 return 1;
7256}
7257
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007258/* Emulate the VMPTRST instruction */
7259static int handle_vmptrst(struct kvm_vcpu *vcpu)
7260{
7261 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7262 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7263 gva_t vmcs_gva;
7264 struct x86_exception e;
7265
7266 if (!nested_vmx_check_permission(vcpu))
7267 return 1;
7268
7269 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007270 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007271 return 1;
7272 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
7273 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7274 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7275 sizeof(u64), &e)) {
7276 kvm_inject_page_fault(vcpu, &e);
7277 return 1;
7278 }
7279 nested_vmx_succeed(vcpu);
7280 skip_emulated_instruction(vcpu);
7281 return 1;
7282}
7283
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007284/* Emulate the INVEPT instruction */
7285static int handle_invept(struct kvm_vcpu *vcpu)
7286{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007287 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007288 u32 vmx_instruction_info, types;
7289 unsigned long type;
7290 gva_t gva;
7291 struct x86_exception e;
7292 struct {
7293 u64 eptp, gpa;
7294 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007295
Wincy Vanb9c237b2015-02-03 23:56:30 +08007296 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7297 SECONDARY_EXEC_ENABLE_EPT) ||
7298 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007299 kvm_queue_exception(vcpu, UD_VECTOR);
7300 return 1;
7301 }
7302
7303 if (!nested_vmx_check_permission(vcpu))
7304 return 1;
7305
7306 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7307 kvm_queue_exception(vcpu, UD_VECTOR);
7308 return 1;
7309 }
7310
7311 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007312 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007313
Wincy Vanb9c237b2015-02-03 23:56:30 +08007314 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007315
7316 if (!(types & (1UL << type))) {
7317 nested_vmx_failValid(vcpu,
7318 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7319 return 1;
7320 }
7321
7322 /* According to the Intel VMX instruction reference, the memory
7323 * operand is read even if it isn't needed (e.g., for type==global)
7324 */
7325 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007326 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007327 return 1;
7328 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7329 sizeof(operand), &e)) {
7330 kvm_inject_page_fault(vcpu, &e);
7331 return 1;
7332 }
7333
7334 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007335 case VMX_EPT_EXTENT_GLOBAL:
7336 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007337 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007338 nested_vmx_succeed(vcpu);
7339 break;
7340 default:
Bandan Das4b855072014-04-19 18:17:44 -04007341 /* Trap single context invalidation invept calls */
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007342 BUG_ON(1);
7343 break;
7344 }
7345
7346 skip_emulated_instruction(vcpu);
7347 return 1;
7348}
7349
Petr Matouseka642fc32014-09-23 20:22:30 +02007350static int handle_invvpid(struct kvm_vcpu *vcpu)
7351{
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007352 struct vcpu_vmx *vmx = to_vmx(vcpu);
7353 u32 vmx_instruction_info;
7354 unsigned long type, types;
7355 gva_t gva;
7356 struct x86_exception e;
7357 int vpid;
7358
7359 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7360 SECONDARY_EXEC_ENABLE_VPID) ||
7361 !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) {
7362 kvm_queue_exception(vcpu, UD_VECTOR);
7363 return 1;
7364 }
7365
7366 if (!nested_vmx_check_permission(vcpu))
7367 return 1;
7368
7369 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7370 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
7371
7372 types = (vmx->nested.nested_vmx_vpid_caps >> 8) & 0x7;
7373
7374 if (!(types & (1UL << type))) {
7375 nested_vmx_failValid(vcpu,
7376 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7377 return 1;
7378 }
7379
7380 /* according to the intel vmx instruction reference, the memory
7381 * operand is read even if it isn't needed (e.g., for type==global)
7382 */
7383 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
7384 vmx_instruction_info, false, &gva))
7385 return 1;
7386 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid,
7387 sizeof(u32), &e)) {
7388 kvm_inject_page_fault(vcpu, &e);
7389 return 1;
7390 }
7391
7392 switch (type) {
7393 case VMX_VPID_EXTENT_ALL_CONTEXT:
7394 if (get_vmcs12(vcpu)->virtual_processor_id == 0) {
7395 nested_vmx_failValid(vcpu,
7396 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7397 return 1;
7398 }
Wanpeng Li5c614b32015-10-13 09:18:36 -07007399 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
Wanpeng Li99b83ac2015-10-13 09:12:21 -07007400 nested_vmx_succeed(vcpu);
7401 break;
7402 default:
7403 /* Trap single context invalidation invvpid calls */
7404 BUG_ON(1);
7405 break;
7406 }
7407
7408 skip_emulated_instruction(vcpu);
Petr Matouseka642fc32014-09-23 20:22:30 +02007409 return 1;
7410}
7411
Kai Huang843e4332015-01-28 10:54:28 +08007412static int handle_pml_full(struct kvm_vcpu *vcpu)
7413{
7414 unsigned long exit_qualification;
7415
7416 trace_kvm_pml_full(vcpu->vcpu_id);
7417
7418 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7419
7420 /*
7421 * PML buffer FULL happened while executing iret from NMI,
7422 * "blocked by NMI" bit has to be set before next VM entry.
7423 */
7424 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7425 cpu_has_virtual_nmis() &&
7426 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7427 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7428 GUEST_INTR_STATE_NMI);
7429
7430 /*
7431 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7432 * here.., and there's no userspace involvement needed for PML.
7433 */
7434 return 1;
7435}
7436
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08007437static int handle_pcommit(struct kvm_vcpu *vcpu)
7438{
7439 /* we never catch pcommit instruct for L1 guest. */
7440 WARN_ON(1);
7441 return 1;
7442}
7443
Nadav Har'El0140cae2011-05-25 23:06:28 +03007444/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007445 * The exit handlers return 1 if the exit was handled fully and guest execution
7446 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7447 * to be done to userspace and return 0.
7448 */
Mathias Krause772e0312012-08-30 01:30:19 +02007449static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007450 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7451 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007452 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007453 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007454 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007455 [EXIT_REASON_CR_ACCESS] = handle_cr,
7456 [EXIT_REASON_DR_ACCESS] = handle_dr,
7457 [EXIT_REASON_CPUID] = handle_cpuid,
7458 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7459 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7460 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7461 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007462 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007463 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007464 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007465 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007466 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007467 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007468 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007469 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007470 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007471 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007472 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007473 [EXIT_REASON_VMOFF] = handle_vmoff,
7474 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007475 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7476 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007477 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007478 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007479 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007480 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007481 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007482 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007483 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7484 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007485 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007486 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007487 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007488 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007489 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007490 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007491 [EXIT_REASON_XSAVES] = handle_xsaves,
7492 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007493 [EXIT_REASON_PML_FULL] = handle_pml_full,
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08007494 [EXIT_REASON_PCOMMIT] = handle_pcommit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007495};
7496
7497static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007498 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007499
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007500static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7501 struct vmcs12 *vmcs12)
7502{
7503 unsigned long exit_qualification;
7504 gpa_t bitmap, last_bitmap;
7505 unsigned int port;
7506 int size;
7507 u8 b;
7508
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007509 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007510 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007511
7512 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7513
7514 port = exit_qualification >> 16;
7515 size = (exit_qualification & 7) + 1;
7516
7517 last_bitmap = (gpa_t)-1;
7518 b = -1;
7519
7520 while (size > 0) {
7521 if (port < 0x8000)
7522 bitmap = vmcs12->io_bitmap_a;
7523 else if (port < 0x10000)
7524 bitmap = vmcs12->io_bitmap_b;
7525 else
Joe Perches1d804d02015-03-30 16:46:09 -07007526 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007527 bitmap += (port & 0x7fff) / 8;
7528
7529 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007530 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007531 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007532 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007533 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007534
7535 port++;
7536 size--;
7537 last_bitmap = bitmap;
7538 }
7539
Joe Perches1d804d02015-03-30 16:46:09 -07007540 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007541}
7542
Nadav Har'El644d7112011-05-25 23:12:35 +03007543/*
7544 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7545 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7546 * disinterest in the current event (read or write a specific MSR) by using an
7547 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7548 */
7549static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7550 struct vmcs12 *vmcs12, u32 exit_reason)
7551{
7552 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7553 gpa_t bitmap;
7554
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007555 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007556 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007557
7558 /*
7559 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7560 * for the four combinations of read/write and low/high MSR numbers.
7561 * First we need to figure out which of the four to use:
7562 */
7563 bitmap = vmcs12->msr_bitmap;
7564 if (exit_reason == EXIT_REASON_MSR_WRITE)
7565 bitmap += 2048;
7566 if (msr_index >= 0xc0000000) {
7567 msr_index -= 0xc0000000;
7568 bitmap += 1024;
7569 }
7570
7571 /* Then read the msr_index'th bit from this bitmap: */
7572 if (msr_index < 1024*8) {
7573 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007574 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007575 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007576 return 1 & (b >> (msr_index & 7));
7577 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007578 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03007579}
7580
7581/*
7582 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7583 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7584 * intercept (via guest_host_mask etc.) the current event.
7585 */
7586static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7587 struct vmcs12 *vmcs12)
7588{
7589 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7590 int cr = exit_qualification & 15;
7591 int reg = (exit_qualification >> 8) & 15;
Nadav Amit1e32c072014-06-18 17:19:25 +03007592 unsigned long val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03007593
7594 switch ((exit_qualification >> 4) & 3) {
7595 case 0: /* mov to cr */
7596 switch (cr) {
7597 case 0:
7598 if (vmcs12->cr0_guest_host_mask &
7599 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007600 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007601 break;
7602 case 3:
7603 if ((vmcs12->cr3_target_count >= 1 &&
7604 vmcs12->cr3_target_value0 == val) ||
7605 (vmcs12->cr3_target_count >= 2 &&
7606 vmcs12->cr3_target_value1 == val) ||
7607 (vmcs12->cr3_target_count >= 3 &&
7608 vmcs12->cr3_target_value2 == val) ||
7609 (vmcs12->cr3_target_count >= 4 &&
7610 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07007611 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007612 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007613 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007614 break;
7615 case 4:
7616 if (vmcs12->cr4_guest_host_mask &
7617 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07007618 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007619 break;
7620 case 8:
7621 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007622 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007623 break;
7624 }
7625 break;
7626 case 2: /* clts */
7627 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
7628 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007629 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007630 break;
7631 case 1: /* mov from cr */
7632 switch (cr) {
7633 case 3:
7634 if (vmcs12->cpu_based_vm_exec_control &
7635 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007636 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007637 break;
7638 case 8:
7639 if (vmcs12->cpu_based_vm_exec_control &
7640 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007641 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007642 break;
7643 }
7644 break;
7645 case 3: /* lmsw */
7646 /*
7647 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
7648 * cr0. Other attempted changes are ignored, with no exit.
7649 */
7650 if (vmcs12->cr0_guest_host_mask & 0xe &
7651 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007652 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007653 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
7654 !(vmcs12->cr0_read_shadow & 0x1) &&
7655 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07007656 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007657 break;
7658 }
Joe Perches1d804d02015-03-30 16:46:09 -07007659 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007660}
7661
7662/*
7663 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
7664 * should handle it ourselves in L0 (and then continue L2). Only call this
7665 * when in is_guest_mode (L2).
7666 */
7667static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
7668{
Nadav Har'El644d7112011-05-25 23:12:35 +03007669 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7670 struct vcpu_vmx *vmx = to_vmx(vcpu);
7671 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01007672 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03007673
Jan Kiszka542060e2014-01-04 18:47:21 +01007674 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
7675 vmcs_readl(EXIT_QUALIFICATION),
7676 vmx->idt_vectoring_info,
7677 intr_info,
7678 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
7679 KVM_ISA_VMX);
7680
Nadav Har'El644d7112011-05-25 23:12:35 +03007681 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07007682 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007683
7684 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02007685 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
7686 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07007687 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007688 }
7689
7690 switch (exit_reason) {
7691 case EXIT_REASON_EXCEPTION_NMI:
7692 if (!is_exception(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07007693 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007694 else if (is_page_fault(intr_info))
7695 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01007696 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01007697 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007698 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007699 return vmcs12->exception_bitmap &
7700 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
7701 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07007702 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007703 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07007704 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007705 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02007706 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03007707 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02007708 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03007709 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07007710 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007711 case EXIT_REASON_CPUID:
Marcelo Tosattibc613492014-09-18 18:24:57 -03007712 if (kvm_register_read(vcpu, VCPU_REGS_RAX) == 0xa)
Joe Perches1d804d02015-03-30 16:46:09 -07007713 return false;
7714 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007715 case EXIT_REASON_HLT:
7716 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
7717 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07007718 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007719 case EXIT_REASON_INVLPG:
7720 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
7721 case EXIT_REASON_RDPMC:
7722 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01007723 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03007724 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
7725 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
7726 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
7727 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
7728 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
7729 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02007730 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03007731 /*
7732 * VMX instructions trap unconditionally. This allows L1 to
7733 * emulate them for its L2 guest, i.e., allows 3-level nesting!
7734 */
Joe Perches1d804d02015-03-30 16:46:09 -07007735 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007736 case EXIT_REASON_CR_ACCESS:
7737 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
7738 case EXIT_REASON_DR_ACCESS:
7739 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
7740 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007741 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03007742 case EXIT_REASON_MSR_READ:
7743 case EXIT_REASON_MSR_WRITE:
7744 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
7745 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07007746 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007747 case EXIT_REASON_MWAIT_INSTRUCTION:
7748 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007749 case EXIT_REASON_MONITOR_TRAP_FLAG:
7750 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03007751 case EXIT_REASON_MONITOR_INSTRUCTION:
7752 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
7753 case EXIT_REASON_PAUSE_INSTRUCTION:
7754 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
7755 nested_cpu_has2(vmcs12,
7756 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
7757 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07007758 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007759 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007760 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03007761 case EXIT_REASON_APIC_ACCESS:
7762 return nested_cpu_has2(vmcs12,
7763 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08007764 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08007765 case EXIT_REASON_EOI_INDUCED:
7766 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07007767 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007768 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03007769 /*
7770 * L0 always deals with the EPT violation. If nested EPT is
7771 * used, and the nested mmu code discovers that the address is
7772 * missing in the guest EPT table (EPT12), the EPT violation
7773 * will be injected with nested_ept_inject_page_fault()
7774 */
Joe Perches1d804d02015-03-30 16:46:09 -07007775 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007776 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03007777 /*
7778 * L2 never uses directly L1's EPT, but rather L0's own EPT
7779 * table (shadow on EPT) or a merged EPT table that L0 built
7780 * (EPT on EPT). So any problems with the structure of the
7781 * table is L0's fault.
7782 */
Joe Perches1d804d02015-03-30 16:46:09 -07007783 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007784 case EXIT_REASON_WBINVD:
7785 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
7786 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07007787 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08007788 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
7789 /*
7790 * This should never happen, since it is not possible to
7791 * set XSS to a non-zero value---neither in L1 nor in L2.
7792 * If if it were, XSS would have to be checked against
7793 * the XSS exit bitmap in vmcs12.
7794 */
7795 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08007796 case EXIT_REASON_PCOMMIT:
7797 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_PCOMMIT);
Nadav Har'El644d7112011-05-25 23:12:35 +03007798 default:
Joe Perches1d804d02015-03-30 16:46:09 -07007799 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007800 }
7801}
7802
Avi Kivity586f9602010-11-18 13:09:54 +02007803static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
7804{
7805 *info1 = vmcs_readl(EXIT_QUALIFICATION);
7806 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
7807}
7808
Kai Huanga3eaa862015-11-04 13:46:05 +08007809static int vmx_create_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08007810{
7811 struct page *pml_pg;
Kai Huang843e4332015-01-28 10:54:28 +08007812
7813 pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
7814 if (!pml_pg)
7815 return -ENOMEM;
7816
7817 vmx->pml_pg = pml_pg;
7818
7819 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
7820 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
7821
Kai Huang843e4332015-01-28 10:54:28 +08007822 return 0;
7823}
7824
Kai Huanga3eaa862015-11-04 13:46:05 +08007825static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
Kai Huang843e4332015-01-28 10:54:28 +08007826{
Kai Huanga3eaa862015-11-04 13:46:05 +08007827 if (vmx->pml_pg) {
7828 __free_page(vmx->pml_pg);
7829 vmx->pml_pg = NULL;
7830 }
Kai Huang843e4332015-01-28 10:54:28 +08007831}
7832
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007833static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08007834{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007835 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08007836 u64 *pml_buf;
7837 u16 pml_idx;
7838
7839 pml_idx = vmcs_read16(GUEST_PML_INDEX);
7840
7841 /* Do nothing if PML buffer is empty */
7842 if (pml_idx == (PML_ENTITY_NUM - 1))
7843 return;
7844
7845 /* PML index always points to next available PML buffer entity */
7846 if (pml_idx >= PML_ENTITY_NUM)
7847 pml_idx = 0;
7848 else
7849 pml_idx++;
7850
7851 pml_buf = page_address(vmx->pml_pg);
7852 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
7853 u64 gpa;
7854
7855 gpa = pml_buf[pml_idx];
7856 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007857 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08007858 }
7859
7860 /* reset PML index */
7861 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
7862}
7863
7864/*
7865 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
7866 * Called before reporting dirty_bitmap to userspace.
7867 */
7868static void kvm_flush_pml_buffers(struct kvm *kvm)
7869{
7870 int i;
7871 struct kvm_vcpu *vcpu;
7872 /*
7873 * We only need to kick vcpu out of guest mode here, as PML buffer
7874 * is flushed at beginning of all VMEXITs, and it's obvious that only
7875 * vcpus running in guest are possible to have unflushed GPAs in PML
7876 * buffer.
7877 */
7878 kvm_for_each_vcpu(i, vcpu, kvm)
7879 kvm_vcpu_kick(vcpu);
7880}
7881
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02007882static void vmx_dump_sel(char *name, uint32_t sel)
7883{
7884 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
7885 name, vmcs_read32(sel),
7886 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
7887 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
7888 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
7889}
7890
7891static void vmx_dump_dtsel(char *name, uint32_t limit)
7892{
7893 pr_err("%s limit=0x%08x, base=0x%016lx\n",
7894 name, vmcs_read32(limit),
7895 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
7896}
7897
7898static void dump_vmcs(void)
7899{
7900 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
7901 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
7902 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7903 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
7904 u32 secondary_exec_control = 0;
7905 unsigned long cr4 = vmcs_readl(GUEST_CR4);
7906 u64 efer = vmcs_readl(GUEST_IA32_EFER);
7907 int i, n;
7908
7909 if (cpu_has_secondary_exec_ctrls())
7910 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7911
7912 pr_err("*** Guest State ***\n");
7913 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
7914 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
7915 vmcs_readl(CR0_GUEST_HOST_MASK));
7916 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
7917 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
7918 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
7919 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
7920 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
7921 {
7922 pr_err("PDPTR0 = 0x%016lx PDPTR1 = 0x%016lx\n",
7923 vmcs_readl(GUEST_PDPTR0), vmcs_readl(GUEST_PDPTR1));
7924 pr_err("PDPTR2 = 0x%016lx PDPTR3 = 0x%016lx\n",
7925 vmcs_readl(GUEST_PDPTR2), vmcs_readl(GUEST_PDPTR3));
7926 }
7927 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
7928 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
7929 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
7930 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
7931 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
7932 vmcs_readl(GUEST_SYSENTER_ESP),
7933 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
7934 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
7935 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
7936 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
7937 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
7938 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
7939 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
7940 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
7941 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
7942 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
7943 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
7944 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
7945 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
7946 pr_err("EFER = 0x%016llx PAT = 0x%016lx\n",
7947 efer, vmcs_readl(GUEST_IA32_PAT));
7948 pr_err("DebugCtl = 0x%016lx DebugExceptions = 0x%016lx\n",
7949 vmcs_readl(GUEST_IA32_DEBUGCTL),
7950 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
7951 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
7952 pr_err("PerfGlobCtl = 0x%016lx\n",
7953 vmcs_readl(GUEST_IA32_PERF_GLOBAL_CTRL));
7954 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
7955 pr_err("BndCfgS = 0x%016lx\n", vmcs_readl(GUEST_BNDCFGS));
7956 pr_err("Interruptibility = %08x ActivityState = %08x\n",
7957 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
7958 vmcs_read32(GUEST_ACTIVITY_STATE));
7959 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
7960 pr_err("InterruptStatus = %04x\n",
7961 vmcs_read16(GUEST_INTR_STATUS));
7962
7963 pr_err("*** Host State ***\n");
7964 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
7965 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
7966 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
7967 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
7968 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
7969 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
7970 vmcs_read16(HOST_TR_SELECTOR));
7971 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
7972 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
7973 vmcs_readl(HOST_TR_BASE));
7974 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
7975 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
7976 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
7977 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
7978 vmcs_readl(HOST_CR4));
7979 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
7980 vmcs_readl(HOST_IA32_SYSENTER_ESP),
7981 vmcs_read32(HOST_IA32_SYSENTER_CS),
7982 vmcs_readl(HOST_IA32_SYSENTER_EIP));
7983 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
7984 pr_err("EFER = 0x%016lx PAT = 0x%016lx\n",
7985 vmcs_readl(HOST_IA32_EFER), vmcs_readl(HOST_IA32_PAT));
7986 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
7987 pr_err("PerfGlobCtl = 0x%016lx\n",
7988 vmcs_readl(HOST_IA32_PERF_GLOBAL_CTRL));
7989
7990 pr_err("*** Control State ***\n");
7991 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
7992 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
7993 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
7994 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
7995 vmcs_read32(EXCEPTION_BITMAP),
7996 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
7997 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
7998 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
7999 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8000 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
8001 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
8002 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
8003 vmcs_read32(VM_EXIT_INTR_INFO),
8004 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
8005 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
8006 pr_err(" reason=%08x qualification=%016lx\n",
8007 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
8008 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
8009 vmcs_read32(IDT_VECTORING_INFO_FIELD),
8010 vmcs_read32(IDT_VECTORING_ERROR_CODE));
8011 pr_err("TSC Offset = 0x%016lx\n", vmcs_readl(TSC_OFFSET));
8012 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
8013 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
8014 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
8015 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
8016 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
8017 pr_err("EPT pointer = 0x%016lx\n", vmcs_readl(EPT_POINTER));
8018 n = vmcs_read32(CR3_TARGET_COUNT);
8019 for (i = 0; i + 1 < n; i += 4)
8020 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
8021 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
8022 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
8023 if (i < n)
8024 pr_err("CR3 target%u=%016lx\n",
8025 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
8026 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
8027 pr_err("PLE Gap=%08x Window=%08x\n",
8028 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
8029 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
8030 pr_err("Virtual processor ID = 0x%04x\n",
8031 vmcs_read16(VIRTUAL_PROCESSOR_ID));
8032}
8033
Avi Kivity6aa8b732006-12-10 02:21:36 -08008034/*
8035 * The guest has exited. See if we can fix it or if we need userspace
8036 * assistance.
8037 */
Avi Kivity851ba692009-08-24 11:10:17 +03008038static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008039{
Avi Kivity29bd8a72007-09-10 17:27:03 +03008040 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08008041 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02008042 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03008043
Kai Huang843e4332015-01-28 10:54:28 +08008044 /*
8045 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
8046 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
8047 * querying dirty_bitmap, we only need to kick all vcpus out of guest
8048 * mode as if vcpus is in root mode, the PML buffer must has been
8049 * flushed already.
8050 */
8051 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02008052 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08008053
Mohammed Gamal80ced182009-09-01 12:48:18 +02008054 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02008055 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02008056 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01008057
Nadav Har'El644d7112011-05-25 23:12:35 +03008058 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01008059 nested_vmx_vmexit(vcpu, exit_reason,
8060 vmcs_read32(VM_EXIT_INTR_INFO),
8061 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03008062 return 1;
8063 }
8064
Mohammed Gamal51207022010-05-31 22:40:54 +03008065 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02008066 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03008067 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8068 vcpu->run->fail_entry.hardware_entry_failure_reason
8069 = exit_reason;
8070 return 0;
8071 }
8072
Avi Kivity29bd8a72007-09-10 17:27:03 +03008073 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03008074 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
8075 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03008076 = vmcs_read32(VM_INSTRUCTION_ERROR);
8077 return 0;
8078 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008079
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008080 /*
8081 * Note:
8082 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
8083 * delivery event since it indicates guest is accessing MMIO.
8084 * The vm-exit can be triggered again after return to guest that
8085 * will cause infinite loop.
8086 */
Mike Dayd77c26f2007-10-08 09:02:08 -04008087 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08008088 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02008089 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08008090 exit_reason != EXIT_REASON_TASK_SWITCH)) {
8091 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8092 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
8093 vcpu->run->internal.ndata = 2;
8094 vcpu->run->internal.data[0] = vectoring_info;
8095 vcpu->run->internal.data[1] = exit_reason;
8096 return 0;
8097 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008098
Nadav Har'El644d7112011-05-25 23:12:35 +03008099 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
8100 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03008101 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03008102 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008103 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008104 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01008105 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008106 /*
8107 * This CPU don't support us in finding the end of an
8108 * NMI-blocked window if the guest runs with IRQs
8109 * disabled. So we pull the trigger after 1 s of
8110 * futile waiting, but inform the user about this.
8111 */
8112 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
8113 "state on VCPU %d after 1 s timeout\n",
8114 __func__, vcpu->vcpu_id);
8115 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008116 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008117 }
8118
Avi Kivity6aa8b732006-12-10 02:21:36 -08008119 if (exit_reason < kvm_vmx_max_exit_handlers
8120 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03008121 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008122 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03008123 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
8124 kvm_queue_exception(vcpu, UD_VECTOR);
8125 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008126 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08008127}
8128
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008129static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008130{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008131 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8132
8133 if (is_guest_mode(vcpu) &&
8134 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
8135 return;
8136
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008137 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008138 vmcs_write32(TPR_THRESHOLD, 0);
8139 return;
8140 }
8141
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008142 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08008143}
8144
Yang Zhang8d146952013-01-25 10:18:50 +08008145static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
8146{
8147 u32 sec_exec_control;
8148
8149 /*
8150 * There is not point to enable virtualize x2apic without enable
8151 * apicv
8152 */
Yang Zhangc7c9c562013-01-25 10:18:51 +08008153 if (!cpu_has_vmx_virtualize_x2apic_mode() ||
Paolo Bonzini35754c92015-07-29 12:05:37 +02008154 !vmx_cpu_uses_apicv(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008155 return;
8156
Paolo Bonzini35754c92015-07-29 12:05:37 +02008157 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08008158 return;
8159
8160 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8161
8162 if (set) {
8163 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8164 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8165 } else {
8166 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
8167 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8168 }
8169 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
8170
8171 vmx_set_msr_bitmap(vcpu);
8172}
8173
Tang Chen38b99172014-09-24 15:57:54 +08008174static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
8175{
8176 struct vcpu_vmx *vmx = to_vmx(vcpu);
8177
8178 /*
8179 * Currently we do not handle the nested case where L2 has an
8180 * APIC access page of its own; that page is still pinned.
8181 * Hence, we skip the case where the VCPU is in guest mode _and_
8182 * L1 prepared an APIC access page for L2.
8183 *
8184 * For the case where L1 and L2 share the same APIC access page
8185 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
8186 * in the vmcs12), this function will only update either the vmcs01
8187 * or the vmcs02. If the former, the vmcs02 will be updated by
8188 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
8189 * the next L2->L1 exit.
8190 */
8191 if (!is_guest_mode(vcpu) ||
8192 !nested_cpu_has2(vmx->nested.current_vmcs12,
8193 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
8194 vmcs_write64(APIC_ACCESS_ADDR, hpa);
8195}
8196
Yang Zhangc7c9c562013-01-25 10:18:51 +08008197static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
8198{
8199 u16 status;
8200 u8 old;
8201
Yang Zhangc7c9c562013-01-25 10:18:51 +08008202 if (isr == -1)
8203 isr = 0;
8204
8205 status = vmcs_read16(GUEST_INTR_STATUS);
8206 old = status >> 8;
8207 if (isr != old) {
8208 status &= 0xff;
8209 status |= isr << 8;
8210 vmcs_write16(GUEST_INTR_STATUS, status);
8211 }
8212}
8213
8214static void vmx_set_rvi(int vector)
8215{
8216 u16 status;
8217 u8 old;
8218
Wei Wang4114c272014-11-05 10:53:43 +08008219 if (vector == -1)
8220 vector = 0;
8221
Yang Zhangc7c9c562013-01-25 10:18:51 +08008222 status = vmcs_read16(GUEST_INTR_STATUS);
8223 old = (u8)status & 0xff;
8224 if ((u8)vector != old) {
8225 status &= ~0xff;
8226 status |= (u8)vector;
8227 vmcs_write16(GUEST_INTR_STATUS, status);
8228 }
8229}
8230
8231static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8232{
Wanpeng Li963fee12014-07-17 19:03:00 +08008233 if (!is_guest_mode(vcpu)) {
8234 vmx_set_rvi(max_irr);
8235 return;
8236 }
8237
Wei Wang4114c272014-11-05 10:53:43 +08008238 if (max_irr == -1)
8239 return;
8240
Wanpeng Li963fee12014-07-17 19:03:00 +08008241 /*
Wei Wang4114c272014-11-05 10:53:43 +08008242 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8243 * handles it.
8244 */
8245 if (nested_exit_on_intr(vcpu))
8246 return;
8247
8248 /*
8249 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008250 * is run without virtual interrupt delivery.
8251 */
8252 if (!kvm_event_needs_reinjection(vcpu) &&
8253 vmx_interrupt_allowed(vcpu)) {
8254 kvm_queue_interrupt(vcpu, max_irr, false);
8255 vmx_inject_irq(vcpu);
8256 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008257}
8258
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02008259static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008260{
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02008261 u64 *eoi_exit_bitmap = vcpu->arch.eoi_exit_bitmap;
Paolo Bonzini35754c92015-07-29 12:05:37 +02008262 if (!vmx_cpu_uses_apicv(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008263 return;
8264
Yang Zhangc7c9c562013-01-25 10:18:51 +08008265 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8266 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8267 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8268 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8269}
8270
Avi Kivity51aa01d2010-07-20 14:31:20 +03008271static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008272{
Avi Kivity00eba012011-03-07 17:24:54 +02008273 u32 exit_intr_info;
8274
8275 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8276 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8277 return;
8278
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008279 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008280 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008281
8282 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008283 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008284 kvm_machine_check();
8285
Gleb Natapov20f65982009-05-11 13:35:55 +03008286 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008287 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008288 (exit_intr_info & INTR_INFO_VALID_MASK)) {
8289 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008290 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008291 kvm_after_handle_nmi(&vmx->vcpu);
8292 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008293}
Gleb Natapov20f65982009-05-11 13:35:55 +03008294
Yang Zhanga547c6d2013-04-11 19:25:10 +08008295static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8296{
8297 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8298
8299 /*
8300 * If external interrupt exists, IF bit is set in rflags/eflags on the
8301 * interrupt stack frame, and interrupt will be enabled on a return
8302 * from interrupt handler.
8303 */
8304 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8305 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8306 unsigned int vector;
8307 unsigned long entry;
8308 gate_desc *desc;
8309 struct vcpu_vmx *vmx = to_vmx(vcpu);
8310#ifdef CONFIG_X86_64
8311 unsigned long tmp;
8312#endif
8313
8314 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8315 desc = (gate_desc *)vmx->host_idt_base + vector;
8316 entry = gate_offset(*desc);
8317 asm volatile(
8318#ifdef CONFIG_X86_64
8319 "mov %%" _ASM_SP ", %[sp]\n\t"
8320 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8321 "push $%c[ss]\n\t"
8322 "push %[sp]\n\t"
8323#endif
8324 "pushf\n\t"
8325 "orl $0x200, (%%" _ASM_SP ")\n\t"
8326 __ASM_SIZE(push) " $%c[cs]\n\t"
8327 "call *%[entry]\n\t"
8328 :
8329#ifdef CONFIG_X86_64
8330 [sp]"=&r"(tmp)
8331#endif
8332 :
8333 [entry]"r"(entry),
8334 [ss]"i"(__KERNEL_DS),
8335 [cs]"i"(__KERNEL_CS)
8336 );
8337 } else
8338 local_irq_enable();
8339}
8340
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008341static bool vmx_has_high_real_mode_segbase(void)
8342{
8343 return enable_unrestricted_guest || emulate_invalid_guest_state;
8344}
8345
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008346static bool vmx_mpx_supported(void)
8347{
8348 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8349 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8350}
8351
Wanpeng Li55412b22014-12-02 19:21:30 +08008352static bool vmx_xsaves_supported(void)
8353{
8354 return vmcs_config.cpu_based_2nd_exec_ctrl &
8355 SECONDARY_EXEC_XSAVES;
8356}
8357
Avi Kivity51aa01d2010-07-20 14:31:20 +03008358static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8359{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008360 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008361 bool unblock_nmi;
8362 u8 vector;
8363 bool idtv_info_valid;
8364
8365 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008366
Avi Kivitycf393f72008-07-01 16:20:21 +03008367 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008368 if (vmx->nmi_known_unmasked)
8369 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008370 /*
8371 * Can't use vmx->exit_intr_info since we're not sure what
8372 * the exit reason is.
8373 */
8374 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008375 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8376 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8377 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008378 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03008379 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8380 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008381 * SDM 3: 23.2.2 (September 2008)
8382 * Bit 12 is undefined in any of the following cases:
8383 * If the VM exit sets the valid bit in the IDT-vectoring
8384 * information field.
8385 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03008386 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008387 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8388 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03008389 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8390 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02008391 else
8392 vmx->nmi_known_unmasked =
8393 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8394 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008395 } else if (unlikely(vmx->soft_vnmi_blocked))
8396 vmx->vnmi_blocked_time +=
8397 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03008398}
8399
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008400static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008401 u32 idt_vectoring_info,
8402 int instr_len_field,
8403 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008404{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008405 u8 vector;
8406 int type;
8407 bool idtv_info_valid;
8408
8409 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008410
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008411 vcpu->arch.nmi_injected = false;
8412 kvm_clear_exception_queue(vcpu);
8413 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008414
8415 if (!idtv_info_valid)
8416 return;
8417
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008418 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008419
Avi Kivity668f6122008-07-02 09:28:55 +03008420 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8421 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008422
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008423 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008424 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008425 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008426 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008427 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008428 * Clear bit "block by NMI" before VM entry if a NMI
8429 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008430 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008431 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008432 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008433 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008434 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008435 /* fall through */
8436 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008437 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008438 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008439 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008440 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008441 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008442 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008443 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008444 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008445 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008446 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008447 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008448 break;
8449 default:
8450 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008451 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008452}
8453
Avi Kivity83422e12010-07-20 14:43:23 +03008454static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8455{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008456 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008457 VM_EXIT_INSTRUCTION_LEN,
8458 IDT_VECTORING_ERROR_CODE);
8459}
8460
Avi Kivityb463a6f2010-07-20 15:06:17 +03008461static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8462{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008463 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008464 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8465 VM_ENTRY_INSTRUCTION_LEN,
8466 VM_ENTRY_EXCEPTION_ERROR_CODE);
8467
8468 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8469}
8470
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008471static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8472{
8473 int i, nr_msrs;
8474 struct perf_guest_switch_msr *msrs;
8475
8476 msrs = perf_guest_get_msrs(&nr_msrs);
8477
8478 if (!msrs)
8479 return;
8480
8481 for (i = 0; i < nr_msrs; i++)
8482 if (msrs[i].host == msrs[i].guest)
8483 clear_atomic_switch_msr(vmx, msrs[i].msr);
8484 else
8485 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8486 msrs[i].host);
8487}
8488
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08008489static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008490{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008491 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008492 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02008493
8494 /* Record the guest's net vcpu time for enforced NMI injections. */
8495 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
8496 vmx->entry_time = ktime_get();
8497
8498 /* Don't enter VMX if guest state is invalid, let the exit handler
8499 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02008500 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02008501 return;
8502
Radim Krčmářa7653ec2014-08-21 18:08:07 +02008503 if (vmx->ple_window_dirty) {
8504 vmx->ple_window_dirty = false;
8505 vmcs_write32(PLE_WINDOW, vmx->ple_window);
8506 }
8507
Abel Gordon012f83c2013-04-18 14:39:25 +03008508 if (vmx->nested.sync_shadow_vmcs) {
8509 copy_vmcs12_to_shadow(vmx);
8510 vmx->nested.sync_shadow_vmcs = false;
8511 }
8512
Avi Kivity104f2262010-11-18 13:12:52 +02008513 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
8514 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
8515 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
8516 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
8517
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07008518 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008519 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
8520 vmcs_writel(HOST_CR4, cr4);
8521 vmx->host_state.vmcs_host_cr4 = cr4;
8522 }
8523
Avi Kivity104f2262010-11-18 13:12:52 +02008524 /* When single-stepping over STI and MOV SS, we must clear the
8525 * corresponding interruptibility bits in the guest state. Otherwise
8526 * vmentry fails as it then expects bit 14 (BS) in pending debug
8527 * exceptions being set, but that's not correct for the guest debugging
8528 * case. */
8529 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8530 vmx_set_interrupt_shadow(vcpu, 0);
8531
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008532 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008533 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008534
Nadav Har'Eld462b812011-05-24 15:26:10 +03008535 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02008536 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08008537 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008538 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
8539 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
8540 "push %%" _ASM_CX " \n\t"
8541 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008542 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008543 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008544 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008545 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008546 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008547 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
8548 "mov %%cr2, %%" _ASM_DX " \n\t"
8549 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008550 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008551 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008552 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008553 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02008554 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008555 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008556 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
8557 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
8558 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
8559 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
8560 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
8561 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008562#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008563 "mov %c[r8](%0), %%r8 \n\t"
8564 "mov %c[r9](%0), %%r9 \n\t"
8565 "mov %c[r10](%0), %%r10 \n\t"
8566 "mov %c[r11](%0), %%r11 \n\t"
8567 "mov %c[r12](%0), %%r12 \n\t"
8568 "mov %c[r13](%0), %%r13 \n\t"
8569 "mov %c[r14](%0), %%r14 \n\t"
8570 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008571#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008572 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03008573
Avi Kivity6aa8b732006-12-10 02:21:36 -08008574 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03008575 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008576 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008577 "jmp 2f \n\t"
8578 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
8579 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08008580 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008581 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02008582 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008583 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
8584 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
8585 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
8586 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
8587 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
8588 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
8589 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008590#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008591 "mov %%r8, %c[r8](%0) \n\t"
8592 "mov %%r9, %c[r9](%0) \n\t"
8593 "mov %%r10, %c[r10](%0) \n\t"
8594 "mov %%r11, %c[r11](%0) \n\t"
8595 "mov %%r12, %c[r12](%0) \n\t"
8596 "mov %%r13, %c[r13](%0) \n\t"
8597 "mov %%r14, %c[r14](%0) \n\t"
8598 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008599#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008600 "mov %%cr2, %%" _ASM_AX " \n\t"
8601 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03008602
Avi Kivityb188c81f2012-09-16 15:10:58 +03008603 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02008604 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008605 ".pushsection .rodata \n\t"
8606 ".global vmx_return \n\t"
8607 "vmx_return: " _ASM_PTR " 2b \n\t"
8608 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02008609 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03008610 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02008611 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03008612 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008613 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
8614 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
8615 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
8616 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
8617 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
8618 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
8619 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008620#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008621 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
8622 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
8623 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
8624 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
8625 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
8626 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
8627 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
8628 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008629#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02008630 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
8631 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02008632 : "cc", "memory"
8633#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03008634 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008635 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008636#else
8637 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008638#endif
8639 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08008640
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008641 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
8642 if (debugctlmsr)
8643 update_debugctlmsr(debugctlmsr);
8644
Avi Kivityaa67f602012-08-01 16:48:03 +03008645#ifndef CONFIG_X86_64
8646 /*
8647 * The sysexit path does not restore ds/es, so we must set them to
8648 * a reasonable value ourselves.
8649 *
8650 * We can't defer this to vmx_load_host_state() since that function
8651 * may be executed in interrupt context, which saves and restore segments
8652 * around it, nullifying its effect.
8653 */
8654 loadsegment(ds, __USER_DS);
8655 loadsegment(es, __USER_DS);
8656#endif
8657
Avi Kivity6de4f3a2009-05-31 22:58:47 +03008658 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02008659 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02008660 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03008661 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02008662 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008663 vcpu->arch.regs_dirty = 0;
8664
Avi Kivity1155f762007-11-22 11:30:47 +02008665 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
8666
Nadav Har'Eld462b812011-05-24 15:26:10 +03008667 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02008668
Avi Kivity51aa01d2010-07-20 14:31:20 +03008669 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02008670 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
Avi Kivity51aa01d2010-07-20 14:31:20 +03008671
Gleb Natapove0b890d2013-09-25 12:51:33 +03008672 /*
8673 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
8674 * we did not inject a still-pending event to L1 now because of
8675 * nested_run_pending, we need to re-enable this bit.
8676 */
8677 if (vmx->nested.nested_run_pending)
8678 kvm_make_request(KVM_REQ_EVENT, vcpu);
8679
8680 vmx->nested.nested_run_pending = 0;
8681
Avi Kivity51aa01d2010-07-20 14:31:20 +03008682 vmx_complete_atomic_exit(vmx);
8683 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03008684 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008685}
8686
Paolo Bonzini4fa77342014-07-17 12:25:16 +02008687static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
8688{
8689 struct vcpu_vmx *vmx = to_vmx(vcpu);
8690 int cpu;
8691
8692 if (vmx->loaded_vmcs == &vmx->vmcs01)
8693 return;
8694
8695 cpu = get_cpu();
8696 vmx->loaded_vmcs = &vmx->vmcs01;
8697 vmx_vcpu_put(vcpu);
8698 vmx_vcpu_load(vcpu, cpu);
8699 vcpu->cpu = cpu;
8700 put_cpu();
8701}
8702
Avi Kivity6aa8b732006-12-10 02:21:36 -08008703static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
8704{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008705 struct vcpu_vmx *vmx = to_vmx(vcpu);
8706
Kai Huang843e4332015-01-28 10:54:28 +08008707 if (enable_pml)
Kai Huanga3eaa862015-11-04 13:46:05 +08008708 vmx_destroy_pml_buffer(vmx);
Wanpeng Li991e7a02015-09-16 17:30:05 +08008709 free_vpid(vmx->vpid);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02008710 leave_guest_mode(vcpu);
8711 vmx_load_vmcs01(vcpu);
Marcelo Tosatti26a865f2014-01-03 17:00:51 -02008712 free_nested(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02008713 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008714 kfree(vmx->guest_msrs);
8715 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10008716 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008717}
8718
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008719static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008720{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008721 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10008722 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03008723 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008724
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008725 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008726 return ERR_PTR(-ENOMEM);
8727
Wanpeng Li991e7a02015-09-16 17:30:05 +08008728 vmx->vpid = allocate_vpid();
Sheng Yang2384d2b2008-01-17 15:14:33 +08008729
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008730 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
8731 if (err)
8732 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08008733
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008734 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02008735 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
8736 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03008737
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02008738 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008739 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008740 goto uninit_vcpu;
8741 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08008742
Nadav Har'Eld462b812011-05-24 15:26:10 +03008743 vmx->loaded_vmcs = &vmx->vmcs01;
8744 vmx->loaded_vmcs->vmcs = alloc_vmcs();
8745 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008746 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03008747 if (!vmm_exclusive)
8748 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
8749 loaded_vmcs_init(vmx->loaded_vmcs);
8750 if (!vmm_exclusive)
8751 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008752
Avi Kivity15ad7142007-07-11 18:17:21 +03008753 cpu = get_cpu();
8754 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10008755 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10008756 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008757 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03008758 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008759 if (err)
8760 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02008761 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02008762 err = alloc_apic_access_page(kvm);
8763 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02008764 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02008765 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08008766
Sheng Yangb927a3c2009-07-21 10:42:48 +08008767 if (enable_ept) {
8768 if (!kvm->arch.ept_identity_map_addr)
8769 kvm->arch.ept_identity_map_addr =
8770 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08008771 err = init_rmode_identity_map(kvm);
8772 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02008773 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08008774 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08008775
Wanpeng Li5c614b32015-10-13 09:18:36 -07008776 if (nested) {
Wincy Vanb9c237b2015-02-03 23:56:30 +08008777 nested_vmx_setup_ctls_msrs(vmx);
Wanpeng Li5c614b32015-10-13 09:18:36 -07008778 vmx->nested.vpid02 = allocate_vpid();
8779 }
Wincy Vanb9c237b2015-02-03 23:56:30 +08008780
Wincy Van705699a2015-02-03 23:58:17 +08008781 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03008782 vmx->nested.current_vmptr = -1ull;
8783 vmx->nested.current_vmcs12 = NULL;
8784
Kai Huang843e4332015-01-28 10:54:28 +08008785 /*
8786 * If PML is turned on, failure on enabling PML just results in failure
8787 * of creating the vcpu, therefore we can simplify PML logic (by
8788 * avoiding dealing with cases, such as enabling PML partially on vcpus
8789 * for the guest, etc.
8790 */
8791 if (enable_pml) {
Kai Huanga3eaa862015-11-04 13:46:05 +08008792 err = vmx_create_pml_buffer(vmx);
Kai Huang843e4332015-01-28 10:54:28 +08008793 if (err)
8794 goto free_vmcs;
8795 }
8796
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008797 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08008798
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008799free_vmcs:
Wanpeng Li5c614b32015-10-13 09:18:36 -07008800 free_vpid(vmx->nested.vpid02);
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08008801 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008802free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008803 kfree(vmx->guest_msrs);
8804uninit_vcpu:
8805 kvm_vcpu_uninit(&vmx->vcpu);
8806free_vcpu:
Wanpeng Li991e7a02015-09-16 17:30:05 +08008807 free_vpid(vmx->vpid);
Rusty Russella4770342007-08-01 14:46:11 +10008808 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008809 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008810}
8811
Yang, Sheng002c7f72007-07-31 14:23:01 +03008812static void __init vmx_check_processor_compat(void *rtn)
8813{
8814 struct vmcs_config vmcs_conf;
8815
8816 *(int *)rtn = 0;
8817 if (setup_vmcs_config(&vmcs_conf) < 0)
8818 *(int *)rtn = -EIO;
8819 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
8820 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
8821 smp_processor_id());
8822 *(int *)rtn = -EIO;
8823 }
8824}
8825
Sheng Yang67253af2008-04-25 10:20:22 +08008826static int get_ept_level(void)
8827{
8828 return VMX_EPT_DEFAULT_GAW + 1;
8829}
8830
Sheng Yang4b12f0d2009-04-27 20:35:42 +08008831static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08008832{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08008833 u8 cache;
8834 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08008835
Sheng Yang522c68c2009-04-27 20:35:43 +08008836 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02008837 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08008838 * 2. EPT with VT-d:
8839 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02008840 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08008841 * b. VT-d with snooping control feature: snooping control feature of
8842 * VT-d engine can guarantee the cache correctness. Just set it
8843 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08008844 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08008845 * consistent with host MTRR
8846 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02008847 if (is_mmio) {
8848 cache = MTRR_TYPE_UNCACHABLE;
8849 goto exit;
8850 }
8851
8852 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08008853 ipat = VMX_EPT_IPAT_BIT;
8854 cache = MTRR_TYPE_WRBACK;
8855 goto exit;
8856 }
8857
8858 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
8859 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02008860 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08008861 cache = MTRR_TYPE_WRBACK;
8862 else
8863 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08008864 goto exit;
8865 }
8866
Xiao Guangrongff536042015-06-15 16:55:22 +08008867 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08008868
8869exit:
8870 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08008871}
8872
Sheng Yang17cc3932010-01-05 19:02:27 +08008873static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02008874{
Sheng Yang878403b2010-01-05 19:02:29 +08008875 if (enable_ept && !cpu_has_vmx_ept_1g_page())
8876 return PT_DIRECTORY_LEVEL;
8877 else
8878 /* For shadow and EPT supported 1GB page */
8879 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02008880}
8881
Xiao Guangrongfeda8052015-09-09 14:05:55 +08008882static void vmcs_set_secondary_exec_control(u32 new_ctl)
8883{
8884 /*
8885 * These bits in the secondary execution controls field
8886 * are dynamic, the others are mostly based on the hypervisor
8887 * architecture and the guest's CPUID. Do not touch the
8888 * dynamic bits.
8889 */
8890 u32 mask =
8891 SECONDARY_EXEC_SHADOW_VMCS |
8892 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
8893 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8894
8895 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8896
8897 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
8898 (new_ctl & ~mask) | (cur_ctl & mask));
8899}
8900
Sheng Yang0e851882009-12-18 16:48:46 +08008901static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
8902{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08008903 struct kvm_cpuid_entry2 *best;
8904 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08008905 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08008906
Sheng Yang4e47c7a2009-12-18 16:48:47 +08008907 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08008908 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
8909 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08008910 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08008911
Paolo Bonzini8b972652015-09-15 17:34:42 +02008912 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08008913 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02008914 vmx->nested.nested_vmx_secondary_ctls_high |=
8915 SECONDARY_EXEC_RDTSCP;
8916 else
8917 vmx->nested.nested_vmx_secondary_ctls_high &=
8918 ~SECONDARY_EXEC_RDTSCP;
8919 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08008920 }
Mao, Junjiead756a12012-07-02 01:18:48 +00008921
Mao, Junjiead756a12012-07-02 01:18:48 +00008922 /* Exposing INVPCID only when PCID is exposed */
8923 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
8924 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08008925 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
8926 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08008927 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08008928
Mao, Junjiead756a12012-07-02 01:18:48 +00008929 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00008930 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00008931 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08008932
Xiao Guangrongfeda8052015-09-09 14:05:55 +08008933 vmcs_set_secondary_exec_control(secondary_exec_ctl);
8934
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08008935 if (static_cpu_has(X86_FEATURE_PCOMMIT) && nested) {
8936 if (guest_cpuid_has_pcommit(vcpu))
8937 vmx->nested.nested_vmx_secondary_ctls_high |=
8938 SECONDARY_EXEC_PCOMMIT;
8939 else
8940 vmx->nested.nested_vmx_secondary_ctls_high &=
8941 ~SECONDARY_EXEC_PCOMMIT;
8942 }
Sheng Yang0e851882009-12-18 16:48:46 +08008943}
8944
Joerg Roedeld4330ef2010-04-22 12:33:11 +02008945static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
8946{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03008947 if (func == 1 && nested)
8948 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02008949}
8950
Yang Zhang25d92082013-08-06 12:00:32 +03008951static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
8952 struct x86_exception *fault)
8953{
Jan Kiszka533558b2014-01-04 18:47:20 +01008954 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8955 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03008956
8957 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01008958 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03008959 else
Jan Kiszka533558b2014-01-04 18:47:20 +01008960 exit_reason = EXIT_REASON_EPT_VIOLATION;
8961 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03008962 vmcs12->guest_physical_address = fault->address;
8963}
8964
Nadav Har'El155a97a2013-08-05 11:07:16 +03008965/* Callbacks for nested_ept_init_mmu_context: */
8966
8967static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
8968{
8969 /* return the page table to be shadowed - in our case, EPT12 */
8970 return get_vmcs12(vcpu)->ept_pointer;
8971}
8972
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02008973static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03008974{
Paolo Bonziniad896af2013-10-02 16:56:14 +02008975 WARN_ON(mmu_is_nested(vcpu));
8976 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08008977 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
8978 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03008979 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
8980 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
8981 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
8982
8983 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03008984}
8985
8986static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
8987{
8988 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
8989}
8990
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03008991static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
8992 u16 error_code)
8993{
8994 bool inequality, bit;
8995
8996 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
8997 inequality =
8998 (error_code & vmcs12->page_fault_error_code_mask) !=
8999 vmcs12->page_fault_error_code_match;
9000 return inequality ^ bit;
9001}
9002
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009003static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
9004 struct x86_exception *fault)
9005{
9006 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9007
9008 WARN_ON(!is_guest_mode(vcpu));
9009
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03009010 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01009011 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
9012 vmcs_read32(VM_EXIT_INTR_INFO),
9013 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009014 else
9015 kvm_inject_page_fault(vcpu, fault);
9016}
9017
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009018static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
9019 struct vmcs12 *vmcs12)
9020{
9021 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03009022 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009023
9024 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009025 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
9026 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009027 return false;
9028
9029 /*
9030 * Translate L1 physical address to host physical
9031 * address for vmcs02. Keep the page pinned, so this
9032 * physical address remains valid. We keep a reference
9033 * to it so we can release it later.
9034 */
9035 if (vmx->nested.apic_access_page) /* shouldn't happen */
9036 nested_release_page(vmx->nested.apic_access_page);
9037 vmx->nested.apic_access_page =
9038 nested_get_page(vcpu, vmcs12->apic_access_addr);
9039 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009040
9041 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009042 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
9043 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009044 return false;
9045
9046 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
9047 nested_release_page(vmx->nested.virtual_apic_page);
9048 vmx->nested.virtual_apic_page =
9049 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
9050
9051 /*
9052 * Failing the vm entry is _not_ what the processor does
9053 * but it's basically the only possibility we have.
9054 * We could still enter the guest if CR8 load exits are
9055 * enabled, CR8 store exits are enabled, and virtualize APIC
9056 * access is disabled; in this case the processor would never
9057 * use the TPR shadow and we could simply clear the bit from
9058 * the execution control. But such a configuration is useless,
9059 * so let's keep the code simple.
9060 */
9061 if (!vmx->nested.virtual_apic_page)
9062 return false;
9063 }
9064
Wincy Van705699a2015-02-03 23:58:17 +08009065 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03009066 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
9067 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08009068 return false;
9069
9070 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
9071 kunmap(vmx->nested.pi_desc_page);
9072 nested_release_page(vmx->nested.pi_desc_page);
9073 }
9074 vmx->nested.pi_desc_page =
9075 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
9076 if (!vmx->nested.pi_desc_page)
9077 return false;
9078
9079 vmx->nested.pi_desc =
9080 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
9081 if (!vmx->nested.pi_desc) {
9082 nested_release_page_clean(vmx->nested.pi_desc_page);
9083 return false;
9084 }
9085 vmx->nested.pi_desc =
9086 (struct pi_desc *)((void *)vmx->nested.pi_desc +
9087 (unsigned long)(vmcs12->posted_intr_desc_addr &
9088 (PAGE_SIZE - 1)));
9089 }
9090
Wanpeng Lia2bcba52014-08-21 19:46:49 +08009091 return true;
9092}
9093
Jan Kiszkaf41245002014-03-07 20:03:13 +01009094static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
9095{
9096 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
9097 struct vcpu_vmx *vmx = to_vmx(vcpu);
9098
9099 if (vcpu->arch.virtual_tsc_khz == 0)
9100 return;
9101
9102 /* Make sure short timeouts reliably trigger an immediate vmexit.
9103 * hrtimer_start does not guarantee this. */
9104 if (preemption_timeout <= 1) {
9105 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
9106 return;
9107 }
9108
9109 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9110 preemption_timeout *= 1000000;
9111 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
9112 hrtimer_start(&vmx->nested.preemption_timer,
9113 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
9114}
9115
Wincy Van3af18d92015-02-03 23:49:31 +08009116static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
9117 struct vmcs12 *vmcs12)
9118{
9119 int maxphyaddr;
9120 u64 addr;
9121
9122 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
9123 return 0;
9124
9125 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
9126 WARN_ON(1);
9127 return -EINVAL;
9128 }
9129 maxphyaddr = cpuid_maxphyaddr(vcpu);
9130
9131 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
9132 ((addr + PAGE_SIZE) >> maxphyaddr))
9133 return -EINVAL;
9134
9135 return 0;
9136}
9137
9138/*
9139 * Merge L0's and L1's MSR bitmap, return false to indicate that
9140 * we do not use the hardware.
9141 */
9142static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
9143 struct vmcs12 *vmcs12)
9144{
Wincy Van82f0dd42015-02-03 23:57:18 +08009145 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08009146 struct page *page;
9147 unsigned long *msr_bitmap;
9148
9149 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
9150 return false;
9151
9152 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
9153 if (!page) {
9154 WARN_ON(1);
9155 return false;
9156 }
9157 msr_bitmap = (unsigned long *)kmap(page);
9158 if (!msr_bitmap) {
9159 nested_release_page_clean(page);
9160 WARN_ON(1);
9161 return false;
9162 }
9163
9164 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08009165 if (nested_cpu_has_apic_reg_virt(vmcs12))
9166 for (msr = 0x800; msr <= 0x8ff; msr++)
9167 nested_vmx_disable_intercept_for_msr(
9168 msr_bitmap,
9169 vmx_msr_bitmap_nested,
9170 msr, MSR_TYPE_R);
Wincy Vanf2b93282015-02-03 23:56:03 +08009171 /* TPR is allowed */
9172 nested_vmx_disable_intercept_for_msr(msr_bitmap,
9173 vmx_msr_bitmap_nested,
9174 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
9175 MSR_TYPE_R | MSR_TYPE_W);
Wincy Van608406e2015-02-03 23:57:51 +08009176 if (nested_cpu_has_vid(vmcs12)) {
9177 /* EOI and self-IPI are allowed */
9178 nested_vmx_disable_intercept_for_msr(
9179 msr_bitmap,
9180 vmx_msr_bitmap_nested,
9181 APIC_BASE_MSR + (APIC_EOI >> 4),
9182 MSR_TYPE_W);
9183 nested_vmx_disable_intercept_for_msr(
9184 msr_bitmap,
9185 vmx_msr_bitmap_nested,
9186 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9187 MSR_TYPE_W);
9188 }
Wincy Van82f0dd42015-02-03 23:57:18 +08009189 } else {
9190 /*
9191 * Enable reading intercept of all the x2apic
9192 * MSRs. We should not rely on vmcs12 to do any
9193 * optimizations here, it may have been modified
9194 * by L1.
9195 */
9196 for (msr = 0x800; msr <= 0x8ff; msr++)
9197 __vmx_enable_intercept_for_msr(
9198 vmx_msr_bitmap_nested,
9199 msr,
9200 MSR_TYPE_R);
9201
Wincy Vanf2b93282015-02-03 23:56:03 +08009202 __vmx_enable_intercept_for_msr(
9203 vmx_msr_bitmap_nested,
9204 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
Wincy Van82f0dd42015-02-03 23:57:18 +08009205 MSR_TYPE_W);
Wincy Van608406e2015-02-03 23:57:51 +08009206 __vmx_enable_intercept_for_msr(
9207 vmx_msr_bitmap_nested,
9208 APIC_BASE_MSR + (APIC_EOI >> 4),
9209 MSR_TYPE_W);
9210 __vmx_enable_intercept_for_msr(
9211 vmx_msr_bitmap_nested,
9212 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9213 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +08009214 }
Wincy Vanf2b93282015-02-03 23:56:03 +08009215 kunmap(page);
9216 nested_release_page_clean(page);
9217
9218 return true;
9219}
9220
9221static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9222 struct vmcs12 *vmcs12)
9223{
Wincy Van82f0dd42015-02-03 23:57:18 +08009224 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009225 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009226 !nested_cpu_has_vid(vmcs12) &&
9227 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009228 return 0;
9229
9230 /*
9231 * If virtualize x2apic mode is enabled,
9232 * virtualize apic access must be disabled.
9233 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009234 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9235 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009236 return -EINVAL;
9237
Wincy Van608406e2015-02-03 23:57:51 +08009238 /*
9239 * If virtual interrupt delivery is enabled,
9240 * we must exit on external interrupts.
9241 */
9242 if (nested_cpu_has_vid(vmcs12) &&
9243 !nested_exit_on_intr(vcpu))
9244 return -EINVAL;
9245
Wincy Van705699a2015-02-03 23:58:17 +08009246 /*
9247 * bits 15:8 should be zero in posted_intr_nv,
9248 * the descriptor address has been already checked
9249 * in nested_get_vmcs12_pages.
9250 */
9251 if (nested_cpu_has_posted_intr(vmcs12) &&
9252 (!nested_cpu_has_vid(vmcs12) ||
9253 !nested_exit_intr_ack_set(vcpu) ||
9254 vmcs12->posted_intr_nv & 0xff00))
9255 return -EINVAL;
9256
Wincy Vanf2b93282015-02-03 23:56:03 +08009257 /* tpr shadow is needed by all apicv features. */
9258 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9259 return -EINVAL;
9260
9261 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009262}
9263
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009264static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9265 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009266 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009267{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009268 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009269 u64 count, addr;
9270
9271 if (vmcs12_read_any(vcpu, count_field, &count) ||
9272 vmcs12_read_any(vcpu, addr_field, &addr)) {
9273 WARN_ON(1);
9274 return -EINVAL;
9275 }
9276 if (count == 0)
9277 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009278 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009279 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9280 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
9281 pr_warn_ratelimited(
9282 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9283 addr_field, maxphyaddr, count, addr);
9284 return -EINVAL;
9285 }
9286 return 0;
9287}
9288
9289static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9290 struct vmcs12 *vmcs12)
9291{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009292 if (vmcs12->vm_exit_msr_load_count == 0 &&
9293 vmcs12->vm_exit_msr_store_count == 0 &&
9294 vmcs12->vm_entry_msr_load_count == 0)
9295 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009296 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009297 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009298 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009299 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009300 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009301 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009302 return -EINVAL;
9303 return 0;
9304}
9305
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009306static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9307 struct vmx_msr_entry *e)
9308{
9309 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009310 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009311 return -EINVAL;
9312 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9313 e->index == MSR_IA32_UCODE_REV)
9314 return -EINVAL;
9315 if (e->reserved != 0)
9316 return -EINVAL;
9317 return 0;
9318}
9319
9320static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9321 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009322{
9323 if (e->index == MSR_FS_BASE ||
9324 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009325 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9326 nested_vmx_msr_check_common(vcpu, e))
9327 return -EINVAL;
9328 return 0;
9329}
9330
9331static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9332 struct vmx_msr_entry *e)
9333{
9334 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9335 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009336 return -EINVAL;
9337 return 0;
9338}
9339
9340/*
9341 * Load guest's/host's msr at nested entry/exit.
9342 * return 0 for success, entry index for failure.
9343 */
9344static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9345{
9346 u32 i;
9347 struct vmx_msr_entry e;
9348 struct msr_data msr;
9349
9350 msr.host_initiated = false;
9351 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009352 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9353 &e, sizeof(e))) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009354 pr_warn_ratelimited(
9355 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9356 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009357 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009358 }
9359 if (nested_vmx_load_msr_check(vcpu, &e)) {
9360 pr_warn_ratelimited(
9361 "%s check failed (%u, 0x%x, 0x%x)\n",
9362 __func__, i, e.index, e.reserved);
9363 goto fail;
9364 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009365 msr.index = e.index;
9366 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009367 if (kvm_set_msr(vcpu, &msr)) {
9368 pr_warn_ratelimited(
9369 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9370 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +03009371 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009372 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009373 }
9374 return 0;
9375fail:
9376 return i + 1;
9377}
9378
9379static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9380{
9381 u32 i;
9382 struct vmx_msr_entry e;
9383
9384 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009385 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009386 if (kvm_vcpu_read_guest(vcpu,
9387 gpa + i * sizeof(e),
9388 &e, 2 * sizeof(u32))) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009389 pr_warn_ratelimited(
9390 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9391 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009392 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009393 }
9394 if (nested_vmx_store_msr_check(vcpu, &e)) {
9395 pr_warn_ratelimited(
9396 "%s check failed (%u, 0x%x, 0x%x)\n",
9397 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +03009398 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009399 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009400 msr_info.host_initiated = false;
9401 msr_info.index = e.index;
9402 if (kvm_get_msr(vcpu, &msr_info)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009403 pr_warn_ratelimited(
9404 "%s cannot read MSR (%u, 0x%x)\n",
9405 __func__, i, e.index);
9406 return -EINVAL;
9407 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009408 if (kvm_vcpu_write_guest(vcpu,
9409 gpa + i * sizeof(e) +
9410 offsetof(struct vmx_msr_entry, value),
9411 &msr_info.data, sizeof(msr_info.data))) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009412 pr_warn_ratelimited(
9413 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009414 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009415 return -EINVAL;
9416 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009417 }
9418 return 0;
9419}
9420
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009421/*
9422 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
9423 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +08009424 * 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 +03009425 * guest in a way that will both be appropriate to L1's requests, and our
9426 * needs. In addition to modifying the active vmcs (which is vmcs02), this
9427 * function also has additional necessary side-effects, like setting various
9428 * vcpu->arch fields.
9429 */
9430static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9431{
9432 struct vcpu_vmx *vmx = to_vmx(vcpu);
9433 u32 exec_control;
9434
9435 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
9436 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
9437 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
9438 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
9439 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
9440 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
9441 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
9442 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
9443 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
9444 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
9445 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
9446 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
9447 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
9448 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
9449 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
9450 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
9451 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
9452 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
9453 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
9454 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
9455 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
9456 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
9457 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
9458 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
9459 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
9460 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
9461 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
9462 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
9463 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
9464 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
9465 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
9466 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
9467 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
9468 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
9469 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
9470 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
9471
Jan Kiszka2996fca2014-06-16 13:59:43 +02009472 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
9473 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
9474 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
9475 } else {
9476 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
9477 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
9478 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009479 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
9480 vmcs12->vm_entry_intr_info_field);
9481 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
9482 vmcs12->vm_entry_exception_error_code);
9483 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
9484 vmcs12->vm_entry_instruction_len);
9485 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
9486 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009487 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +03009488 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009489 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
9490 vmcs12->guest_pending_dbg_exceptions);
9491 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
9492 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
9493
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009494 if (nested_cpu_has_xsaves(vmcs12))
9495 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009496 vmcs_write64(VMCS_LINK_POINTER, -1ull);
9497
Jan Kiszkaf41245002014-03-07 20:03:13 +01009498 exec_control = vmcs12->pin_based_vm_exec_control;
9499 exec_control |= vmcs_config.pin_based_exec_ctrl;
Wincy Van705699a2015-02-03 23:58:17 +08009500 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9501
9502 if (nested_cpu_has_posted_intr(vmcs12)) {
9503 /*
9504 * Note that we use L0's vector here and in
9505 * vmx_deliver_nested_posted_interrupt.
9506 */
9507 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
9508 vmx->nested.pi_pending = false;
9509 vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
9510 vmcs_write64(POSTED_INTR_DESC_ADDR,
9511 page_to_phys(vmx->nested.pi_desc_page) +
9512 (unsigned long)(vmcs12->posted_intr_desc_addr &
9513 (PAGE_SIZE - 1)));
9514 } else
9515 exec_control &= ~PIN_BASED_POSTED_INTR;
9516
Jan Kiszkaf41245002014-03-07 20:03:13 +01009517 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009518
Jan Kiszkaf41245002014-03-07 20:03:13 +01009519 vmx->nested.preemption_timer_expired = false;
9520 if (nested_cpu_has_preemption_timer(vmcs12))
9521 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +01009522
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009523 /*
9524 * Whether page-faults are trapped is determined by a combination of
9525 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
9526 * If enable_ept, L0 doesn't care about page faults and we should
9527 * set all of these to L1's desires. However, if !enable_ept, L0 does
9528 * care about (at least some) page faults, and because it is not easy
9529 * (if at all possible?) to merge L0 and L1's desires, we simply ask
9530 * to exit on each and every L2 page fault. This is done by setting
9531 * MASK=MATCH=0 and (see below) EB.PF=1.
9532 * Note that below we don't need special code to set EB.PF beyond the
9533 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
9534 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
9535 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
9536 *
9537 * A problem with this approach (when !enable_ept) is that L1 may be
9538 * injected with more page faults than it asked for. This could have
9539 * caused problems, but in practice existing hypervisors don't care.
9540 * To fix this, we will need to emulate the PFEC checking (on the L1
9541 * page tables), using walk_addr(), when injecting PFs to L1.
9542 */
9543 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
9544 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
9545 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
9546 enable_ept ? vmcs12->page_fault_error_code_match : 0);
9547
9548 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf41245002014-03-07 20:03:13 +01009549 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +08009550
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009551 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009552 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009553 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009554 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009555 SECONDARY_EXEC_APIC_REGISTER_VIRT |
9556 SECONDARY_EXEC_PCOMMIT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009557 if (nested_cpu_has(vmcs12,
9558 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
9559 exec_control |= vmcs12->secondary_vm_exec_control;
9560
9561 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
9562 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009563 * If translation failed, no matter: This feature asks
9564 * to exit when accessing the given address, and if it
9565 * can never be accessed, this feature won't do
9566 * anything anyway.
9567 */
9568 if (!vmx->nested.apic_access_page)
9569 exec_control &=
9570 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9571 else
9572 vmcs_write64(APIC_ACCESS_ADDR,
9573 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +08009574 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +02009575 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +01009576 exec_control |=
9577 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +08009578 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009579 }
9580
Wincy Van608406e2015-02-03 23:57:51 +08009581 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
9582 vmcs_write64(EOI_EXIT_BITMAP0,
9583 vmcs12->eoi_exit_bitmap0);
9584 vmcs_write64(EOI_EXIT_BITMAP1,
9585 vmcs12->eoi_exit_bitmap1);
9586 vmcs_write64(EOI_EXIT_BITMAP2,
9587 vmcs12->eoi_exit_bitmap2);
9588 vmcs_write64(EOI_EXIT_BITMAP3,
9589 vmcs12->eoi_exit_bitmap3);
9590 vmcs_write16(GUEST_INTR_STATUS,
9591 vmcs12->guest_intr_status);
9592 }
9593
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009594 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
9595 }
9596
9597
9598 /*
9599 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
9600 * Some constant fields are set here by vmx_set_constant_host_state().
9601 * Other fields are different per CPU, and will be set later when
9602 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
9603 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08009604 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009605
9606 /*
9607 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
9608 * entry, but only if the current (host) sp changed from the value
9609 * we wrote last (vmx->host_rsp). This cache is no longer relevant
9610 * if we switch vmcs, and rather than hold a separate cache per vmcs,
9611 * here we just force the write to happen on entry.
9612 */
9613 vmx->host_rsp = 0;
9614
9615 exec_control = vmx_exec_control(vmx); /* L0's desires */
9616 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
9617 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
9618 exec_control &= ~CPU_BASED_TPR_SHADOW;
9619 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009620
9621 if (exec_control & CPU_BASED_TPR_SHADOW) {
9622 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
9623 page_to_phys(vmx->nested.virtual_apic_page));
9624 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
9625 }
9626
Wincy Van3af18d92015-02-03 23:49:31 +08009627 if (cpu_has_vmx_msr_bitmap() &&
Wincy Van670125b2015-03-04 14:31:56 +08009628 exec_control & CPU_BASED_USE_MSR_BITMAPS) {
9629 nested_vmx_merge_msr_bitmap(vcpu, vmcs12);
9630 /* MSR_BITMAP will be set by following vmx_set_efer. */
Wincy Van3af18d92015-02-03 23:49:31 +08009631 } else
9632 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
9633
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009634 /*
Wincy Van3af18d92015-02-03 23:49:31 +08009635 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009636 * Rather, exit every time.
9637 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009638 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
9639 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
9640
9641 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
9642
9643 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
9644 * bitwise-or of what L1 wants to trap for L2, and what we want to
9645 * trap. Note that CR0.TS also needs updating - we do this later.
9646 */
9647 update_exception_bitmap(vcpu);
9648 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
9649 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
9650
Nadav Har'El8049d652013-08-05 11:07:06 +03009651 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
9652 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
9653 * bits are further modified by vmx_set_efer() below.
9654 */
Jan Kiszkaf41245002014-03-07 20:03:13 +01009655 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +03009656
9657 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
9658 * emulated by vmx_set_efer(), below.
9659 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02009660 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +03009661 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
9662 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009663 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
9664
Jan Kiszka44811c02013-08-04 17:17:27 +02009665 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009666 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02009667 vcpu->arch.pat = vmcs12->guest_ia32_pat;
9668 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009669 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
9670
9671
9672 set_cr4_guest_host_mask(vmx);
9673
Paolo Bonzini36be0b92014-02-24 12:30:04 +01009674 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
9675 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
9676
Nadav Har'El27fc51b2011-08-02 15:54:52 +03009677 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
9678 vmcs_write64(TSC_OFFSET,
9679 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
9680 else
9681 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009682
9683 if (enable_vpid) {
9684 /*
Wanpeng Li5c614b32015-10-13 09:18:36 -07009685 * There is no direct mapping between vpid02 and vpid12, the
9686 * vpid02 is per-vCPU for L0 and reused while the value of
9687 * vpid12 is changed w/ one invvpid during nested vmentry.
9688 * The vpid12 is allocated by L1 for L2, so it will not
9689 * influence global bitmap(for vpid01 and vpid02 allocation)
9690 * even if spawn a lot of nested vCPUs.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009691 */
Wanpeng Li5c614b32015-10-13 09:18:36 -07009692 if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) {
9693 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02);
9694 if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) {
9695 vmx->nested.last_vpid = vmcs12->virtual_processor_id;
9696 __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02);
9697 }
9698 } else {
9699 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
9700 vmx_flush_tlb(vcpu);
9701 }
9702
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009703 }
9704
Nadav Har'El155a97a2013-08-05 11:07:16 +03009705 if (nested_cpu_has_ept(vmcs12)) {
9706 kvm_mmu_unload(vcpu);
9707 nested_ept_init_mmu_context(vcpu);
9708 }
9709
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009710 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
9711 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +02009712 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009713 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
9714 else
9715 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
9716 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
9717 vmx_set_efer(vcpu, vcpu->arch.efer);
9718
9719 /*
9720 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
9721 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
9722 * The CR0_READ_SHADOW is what L2 should have expected to read given
9723 * the specifications by L1; It's not enough to take
9724 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
9725 * have more bits than L1 expected.
9726 */
9727 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
9728 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
9729
9730 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
9731 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
9732
9733 /* shadow page tables on either EPT or shadow page tables */
9734 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
9735 kvm_mmu_reset_context(vcpu);
9736
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009737 if (!enable_ept)
9738 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
9739
Nadav Har'El3633cfc2013-08-05 11:07:07 +03009740 /*
9741 * L1 may access the L2's PDPTR, so save them to construct vmcs12
9742 */
9743 if (enable_ept) {
9744 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
9745 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
9746 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
9747 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
9748 }
9749
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009750 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
9751 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
9752}
9753
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009754/*
9755 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
9756 * for running an L2 nested guest.
9757 */
9758static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
9759{
9760 struct vmcs12 *vmcs12;
9761 struct vcpu_vmx *vmx = to_vmx(vcpu);
9762 int cpu;
9763 struct loaded_vmcs *vmcs02;
Jan Kiszka384bb782013-04-20 10:52:36 +02009764 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +03009765 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009766
9767 if (!nested_vmx_check_permission(vcpu) ||
9768 !nested_vmx_check_vmcs12(vcpu))
9769 return 1;
9770
9771 skip_emulated_instruction(vcpu);
9772 vmcs12 = get_vmcs12(vcpu);
9773
Abel Gordon012f83c2013-04-18 14:39:25 +03009774 if (enable_shadow_vmcs)
9775 copy_shadow_to_vmcs12(vmx);
9776
Nadav Har'El7c177932011-05-25 23:12:04 +03009777 /*
9778 * The nested entry process starts with enforcing various prerequisites
9779 * on vmcs12 as required by the Intel SDM, and act appropriately when
9780 * they fail: As the SDM explains, some conditions should cause the
9781 * instruction to fail, while others will cause the instruction to seem
9782 * to succeed, but return an EXIT_REASON_INVALID_STATE.
9783 * To speed up the normal (success) code path, we should avoid checking
9784 * for misconfigurations which will anyway be caught by the processor
9785 * when using the merged vmcs02.
9786 */
9787 if (vmcs12->launch_state == launch) {
9788 nested_vmx_failValid(vcpu,
9789 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
9790 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
9791 return 1;
9792 }
9793
Jan Kiszka6dfacad2013-12-04 08:58:54 +01009794 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
9795 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +02009796 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
9797 return 1;
9798 }
9799
Wincy Van3af18d92015-02-03 23:49:31 +08009800 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +03009801 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
9802 return 1;
9803 }
9804
Wincy Van3af18d92015-02-03 23:49:31 +08009805 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +03009806 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
9807 return 1;
9808 }
9809
Wincy Vanf2b93282015-02-03 23:56:03 +08009810 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
9811 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
9812 return 1;
9813 }
9814
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009815 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
9816 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
9817 return 1;
9818 }
9819
Nadav Har'El7c177932011-05-25 23:12:04 +03009820 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009821 vmx->nested.nested_vmx_true_procbased_ctls_low,
9822 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03009823 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009824 vmx->nested.nested_vmx_secondary_ctls_low,
9825 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03009826 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009827 vmx->nested.nested_vmx_pinbased_ctls_low,
9828 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03009829 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009830 vmx->nested.nested_vmx_true_exit_ctls_low,
9831 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03009832 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009833 vmx->nested.nested_vmx_true_entry_ctls_low,
9834 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +03009835 {
9836 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
9837 return 1;
9838 }
9839
9840 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
9841 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
9842 nested_vmx_failValid(vcpu,
9843 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
9844 return 1;
9845 }
9846
Wincy Vanb9c237b2015-02-03 23:56:30 +08009847 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03009848 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
9849 nested_vmx_entry_failure(vcpu, vmcs12,
9850 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
9851 return 1;
9852 }
9853 if (vmcs12->vmcs_link_pointer != -1ull) {
9854 nested_vmx_entry_failure(vcpu, vmcs12,
9855 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
9856 return 1;
9857 }
9858
9859 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +02009860 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +02009861 * are performed on the field for the IA32_EFER MSR:
9862 * - Bits reserved in the IA32_EFER MSR must be 0.
9863 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
9864 * the IA-32e mode guest VM-exit control. It must also be identical
9865 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
9866 * CR0.PG) is 1.
9867 */
9868 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
9869 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
9870 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
9871 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
9872 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
9873 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
9874 nested_vmx_entry_failure(vcpu, vmcs12,
9875 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
9876 return 1;
9877 }
9878 }
9879
9880 /*
9881 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
9882 * IA32_EFER MSR must be 0 in the field for that register. In addition,
9883 * the values of the LMA and LME bits in the field must each be that of
9884 * the host address-space size VM-exit control.
9885 */
9886 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
9887 ia32e = (vmcs12->vm_exit_controls &
9888 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
9889 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
9890 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
9891 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
9892 nested_vmx_entry_failure(vcpu, vmcs12,
9893 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
9894 return 1;
9895 }
9896 }
9897
9898 /*
Nadav Har'El7c177932011-05-25 23:12:04 +03009899 * We're finally done with prerequisite checking, and can start with
9900 * the nested entry.
9901 */
9902
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009903 vmcs02 = nested_get_current_vmcs02(vmx);
9904 if (!vmcs02)
9905 return -ENOMEM;
9906
9907 enter_guest_mode(vcpu);
9908
9909 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
9910
Jan Kiszka2996fca2014-06-16 13:59:43 +02009911 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
9912 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
9913
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009914 cpu = get_cpu();
9915 vmx->loaded_vmcs = vmcs02;
9916 vmx_vcpu_put(vcpu);
9917 vmx_vcpu_load(vcpu, cpu);
9918 vcpu->cpu = cpu;
9919 put_cpu();
9920
Jan Kiszka36c3cc42013-02-23 22:35:37 +01009921 vmx_segment_cache_clear(vmx);
9922
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009923 prepare_vmcs02(vcpu, vmcs12);
9924
Wincy Vanff651cb2014-12-11 08:52:58 +03009925 msr_entry_idx = nested_vmx_load_msr(vcpu,
9926 vmcs12->vm_entry_msr_load_addr,
9927 vmcs12->vm_entry_msr_load_count);
9928 if (msr_entry_idx) {
9929 leave_guest_mode(vcpu);
9930 vmx_load_vmcs01(vcpu);
9931 nested_vmx_entry_failure(vcpu, vmcs12,
9932 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
9933 return 1;
9934 }
9935
9936 vmcs12->launch_state = 1;
9937
Jan Kiszka6dfacad2013-12-04 08:58:54 +01009938 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -06009939 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +01009940
Jan Kiszka7af40ad32014-01-04 18:47:23 +01009941 vmx->nested.nested_run_pending = 1;
9942
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009943 /*
9944 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
9945 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
9946 * returned as far as L1 is concerned. It will only return (and set
9947 * the success flag) when L2 exits (see nested_vmx_vmexit()).
9948 */
9949 return 1;
9950}
9951
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009952/*
9953 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
9954 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
9955 * This function returns the new value we should put in vmcs12.guest_cr0.
9956 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
9957 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
9958 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
9959 * didn't trap the bit, because if L1 did, so would L0).
9960 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
9961 * been modified by L2, and L1 knows it. So just leave the old value of
9962 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
9963 * isn't relevant, because if L0 traps this bit it can set it to anything.
9964 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
9965 * changed these bits, and therefore they need to be updated, but L0
9966 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
9967 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
9968 */
9969static inline unsigned long
9970vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9971{
9972 return
9973 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
9974 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
9975 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
9976 vcpu->arch.cr0_guest_owned_bits));
9977}
9978
9979static inline unsigned long
9980vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9981{
9982 return
9983 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
9984 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
9985 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
9986 vcpu->arch.cr4_guest_owned_bits));
9987}
9988
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009989static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
9990 struct vmcs12 *vmcs12)
9991{
9992 u32 idt_vectoring;
9993 unsigned int nr;
9994
Gleb Natapov851eb6672013-09-25 12:51:34 +03009995 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009996 nr = vcpu->arch.exception.nr;
9997 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
9998
9999 if (kvm_exception_is_soft(nr)) {
10000 vmcs12->vm_exit_instruction_len =
10001 vcpu->arch.event_exit_inst_len;
10002 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
10003 } else
10004 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
10005
10006 if (vcpu->arch.exception.has_error_code) {
10007 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
10008 vmcs12->idt_vectoring_error_code =
10009 vcpu->arch.exception.error_code;
10010 }
10011
10012 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +010010013 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010014 vmcs12->idt_vectoring_info_field =
10015 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
10016 } else if (vcpu->arch.interrupt.pending) {
10017 nr = vcpu->arch.interrupt.nr;
10018 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
10019
10020 if (vcpu->arch.interrupt.soft) {
10021 idt_vectoring |= INTR_TYPE_SOFT_INTR;
10022 vmcs12->vm_entry_instruction_len =
10023 vcpu->arch.event_exit_inst_len;
10024 } else
10025 idt_vectoring |= INTR_TYPE_EXT_INTR;
10026
10027 vmcs12->idt_vectoring_info_field = idt_vectoring;
10028 }
10029}
10030
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010031static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
10032{
10033 struct vcpu_vmx *vmx = to_vmx(vcpu);
10034
Jan Kiszkaf41245002014-03-07 20:03:13 +010010035 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
10036 vmx->nested.preemption_timer_expired) {
10037 if (vmx->nested.nested_run_pending)
10038 return -EBUSY;
10039 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
10040 return 0;
10041 }
10042
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010043 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +010010044 if (vmx->nested.nested_run_pending ||
10045 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010046 return -EBUSY;
10047 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
10048 NMI_VECTOR | INTR_TYPE_NMI_INTR |
10049 INTR_INFO_VALID_MASK, 0);
10050 /*
10051 * The NMI-triggered VM exit counts as injection:
10052 * clear this one and block further NMIs.
10053 */
10054 vcpu->arch.nmi_pending = 0;
10055 vmx_set_nmi_mask(vcpu, true);
10056 return 0;
10057 }
10058
10059 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
10060 nested_exit_on_intr(vcpu)) {
10061 if (vmx->nested.nested_run_pending)
10062 return -EBUSY;
10063 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +080010064 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010065 }
10066
Wincy Van705699a2015-02-03 23:58:17 +080010067 return vmx_complete_nested_posted_interrupt(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010068}
10069
Jan Kiszkaf41245002014-03-07 20:03:13 +010010070static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
10071{
10072 ktime_t remaining =
10073 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
10074 u64 value;
10075
10076 if (ktime_to_ns(remaining) <= 0)
10077 return 0;
10078
10079 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
10080 do_div(value, 1000000);
10081 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
10082}
10083
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010084/*
10085 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
10086 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
10087 * and this function updates it to reflect the changes to the guest state while
10088 * L2 was running (and perhaps made some exits which were handled directly by L0
10089 * without going back to L1), and to reflect the exit reason.
10090 * Note that we do not have to copy here all VMCS fields, just those that
10091 * could have changed by the L2 guest or the exit - i.e., the guest-state and
10092 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
10093 * which already writes to vmcs12 directly.
10094 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010095static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
10096 u32 exit_reason, u32 exit_intr_info,
10097 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010098{
10099 /* update guest state fields: */
10100 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
10101 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
10102
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010103 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
10104 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
10105 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
10106
10107 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
10108 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
10109 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
10110 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
10111 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
10112 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
10113 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
10114 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
10115 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
10116 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
10117 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
10118 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
10119 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
10120 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
10121 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
10122 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
10123 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
10124 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
10125 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
10126 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
10127 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
10128 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
10129 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
10130 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
10131 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
10132 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
10133 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
10134 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
10135 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
10136 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
10137 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
10138 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
10139 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
10140 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
10141 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
10142 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
10143
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010144 vmcs12->guest_interruptibility_info =
10145 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
10146 vmcs12->guest_pending_dbg_exceptions =
10147 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +010010148 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
10149 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
10150 else
10151 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010152
Jan Kiszkaf41245002014-03-07 20:03:13 +010010153 if (nested_cpu_has_preemption_timer(vmcs12)) {
10154 if (vmcs12->vm_exit_controls &
10155 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
10156 vmcs12->vmx_preemption_timer_value =
10157 vmx_get_preemption_timer_value(vcpu);
10158 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
10159 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +080010160
Nadav Har'El3633cfc2013-08-05 11:07:07 +030010161 /*
10162 * In some cases (usually, nested EPT), L2 is allowed to change its
10163 * own CR3 without exiting. If it has changed it, we must keep it.
10164 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
10165 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
10166 *
10167 * Additionally, restore L2's PDPTR to vmcs12.
10168 */
10169 if (enable_ept) {
10170 vmcs12->guest_cr3 = vmcs_read64(GUEST_CR3);
10171 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
10172 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
10173 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
10174 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
10175 }
10176
Wincy Van608406e2015-02-03 23:57:51 +080010177 if (nested_cpu_has_vid(vmcs12))
10178 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
10179
Jan Kiszkac18911a2013-03-13 16:06:41 +010010180 vmcs12->vm_entry_controls =
10181 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +020010182 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +010010183
Jan Kiszka2996fca2014-06-16 13:59:43 +020010184 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
10185 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
10186 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
10187 }
10188
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010189 /* TODO: These cannot have changed unless we have MSR bitmaps and
10190 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +020010191 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010192 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +020010193 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
10194 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010195 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
10196 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
10197 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010198 if (vmx_mpx_supported())
10199 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +080010200 if (nested_cpu_has_xsaves(vmcs12))
10201 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010202
10203 /* update exit information fields: */
10204
Jan Kiszka533558b2014-01-04 18:47:20 +010010205 vmcs12->vm_exit_reason = exit_reason;
10206 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010207
Jan Kiszka533558b2014-01-04 18:47:20 +010010208 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +020010209 if ((vmcs12->vm_exit_intr_info &
10210 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
10211 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
10212 vmcs12->vm_exit_intr_error_code =
10213 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010214 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010215 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
10216 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
10217
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010218 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
10219 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10220 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010221 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010222
10223 /*
10224 * Transfer the event that L0 or L1 may wanted to inject into
10225 * L2 to IDT_VECTORING_INFO_FIELD.
10226 */
10227 vmcs12_save_pending_event(vcpu, vmcs12);
10228 }
10229
10230 /*
10231 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10232 * preserved above and would only end up incorrectly in L1.
10233 */
10234 vcpu->arch.nmi_injected = false;
10235 kvm_clear_exception_queue(vcpu);
10236 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010237}
10238
10239/*
10240 * A part of what we need to when the nested L2 guest exits and we want to
10241 * run its L1 parent, is to reset L1's guest state to the host state specified
10242 * in vmcs12.
10243 * This function is to be called not only on normal nested exit, but also on
10244 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10245 * Failures During or After Loading Guest State").
10246 * This function should be called when the active VMCS is L1's (vmcs01).
10247 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010248static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10249 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010250{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010251 struct kvm_segment seg;
10252
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010253 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10254 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010255 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010256 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10257 else
10258 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10259 vmx_set_efer(vcpu, vcpu->arch.efer);
10260
10261 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10262 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010263 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010264 /*
10265 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
10266 * actually changed, because it depends on the current state of
10267 * fpu_active (which may have changed).
10268 * Note that vmx_set_cr0 refers to efer set above.
10269 */
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020010270 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010271 /*
10272 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
10273 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
10274 * but we also need to update cr0_guest_host_mask and exception_bitmap.
10275 */
10276 update_exception_bitmap(vcpu);
10277 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
10278 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10279
10280 /*
10281 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
10282 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
10283 */
10284 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
10285 kvm_set_cr4(vcpu, vmcs12->host_cr4);
10286
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010287 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010288
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010289 kvm_set_cr3(vcpu, vmcs12->host_cr3);
10290 kvm_mmu_reset_context(vcpu);
10291
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010292 if (!enable_ept)
10293 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10294
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010295 if (enable_vpid) {
10296 /*
10297 * Trivially support vpid by letting L2s share their parent
10298 * L1's vpid. TODO: move to a more elaborate solution, giving
10299 * each L2 its own vpid and exposing the vpid feature to L1.
10300 */
10301 vmx_flush_tlb(vcpu);
10302 }
10303
10304
10305 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10306 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10307 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10308 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10309 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010310
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010311 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10312 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10313 vmcs_write64(GUEST_BNDCFGS, 0);
10314
Jan Kiszka44811c02013-08-04 17:17:27 +020010315 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010316 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010317 vcpu->arch.pat = vmcs12->host_ia32_pat;
10318 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010319 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10320 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10321 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010322
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010323 /* Set L1 segment info according to Intel SDM
10324 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10325 seg = (struct kvm_segment) {
10326 .base = 0,
10327 .limit = 0xFFFFFFFF,
10328 .selector = vmcs12->host_cs_selector,
10329 .type = 11,
10330 .present = 1,
10331 .s = 1,
10332 .g = 1
10333 };
10334 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10335 seg.l = 1;
10336 else
10337 seg.db = 1;
10338 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
10339 seg = (struct kvm_segment) {
10340 .base = 0,
10341 .limit = 0xFFFFFFFF,
10342 .type = 3,
10343 .present = 1,
10344 .s = 1,
10345 .db = 1,
10346 .g = 1
10347 };
10348 seg.selector = vmcs12->host_ds_selector;
10349 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
10350 seg.selector = vmcs12->host_es_selector;
10351 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
10352 seg.selector = vmcs12->host_ss_selector;
10353 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
10354 seg.selector = vmcs12->host_fs_selector;
10355 seg.base = vmcs12->host_fs_base;
10356 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
10357 seg.selector = vmcs12->host_gs_selector;
10358 seg.base = vmcs12->host_gs_base;
10359 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
10360 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030010361 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010362 .limit = 0x67,
10363 .selector = vmcs12->host_tr_selector,
10364 .type = 11,
10365 .present = 1
10366 };
10367 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
10368
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010369 kvm_set_dr(vcpu, 7, 0x400);
10370 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030010371
Wincy Van3af18d92015-02-03 23:49:31 +080010372 if (cpu_has_vmx_msr_bitmap())
10373 vmx_set_msr_bitmap(vcpu);
10374
Wincy Vanff651cb2014-12-11 08:52:58 +030010375 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
10376 vmcs12->vm_exit_msr_load_count))
10377 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010378}
10379
10380/*
10381 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
10382 * and modify vmcs12 to make it see what it would expect to see there if
10383 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
10384 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010385static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
10386 u32 exit_intr_info,
10387 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010388{
10389 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010390 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10391
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010392 /* trying to cancel vmlaunch/vmresume is a bug */
10393 WARN_ON_ONCE(vmx->nested.nested_run_pending);
10394
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010395 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010396 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
10397 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010398
Wincy Vanff651cb2014-12-11 08:52:58 +030010399 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
10400 vmcs12->vm_exit_msr_store_count))
10401 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
10402
Wanpeng Lif3380ca52014-08-05 12:42:23 +080010403 vmx_load_vmcs01(vcpu);
10404
Bandan Das77b0f5d2014-04-19 18:17:45 -040010405 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
10406 && nested_exit_intr_ack_set(vcpu)) {
10407 int irq = kvm_cpu_get_interrupt(vcpu);
10408 WARN_ON(irq < 0);
10409 vmcs12->vm_exit_intr_info = irq |
10410 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
10411 }
10412
Jan Kiszka542060e2014-01-04 18:47:21 +010010413 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
10414 vmcs12->exit_qualification,
10415 vmcs12->idt_vectoring_info_field,
10416 vmcs12->vm_exit_intr_info,
10417 vmcs12->vm_exit_intr_error_code,
10418 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010419
Gleb Natapov2961e8762013-11-25 15:37:13 +020010420 vm_entry_controls_init(vmx, vmcs_read32(VM_ENTRY_CONTROLS));
10421 vm_exit_controls_init(vmx, vmcs_read32(VM_EXIT_CONTROLS));
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010422 vmx_segment_cache_clear(vmx);
10423
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010424 /* if no vmcs02 cache requested, remove the one we used */
10425 if (VMCS02_POOL_SIZE == 0)
10426 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
10427
10428 load_vmcs12_host_state(vcpu, vmcs12);
10429
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010430 /* Update TSC_OFFSET if TSC was changed while L2 ran */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010431 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
10432
10433 /* This is needed for same reason as it was needed in prepare_vmcs02 */
10434 vmx->host_rsp = 0;
10435
10436 /* Unpin physical memory we referred to in vmcs02 */
10437 if (vmx->nested.apic_access_page) {
10438 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010439 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010440 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010441 if (vmx->nested.virtual_apic_page) {
10442 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010443 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010444 }
Wincy Van705699a2015-02-03 23:58:17 +080010445 if (vmx->nested.pi_desc_page) {
10446 kunmap(vmx->nested.pi_desc_page);
10447 nested_release_page(vmx->nested.pi_desc_page);
10448 vmx->nested.pi_desc_page = NULL;
10449 vmx->nested.pi_desc = NULL;
10450 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010451
10452 /*
Tang Chen38b99172014-09-24 15:57:54 +080010453 * We are now running in L2, mmu_notifier will force to reload the
10454 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
10455 */
10456 kvm_vcpu_reload_apic_access_page(vcpu);
10457
10458 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010459 * Exiting from L2 to L1, we're now back to L1 which thinks it just
10460 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
10461 * success or failure flag accordingly.
10462 */
10463 if (unlikely(vmx->fail)) {
10464 vmx->fail = 0;
10465 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
10466 } else
10467 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010468 if (enable_shadow_vmcs)
10469 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010470
10471 /* in case we halted in L2 */
10472 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010473}
10474
Nadav Har'El7c177932011-05-25 23:12:04 +030010475/*
Jan Kiszka42124922014-01-04 18:47:19 +010010476 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
10477 */
10478static void vmx_leave_nested(struct kvm_vcpu *vcpu)
10479{
10480 if (is_guest_mode(vcpu))
Jan Kiszka533558b2014-01-04 18:47:20 +010010481 nested_vmx_vmexit(vcpu, -1, 0, 0);
Jan Kiszka42124922014-01-04 18:47:19 +010010482 free_nested(to_vmx(vcpu));
10483}
10484
10485/*
Nadav Har'El7c177932011-05-25 23:12:04 +030010486 * L1's failure to enter L2 is a subset of a normal exit, as explained in
10487 * 23.7 "VM-entry failures during or after loading guest state" (this also
10488 * lists the acceptable exit-reason and exit-qualification parameters).
10489 * It should only be called before L2 actually succeeded to run, and when
10490 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
10491 */
10492static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
10493 struct vmcs12 *vmcs12,
10494 u32 reason, unsigned long qualification)
10495{
10496 load_vmcs12_host_state(vcpu, vmcs12);
10497 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
10498 vmcs12->exit_qualification = qualification;
10499 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010500 if (enable_shadow_vmcs)
10501 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030010502}
10503
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020010504static int vmx_check_intercept(struct kvm_vcpu *vcpu,
10505 struct x86_instruction_info *info,
10506 enum x86_intercept_stage stage)
10507{
10508 return X86EMUL_CONTINUE;
10509}
10510
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010511static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010512{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020010513 if (ple_gap)
10514 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010515}
10516
Kai Huang843e4332015-01-28 10:54:28 +080010517static void vmx_slot_enable_log_dirty(struct kvm *kvm,
10518 struct kvm_memory_slot *slot)
10519{
10520 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
10521 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
10522}
10523
10524static void vmx_slot_disable_log_dirty(struct kvm *kvm,
10525 struct kvm_memory_slot *slot)
10526{
10527 kvm_mmu_slot_set_dirty(kvm, slot);
10528}
10529
10530static void vmx_flush_log_dirty(struct kvm *kvm)
10531{
10532 kvm_flush_pml_buffers(kvm);
10533}
10534
10535static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
10536 struct kvm_memory_slot *memslot,
10537 gfn_t offset, unsigned long mask)
10538{
10539 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
10540}
10541
Feng Wuefc64402015-09-18 22:29:51 +080010542/*
Feng Wubf9f6ac2015-09-18 22:29:55 +080010543 * This routine does the following things for vCPU which is going
10544 * to be blocked if VT-d PI is enabled.
10545 * - Store the vCPU to the wakeup list, so when interrupts happen
10546 * we can find the right vCPU to wake up.
10547 * - Change the Posted-interrupt descriptor as below:
10548 * 'NDST' <-- vcpu->pre_pcpu
10549 * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
10550 * - If 'ON' is set during this process, which means at least one
10551 * interrupt is posted for this vCPU, we cannot block it, in
10552 * this case, return 1, otherwise, return 0.
10553 *
10554 */
10555static int vmx_pre_block(struct kvm_vcpu *vcpu)
10556{
10557 unsigned long flags;
10558 unsigned int dest;
10559 struct pi_desc old, new;
10560 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
10561
10562 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
10563 !irq_remapping_cap(IRQ_POSTING_CAP))
10564 return 0;
10565
10566 vcpu->pre_pcpu = vcpu->cpu;
10567 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
10568 vcpu->pre_pcpu), flags);
10569 list_add_tail(&vcpu->blocked_vcpu_list,
10570 &per_cpu(blocked_vcpu_on_cpu,
10571 vcpu->pre_pcpu));
10572 spin_unlock_irqrestore(&per_cpu(blocked_vcpu_on_cpu_lock,
10573 vcpu->pre_pcpu), flags);
10574
10575 do {
10576 old.control = new.control = pi_desc->control;
10577
10578 /*
10579 * We should not block the vCPU if
10580 * an interrupt is posted for it.
10581 */
10582 if (pi_test_on(pi_desc) == 1) {
10583 spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock,
10584 vcpu->pre_pcpu), flags);
10585 list_del(&vcpu->blocked_vcpu_list);
10586 spin_unlock_irqrestore(
10587 &per_cpu(blocked_vcpu_on_cpu_lock,
10588 vcpu->pre_pcpu), flags);
10589 vcpu->pre_pcpu = -1;
10590
10591 return 1;
10592 }
10593
10594 WARN((pi_desc->sn == 1),
10595 "Warning: SN field of posted-interrupts "
10596 "is set before blocking\n");
10597
10598 /*
10599 * Since vCPU can be preempted during this process,
10600 * vcpu->cpu could be different with pre_pcpu, we
10601 * need to set pre_pcpu as the destination of wakeup
10602 * notification event, then we can find the right vCPU
10603 * to wakeup in wakeup handler if interrupts happen
10604 * when the vCPU is in blocked state.
10605 */
10606 dest = cpu_physical_id(vcpu->pre_pcpu);
10607
10608 if (x2apic_enabled())
10609 new.ndst = dest;
10610 else
10611 new.ndst = (dest << 8) & 0xFF00;
10612
10613 /* set 'NV' to 'wakeup vector' */
10614 new.nv = POSTED_INTR_WAKEUP_VECTOR;
10615 } while (cmpxchg(&pi_desc->control, old.control,
10616 new.control) != old.control);
10617
10618 return 0;
10619}
10620
10621static void vmx_post_block(struct kvm_vcpu *vcpu)
10622{
10623 struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
10624 struct pi_desc old, new;
10625 unsigned int dest;
10626 unsigned long flags;
10627
10628 if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
10629 !irq_remapping_cap(IRQ_POSTING_CAP))
10630 return;
10631
10632 do {
10633 old.control = new.control = pi_desc->control;
10634
10635 dest = cpu_physical_id(vcpu->cpu);
10636
10637 if (x2apic_enabled())
10638 new.ndst = dest;
10639 else
10640 new.ndst = (dest << 8) & 0xFF00;
10641
10642 /* Allow posting non-urgent interrupts */
10643 new.sn = 0;
10644
10645 /* set 'NV' to 'notification vector' */
10646 new.nv = POSTED_INTR_VECTOR;
10647 } while (cmpxchg(&pi_desc->control, old.control,
10648 new.control) != old.control);
10649
10650 if(vcpu->pre_pcpu != -1) {
10651 spin_lock_irqsave(
10652 &per_cpu(blocked_vcpu_on_cpu_lock,
10653 vcpu->pre_pcpu), flags);
10654 list_del(&vcpu->blocked_vcpu_list);
10655 spin_unlock_irqrestore(
10656 &per_cpu(blocked_vcpu_on_cpu_lock,
10657 vcpu->pre_pcpu), flags);
10658 vcpu->pre_pcpu = -1;
10659 }
10660}
10661
10662/*
Feng Wuefc64402015-09-18 22:29:51 +080010663 * vmx_update_pi_irte - set IRTE for Posted-Interrupts
10664 *
10665 * @kvm: kvm
10666 * @host_irq: host irq of the interrupt
10667 * @guest_irq: gsi of the interrupt
10668 * @set: set or unset PI
10669 * returns 0 on success, < 0 on failure
10670 */
10671static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
10672 uint32_t guest_irq, bool set)
10673{
10674 struct kvm_kernel_irq_routing_entry *e;
10675 struct kvm_irq_routing_table *irq_rt;
10676 struct kvm_lapic_irq irq;
10677 struct kvm_vcpu *vcpu;
10678 struct vcpu_data vcpu_info;
10679 int idx, ret = -EINVAL;
10680
10681 if (!kvm_arch_has_assigned_device(kvm) ||
10682 !irq_remapping_cap(IRQ_POSTING_CAP))
10683 return 0;
10684
10685 idx = srcu_read_lock(&kvm->irq_srcu);
10686 irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
10687 BUG_ON(guest_irq >= irq_rt->nr_rt_entries);
10688
10689 hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
10690 if (e->type != KVM_IRQ_ROUTING_MSI)
10691 continue;
10692 /*
10693 * VT-d PI cannot support posting multicast/broadcast
10694 * interrupts to a vCPU, we still use interrupt remapping
10695 * for these kind of interrupts.
10696 *
10697 * For lowest-priority interrupts, we only support
10698 * those with single CPU as the destination, e.g. user
10699 * configures the interrupts via /proc/irq or uses
10700 * irqbalance to make the interrupts single-CPU.
10701 *
10702 * We will support full lowest-priority interrupt later.
10703 */
10704
10705 kvm_set_msi_irq(e, &irq);
10706 if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu))
10707 continue;
10708
10709 vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
10710 vcpu_info.vector = irq.vector;
10711
10712 trace_kvm_pi_irte_update(vcpu->vcpu_id, e->gsi,
10713 vcpu_info.vector, vcpu_info.pi_desc_addr, set);
10714
10715 if (set)
10716 ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
10717 else {
10718 /* suppress notification event before unposting */
10719 pi_set_sn(vcpu_to_pi_desc(vcpu));
10720 ret = irq_set_vcpu_affinity(host_irq, NULL);
10721 pi_clear_sn(vcpu_to_pi_desc(vcpu));
10722 }
10723
10724 if (ret < 0) {
10725 printk(KERN_INFO "%s: failed to update PI IRTE\n",
10726 __func__);
10727 goto out;
10728 }
10729 }
10730
10731 ret = 0;
10732out:
10733 srcu_read_unlock(&kvm->irq_srcu, idx);
10734 return ret;
10735}
10736
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030010737static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080010738 .cpu_has_kvm_support = cpu_has_kvm_support,
10739 .disabled_by_bios = vmx_disabled_by_bios,
10740 .hardware_setup = hardware_setup,
10741 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030010742 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010743 .hardware_enable = hardware_enable,
10744 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080010745 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010746 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010747
10748 .vcpu_create = vmx_create_vcpu,
10749 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030010750 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010751
Avi Kivity04d2cc72007-09-10 18:10:54 +030010752 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010753 .vcpu_load = vmx_vcpu_load,
10754 .vcpu_put = vmx_vcpu_put,
10755
Jan Kiszkac8639012012-09-21 05:42:55 +020010756 .update_db_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010757 .get_msr = vmx_get_msr,
10758 .set_msr = vmx_set_msr,
10759 .get_segment_base = vmx_get_segment_base,
10760 .get_segment = vmx_get_segment,
10761 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020010762 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010763 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020010764 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020010765 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030010766 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010767 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010768 .set_cr3 = vmx_set_cr3,
10769 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010770 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010771 .get_idt = vmx_get_idt,
10772 .set_idt = vmx_set_idt,
10773 .get_gdt = vmx_get_gdt,
10774 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010010775 .get_dr6 = vmx_get_dr6,
10776 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030010777 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010010778 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010779 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010780 .get_rflags = vmx_get_rflags,
10781 .set_rflags = vmx_set_rflags,
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020010782 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020010783 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010784
10785 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010786
Avi Kivity6aa8b732006-12-10 02:21:36 -080010787 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020010788 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010789 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040010790 .set_interrupt_shadow = vmx_set_interrupt_shadow,
10791 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020010792 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030010793 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010794 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020010795 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010796 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020010797 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010798 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010010799 .get_nmi_mask = vmx_get_nmi_mask,
10800 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010801 .enable_nmi_window = enable_nmi_window,
10802 .enable_irq_window = enable_irq_window,
10803 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080010804 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080010805 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +020010806 .cpu_uses_apicv = vmx_cpu_uses_apicv,
Yang Zhangc7c9c562013-01-25 10:18:51 +080010807 .load_eoi_exitmap = vmx_load_eoi_exitmap,
10808 .hwapic_irr_update = vmx_hwapic_irr_update,
10809 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080010810 .sync_pir_to_irr = vmx_sync_pir_to_irr,
10811 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010812
Izik Eiduscbc94022007-10-25 00:29:55 +020010813 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080010814 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010815 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010816
Avi Kivity586f9602010-11-18 13:09:54 +020010817 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020010818
Sheng Yang17cc3932010-01-05 19:02:27 +080010819 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080010820
10821 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010822
10823 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000010824 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010825
10826 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080010827
10828 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100010829
Will Auldba904632012-11-29 12:42:50 -080010830 .read_tsc_offset = vmx_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100010831 .write_tsc_offset = vmx_write_tsc_offset,
Haozhong Zhang58ea6762015-10-20 15:39:06 +080010832 .adjust_tsc_offset_guest = vmx_adjust_tsc_offset_guest,
Nadav Har'Eld5c17852011-08-02 15:54:20 +030010833 .read_l1_tsc = vmx_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020010834
10835 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020010836
10837 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080010838 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010839 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080010840 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010841
10842 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010843
10844 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080010845
10846 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
10847 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
10848 .flush_log_dirty = vmx_flush_log_dirty,
10849 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020010850
Feng Wubf9f6ac2015-09-18 22:29:55 +080010851 .pre_block = vmx_pre_block,
10852 .post_block = vmx_post_block,
10853
Wei Huang25462f72015-06-19 15:45:05 +020010854 .pmu_ops = &intel_pmu_ops,
Feng Wuefc64402015-09-18 22:29:51 +080010855
10856 .update_pi_irte = vmx_update_pi_irte,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010857};
10858
10859static int __init vmx_init(void)
10860{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080010861 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
10862 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030010863 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080010864 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080010865
Dave Young2965faa2015-09-09 15:38:55 -070010866#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080010867 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
10868 crash_vmclear_local_loaded_vmcss);
10869#endif
10870
He, Qingfdef3ad2007-04-30 09:45:24 +030010871 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010872}
10873
10874static void __exit vmx_exit(void)
10875{
Dave Young2965faa2015-09-09 15:38:55 -070010876#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053010877 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080010878 synchronize_rcu();
10879#endif
10880
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080010881 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080010882}
10883
10884module_init(vmx_init)
10885module_exit(vmx_exit)