blob: 4ebdccd882c890dca9ab4d58c288fd1396d3a29b [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
Avi Kivity6aa8b732006-12-10 02:21:36 -080038#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080039#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020040#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020041#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080042#include <asm/mce.h>
Ingo Molnar952f07e2015-04-26 16:56:05 +020043#include <asm/fpu/internal.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020044#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010045#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080046#include <asm/kexec.h>
Radim Krčmářdab20872015-02-09 22:44:07 +010047#include <asm/apic.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080048
Marcelo Tosatti229456f2009-06-17 09:22:14 -030049#include "trace.h"
Wei Huang25462f72015-06-19 15:45:05 +020050#include "pmu.h"
Marcelo Tosatti229456f2009-06-17 09:22:14 -030051
Avi Kivity4ecac3f2008-05-13 13:23:38 +030052#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040053#define __ex_clear(x, reg) \
54 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030055
Avi Kivity6aa8b732006-12-10 02:21:36 -080056MODULE_AUTHOR("Qumranet");
57MODULE_LICENSE("GPL");
58
Josh Triplette9bda3b2012-03-20 23:33:51 -070059static const struct x86_cpu_id vmx_cpu_id[] = {
60 X86_FEATURE_MATCH(X86_FEATURE_VMX),
61 {}
62};
63MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
64
Rusty Russell476bc002012-01-13 09:32:18 +103065static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020066module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080067
Rusty Russell476bc002012-01-13 09:32:18 +103068static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020069module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020070
Rusty Russell476bc002012-01-13 09:32:18 +103071static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020072module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080073
Rusty Russell476bc002012-01-13 09:32:18 +103074static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070075module_param_named(unrestricted_guest,
76 enable_unrestricted_guest, bool, S_IRUGO);
77
Xudong Hao83c3a332012-05-28 19:33:35 +080078static bool __read_mostly enable_ept_ad_bits = 1;
79module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
80
Avi Kivitya27685c2012-06-12 20:30:18 +030081static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020082module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030083
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080085module_param(vmm_exclusive, bool, S_IRUGO);
86
Rusty Russell476bc002012-01-13 09:32:18 +103087static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030088module_param(fasteoi, bool, S_IRUGO);
89
Yang Zhang5a717852013-04-11 19:25:16 +080090static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080091module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080092
Abel Gordonabc4fc52013-04-18 14:35:25 +030093static bool __read_mostly enable_shadow_vmcs = 1;
94module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +030095/*
96 * If nested=1, nested virtualization is supported, i.e., guests may use
97 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
98 * use VMX instructions.
99 */
Rusty Russell476bc002012-01-13 09:32:18 +1030100static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300101module_param(nested, bool, S_IRUGO);
102
Wanpeng Li20300092014-12-02 19:14:59 +0800103static u64 __read_mostly host_xss;
104
Kai Huang843e4332015-01-28 10:54:28 +0800105static bool __read_mostly enable_pml = 1;
106module_param_named(pml, enable_pml, bool, S_IRUGO);
107
Gleb Natapov50378782013-02-04 16:00:28 +0200108#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
109#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200110#define KVM_VM_CR0_ALWAYS_ON \
111 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200112#define KVM_CR4_GUEST_OWNED_BITS \
113 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700114 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200115
Avi Kivitycdc0e242009-12-06 17:21:14 +0200116#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
117#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
118
Avi Kivity78ac8b42010-04-08 18:19:35 +0300119#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
120
Jan Kiszkaf41245002014-03-07 20:03:13 +0100121#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
122
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800123/*
124 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
125 * ple_gap: upper bound on the amount of time between two successive
126 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500127 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800128 * ple_window: upper bound on the amount of time a guest is allowed to execute
129 * in a PAUSE loop. Tests indicate that most spinlocks are held for
130 * less than 2^12 cycles
131 * Time is measured based on a counter that runs at the same rate as the TSC,
132 * refer SDM volume 3b section 21.6.13 & 22.1.3.
133 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200134#define KVM_VMX_DEFAULT_PLE_GAP 128
135#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
136#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
137#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
138#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
139 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
140
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800141static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
142module_param(ple_gap, int, S_IRUGO);
143
144static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
145module_param(ple_window, int, S_IRUGO);
146
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200147/* Default doubles per-vcpu window every exit. */
148static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
149module_param(ple_window_grow, int, S_IRUGO);
150
151/* Default resets per-vcpu window every exit to ple_window. */
152static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
153module_param(ple_window_shrink, int, S_IRUGO);
154
155/* Default is to compute the maximum so we can never overflow. */
156static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
157static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
158module_param(ple_window_max, int, S_IRUGO);
159
Avi Kivity83287ea422012-09-16 15:10:57 +0300160extern const ulong vmx_return;
161
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200162#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300163#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300164
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400165struct vmcs {
166 u32 revision_id;
167 u32 abort;
168 char data[0];
169};
170
Nadav Har'Eld462b812011-05-24 15:26:10 +0300171/*
172 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
173 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
174 * loaded on this CPU (so we can clear them if the CPU goes down).
175 */
176struct loaded_vmcs {
177 struct vmcs *vmcs;
178 int cpu;
179 int launched;
180 struct list_head loaded_vmcss_on_cpu_link;
181};
182
Avi Kivity26bb0982009-09-07 11:14:12 +0300183struct shared_msr_entry {
184 unsigned index;
185 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200186 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300187};
188
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300189/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300190 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
191 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
192 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
193 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
194 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
195 * More than one of these structures may exist, if L1 runs multiple L2 guests.
196 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
197 * underlying hardware which will be used to run L2.
198 * This structure is packed to ensure that its layout is identical across
199 * machines (necessary for live migration).
200 * If there are changes in this struct, VMCS12_REVISION must be changed.
201 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300202typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300203struct __packed vmcs12 {
204 /* According to the Intel spec, a VMCS region must start with the
205 * following two fields. Then follow implementation-specific data.
206 */
207 u32 revision_id;
208 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300209
Nadav Har'El27d6c862011-05-25 23:06:59 +0300210 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
211 u32 padding[7]; /* room for future expansion */
212
Nadav Har'El22bd0352011-05-25 23:05:57 +0300213 u64 io_bitmap_a;
214 u64 io_bitmap_b;
215 u64 msr_bitmap;
216 u64 vm_exit_msr_store_addr;
217 u64 vm_exit_msr_load_addr;
218 u64 vm_entry_msr_load_addr;
219 u64 tsc_offset;
220 u64 virtual_apic_page_addr;
221 u64 apic_access_addr;
Wincy Van705699a2015-02-03 23:58:17 +0800222 u64 posted_intr_desc_addr;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300223 u64 ept_pointer;
Wincy Van608406e2015-02-03 23:57:51 +0800224 u64 eoi_exit_bitmap0;
225 u64 eoi_exit_bitmap1;
226 u64 eoi_exit_bitmap2;
227 u64 eoi_exit_bitmap3;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800228 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300229 u64 guest_physical_address;
230 u64 vmcs_link_pointer;
231 u64 guest_ia32_debugctl;
232 u64 guest_ia32_pat;
233 u64 guest_ia32_efer;
234 u64 guest_ia32_perf_global_ctrl;
235 u64 guest_pdptr0;
236 u64 guest_pdptr1;
237 u64 guest_pdptr2;
238 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100239 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300240 u64 host_ia32_pat;
241 u64 host_ia32_efer;
242 u64 host_ia32_perf_global_ctrl;
243 u64 padding64[8]; /* room for future expansion */
244 /*
245 * To allow migration of L1 (complete with its L2 guests) between
246 * machines of different natural widths (32 or 64 bit), we cannot have
247 * unsigned long fields with no explict size. We use u64 (aliased
248 * natural_width) instead. Luckily, x86 is little-endian.
249 */
250 natural_width cr0_guest_host_mask;
251 natural_width cr4_guest_host_mask;
252 natural_width cr0_read_shadow;
253 natural_width cr4_read_shadow;
254 natural_width cr3_target_value0;
255 natural_width cr3_target_value1;
256 natural_width cr3_target_value2;
257 natural_width cr3_target_value3;
258 natural_width exit_qualification;
259 natural_width guest_linear_address;
260 natural_width guest_cr0;
261 natural_width guest_cr3;
262 natural_width guest_cr4;
263 natural_width guest_es_base;
264 natural_width guest_cs_base;
265 natural_width guest_ss_base;
266 natural_width guest_ds_base;
267 natural_width guest_fs_base;
268 natural_width guest_gs_base;
269 natural_width guest_ldtr_base;
270 natural_width guest_tr_base;
271 natural_width guest_gdtr_base;
272 natural_width guest_idtr_base;
273 natural_width guest_dr7;
274 natural_width guest_rsp;
275 natural_width guest_rip;
276 natural_width guest_rflags;
277 natural_width guest_pending_dbg_exceptions;
278 natural_width guest_sysenter_esp;
279 natural_width guest_sysenter_eip;
280 natural_width host_cr0;
281 natural_width host_cr3;
282 natural_width host_cr4;
283 natural_width host_fs_base;
284 natural_width host_gs_base;
285 natural_width host_tr_base;
286 natural_width host_gdtr_base;
287 natural_width host_idtr_base;
288 natural_width host_ia32_sysenter_esp;
289 natural_width host_ia32_sysenter_eip;
290 natural_width host_rsp;
291 natural_width host_rip;
292 natural_width paddingl[8]; /* room for future expansion */
293 u32 pin_based_vm_exec_control;
294 u32 cpu_based_vm_exec_control;
295 u32 exception_bitmap;
296 u32 page_fault_error_code_mask;
297 u32 page_fault_error_code_match;
298 u32 cr3_target_count;
299 u32 vm_exit_controls;
300 u32 vm_exit_msr_store_count;
301 u32 vm_exit_msr_load_count;
302 u32 vm_entry_controls;
303 u32 vm_entry_msr_load_count;
304 u32 vm_entry_intr_info_field;
305 u32 vm_entry_exception_error_code;
306 u32 vm_entry_instruction_len;
307 u32 tpr_threshold;
308 u32 secondary_vm_exec_control;
309 u32 vm_instruction_error;
310 u32 vm_exit_reason;
311 u32 vm_exit_intr_info;
312 u32 vm_exit_intr_error_code;
313 u32 idt_vectoring_info_field;
314 u32 idt_vectoring_error_code;
315 u32 vm_exit_instruction_len;
316 u32 vmx_instruction_info;
317 u32 guest_es_limit;
318 u32 guest_cs_limit;
319 u32 guest_ss_limit;
320 u32 guest_ds_limit;
321 u32 guest_fs_limit;
322 u32 guest_gs_limit;
323 u32 guest_ldtr_limit;
324 u32 guest_tr_limit;
325 u32 guest_gdtr_limit;
326 u32 guest_idtr_limit;
327 u32 guest_es_ar_bytes;
328 u32 guest_cs_ar_bytes;
329 u32 guest_ss_ar_bytes;
330 u32 guest_ds_ar_bytes;
331 u32 guest_fs_ar_bytes;
332 u32 guest_gs_ar_bytes;
333 u32 guest_ldtr_ar_bytes;
334 u32 guest_tr_ar_bytes;
335 u32 guest_interruptibility_info;
336 u32 guest_activity_state;
337 u32 guest_sysenter_cs;
338 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100339 u32 vmx_preemption_timer_value;
340 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300341 u16 virtual_processor_id;
Wincy Van705699a2015-02-03 23:58:17 +0800342 u16 posted_intr_nv;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300343 u16 guest_es_selector;
344 u16 guest_cs_selector;
345 u16 guest_ss_selector;
346 u16 guest_ds_selector;
347 u16 guest_fs_selector;
348 u16 guest_gs_selector;
349 u16 guest_ldtr_selector;
350 u16 guest_tr_selector;
Wincy Van608406e2015-02-03 23:57:51 +0800351 u16 guest_intr_status;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300352 u16 host_es_selector;
353 u16 host_cs_selector;
354 u16 host_ss_selector;
355 u16 host_ds_selector;
356 u16 host_fs_selector;
357 u16 host_gs_selector;
358 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300359};
360
361/*
362 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
363 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
364 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
365 */
366#define VMCS12_REVISION 0x11e57ed0
367
368/*
369 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
370 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
371 * current implementation, 4K are reserved to avoid future complications.
372 */
373#define VMCS12_SIZE 0x1000
374
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300375/* Used to remember the last vmcs02 used for some recently used vmcs12s */
376struct vmcs02_list {
377 struct list_head list;
378 gpa_t vmptr;
379 struct loaded_vmcs vmcs02;
380};
381
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300382/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300383 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
384 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
385 */
386struct nested_vmx {
387 /* Has the level1 guest done vmxon? */
388 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400389 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300390
391 /* The guest-physical address of the current VMCS L1 keeps for L2 */
392 gpa_t current_vmptr;
393 /* The host-usable pointer to the above */
394 struct page *current_vmcs12_page;
395 struct vmcs12 *current_vmcs12;
Abel Gordon8de48832013-04-18 14:37:25 +0300396 struct vmcs *current_shadow_vmcs;
Abel Gordon012f83c2013-04-18 14:39:25 +0300397 /*
398 * Indicates if the shadow vmcs must be updated with the
399 * data hold by vmcs12
400 */
401 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300402
403 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
404 struct list_head vmcs02_pool;
405 int vmcs02_num;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300406 u64 vmcs01_tsc_offset;
Nadav Har'El644d7112011-05-25 23:12:35 +0300407 /* L2 must run next, and mustn't decide to exit to L1. */
408 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300409 /*
410 * Guest pages referred to in vmcs02 with host-physical pointers, so
411 * we must keep them pinned while L2 runs.
412 */
413 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800414 struct page *virtual_apic_page;
Wincy Van705699a2015-02-03 23:58:17 +0800415 struct page *pi_desc_page;
416 struct pi_desc *pi_desc;
417 bool pi_pending;
418 u16 posted_intr_nv;
Nadav Har'Elb3897a42013-07-08 19:12:35 +0800419 u64 msr_ia32_feature_control;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100420
421 struct hrtimer preemption_timer;
422 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200423
424 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
425 u64 vmcs01_debugctl;
Wincy Vanb9c237b2015-02-03 23:56:30 +0800426
427 u32 nested_vmx_procbased_ctls_low;
428 u32 nested_vmx_procbased_ctls_high;
429 u32 nested_vmx_true_procbased_ctls_low;
430 u32 nested_vmx_secondary_ctls_low;
431 u32 nested_vmx_secondary_ctls_high;
432 u32 nested_vmx_pinbased_ctls_low;
433 u32 nested_vmx_pinbased_ctls_high;
434 u32 nested_vmx_exit_ctls_low;
435 u32 nested_vmx_exit_ctls_high;
436 u32 nested_vmx_true_exit_ctls_low;
437 u32 nested_vmx_entry_ctls_low;
438 u32 nested_vmx_entry_ctls_high;
439 u32 nested_vmx_true_entry_ctls_low;
440 u32 nested_vmx_misc_low;
441 u32 nested_vmx_misc_high;
442 u32 nested_vmx_ept_caps;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300443};
444
Yang Zhang01e439b2013-04-11 19:25:12 +0800445#define POSTED_INTR_ON 0
446/* Posted-Interrupt Descriptor */
447struct pi_desc {
448 u32 pir[8]; /* Posted interrupt requested */
Feng Wu6ef15222015-09-18 22:29:45 +0800449 union {
450 struct {
451 /* bit 256 - Outstanding Notification */
452 u16 on : 1,
453 /* bit 257 - Suppress Notification */
454 sn : 1,
455 /* bit 271:258 - Reserved */
456 rsvd_1 : 14;
457 /* bit 279:272 - Notification Vector */
458 u8 nv;
459 /* bit 287:280 - Reserved */
460 u8 rsvd_2;
461 /* bit 319:288 - Notification Destination */
462 u32 ndst;
463 };
464 u64 control;
465 };
466 u32 rsvd[6];
Yang Zhang01e439b2013-04-11 19:25:12 +0800467} __aligned(64);
468
Yang Zhanga20ed542013-04-11 19:25:15 +0800469static bool pi_test_and_set_on(struct pi_desc *pi_desc)
470{
471 return test_and_set_bit(POSTED_INTR_ON,
472 (unsigned long *)&pi_desc->control);
473}
474
475static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
476{
477 return test_and_clear_bit(POSTED_INTR_ON,
478 (unsigned long *)&pi_desc->control);
479}
480
481static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
482{
483 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
484}
485
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400486struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000487 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300488 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300489 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200490 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300491 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200492 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200493 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300494 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400495 int nmsrs;
496 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800497 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400498#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300499 u64 msr_host_kernel_gs_base;
500 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400501#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200502 u32 vm_entry_controls_shadow;
503 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300504 /*
505 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
506 * non-nested (L1) guest, it always points to vmcs01. For a nested
507 * guest (L2), it points to a different VMCS.
508 */
509 struct loaded_vmcs vmcs01;
510 struct loaded_vmcs *loaded_vmcs;
511 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300512 struct msr_autoload {
513 unsigned nr;
514 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
515 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
516 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400517 struct {
518 int loaded;
519 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300520#ifdef CONFIG_X86_64
521 u16 ds_sel, es_sel;
522#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200523 int gs_ldt_reload_needed;
524 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000525 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700526 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400527 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200528 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300529 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300530 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300531 struct kvm_segment segs[8];
532 } rmode;
533 struct {
534 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300535 struct kvm_save_segment {
536 u16 selector;
537 unsigned long base;
538 u32 limit;
539 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300540 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300541 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800542 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300543 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200544
545 /* Support for vnmi-less CPUs */
546 int soft_vnmi_blocked;
547 ktime_t entry_time;
548 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800549 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800550
Yang Zhang01e439b2013-04-11 19:25:12 +0800551 /* Posted interrupt descriptor */
552 struct pi_desc pi_desc;
553
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300554 /* Support for a guest hypervisor (nested VMX) */
555 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200556
557 /* Dynamic PLE window. */
558 int ple_window;
559 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800560
561 /* Support for PML */
562#define PML_ENTITY_NUM 512
563 struct page *pml_pg;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400564};
565
Avi Kivity2fb92db2011-04-27 19:42:18 +0300566enum segment_cache_field {
567 SEG_FIELD_SEL = 0,
568 SEG_FIELD_BASE = 1,
569 SEG_FIELD_LIMIT = 2,
570 SEG_FIELD_AR = 3,
571
572 SEG_FIELD_NR = 4
573};
574
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400575static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
576{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000577 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400578}
579
Nadav Har'El22bd0352011-05-25 23:05:57 +0300580#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
581#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
582#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
583 [number##_HIGH] = VMCS12_OFFSET(name)+4
584
Abel Gordon4607c2d2013-04-18 14:35:55 +0300585
Bandan Dasfe2b2012014-04-21 15:20:14 -0400586static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300587 /*
588 * We do NOT shadow fields that are modified when L0
589 * traps and emulates any vmx instruction (e.g. VMPTRLD,
590 * VMXON...) executed by L1.
591 * For example, VM_INSTRUCTION_ERROR is read
592 * by L1 if a vmx instruction fails (part of the error path).
593 * Note the code assumes this logic. If for some reason
594 * we start shadowing these fields then we need to
595 * force a shadow sync when L0 emulates vmx instructions
596 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
597 * by nested_vmx_failValid)
598 */
599 VM_EXIT_REASON,
600 VM_EXIT_INTR_INFO,
601 VM_EXIT_INSTRUCTION_LEN,
602 IDT_VECTORING_INFO_FIELD,
603 IDT_VECTORING_ERROR_CODE,
604 VM_EXIT_INTR_ERROR_CODE,
605 EXIT_QUALIFICATION,
606 GUEST_LINEAR_ADDRESS,
607 GUEST_PHYSICAL_ADDRESS
608};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400609static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300610 ARRAY_SIZE(shadow_read_only_fields);
611
Bandan Dasfe2b2012014-04-21 15:20:14 -0400612static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800613 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300614 GUEST_RIP,
615 GUEST_RSP,
616 GUEST_CR0,
617 GUEST_CR3,
618 GUEST_CR4,
619 GUEST_INTERRUPTIBILITY_INFO,
620 GUEST_RFLAGS,
621 GUEST_CS_SELECTOR,
622 GUEST_CS_AR_BYTES,
623 GUEST_CS_LIMIT,
624 GUEST_CS_BASE,
625 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100626 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300627 CR0_GUEST_HOST_MASK,
628 CR0_READ_SHADOW,
629 CR4_READ_SHADOW,
630 TSC_OFFSET,
631 EXCEPTION_BITMAP,
632 CPU_BASED_VM_EXEC_CONTROL,
633 VM_ENTRY_EXCEPTION_ERROR_CODE,
634 VM_ENTRY_INTR_INFO_FIELD,
635 VM_ENTRY_INSTRUCTION_LEN,
636 VM_ENTRY_EXCEPTION_ERROR_CODE,
637 HOST_FS_BASE,
638 HOST_GS_BASE,
639 HOST_FS_SELECTOR,
640 HOST_GS_SELECTOR
641};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400642static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300643 ARRAY_SIZE(shadow_read_write_fields);
644
Mathias Krause772e0312012-08-30 01:30:19 +0200645static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300646 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
Wincy Van705699a2015-02-03 23:58:17 +0800647 FIELD(POSTED_INTR_NV, posted_intr_nv),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300648 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
649 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
650 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
651 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
652 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
653 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
654 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
655 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
Wincy Van608406e2015-02-03 23:57:51 +0800656 FIELD(GUEST_INTR_STATUS, guest_intr_status),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300657 FIELD(HOST_ES_SELECTOR, host_es_selector),
658 FIELD(HOST_CS_SELECTOR, host_cs_selector),
659 FIELD(HOST_SS_SELECTOR, host_ss_selector),
660 FIELD(HOST_DS_SELECTOR, host_ds_selector),
661 FIELD(HOST_FS_SELECTOR, host_fs_selector),
662 FIELD(HOST_GS_SELECTOR, host_gs_selector),
663 FIELD(HOST_TR_SELECTOR, host_tr_selector),
664 FIELD64(IO_BITMAP_A, io_bitmap_a),
665 FIELD64(IO_BITMAP_B, io_bitmap_b),
666 FIELD64(MSR_BITMAP, msr_bitmap),
667 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
668 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
669 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
670 FIELD64(TSC_OFFSET, tsc_offset),
671 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
672 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
Wincy Van705699a2015-02-03 23:58:17 +0800673 FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300674 FIELD64(EPT_POINTER, ept_pointer),
Wincy Van608406e2015-02-03 23:57:51 +0800675 FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0),
676 FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1),
677 FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2),
678 FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800679 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300680 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
681 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
682 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
683 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
684 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
685 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
686 FIELD64(GUEST_PDPTR0, guest_pdptr0),
687 FIELD64(GUEST_PDPTR1, guest_pdptr1),
688 FIELD64(GUEST_PDPTR2, guest_pdptr2),
689 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100690 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300691 FIELD64(HOST_IA32_PAT, host_ia32_pat),
692 FIELD64(HOST_IA32_EFER, host_ia32_efer),
693 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
694 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
695 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
696 FIELD(EXCEPTION_BITMAP, exception_bitmap),
697 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
698 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
699 FIELD(CR3_TARGET_COUNT, cr3_target_count),
700 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
701 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
702 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
703 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
704 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
705 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
706 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
707 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
708 FIELD(TPR_THRESHOLD, tpr_threshold),
709 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
710 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
711 FIELD(VM_EXIT_REASON, vm_exit_reason),
712 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
713 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
714 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
715 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
716 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
717 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
718 FIELD(GUEST_ES_LIMIT, guest_es_limit),
719 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
720 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
721 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
722 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
723 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
724 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
725 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
726 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
727 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
728 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
729 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
730 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
731 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
732 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
733 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
734 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
735 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
736 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
737 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
738 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
739 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100740 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300741 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
742 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
743 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
744 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
745 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
746 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
747 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
748 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
749 FIELD(EXIT_QUALIFICATION, exit_qualification),
750 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
751 FIELD(GUEST_CR0, guest_cr0),
752 FIELD(GUEST_CR3, guest_cr3),
753 FIELD(GUEST_CR4, guest_cr4),
754 FIELD(GUEST_ES_BASE, guest_es_base),
755 FIELD(GUEST_CS_BASE, guest_cs_base),
756 FIELD(GUEST_SS_BASE, guest_ss_base),
757 FIELD(GUEST_DS_BASE, guest_ds_base),
758 FIELD(GUEST_FS_BASE, guest_fs_base),
759 FIELD(GUEST_GS_BASE, guest_gs_base),
760 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
761 FIELD(GUEST_TR_BASE, guest_tr_base),
762 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
763 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
764 FIELD(GUEST_DR7, guest_dr7),
765 FIELD(GUEST_RSP, guest_rsp),
766 FIELD(GUEST_RIP, guest_rip),
767 FIELD(GUEST_RFLAGS, guest_rflags),
768 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
769 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
770 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
771 FIELD(HOST_CR0, host_cr0),
772 FIELD(HOST_CR3, host_cr3),
773 FIELD(HOST_CR4, host_cr4),
774 FIELD(HOST_FS_BASE, host_fs_base),
775 FIELD(HOST_GS_BASE, host_gs_base),
776 FIELD(HOST_TR_BASE, host_tr_base),
777 FIELD(HOST_GDTR_BASE, host_gdtr_base),
778 FIELD(HOST_IDTR_BASE, host_idtr_base),
779 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
780 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
781 FIELD(HOST_RSP, host_rsp),
782 FIELD(HOST_RIP, host_rip),
783};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300784
785static inline short vmcs_field_to_offset(unsigned long field)
786{
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100787 BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
788
789 if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
790 vmcs_field_to_offset_table[field] == 0)
791 return -ENOENT;
792
Nadav Har'El22bd0352011-05-25 23:05:57 +0300793 return vmcs_field_to_offset_table[field];
794}
795
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300796static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
797{
798 return to_vmx(vcpu)->nested.current_vmcs12;
799}
800
801static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
802{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +0200803 struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800804 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300805 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800806
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300807 return page;
808}
809
810static void nested_release_page(struct page *page)
811{
812 kvm_release_page_dirty(page);
813}
814
815static void nested_release_page_clean(struct page *page)
816{
817 kvm_release_page_clean(page);
818}
819
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300820static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800821static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800822static void kvm_cpu_vmxon(u64 addr);
823static void kvm_cpu_vmxoff(void);
Paolo Bonzini93c4adc2014-03-05 23:19:52 +0100824static bool vmx_mpx_supported(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800825static bool vmx_xsaves_supported(void);
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +0200826static int vmx_cpu_uses_apicv(struct kvm_vcpu *vcpu);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200827static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300828static void vmx_set_segment(struct kvm_vcpu *vcpu,
829 struct kvm_segment *var, int seg);
830static void vmx_get_segment(struct kvm_vcpu *vcpu,
831 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200832static bool guest_state_valid(struct kvm_vcpu *vcpu);
833static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yang Zhanga20ed542013-04-11 19:25:15 +0800834static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu);
Abel Gordonc3114422013-04-18 14:38:55 +0300835static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300836static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +0800837static int alloc_identity_pagetable(struct kvm *kvm);
Avi Kivity75880a02007-06-20 11:20:04 +0300838
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839static DEFINE_PER_CPU(struct vmcs *, vmxarea);
840static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300841/*
842 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
843 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
844 */
845static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300846static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200848static unsigned long *vmx_io_bitmap_a;
849static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200850static unsigned long *vmx_msr_bitmap_legacy;
851static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800852static unsigned long *vmx_msr_bitmap_legacy_x2apic;
853static unsigned long *vmx_msr_bitmap_longmode_x2apic;
Wincy Van3af18d92015-02-03 23:49:31 +0800854static unsigned long *vmx_msr_bitmap_nested;
Abel Gordon4607c2d2013-04-18 14:35:55 +0300855static unsigned long *vmx_vmread_bitmap;
856static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300857
Avi Kivity110312c2010-12-21 12:54:20 +0200858static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200859static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200860
Sheng Yang2384d2b2008-01-17 15:14:33 +0800861static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
862static DEFINE_SPINLOCK(vmx_vpid_lock);
863
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300864static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 int size;
866 int order;
867 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300868 u32 pin_based_exec_ctrl;
869 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800870 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300871 u32 vmexit_ctrl;
872 u32 vmentry_ctrl;
873} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874
Hannes Ederefff9e52008-11-28 17:02:06 +0100875static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800876 u32 ept;
877 u32 vpid;
878} vmx_capability;
879
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880#define VMX_SEGMENT_FIELD(seg) \
881 [VCPU_SREG_##seg] = { \
882 .selector = GUEST_##seg##_SELECTOR, \
883 .base = GUEST_##seg##_BASE, \
884 .limit = GUEST_##seg##_LIMIT, \
885 .ar_bytes = GUEST_##seg##_AR_BYTES, \
886 }
887
Mathias Krause772e0312012-08-30 01:30:19 +0200888static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800889 unsigned selector;
890 unsigned base;
891 unsigned limit;
892 unsigned ar_bytes;
893} kvm_vmx_segment_fields[] = {
894 VMX_SEGMENT_FIELD(CS),
895 VMX_SEGMENT_FIELD(DS),
896 VMX_SEGMENT_FIELD(ES),
897 VMX_SEGMENT_FIELD(FS),
898 VMX_SEGMENT_FIELD(GS),
899 VMX_SEGMENT_FIELD(SS),
900 VMX_SEGMENT_FIELD(TR),
901 VMX_SEGMENT_FIELD(LDTR),
902};
903
Avi Kivity26bb0982009-09-07 11:14:12 +0300904static u64 host_efer;
905
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300906static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
907
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300908/*
Brian Gerst8c065852010-07-17 09:03:26 -0400909 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300910 * away by decrementing the array size.
911 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800913#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300914 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400916 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800917};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800918
Gui Jianfeng31299942010-03-15 17:29:09 +0800919static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800920{
921 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
922 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100923 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924}
925
Gui Jianfeng31299942010-03-15 17:29:09 +0800926static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300927{
928 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
929 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100930 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300931}
932
Gui Jianfeng31299942010-03-15 17:29:09 +0800933static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500934{
935 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
936 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100937 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500938}
939
Gui Jianfeng31299942010-03-15 17:29:09 +0800940static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800941{
942 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
943 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
944}
945
Gui Jianfeng31299942010-03-15 17:29:09 +0800946static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800947{
948 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
949 INTR_INFO_VALID_MASK)) ==
950 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
951}
952
Gui Jianfeng31299942010-03-15 17:29:09 +0800953static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800954{
Sheng Yang04547152009-04-01 15:52:31 +0800955 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800956}
957
Gui Jianfeng31299942010-03-15 17:29:09 +0800958static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800959{
Sheng Yang04547152009-04-01 15:52:31 +0800960 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800961}
962
Paolo Bonzini35754c92015-07-29 12:05:37 +0200963static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800964{
Paolo Bonzini35754c92015-07-29 12:05:37 +0200965 return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800966}
967
Gui Jianfeng31299942010-03-15 17:29:09 +0800968static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800969{
Sheng Yang04547152009-04-01 15:52:31 +0800970 return vmcs_config.cpu_based_exec_ctrl &
971 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800972}
973
Avi Kivity774ead32007-12-26 13:57:04 +0200974static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800975{
Sheng Yang04547152009-04-01 15:52:31 +0800976 return vmcs_config.cpu_based_2nd_exec_ctrl &
977 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
978}
979
Yang Zhang8d146952013-01-25 10:18:50 +0800980static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
981{
982 return vmcs_config.cpu_based_2nd_exec_ctrl &
983 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
984}
985
Yang Zhang83d4c282013-01-25 10:18:49 +0800986static inline bool cpu_has_vmx_apic_register_virt(void)
987{
988 return vmcs_config.cpu_based_2nd_exec_ctrl &
989 SECONDARY_EXEC_APIC_REGISTER_VIRT;
990}
991
Yang Zhangc7c9c562013-01-25 10:18:51 +0800992static inline bool cpu_has_vmx_virtual_intr_delivery(void)
993{
994 return vmcs_config.cpu_based_2nd_exec_ctrl &
995 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
996}
997
Yang Zhang01e439b2013-04-11 19:25:12 +0800998static inline bool cpu_has_vmx_posted_intr(void)
999{
Paolo Bonzinid6a858d2015-09-28 11:58:14 +02001000 return IS_ENABLED(CONFIG_X86_LOCAL_APIC) &&
1001 vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
Yang Zhang01e439b2013-04-11 19:25:12 +08001002}
1003
1004static inline bool cpu_has_vmx_apicv(void)
1005{
1006 return cpu_has_vmx_apic_register_virt() &&
1007 cpu_has_vmx_virtual_intr_delivery() &&
1008 cpu_has_vmx_posted_intr();
1009}
1010
Sheng Yang04547152009-04-01 15:52:31 +08001011static inline bool cpu_has_vmx_flexpriority(void)
1012{
1013 return cpu_has_vmx_tpr_shadow() &&
1014 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +08001015}
1016
Marcelo Tosattie7997942009-06-11 12:07:40 -03001017static inline bool cpu_has_vmx_ept_execute_only(void)
1018{
Gui Jianfeng31299942010-03-15 17:29:09 +08001019 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001020}
1021
Marcelo Tosattie7997942009-06-11 12:07:40 -03001022static inline bool cpu_has_vmx_ept_2m_page(void)
1023{
Gui Jianfeng31299942010-03-15 17:29:09 +08001024 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -03001025}
1026
Sheng Yang878403b2010-01-05 19:02:29 +08001027static inline bool cpu_has_vmx_ept_1g_page(void)
1028{
Gui Jianfeng31299942010-03-15 17:29:09 +08001029 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +08001030}
1031
Sheng Yang4bc9b982010-06-02 14:05:24 +08001032static inline bool cpu_has_vmx_ept_4levels(void)
1033{
1034 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
1035}
1036
Xudong Hao83c3a332012-05-28 19:33:35 +08001037static inline bool cpu_has_vmx_ept_ad_bits(void)
1038{
1039 return vmx_capability.ept & VMX_EPT_AD_BIT;
1040}
1041
Gui Jianfeng31299942010-03-15 17:29:09 +08001042static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001043{
Gui Jianfeng31299942010-03-15 17:29:09 +08001044 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001045}
1046
Gui Jianfeng31299942010-03-15 17:29:09 +08001047static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001048{
Gui Jianfeng31299942010-03-15 17:29:09 +08001049 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001050}
1051
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001052static inline bool cpu_has_vmx_invvpid_single(void)
1053{
1054 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1055}
1056
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001057static inline bool cpu_has_vmx_invvpid_global(void)
1058{
1059 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1060}
1061
Gui Jianfeng31299942010-03-15 17:29:09 +08001062static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001063{
Sheng Yang04547152009-04-01 15:52:31 +08001064 return vmcs_config.cpu_based_2nd_exec_ctrl &
1065 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001066}
1067
Gui Jianfeng31299942010-03-15 17:29:09 +08001068static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001069{
1070 return vmcs_config.cpu_based_2nd_exec_ctrl &
1071 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1072}
1073
Gui Jianfeng31299942010-03-15 17:29:09 +08001074static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001075{
1076 return vmcs_config.cpu_based_2nd_exec_ctrl &
1077 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1078}
1079
Paolo Bonzini35754c92015-07-29 12:05:37 +02001080static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001081{
Paolo Bonzini35754c92015-07-29 12:05:37 +02001082 return flexpriority_enabled && lapic_in_kernel(vcpu);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001083}
1084
Gui Jianfeng31299942010-03-15 17:29:09 +08001085static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001086{
Sheng Yang04547152009-04-01 15:52:31 +08001087 return vmcs_config.cpu_based_2nd_exec_ctrl &
1088 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001089}
1090
Gui Jianfeng31299942010-03-15 17:29:09 +08001091static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001092{
1093 return vmcs_config.cpu_based_2nd_exec_ctrl &
1094 SECONDARY_EXEC_RDTSCP;
1095}
1096
Mao, Junjiead756a12012-07-02 01:18:48 +00001097static inline bool cpu_has_vmx_invpcid(void)
1098{
1099 return vmcs_config.cpu_based_2nd_exec_ctrl &
1100 SECONDARY_EXEC_ENABLE_INVPCID;
1101}
1102
Gui Jianfeng31299942010-03-15 17:29:09 +08001103static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001104{
1105 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1106}
1107
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001108static inline bool cpu_has_vmx_wbinvd_exit(void)
1109{
1110 return vmcs_config.cpu_based_2nd_exec_ctrl &
1111 SECONDARY_EXEC_WBINVD_EXITING;
1112}
1113
Abel Gordonabc4fc52013-04-18 14:35:25 +03001114static inline bool cpu_has_vmx_shadow_vmcs(void)
1115{
1116 u64 vmx_msr;
1117 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1118 /* check if the cpu supports writing r/o exit information fields */
1119 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1120 return false;
1121
1122 return vmcs_config.cpu_based_2nd_exec_ctrl &
1123 SECONDARY_EXEC_SHADOW_VMCS;
1124}
1125
Kai Huang843e4332015-01-28 10:54:28 +08001126static inline bool cpu_has_vmx_pml(void)
1127{
1128 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1129}
1130
Sheng Yang04547152009-04-01 15:52:31 +08001131static inline bool report_flexpriority(void)
1132{
1133 return flexpriority_enabled;
1134}
1135
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001136static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1137{
1138 return vmcs12->cpu_based_vm_exec_control & bit;
1139}
1140
1141static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1142{
1143 return (vmcs12->cpu_based_vm_exec_control &
1144 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1145 (vmcs12->secondary_vm_exec_control & bit);
1146}
1147
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001148static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001149{
1150 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1151}
1152
Jan Kiszkaf41245002014-03-07 20:03:13 +01001153static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1154{
1155 return vmcs12->pin_based_vm_exec_control &
1156 PIN_BASED_VMX_PREEMPTION_TIMER;
1157}
1158
Nadav Har'El155a97a2013-08-05 11:07:16 +03001159static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1160{
1161 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1162}
1163
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001164static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1165{
1166 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1167 vmx_xsaves_supported();
1168}
1169
Wincy Vanf2b93282015-02-03 23:56:03 +08001170static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12)
1171{
1172 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
1173}
1174
Wincy Van82f0dd42015-02-03 23:57:18 +08001175static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12)
1176{
1177 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT);
1178}
1179
Wincy Van608406e2015-02-03 23:57:51 +08001180static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12)
1181{
1182 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
1183}
1184
Wincy Van705699a2015-02-03 23:58:17 +08001185static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12)
1186{
1187 return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR;
1188}
1189
Nadav Har'El644d7112011-05-25 23:12:35 +03001190static inline bool is_exception(u32 intr_info)
1191{
1192 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1193 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
1194}
1195
Jan Kiszka533558b2014-01-04 18:47:20 +01001196static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1197 u32 exit_intr_info,
1198 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001199static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1200 struct vmcs12 *vmcs12,
1201 u32 reason, unsigned long qualification);
1202
Rusty Russell8b9cf982007-07-30 16:31:43 +10001203static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001204{
1205 int i;
1206
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001207 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001208 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001209 return i;
1210 return -1;
1211}
1212
Sheng Yang2384d2b2008-01-17 15:14:33 +08001213static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1214{
1215 struct {
1216 u64 vpid : 16;
1217 u64 rsvd : 48;
1218 u64 gva;
1219 } operand = { vpid, 0, gva };
1220
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001221 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001222 /* CF==1 or ZF==1 --> rc = -1 */
1223 "; ja 1f ; ud2 ; 1:"
1224 : : "a"(&operand), "c"(ext) : "cc", "memory");
1225}
1226
Sheng Yang14394422008-04-28 12:24:45 +08001227static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1228{
1229 struct {
1230 u64 eptp, gpa;
1231 } operand = {eptp, gpa};
1232
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001233 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001234 /* CF==1 or ZF==1 --> rc = -1 */
1235 "; ja 1f ; ud2 ; 1:\n"
1236 : : "a" (&operand), "c" (ext) : "cc", "memory");
1237}
1238
Avi Kivity26bb0982009-09-07 11:14:12 +03001239static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001240{
1241 int i;
1242
Rusty Russell8b9cf982007-07-30 16:31:43 +10001243 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001244 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001245 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001246 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001247}
1248
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249static void vmcs_clear(struct vmcs *vmcs)
1250{
1251 u64 phys_addr = __pa(vmcs);
1252 u8 error;
1253
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001254 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001255 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256 : "cc", "memory");
1257 if (error)
1258 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1259 vmcs, phys_addr);
1260}
1261
Nadav Har'Eld462b812011-05-24 15:26:10 +03001262static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1263{
1264 vmcs_clear(loaded_vmcs->vmcs);
1265 loaded_vmcs->cpu = -1;
1266 loaded_vmcs->launched = 0;
1267}
1268
Dongxiao Xu7725b892010-05-11 18:29:38 +08001269static void vmcs_load(struct vmcs *vmcs)
1270{
1271 u64 phys_addr = __pa(vmcs);
1272 u8 error;
1273
1274 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001275 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001276 : "cc", "memory");
1277 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001278 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001279 vmcs, phys_addr);
1280}
1281
Dave Young2965faa2015-09-09 15:38:55 -07001282#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001283/*
1284 * This bitmap is used to indicate whether the vmclear
1285 * operation is enabled on all cpus. All disabled by
1286 * default.
1287 */
1288static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1289
1290static inline void crash_enable_local_vmclear(int cpu)
1291{
1292 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1293}
1294
1295static inline void crash_disable_local_vmclear(int cpu)
1296{
1297 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1298}
1299
1300static inline int crash_local_vmclear_enabled(int cpu)
1301{
1302 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1303}
1304
1305static void crash_vmclear_local_loaded_vmcss(void)
1306{
1307 int cpu = raw_smp_processor_id();
1308 struct loaded_vmcs *v;
1309
1310 if (!crash_local_vmclear_enabled(cpu))
1311 return;
1312
1313 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1314 loaded_vmcss_on_cpu_link)
1315 vmcs_clear(v->vmcs);
1316}
1317#else
1318static inline void crash_enable_local_vmclear(int cpu) { }
1319static inline void crash_disable_local_vmclear(int cpu) { }
Dave Young2965faa2015-09-09 15:38:55 -07001320#endif /* CONFIG_KEXEC_CORE */
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001321
Nadav Har'Eld462b812011-05-24 15:26:10 +03001322static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001324 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001325 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326
Nadav Har'Eld462b812011-05-24 15:26:10 +03001327 if (loaded_vmcs->cpu != cpu)
1328 return; /* vcpu migration can race with cpu offline */
1329 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001331 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001332 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001333
1334 /*
1335 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1336 * is before setting loaded_vmcs->vcpu to -1 which is done in
1337 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1338 * then adds the vmcs into percpu list before it is deleted.
1339 */
1340 smp_wmb();
1341
Nadav Har'Eld462b812011-05-24 15:26:10 +03001342 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001343 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344}
1345
Nadav Har'Eld462b812011-05-24 15:26:10 +03001346static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001347{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001348 int cpu = loaded_vmcs->cpu;
1349
1350 if (cpu != -1)
1351 smp_call_function_single(cpu,
1352 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001353}
1354
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001355static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001356{
1357 if (vmx->vpid == 0)
1358 return;
1359
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001360 if (cpu_has_vmx_invvpid_single())
1361 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001362}
1363
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001364static inline void vpid_sync_vcpu_global(void)
1365{
1366 if (cpu_has_vmx_invvpid_global())
1367 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1368}
1369
1370static inline void vpid_sync_context(struct vcpu_vmx *vmx)
1371{
1372 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001373 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001374 else
1375 vpid_sync_vcpu_global();
1376}
1377
Sheng Yang14394422008-04-28 12:24:45 +08001378static inline void ept_sync_global(void)
1379{
1380 if (cpu_has_vmx_invept_global())
1381 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1382}
1383
1384static inline void ept_sync_context(u64 eptp)
1385{
Avi Kivity089d0342009-03-23 18:26:32 +02001386 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001387 if (cpu_has_vmx_invept_context())
1388 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1389 else
1390 ept_sync_global();
1391 }
1392}
1393
Avi Kivity96304212011-05-15 10:13:13 -04001394static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395{
Avi Kivity5e520e62011-05-15 10:13:12 -04001396 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397
Avi Kivity5e520e62011-05-15 10:13:12 -04001398 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1399 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400 return value;
1401}
1402
Avi Kivity96304212011-05-15 10:13:13 -04001403static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001404{
1405 return vmcs_readl(field);
1406}
1407
Avi Kivity96304212011-05-15 10:13:13 -04001408static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409{
1410 return vmcs_readl(field);
1411}
1412
Avi Kivity96304212011-05-15 10:13:13 -04001413static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414{
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001415#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001416 return vmcs_readl(field);
1417#else
1418 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
1419#endif
1420}
1421
Avi Kivitye52de1b2007-01-05 16:36:56 -08001422static noinline void vmwrite_error(unsigned long field, unsigned long value)
1423{
1424 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1425 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1426 dump_stack();
1427}
1428
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429static void vmcs_writel(unsigned long field, unsigned long value)
1430{
1431 u8 error;
1432
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001433 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001434 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001435 if (unlikely(error))
1436 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001437}
1438
1439static void vmcs_write16(unsigned long field, u16 value)
1440{
1441 vmcs_writel(field, value);
1442}
1443
1444static void vmcs_write32(unsigned long field, u32 value)
1445{
1446 vmcs_writel(field, value);
1447}
1448
1449static void vmcs_write64(unsigned long field, u64 value)
1450{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001451 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001452#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453 asm volatile ("");
1454 vmcs_writel(field+1, value >> 32);
1455#endif
1456}
1457
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001458static void vmcs_clear_bits(unsigned long field, u32 mask)
1459{
1460 vmcs_writel(field, vmcs_readl(field) & ~mask);
1461}
1462
1463static void vmcs_set_bits(unsigned long field, u32 mask)
1464{
1465 vmcs_writel(field, vmcs_readl(field) | mask);
1466}
1467
Gleb Natapov2961e8762013-11-25 15:37:13 +02001468static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1469{
1470 vmcs_write32(VM_ENTRY_CONTROLS, val);
1471 vmx->vm_entry_controls_shadow = val;
1472}
1473
1474static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1475{
1476 if (vmx->vm_entry_controls_shadow != val)
1477 vm_entry_controls_init(vmx, val);
1478}
1479
1480static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1481{
1482 return vmx->vm_entry_controls_shadow;
1483}
1484
1485
1486static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1487{
1488 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1489}
1490
1491static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1492{
1493 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1494}
1495
1496static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1497{
1498 vmcs_write32(VM_EXIT_CONTROLS, val);
1499 vmx->vm_exit_controls_shadow = val;
1500}
1501
1502static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1503{
1504 if (vmx->vm_exit_controls_shadow != val)
1505 vm_exit_controls_init(vmx, val);
1506}
1507
1508static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1509{
1510 return vmx->vm_exit_controls_shadow;
1511}
1512
1513
1514static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1515{
1516 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1517}
1518
1519static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1520{
1521 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1522}
1523
Avi Kivity2fb92db2011-04-27 19:42:18 +03001524static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1525{
1526 vmx->segment_cache.bitmask = 0;
1527}
1528
1529static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1530 unsigned field)
1531{
1532 bool ret;
1533 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1534
1535 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1536 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1537 vmx->segment_cache.bitmask = 0;
1538 }
1539 ret = vmx->segment_cache.bitmask & mask;
1540 vmx->segment_cache.bitmask |= mask;
1541 return ret;
1542}
1543
1544static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1545{
1546 u16 *p = &vmx->segment_cache.seg[seg].selector;
1547
1548 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1549 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1550 return *p;
1551}
1552
1553static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1554{
1555 ulong *p = &vmx->segment_cache.seg[seg].base;
1556
1557 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1558 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1559 return *p;
1560}
1561
1562static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1563{
1564 u32 *p = &vmx->segment_cache.seg[seg].limit;
1565
1566 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1567 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1568 return *p;
1569}
1570
1571static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1572{
1573 u32 *p = &vmx->segment_cache.seg[seg].ar;
1574
1575 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1576 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1577 return *p;
1578}
1579
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001580static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1581{
1582 u32 eb;
1583
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001584 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1585 (1u << NM_VECTOR) | (1u << DB_VECTOR);
1586 if ((vcpu->guest_debug &
1587 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1588 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1589 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001590 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001591 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001592 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001593 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001594 if (vcpu->fpu_active)
1595 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001596
1597 /* When we are running a nested L2 guest and L1 specified for it a
1598 * certain exception bitmap, we must trap the same exceptions and pass
1599 * them to L1. When running L2, we will only handle the exceptions
1600 * specified above if L1 did not want them.
1601 */
1602 if (is_guest_mode(vcpu))
1603 eb |= get_vmcs12(vcpu)->exception_bitmap;
1604
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001605 vmcs_write32(EXCEPTION_BITMAP, eb);
1606}
1607
Gleb Natapov2961e8762013-11-25 15:37:13 +02001608static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1609 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001610{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001611 vm_entry_controls_clearbit(vmx, entry);
1612 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001613}
1614
Avi Kivity61d2ef22010-04-28 16:40:38 +03001615static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1616{
1617 unsigned i;
1618 struct msr_autoload *m = &vmx->msr_autoload;
1619
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001620 switch (msr) {
1621 case MSR_EFER:
1622 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001623 clear_atomic_switch_msr_special(vmx,
1624 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001625 VM_EXIT_LOAD_IA32_EFER);
1626 return;
1627 }
1628 break;
1629 case MSR_CORE_PERF_GLOBAL_CTRL:
1630 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001631 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001632 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1633 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1634 return;
1635 }
1636 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001637 }
1638
Avi Kivity61d2ef22010-04-28 16:40:38 +03001639 for (i = 0; i < m->nr; ++i)
1640 if (m->guest[i].index == msr)
1641 break;
1642
1643 if (i == m->nr)
1644 return;
1645 --m->nr;
1646 m->guest[i] = m->guest[m->nr];
1647 m->host[i] = m->host[m->nr];
1648 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1649 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1650}
1651
Gleb Natapov2961e8762013-11-25 15:37:13 +02001652static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1653 unsigned long entry, unsigned long exit,
1654 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1655 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001656{
1657 vmcs_write64(guest_val_vmcs, guest_val);
1658 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001659 vm_entry_controls_setbit(vmx, entry);
1660 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001661}
1662
Avi Kivity61d2ef22010-04-28 16:40:38 +03001663static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1664 u64 guest_val, u64 host_val)
1665{
1666 unsigned i;
1667 struct msr_autoload *m = &vmx->msr_autoload;
1668
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001669 switch (msr) {
1670 case MSR_EFER:
1671 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001672 add_atomic_switch_msr_special(vmx,
1673 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001674 VM_EXIT_LOAD_IA32_EFER,
1675 GUEST_IA32_EFER,
1676 HOST_IA32_EFER,
1677 guest_val, host_val);
1678 return;
1679 }
1680 break;
1681 case MSR_CORE_PERF_GLOBAL_CTRL:
1682 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001683 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001684 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1685 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1686 GUEST_IA32_PERF_GLOBAL_CTRL,
1687 HOST_IA32_PERF_GLOBAL_CTRL,
1688 guest_val, host_val);
1689 return;
1690 }
1691 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001692 }
1693
Avi Kivity61d2ef22010-04-28 16:40:38 +03001694 for (i = 0; i < m->nr; ++i)
1695 if (m->guest[i].index == msr)
1696 break;
1697
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001698 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001699 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001700 "Can't add msr %x\n", msr);
1701 return;
1702 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001703 ++m->nr;
1704 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1705 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1706 }
1707
1708 m->guest[i].index = msr;
1709 m->guest[i].value = guest_val;
1710 m->host[i].index = msr;
1711 m->host[i].value = host_val;
1712}
1713
Avi Kivity33ed6322007-05-02 16:54:03 +03001714static void reload_tss(void)
1715{
Avi Kivity33ed6322007-05-02 16:54:03 +03001716 /*
1717 * VT restores TR but not its size. Useless.
1718 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05001719 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001720 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001721
Avi Kivityd3591922010-07-26 18:32:39 +03001722 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001723 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1724 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001725}
1726
Avi Kivity92c0d902009-10-29 11:00:16 +02001727static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001728{
Roel Kluin3a34a882009-08-04 02:08:45 -07001729 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001730 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +03001731
Avi Kivityf6801df2010-01-21 15:31:50 +02001732 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -07001733
Avi Kivity51c6cf62007-08-29 03:48:05 +03001734 /*
Guo Chao0fa06072012-06-28 15:16:19 +08001735 * NX is emulated; LMA and LME handled by hardware; SCE meaningless
Avi Kivity51c6cf62007-08-29 03:48:05 +03001736 * outside long mode
1737 */
1738 ignore_bits = EFER_NX | EFER_SCE;
1739#ifdef CONFIG_X86_64
1740 ignore_bits |= EFER_LMA | EFER_LME;
1741 /* SCE is meaningful only in long mode on Intel */
1742 if (guest_efer & EFER_LMA)
1743 ignore_bits &= ~(u64)EFER_SCE;
1744#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +03001745 guest_efer &= ~ignore_bits;
1746 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +03001747 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +02001748 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03001749
1750 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001751
1752 /*
1753 * On EPT, we can't emulate NX, so we must switch EFER atomically.
1754 * On CPUs that support "load IA32_EFER", always switch EFER
1755 * atomically, since it's faster than switching it manually.
1756 */
1757 if (cpu_has_load_ia32_efer ||
1758 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03001759 guest_efer = vmx->vcpu.arch.efer;
1760 if (!(guest_efer & EFER_LMA))
1761 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08001762 if (guest_efer != host_efer)
1763 add_atomic_switch_msr(vmx, MSR_EFER,
1764 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03001765 return false;
1766 }
1767
Avi Kivity26bb0982009-09-07 11:14:12 +03001768 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001769}
1770
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001771static unsigned long segment_base(u16 selector)
1772{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001773 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001774 struct desc_struct *d;
1775 unsigned long table_base;
1776 unsigned long v;
1777
1778 if (!(selector & ~3))
1779 return 0;
1780
Avi Kivityd3591922010-07-26 18:32:39 +03001781 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001782
1783 if (selector & 4) { /* from ldt */
1784 u16 ldt_selector = kvm_read_ldt();
1785
1786 if (!(ldt_selector & ~3))
1787 return 0;
1788
1789 table_base = segment_base(ldt_selector);
1790 }
1791 d = (struct desc_struct *)(table_base + (selector & ~7));
1792 v = get_desc_base(d);
1793#ifdef CONFIG_X86_64
1794 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
1795 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
1796#endif
1797 return v;
1798}
1799
1800static inline unsigned long kvm_read_tr_base(void)
1801{
1802 u16 tr;
1803 asm("str %0" : "=g"(tr));
1804 return segment_base(tr);
1805}
1806
Avi Kivity04d2cc72007-09-10 18:10:54 +03001807static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001808{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001809 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001810 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001811
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001812 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001813 return;
1814
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001815 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001816 /*
1817 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1818 * allow segment selectors with cpl > 0 or ti == 1.
1819 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001820 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02001821 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02001822 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001823 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001824 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001825 vmx->host_state.fs_reload_needed = 0;
1826 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03001827 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001828 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001829 }
Avi Kivity9581d442010-10-19 16:46:55 +02001830 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001831 if (!(vmx->host_state.gs_sel & 7))
1832 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001833 else {
1834 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001835 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001836 }
1837
1838#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03001839 savesegment(ds, vmx->host_state.ds_sel);
1840 savesegment(es, vmx->host_state.es_sel);
1841#endif
1842
1843#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03001844 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1845 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1846#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001847 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1848 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03001849#endif
Avi Kivity707c0872007-05-02 17:33:43 +03001850
1851#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001852 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1853 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03001854 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03001855#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001856 if (boot_cpu_has(X86_FEATURE_MPX))
1857 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03001858 for (i = 0; i < vmx->save_nmsrs; ++i)
1859 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02001860 vmx->guest_msrs[i].data,
1861 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03001862}
1863
Avi Kivitya9b21b62008-06-24 11:48:49 +03001864static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001865{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001866 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001867 return;
1868
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001869 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001870 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02001871#ifdef CONFIG_X86_64
1872 if (is_long_mode(&vmx->vcpu))
1873 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1874#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001875 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001876 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001877#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02001878 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001879#else
1880 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001881#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001882 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02001883 if (vmx->host_state.fs_reload_needed)
1884 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001885#ifdef CONFIG_X86_64
1886 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
1887 loadsegment(ds, vmx->host_state.ds_sel);
1888 loadsegment(es, vmx->host_state.es_sel);
1889 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001890#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001891 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001892#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001893 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001894#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001895 if (vmx->host_state.msr_host_bndcfgs)
1896 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07001897 /*
1898 * If the FPU is not active (through the host task or
1899 * the guest vcpu), then restore the cr0.TS bit.
1900 */
Ingo Molnar3c6dffa2015-04-28 12:28:08 +02001901 if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded)
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07001902 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05001903 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03001904}
1905
Avi Kivitya9b21b62008-06-24 11:48:49 +03001906static void vmx_load_host_state(struct vcpu_vmx *vmx)
1907{
1908 preempt_disable();
1909 __vmx_load_host_state(vmx);
1910 preempt_enable();
1911}
1912
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913/*
1914 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1915 * vcpu mutex is already taken.
1916 */
Avi Kivity15ad7142007-07-11 18:17:21 +03001917static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001918{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001919 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001920 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001921
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001922 if (!vmm_exclusive)
1923 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001924 else if (vmx->loaded_vmcs->cpu != cpu)
1925 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926
Nadav Har'Eld462b812011-05-24 15:26:10 +03001927 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1928 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1929 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930 }
1931
Nadav Har'Eld462b812011-05-24 15:26:10 +03001932 if (vmx->loaded_vmcs->cpu != cpu) {
Christoph Lameter89cbc762014-08-17 12:30:40 -05001933 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001934 unsigned long sysenter_esp;
1935
Avi Kivitya8eeb042010-05-10 12:34:53 +03001936 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001937 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001938 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001939
1940 /*
1941 * Read loaded_vmcs->cpu should be before fetching
1942 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1943 * See the comments in __loaded_vmcs_clear().
1944 */
1945 smp_rmb();
1946
Nadav Har'Eld462b812011-05-24 15:26:10 +03001947 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1948 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001949 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001950 local_irq_enable();
1951
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952 /*
1953 * Linux uses per-cpu TSS and GDT, so set these when switching
1954 * processors.
1955 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001956 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03001957 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001958
1959 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1960 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Nadav Har'Eld462b812011-05-24 15:26:10 +03001961 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001962 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963}
1964
1965static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1966{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001967 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001968 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001969 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1970 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001971 kvm_cpu_vmxoff();
1972 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973}
1974
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001975static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1976{
Avi Kivity81231c62010-01-24 16:26:40 +02001977 ulong cr0;
1978
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001979 if (vcpu->fpu_active)
1980 return;
1981 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001982 cr0 = vmcs_readl(GUEST_CR0);
1983 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1984 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1985 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001986 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001987 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001988 if (is_guest_mode(vcpu))
1989 vcpu->arch.cr0_guest_owned_bits &=
1990 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02001991 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001992}
1993
Avi Kivityedcafe32009-12-30 18:07:40 +02001994static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1995
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001996/*
1997 * Return the cr0 value that a nested guest would read. This is a combination
1998 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1999 * its hypervisor (cr0_read_shadow).
2000 */
2001static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
2002{
2003 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
2004 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
2005}
2006static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
2007{
2008 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
2009 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
2010}
2011
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002012static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
2013{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002014 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
2015 * set this *before* calling this function.
2016 */
Avi Kivityedcafe32009-12-30 18:07:40 +02002017 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02002018 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002019 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02002020 vcpu->arch.cr0_guest_owned_bits = 0;
2021 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03002022 if (is_guest_mode(vcpu)) {
2023 /*
2024 * L1's specified read shadow might not contain the TS bit,
2025 * so now that we turned on shadowing of this bit, we need to
2026 * set this bit of the shadow. Like in nested_vmx_run we need
2027 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
2028 * up-to-date here because we just decached cr0.TS (and we'll
2029 * only update vmcs12->guest_cr0 on nested exit).
2030 */
2031 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2032 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
2033 (vcpu->arch.cr0 & X86_CR0_TS);
2034 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
2035 } else
2036 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002037}
2038
Avi Kivity6aa8b732006-12-10 02:21:36 -08002039static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2040{
Avi Kivity78ac8b42010-04-08 18:19:35 +03002041 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03002042
Avi Kivity6de12732011-03-07 12:51:22 +02002043 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
2044 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2045 rflags = vmcs_readl(GUEST_RFLAGS);
2046 if (to_vmx(vcpu)->rmode.vm86_active) {
2047 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2048 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
2049 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2050 }
2051 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002052 }
Avi Kivity6de12732011-03-07 12:51:22 +02002053 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054}
2055
2056static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2057{
Avi Kivity6de12732011-03-07 12:51:22 +02002058 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
2059 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002060 if (to_vmx(vcpu)->rmode.vm86_active) {
2061 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002062 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03002063 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002064 vmcs_writel(GUEST_RFLAGS, rflags);
2065}
2066
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002067static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002068{
2069 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2070 int ret = 0;
2071
2072 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002073 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002074 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002075 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002076
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002077 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002078}
2079
2080static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2081{
2082 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2083 u32 interruptibility = interruptibility_old;
2084
2085 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2086
Jan Kiszka48005f62010-02-19 19:38:07 +01002087 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002088 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002089 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002090 interruptibility |= GUEST_INTR_STATE_STI;
2091
2092 if ((interruptibility != interruptibility_old))
2093 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2094}
2095
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2097{
2098 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002099
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002100 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002101 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002102 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002103
Glauber Costa2809f5d2009-05-12 16:21:05 -04002104 /* skipping an emulated instruction also counts */
2105 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002106}
2107
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002108/*
2109 * KVM wants to inject page-faults which it got to the guest. This function
2110 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002111 */
Gleb Natapove011c662013-09-25 12:51:35 +03002112static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002113{
2114 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2115
Gleb Natapove011c662013-09-25 12:51:35 +03002116 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002117 return 0;
2118
Jan Kiszka533558b2014-01-04 18:47:20 +01002119 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
2120 vmcs_read32(VM_EXIT_INTR_INFO),
2121 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002122 return 1;
2123}
2124
Avi Kivity298101d2007-11-25 13:41:11 +02002125static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002126 bool has_error_code, u32 error_code,
2127 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002128{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002129 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002130 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002131
Gleb Natapove011c662013-09-25 12:51:35 +03002132 if (!reinject && is_guest_mode(vcpu) &&
2133 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002134 return;
2135
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002136 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002137 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002138 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2139 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002140
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002141 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002142 int inc_eip = 0;
2143 if (kvm_exception_is_soft(nr))
2144 inc_eip = vcpu->arch.event_exit_inst_len;
2145 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002146 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002147 return;
2148 }
2149
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002150 if (kvm_exception_is_soft(nr)) {
2151 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2152 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002153 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2154 } else
2155 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2156
2157 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002158}
2159
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002160static bool vmx_rdtscp_supported(void)
2161{
2162 return cpu_has_vmx_rdtscp();
2163}
2164
Mao, Junjiead756a12012-07-02 01:18:48 +00002165static bool vmx_invpcid_supported(void)
2166{
2167 return cpu_has_vmx_invpcid() && enable_ept;
2168}
2169
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170/*
Eddie Donga75beee2007-05-17 18:55:15 +03002171 * Swap MSR entry in host/guest MSR entry array.
2172 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002173static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002174{
Avi Kivity26bb0982009-09-07 11:14:12 +03002175 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002176
2177 tmp = vmx->guest_msrs[to];
2178 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2179 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002180}
2181
Yang Zhang8d146952013-01-25 10:18:50 +08002182static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2183{
2184 unsigned long *msr_bitmap;
2185
Wincy Van670125b2015-03-04 14:31:56 +08002186 if (is_guest_mode(vcpu))
2187 msr_bitmap = vmx_msr_bitmap_nested;
Jan Kiszka8a9781f2015-05-04 08:32:32 +02002188 else if (vcpu->arch.apic_base & X2APIC_ENABLE) {
Yang Zhang8d146952013-01-25 10:18:50 +08002189 if (is_long_mode(vcpu))
2190 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2191 else
2192 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
2193 } else {
2194 if (is_long_mode(vcpu))
2195 msr_bitmap = vmx_msr_bitmap_longmode;
2196 else
2197 msr_bitmap = vmx_msr_bitmap_legacy;
2198 }
2199
2200 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2201}
2202
Eddie Donga75beee2007-05-17 18:55:15 +03002203/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002204 * Set up the vmcs to automatically save and restore system
2205 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2206 * mode, as fiddling with msrs is very expensive.
2207 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002208static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002209{
Avi Kivity26bb0982009-09-07 11:14:12 +03002210 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002211
Eddie Donga75beee2007-05-17 18:55:15 +03002212 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002213#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002214 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002215 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002216 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002217 move_msr_up(vmx, index, save_nmsrs++);
2218 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002219 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002220 move_msr_up(vmx, index, save_nmsrs++);
2221 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002222 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002223 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002224 index = __find_msr_index(vmx, MSR_TSC_AUX);
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002225 if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002226 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002227 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002228 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002229 * if efer.sce is enabled.
2230 */
Brian Gerst8c065852010-07-17 09:03:26 -04002231 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002232 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002233 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002234 }
Eddie Donga75beee2007-05-17 18:55:15 +03002235#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002236 index = __find_msr_index(vmx, MSR_EFER);
2237 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002238 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002239
Avi Kivity26bb0982009-09-07 11:14:12 +03002240 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002241
Yang Zhang8d146952013-01-25 10:18:50 +08002242 if (cpu_has_vmx_msr_bitmap())
2243 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002244}
2245
2246/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247 * reads and returns guest's timestamp counter "register"
2248 * guest_tsc = host_tsc + tsc_offset -- 21.3
2249 */
2250static u64 guest_read_tsc(void)
2251{
2252 u64 host_tsc, tsc_offset;
2253
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002254 host_tsc = rdtsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255 tsc_offset = vmcs_read64(TSC_OFFSET);
2256 return host_tsc + tsc_offset;
2257}
2258
2259/*
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002260 * Like guest_read_tsc, but always returns L1's notion of the timestamp
2261 * counter, even if a nested guest (L2) is currently running.
2262 */
Paolo Bonzini48d89b92014-08-26 13:27:46 +02002263static u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002264{
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002265 u64 tsc_offset;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002266
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002267 tsc_offset = is_guest_mode(vcpu) ?
2268 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
2269 vmcs_read64(TSC_OFFSET);
2270 return host_tsc + tsc_offset;
2271}
2272
2273/*
Zachary Amsdencc578282012-02-03 15:43:50 -02002274 * Engage any workarounds for mis-matched TSC rates. Currently limited to
2275 * software catchup for faster rates on slower CPUs.
Joerg Roedel4051b182011-03-25 09:44:49 +01002276 */
Zachary Amsdencc578282012-02-03 15:43:50 -02002277static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +01002278{
Zachary Amsdencc578282012-02-03 15:43:50 -02002279 if (!scale)
2280 return;
2281
2282 if (user_tsc_khz > tsc_khz) {
2283 vcpu->arch.tsc_catchup = 1;
2284 vcpu->arch.tsc_always_catchup = 1;
2285 } else
2286 WARN(1, "user requested TSC rate below hardware speed\n");
Joerg Roedel4051b182011-03-25 09:44:49 +01002287}
2288
Will Auldba904632012-11-29 12:42:50 -08002289static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
2290{
2291 return vmcs_read64(TSC_OFFSET);
2292}
2293
Joerg Roedel4051b182011-03-25 09:44:49 +01002294/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002295 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002296 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002297static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002299 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002300 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002301 * We're here if L1 chose not to trap WRMSR to TSC. According
2302 * to the spec, this should set L1's TSC; The offset that L1
2303 * set for L2 remains unchanged, and still needs to be added
2304 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002305 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002306 struct vmcs12 *vmcs12;
2307 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
2308 /* recalculate vmcs02.TSC_OFFSET: */
2309 vmcs12 = get_vmcs12(vcpu);
2310 vmcs_write64(TSC_OFFSET, offset +
2311 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2312 vmcs12->tsc_offset : 0));
2313 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002314 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2315 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002316 vmcs_write64(TSC_OFFSET, offset);
2317 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318}
2319
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002320static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10002321{
2322 u64 offset = vmcs_read64(TSC_OFFSET);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002323
Zachary Amsdene48672f2010-08-19 22:07:23 -10002324 vmcs_write64(TSC_OFFSET, offset + adjustment);
Nadav Har'El79918252011-05-25 23:15:39 +03002325 if (is_guest_mode(vcpu)) {
2326 /* Even when running L2, the adjustment needs to apply to L1 */
2327 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002328 } else
2329 trace_kvm_write_tsc_offset(vcpu->vcpu_id, offset,
2330 offset + adjustment);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002331}
2332
Joerg Roedel857e4092011-03-25 09:44:50 +01002333static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
2334{
Andy Lutomirski4ea16362015-06-25 18:44:07 +02002335 return target_tsc - rdtsc();
Joerg Roedel857e4092011-03-25 09:44:50 +01002336}
2337
Nadav Har'El801d3422011-05-25 23:02:23 +03002338static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2339{
2340 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2341 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2342}
2343
2344/*
2345 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2346 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2347 * all guests if the "nested" module option is off, and can also be disabled
2348 * for a single guest by disabling its VMX cpuid bit.
2349 */
2350static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2351{
2352 return nested && guest_cpuid_has_vmx(vcpu);
2353}
2354
Avi Kivity6aa8b732006-12-10 02:21:36 -08002355/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002356 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2357 * returned for the various VMX controls MSRs when nested VMX is enabled.
2358 * The same values should also be used to verify that vmcs12 control fields are
2359 * valid during nested entry from L1 to L2.
2360 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2361 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2362 * bit in the high half is on if the corresponding bit in the control field
2363 * may be on. See also vmx_control_verify().
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002364 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002365static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx)
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002366{
2367 /*
2368 * Note that as a general rule, the high half of the MSRs (bits in
2369 * the control fields which may be 1) should be initialized by the
2370 * intersection of the underlying hardware's MSR (i.e., features which
2371 * can be supported) and the list of features we want to expose -
2372 * because they are known to be properly supported in our code.
2373 * Also, usually, the low half of the MSRs (bits which must be 1) can
2374 * be set to 0, meaning that L1 may turn off any of these bits. The
2375 * reason is that if one of these bits is necessary, it will appear
2376 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2377 * fields of vmcs01 and vmcs02, will turn these bits off - and
2378 * nested_vmx_exit_handled() will not pass related exits to L1.
2379 * These rules have exceptions below.
2380 */
2381
2382 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002383 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002384 vmx->nested.nested_vmx_pinbased_ctls_low,
2385 vmx->nested.nested_vmx_pinbased_ctls_high);
2386 vmx->nested.nested_vmx_pinbased_ctls_low |=
2387 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2388 vmx->nested.nested_vmx_pinbased_ctls_high &=
2389 PIN_BASED_EXT_INTR_MASK |
2390 PIN_BASED_NMI_EXITING |
2391 PIN_BASED_VIRTUAL_NMIS;
2392 vmx->nested.nested_vmx_pinbased_ctls_high |=
2393 PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002394 PIN_BASED_VMX_PREEMPTION_TIMER;
Paolo Bonzini35754c92015-07-29 12:05:37 +02002395 if (vmx_cpu_uses_apicv(&vmx->vcpu))
Wincy Van705699a2015-02-03 23:58:17 +08002396 vmx->nested.nested_vmx_pinbased_ctls_high |=
2397 PIN_BASED_POSTED_INTR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002398
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002399 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002400 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002401 vmx->nested.nested_vmx_exit_ctls_low,
2402 vmx->nested.nested_vmx_exit_ctls_high);
2403 vmx->nested.nested_vmx_exit_ctls_low =
2404 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002405
Wincy Vanb9c237b2015-02-03 23:56:30 +08002406 vmx->nested.nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002407#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002408 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002409#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002410 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002411 vmx->nested.nested_vmx_exit_ctls_high |=
2412 VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002413 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002414 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2415
Paolo Bonzini36be0b92014-02-24 12:30:04 +01002416 if (vmx_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002417 vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002418
Jan Kiszka2996fca2014-06-16 13:59:43 +02002419 /* We support free control of debug control saving. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002420 vmx->nested.nested_vmx_true_exit_ctls_low =
2421 vmx->nested.nested_vmx_exit_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002422 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2423
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002424 /* entry controls */
2425 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002426 vmx->nested.nested_vmx_entry_ctls_low,
2427 vmx->nested.nested_vmx_entry_ctls_high);
2428 vmx->nested.nested_vmx_entry_ctls_low =
2429 VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
2430 vmx->nested.nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002431#ifdef CONFIG_X86_64
2432 VM_ENTRY_IA32E_MODE |
2433#endif
2434 VM_ENTRY_LOAD_IA32_PAT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002435 vmx->nested.nested_vmx_entry_ctls_high |=
2436 (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzini36be0b92014-02-24 12:30:04 +01002437 if (vmx_mpx_supported())
Wincy Vanb9c237b2015-02-03 23:56:30 +08002438 vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002439
Jan Kiszka2996fca2014-06-16 13:59:43 +02002440 /* We support free control of debug control loading. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002441 vmx->nested.nested_vmx_true_entry_ctls_low =
2442 vmx->nested.nested_vmx_entry_ctls_low &
Jan Kiszka2996fca2014-06-16 13:59:43 +02002443 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2444
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002445 /* cpu-based controls */
2446 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002447 vmx->nested.nested_vmx_procbased_ctls_low,
2448 vmx->nested.nested_vmx_procbased_ctls_high);
2449 vmx->nested.nested_vmx_procbased_ctls_low =
2450 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2451 vmx->nested.nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002452 CPU_BASED_VIRTUAL_INTR_PENDING |
2453 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002454 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2455 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2456 CPU_BASED_CR3_STORE_EXITING |
2457#ifdef CONFIG_X86_64
2458 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2459#endif
2460 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03002461 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG |
2462 CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING |
2463 CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING |
2464 CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002465 /*
2466 * We can allow some features even when not supported by the
2467 * hardware. For example, L1 can specify an MSR bitmap - and we
2468 * can use it to avoid exits to L1 - even when L0 runs L2
2469 * without MSR bitmaps.
2470 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002471 vmx->nested.nested_vmx_procbased_ctls_high |=
2472 CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002473 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002474
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002475 /* We support free control of CR3 access interception. */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002476 vmx->nested.nested_vmx_true_procbased_ctls_low =
2477 vmx->nested.nested_vmx_procbased_ctls_low &
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002478 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2479
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002480 /* secondary cpu-based controls */
2481 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
Wincy Vanb9c237b2015-02-03 23:56:30 +08002482 vmx->nested.nested_vmx_secondary_ctls_low,
2483 vmx->nested.nested_vmx_secondary_ctls_high);
2484 vmx->nested.nested_vmx_secondary_ctls_low = 0;
2485 vmx->nested.nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002486 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01002487 SECONDARY_EXEC_RDTSCP |
Wincy Vanf2b93282015-02-03 23:56:03 +08002488 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Wincy Van82f0dd42015-02-03 23:57:18 +08002489 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Wincy Van608406e2015-02-03 23:57:51 +08002490 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002491 SECONDARY_EXEC_WBINVD_EXITING |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08002492 SECONDARY_EXEC_XSAVES |
2493 SECONDARY_EXEC_PCOMMIT;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002494
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002495 if (enable_ept) {
2496 /* nested EPT: emulate EPT also to L1 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002497 vmx->nested.nested_vmx_secondary_ctls_high |=
Radim Krčmář0790ec12015-03-17 14:02:32 +01002498 SECONDARY_EXEC_ENABLE_EPT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002499 vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002500 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2501 VMX_EPT_INVEPT_BIT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002502 vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002503 /*
Bandan Das4b855072014-04-19 18:17:44 -04002504 * For nested guests, we don't do anything specific
2505 * for single context invalidation. Hence, only advertise
2506 * support for global context invalidation.
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002507 */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002508 vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002509 } else
Wincy Vanb9c237b2015-02-03 23:56:30 +08002510 vmx->nested.nested_vmx_ept_caps = 0;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002511
Radim Krčmář0790ec12015-03-17 14:02:32 +01002512 if (enable_unrestricted_guest)
2513 vmx->nested.nested_vmx_secondary_ctls_high |=
2514 SECONDARY_EXEC_UNRESTRICTED_GUEST;
2515
Jan Kiszkac18911a2013-03-13 16:06:41 +01002516 /* miscellaneous data */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002517 rdmsr(MSR_IA32_VMX_MISC,
2518 vmx->nested.nested_vmx_misc_low,
2519 vmx->nested.nested_vmx_misc_high);
2520 vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2521 vmx->nested.nested_vmx_misc_low |=
2522 VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002523 VMX_MISC_ACTIVITY_HLT;
Wincy Vanb9c237b2015-02-03 23:56:30 +08002524 vmx->nested.nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002525}
2526
2527static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2528{
2529 /*
2530 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2531 */
2532 return ((control & high) | low) == control;
2533}
2534
2535static inline u64 vmx_control_msr(u32 low, u32 high)
2536{
2537 return low | ((u64)high << 32);
2538}
2539
Jan Kiszkacae50132014-01-04 18:47:22 +01002540/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002541static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2542{
Wincy Vanb9c237b2015-02-03 23:56:30 +08002543 struct vcpu_vmx *vmx = to_vmx(vcpu);
2544
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002545 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002546 case MSR_IA32_VMX_BASIC:
2547 /*
2548 * This MSR reports some information about VMX support. We
2549 * should return information about the VMX we emulate for the
2550 * guest, and the VMCS structure we give it - not about the
2551 * VMX support of the underlying hardware.
2552 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002553 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002554 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2555 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2556 break;
2557 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2558 case MSR_IA32_VMX_PINBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002559 *pdata = vmx_control_msr(
2560 vmx->nested.nested_vmx_pinbased_ctls_low,
2561 vmx->nested.nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002562 break;
2563 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002564 *pdata = vmx_control_msr(
2565 vmx->nested.nested_vmx_true_procbased_ctls_low,
2566 vmx->nested.nested_vmx_procbased_ctls_high);
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002567 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002568 case MSR_IA32_VMX_PROCBASED_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002569 *pdata = vmx_control_msr(
2570 vmx->nested.nested_vmx_procbased_ctls_low,
2571 vmx->nested.nested_vmx_procbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002572 break;
2573 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002574 *pdata = vmx_control_msr(
2575 vmx->nested.nested_vmx_true_exit_ctls_low,
2576 vmx->nested.nested_vmx_exit_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002577 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002578 case MSR_IA32_VMX_EXIT_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002579 *pdata = vmx_control_msr(
2580 vmx->nested.nested_vmx_exit_ctls_low,
2581 vmx->nested.nested_vmx_exit_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002582 break;
2583 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002584 *pdata = vmx_control_msr(
2585 vmx->nested.nested_vmx_true_entry_ctls_low,
2586 vmx->nested.nested_vmx_entry_ctls_high);
Jan Kiszka2996fca2014-06-16 13:59:43 +02002587 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002588 case MSR_IA32_VMX_ENTRY_CTLS:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002589 *pdata = vmx_control_msr(
2590 vmx->nested.nested_vmx_entry_ctls_low,
2591 vmx->nested.nested_vmx_entry_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002592 break;
2593 case MSR_IA32_VMX_MISC:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002594 *pdata = vmx_control_msr(
2595 vmx->nested.nested_vmx_misc_low,
2596 vmx->nested.nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002597 break;
2598 /*
2599 * These MSRs specify bits which the guest must keep fixed (on or off)
2600 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2601 * We picked the standard core2 setting.
2602 */
2603#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2604#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2605 case MSR_IA32_VMX_CR0_FIXED0:
2606 *pdata = VMXON_CR0_ALWAYSON;
2607 break;
2608 case MSR_IA32_VMX_CR0_FIXED1:
2609 *pdata = -1ULL;
2610 break;
2611 case MSR_IA32_VMX_CR4_FIXED0:
2612 *pdata = VMXON_CR4_ALWAYSON;
2613 break;
2614 case MSR_IA32_VMX_CR4_FIXED1:
2615 *pdata = -1ULL;
2616 break;
2617 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02002618 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002619 break;
2620 case MSR_IA32_VMX_PROCBASED_CTLS2:
Wincy Vanb9c237b2015-02-03 23:56:30 +08002621 *pdata = vmx_control_msr(
2622 vmx->nested.nested_vmx_secondary_ctls_low,
2623 vmx->nested.nested_vmx_secondary_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002624 break;
2625 case MSR_IA32_VMX_EPT_VPID_CAP:
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002626 /* Currently, no nested vpid support */
Wincy Vanb9c237b2015-02-03 23:56:30 +08002627 *pdata = vmx->nested.nested_vmx_ept_caps;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002628 break;
2629 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002630 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002631 }
2632
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002633 return 0;
2634}
2635
2636/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 * Reads an msr value (of 'msr_index') into 'pdata'.
2638 * Returns 0 on success, non-0 otherwise.
2639 * Assumes vcpu_load() was already called.
2640 */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002641static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642{
Avi Kivity26bb0982009-09-07 11:14:12 +03002643 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002645 switch (msr_info->index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002646#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647 case MSR_FS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002648 msr_info->data = vmcs_readl(GUEST_FS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002649 break;
2650 case MSR_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002651 msr_info->data = vmcs_readl(GUEST_GS_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002653 case MSR_KERNEL_GS_BASE:
2654 vmx_load_host_state(to_vmx(vcpu));
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002655 msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002656 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002657#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658 case MSR_EFER:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002659 return kvm_get_msr_common(vcpu, msr_info);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302660 case MSR_IA32_TSC:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002661 msr_info->data = guest_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 break;
2663 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002664 msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665 break;
2666 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002667 msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668 break;
2669 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002670 msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002672 case MSR_IA32_BNDCFGS:
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002673 if (!vmx_mpx_supported())
2674 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002675 msr_info->data = vmcs_read64(GUEST_BNDCFGS);
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002676 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002677 case MSR_IA32_FEATURE_CONTROL:
2678 if (!nested_vmx_allowed(vcpu))
2679 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002680 msr_info->data = to_vmx(vcpu)->nested.msr_ia32_feature_control;
Jan Kiszkacae50132014-01-04 18:47:22 +01002681 break;
2682 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2683 if (!nested_vmx_allowed(vcpu))
2684 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002685 return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data);
Wanpeng Li20300092014-12-02 19:14:59 +08002686 case MSR_IA32_XSS:
2687 if (!vmx_xsaves_supported())
2688 return 1;
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002689 msr_info->data = vcpu->arch.ia32_xss;
Wanpeng Li20300092014-12-02 19:14:59 +08002690 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002691 case MSR_TSC_AUX:
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002692 if (!guest_cpuid_has_rdtscp(vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002693 return 1;
2694 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002696 msr = find_msr_entry(to_vmx(vcpu), msr_info->index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002697 if (msr) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002698 msr_info->data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08002699 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002700 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002701 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 }
2703
Avi Kivity6aa8b732006-12-10 02:21:36 -08002704 return 0;
2705}
2706
Jan Kiszkacae50132014-01-04 18:47:22 +01002707static void vmx_leave_nested(struct kvm_vcpu *vcpu);
2708
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709/*
2710 * Writes msr value into into the appropriate "register".
2711 * Returns 0 on success, non-0 otherwise.
2712 * Assumes vcpu_load() was already called.
2713 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002714static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002716 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002717 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002718 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002719 u32 msr_index = msr_info->index;
2720 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03002721
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002723 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002724 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002725 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002726#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002727 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002728 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729 vmcs_writel(GUEST_FS_BASE, data);
2730 break;
2731 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002732 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733 vmcs_writel(GUEST_GS_BASE, data);
2734 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002735 case MSR_KERNEL_GS_BASE:
2736 vmx_load_host_state(vmx);
2737 vmx->msr_guest_kernel_gs_base = data;
2738 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739#endif
2740 case MSR_IA32_SYSENTER_CS:
2741 vmcs_write32(GUEST_SYSENTER_CS, data);
2742 break;
2743 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002744 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002745 break;
2746 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002747 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002749 case MSR_IA32_BNDCFGS:
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002750 if (!vmx_mpx_supported())
2751 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002752 vmcs_write64(GUEST_BNDCFGS, data);
2753 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302754 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08002755 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002757 case MSR_IA32_CR_PAT:
2758 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03002759 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2760 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08002761 vmcs_write64(GUEST_IA32_PAT, data);
2762 vcpu->arch.pat = data;
2763 break;
2764 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002765 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002766 break;
Will Auldba904632012-11-29 12:42:50 -08002767 case MSR_IA32_TSC_ADJUST:
2768 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002769 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002770 case MSR_IA32_FEATURE_CONTROL:
2771 if (!nested_vmx_allowed(vcpu) ||
2772 (to_vmx(vcpu)->nested.msr_ia32_feature_control &
2773 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
2774 return 1;
2775 vmx->nested.msr_ia32_feature_control = data;
2776 if (msr_info->host_initiated && data == 0)
2777 vmx_leave_nested(vcpu);
2778 break;
2779 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2780 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08002781 case MSR_IA32_XSS:
2782 if (!vmx_xsaves_supported())
2783 return 1;
2784 /*
2785 * The only supported bit as of Skylake is bit 8, but
2786 * it is not supported on KVM.
2787 */
2788 if (data != 0)
2789 return 1;
2790 vcpu->arch.ia32_xss = data;
2791 if (vcpu->arch.ia32_xss != host_xss)
2792 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
2793 vcpu->arch.ia32_xss, host_xss);
2794 else
2795 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
2796 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002797 case MSR_TSC_AUX:
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08002798 if (!guest_cpuid_has_rdtscp(vcpu))
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002799 return 1;
2800 /* Check reserved bit, higher 32 bits should be zero */
2801 if ((data >> 32) != 0)
2802 return 1;
2803 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002805 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002806 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07002807 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08002808 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002809 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2810 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07002811 ret = kvm_set_shared_msr(msr->index, msr->data,
2812 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03002813 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07002814 if (ret)
2815 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002816 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002817 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002819 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820 }
2821
Eddie Dong2cc51562007-05-21 07:28:09 +03002822 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823}
2824
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002825static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002827 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2828 switch (reg) {
2829 case VCPU_REGS_RSP:
2830 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2831 break;
2832 case VCPU_REGS_RIP:
2833 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2834 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002835 case VCPU_EXREG_PDPTR:
2836 if (enable_ept)
2837 ept_save_pdptrs(vcpu);
2838 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002839 default:
2840 break;
2841 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842}
2843
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844static __init int cpu_has_kvm_support(void)
2845{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002846 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847}
2848
2849static __init int vmx_disabled_by_bios(void)
2850{
2851 u64 msr;
2852
2853 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04002854 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08002855 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04002856 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2857 && tboot_enabled())
2858 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08002859 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04002860 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08002861 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08002862 && !tboot_enabled()) {
2863 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08002864 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04002865 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08002866 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08002867 /* launched w/o TXT and VMX disabled */
2868 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2869 && !tboot_enabled())
2870 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04002871 }
2872
2873 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874}
2875
Dongxiao Xu7725b892010-05-11 18:29:38 +08002876static void kvm_cpu_vmxon(u64 addr)
2877{
2878 asm volatile (ASM_VMX_VMXON_RAX
2879 : : "a"(&addr), "m"(addr)
2880 : "memory", "cc");
2881}
2882
Radim Krčmář13a34e02014-08-28 15:13:03 +02002883static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884{
2885 int cpu = raw_smp_processor_id();
2886 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04002887 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07002889 if (cr4_read_shadow() & X86_CR4_VMXE)
Alexander Graf10474ae2009-09-15 11:37:46 +02002890 return -EBUSY;
2891
Nadav Har'Eld462b812011-05-24 15:26:10 +03002892 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002893
2894 /*
2895 * Now we can enable the vmclear operation in kdump
2896 * since the loaded_vmcss_on_cpu list on this cpu
2897 * has been initialized.
2898 *
2899 * Though the cpu is not in VMX operation now, there
2900 * is no problem to enable the vmclear operation
2901 * for the loaded_vmcss_on_cpu list is empty!
2902 */
2903 crash_enable_local_vmclear(cpu);
2904
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04002906
2907 test_bits = FEATURE_CONTROL_LOCKED;
2908 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2909 if (tboot_enabled())
2910 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2911
2912 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04002914 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2915 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07002916 cr4_set_bits(X86_CR4_VMXE);
Alexander Graf10474ae2009-09-15 11:37:46 +02002917
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002918 if (vmm_exclusive) {
2919 kvm_cpu_vmxon(phys_addr);
2920 ept_sync_global();
2921 }
Alexander Graf10474ae2009-09-15 11:37:46 +02002922
Christoph Lameter89cbc762014-08-17 12:30:40 -05002923 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03002924
Alexander Graf10474ae2009-09-15 11:37:46 +02002925 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002926}
2927
Nadav Har'Eld462b812011-05-24 15:26:10 +03002928static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002929{
2930 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002931 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002932
Nadav Har'Eld462b812011-05-24 15:26:10 +03002933 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2934 loaded_vmcss_on_cpu_link)
2935 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002936}
2937
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002938
2939/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2940 * tricks.
2941 */
2942static void kvm_cpu_vmxoff(void)
2943{
2944 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002945}
2946
Radim Krčmář13a34e02014-08-28 15:13:03 +02002947static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002949 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002950 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002951 kvm_cpu_vmxoff();
2952 }
Andy Lutomirski375074c2014-10-24 15:58:07 -07002953 cr4_clear_bits(X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954}
2955
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002956static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002957 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958{
2959 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002960 u32 ctl = ctl_min | ctl_opt;
2961
2962 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2963
2964 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2965 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2966
2967 /* Ensure minimum (required) set of control bits are supported. */
2968 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002969 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002970
2971 *result = ctl;
2972 return 0;
2973}
2974
Avi Kivity110312c2010-12-21 12:54:20 +02002975static __init bool allow_1_setting(u32 msr, u32 ctl)
2976{
2977 u32 vmx_msr_low, vmx_msr_high;
2978
2979 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2980 return vmx_msr_high & ctl;
2981}
2982
Yang, Sheng002c7f72007-07-31 14:23:01 +03002983static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002984{
2985 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002986 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002987 u32 _pin_based_exec_control = 0;
2988 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002989 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002990 u32 _vmexit_control = 0;
2991 u32 _vmentry_control = 0;
2992
Raghavendra K T10166742012-02-07 23:19:20 +05302993 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002994#ifdef CONFIG_X86_64
2995 CPU_BASED_CR8_LOAD_EXITING |
2996 CPU_BASED_CR8_STORE_EXITING |
2997#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002998 CPU_BASED_CR3_LOAD_EXITING |
2999 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003000 CPU_BASED_USE_IO_BITMAPS |
3001 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03003002 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08003003 CPU_BASED_MWAIT_EXITING |
3004 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02003005 CPU_BASED_INVLPG_EXITING |
3006 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06003007
Sheng Yangf78e0e22007-10-29 09:40:42 +08003008 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08003009 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08003010 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003011 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3012 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003013 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003014#ifdef CONFIG_X86_64
3015 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3016 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3017 ~CPU_BASED_CR8_STORE_EXITING;
3018#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08003019 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08003020 min2 = 0;
3021 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08003022 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08003023 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08003024 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003025 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003026 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003027 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00003028 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08003029 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003030 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03003031 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08003032 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08003033 SECONDARY_EXEC_XSAVES |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08003034 SECONDARY_EXEC_ENABLE_PML |
3035 SECONDARY_EXEC_PCOMMIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08003036 if (adjust_vmx_controls(min2, opt2,
3037 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003038 &_cpu_based_2nd_exec_control) < 0)
3039 return -EIO;
3040 }
3041#ifndef CONFIG_X86_64
3042 if (!(_cpu_based_2nd_exec_control &
3043 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3044 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3045#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08003046
3047 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3048 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08003049 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08003050 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3051 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08003052
Sheng Yangd56f5462008-04-25 10:13:16 +08003053 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03003054 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3055 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03003056 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3057 CPU_BASED_CR3_STORE_EXITING |
3058 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08003059 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
3060 vmx_capability.ept, vmx_capability.vpid);
3061 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003062
Paolo Bonzini81908bf2014-02-21 10:32:27 +01003063 min = VM_EXIT_SAVE_DEBUG_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003064#ifdef CONFIG_X86_64
3065 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3066#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08003067 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003068 VM_EXIT_ACK_INTR_ON_EXIT | VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003069 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3070 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003071 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003072
Yang Zhang01e439b2013-04-11 19:25:12 +08003073 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3074 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR;
3075 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3076 &_pin_based_exec_control) < 0)
3077 return -EIO;
3078
3079 if (!(_cpu_based_2nd_exec_control &
3080 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) ||
3081 !(_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT))
3082 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3083
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01003084 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00003085 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003086 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3087 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003088 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003090 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003091
3092 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3093 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003094 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003095
3096#ifdef CONFIG_X86_64
3097 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3098 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003099 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003100#endif
3101
3102 /* Require Write-Back (WB) memory type for VMCS accesses. */
3103 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003104 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003105
Yang, Sheng002c7f72007-07-31 14:23:01 +03003106 vmcs_conf->size = vmx_msr_high & 0x1fff;
3107 vmcs_conf->order = get_order(vmcs_config.size);
3108 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003109
Yang, Sheng002c7f72007-07-31 14:23:01 +03003110 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3111 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003112 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003113 vmcs_conf->vmexit_ctrl = _vmexit_control;
3114 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003115
Avi Kivity110312c2010-12-21 12:54:20 +02003116 cpu_has_load_ia32_efer =
3117 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3118 VM_ENTRY_LOAD_IA32_EFER)
3119 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3120 VM_EXIT_LOAD_IA32_EFER);
3121
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003122 cpu_has_load_perf_global_ctrl =
3123 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3124 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3125 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3126 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3127
3128 /*
3129 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
3130 * but due to arrata below it can't be used. Workaround is to use
3131 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3132 *
3133 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3134 *
3135 * AAK155 (model 26)
3136 * AAP115 (model 30)
3137 * AAT100 (model 37)
3138 * BC86,AAY89,BD102 (model 44)
3139 * BA97 (model 46)
3140 *
3141 */
3142 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3143 switch (boot_cpu_data.x86_model) {
3144 case 26:
3145 case 30:
3146 case 37:
3147 case 44:
3148 case 46:
3149 cpu_has_load_perf_global_ctrl = false;
3150 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3151 "does not work properly. Using workaround\n");
3152 break;
3153 default:
3154 break;
3155 }
3156 }
3157
Wanpeng Li20300092014-12-02 19:14:59 +08003158 if (cpu_has_xsaves)
3159 rdmsrl(MSR_IA32_XSS, host_xss);
3160
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003161 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003162}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163
3164static struct vmcs *alloc_vmcs_cpu(int cpu)
3165{
3166 int node = cpu_to_node(cpu);
3167 struct page *pages;
3168 struct vmcs *vmcs;
3169
Vlastimil Babka96db8002015-09-08 15:03:50 -07003170 pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171 if (!pages)
3172 return NULL;
3173 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003174 memset(vmcs, 0, vmcs_config.size);
3175 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176 return vmcs;
3177}
3178
3179static struct vmcs *alloc_vmcs(void)
3180{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003181 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182}
3183
3184static void free_vmcs(struct vmcs *vmcs)
3185{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003186 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003187}
3188
Nadav Har'Eld462b812011-05-24 15:26:10 +03003189/*
3190 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3191 */
3192static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3193{
3194 if (!loaded_vmcs->vmcs)
3195 return;
3196 loaded_vmcs_clear(loaded_vmcs);
3197 free_vmcs(loaded_vmcs->vmcs);
3198 loaded_vmcs->vmcs = NULL;
3199}
3200
Sam Ravnborg39959582007-06-01 00:47:13 -07003201static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202{
3203 int cpu;
3204
Zachary Amsden3230bb42009-09-29 11:38:37 -10003205 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003207 per_cpu(vmxarea, cpu) = NULL;
3208 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209}
3210
Bandan Dasfe2b2012014-04-21 15:20:14 -04003211static void init_vmcs_shadow_fields(void)
3212{
3213 int i, j;
3214
3215 /* No checks for read only fields yet */
3216
3217 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3218 switch (shadow_read_write_fields[i]) {
3219 case GUEST_BNDCFGS:
3220 if (!vmx_mpx_supported())
3221 continue;
3222 break;
3223 default:
3224 break;
3225 }
3226
3227 if (j < i)
3228 shadow_read_write_fields[j] =
3229 shadow_read_write_fields[i];
3230 j++;
3231 }
3232 max_shadow_read_write_fields = j;
3233
3234 /* shadowed fields guest access without vmexit */
3235 for (i = 0; i < max_shadow_read_write_fields; i++) {
3236 clear_bit(shadow_read_write_fields[i],
3237 vmx_vmwrite_bitmap);
3238 clear_bit(shadow_read_write_fields[i],
3239 vmx_vmread_bitmap);
3240 }
3241 for (i = 0; i < max_shadow_read_only_fields; i++)
3242 clear_bit(shadow_read_only_fields[i],
3243 vmx_vmread_bitmap);
3244}
3245
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246static __init int alloc_kvm_area(void)
3247{
3248 int cpu;
3249
Zachary Amsden3230bb42009-09-29 11:38:37 -10003250 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003251 struct vmcs *vmcs;
3252
3253 vmcs = alloc_vmcs_cpu(cpu);
3254 if (!vmcs) {
3255 free_kvm_area();
3256 return -ENOMEM;
3257 }
3258
3259 per_cpu(vmxarea, cpu) = vmcs;
3260 }
3261 return 0;
3262}
3263
Gleb Natapov14168782013-01-21 15:36:49 +02003264static bool emulation_required(struct kvm_vcpu *vcpu)
3265{
3266 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3267}
3268
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003269static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003270 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003272 if (!emulate_invalid_guest_state) {
3273 /*
3274 * CS and SS RPL should be equal during guest entry according
3275 * to VMX spec, but in reality it is not always so. Since vcpu
3276 * is in the middle of the transition from real mode to
3277 * protected mode it is safe to assume that RPL 0 is a good
3278 * default value.
3279 */
3280 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
Nadav Amitb32a9912015-03-29 16:33:04 +03003281 save->selector &= ~SEGMENT_RPL_MASK;
3282 save->dpl = save->selector & SEGMENT_RPL_MASK;
Gleb Natapovd99e4152012-12-20 16:57:45 +02003283 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003284 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003285 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003286}
3287
3288static void enter_pmode(struct kvm_vcpu *vcpu)
3289{
3290 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003291 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003292
Gleb Natapovd99e4152012-12-20 16:57:45 +02003293 /*
3294 * Update real mode segment cache. It may be not up-to-date if sement
3295 * register was written while vcpu was in a guest mode.
3296 */
3297 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3298 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3299 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3300 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3301 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3302 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3303
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003304 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305
Avi Kivity2fb92db2011-04-27 19:42:18 +03003306 vmx_segment_cache_clear(vmx);
3307
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003308 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003309
3310 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003311 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3312 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003313 vmcs_writel(GUEST_RFLAGS, flags);
3314
Rusty Russell66aee912007-07-17 23:34:16 +10003315 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3316 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003317
3318 update_exception_bitmap(vcpu);
3319
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003320 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3321 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3322 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3323 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3324 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3325 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003326}
3327
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003328static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003329{
Mathias Krause772e0312012-08-30 01:30:19 +02003330 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003331 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332
Gleb Natapovd99e4152012-12-20 16:57:45 +02003333 var.dpl = 0x3;
3334 if (seg == VCPU_SREG_CS)
3335 var.type = 0x3;
3336
3337 if (!emulate_invalid_guest_state) {
3338 var.selector = var.base >> 4;
3339 var.base = var.base & 0xffff0;
3340 var.limit = 0xffff;
3341 var.g = 0;
3342 var.db = 0;
3343 var.present = 1;
3344 var.s = 1;
3345 var.l = 0;
3346 var.unusable = 0;
3347 var.type = 0x3;
3348 var.avl = 0;
3349 if (save->base & 0xf)
3350 printk_once(KERN_WARNING "kvm: segment base is not "
3351 "paragraph aligned when entering "
3352 "protected mode (seg=%d)", seg);
3353 }
3354
3355 vmcs_write16(sf->selector, var.selector);
3356 vmcs_write32(sf->base, var.base);
3357 vmcs_write32(sf->limit, var.limit);
3358 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003359}
3360
3361static void enter_rmode(struct kvm_vcpu *vcpu)
3362{
3363 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003364 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003365
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003366 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3367 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3368 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3369 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3370 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003371 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3372 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003373
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003374 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003375
Gleb Natapov776e58e2011-03-13 12:34:27 +02003376 /*
3377 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003378 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003379 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003380 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003381 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3382 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003383
Avi Kivity2fb92db2011-04-27 19:42:18 +03003384 vmx_segment_cache_clear(vmx);
3385
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003386 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003387 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003388 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3389
3390 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003391 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003392
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003393 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003394
3395 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003396 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003397 update_exception_bitmap(vcpu);
3398
Gleb Natapovd99e4152012-12-20 16:57:45 +02003399 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3400 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3401 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3402 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3403 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3404 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003405
Eddie Dong8668a3c2007-10-10 14:26:45 +08003406 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003407}
3408
Amit Shah401d10d2009-02-20 22:53:37 +05303409static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3410{
3411 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003412 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3413
3414 if (!msr)
3415 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303416
Avi Kivity44ea2b12009-09-06 15:55:37 +03003417 /*
3418 * Force kernel_gs_base reloading before EFER changes, as control
3419 * of this msr depends on is_long_mode().
3420 */
3421 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003422 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303423 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003424 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303425 msr->data = efer;
3426 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003427 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303428
3429 msr->data = efer & ~EFER_LME;
3430 }
3431 setup_msrs(vmx);
3432}
3433
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003434#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003435
3436static void enter_lmode(struct kvm_vcpu *vcpu)
3437{
3438 u32 guest_tr_ar;
3439
Avi Kivity2fb92db2011-04-27 19:42:18 +03003440 vmx_segment_cache_clear(to_vmx(vcpu));
3441
Avi Kivity6aa8b732006-12-10 02:21:36 -08003442 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003443 if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003444 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3445 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003446 vmcs_write32(GUEST_TR_AR_BYTES,
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003447 (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3448 | VMX_AR_TYPE_BUSY_64_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003449 }
Avi Kivityda38f432010-07-06 11:30:49 +03003450 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003451}
3452
3453static void exit_lmode(struct kvm_vcpu *vcpu)
3454{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003455 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003456 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003457}
3458
3459#endif
3460
Sheng Yang2384d2b2008-01-17 15:14:33 +08003461static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3462{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08003463 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003464 if (enable_ept) {
3465 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3466 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003467 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003468 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003469}
3470
Avi Kivitye8467fd2009-12-29 18:43:06 +02003471static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3472{
3473 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3474
3475 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3476 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3477}
3478
Avi Kivityaff48ba2010-12-05 18:56:11 +02003479static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3480{
3481 if (enable_ept && is_paging(vcpu))
3482 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3483 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3484}
3485
Anthony Liguori25c4c272007-04-27 09:29:21 +03003486static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003487{
Avi Kivityfc78f512009-12-07 12:16:48 +02003488 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3489
3490 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3491 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003492}
3493
Sheng Yang14394422008-04-28 12:24:45 +08003494static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3495{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003496 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3497
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003498 if (!test_bit(VCPU_EXREG_PDPTR,
3499 (unsigned long *)&vcpu->arch.regs_dirty))
3500 return;
3501
Sheng Yang14394422008-04-28 12:24:45 +08003502 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003503 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3504 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3505 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3506 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003507 }
3508}
3509
Avi Kivity8f5d5492009-05-31 18:41:29 +03003510static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3511{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003512 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3513
Avi Kivity8f5d5492009-05-31 18:41:29 +03003514 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003515 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3516 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3517 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3518 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003519 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003520
3521 __set_bit(VCPU_EXREG_PDPTR,
3522 (unsigned long *)&vcpu->arch.regs_avail);
3523 __set_bit(VCPU_EXREG_PDPTR,
3524 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003525}
3526
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003527static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003528
3529static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3530 unsigned long cr0,
3531 struct kvm_vcpu *vcpu)
3532{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003533 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3534 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003535 if (!(cr0 & X86_CR0_PG)) {
3536 /* From paging/starting to nonpaging */
3537 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003538 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003539 (CPU_BASED_CR3_LOAD_EXITING |
3540 CPU_BASED_CR3_STORE_EXITING));
3541 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003542 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003543 } else if (!is_paging(vcpu)) {
3544 /* From nonpaging to paging */
3545 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003546 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003547 ~(CPU_BASED_CR3_LOAD_EXITING |
3548 CPU_BASED_CR3_STORE_EXITING));
3549 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003550 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003551 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003552
3553 if (!(cr0 & X86_CR0_WP))
3554 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003555}
3556
Avi Kivity6aa8b732006-12-10 02:21:36 -08003557static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3558{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003559 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003560 unsigned long hw_cr0;
3561
Gleb Natapov50378782013-02-04 16:00:28 +02003562 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003563 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003564 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003565 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003566 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003567
Gleb Natapov218e7632013-01-21 15:36:45 +02003568 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3569 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003570
Gleb Natapov218e7632013-01-21 15:36:45 +02003571 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3572 enter_rmode(vcpu);
3573 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003574
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003575#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003576 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003577 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003578 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003579 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003580 exit_lmode(vcpu);
3581 }
3582#endif
3583
Avi Kivity089d0342009-03-23 18:26:32 +02003584 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003585 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3586
Avi Kivity02daab22009-12-30 12:40:26 +02003587 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003588 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003589
Avi Kivity6aa8b732006-12-10 02:21:36 -08003590 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003591 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003592 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003593
3594 /* depends on vcpu->arch.cr0 to be set to a new value */
3595 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003596}
3597
Sheng Yang14394422008-04-28 12:24:45 +08003598static u64 construct_eptp(unsigned long root_hpa)
3599{
3600 u64 eptp;
3601
3602 /* TODO write the value reading from MSR */
3603 eptp = VMX_EPT_DEFAULT_MT |
3604 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003605 if (enable_ept_ad_bits)
3606 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003607 eptp |= (root_hpa & PAGE_MASK);
3608
3609 return eptp;
3610}
3611
Avi Kivity6aa8b732006-12-10 02:21:36 -08003612static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3613{
Sheng Yang14394422008-04-28 12:24:45 +08003614 unsigned long guest_cr3;
3615 u64 eptp;
3616
3617 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003618 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003619 eptp = construct_eptp(cr3);
3620 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02003621 if (is_paging(vcpu) || is_guest_mode(vcpu))
3622 guest_cr3 = kvm_read_cr3(vcpu);
3623 else
3624 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02003625 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003626 }
3627
Sheng Yang2384d2b2008-01-17 15:14:33 +08003628 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003629 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003630}
3631
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003632static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633{
Ben Serebrin085e68e2015-04-16 11:58:05 -07003634 /*
3635 * Pass through host's Machine Check Enable value to hw_cr4, which
3636 * is in force while we are in guest mode. Do not let guests control
3637 * this bit, even if host CR4.MCE == 0.
3638 */
3639 unsigned long hw_cr4 =
3640 (cr4_read_shadow() & X86_CR4_MCE) |
3641 (cr4 & ~X86_CR4_MCE) |
3642 (to_vmx(vcpu)->rmode.vm86_active ?
3643 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
Sheng Yang14394422008-04-28 12:24:45 +08003644
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003645 if (cr4 & X86_CR4_VMXE) {
3646 /*
3647 * To use VMXON (and later other VMX instructions), a guest
3648 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3649 * So basically the check on whether to allow nested VMX
3650 * is here.
3651 */
3652 if (!nested_vmx_allowed(vcpu))
3653 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003654 }
3655 if (to_vmx(vcpu)->nested.vmxon &&
3656 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003657 return 1;
3658
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003659 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02003660 if (enable_ept) {
3661 if (!is_paging(vcpu)) {
3662 hw_cr4 &= ~X86_CR4_PAE;
3663 hw_cr4 |= X86_CR4_PSE;
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003664 /*
Feng Wue1e746b2014-04-01 17:46:35 +08003665 * SMEP/SMAP is disabled if CPU is in non-paging mode
3666 * in hardware. However KVM always uses paging mode to
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003667 * emulate guest non-paging mode with TDP.
Feng Wue1e746b2014-04-01 17:46:35 +08003668 * To emulate this behavior, SMEP/SMAP needs to be
3669 * manually disabled when guest switches to non-paging
3670 * mode.
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003671 */
Feng Wue1e746b2014-04-01 17:46:35 +08003672 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP);
Avi Kivitybc230082009-12-08 12:14:42 +02003673 } else if (!(cr4 & X86_CR4_PAE)) {
3674 hw_cr4 &= ~X86_CR4_PAE;
3675 }
3676 }
Sheng Yang14394422008-04-28 12:24:45 +08003677
3678 vmcs_writel(CR4_READ_SHADOW, cr4);
3679 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003680 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003681}
3682
Avi Kivity6aa8b732006-12-10 02:21:36 -08003683static void vmx_get_segment(struct kvm_vcpu *vcpu,
3684 struct kvm_segment *var, int seg)
3685{
Avi Kivitya9179492011-01-03 14:28:52 +02003686 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003687 u32 ar;
3688
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003689 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003690 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003691 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003692 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003693 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003694 var->base = vmx_read_guest_seg_base(vmx, seg);
3695 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3696 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003697 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003698 var->base = vmx_read_guest_seg_base(vmx, seg);
3699 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3700 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3701 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003702 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003703 var->type = ar & 15;
3704 var->s = (ar >> 4) & 1;
3705 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003706 /*
3707 * Some userspaces do not preserve unusable property. Since usable
3708 * segment has to be present according to VMX spec we can use present
3709 * property to amend userspace bug by making unusable segment always
3710 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3711 * segment as unusable.
3712 */
3713 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003714 var->avl = (ar >> 12) & 1;
3715 var->l = (ar >> 13) & 1;
3716 var->db = (ar >> 14) & 1;
3717 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718}
3719
Avi Kivitya9179492011-01-03 14:28:52 +02003720static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3721{
Avi Kivitya9179492011-01-03 14:28:52 +02003722 struct kvm_segment s;
3723
3724 if (to_vmx(vcpu)->rmode.vm86_active) {
3725 vmx_get_segment(vcpu, &s, seg);
3726 return s.base;
3727 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003728 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003729}
3730
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003731static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003732{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003733 struct vcpu_vmx *vmx = to_vmx(vcpu);
3734
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003735 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003736 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003737 else {
3738 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003739 return VMX_AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02003740 }
Avi Kivity69c73022011-03-07 15:26:44 +02003741}
3742
Avi Kivity653e3102007-05-07 10:55:37 +03003743static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003744{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003745 u32 ar;
3746
Avi Kivityf0495f92012-06-07 17:06:10 +03003747 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003748 ar = 1 << 16;
3749 else {
3750 ar = var->type & 15;
3751 ar |= (var->s & 1) << 4;
3752 ar |= (var->dpl & 3) << 5;
3753 ar |= (var->present & 1) << 7;
3754 ar |= (var->avl & 1) << 12;
3755 ar |= (var->l & 1) << 13;
3756 ar |= (var->db & 1) << 14;
3757 ar |= (var->g & 1) << 15;
3758 }
Avi Kivity653e3102007-05-07 10:55:37 +03003759
3760 return ar;
3761}
3762
3763static void vmx_set_segment(struct kvm_vcpu *vcpu,
3764 struct kvm_segment *var, int seg)
3765{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003766 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003767 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003768
Avi Kivity2fb92db2011-04-27 19:42:18 +03003769 vmx_segment_cache_clear(vmx);
3770
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003771 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3772 vmx->rmode.segs[seg] = *var;
3773 if (seg == VCPU_SREG_TR)
3774 vmcs_write16(sf->selector, var->selector);
3775 else if (var->s)
3776 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003777 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003778 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003779
Avi Kivity653e3102007-05-07 10:55:37 +03003780 vmcs_writel(sf->base, var->base);
3781 vmcs_write32(sf->limit, var->limit);
3782 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003783
3784 /*
3785 * Fix the "Accessed" bit in AR field of segment registers for older
3786 * qemu binaries.
3787 * IA32 arch specifies that at the time of processor reset the
3788 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003789 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003790 * state vmexit when "unrestricted guest" mode is turned on.
3791 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3792 * tree. Newer qemu binaries with that qemu fix would not need this
3793 * kvm hack.
3794 */
3795 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003796 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003797
Gleb Natapovf924d662012-12-12 19:10:55 +02003798 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003799
3800out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01003801 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003802}
3803
Avi Kivity6aa8b732006-12-10 02:21:36 -08003804static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3805{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003806 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003807
3808 *db = (ar >> 14) & 1;
3809 *l = (ar >> 13) & 1;
3810}
3811
Gleb Natapov89a27f42010-02-16 10:51:48 +02003812static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003813{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003814 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3815 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003816}
3817
Gleb Natapov89a27f42010-02-16 10:51:48 +02003818static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003819{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003820 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3821 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003822}
3823
Gleb Natapov89a27f42010-02-16 10:51:48 +02003824static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003825{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003826 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3827 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003828}
3829
Gleb Natapov89a27f42010-02-16 10:51:48 +02003830static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003831{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003832 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3833 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003834}
3835
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003836static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3837{
3838 struct kvm_segment var;
3839 u32 ar;
3840
3841 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003842 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003843 if (seg == VCPU_SREG_CS)
3844 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003845 ar = vmx_segment_access_rights(&var);
3846
3847 if (var.base != (var.selector << 4))
3848 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003849 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003850 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003851 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003852 return false;
3853
3854 return true;
3855}
3856
3857static bool code_segment_valid(struct kvm_vcpu *vcpu)
3858{
3859 struct kvm_segment cs;
3860 unsigned int cs_rpl;
3861
3862 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003863 cs_rpl = cs.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003864
Avi Kivity1872a3f2009-01-04 23:26:52 +02003865 if (cs.unusable)
3866 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003867 if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003868 return false;
3869 if (!cs.s)
3870 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003871 if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003872 if (cs.dpl > cs_rpl)
3873 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003874 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003875 if (cs.dpl != cs_rpl)
3876 return false;
3877 }
3878 if (!cs.present)
3879 return false;
3880
3881 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3882 return true;
3883}
3884
3885static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3886{
3887 struct kvm_segment ss;
3888 unsigned int ss_rpl;
3889
3890 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
Nadav Amitb32a9912015-03-29 16:33:04 +03003891 ss_rpl = ss.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003892
Avi Kivity1872a3f2009-01-04 23:26:52 +02003893 if (ss.unusable)
3894 return true;
3895 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003896 return false;
3897 if (!ss.s)
3898 return false;
3899 if (ss.dpl != ss_rpl) /* DPL != RPL */
3900 return false;
3901 if (!ss.present)
3902 return false;
3903
3904 return true;
3905}
3906
3907static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3908{
3909 struct kvm_segment var;
3910 unsigned int rpl;
3911
3912 vmx_get_segment(vcpu, &var, seg);
Nadav Amitb32a9912015-03-29 16:33:04 +03003913 rpl = var.selector & SEGMENT_RPL_MASK;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003914
Avi Kivity1872a3f2009-01-04 23:26:52 +02003915 if (var.unusable)
3916 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003917 if (!var.s)
3918 return false;
3919 if (!var.present)
3920 return false;
Andy Lutomirski4d283ec2015-08-13 13:18:48 -07003921 if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003922 if (var.dpl < rpl) /* DPL < RPL */
3923 return false;
3924 }
3925
3926 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3927 * rights flags
3928 */
3929 return true;
3930}
3931
3932static bool tr_valid(struct kvm_vcpu *vcpu)
3933{
3934 struct kvm_segment tr;
3935
3936 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3937
Avi Kivity1872a3f2009-01-04 23:26:52 +02003938 if (tr.unusable)
3939 return false;
Nadav Amitb32a9912015-03-29 16:33:04 +03003940 if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003941 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003942 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003943 return false;
3944 if (!tr.present)
3945 return false;
3946
3947 return true;
3948}
3949
3950static bool ldtr_valid(struct kvm_vcpu *vcpu)
3951{
3952 struct kvm_segment ldtr;
3953
3954 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3955
Avi Kivity1872a3f2009-01-04 23:26:52 +02003956 if (ldtr.unusable)
3957 return true;
Nadav Amitb32a9912015-03-29 16:33:04 +03003958 if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003959 return false;
3960 if (ldtr.type != 2)
3961 return false;
3962 if (!ldtr.present)
3963 return false;
3964
3965 return true;
3966}
3967
3968static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3969{
3970 struct kvm_segment cs, ss;
3971
3972 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3973 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3974
Nadav Amitb32a9912015-03-29 16:33:04 +03003975 return ((cs.selector & SEGMENT_RPL_MASK) ==
3976 (ss.selector & SEGMENT_RPL_MASK));
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003977}
3978
3979/*
3980 * Check if guest state is valid. Returns true if valid, false if
3981 * not.
3982 * We assume that registers are always usable
3983 */
3984static bool guest_state_valid(struct kvm_vcpu *vcpu)
3985{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02003986 if (enable_unrestricted_guest)
3987 return true;
3988
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003989 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003990 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003991 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3992 return false;
3993 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3994 return false;
3995 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3996 return false;
3997 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3998 return false;
3999 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4000 return false;
4001 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4002 return false;
4003 } else {
4004 /* protected mode guest state checks */
4005 if (!cs_ss_rpl_check(vcpu))
4006 return false;
4007 if (!code_segment_valid(vcpu))
4008 return false;
4009 if (!stack_segment_valid(vcpu))
4010 return false;
4011 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4012 return false;
4013 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4014 return false;
4015 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4016 return false;
4017 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4018 return false;
4019 if (!tr_valid(vcpu))
4020 return false;
4021 if (!ldtr_valid(vcpu))
4022 return false;
4023 }
4024 /* TODO:
4025 * - Add checks on RIP
4026 * - Add checks on RFLAGS
4027 */
4028
4029 return true;
4030}
4031
Mike Dayd77c26f2007-10-08 09:02:08 -04004032static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004033{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004034 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02004035 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004036 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004037
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004038 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01004039 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02004040 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4041 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004042 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004043 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08004044 r = kvm_write_guest_page(kvm, fn++, &data,
4045 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02004046 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004047 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004048 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4049 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004050 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004051 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4052 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004053 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02004054 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004055 r = kvm_write_guest_page(kvm, fn, &data,
4056 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4057 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05004058out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004059 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004060 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004061}
4062
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004063static int init_rmode_identity_map(struct kvm *kvm)
4064{
Tang Chenf51770e2014-09-16 18:41:59 +08004065 int i, idx, r = 0;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004066 pfn_t identity_map_pfn;
4067 u32 tmp;
4068
Avi Kivity089d0342009-03-23 18:26:32 +02004069 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08004070 return 0;
Tang Chena255d472014-09-16 18:41:58 +08004071
4072 /* Protect kvm->arch.ept_identity_pagetable_done. */
4073 mutex_lock(&kvm->slots_lock);
4074
Tang Chenf51770e2014-09-16 18:41:59 +08004075 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08004076 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08004077
Sheng Yangb927a3c2009-07-21 10:42:48 +08004078 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08004079
4080 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08004081 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08004082 goto out2;
4083
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004084 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004085 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4086 if (r < 0)
4087 goto out;
4088 /* Set up identity-mapping pagetable for EPT in real mode */
4089 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4090 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4091 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4092 r = kvm_write_guest_page(kvm, identity_map_pfn,
4093 &tmp, i * sizeof(tmp), sizeof(tmp));
4094 if (r < 0)
4095 goto out;
4096 }
4097 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08004098
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004099out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08004100 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08004101
4102out2:
4103 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004104 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004105}
4106
Avi Kivity6aa8b732006-12-10 02:21:36 -08004107static void seg_setup(int seg)
4108{
Mathias Krause772e0312012-08-30 01:30:19 +02004109 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004110 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004111
4112 vmcs_write16(sf->selector, 0);
4113 vmcs_writel(sf->base, 0);
4114 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004115 ar = 0x93;
4116 if (seg == VCPU_SREG_CS)
4117 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004118
4119 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004120}
4121
Sheng Yangf78e0e22007-10-29 09:40:42 +08004122static int alloc_apic_access_page(struct kvm *kvm)
4123{
Xiao Guangrong44841412012-09-07 14:14:20 +08004124 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004125 struct kvm_userspace_memory_region kvm_userspace_mem;
4126 int r = 0;
4127
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004128 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004129 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004130 goto out;
4131 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
4132 kvm_userspace_mem.flags = 0;
Tang Chen73a6d942014-09-11 13:38:00 +08004133 kvm_userspace_mem.guest_phys_addr = APIC_DEFAULT_PHYS_BASE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004134 kvm_userspace_mem.memory_size = PAGE_SIZE;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02004135 r = __x86_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004136 if (r)
4137 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004138
Tang Chen73a6d942014-09-11 13:38:00 +08004139 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004140 if (is_error_page(page)) {
4141 r = -EFAULT;
4142 goto out;
4143 }
4144
Tang Chenc24ae0d2014-09-24 15:57:58 +08004145 /*
4146 * Do not pin the page in memory, so that memory hot-unplug
4147 * is able to migrate it.
4148 */
4149 put_page(page);
4150 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004151out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004152 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004153 return r;
4154}
4155
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004156static int alloc_identity_pagetable(struct kvm *kvm)
4157{
Tang Chena255d472014-09-16 18:41:58 +08004158 /* Called with kvm->slots_lock held. */
4159
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004160 struct kvm_userspace_memory_region kvm_userspace_mem;
4161 int r = 0;
4162
Tang Chena255d472014-09-16 18:41:58 +08004163 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4164
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004165 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
4166 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004167 kvm_userspace_mem.guest_phys_addr =
4168 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004169 kvm_userspace_mem.memory_size = PAGE_SIZE;
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02004170 r = __x86_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004171
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004172 return r;
4173}
4174
Sheng Yang2384d2b2008-01-17 15:14:33 +08004175static void allocate_vpid(struct vcpu_vmx *vmx)
4176{
4177 int vpid;
4178
4179 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02004180 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004181 return;
4182 spin_lock(&vmx_vpid_lock);
4183 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
4184 if (vpid < VMX_NR_VPIDS) {
4185 vmx->vpid = vpid;
4186 __set_bit(vpid, vmx_vpid_bitmap);
4187 }
4188 spin_unlock(&vmx_vpid_lock);
4189}
4190
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004191static void free_vpid(struct vcpu_vmx *vmx)
4192{
4193 if (!enable_vpid)
4194 return;
4195 spin_lock(&vmx_vpid_lock);
4196 if (vmx->vpid != 0)
4197 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
4198 spin_unlock(&vmx_vpid_lock);
4199}
4200
Yang Zhang8d146952013-01-25 10:18:50 +08004201#define MSR_TYPE_R 1
4202#define MSR_TYPE_W 2
4203static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4204 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004205{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004206 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004207
4208 if (!cpu_has_vmx_msr_bitmap())
4209 return;
4210
4211 /*
4212 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4213 * have the write-low and read-high bitmap offsets the wrong way round.
4214 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4215 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004216 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004217 if (type & MSR_TYPE_R)
4218 /* read-low */
4219 __clear_bit(msr, msr_bitmap + 0x000 / f);
4220
4221 if (type & MSR_TYPE_W)
4222 /* write-low */
4223 __clear_bit(msr, msr_bitmap + 0x800 / f);
4224
Sheng Yang25c5f222008-03-28 13:18:56 +08004225 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4226 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004227 if (type & MSR_TYPE_R)
4228 /* read-high */
4229 __clear_bit(msr, msr_bitmap + 0x400 / f);
4230
4231 if (type & MSR_TYPE_W)
4232 /* write-high */
4233 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4234
4235 }
4236}
4237
4238static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4239 u32 msr, int type)
4240{
4241 int f = sizeof(unsigned long);
4242
4243 if (!cpu_has_vmx_msr_bitmap())
4244 return;
4245
4246 /*
4247 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4248 * have the write-low and read-high bitmap offsets the wrong way round.
4249 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4250 */
4251 if (msr <= 0x1fff) {
4252 if (type & MSR_TYPE_R)
4253 /* read-low */
4254 __set_bit(msr, msr_bitmap + 0x000 / f);
4255
4256 if (type & MSR_TYPE_W)
4257 /* write-low */
4258 __set_bit(msr, msr_bitmap + 0x800 / f);
4259
4260 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4261 msr &= 0x1fff;
4262 if (type & MSR_TYPE_R)
4263 /* read-high */
4264 __set_bit(msr, msr_bitmap + 0x400 / f);
4265
4266 if (type & MSR_TYPE_W)
4267 /* write-high */
4268 __set_bit(msr, msr_bitmap + 0xc00 / f);
4269
Sheng Yang25c5f222008-03-28 13:18:56 +08004270 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004271}
4272
Wincy Vanf2b93282015-02-03 23:56:03 +08004273/*
4274 * If a msr is allowed by L0, we should check whether it is allowed by L1.
4275 * The corresponding bit will be cleared unless both of L0 and L1 allow it.
4276 */
4277static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1,
4278 unsigned long *msr_bitmap_nested,
4279 u32 msr, int type)
4280{
4281 int f = sizeof(unsigned long);
4282
4283 if (!cpu_has_vmx_msr_bitmap()) {
4284 WARN_ON(1);
4285 return;
4286 }
4287
4288 /*
4289 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4290 * have the write-low and read-high bitmap offsets the wrong way round.
4291 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4292 */
4293 if (msr <= 0x1fff) {
4294 if (type & MSR_TYPE_R &&
4295 !test_bit(msr, msr_bitmap_l1 + 0x000 / f))
4296 /* read-low */
4297 __clear_bit(msr, msr_bitmap_nested + 0x000 / f);
4298
4299 if (type & MSR_TYPE_W &&
4300 !test_bit(msr, msr_bitmap_l1 + 0x800 / f))
4301 /* write-low */
4302 __clear_bit(msr, msr_bitmap_nested + 0x800 / f);
4303
4304 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4305 msr &= 0x1fff;
4306 if (type & MSR_TYPE_R &&
4307 !test_bit(msr, msr_bitmap_l1 + 0x400 / f))
4308 /* read-high */
4309 __clear_bit(msr, msr_bitmap_nested + 0x400 / f);
4310
4311 if (type & MSR_TYPE_W &&
4312 !test_bit(msr, msr_bitmap_l1 + 0xc00 / f))
4313 /* write-high */
4314 __clear_bit(msr, msr_bitmap_nested + 0xc00 / f);
4315
4316 }
4317}
4318
Avi Kivity58972972009-02-24 22:26:47 +02004319static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4320{
4321 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004322 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4323 msr, MSR_TYPE_R | MSR_TYPE_W);
4324 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4325 msr, MSR_TYPE_R | MSR_TYPE_W);
4326}
4327
4328static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
4329{
4330 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4331 msr, MSR_TYPE_R);
4332 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4333 msr, MSR_TYPE_R);
4334}
4335
4336static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
4337{
4338 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4339 msr, MSR_TYPE_R);
4340 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4341 msr, MSR_TYPE_R);
4342}
4343
4344static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
4345{
4346 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4347 msr, MSR_TYPE_W);
4348 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4349 msr, MSR_TYPE_W);
Avi Kivity58972972009-02-24 22:26:47 +02004350}
4351
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004352static int vmx_cpu_uses_apicv(struct kvm_vcpu *vcpu)
4353{
Paolo Bonzini35754c92015-07-29 12:05:37 +02004354 return enable_apicv && lapic_in_kernel(vcpu);
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +02004355}
4356
Wincy Van705699a2015-02-03 23:58:17 +08004357static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu)
4358{
4359 struct vcpu_vmx *vmx = to_vmx(vcpu);
4360 int max_irr;
4361 void *vapic_page;
4362 u16 status;
4363
4364 if (vmx->nested.pi_desc &&
4365 vmx->nested.pi_pending) {
4366 vmx->nested.pi_pending = false;
4367 if (!pi_test_and_clear_on(vmx->nested.pi_desc))
4368 return 0;
4369
4370 max_irr = find_last_bit(
4371 (unsigned long *)vmx->nested.pi_desc->pir, 256);
4372
4373 if (max_irr == 256)
4374 return 0;
4375
4376 vapic_page = kmap(vmx->nested.virtual_apic_page);
4377 if (!vapic_page) {
4378 WARN_ON(1);
4379 return -ENOMEM;
4380 }
4381 __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page);
4382 kunmap(vmx->nested.virtual_apic_page);
4383
4384 status = vmcs_read16(GUEST_INTR_STATUS);
4385 if ((u8)max_irr > ((u8)status & 0xff)) {
4386 status &= ~0xff;
4387 status |= (u8)max_irr;
4388 vmcs_write16(GUEST_INTR_STATUS, status);
4389 }
4390 }
4391 return 0;
4392}
4393
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004394static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
4395{
4396#ifdef CONFIG_SMP
4397 if (vcpu->mode == IN_GUEST_MODE) {
4398 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4399 POSTED_INTR_VECTOR);
4400 return true;
4401 }
4402#endif
4403 return false;
4404}
4405
Wincy Van705699a2015-02-03 23:58:17 +08004406static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4407 int vector)
4408{
4409 struct vcpu_vmx *vmx = to_vmx(vcpu);
4410
4411 if (is_guest_mode(vcpu) &&
4412 vector == vmx->nested.posted_intr_nv) {
4413 /* the PIR and ON have been set by L1. */
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004414 kvm_vcpu_trigger_posted_interrupt(vcpu);
Wincy Van705699a2015-02-03 23:58:17 +08004415 /*
4416 * If a posted intr is not recognized by hardware,
4417 * we will accomplish it in the next vmentry.
4418 */
4419 vmx->nested.pi_pending = true;
4420 kvm_make_request(KVM_REQ_EVENT, vcpu);
4421 return 0;
4422 }
4423 return -1;
4424}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004425/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004426 * Send interrupt to vcpu via posted interrupt way.
4427 * 1. If target vcpu is running(non-root mode), send posted interrupt
4428 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4429 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4430 * interrupt from PIR in next vmentry.
4431 */
4432static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4433{
4434 struct vcpu_vmx *vmx = to_vmx(vcpu);
4435 int r;
4436
Wincy Van705699a2015-02-03 23:58:17 +08004437 r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4438 if (!r)
4439 return;
4440
Yang Zhanga20ed542013-04-11 19:25:15 +08004441 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4442 return;
4443
4444 r = pi_test_and_set_on(&vmx->pi_desc);
4445 kvm_make_request(KVM_REQ_EVENT, vcpu);
Radim Krčmář21bc8dc2015-02-16 15:36:33 +01004446 if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu))
Yang Zhanga20ed542013-04-11 19:25:15 +08004447 kvm_vcpu_kick(vcpu);
4448}
4449
4450static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4451{
4452 struct vcpu_vmx *vmx = to_vmx(vcpu);
4453
4454 if (!pi_test_and_clear_on(&vmx->pi_desc))
4455 return;
4456
4457 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4458}
4459
4460static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu)
4461{
4462 return;
4463}
4464
Avi Kivity6aa8b732006-12-10 02:21:36 -08004465/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004466 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4467 * will not change in the lifetime of the guest.
4468 * Note that host-state that does change is set elsewhere. E.g., host-state
4469 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4470 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004471static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004472{
4473 u32 low32, high32;
4474 unsigned long tmpl;
4475 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004476 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004477
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07004478 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004479 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4480
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004481 /* Save the most likely value for this task's CR4 in the VMCS. */
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07004482 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004483 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
4484 vmx->host_state.vmcs_host_cr4 = cr4;
4485
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004486 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004487#ifdef CONFIG_X86_64
4488 /*
4489 * Load null selectors, so we can avoid reloading them in
4490 * __vmx_load_host_state(), in case userspace uses the null selectors
4491 * too (the expected case).
4492 */
4493 vmcs_write16(HOST_DS_SELECTOR, 0);
4494 vmcs_write16(HOST_ES_SELECTOR, 0);
4495#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004496 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4497 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004498#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004499 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4500 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4501
4502 native_store_idt(&dt);
4503 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004504 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004505
Avi Kivity83287ea422012-09-16 15:10:57 +03004506 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004507
4508 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4509 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4510 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4511 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4512
4513 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4514 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4515 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4516 }
4517}
4518
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004519static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4520{
4521 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4522 if (enable_ept)
4523 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004524 if (is_guest_mode(&vmx->vcpu))
4525 vmx->vcpu.arch.cr4_guest_owned_bits &=
4526 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004527 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4528}
4529
Yang Zhang01e439b2013-04-11 19:25:12 +08004530static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4531{
4532 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4533
Paolo Bonzini35754c92015-07-29 12:05:37 +02004534 if (!vmx_cpu_uses_apicv(&vmx->vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004535 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
4536 return pin_based_exec_ctrl;
4537}
4538
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004539static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4540{
4541 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01004542
4543 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4544 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4545
Paolo Bonzini35754c92015-07-29 12:05:37 +02004546 if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004547 exec_control &= ~CPU_BASED_TPR_SHADOW;
4548#ifdef CONFIG_X86_64
4549 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4550 CPU_BASED_CR8_LOAD_EXITING;
4551#endif
4552 }
4553 if (!enable_ept)
4554 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4555 CPU_BASED_CR3_LOAD_EXITING |
4556 CPU_BASED_INVLPG_EXITING;
4557 return exec_control;
4558}
4559
4560static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4561{
4562 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004563 if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu))
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004564 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4565 if (vmx->vpid == 0)
4566 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4567 if (!enable_ept) {
4568 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4569 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00004570 /* Enable INVPCID for non-ept guests may cause performance regression. */
4571 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004572 }
4573 if (!enable_unrestricted_guest)
4574 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4575 if (!ple_gap)
4576 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Paolo Bonzini35754c92015-07-29 12:05:37 +02004577 if (!vmx_cpu_uses_apicv(&vmx->vcpu))
Yang Zhangc7c9c562013-01-25 10:18:51 +08004578 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4579 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004580 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03004581 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4582 (handle_vmptrld).
4583 We can NOT enable shadow_vmcs here because we don't have yet
4584 a current VMCS12
4585 */
4586 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huang843e4332015-01-28 10:54:28 +08004587 /* PML is enabled/disabled in creating/destorying vcpu */
4588 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
4589
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004590 /* Currently, we allow L1 guest to directly run pcommit instruction. */
4591 exec_control &= ~SECONDARY_EXEC_PCOMMIT;
4592
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004593 return exec_control;
4594}
4595
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004596static void ept_set_mmio_spte_mask(void)
4597{
4598 /*
4599 * EPT Misconfigurations can be generated if the value of bits 2:0
4600 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08004601 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004602 * spte.
4603 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08004604 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004605}
4606
Wanpeng Lif53cd632014-12-02 19:14:58 +08004607#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004608/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004609 * Sets up the vmcs for emulated real mode.
4610 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004611static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004612{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004613#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004614 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004615#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004616 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004617
Avi Kivity6aa8b732006-12-10 02:21:36 -08004618 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004619 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4620 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004621
Abel Gordon4607c2d2013-04-18 14:35:55 +03004622 if (enable_shadow_vmcs) {
4623 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
4624 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
4625 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004626 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02004627 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08004628
Avi Kivity6aa8b732006-12-10 02:21:36 -08004629 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4630
Avi Kivity6aa8b732006-12-10 02:21:36 -08004631 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08004632 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004633
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004634 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004635
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08004636 if (cpu_has_secondary_exec_ctrls())
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004637 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4638 vmx_secondary_exec_control(vmx));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004639
Paolo Bonzini35754c92015-07-29 12:05:37 +02004640 if (vmx_cpu_uses_apicv(&vmx->vcpu)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004641 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4642 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4643 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4644 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4645
4646 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004647
4648 vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
4649 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004650 }
4651
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004652 if (ple_gap) {
4653 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02004654 vmx->ple_window = ple_window;
4655 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004656 }
4657
Xiao Guangrongc3707952011-07-12 03:28:04 +08004658 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4659 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004660 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4661
Avi Kivity9581d442010-10-19 16:46:55 +02004662 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4663 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004664 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004665#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004666 rdmsrl(MSR_FS_BASE, a);
4667 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
4668 rdmsrl(MSR_GS_BASE, a);
4669 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
4670#else
4671 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4672 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
4673#endif
4674
Eddie Dong2cc51562007-05-21 07:28:09 +03004675 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4676 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004677 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03004678 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004679 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004680
Radim Krčmář74545702015-04-27 15:11:25 +02004681 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
4682 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
Sheng Yang468d4722008-10-09 16:01:55 +08004683
Paolo Bonzini03916db2014-07-24 14:21:57 +02004684 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004685 u32 index = vmx_msr_index[i];
4686 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004687 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004688
4689 if (rdmsr_safe(index, &data_low, &data_high) < 0)
4690 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08004691 if (wrmsr_safe(index, data_low, data_high) < 0)
4692 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03004693 vmx->guest_msrs[j].index = i;
4694 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02004695 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004696 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004697 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004698
Gleb Natapov2961e8762013-11-25 15:37:13 +02004699
4700 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004701
4702 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02004703 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004704
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004705 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004706 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004707
Wanpeng Lif53cd632014-12-02 19:14:58 +08004708 if (vmx_xsaves_supported())
4709 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
4710
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004711 return 0;
4712}
4713
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004714static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004715{
4716 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004717 struct msr_data apic_base_msr;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004718 u64 cr0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004719
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004720 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004721
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004722 vmx->soft_vnmi_blocked = 0;
4723
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004724 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004725 kvm_set_cr8(vcpu, 0);
4726
4727 if (!init_event) {
4728 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
4729 MSR_IA32_APICBASE_ENABLE;
4730 if (kvm_vcpu_is_reset_bsp(vcpu))
4731 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4732 apic_base_msr.host_initiated = true;
4733 kvm_set_apic_base(vcpu, &apic_base_msr);
4734 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004735
Avi Kivity2fb92db2011-04-27 19:42:18 +03004736 vmx_segment_cache_clear(vmx);
4737
Avi Kivity5706be02008-08-20 15:07:31 +03004738 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004739 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzini04b66832013-03-19 16:30:26 +01004740 vmcs_write32(GUEST_CS_BASE, 0xffff0000);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004741
4742 seg_setup(VCPU_SREG_DS);
4743 seg_setup(VCPU_SREG_ES);
4744 seg_setup(VCPU_SREG_FS);
4745 seg_setup(VCPU_SREG_GS);
4746 seg_setup(VCPU_SREG_SS);
4747
4748 vmcs_write16(GUEST_TR_SELECTOR, 0);
4749 vmcs_writel(GUEST_TR_BASE, 0);
4750 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4751 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4752
4753 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4754 vmcs_writel(GUEST_LDTR_BASE, 0);
4755 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4756 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4757
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004758 if (!init_event) {
4759 vmcs_write32(GUEST_SYSENTER_CS, 0);
4760 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4761 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4762 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4763 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004764
4765 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01004766 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004767
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004768 vmcs_writel(GUEST_GDTR_BASE, 0);
4769 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4770
4771 vmcs_writel(GUEST_IDTR_BASE, 0);
4772 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4773
Anthony Liguori443381a2010-12-06 10:53:38 -06004774 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004775 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
4776 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
4777
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004778 setup_msrs(vmx);
4779
Avi Kivity6aa8b732006-12-10 02:21:36 -08004780 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4781
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004782 if (cpu_has_vmx_tpr_shadow() && !init_event) {
Sheng Yangf78e0e22007-10-29 09:40:42 +08004783 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
Paolo Bonzini35754c92015-07-29 12:05:37 +02004784 if (cpu_need_tpr_shadow(vcpu))
Sheng Yangf78e0e22007-10-29 09:40:42 +08004785 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004786 __pa(vcpu->arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004787 vmcs_write32(TPR_THRESHOLD, 0);
4788 }
4789
Paolo Bonzinia73896c2014-11-02 07:54:30 +01004790 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004791
Paolo Bonzini35754c92015-07-29 12:05:37 +02004792 if (vmx_cpu_uses_apicv(vcpu))
Yang Zhang01e439b2013-04-11 19:25:12 +08004793 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
4794
Sheng Yang2384d2b2008-01-17 15:14:33 +08004795 if (vmx->vpid != 0)
4796 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4797
Nadav Amitd28bc9d2015-04-13 14:34:08 +03004798 cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
4799 vmx_set_cr0(vcpu, cr0); /* enter rmode */
4800 vmx->vcpu.arch.cr0 = cr0;
4801 vmx_set_cr4(vcpu, 0);
4802 if (!init_event)
4803 vmx_set_efer(vcpu, 0);
4804 vmx_fpu_activate(vcpu);
4805 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004806
Gui Jianfengb9d762f2010-06-07 10:32:29 +08004807 vpid_sync_context(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004808}
4809
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004810/*
4811 * In nested virtualization, check if L1 asked to exit on external interrupts.
4812 * For most existing hypervisors, this will always return true.
4813 */
4814static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4815{
4816 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4817 PIN_BASED_EXT_INTR_MASK;
4818}
4819
Bandan Das77b0f5d2014-04-19 18:17:45 -04004820/*
4821 * In nested virtualization, check if L1 has set
4822 * VM_EXIT_ACK_INTR_ON_EXIT
4823 */
4824static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
4825{
4826 return get_vmcs12(vcpu)->vm_exit_controls &
4827 VM_EXIT_ACK_INTR_ON_EXIT;
4828}
4829
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004830static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
4831{
4832 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4833 PIN_BASED_NMI_EXITING;
4834}
4835
Jan Kiszkac9a79532014-03-07 20:03:15 +01004836static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004837{
4838 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02004839
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004840 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4841 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
4842 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4843}
4844
Jan Kiszkac9a79532014-03-07 20:03:15 +01004845static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004846{
4847 u32 cpu_based_vm_exec_control;
4848
Jan Kiszkac9a79532014-03-07 20:03:15 +01004849 if (!cpu_has_virtual_nmis() ||
4850 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
4851 enable_irq_window(vcpu);
4852 return;
4853 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004854
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004855 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4856 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
4857 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4858}
4859
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004860static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004861{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004862 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004863 uint32_t intr;
4864 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004865
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004866 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004867
Avi Kivityfa89a812008-09-01 15:57:51 +03004868 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004869 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004870 int inc_eip = 0;
4871 if (vcpu->arch.interrupt.soft)
4872 inc_eip = vcpu->arch.event_exit_inst_len;
4873 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004874 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004875 return;
4876 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004877 intr = irq | INTR_INFO_VALID_MASK;
4878 if (vcpu->arch.interrupt.soft) {
4879 intr |= INTR_TYPE_SOFT_INTR;
4880 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4881 vmx->vcpu.arch.event_exit_inst_len);
4882 } else
4883 intr |= INTR_TYPE_EXT_INTR;
4884 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03004885}
4886
Sheng Yangf08864b2008-05-15 18:23:25 +08004887static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4888{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004889 struct vcpu_vmx *vmx = to_vmx(vcpu);
4890
Nadav Har'El0b6ac342011-05-25 23:13:36 +03004891 if (is_guest_mode(vcpu))
4892 return;
4893
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004894 if (!cpu_has_virtual_nmis()) {
4895 /*
4896 * Tracking the NMI-blocked state in software is built upon
4897 * finding the next open IRQ window. This, in turn, depends on
4898 * well-behaving guests: They have to keep IRQs disabled at
4899 * least as long as the NMI handler runs. Otherwise we may
4900 * cause NMI nesting, maybe breaking the guest. But as this is
4901 * highly unlikely, we can live with the residual risk.
4902 */
4903 vmx->soft_vnmi_blocked = 1;
4904 vmx->vnmi_blocked_time = 0;
4905 }
4906
Jan Kiszka487b3912008-09-26 09:30:56 +02004907 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02004908 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004909 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004910 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004911 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004912 return;
4913 }
Sheng Yangf08864b2008-05-15 18:23:25 +08004914 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4915 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08004916}
4917
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004918static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
4919{
4920 if (!cpu_has_virtual_nmis())
4921 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02004922 if (to_vmx(vcpu)->nmi_known_unmasked)
4923 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03004924 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004925}
4926
4927static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4928{
4929 struct vcpu_vmx *vmx = to_vmx(vcpu);
4930
4931 if (!cpu_has_virtual_nmis()) {
4932 if (vmx->soft_vnmi_blocked != masked) {
4933 vmx->soft_vnmi_blocked = masked;
4934 vmx->vnmi_blocked_time = 0;
4935 }
4936 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02004937 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004938 if (masked)
4939 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4940 GUEST_INTR_STATE_NMI);
4941 else
4942 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4943 GUEST_INTR_STATE_NMI);
4944 }
4945}
4946
Jan Kiszka2505dc92013-04-14 12:12:47 +02004947static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
4948{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004949 if (to_vmx(vcpu)->nested.nested_run_pending)
4950 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004951
Jan Kiszka2505dc92013-04-14 12:12:47 +02004952 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
4953 return 0;
4954
4955 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4956 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4957 | GUEST_INTR_STATE_NMI));
4958}
4959
Gleb Natapov78646122009-03-23 12:12:11 +02004960static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4961{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004962 return (!to_vmx(vcpu)->nested.nested_run_pending &&
4963 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03004964 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4965 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02004966}
4967
Izik Eiduscbc94022007-10-25 00:29:55 +02004968static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4969{
4970 int ret;
4971 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08004972 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02004973 .guest_phys_addr = addr,
4974 .memory_size = PAGE_SIZE * 3,
4975 .flags = 0,
4976 };
4977
Paolo Bonzini9da0e4d2015-05-18 13:33:16 +02004978 ret = x86_set_memory_region(kvm, &tss_mem);
Izik Eiduscbc94022007-10-25 00:29:55 +02004979 if (ret)
4980 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004981 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004982 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02004983}
4984
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004985static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004986{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004987 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004988 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004989 /*
4990 * Update instruction length as we may reinject the exception
4991 * from user space while in guest debugging mode.
4992 */
4993 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4994 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004995 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004996 return false;
4997 /* fall through */
4998 case DB_VECTOR:
4999 if (vcpu->guest_debug &
5000 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5001 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005002 /* fall through */
5003 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005004 case OF_VECTOR:
5005 case BR_VECTOR:
5006 case UD_VECTOR:
5007 case DF_VECTOR:
5008 case SS_VECTOR:
5009 case GP_VECTOR:
5010 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005011 return true;
5012 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02005013 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005014 return false;
5015}
5016
5017static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5018 int vec, u32 err_code)
5019{
5020 /*
5021 * Instruction with address size override prefix opcode 0x67
5022 * Cause the #SS fault with 0 error code in VM86 mode.
5023 */
5024 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5025 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
5026 if (vcpu->arch.halt_request) {
5027 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005028 return kvm_vcpu_halt(vcpu);
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005029 }
5030 return 1;
5031 }
5032 return 0;
5033 }
5034
5035 /*
5036 * Forward all other exceptions that are valid in real mode.
5037 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5038 * the required debugging infrastructure rework.
5039 */
5040 kvm_queue_exception(vcpu, vec);
5041 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042}
5043
Andi Kleena0861c02009-06-08 17:37:09 +08005044/*
5045 * Trigger machine check on the host. We assume all the MSRs are already set up
5046 * by the CPU and that we still run on the same CPU as the MCE occurred on.
5047 * We pass a fake environment to the machine check handler because we want
5048 * the guest to be always treated like user space, no matter what context
5049 * it used internally.
5050 */
5051static void kvm_machine_check(void)
5052{
5053#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5054 struct pt_regs regs = {
5055 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5056 .flags = X86_EFLAGS_IF,
5057 };
5058
5059 do_machine_check(&regs, 0);
5060#endif
5061}
5062
Avi Kivity851ba692009-08-24 11:10:17 +03005063static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08005064{
5065 /* already handled by vcpu_run */
5066 return 1;
5067}
5068
Avi Kivity851ba692009-08-24 11:10:17 +03005069static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005070{
Avi Kivity1155f762007-11-22 11:30:47 +02005071 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005072 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005073 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005074 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005075 u32 vect_info;
5076 enum emulation_result er;
5077
Avi Kivity1155f762007-11-22 11:30:47 +02005078 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02005079 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005080
Andi Kleena0861c02009-06-08 17:37:09 +08005081 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03005082 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005083
Jan Kiszkae4a41882008-09-26 09:30:46 +02005084 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02005085 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005086
5087 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03005088 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03005089 return 1;
5090 }
5091
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005092 if (is_invalid_opcode(intr_info)) {
Jan Kiszkaae1f5762015-03-09 20:56:43 +01005093 if (is_guest_mode(vcpu)) {
5094 kvm_queue_exception(vcpu, UD_VECTOR);
5095 return 1;
5096 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005097 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005098 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02005099 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005100 return 1;
5101 }
5102
Avi Kivity6aa8b732006-12-10 02:21:36 -08005103 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06005104 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005105 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005106
5107 /*
5108 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5109 * MMIO, it is better to report an internal error.
5110 * See the comments in vmx_handle_exit.
5111 */
5112 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5113 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5114 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5115 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005116 vcpu->run->internal.ndata = 3;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005117 vcpu->run->internal.data[0] = vect_info;
5118 vcpu->run->internal.data[1] = intr_info;
Radim Krčmář80f0e952015-04-02 21:11:05 +02005119 vcpu->run->internal.data[2] = error_code;
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08005120 return 0;
5121 }
5122
Avi Kivity6aa8b732006-12-10 02:21:36 -08005123 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08005124 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02005125 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005126 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005127 trace_kvm_page_fault(cr2, error_code);
5128
Gleb Natapov3298b752009-05-11 13:35:46 +03005129 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03005130 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01005131 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005132 }
5133
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005134 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02005135
5136 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5137 return handle_rmode_exception(vcpu, ex_no, error_code);
5138
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005139 switch (ex_no) {
5140 case DB_VECTOR:
5141 dr6 = vmcs_readl(EXIT_QUALIFICATION);
5142 if (!(vcpu->guest_debug &
5143 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01005144 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005145 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01005146 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
5147 skip_emulated_instruction(vcpu);
5148
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005149 kvm_queue_exception(vcpu, DB_VECTOR);
5150 return 1;
5151 }
5152 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5153 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5154 /* fall through */
5155 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01005156 /*
5157 * Update instruction length as we may reinject #BP from
5158 * user space while in guest debugging mode. Reading it for
5159 * #DB as well causes no harm, it is not used in that case.
5160 */
5161 vmx->vcpu.arch.event_exit_inst_len =
5162 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005163 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03005164 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005165 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5166 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005167 break;
5168 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01005169 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5170 kvm_run->ex.exception = ex_no;
5171 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005172 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005173 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005174 return 0;
5175}
5176
Avi Kivity851ba692009-08-24 11:10:17 +03005177static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005178{
Avi Kivity1165f5f2007-04-19 17:27:43 +03005179 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005180 return 1;
5181}
5182
Avi Kivity851ba692009-08-24 11:10:17 +03005183static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08005184{
Avi Kivity851ba692009-08-24 11:10:17 +03005185 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08005186 return 0;
5187}
Avi Kivity6aa8b732006-12-10 02:21:36 -08005188
Avi Kivity851ba692009-08-24 11:10:17 +03005189static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005190{
He, Qingbfdaab02007-09-12 14:18:28 +08005191 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01005192 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02005193 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005194
He, Qingbfdaab02007-09-12 14:18:28 +08005195 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02005196 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005197 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03005198
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005199 ++vcpu->stat.io_exits;
5200
5201 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01005202 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005203
5204 port = exit_qualification >> 16;
5205 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01005206 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02005207
5208 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005209}
5210
Ingo Molnar102d8322007-02-19 14:37:47 +02005211static void
5212vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5213{
5214 /*
5215 * Patch in the VMCALL instruction:
5216 */
5217 hypercall[0] = 0x0f;
5218 hypercall[1] = 0x01;
5219 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02005220}
5221
Wincy Vanb9c237b2015-02-03 23:56:30 +08005222static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val)
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005223{
5224 unsigned long always_on = VMXON_CR0_ALWAYSON;
Wincy Vanb9c237b2015-02-03 23:56:30 +08005225 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005226
Wincy Vanb9c237b2015-02-03 23:56:30 +08005227 if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high &
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005228 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5229 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5230 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5231 return (val & always_on) == always_on;
5232}
5233
Guo Chao0fa06072012-06-28 15:16:19 +08005234/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005235static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5236{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005237 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005238 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5239 unsigned long orig_val = val;
5240
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005241 /*
5242 * We get here when L2 changed cr0 in a way that did not change
5243 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005244 * but did change L0 shadowed bits. So we first calculate the
5245 * effective cr0 value that L1 would like to write into the
5246 * hardware. It consists of the L2-owned bits from the new
5247 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005248 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005249 val = (val & ~vmcs12->cr0_guest_host_mask) |
5250 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5251
Wincy Vanb9c237b2015-02-03 23:56:30 +08005252 if (!nested_cr0_valid(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005253 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005254
5255 if (kvm_set_cr0(vcpu, val))
5256 return 1;
5257 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005258 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005259 } else {
5260 if (to_vmx(vcpu)->nested.vmxon &&
5261 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5262 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005263 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005264 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005265}
5266
5267static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5268{
5269 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005270 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5271 unsigned long orig_val = val;
5272
5273 /* analogously to handle_set_cr0 */
5274 val = (val & ~vmcs12->cr4_guest_host_mask) |
5275 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5276 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005277 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005278 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005279 return 0;
5280 } else
5281 return kvm_set_cr4(vcpu, val);
5282}
5283
5284/* called to set cr0 as approriate for clts instruction exit. */
5285static void handle_clts(struct kvm_vcpu *vcpu)
5286{
5287 if (is_guest_mode(vcpu)) {
5288 /*
5289 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5290 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5291 * just pretend it's off (also in arch.cr0 for fpu_activate).
5292 */
5293 vmcs_writel(CR0_READ_SHADOW,
5294 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5295 vcpu->arch.cr0 &= ~X86_CR0_TS;
5296 } else
5297 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5298}
5299
Avi Kivity851ba692009-08-24 11:10:17 +03005300static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005301{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005302 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005303 int cr;
5304 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005305 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005306
He, Qingbfdaab02007-09-12 14:18:28 +08005307 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005308 cr = exit_qualification & 15;
5309 reg = (exit_qualification >> 8) & 15;
5310 switch ((exit_qualification >> 4) & 3) {
5311 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005312 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005313 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005314 switch (cr) {
5315 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005316 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005317 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005318 return 1;
5319 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005320 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005321 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005322 return 1;
5323 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005324 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005325 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005326 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005327 case 8: {
5328 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005329 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005330 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005331 kvm_complete_insn_gp(vcpu, err);
Paolo Bonzini35754c92015-07-29 12:05:37 +02005332 if (lapic_in_kernel(vcpu))
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005333 return 1;
5334 if (cr8_prev <= cr8)
5335 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005336 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005337 return 0;
5338 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005339 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005340 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005341 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005342 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005343 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005344 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005345 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005346 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005347 case 1: /*mov from cr*/
5348 switch (cr) {
5349 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005350 val = kvm_read_cr3(vcpu);
5351 kvm_register_write(vcpu, reg, val);
5352 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005353 skip_emulated_instruction(vcpu);
5354 return 1;
5355 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005356 val = kvm_get_cr8(vcpu);
5357 kvm_register_write(vcpu, reg, val);
5358 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005359 skip_emulated_instruction(vcpu);
5360 return 1;
5361 }
5362 break;
5363 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005364 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005365 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005366 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005367
5368 skip_emulated_instruction(vcpu);
5369 return 1;
5370 default:
5371 break;
5372 }
Avi Kivity851ba692009-08-24 11:10:17 +03005373 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005374 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005375 (int)(exit_qualification >> 4) & 3, cr);
5376 return 0;
5377}
5378
Avi Kivity851ba692009-08-24 11:10:17 +03005379static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005380{
He, Qingbfdaab02007-09-12 14:18:28 +08005381 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005382 int dr, dr7, reg;
5383
5384 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5385 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5386
5387 /* First, if DR does not exist, trigger UD */
5388 if (!kvm_require_dr(vcpu, dr))
5389 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005390
Jan Kiszkaf2483412010-01-20 18:20:20 +01005391 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005392 if (!kvm_require_cpl(vcpu, 0))
5393 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005394 dr7 = vmcs_readl(GUEST_DR7);
5395 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005396 /*
5397 * As the vm-exit takes precedence over the debug trap, we
5398 * need to emulate the latter, either for the host or the
5399 * guest debugging itself.
5400 */
5401 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005402 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005403 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005404 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005405 vcpu->run->debug.arch.exception = DB_VECTOR;
5406 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005407 return 0;
5408 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02005409 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005410 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005411 kvm_queue_exception(vcpu, DB_VECTOR);
5412 return 1;
5413 }
5414 }
5415
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005416 if (vcpu->guest_debug == 0) {
5417 u32 cpu_based_vm_exec_control;
5418
5419 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5420 cpu_based_vm_exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5421 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5422
5423 /*
5424 * No more DR vmexits; force a reload of the debug registers
5425 * and reenter on this instruction. The next vmexit will
5426 * retrieve the full state of the debug registers.
5427 */
5428 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5429 return 1;
5430 }
5431
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005432 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5433 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005434 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005435
5436 if (kvm_get_dr(vcpu, dr, &val))
5437 return 1;
5438 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005439 } else
Nadav Amit57773922014-06-18 17:19:23 +03005440 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005441 return 1;
5442
Avi Kivity6aa8b732006-12-10 02:21:36 -08005443 skip_emulated_instruction(vcpu);
5444 return 1;
5445}
5446
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005447static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5448{
5449 return vcpu->arch.dr6;
5450}
5451
5452static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5453{
5454}
5455
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005456static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5457{
5458 u32 cpu_based_vm_exec_control;
5459
5460 get_debugreg(vcpu->arch.db[0], 0);
5461 get_debugreg(vcpu->arch.db[1], 1);
5462 get_debugreg(vcpu->arch.db[2], 2);
5463 get_debugreg(vcpu->arch.db[3], 3);
5464 get_debugreg(vcpu->arch.dr6, 6);
5465 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5466
5467 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
5468
5469 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5470 cpu_based_vm_exec_control |= CPU_BASED_MOV_DR_EXITING;
5471 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5472}
5473
Gleb Natapov020df072010-04-13 10:05:23 +03005474static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5475{
5476 vmcs_writel(GUEST_DR7, val);
5477}
5478
Avi Kivity851ba692009-08-24 11:10:17 +03005479static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005480{
Avi Kivity06465c52007-02-28 20:46:53 +02005481 kvm_emulate_cpuid(vcpu);
5482 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005483}
5484
Avi Kivity851ba692009-08-24 11:10:17 +03005485static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005486{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005487 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005488 struct msr_data msr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005489
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005490 msr_info.index = ecx;
5491 msr_info.host_initiated = false;
5492 if (vmx_get_msr(vcpu, &msr_info)) {
Avi Kivity59200272010-01-25 19:47:02 +02005493 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005494 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005495 return 1;
5496 }
5497
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005498 trace_kvm_msr_read(ecx, msr_info.data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005499
Avi Kivity6aa8b732006-12-10 02:21:36 -08005500 /* FIXME: handling of bits 32:63 of rax, rdx */
Paolo Bonzini609e36d2015-04-08 15:30:38 +02005501 vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u;
5502 vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005503 skip_emulated_instruction(vcpu);
5504 return 1;
5505}
5506
Avi Kivity851ba692009-08-24 11:10:17 +03005507static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005508{
Will Auld8fe8ab42012-11-29 12:42:12 -08005509 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005510 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5511 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5512 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005513
Will Auld8fe8ab42012-11-29 12:42:12 -08005514 msr.data = data;
5515 msr.index = ecx;
5516 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03005517 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005518 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005519 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005520 return 1;
5521 }
5522
Avi Kivity59200272010-01-25 19:47:02 +02005523 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005524 skip_emulated_instruction(vcpu);
5525 return 1;
5526}
5527
Avi Kivity851ba692009-08-24 11:10:17 +03005528static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005529{
Avi Kivity3842d132010-07-27 12:30:24 +03005530 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005531 return 1;
5532}
5533
Avi Kivity851ba692009-08-24 11:10:17 +03005534static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005535{
Eddie Dong85f455f2007-07-06 12:20:49 +03005536 u32 cpu_based_vm_exec_control;
5537
5538 /* clear pending irq */
5539 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5540 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5541 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005542
Avi Kivity3842d132010-07-27 12:30:24 +03005543 kvm_make_request(KVM_REQ_EVENT, vcpu);
5544
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005545 ++vcpu->stat.irq_window_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005546 return 1;
5547}
5548
Avi Kivity851ba692009-08-24 11:10:17 +03005549static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005550{
Avi Kivityd3bef152007-06-05 15:53:05 +03005551 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005552}
5553
Avi Kivity851ba692009-08-24 11:10:17 +03005554static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005555{
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005556 kvm_emulate_hypercall(vcpu);
5557 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02005558}
5559
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005560static int handle_invd(struct kvm_vcpu *vcpu)
5561{
Andre Przywara51d8b662010-12-21 11:12:02 +01005562 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005563}
5564
Avi Kivity851ba692009-08-24 11:10:17 +03005565static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005566{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005567 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005568
5569 kvm_mmu_invlpg(vcpu, exit_qualification);
5570 skip_emulated_instruction(vcpu);
5571 return 1;
5572}
5573
Avi Kivityfee84b02011-11-10 14:57:25 +02005574static int handle_rdpmc(struct kvm_vcpu *vcpu)
5575{
5576 int err;
5577
5578 err = kvm_rdpmc(vcpu);
5579 kvm_complete_insn_gp(vcpu, err);
5580
5581 return 1;
5582}
5583
Avi Kivity851ba692009-08-24 11:10:17 +03005584static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005585{
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005586 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005587 return 1;
5588}
5589
Dexuan Cui2acf9232010-06-10 11:27:12 +08005590static int handle_xsetbv(struct kvm_vcpu *vcpu)
5591{
5592 u64 new_bv = kvm_read_edx_eax(vcpu);
5593 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5594
5595 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5596 skip_emulated_instruction(vcpu);
5597 return 1;
5598}
5599
Wanpeng Lif53cd632014-12-02 19:14:58 +08005600static int handle_xsaves(struct kvm_vcpu *vcpu)
5601{
5602 skip_emulated_instruction(vcpu);
5603 WARN(1, "this should never happen\n");
5604 return 1;
5605}
5606
5607static int handle_xrstors(struct kvm_vcpu *vcpu)
5608{
5609 skip_emulated_instruction(vcpu);
5610 WARN(1, "this should never happen\n");
5611 return 1;
5612}
5613
Avi Kivity851ba692009-08-24 11:10:17 +03005614static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005615{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005616 if (likely(fasteoi)) {
5617 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5618 int access_type, offset;
5619
5620 access_type = exit_qualification & APIC_ACCESS_TYPE;
5621 offset = exit_qualification & APIC_ACCESS_OFFSET;
5622 /*
5623 * Sane guest uses MOV to write EOI, with written value
5624 * not cared. So make a short-circuit here by avoiding
5625 * heavy instruction emulation.
5626 */
5627 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5628 (offset == APIC_EOI)) {
5629 kvm_lapic_set_eoi(vcpu);
5630 skip_emulated_instruction(vcpu);
5631 return 1;
5632 }
5633 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005634 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005635}
5636
Yang Zhangc7c9c562013-01-25 10:18:51 +08005637static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5638{
5639 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5640 int vector = exit_qualification & 0xff;
5641
5642 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5643 kvm_apic_set_eoi_accelerated(vcpu, vector);
5644 return 1;
5645}
5646
Yang Zhang83d4c282013-01-25 10:18:49 +08005647static int handle_apic_write(struct kvm_vcpu *vcpu)
5648{
5649 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5650 u32 offset = exit_qualification & 0xfff;
5651
5652 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5653 kvm_apic_write_nodecode(vcpu, offset);
5654 return 1;
5655}
5656
Avi Kivity851ba692009-08-24 11:10:17 +03005657static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005658{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005659 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005660 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005661 bool has_error_code = false;
5662 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005663 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005664 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005665
5666 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005667 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005668 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005669
5670 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5671
5672 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005673 if (reason == TASK_SWITCH_GATE && idt_v) {
5674 switch (type) {
5675 case INTR_TYPE_NMI_INTR:
5676 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005677 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005678 break;
5679 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005680 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005681 kvm_clear_interrupt_queue(vcpu);
5682 break;
5683 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005684 if (vmx->idt_vectoring_info &
5685 VECTORING_INFO_DELIVER_CODE_MASK) {
5686 has_error_code = true;
5687 error_code =
5688 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5689 }
5690 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005691 case INTR_TYPE_SOFT_EXCEPTION:
5692 kvm_clear_exception_queue(vcpu);
5693 break;
5694 default:
5695 break;
5696 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005697 }
Izik Eidus37817f22008-03-24 23:14:53 +02005698 tss_selector = exit_qualification;
5699
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005700 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5701 type != INTR_TYPE_EXT_INTR &&
5702 type != INTR_TYPE_NMI_INTR))
5703 skip_emulated_instruction(vcpu);
5704
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005705 if (kvm_task_switch(vcpu, tss_selector,
5706 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
5707 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03005708 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5709 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5710 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005711 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03005712 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005713
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005714 /*
5715 * TODO: What about debug traps on tss switch?
5716 * Are we supposed to inject them and update dr6?
5717 */
5718
5719 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02005720}
5721
Avi Kivity851ba692009-08-24 11:10:17 +03005722static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005723{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005724 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005725 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005726 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005727 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08005728
Sheng Yangf9c617f2009-03-25 10:08:52 +08005729 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08005730
Sheng Yang14394422008-04-28 12:24:45 +08005731 gla_validity = (exit_qualification >> 7) & 0x3;
5732 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
5733 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
5734 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
5735 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08005736 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08005737 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
5738 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03005739 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5740 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03005741 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08005742 }
5743
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005744 /*
5745 * EPT violation happened while executing iret from NMI,
5746 * "blocked by NMI" bit has to be set before next VM entry.
5747 * There are errata that may cause this bit to not be set:
5748 * AAK134, BY25.
5749 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005750 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
5751 cpu_has_virtual_nmis() &&
5752 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005753 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5754
Sheng Yang14394422008-04-28 12:24:45 +08005755 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005756 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005757
5758 /* It is a write fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08005759 error_code = exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03005760 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08005761 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005762 /* ept page table is present? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08005763 error_code |= (exit_qualification >> 3) & PFERR_PRESENT_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005764
Yang Zhang25d92082013-08-06 12:00:32 +03005765 vcpu->arch.exit_qualification = exit_qualification;
5766
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005767 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005768}
5769
Avi Kivity851ba692009-08-24 11:10:17 +03005770static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005771{
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08005772 int ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005773 gpa_t gpa;
5774
5775 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00005776 if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005777 skip_emulated_instruction(vcpu);
Jason Wang931c33b2015-09-15 14:41:58 +08005778 trace_kvm_fast_mmio(gpa);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005779 return 1;
5780 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005781
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005782 ret = handle_mmio_page_fault_common(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005783 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005784 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
5785 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005786
5787 if (unlikely(ret == RET_MMIO_PF_INVALID))
5788 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
5789
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005790 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005791 return 1;
5792
5793 /* It is the real ept misconfig */
Xiao Guangrongf735d4a2015-08-05 12:04:27 +08005794 WARN_ON(1);
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005795
Avi Kivity851ba692009-08-24 11:10:17 +03005796 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5797 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005798
5799 return 0;
5800}
5801
Avi Kivity851ba692009-08-24 11:10:17 +03005802static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005803{
5804 u32 cpu_based_vm_exec_control;
5805
5806 /* clear pending NMI */
5807 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5808 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
5809 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5810 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005811 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005812
5813 return 1;
5814}
5815
Mohammed Gamal80ced182009-09-01 12:48:18 +02005816static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005817{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005818 struct vcpu_vmx *vmx = to_vmx(vcpu);
5819 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005820 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02005821 u32 cpu_exec_ctrl;
5822 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005823 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005824
5825 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5826 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005827
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005828 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03005829 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005830 return handle_interrupt_window(&vmx->vcpu);
5831
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005832 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
5833 return 1;
5834
Gleb Natapov991eebf2013-04-11 12:10:51 +03005835 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005836
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005837 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02005838 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005839 ret = 0;
5840 goto out;
5841 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005842
Avi Kivityde5f70e2012-06-12 20:22:28 +03005843 if (err != EMULATE_DONE) {
5844 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5845 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5846 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005847 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03005848 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005849
Gleb Natapov8d76c492013-05-08 18:38:44 +03005850 if (vcpu->arch.halt_request) {
5851 vcpu->arch.halt_request = 0;
Joel Schopp5cb56052015-03-02 13:43:31 -06005852 ret = kvm_vcpu_halt(vcpu);
Gleb Natapov8d76c492013-05-08 18:38:44 +03005853 goto out;
5854 }
5855
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005856 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02005857 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005858 if (need_resched())
5859 schedule();
5860 }
5861
Mohammed Gamal80ced182009-09-01 12:48:18 +02005862out:
5863 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005864}
5865
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005866static int __grow_ple_window(int val)
5867{
5868 if (ple_window_grow < 1)
5869 return ple_window;
5870
5871 val = min(val, ple_window_actual_max);
5872
5873 if (ple_window_grow < ple_window)
5874 val *= ple_window_grow;
5875 else
5876 val += ple_window_grow;
5877
5878 return val;
5879}
5880
5881static int __shrink_ple_window(int val, int modifier, int minimum)
5882{
5883 if (modifier < 1)
5884 return ple_window;
5885
5886 if (modifier < ple_window)
5887 val /= modifier;
5888 else
5889 val -= modifier;
5890
5891 return max(val, minimum);
5892}
5893
5894static void grow_ple_window(struct kvm_vcpu *vcpu)
5895{
5896 struct vcpu_vmx *vmx = to_vmx(vcpu);
5897 int old = vmx->ple_window;
5898
5899 vmx->ple_window = __grow_ple_window(old);
5900
5901 if (vmx->ple_window != old)
5902 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02005903
5904 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005905}
5906
5907static void shrink_ple_window(struct kvm_vcpu *vcpu)
5908{
5909 struct vcpu_vmx *vmx = to_vmx(vcpu);
5910 int old = vmx->ple_window;
5911
5912 vmx->ple_window = __shrink_ple_window(old,
5913 ple_window_shrink, ple_window);
5914
5915 if (vmx->ple_window != old)
5916 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02005917
5918 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005919}
5920
5921/*
5922 * ple_window_actual_max is computed to be one grow_ple_window() below
5923 * ple_window_max. (See __grow_ple_window for the reason.)
5924 * This prevents overflows, because ple_window_max is int.
5925 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
5926 * this process.
5927 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
5928 */
5929static void update_ple_window_actual_max(void)
5930{
5931 ple_window_actual_max =
5932 __shrink_ple_window(max(ple_window_max, ple_window),
5933 ple_window_grow, INT_MIN);
5934}
5935
Tiejun Chenf2c76482014-10-28 10:14:47 +08005936static __init int hardware_setup(void)
5937{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08005938 int r = -ENOMEM, i, msr;
5939
5940 rdmsrl_safe(MSR_EFER, &host_efer);
5941
5942 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
5943 kvm_define_shared_msr(i, vmx_msr_index[i]);
5944
5945 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
5946 if (!vmx_io_bitmap_a)
5947 return r;
5948
5949 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
5950 if (!vmx_io_bitmap_b)
5951 goto out;
5952
5953 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
5954 if (!vmx_msr_bitmap_legacy)
5955 goto out1;
5956
5957 vmx_msr_bitmap_legacy_x2apic =
5958 (unsigned long *)__get_free_page(GFP_KERNEL);
5959 if (!vmx_msr_bitmap_legacy_x2apic)
5960 goto out2;
5961
5962 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
5963 if (!vmx_msr_bitmap_longmode)
5964 goto out3;
5965
5966 vmx_msr_bitmap_longmode_x2apic =
5967 (unsigned long *)__get_free_page(GFP_KERNEL);
5968 if (!vmx_msr_bitmap_longmode_x2apic)
5969 goto out4;
Wincy Van3af18d92015-02-03 23:49:31 +08005970
5971 if (nested) {
5972 vmx_msr_bitmap_nested =
5973 (unsigned long *)__get_free_page(GFP_KERNEL);
5974 if (!vmx_msr_bitmap_nested)
5975 goto out5;
5976 }
5977
Tiejun Chen34a1cd62014-10-28 10:14:48 +08005978 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
5979 if (!vmx_vmread_bitmap)
Wincy Van3af18d92015-02-03 23:49:31 +08005980 goto out6;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08005981
5982 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
5983 if (!vmx_vmwrite_bitmap)
Wincy Van3af18d92015-02-03 23:49:31 +08005984 goto out7;
Tiejun Chen34a1cd62014-10-28 10:14:48 +08005985
5986 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
5987 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
5988
5989 /*
5990 * Allow direct access to the PC debug port (it is often used for I/O
5991 * delays, but the vmexits simply slow things down).
5992 */
5993 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
5994 clear_bit(0x80, vmx_io_bitmap_a);
5995
5996 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
5997
5998 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
5999 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Wincy Van3af18d92015-02-03 23:49:31 +08006000 if (nested)
6001 memset(vmx_msr_bitmap_nested, 0xff, PAGE_SIZE);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006002
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006003 if (setup_vmcs_config(&vmcs_config) < 0) {
6004 r = -EIO;
Wincy Van3af18d92015-02-03 23:49:31 +08006005 goto out8;
Tiejun Chenbaa03522014-12-23 16:21:11 +08006006 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08006007
6008 if (boot_cpu_has(X86_FEATURE_NX))
6009 kvm_enable_efer_bits(EFER_NX);
6010
6011 if (!cpu_has_vmx_vpid())
6012 enable_vpid = 0;
6013 if (!cpu_has_vmx_shadow_vmcs())
6014 enable_shadow_vmcs = 0;
6015 if (enable_shadow_vmcs)
6016 init_vmcs_shadow_fields();
6017
6018 if (!cpu_has_vmx_ept() ||
6019 !cpu_has_vmx_ept_4levels()) {
6020 enable_ept = 0;
6021 enable_unrestricted_guest = 0;
6022 enable_ept_ad_bits = 0;
6023 }
6024
6025 if (!cpu_has_vmx_ept_ad_bits())
6026 enable_ept_ad_bits = 0;
6027
6028 if (!cpu_has_vmx_unrestricted_guest())
6029 enable_unrestricted_guest = 0;
6030
Paolo Bonziniad15a292015-01-30 16:18:49 +01006031 if (!cpu_has_vmx_flexpriority())
Tiejun Chenf2c76482014-10-28 10:14:47 +08006032 flexpriority_enabled = 0;
6033
Paolo Bonziniad15a292015-01-30 16:18:49 +01006034 /*
6035 * set_apic_access_page_addr() is used to reload apic access
6036 * page upon invalidation. No need to do anything if not
6037 * using the APIC_ACCESS_ADDR VMCS field.
6038 */
6039 if (!flexpriority_enabled)
Tiejun Chenf2c76482014-10-28 10:14:47 +08006040 kvm_x86_ops->set_apic_access_page_addr = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006041
6042 if (!cpu_has_vmx_tpr_shadow())
6043 kvm_x86_ops->update_cr8_intercept = NULL;
6044
6045 if (enable_ept && !cpu_has_vmx_ept_2m_page())
6046 kvm_disable_largepages();
6047
6048 if (!cpu_has_vmx_ple())
6049 ple_gap = 0;
6050
6051 if (!cpu_has_vmx_apicv())
6052 enable_apicv = 0;
6053
6054 if (enable_apicv)
6055 kvm_x86_ops->update_cr8_intercept = NULL;
6056 else {
6057 kvm_x86_ops->hwapic_irr_update = NULL;
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01006058 kvm_x86_ops->hwapic_isr_update = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006059 kvm_x86_ops->deliver_posted_interrupt = NULL;
6060 kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy;
6061 }
6062
Tiejun Chenbaa03522014-12-23 16:21:11 +08006063 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
6064 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
6065 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
6066 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
6067 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
6068 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
6069 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
6070
6071 memcpy(vmx_msr_bitmap_legacy_x2apic,
6072 vmx_msr_bitmap_legacy, PAGE_SIZE);
6073 memcpy(vmx_msr_bitmap_longmode_x2apic,
6074 vmx_msr_bitmap_longmode, PAGE_SIZE);
6075
Wanpeng Li04bb92e2015-09-16 19:31:11 +08006076 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
6077
Tiejun Chenbaa03522014-12-23 16:21:11 +08006078 if (enable_apicv) {
6079 for (msr = 0x800; msr <= 0x8ff; msr++)
6080 vmx_disable_intercept_msr_read_x2apic(msr);
6081
6082 /* According SDM, in x2apic mode, the whole id reg is used.
6083 * But in KVM, it only use the highest eight bits. Need to
6084 * intercept it */
6085 vmx_enable_intercept_msr_read_x2apic(0x802);
6086 /* TMCCT */
6087 vmx_enable_intercept_msr_read_x2apic(0x839);
6088 /* TPR */
6089 vmx_disable_intercept_msr_write_x2apic(0x808);
6090 /* EOI */
6091 vmx_disable_intercept_msr_write_x2apic(0x80b);
6092 /* SELF-IPI */
6093 vmx_disable_intercept_msr_write_x2apic(0x83f);
6094 }
6095
6096 if (enable_ept) {
6097 kvm_mmu_set_mask_ptes(0ull,
6098 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
6099 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
6100 0ull, VMX_EPT_EXECUTABLE_MASK);
6101 ept_set_mmio_spte_mask();
6102 kvm_enable_tdp();
6103 } else
6104 kvm_disable_tdp();
6105
6106 update_ple_window_actual_max();
6107
Kai Huang843e4332015-01-28 10:54:28 +08006108 /*
6109 * Only enable PML when hardware supports PML feature, and both EPT
6110 * and EPT A/D bit features are enabled -- PML depends on them to work.
6111 */
6112 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
6113 enable_pml = 0;
6114
6115 if (!enable_pml) {
6116 kvm_x86_ops->slot_enable_log_dirty = NULL;
6117 kvm_x86_ops->slot_disable_log_dirty = NULL;
6118 kvm_x86_ops->flush_log_dirty = NULL;
6119 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
6120 }
6121
Tiejun Chenf2c76482014-10-28 10:14:47 +08006122 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006123
Wincy Van3af18d92015-02-03 23:49:31 +08006124out8:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006125 free_page((unsigned long)vmx_vmwrite_bitmap);
Wincy Van3af18d92015-02-03 23:49:31 +08006126out7:
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006127 free_page((unsigned long)vmx_vmread_bitmap);
Wincy Van3af18d92015-02-03 23:49:31 +08006128out6:
6129 if (nested)
6130 free_page((unsigned long)vmx_msr_bitmap_nested);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006131out5:
6132 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
6133out4:
6134 free_page((unsigned long)vmx_msr_bitmap_longmode);
6135out3:
6136 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
6137out2:
6138 free_page((unsigned long)vmx_msr_bitmap_legacy);
6139out1:
6140 free_page((unsigned long)vmx_io_bitmap_b);
6141out:
6142 free_page((unsigned long)vmx_io_bitmap_a);
6143
6144 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08006145}
6146
6147static __exit void hardware_unsetup(void)
6148{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006149 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
6150 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
6151 free_page((unsigned long)vmx_msr_bitmap_legacy);
6152 free_page((unsigned long)vmx_msr_bitmap_longmode);
6153 free_page((unsigned long)vmx_io_bitmap_b);
6154 free_page((unsigned long)vmx_io_bitmap_a);
6155 free_page((unsigned long)vmx_vmwrite_bitmap);
6156 free_page((unsigned long)vmx_vmread_bitmap);
Wincy Van3af18d92015-02-03 23:49:31 +08006157 if (nested)
6158 free_page((unsigned long)vmx_msr_bitmap_nested);
Tiejun Chen34a1cd62014-10-28 10:14:48 +08006159
Tiejun Chenf2c76482014-10-28 10:14:47 +08006160 free_kvm_area();
6161}
6162
Avi Kivity6aa8b732006-12-10 02:21:36 -08006163/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006164 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6165 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6166 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006167static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006168{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006169 if (ple_gap)
6170 grow_ple_window(vcpu);
6171
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006172 skip_emulated_instruction(vcpu);
6173 kvm_vcpu_on_spin(vcpu);
6174
6175 return 1;
6176}
6177
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006178static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006179{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006180 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006181 return 1;
6182}
6183
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006184static int handle_mwait(struct kvm_vcpu *vcpu)
6185{
6186 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6187 return handle_nop(vcpu);
6188}
6189
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03006190static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6191{
6192 return 1;
6193}
6194
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006195static int handle_monitor(struct kvm_vcpu *vcpu)
6196{
6197 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6198 return handle_nop(vcpu);
6199}
6200
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006201/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006202 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6203 * We could reuse a single VMCS for all the L2 guests, but we also want the
6204 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6205 * allows keeping them loaded on the processor, and in the future will allow
6206 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6207 * every entry if they never change.
6208 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6209 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6210 *
6211 * The following functions allocate and free a vmcs02 in this pool.
6212 */
6213
6214/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6215static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6216{
6217 struct vmcs02_list *item;
6218 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6219 if (item->vmptr == vmx->nested.current_vmptr) {
6220 list_move(&item->list, &vmx->nested.vmcs02_pool);
6221 return &item->vmcs02;
6222 }
6223
6224 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
6225 /* Recycle the least recently used VMCS. */
6226 item = list_entry(vmx->nested.vmcs02_pool.prev,
6227 struct vmcs02_list, list);
6228 item->vmptr = vmx->nested.current_vmptr;
6229 list_move(&item->list, &vmx->nested.vmcs02_pool);
6230 return &item->vmcs02;
6231 }
6232
6233 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02006234 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006235 if (!item)
6236 return NULL;
6237 item->vmcs02.vmcs = alloc_vmcs();
6238 if (!item->vmcs02.vmcs) {
6239 kfree(item);
6240 return NULL;
6241 }
6242 loaded_vmcs_init(&item->vmcs02);
6243 item->vmptr = vmx->nested.current_vmptr;
6244 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
6245 vmx->nested.vmcs02_num++;
6246 return &item->vmcs02;
6247}
6248
6249/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
6250static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
6251{
6252 struct vmcs02_list *item;
6253 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6254 if (item->vmptr == vmptr) {
6255 free_loaded_vmcs(&item->vmcs02);
6256 list_del(&item->list);
6257 kfree(item);
6258 vmx->nested.vmcs02_num--;
6259 return;
6260 }
6261}
6262
6263/*
6264 * Free all VMCSs saved for this vcpu, except the one pointed by
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006265 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
6266 * must be &vmx->vmcs01.
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006267 */
6268static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
6269{
6270 struct vmcs02_list *item, *n;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006271
6272 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006273 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006274 /*
6275 * Something will leak if the above WARN triggers. Better than
6276 * a use-after-free.
6277 */
6278 if (vmx->loaded_vmcs == &item->vmcs02)
6279 continue;
6280
6281 free_loaded_vmcs(&item->vmcs02);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006282 list_del(&item->list);
6283 kfree(item);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006284 vmx->nested.vmcs02_num--;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006285 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006286}
6287
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006288/*
6289 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6290 * set the success or error code of an emulated VMX instruction, as specified
6291 * by Vol 2B, VMX Instruction Reference, "Conventions".
6292 */
6293static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6294{
6295 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6296 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6297 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6298}
6299
6300static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6301{
6302 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6303 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6304 X86_EFLAGS_SF | X86_EFLAGS_OF))
6305 | X86_EFLAGS_CF);
6306}
6307
Abel Gordon145c28d2013-04-18 14:36:55 +03006308static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006309 u32 vm_instruction_error)
6310{
6311 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6312 /*
6313 * failValid writes the error number to the current VMCS, which
6314 * can't be done there isn't a current VMCS.
6315 */
6316 nested_vmx_failInvalid(vcpu);
6317 return;
6318 }
6319 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6320 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6321 X86_EFLAGS_SF | X86_EFLAGS_OF))
6322 | X86_EFLAGS_ZF);
6323 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6324 /*
6325 * We don't need to force a shadow sync because
6326 * VM_INSTRUCTION_ERROR is not shadowed
6327 */
6328}
Abel Gordon145c28d2013-04-18 14:36:55 +03006329
Wincy Vanff651cb2014-12-11 08:52:58 +03006330static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6331{
6332 /* TODO: not to reset guest simply here. */
6333 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6334 pr_warn("kvm: nested vmx abort, indicator %d\n", indicator);
6335}
6336
Jan Kiszkaf41245002014-03-07 20:03:13 +01006337static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6338{
6339 struct vcpu_vmx *vmx =
6340 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6341
6342 vmx->nested.preemption_timer_expired = true;
6343 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6344 kvm_vcpu_kick(&vmx->vcpu);
6345
6346 return HRTIMER_NORESTART;
6347}
6348
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006349/*
Bandan Das19677e32014-05-06 02:19:15 -04006350 * Decode the memory-address operand of a vmx instruction, as recorded on an
6351 * exit caused by such an instruction (run by a guest hypervisor).
6352 * On success, returns 0. When the operand is invalid, returns 1 and throws
6353 * #UD or #GP.
6354 */
6355static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6356 unsigned long exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006357 u32 vmx_instruction_info, bool wr, gva_t *ret)
Bandan Das19677e32014-05-06 02:19:15 -04006358{
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006359 gva_t off;
6360 bool exn;
6361 struct kvm_segment s;
6362
Bandan Das19677e32014-05-06 02:19:15 -04006363 /*
6364 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6365 * Execution", on an exit, vmx_instruction_info holds most of the
6366 * addressing components of the operand. Only the displacement part
6367 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6368 * For how an actual address is calculated from all these components,
6369 * refer to Vol. 1, "Operand Addressing".
6370 */
6371 int scaling = vmx_instruction_info & 3;
6372 int addr_size = (vmx_instruction_info >> 7) & 7;
6373 bool is_reg = vmx_instruction_info & (1u << 10);
6374 int seg_reg = (vmx_instruction_info >> 15) & 7;
6375 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6376 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6377 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6378 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6379
6380 if (is_reg) {
6381 kvm_queue_exception(vcpu, UD_VECTOR);
6382 return 1;
6383 }
6384
6385 /* Addr = segment_base + offset */
6386 /* offset = base + [index * scale] + displacement */
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006387 off = exit_qualification; /* holds the displacement */
Bandan Das19677e32014-05-06 02:19:15 -04006388 if (base_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006389 off += kvm_register_read(vcpu, base_reg);
Bandan Das19677e32014-05-06 02:19:15 -04006390 if (index_is_valid)
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006391 off += kvm_register_read(vcpu, index_reg)<<scaling;
6392 vmx_get_segment(vcpu, &s, seg_reg);
6393 *ret = s.base + off;
Bandan Das19677e32014-05-06 02:19:15 -04006394
6395 if (addr_size == 1) /* 32 bit */
6396 *ret &= 0xffffffff;
6397
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006398 /* Checks for #GP/#SS exceptions. */
6399 exn = false;
6400 if (is_protmode(vcpu)) {
6401 /* Protected mode: apply checks for segment validity in the
6402 * following order:
6403 * - segment type check (#GP(0) may be thrown)
6404 * - usability check (#GP(0)/#SS(0))
6405 * - limit check (#GP(0)/#SS(0))
6406 */
6407 if (wr)
6408 /* #GP(0) if the destination operand is located in a
6409 * read-only data segment or any code segment.
6410 */
6411 exn = ((s.type & 0xa) == 0 || (s.type & 8));
6412 else
6413 /* #GP(0) if the source operand is located in an
6414 * execute-only code segment
6415 */
6416 exn = ((s.type & 0xa) == 8);
6417 }
6418 if (exn) {
6419 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
6420 return 1;
6421 }
6422 if (is_long_mode(vcpu)) {
6423 /* Long mode: #GP(0)/#SS(0) if the memory address is in a
6424 * non-canonical form. This is an only check for long mode.
6425 */
6426 exn = is_noncanonical_address(*ret);
6427 } else if (is_protmode(vcpu)) {
6428 /* Protected mode: #GP(0)/#SS(0) if the segment is unusable.
6429 */
6430 exn = (s.unusable != 0);
6431 /* Protected mode: #GP(0)/#SS(0) if the memory
6432 * operand is outside the segment limit.
6433 */
6434 exn = exn || (off + sizeof(u64) > s.limit);
6435 }
6436 if (exn) {
6437 kvm_queue_exception_e(vcpu,
6438 seg_reg == VCPU_SREG_SS ?
6439 SS_VECTOR : GP_VECTOR,
6440 0);
6441 return 1;
6442 }
6443
Bandan Das19677e32014-05-06 02:19:15 -04006444 return 0;
6445}
6446
6447/*
Bandan Das3573e222014-05-06 02:19:16 -04006448 * This function performs the various checks including
6449 * - if it's 4KB aligned
6450 * - No bits beyond the physical address width are set
6451 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006452 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006453 */
Bandan Das4291b582014-05-06 02:19:18 -04006454static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6455 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006456{
6457 gva_t gva;
6458 gpa_t vmptr;
6459 struct x86_exception e;
6460 struct page *page;
6461 struct vcpu_vmx *vmx = to_vmx(vcpu);
6462 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6463
6464 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006465 vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva))
Bandan Das3573e222014-05-06 02:19:16 -04006466 return 1;
6467
6468 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6469 sizeof(vmptr), &e)) {
6470 kvm_inject_page_fault(vcpu, &e);
6471 return 1;
6472 }
6473
6474 switch (exit_reason) {
6475 case EXIT_REASON_VMON:
6476 /*
6477 * SDM 3: 24.11.5
6478 * The first 4 bytes of VMXON region contain the supported
6479 * VMCS revision identifier
6480 *
6481 * Note - IA32_VMX_BASIC[48] will never be 1
6482 * for the nested case;
6483 * which replaces physical address width with 32
6484 *
6485 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006486 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04006487 nested_vmx_failInvalid(vcpu);
6488 skip_emulated_instruction(vcpu);
6489 return 1;
6490 }
6491
6492 page = nested_get_page(vcpu, vmptr);
6493 if (page == NULL ||
6494 *(u32 *)kmap(page) != VMCS12_REVISION) {
6495 nested_vmx_failInvalid(vcpu);
6496 kunmap(page);
6497 skip_emulated_instruction(vcpu);
6498 return 1;
6499 }
6500 kunmap(page);
6501 vmx->nested.vmxon_ptr = vmptr;
6502 break;
Bandan Das4291b582014-05-06 02:19:18 -04006503 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006504 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006505 nested_vmx_failValid(vcpu,
6506 VMXERR_VMCLEAR_INVALID_ADDRESS);
6507 skip_emulated_instruction(vcpu);
6508 return 1;
6509 }
Bandan Das3573e222014-05-06 02:19:16 -04006510
Bandan Das4291b582014-05-06 02:19:18 -04006511 if (vmptr == vmx->nested.vmxon_ptr) {
6512 nested_vmx_failValid(vcpu,
6513 VMXERR_VMCLEAR_VMXON_POINTER);
6514 skip_emulated_instruction(vcpu);
6515 return 1;
6516 }
6517 break;
6518 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006519 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006520 nested_vmx_failValid(vcpu,
6521 VMXERR_VMPTRLD_INVALID_ADDRESS);
6522 skip_emulated_instruction(vcpu);
6523 return 1;
6524 }
6525
6526 if (vmptr == vmx->nested.vmxon_ptr) {
6527 nested_vmx_failValid(vcpu,
6528 VMXERR_VMCLEAR_VMXON_POINTER);
6529 skip_emulated_instruction(vcpu);
6530 return 1;
6531 }
6532 break;
Bandan Das3573e222014-05-06 02:19:16 -04006533 default:
6534 return 1; /* shouldn't happen */
6535 }
6536
Bandan Das4291b582014-05-06 02:19:18 -04006537 if (vmpointer)
6538 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04006539 return 0;
6540}
6541
6542/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006543 * Emulate the VMXON instruction.
6544 * Currently, we just remember that VMX is active, and do not save or even
6545 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
6546 * do not currently need to store anything in that guest-allocated memory
6547 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
6548 * argument is different from the VMXON pointer (which the spec says they do).
6549 */
6550static int handle_vmon(struct kvm_vcpu *vcpu)
6551{
6552 struct kvm_segment cs;
6553 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03006554 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006555 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
6556 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006557
6558 /* The Intel VMX Instruction Reference lists a bunch of bits that
6559 * are prerequisite to running VMXON, most notably cr4.VMXE must be
6560 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
6561 * Otherwise, we should fail with #UD. We test these now:
6562 */
6563 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
6564 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
6565 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
6566 kvm_queue_exception(vcpu, UD_VECTOR);
6567 return 1;
6568 }
6569
6570 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6571 if (is_long_mode(vcpu) && !cs.l) {
6572 kvm_queue_exception(vcpu, UD_VECTOR);
6573 return 1;
6574 }
6575
6576 if (vmx_get_cpl(vcpu)) {
6577 kvm_inject_gp(vcpu, 0);
6578 return 1;
6579 }
Bandan Das3573e222014-05-06 02:19:16 -04006580
Bandan Das4291b582014-05-06 02:19:18 -04006581 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04006582 return 1;
6583
Abel Gordon145c28d2013-04-18 14:36:55 +03006584 if (vmx->nested.vmxon) {
6585 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
6586 skip_emulated_instruction(vcpu);
6587 return 1;
6588 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006589
6590 if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
6591 != VMXON_NEEDED_FEATURES) {
6592 kvm_inject_gp(vcpu, 0);
6593 return 1;
6594 }
6595
Abel Gordon8de48832013-04-18 14:37:25 +03006596 if (enable_shadow_vmcs) {
6597 shadow_vmcs = alloc_vmcs();
6598 if (!shadow_vmcs)
6599 return -ENOMEM;
6600 /* mark vmcs as shadow */
6601 shadow_vmcs->revision_id |= (1u << 31);
6602 /* init shadow vmcs */
6603 vmcs_clear(shadow_vmcs);
6604 vmx->nested.current_shadow_vmcs = shadow_vmcs;
6605 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006606
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006607 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
6608 vmx->nested.vmcs02_num = 0;
6609
Jan Kiszkaf41245002014-03-07 20:03:13 +01006610 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
6611 HRTIMER_MODE_REL);
6612 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
6613
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006614 vmx->nested.vmxon = true;
6615
6616 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08006617 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006618 return 1;
6619}
6620
6621/*
6622 * Intel's VMX Instruction Reference specifies a common set of prerequisites
6623 * for running VMX instructions (except VMXON, whose prerequisites are
6624 * slightly different). It also specifies what exception to inject otherwise.
6625 */
6626static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
6627{
6628 struct kvm_segment cs;
6629 struct vcpu_vmx *vmx = to_vmx(vcpu);
6630
6631 if (!vmx->nested.vmxon) {
6632 kvm_queue_exception(vcpu, UD_VECTOR);
6633 return 0;
6634 }
6635
6636 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6637 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
6638 (is_long_mode(vcpu) && !cs.l)) {
6639 kvm_queue_exception(vcpu, UD_VECTOR);
6640 return 0;
6641 }
6642
6643 if (vmx_get_cpl(vcpu)) {
6644 kvm_inject_gp(vcpu, 0);
6645 return 0;
6646 }
6647
6648 return 1;
6649}
6650
Abel Gordone7953d72013-04-18 14:37:55 +03006651static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
6652{
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006653 if (vmx->nested.current_vmptr == -1ull)
6654 return;
6655
6656 /* current_vmptr and current_vmcs12 are always set/reset together */
6657 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
6658 return;
6659
Abel Gordon012f83c2013-04-18 14:39:25 +03006660 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006661 /* copy to memory all shadowed fields in case
6662 they were modified */
6663 copy_shadow_to_vmcs12(vmx);
6664 vmx->nested.sync_shadow_vmcs = false;
Xiao Guangrong7ec36292015-09-09 14:05:56 +08006665 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL,
6666 SECONDARY_EXEC_SHADOW_VMCS);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006667 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03006668 }
Wincy Van705699a2015-02-03 23:58:17 +08006669 vmx->nested.posted_intr_nv = -1;
Abel Gordone7953d72013-04-18 14:37:55 +03006670 kunmap(vmx->nested.current_vmcs12_page);
6671 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006672 vmx->nested.current_vmptr = -1ull;
6673 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03006674}
6675
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006676/*
6677 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
6678 * just stops using VMX.
6679 */
6680static void free_nested(struct vcpu_vmx *vmx)
6681{
6682 if (!vmx->nested.vmxon)
6683 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006684
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006685 vmx->nested.vmxon = false;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006686 nested_release_vmcs12(vmx);
Abel Gordone7953d72013-04-18 14:37:55 +03006687 if (enable_shadow_vmcs)
6688 free_vmcs(vmx->nested.current_shadow_vmcs);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006689 /* Unpin physical memory we referred to in current vmcs02 */
6690 if (vmx->nested.apic_access_page) {
6691 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02006692 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006693 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006694 if (vmx->nested.virtual_apic_page) {
6695 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02006696 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006697 }
Wincy Van705699a2015-02-03 23:58:17 +08006698 if (vmx->nested.pi_desc_page) {
6699 kunmap(vmx->nested.pi_desc_page);
6700 nested_release_page(vmx->nested.pi_desc_page);
6701 vmx->nested.pi_desc_page = NULL;
6702 vmx->nested.pi_desc = NULL;
6703 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006704
6705 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006706}
6707
6708/* Emulate the VMXOFF instruction */
6709static int handle_vmoff(struct kvm_vcpu *vcpu)
6710{
6711 if (!nested_vmx_check_permission(vcpu))
6712 return 1;
6713 free_nested(to_vmx(vcpu));
6714 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08006715 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006716 return 1;
6717}
6718
Nadav Har'El27d6c862011-05-25 23:06:59 +03006719/* Emulate the VMCLEAR instruction */
6720static int handle_vmclear(struct kvm_vcpu *vcpu)
6721{
6722 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03006723 gpa_t vmptr;
6724 struct vmcs12 *vmcs12;
6725 struct page *page;
Nadav Har'El27d6c862011-05-25 23:06:59 +03006726
6727 if (!nested_vmx_check_permission(vcpu))
6728 return 1;
6729
Bandan Das4291b582014-05-06 02:19:18 -04006730 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03006731 return 1;
6732
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006733 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03006734 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03006735
6736 page = nested_get_page(vcpu, vmptr);
6737 if (page == NULL) {
6738 /*
6739 * For accurate processor emulation, VMCLEAR beyond available
6740 * physical memory should do nothing at all. However, it is
6741 * possible that a nested vmx bug, not a guest hypervisor bug,
6742 * resulted in this case, so let's shut down before doing any
6743 * more damage:
6744 */
6745 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6746 return 1;
6747 }
6748 vmcs12 = kmap(page);
6749 vmcs12->launch_state = 0;
6750 kunmap(page);
6751 nested_release_page(page);
6752
6753 nested_free_vmcs02(vmx, vmptr);
6754
6755 skip_emulated_instruction(vcpu);
6756 nested_vmx_succeed(vcpu);
6757 return 1;
6758}
6759
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006760static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
6761
6762/* Emulate the VMLAUNCH instruction */
6763static int handle_vmlaunch(struct kvm_vcpu *vcpu)
6764{
6765 return nested_vmx_run(vcpu, true);
6766}
6767
6768/* Emulate the VMRESUME instruction */
6769static int handle_vmresume(struct kvm_vcpu *vcpu)
6770{
6771
6772 return nested_vmx_run(vcpu, false);
6773}
6774
Nadav Har'El49f705c2011-05-25 23:08:30 +03006775enum vmcs_field_type {
6776 VMCS_FIELD_TYPE_U16 = 0,
6777 VMCS_FIELD_TYPE_U64 = 1,
6778 VMCS_FIELD_TYPE_U32 = 2,
6779 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
6780};
6781
6782static inline int vmcs_field_type(unsigned long field)
6783{
6784 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
6785 return VMCS_FIELD_TYPE_U32;
6786 return (field >> 13) & 0x3 ;
6787}
6788
6789static inline int vmcs_field_readonly(unsigned long field)
6790{
6791 return (((field >> 10) & 0x3) == 1);
6792}
6793
6794/*
6795 * Read a vmcs12 field. Since these can have varying lengths and we return
6796 * one type, we chose the biggest type (u64) and zero-extend the return value
6797 * to that size. Note that the caller, handle_vmread, might need to use only
6798 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
6799 * 64-bit fields are to be returned).
6800 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006801static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
6802 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03006803{
6804 short offset = vmcs_field_to_offset(field);
6805 char *p;
6806
6807 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006808 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006809
6810 p = ((char *)(get_vmcs12(vcpu))) + offset;
6811
6812 switch (vmcs_field_type(field)) {
6813 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6814 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006815 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006816 case VMCS_FIELD_TYPE_U16:
6817 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006818 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006819 case VMCS_FIELD_TYPE_U32:
6820 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006821 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006822 case VMCS_FIELD_TYPE_U64:
6823 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006824 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006825 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006826 WARN_ON(1);
6827 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006828 }
6829}
6830
Abel Gordon20b97fe2013-04-18 14:36:25 +03006831
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006832static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
6833 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03006834 short offset = vmcs_field_to_offset(field);
6835 char *p = ((char *) get_vmcs12(vcpu)) + offset;
6836 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006837 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006838
6839 switch (vmcs_field_type(field)) {
6840 case VMCS_FIELD_TYPE_U16:
6841 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006842 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006843 case VMCS_FIELD_TYPE_U32:
6844 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006845 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006846 case VMCS_FIELD_TYPE_U64:
6847 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006848 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006849 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6850 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006851 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006852 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006853 WARN_ON(1);
6854 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006855 }
6856
6857}
6858
Abel Gordon16f5b902013-04-18 14:38:25 +03006859static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
6860{
6861 int i;
6862 unsigned long field;
6863 u64 field_value;
6864 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02006865 const unsigned long *fields = shadow_read_write_fields;
6866 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03006867
Jan Kiszka282da872014-10-08 18:05:39 +02006868 preempt_disable();
6869
Abel Gordon16f5b902013-04-18 14:38:25 +03006870 vmcs_load(shadow_vmcs);
6871
6872 for (i = 0; i < num_fields; i++) {
6873 field = fields[i];
6874 switch (vmcs_field_type(field)) {
6875 case VMCS_FIELD_TYPE_U16:
6876 field_value = vmcs_read16(field);
6877 break;
6878 case VMCS_FIELD_TYPE_U32:
6879 field_value = vmcs_read32(field);
6880 break;
6881 case VMCS_FIELD_TYPE_U64:
6882 field_value = vmcs_read64(field);
6883 break;
6884 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6885 field_value = vmcs_readl(field);
6886 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006887 default:
6888 WARN_ON(1);
6889 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03006890 }
6891 vmcs12_write_any(&vmx->vcpu, field, field_value);
6892 }
6893
6894 vmcs_clear(shadow_vmcs);
6895 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02006896
6897 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03006898}
6899
Abel Gordonc3114422013-04-18 14:38:55 +03006900static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
6901{
Mathias Krausec2bae892013-06-26 20:36:21 +02006902 const unsigned long *fields[] = {
6903 shadow_read_write_fields,
6904 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03006905 };
Mathias Krausec2bae892013-06-26 20:36:21 +02006906 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03006907 max_shadow_read_write_fields,
6908 max_shadow_read_only_fields
6909 };
6910 int i, q;
6911 unsigned long field;
6912 u64 field_value = 0;
6913 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
6914
6915 vmcs_load(shadow_vmcs);
6916
Mathias Krausec2bae892013-06-26 20:36:21 +02006917 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03006918 for (i = 0; i < max_fields[q]; i++) {
6919 field = fields[q][i];
6920 vmcs12_read_any(&vmx->vcpu, field, &field_value);
6921
6922 switch (vmcs_field_type(field)) {
6923 case VMCS_FIELD_TYPE_U16:
6924 vmcs_write16(field, (u16)field_value);
6925 break;
6926 case VMCS_FIELD_TYPE_U32:
6927 vmcs_write32(field, (u32)field_value);
6928 break;
6929 case VMCS_FIELD_TYPE_U64:
6930 vmcs_write64(field, (u64)field_value);
6931 break;
6932 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6933 vmcs_writel(field, (long)field_value);
6934 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006935 default:
6936 WARN_ON(1);
6937 break;
Abel Gordonc3114422013-04-18 14:38:55 +03006938 }
6939 }
6940 }
6941
6942 vmcs_clear(shadow_vmcs);
6943 vmcs_load(vmx->loaded_vmcs->vmcs);
6944}
6945
Nadav Har'El49f705c2011-05-25 23:08:30 +03006946/*
6947 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
6948 * used before) all generate the same failure when it is missing.
6949 */
6950static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
6951{
6952 struct vcpu_vmx *vmx = to_vmx(vcpu);
6953 if (vmx->nested.current_vmptr == -1ull) {
6954 nested_vmx_failInvalid(vcpu);
6955 skip_emulated_instruction(vcpu);
6956 return 0;
6957 }
6958 return 1;
6959}
6960
6961static int handle_vmread(struct kvm_vcpu *vcpu)
6962{
6963 unsigned long field;
6964 u64 field_value;
6965 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6966 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6967 gva_t gva = 0;
6968
6969 if (!nested_vmx_check_permission(vcpu) ||
6970 !nested_vmx_check_vmcs12(vcpu))
6971 return 1;
6972
6973 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03006974 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03006975 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006976 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03006977 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
6978 skip_emulated_instruction(vcpu);
6979 return 1;
6980 }
6981 /*
6982 * Now copy part of this value to register or memory, as requested.
6983 * Note that the number of bits actually copied is 32 or 64 depending
6984 * on the guest's mode (32 or 64 bit), not on the given field's length.
6985 */
6986 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03006987 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03006988 field_value);
6989 } else {
6990 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00006991 vmx_instruction_info, true, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03006992 return 1;
6993 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
6994 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
6995 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
6996 }
6997
6998 nested_vmx_succeed(vcpu);
6999 skip_emulated_instruction(vcpu);
7000 return 1;
7001}
7002
7003
7004static int handle_vmwrite(struct kvm_vcpu *vcpu)
7005{
7006 unsigned long field;
7007 gva_t gva;
7008 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7009 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03007010 /* The value to write might be 32 or 64 bits, depending on L1's long
7011 * mode, and eventually we need to write that into a field of several
7012 * possible lengths. The code below first zero-extends the value to 64
7013 * bit (field_value), and then copies only the approriate number of
7014 * bits into the vmcs12 field.
7015 */
7016 u64 field_value = 0;
7017 struct x86_exception e;
7018
7019 if (!nested_vmx_check_permission(vcpu) ||
7020 !nested_vmx_check_vmcs12(vcpu))
7021 return 1;
7022
7023 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03007024 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007025 (((vmx_instruction_info) >> 3) & 0xf));
7026 else {
7027 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007028 vmx_instruction_info, false, &gva))
Nadav Har'El49f705c2011-05-25 23:08:30 +03007029 return 1;
7030 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03007031 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007032 kvm_inject_page_fault(vcpu, &e);
7033 return 1;
7034 }
7035 }
7036
7037
Nadav Amit27e6fb52014-06-18 17:19:26 +03007038 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03007039 if (vmcs_field_readonly(field)) {
7040 nested_vmx_failValid(vcpu,
7041 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
7042 skip_emulated_instruction(vcpu);
7043 return 1;
7044 }
7045
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01007046 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03007047 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
7048 skip_emulated_instruction(vcpu);
7049 return 1;
7050 }
7051
7052 nested_vmx_succeed(vcpu);
7053 skip_emulated_instruction(vcpu);
7054 return 1;
7055}
7056
Nadav Har'El63846662011-05-25 23:07:29 +03007057/* Emulate the VMPTRLD instruction */
7058static int handle_vmptrld(struct kvm_vcpu *vcpu)
7059{
7060 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03007061 gpa_t vmptr;
Nadav Har'El63846662011-05-25 23:07:29 +03007062
7063 if (!nested_vmx_check_permission(vcpu))
7064 return 1;
7065
Bandan Das4291b582014-05-06 02:19:18 -04007066 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03007067 return 1;
7068
Nadav Har'El63846662011-05-25 23:07:29 +03007069 if (vmx->nested.current_vmptr != vmptr) {
7070 struct vmcs12 *new_vmcs12;
7071 struct page *page;
7072 page = nested_get_page(vcpu, vmptr);
7073 if (page == NULL) {
7074 nested_vmx_failInvalid(vcpu);
7075 skip_emulated_instruction(vcpu);
7076 return 1;
7077 }
7078 new_vmcs12 = kmap(page);
7079 if (new_vmcs12->revision_id != VMCS12_REVISION) {
7080 kunmap(page);
7081 nested_release_page_clean(page);
7082 nested_vmx_failValid(vcpu,
7083 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
7084 skip_emulated_instruction(vcpu);
7085 return 1;
7086 }
Nadav Har'El63846662011-05-25 23:07:29 +03007087
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02007088 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03007089 vmx->nested.current_vmptr = vmptr;
7090 vmx->nested.current_vmcs12 = new_vmcs12;
7091 vmx->nested.current_vmcs12_page = page;
Abel Gordon012f83c2013-04-18 14:39:25 +03007092 if (enable_shadow_vmcs) {
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007093 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL,
7094 SECONDARY_EXEC_SHADOW_VMCS);
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03007095 vmcs_write64(VMCS_LINK_POINTER,
7096 __pa(vmx->nested.current_shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03007097 vmx->nested.sync_shadow_vmcs = true;
7098 }
Nadav Har'El63846662011-05-25 23:07:29 +03007099 }
7100
7101 nested_vmx_succeed(vcpu);
7102 skip_emulated_instruction(vcpu);
7103 return 1;
7104}
7105
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007106/* Emulate the VMPTRST instruction */
7107static int handle_vmptrst(struct kvm_vcpu *vcpu)
7108{
7109 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7110 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7111 gva_t vmcs_gva;
7112 struct x86_exception e;
7113
7114 if (!nested_vmx_check_permission(vcpu))
7115 return 1;
7116
7117 if (get_vmx_mem_address(vcpu, exit_qualification,
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007118 vmx_instruction_info, true, &vmcs_gva))
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007119 return 1;
7120 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
7121 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
7122 (void *)&to_vmx(vcpu)->nested.current_vmptr,
7123 sizeof(u64), &e)) {
7124 kvm_inject_page_fault(vcpu, &e);
7125 return 1;
7126 }
7127 nested_vmx_succeed(vcpu);
7128 skip_emulated_instruction(vcpu);
7129 return 1;
7130}
7131
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007132/* Emulate the INVEPT instruction */
7133static int handle_invept(struct kvm_vcpu *vcpu)
7134{
Wincy Vanb9c237b2015-02-03 23:56:30 +08007135 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007136 u32 vmx_instruction_info, types;
7137 unsigned long type;
7138 gva_t gva;
7139 struct x86_exception e;
7140 struct {
7141 u64 eptp, gpa;
7142 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007143
Wincy Vanb9c237b2015-02-03 23:56:30 +08007144 if (!(vmx->nested.nested_vmx_secondary_ctls_high &
7145 SECONDARY_EXEC_ENABLE_EPT) ||
7146 !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007147 kvm_queue_exception(vcpu, UD_VECTOR);
7148 return 1;
7149 }
7150
7151 if (!nested_vmx_check_permission(vcpu))
7152 return 1;
7153
7154 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
7155 kvm_queue_exception(vcpu, UD_VECTOR);
7156 return 1;
7157 }
7158
7159 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03007160 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007161
Wincy Vanb9c237b2015-02-03 23:56:30 +08007162 types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007163
7164 if (!(types & (1UL << type))) {
7165 nested_vmx_failValid(vcpu,
7166 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
7167 return 1;
7168 }
7169
7170 /* According to the Intel VMX instruction reference, the memory
7171 * operand is read even if it isn't needed (e.g., for type==global)
7172 */
7173 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
Eugene Korenevskyf9eb4af2015-04-17 02:22:21 +00007174 vmx_instruction_info, false, &gva))
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007175 return 1;
7176 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
7177 sizeof(operand), &e)) {
7178 kvm_inject_page_fault(vcpu, &e);
7179 return 1;
7180 }
7181
7182 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007183 case VMX_EPT_EXTENT_GLOBAL:
7184 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04007185 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007186 nested_vmx_succeed(vcpu);
7187 break;
7188 default:
Bandan Das4b855072014-04-19 18:17:44 -04007189 /* Trap single context invalidation invept calls */
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007190 BUG_ON(1);
7191 break;
7192 }
7193
7194 skip_emulated_instruction(vcpu);
7195 return 1;
7196}
7197
Petr Matouseka642fc32014-09-23 20:22:30 +02007198static int handle_invvpid(struct kvm_vcpu *vcpu)
7199{
7200 kvm_queue_exception(vcpu, UD_VECTOR);
7201 return 1;
7202}
7203
Kai Huang843e4332015-01-28 10:54:28 +08007204static int handle_pml_full(struct kvm_vcpu *vcpu)
7205{
7206 unsigned long exit_qualification;
7207
7208 trace_kvm_pml_full(vcpu->vcpu_id);
7209
7210 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7211
7212 /*
7213 * PML buffer FULL happened while executing iret from NMI,
7214 * "blocked by NMI" bit has to be set before next VM entry.
7215 */
7216 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7217 cpu_has_virtual_nmis() &&
7218 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7219 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7220 GUEST_INTR_STATE_NMI);
7221
7222 /*
7223 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7224 * here.., and there's no userspace involvement needed for PML.
7225 */
7226 return 1;
7227}
7228
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08007229static int handle_pcommit(struct kvm_vcpu *vcpu)
7230{
7231 /* we never catch pcommit instruct for L1 guest. */
7232 WARN_ON(1);
7233 return 1;
7234}
7235
Nadav Har'El0140cae2011-05-25 23:06:28 +03007236/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007237 * The exit handlers return 1 if the exit was handled fully and guest execution
7238 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7239 * to be done to userspace and return 0.
7240 */
Mathias Krause772e0312012-08-30 01:30:19 +02007241static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007242 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7243 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007244 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007245 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007246 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007247 [EXIT_REASON_CR_ACCESS] = handle_cr,
7248 [EXIT_REASON_DR_ACCESS] = handle_dr,
7249 [EXIT_REASON_CPUID] = handle_cpuid,
7250 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7251 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7252 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7253 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007254 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007255 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007256 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007257 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007258 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007259 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007260 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007261 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007262 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007263 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007264 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007265 [EXIT_REASON_VMOFF] = handle_vmoff,
7266 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007267 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7268 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007269 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007270 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007271 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007272 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007273 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007274 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007275 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7276 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007277 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007278 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007279 [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007280 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007281 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007282 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007283 [EXIT_REASON_XSAVES] = handle_xsaves,
7284 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007285 [EXIT_REASON_PML_FULL] = handle_pml_full,
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08007286 [EXIT_REASON_PCOMMIT] = handle_pcommit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007287};
7288
7289static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007290 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007291
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007292static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7293 struct vmcs12 *vmcs12)
7294{
7295 unsigned long exit_qualification;
7296 gpa_t bitmap, last_bitmap;
7297 unsigned int port;
7298 int size;
7299 u8 b;
7300
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007301 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007302 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007303
7304 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7305
7306 port = exit_qualification >> 16;
7307 size = (exit_qualification & 7) + 1;
7308
7309 last_bitmap = (gpa_t)-1;
7310 b = -1;
7311
7312 while (size > 0) {
7313 if (port < 0x8000)
7314 bitmap = vmcs12->io_bitmap_a;
7315 else if (port < 0x10000)
7316 bitmap = vmcs12->io_bitmap_b;
7317 else
Joe Perches1d804d02015-03-30 16:46:09 -07007318 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007319 bitmap += (port & 0x7fff) / 8;
7320
7321 if (last_bitmap != bitmap)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007322 if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007323 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007324 if (b & (1 << (port & 7)))
Joe Perches1d804d02015-03-30 16:46:09 -07007325 return true;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007326
7327 port++;
7328 size--;
7329 last_bitmap = bitmap;
7330 }
7331
Joe Perches1d804d02015-03-30 16:46:09 -07007332 return false;
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007333}
7334
Nadav Har'El644d7112011-05-25 23:12:35 +03007335/*
7336 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7337 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7338 * disinterest in the current event (read or write a specific MSR) by using an
7339 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7340 */
7341static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7342 struct vmcs12 *vmcs12, u32 exit_reason)
7343{
7344 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7345 gpa_t bitmap;
7346
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007347 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Joe Perches1d804d02015-03-30 16:46:09 -07007348 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007349
7350 /*
7351 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7352 * for the four combinations of read/write and low/high MSR numbers.
7353 * First we need to figure out which of the four to use:
7354 */
7355 bitmap = vmcs12->msr_bitmap;
7356 if (exit_reason == EXIT_REASON_MSR_WRITE)
7357 bitmap += 2048;
7358 if (msr_index >= 0xc0000000) {
7359 msr_index -= 0xc0000000;
7360 bitmap += 1024;
7361 }
7362
7363 /* Then read the msr_index'th bit from this bitmap: */
7364 if (msr_index < 1024*8) {
7365 unsigned char b;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007366 if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1))
Joe Perches1d804d02015-03-30 16:46:09 -07007367 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007368 return 1 & (b >> (msr_index & 7));
7369 } else
Joe Perches1d804d02015-03-30 16:46:09 -07007370 return true; /* let L1 handle the wrong parameter */
Nadav Har'El644d7112011-05-25 23:12:35 +03007371}
7372
7373/*
7374 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7375 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7376 * intercept (via guest_host_mask etc.) the current event.
7377 */
7378static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7379 struct vmcs12 *vmcs12)
7380{
7381 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7382 int cr = exit_qualification & 15;
7383 int reg = (exit_qualification >> 8) & 15;
Nadav Amit1e32c072014-06-18 17:19:25 +03007384 unsigned long val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03007385
7386 switch ((exit_qualification >> 4) & 3) {
7387 case 0: /* mov to cr */
7388 switch (cr) {
7389 case 0:
7390 if (vmcs12->cr0_guest_host_mask &
7391 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007392 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007393 break;
7394 case 3:
7395 if ((vmcs12->cr3_target_count >= 1 &&
7396 vmcs12->cr3_target_value0 == val) ||
7397 (vmcs12->cr3_target_count >= 2 &&
7398 vmcs12->cr3_target_value1 == val) ||
7399 (vmcs12->cr3_target_count >= 3 &&
7400 vmcs12->cr3_target_value2 == val) ||
7401 (vmcs12->cr3_target_count >= 4 &&
7402 vmcs12->cr3_target_value3 == val))
Joe Perches1d804d02015-03-30 16:46:09 -07007403 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007404 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007405 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007406 break;
7407 case 4:
7408 if (vmcs12->cr4_guest_host_mask &
7409 (vmcs12->cr4_read_shadow ^ val))
Joe Perches1d804d02015-03-30 16:46:09 -07007410 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007411 break;
7412 case 8:
7413 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
Joe Perches1d804d02015-03-30 16:46:09 -07007414 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007415 break;
7416 }
7417 break;
7418 case 2: /* clts */
7419 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
7420 (vmcs12->cr0_read_shadow & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007421 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007422 break;
7423 case 1: /* mov from cr */
7424 switch (cr) {
7425 case 3:
7426 if (vmcs12->cpu_based_vm_exec_control &
7427 CPU_BASED_CR3_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007428 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007429 break;
7430 case 8:
7431 if (vmcs12->cpu_based_vm_exec_control &
7432 CPU_BASED_CR8_STORE_EXITING)
Joe Perches1d804d02015-03-30 16:46:09 -07007433 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007434 break;
7435 }
7436 break;
7437 case 3: /* lmsw */
7438 /*
7439 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
7440 * cr0. Other attempted changes are ignored, with no exit.
7441 */
7442 if (vmcs12->cr0_guest_host_mask & 0xe &
7443 (val ^ vmcs12->cr0_read_shadow))
Joe Perches1d804d02015-03-30 16:46:09 -07007444 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007445 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
7446 !(vmcs12->cr0_read_shadow & 0x1) &&
7447 (val & 0x1))
Joe Perches1d804d02015-03-30 16:46:09 -07007448 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007449 break;
7450 }
Joe Perches1d804d02015-03-30 16:46:09 -07007451 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007452}
7453
7454/*
7455 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
7456 * should handle it ourselves in L0 (and then continue L2). Only call this
7457 * when in is_guest_mode (L2).
7458 */
7459static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
7460{
Nadav Har'El644d7112011-05-25 23:12:35 +03007461 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7462 struct vcpu_vmx *vmx = to_vmx(vcpu);
7463 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01007464 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03007465
Jan Kiszka542060e2014-01-04 18:47:21 +01007466 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
7467 vmcs_readl(EXIT_QUALIFICATION),
7468 vmx->idt_vectoring_info,
7469 intr_info,
7470 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
7471 KVM_ISA_VMX);
7472
Nadav Har'El644d7112011-05-25 23:12:35 +03007473 if (vmx->nested.nested_run_pending)
Joe Perches1d804d02015-03-30 16:46:09 -07007474 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007475
7476 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02007477 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
7478 vmcs_read32(VM_INSTRUCTION_ERROR));
Joe Perches1d804d02015-03-30 16:46:09 -07007479 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007480 }
7481
7482 switch (exit_reason) {
7483 case EXIT_REASON_EXCEPTION_NMI:
7484 if (!is_exception(intr_info))
Joe Perches1d804d02015-03-30 16:46:09 -07007485 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007486 else if (is_page_fault(intr_info))
7487 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01007488 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01007489 !(vmcs12->guest_cr0 & X86_CR0_TS))
Joe Perches1d804d02015-03-30 16:46:09 -07007490 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007491 return vmcs12->exception_bitmap &
7492 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
7493 case EXIT_REASON_EXTERNAL_INTERRUPT:
Joe Perches1d804d02015-03-30 16:46:09 -07007494 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007495 case EXIT_REASON_TRIPLE_FAULT:
Joe Perches1d804d02015-03-30 16:46:09 -07007496 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007497 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02007498 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03007499 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02007500 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03007501 case EXIT_REASON_TASK_SWITCH:
Joe Perches1d804d02015-03-30 16:46:09 -07007502 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007503 case EXIT_REASON_CPUID:
Marcelo Tosattibc613492014-09-18 18:24:57 -03007504 if (kvm_register_read(vcpu, VCPU_REGS_RAX) == 0xa)
Joe Perches1d804d02015-03-30 16:46:09 -07007505 return false;
7506 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007507 case EXIT_REASON_HLT:
7508 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
7509 case EXIT_REASON_INVD:
Joe Perches1d804d02015-03-30 16:46:09 -07007510 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007511 case EXIT_REASON_INVLPG:
7512 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
7513 case EXIT_REASON_RDPMC:
7514 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
Jan Kiszkab3a2a902015-03-23 19:27:19 +01007515 case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP:
Nadav Har'El644d7112011-05-25 23:12:35 +03007516 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
7517 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
7518 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
7519 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
7520 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
7521 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02007522 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03007523 /*
7524 * VMX instructions trap unconditionally. This allows L1 to
7525 * emulate them for its L2 guest, i.e., allows 3-level nesting!
7526 */
Joe Perches1d804d02015-03-30 16:46:09 -07007527 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007528 case EXIT_REASON_CR_ACCESS:
7529 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
7530 case EXIT_REASON_DR_ACCESS:
7531 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
7532 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007533 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03007534 case EXIT_REASON_MSR_READ:
7535 case EXIT_REASON_MSR_WRITE:
7536 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
7537 case EXIT_REASON_INVALID_STATE:
Joe Perches1d804d02015-03-30 16:46:09 -07007538 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007539 case EXIT_REASON_MWAIT_INSTRUCTION:
7540 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
Mihai Donțu5f3d45e2015-07-05 20:08:57 +03007541 case EXIT_REASON_MONITOR_TRAP_FLAG:
7542 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG);
Nadav Har'El644d7112011-05-25 23:12:35 +03007543 case EXIT_REASON_MONITOR_INSTRUCTION:
7544 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
7545 case EXIT_REASON_PAUSE_INSTRUCTION:
7546 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
7547 nested_cpu_has2(vmcs12,
7548 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
7549 case EXIT_REASON_MCE_DURING_VMENTRY:
Joe Perches1d804d02015-03-30 16:46:09 -07007550 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007551 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007552 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03007553 case EXIT_REASON_APIC_ACCESS:
7554 return nested_cpu_has2(vmcs12,
7555 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Wincy Van82f0dd42015-02-03 23:57:18 +08007556 case EXIT_REASON_APIC_WRITE:
Wincy Van608406e2015-02-03 23:57:51 +08007557 case EXIT_REASON_EOI_INDUCED:
7558 /* apic_write and eoi_induced should exit unconditionally. */
Joe Perches1d804d02015-03-30 16:46:09 -07007559 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007560 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03007561 /*
7562 * L0 always deals with the EPT violation. If nested EPT is
7563 * used, and the nested mmu code discovers that the address is
7564 * missing in the guest EPT table (EPT12), the EPT violation
7565 * will be injected with nested_ept_inject_page_fault()
7566 */
Joe Perches1d804d02015-03-30 16:46:09 -07007567 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007568 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03007569 /*
7570 * L2 never uses directly L1's EPT, but rather L0's own EPT
7571 * table (shadow on EPT) or a merged EPT table that L0 built
7572 * (EPT on EPT). So any problems with the structure of the
7573 * table is L0's fault.
7574 */
Joe Perches1d804d02015-03-30 16:46:09 -07007575 return false;
Nadav Har'El644d7112011-05-25 23:12:35 +03007576 case EXIT_REASON_WBINVD:
7577 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
7578 case EXIT_REASON_XSETBV:
Joe Perches1d804d02015-03-30 16:46:09 -07007579 return true;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08007580 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
7581 /*
7582 * This should never happen, since it is not possible to
7583 * set XSS to a non-zero value---neither in L1 nor in L2.
7584 * If if it were, XSS would have to be checked against
7585 * the XSS exit bitmap in vmcs12.
7586 */
7587 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08007588 case EXIT_REASON_PCOMMIT:
7589 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_PCOMMIT);
Nadav Har'El644d7112011-05-25 23:12:35 +03007590 default:
Joe Perches1d804d02015-03-30 16:46:09 -07007591 return true;
Nadav Har'El644d7112011-05-25 23:12:35 +03007592 }
7593}
7594
Avi Kivity586f9602010-11-18 13:09:54 +02007595static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
7596{
7597 *info1 = vmcs_readl(EXIT_QUALIFICATION);
7598 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
7599}
7600
Kai Huang843e4332015-01-28 10:54:28 +08007601static int vmx_enable_pml(struct vcpu_vmx *vmx)
7602{
7603 struct page *pml_pg;
Kai Huang843e4332015-01-28 10:54:28 +08007604
7605 pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
7606 if (!pml_pg)
7607 return -ENOMEM;
7608
7609 vmx->pml_pg = pml_pg;
7610
7611 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
7612 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
7613
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007614 vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_ENABLE_PML);
Kai Huang843e4332015-01-28 10:54:28 +08007615
7616 return 0;
7617}
7618
7619static void vmx_disable_pml(struct vcpu_vmx *vmx)
7620{
Kai Huang843e4332015-01-28 10:54:28 +08007621 ASSERT(vmx->pml_pg);
7622 __free_page(vmx->pml_pg);
7623 vmx->pml_pg = NULL;
7624
Xiao Guangrong7ec36292015-09-09 14:05:56 +08007625 vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_ENABLE_PML);
Kai Huang843e4332015-01-28 10:54:28 +08007626}
7627
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007628static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
Kai Huang843e4332015-01-28 10:54:28 +08007629{
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007630 struct vcpu_vmx *vmx = to_vmx(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08007631 u64 *pml_buf;
7632 u16 pml_idx;
7633
7634 pml_idx = vmcs_read16(GUEST_PML_INDEX);
7635
7636 /* Do nothing if PML buffer is empty */
7637 if (pml_idx == (PML_ENTITY_NUM - 1))
7638 return;
7639
7640 /* PML index always points to next available PML buffer entity */
7641 if (pml_idx >= PML_ENTITY_NUM)
7642 pml_idx = 0;
7643 else
7644 pml_idx++;
7645
7646 pml_buf = page_address(vmx->pml_pg);
7647 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
7648 u64 gpa;
7649
7650 gpa = pml_buf[pml_idx];
7651 WARN_ON(gpa & (PAGE_SIZE - 1));
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007652 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
Kai Huang843e4332015-01-28 10:54:28 +08007653 }
7654
7655 /* reset PML index */
7656 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
7657}
7658
7659/*
7660 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
7661 * Called before reporting dirty_bitmap to userspace.
7662 */
7663static void kvm_flush_pml_buffers(struct kvm *kvm)
7664{
7665 int i;
7666 struct kvm_vcpu *vcpu;
7667 /*
7668 * We only need to kick vcpu out of guest mode here, as PML buffer
7669 * is flushed at beginning of all VMEXITs, and it's obvious that only
7670 * vcpus running in guest are possible to have unflushed GPAs in PML
7671 * buffer.
7672 */
7673 kvm_for_each_vcpu(i, vcpu, kvm)
7674 kvm_vcpu_kick(vcpu);
7675}
7676
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02007677static void vmx_dump_sel(char *name, uint32_t sel)
7678{
7679 pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
7680 name, vmcs_read32(sel),
7681 vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
7682 vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
7683 vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
7684}
7685
7686static void vmx_dump_dtsel(char *name, uint32_t limit)
7687{
7688 pr_err("%s limit=0x%08x, base=0x%016lx\n",
7689 name, vmcs_read32(limit),
7690 vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
7691}
7692
7693static void dump_vmcs(void)
7694{
7695 u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
7696 u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
7697 u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
7698 u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
7699 u32 secondary_exec_control = 0;
7700 unsigned long cr4 = vmcs_readl(GUEST_CR4);
7701 u64 efer = vmcs_readl(GUEST_IA32_EFER);
7702 int i, n;
7703
7704 if (cpu_has_secondary_exec_ctrls())
7705 secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7706
7707 pr_err("*** Guest State ***\n");
7708 pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
7709 vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
7710 vmcs_readl(CR0_GUEST_HOST_MASK));
7711 pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
7712 cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
7713 pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
7714 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
7715 (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
7716 {
7717 pr_err("PDPTR0 = 0x%016lx PDPTR1 = 0x%016lx\n",
7718 vmcs_readl(GUEST_PDPTR0), vmcs_readl(GUEST_PDPTR1));
7719 pr_err("PDPTR2 = 0x%016lx PDPTR3 = 0x%016lx\n",
7720 vmcs_readl(GUEST_PDPTR2), vmcs_readl(GUEST_PDPTR3));
7721 }
7722 pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
7723 vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
7724 pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
7725 vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
7726 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
7727 vmcs_readl(GUEST_SYSENTER_ESP),
7728 vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
7729 vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
7730 vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
7731 vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
7732 vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
7733 vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
7734 vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
7735 vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
7736 vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
7737 vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
7738 vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
7739 if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
7740 (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
7741 pr_err("EFER = 0x%016llx PAT = 0x%016lx\n",
7742 efer, vmcs_readl(GUEST_IA32_PAT));
7743 pr_err("DebugCtl = 0x%016lx DebugExceptions = 0x%016lx\n",
7744 vmcs_readl(GUEST_IA32_DEBUGCTL),
7745 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
7746 if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
7747 pr_err("PerfGlobCtl = 0x%016lx\n",
7748 vmcs_readl(GUEST_IA32_PERF_GLOBAL_CTRL));
7749 if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
7750 pr_err("BndCfgS = 0x%016lx\n", vmcs_readl(GUEST_BNDCFGS));
7751 pr_err("Interruptibility = %08x ActivityState = %08x\n",
7752 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
7753 vmcs_read32(GUEST_ACTIVITY_STATE));
7754 if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
7755 pr_err("InterruptStatus = %04x\n",
7756 vmcs_read16(GUEST_INTR_STATUS));
7757
7758 pr_err("*** Host State ***\n");
7759 pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
7760 vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
7761 pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
7762 vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
7763 vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
7764 vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
7765 vmcs_read16(HOST_TR_SELECTOR));
7766 pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
7767 vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
7768 vmcs_readl(HOST_TR_BASE));
7769 pr_err("GDTBase=%016lx IDTBase=%016lx\n",
7770 vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
7771 pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
7772 vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
7773 vmcs_readl(HOST_CR4));
7774 pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
7775 vmcs_readl(HOST_IA32_SYSENTER_ESP),
7776 vmcs_read32(HOST_IA32_SYSENTER_CS),
7777 vmcs_readl(HOST_IA32_SYSENTER_EIP));
7778 if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
7779 pr_err("EFER = 0x%016lx PAT = 0x%016lx\n",
7780 vmcs_readl(HOST_IA32_EFER), vmcs_readl(HOST_IA32_PAT));
7781 if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
7782 pr_err("PerfGlobCtl = 0x%016lx\n",
7783 vmcs_readl(HOST_IA32_PERF_GLOBAL_CTRL));
7784
7785 pr_err("*** Control State ***\n");
7786 pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
7787 pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
7788 pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
7789 pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
7790 vmcs_read32(EXCEPTION_BITMAP),
7791 vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
7792 vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
7793 pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
7794 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
7795 vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
7796 vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
7797 pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
7798 vmcs_read32(VM_EXIT_INTR_INFO),
7799 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
7800 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
7801 pr_err(" reason=%08x qualification=%016lx\n",
7802 vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
7803 pr_err("IDTVectoring: info=%08x errcode=%08x\n",
7804 vmcs_read32(IDT_VECTORING_INFO_FIELD),
7805 vmcs_read32(IDT_VECTORING_ERROR_CODE));
7806 pr_err("TSC Offset = 0x%016lx\n", vmcs_readl(TSC_OFFSET));
7807 if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW)
7808 pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
7809 if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
7810 pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
7811 if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
7812 pr_err("EPT pointer = 0x%016lx\n", vmcs_readl(EPT_POINTER));
7813 n = vmcs_read32(CR3_TARGET_COUNT);
7814 for (i = 0; i + 1 < n; i += 4)
7815 pr_err("CR3 target%u=%016lx target%u=%016lx\n",
7816 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
7817 i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
7818 if (i < n)
7819 pr_err("CR3 target%u=%016lx\n",
7820 i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
7821 if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
7822 pr_err("PLE Gap=%08x Window=%08x\n",
7823 vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
7824 if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
7825 pr_err("Virtual processor ID = 0x%04x\n",
7826 vmcs_read16(VIRTUAL_PROCESSOR_ID));
7827}
7828
Avi Kivity6aa8b732006-12-10 02:21:36 -08007829/*
7830 * The guest has exited. See if we can fix it or if we need userspace
7831 * assistance.
7832 */
Avi Kivity851ba692009-08-24 11:10:17 +03007833static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007834{
Avi Kivity29bd8a72007-09-10 17:27:03 +03007835 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007836 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02007837 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03007838
Kai Huang843e4332015-01-28 10:54:28 +08007839 /*
7840 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
7841 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
7842 * querying dirty_bitmap, we only need to kick all vcpus out of guest
7843 * mode as if vcpus is in root mode, the PML buffer must has been
7844 * flushed already.
7845 */
7846 if (enable_pml)
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02007847 vmx_flush_pml_buffer(vcpu);
Kai Huang843e4332015-01-28 10:54:28 +08007848
Mohammed Gamal80ced182009-09-01 12:48:18 +02007849 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02007850 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02007851 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007852
Nadav Har'El644d7112011-05-25 23:12:35 +03007853 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01007854 nested_vmx_vmexit(vcpu, exit_reason,
7855 vmcs_read32(VM_EXIT_INTR_INFO),
7856 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03007857 return 1;
7858 }
7859
Mohammed Gamal51207022010-05-31 22:40:54 +03007860 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
Paolo Bonzini4eb64dc2015-04-30 12:57:28 +02007861 dump_vmcs();
Mohammed Gamal51207022010-05-31 22:40:54 +03007862 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
7863 vcpu->run->fail_entry.hardware_entry_failure_reason
7864 = exit_reason;
7865 return 0;
7866 }
7867
Avi Kivity29bd8a72007-09-10 17:27:03 +03007868 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03007869 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
7870 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03007871 = vmcs_read32(VM_INSTRUCTION_ERROR);
7872 return 0;
7873 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007874
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08007875 /*
7876 * Note:
7877 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
7878 * delivery event since it indicates guest is accessing MMIO.
7879 * The vm-exit can be triggered again after return to guest that
7880 * will cause infinite loop.
7881 */
Mike Dayd77c26f2007-10-08 09:02:08 -04007882 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08007883 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02007884 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08007885 exit_reason != EXIT_REASON_TASK_SWITCH)) {
7886 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7887 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
7888 vcpu->run->internal.ndata = 2;
7889 vcpu->run->internal.data[0] = vectoring_info;
7890 vcpu->run->internal.data[1] = exit_reason;
7891 return 0;
7892 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007893
Nadav Har'El644d7112011-05-25 23:12:35 +03007894 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
7895 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03007896 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03007897 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007898 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007899 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01007900 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007901 /*
7902 * This CPU don't support us in finding the end of an
7903 * NMI-blocked window if the guest runs with IRQs
7904 * disabled. So we pull the trigger after 1 s of
7905 * futile waiting, but inform the user about this.
7906 */
7907 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
7908 "state on VCPU %d after 1 s timeout\n",
7909 __func__, vcpu->vcpu_id);
7910 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007911 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007912 }
7913
Avi Kivity6aa8b732006-12-10 02:21:36 -08007914 if (exit_reason < kvm_vmx_max_exit_handlers
7915 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03007916 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007917 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03007918 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
7919 kvm_queue_exception(vcpu, UD_VECTOR);
7920 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007921 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007922}
7923
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007924static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007925{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007926 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7927
7928 if (is_guest_mode(vcpu) &&
7929 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
7930 return;
7931
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007932 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007933 vmcs_write32(TPR_THRESHOLD, 0);
7934 return;
7935 }
7936
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007937 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007938}
7939
Yang Zhang8d146952013-01-25 10:18:50 +08007940static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
7941{
7942 u32 sec_exec_control;
7943
7944 /*
7945 * There is not point to enable virtualize x2apic without enable
7946 * apicv
7947 */
Yang Zhangc7c9c562013-01-25 10:18:51 +08007948 if (!cpu_has_vmx_virtualize_x2apic_mode() ||
Paolo Bonzini35754c92015-07-29 12:05:37 +02007949 !vmx_cpu_uses_apicv(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08007950 return;
7951
Paolo Bonzini35754c92015-07-29 12:05:37 +02007952 if (!cpu_need_tpr_shadow(vcpu))
Yang Zhang8d146952013-01-25 10:18:50 +08007953 return;
7954
7955 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7956
7957 if (set) {
7958 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7959 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
7960 } else {
7961 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
7962 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7963 }
7964 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
7965
7966 vmx_set_msr_bitmap(vcpu);
7967}
7968
Tang Chen38b99172014-09-24 15:57:54 +08007969static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
7970{
7971 struct vcpu_vmx *vmx = to_vmx(vcpu);
7972
7973 /*
7974 * Currently we do not handle the nested case where L2 has an
7975 * APIC access page of its own; that page is still pinned.
7976 * Hence, we skip the case where the VCPU is in guest mode _and_
7977 * L1 prepared an APIC access page for L2.
7978 *
7979 * For the case where L1 and L2 share the same APIC access page
7980 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
7981 * in the vmcs12), this function will only update either the vmcs01
7982 * or the vmcs02. If the former, the vmcs02 will be updated by
7983 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
7984 * the next L2->L1 exit.
7985 */
7986 if (!is_guest_mode(vcpu) ||
7987 !nested_cpu_has2(vmx->nested.current_vmcs12,
7988 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
7989 vmcs_write64(APIC_ACCESS_ADDR, hpa);
7990}
7991
Yang Zhangc7c9c562013-01-25 10:18:51 +08007992static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
7993{
7994 u16 status;
7995 u8 old;
7996
Yang Zhangc7c9c562013-01-25 10:18:51 +08007997 if (isr == -1)
7998 isr = 0;
7999
8000 status = vmcs_read16(GUEST_INTR_STATUS);
8001 old = status >> 8;
8002 if (isr != old) {
8003 status &= 0xff;
8004 status |= isr << 8;
8005 vmcs_write16(GUEST_INTR_STATUS, status);
8006 }
8007}
8008
8009static void vmx_set_rvi(int vector)
8010{
8011 u16 status;
8012 u8 old;
8013
Wei Wang4114c272014-11-05 10:53:43 +08008014 if (vector == -1)
8015 vector = 0;
8016
Yang Zhangc7c9c562013-01-25 10:18:51 +08008017 status = vmcs_read16(GUEST_INTR_STATUS);
8018 old = (u8)status & 0xff;
8019 if ((u8)vector != old) {
8020 status &= ~0xff;
8021 status |= (u8)vector;
8022 vmcs_write16(GUEST_INTR_STATUS, status);
8023 }
8024}
8025
8026static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
8027{
Wanpeng Li963fee12014-07-17 19:03:00 +08008028 if (!is_guest_mode(vcpu)) {
8029 vmx_set_rvi(max_irr);
8030 return;
8031 }
8032
Wei Wang4114c272014-11-05 10:53:43 +08008033 if (max_irr == -1)
8034 return;
8035
Wanpeng Li963fee12014-07-17 19:03:00 +08008036 /*
Wei Wang4114c272014-11-05 10:53:43 +08008037 * In guest mode. If a vmexit is needed, vmx_check_nested_events
8038 * handles it.
8039 */
8040 if (nested_exit_on_intr(vcpu))
8041 return;
8042
8043 /*
8044 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08008045 * is run without virtual interrupt delivery.
8046 */
8047 if (!kvm_event_needs_reinjection(vcpu) &&
8048 vmx_interrupt_allowed(vcpu)) {
8049 kvm_queue_interrupt(vcpu, max_irr, false);
8050 vmx_inject_irq(vcpu);
8051 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08008052}
8053
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02008054static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu)
Yang Zhangc7c9c562013-01-25 10:18:51 +08008055{
Paolo Bonzini3bb345f2015-07-29 10:43:18 +02008056 u64 *eoi_exit_bitmap = vcpu->arch.eoi_exit_bitmap;
Paolo Bonzini35754c92015-07-29 12:05:37 +02008057 if (!vmx_cpu_uses_apicv(vcpu))
Yang Zhang3d81bc72013-04-11 19:25:13 +08008058 return;
8059
Yang Zhangc7c9c562013-01-25 10:18:51 +08008060 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
8061 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
8062 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
8063 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
8064}
8065
Avi Kivity51aa01d2010-07-20 14:31:20 +03008066static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03008067{
Avi Kivity00eba012011-03-07 17:24:54 +02008068 u32 exit_intr_info;
8069
8070 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
8071 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
8072 return;
8073
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008074 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02008075 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08008076
8077 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008078 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08008079 kvm_machine_check();
8080
Gleb Natapov20f65982009-05-11 13:35:55 +03008081 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02008082 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008083 (exit_intr_info & INTR_INFO_VALID_MASK)) {
8084 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03008085 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08008086 kvm_after_handle_nmi(&vmx->vcpu);
8087 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03008088}
Gleb Natapov20f65982009-05-11 13:35:55 +03008089
Yang Zhanga547c6d2013-04-11 19:25:10 +08008090static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
8091{
8092 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
8093
8094 /*
8095 * If external interrupt exists, IF bit is set in rflags/eflags on the
8096 * interrupt stack frame, and interrupt will be enabled on a return
8097 * from interrupt handler.
8098 */
8099 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
8100 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
8101 unsigned int vector;
8102 unsigned long entry;
8103 gate_desc *desc;
8104 struct vcpu_vmx *vmx = to_vmx(vcpu);
8105#ifdef CONFIG_X86_64
8106 unsigned long tmp;
8107#endif
8108
8109 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8110 desc = (gate_desc *)vmx->host_idt_base + vector;
8111 entry = gate_offset(*desc);
8112 asm volatile(
8113#ifdef CONFIG_X86_64
8114 "mov %%" _ASM_SP ", %[sp]\n\t"
8115 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
8116 "push $%c[ss]\n\t"
8117 "push %[sp]\n\t"
8118#endif
8119 "pushf\n\t"
8120 "orl $0x200, (%%" _ASM_SP ")\n\t"
8121 __ASM_SIZE(push) " $%c[cs]\n\t"
8122 "call *%[entry]\n\t"
8123 :
8124#ifdef CONFIG_X86_64
8125 [sp]"=&r"(tmp)
8126#endif
8127 :
8128 [entry]"r"(entry),
8129 [ss]"i"(__KERNEL_DS),
8130 [cs]"i"(__KERNEL_CS)
8131 );
8132 } else
8133 local_irq_enable();
8134}
8135
Paolo Bonzini6d396b52015-04-01 14:25:33 +02008136static bool vmx_has_high_real_mode_segbase(void)
8137{
8138 return enable_unrestricted_guest || emulate_invalid_guest_state;
8139}
8140
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008141static bool vmx_mpx_supported(void)
8142{
8143 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
8144 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
8145}
8146
Wanpeng Li55412b22014-12-02 19:21:30 +08008147static bool vmx_xsaves_supported(void)
8148{
8149 return vmcs_config.cpu_based_2nd_exec_ctrl &
8150 SECONDARY_EXEC_XSAVES;
8151}
8152
Avi Kivity51aa01d2010-07-20 14:31:20 +03008153static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
8154{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008155 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03008156 bool unblock_nmi;
8157 u8 vector;
8158 bool idtv_info_valid;
8159
8160 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03008161
Avi Kivitycf393f72008-07-01 16:20:21 +03008162 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02008163 if (vmx->nmi_known_unmasked)
8164 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02008165 /*
8166 * Can't use vmx->exit_intr_info since we're not sure what
8167 * the exit reason is.
8168 */
8169 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03008170 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
8171 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
8172 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008173 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03008174 * Re-set bit "block by NMI" before VM entry if vmexit caused by
8175 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008176 * SDM 3: 23.2.2 (September 2008)
8177 * Bit 12 is undefined in any of the following cases:
8178 * If the VM exit sets the valid bit in the IDT-vectoring
8179 * information field.
8180 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03008181 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008182 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
8183 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03008184 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
8185 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02008186 else
8187 vmx->nmi_known_unmasked =
8188 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
8189 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02008190 } else if (unlikely(vmx->soft_vnmi_blocked))
8191 vmx->vnmi_blocked_time +=
8192 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03008193}
8194
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008195static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03008196 u32 idt_vectoring_info,
8197 int instr_len_field,
8198 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03008199{
Avi Kivity51aa01d2010-07-20 14:31:20 +03008200 u8 vector;
8201 int type;
8202 bool idtv_info_valid;
8203
8204 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03008205
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008206 vcpu->arch.nmi_injected = false;
8207 kvm_clear_exception_queue(vcpu);
8208 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008209
8210 if (!idtv_info_valid)
8211 return;
8212
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008213 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03008214
Avi Kivity668f6122008-07-02 09:28:55 +03008215 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
8216 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008217
Gleb Natapov64a7ec02009-03-30 16:03:29 +03008218 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03008219 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008220 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03008221 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03008222 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03008223 * Clear bit "block by NMI" before VM entry if a NMI
8224 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03008225 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008226 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008227 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03008228 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008229 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008230 /* fall through */
8231 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03008232 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03008233 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03008234 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03008235 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03008236 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008237 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008238 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008239 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03008240 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03008241 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008242 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03008243 break;
8244 default:
8245 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03008246 }
Avi Kivitycf393f72008-07-01 16:20:21 +03008247}
8248
Avi Kivity83422e12010-07-20 14:43:23 +03008249static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
8250{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008251 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03008252 VM_EXIT_INSTRUCTION_LEN,
8253 IDT_VECTORING_ERROR_CODE);
8254}
8255
Avi Kivityb463a6f2010-07-20 15:06:17 +03008256static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
8257{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01008258 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008259 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
8260 VM_ENTRY_INSTRUCTION_LEN,
8261 VM_ENTRY_EXCEPTION_ERROR_CODE);
8262
8263 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
8264}
8265
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008266static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
8267{
8268 int i, nr_msrs;
8269 struct perf_guest_switch_msr *msrs;
8270
8271 msrs = perf_guest_get_msrs(&nr_msrs);
8272
8273 if (!msrs)
8274 return;
8275
8276 for (i = 0; i < nr_msrs; i++)
8277 if (msrs[i].host == msrs[i].guest)
8278 clear_atomic_switch_msr(vmx, msrs[i].msr);
8279 else
8280 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
8281 msrs[i].host);
8282}
8283
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08008284static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008285{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008286 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008287 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02008288
8289 /* Record the guest's net vcpu time for enforced NMI injections. */
8290 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
8291 vmx->entry_time = ktime_get();
8292
8293 /* Don't enter VMX if guest state is invalid, let the exit handler
8294 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02008295 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02008296 return;
8297
Radim Krčmářa7653ec2014-08-21 18:08:07 +02008298 if (vmx->ple_window_dirty) {
8299 vmx->ple_window_dirty = false;
8300 vmcs_write32(PLE_WINDOW, vmx->ple_window);
8301 }
8302
Abel Gordon012f83c2013-04-18 14:39:25 +03008303 if (vmx->nested.sync_shadow_vmcs) {
8304 copy_vmcs12_to_shadow(vmx);
8305 vmx->nested.sync_shadow_vmcs = false;
8306 }
8307
Avi Kivity104f2262010-11-18 13:12:52 +02008308 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
8309 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
8310 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
8311 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
8312
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07008313 cr4 = cr4_read_shadow();
Andy Lutomirskid974baa2014-10-08 09:02:13 -07008314 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
8315 vmcs_writel(HOST_CR4, cr4);
8316 vmx->host_state.vmcs_host_cr4 = cr4;
8317 }
8318
Avi Kivity104f2262010-11-18 13:12:52 +02008319 /* When single-stepping over STI and MOV SS, we must clear the
8320 * corresponding interruptibility bits in the guest state. Otherwise
8321 * vmentry fails as it then expects bit 14 (BS) in pending debug
8322 * exceptions being set, but that's not correct for the guest debugging
8323 * case. */
8324 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8325 vmx_set_interrupt_shadow(vcpu, 0);
8326
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008327 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008328 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02008329
Nadav Har'Eld462b812011-05-24 15:26:10 +03008330 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02008331 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08008332 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008333 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
8334 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
8335 "push %%" _ASM_CX " \n\t"
8336 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008337 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008338 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008339 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03008340 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008341 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008342 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
8343 "mov %%cr2, %%" _ASM_DX " \n\t"
8344 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008345 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008346 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03008347 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008348 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02008349 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008350 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008351 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
8352 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
8353 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
8354 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
8355 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
8356 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008357#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008358 "mov %c[r8](%0), %%r8 \n\t"
8359 "mov %c[r9](%0), %%r9 \n\t"
8360 "mov %c[r10](%0), %%r10 \n\t"
8361 "mov %c[r11](%0), %%r11 \n\t"
8362 "mov %c[r12](%0), %%r12 \n\t"
8363 "mov %c[r13](%0), %%r13 \n\t"
8364 "mov %c[r14](%0), %%r14 \n\t"
8365 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008366#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008367 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03008368
Avi Kivity6aa8b732006-12-10 02:21:36 -08008369 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03008370 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008371 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008372 "jmp 2f \n\t"
8373 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
8374 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08008375 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008376 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02008377 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008378 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
8379 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
8380 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
8381 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
8382 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
8383 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
8384 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008385#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008386 "mov %%r8, %c[r8](%0) \n\t"
8387 "mov %%r9, %c[r9](%0) \n\t"
8388 "mov %%r10, %c[r10](%0) \n\t"
8389 "mov %%r11, %c[r11](%0) \n\t"
8390 "mov %%r12, %c[r12](%0) \n\t"
8391 "mov %%r13, %c[r13](%0) \n\t"
8392 "mov %%r14, %c[r14](%0) \n\t"
8393 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008394#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008395 "mov %%cr2, %%" _ASM_AX " \n\t"
8396 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03008397
Avi Kivityb188c81f2012-09-16 15:10:58 +03008398 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02008399 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008400 ".pushsection .rodata \n\t"
8401 ".global vmx_return \n\t"
8402 "vmx_return: " _ASM_PTR " 2b \n\t"
8403 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02008404 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03008405 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02008406 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03008407 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008408 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
8409 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
8410 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
8411 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
8412 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
8413 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
8414 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008415#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008416 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
8417 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
8418 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
8419 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
8420 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
8421 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
8422 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
8423 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008424#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02008425 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
8426 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02008427 : "cc", "memory"
8428#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03008429 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008430 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008431#else
8432 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008433#endif
8434 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08008435
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008436 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
8437 if (debugctlmsr)
8438 update_debugctlmsr(debugctlmsr);
8439
Avi Kivityaa67f602012-08-01 16:48:03 +03008440#ifndef CONFIG_X86_64
8441 /*
8442 * The sysexit path does not restore ds/es, so we must set them to
8443 * a reasonable value ourselves.
8444 *
8445 * We can't defer this to vmx_load_host_state() since that function
8446 * may be executed in interrupt context, which saves and restore segments
8447 * around it, nullifying its effect.
8448 */
8449 loadsegment(ds, __USER_DS);
8450 loadsegment(es, __USER_DS);
8451#endif
8452
Avi Kivity6de4f3a2009-05-31 22:58:47 +03008453 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02008454 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02008455 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03008456 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02008457 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008458 vcpu->arch.regs_dirty = 0;
8459
Avi Kivity1155f762007-11-22 11:30:47 +02008460 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
8461
Nadav Har'Eld462b812011-05-24 15:26:10 +03008462 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02008463
Avi Kivity51aa01d2010-07-20 14:31:20 +03008464 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02008465 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
Avi Kivity51aa01d2010-07-20 14:31:20 +03008466
Gleb Natapove0b890d2013-09-25 12:51:33 +03008467 /*
8468 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
8469 * we did not inject a still-pending event to L1 now because of
8470 * nested_run_pending, we need to re-enable this bit.
8471 */
8472 if (vmx->nested.nested_run_pending)
8473 kvm_make_request(KVM_REQ_EVENT, vcpu);
8474
8475 vmx->nested.nested_run_pending = 0;
8476
Avi Kivity51aa01d2010-07-20 14:31:20 +03008477 vmx_complete_atomic_exit(vmx);
8478 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03008479 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008480}
8481
Paolo Bonzini4fa77342014-07-17 12:25:16 +02008482static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
8483{
8484 struct vcpu_vmx *vmx = to_vmx(vcpu);
8485 int cpu;
8486
8487 if (vmx->loaded_vmcs == &vmx->vmcs01)
8488 return;
8489
8490 cpu = get_cpu();
8491 vmx->loaded_vmcs = &vmx->vmcs01;
8492 vmx_vcpu_put(vcpu);
8493 vmx_vcpu_load(vcpu, cpu);
8494 vcpu->cpu = cpu;
8495 put_cpu();
8496}
8497
Avi Kivity6aa8b732006-12-10 02:21:36 -08008498static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
8499{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008500 struct vcpu_vmx *vmx = to_vmx(vcpu);
8501
Kai Huang843e4332015-01-28 10:54:28 +08008502 if (enable_pml)
8503 vmx_disable_pml(vmx);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08008504 free_vpid(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02008505 leave_guest_mode(vcpu);
8506 vmx_load_vmcs01(vcpu);
Marcelo Tosatti26a865f2014-01-03 17:00:51 -02008507 free_nested(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02008508 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008509 kfree(vmx->guest_msrs);
8510 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10008511 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008512}
8513
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008514static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008515{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008516 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10008517 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03008518 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008519
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008520 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008521 return ERR_PTR(-ENOMEM);
8522
Sheng Yang2384d2b2008-01-17 15:14:33 +08008523 allocate_vpid(vmx);
8524
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008525 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
8526 if (err)
8527 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08008528
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008529 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02008530 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
8531 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03008532
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02008533 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008534 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008535 goto uninit_vcpu;
8536 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08008537
Nadav Har'Eld462b812011-05-24 15:26:10 +03008538 vmx->loaded_vmcs = &vmx->vmcs01;
8539 vmx->loaded_vmcs->vmcs = alloc_vmcs();
8540 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008541 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03008542 if (!vmm_exclusive)
8543 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
8544 loaded_vmcs_init(vmx->loaded_vmcs);
8545 if (!vmm_exclusive)
8546 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008547
Avi Kivity15ad7142007-07-11 18:17:21 +03008548 cpu = get_cpu();
8549 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10008550 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10008551 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008552 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03008553 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008554 if (err)
8555 goto free_vmcs;
Paolo Bonzini35754c92015-07-29 12:05:37 +02008556 if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02008557 err = alloc_apic_access_page(kvm);
8558 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02008559 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02008560 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08008561
Sheng Yangb927a3c2009-07-21 10:42:48 +08008562 if (enable_ept) {
8563 if (!kvm->arch.ept_identity_map_addr)
8564 kvm->arch.ept_identity_map_addr =
8565 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08008566 err = init_rmode_identity_map(kvm);
8567 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02008568 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08008569 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08008570
Wincy Vanb9c237b2015-02-03 23:56:30 +08008571 if (nested)
8572 nested_vmx_setup_ctls_msrs(vmx);
8573
Wincy Van705699a2015-02-03 23:58:17 +08008574 vmx->nested.posted_intr_nv = -1;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03008575 vmx->nested.current_vmptr = -1ull;
8576 vmx->nested.current_vmcs12 = NULL;
8577
Kai Huang843e4332015-01-28 10:54:28 +08008578 /*
8579 * If PML is turned on, failure on enabling PML just results in failure
8580 * of creating the vcpu, therefore we can simplify PML logic (by
8581 * avoiding dealing with cases, such as enabling PML partially on vcpus
8582 * for the guest, etc.
8583 */
8584 if (enable_pml) {
8585 err = vmx_enable_pml(vmx);
8586 if (err)
8587 goto free_vmcs;
8588 }
8589
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008590 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08008591
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008592free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08008593 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008594free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008595 kfree(vmx->guest_msrs);
8596uninit_vcpu:
8597 kvm_vcpu_uninit(&vmx->vcpu);
8598free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08008599 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10008600 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008601 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008602}
8603
Yang, Sheng002c7f72007-07-31 14:23:01 +03008604static void __init vmx_check_processor_compat(void *rtn)
8605{
8606 struct vmcs_config vmcs_conf;
8607
8608 *(int *)rtn = 0;
8609 if (setup_vmcs_config(&vmcs_conf) < 0)
8610 *(int *)rtn = -EIO;
8611 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
8612 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
8613 smp_processor_id());
8614 *(int *)rtn = -EIO;
8615 }
8616}
8617
Sheng Yang67253af2008-04-25 10:20:22 +08008618static int get_ept_level(void)
8619{
8620 return VMX_EPT_DEFAULT_GAW + 1;
8621}
8622
Sheng Yang4b12f0d2009-04-27 20:35:42 +08008623static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08008624{
Xiao Guangrongb18d5432015-06-15 16:55:21 +08008625 u8 cache;
8626 u64 ipat = 0;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08008627
Sheng Yang522c68c2009-04-27 20:35:43 +08008628 /* For VT-d and EPT combination
Paolo Bonzini606decd2015-10-01 13:12:47 +02008629 * 1. MMIO: always map as UC
Sheng Yang522c68c2009-04-27 20:35:43 +08008630 * 2. EPT with VT-d:
8631 * a. VT-d without snooping control feature: can't guarantee the
Paolo Bonzini606decd2015-10-01 13:12:47 +02008632 * result, try to trust guest.
Sheng Yang522c68c2009-04-27 20:35:43 +08008633 * b. VT-d with snooping control feature: snooping control feature of
8634 * VT-d engine can guarantee the cache correctness. Just set it
8635 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08008636 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08008637 * consistent with host MTRR
8638 */
Paolo Bonzini606decd2015-10-01 13:12:47 +02008639 if (is_mmio) {
8640 cache = MTRR_TYPE_UNCACHABLE;
8641 goto exit;
8642 }
8643
8644 if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
Xiao Guangrongb18d5432015-06-15 16:55:21 +08008645 ipat = VMX_EPT_IPAT_BIT;
8646 cache = MTRR_TYPE_WRBACK;
8647 goto exit;
8648 }
8649
8650 if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
8651 ipat = VMX_EPT_IPAT_BIT;
Paolo Bonzini0da029e2015-07-23 08:24:42 +02008652 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Xiao Guangrongfb2799502015-07-16 03:25:56 +08008653 cache = MTRR_TYPE_WRBACK;
8654 else
8655 cache = MTRR_TYPE_UNCACHABLE;
Xiao Guangrongb18d5432015-06-15 16:55:21 +08008656 goto exit;
8657 }
8658
Xiao Guangrongff536042015-06-15 16:55:22 +08008659 cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
Xiao Guangrongb18d5432015-06-15 16:55:21 +08008660
8661exit:
8662 return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
Sheng Yang64d4d522008-10-09 16:01:57 +08008663}
8664
Sheng Yang17cc3932010-01-05 19:02:27 +08008665static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02008666{
Sheng Yang878403b2010-01-05 19:02:29 +08008667 if (enable_ept && !cpu_has_vmx_ept_1g_page())
8668 return PT_DIRECTORY_LEVEL;
8669 else
8670 /* For shadow and EPT supported 1GB page */
8671 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02008672}
8673
Xiao Guangrongfeda8052015-09-09 14:05:55 +08008674static void vmcs_set_secondary_exec_control(u32 new_ctl)
8675{
8676 /*
8677 * These bits in the secondary execution controls field
8678 * are dynamic, the others are mostly based on the hypervisor
8679 * architecture and the guest's CPUID. Do not touch the
8680 * dynamic bits.
8681 */
8682 u32 mask =
8683 SECONDARY_EXEC_SHADOW_VMCS |
8684 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
8685 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8686
8687 u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8688
8689 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
8690 (new_ctl & ~mask) | (cur_ctl & mask));
8691}
8692
Sheng Yang0e851882009-12-18 16:48:46 +08008693static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
8694{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08008695 struct kvm_cpuid_entry2 *best;
8696 struct vcpu_vmx *vmx = to_vmx(vcpu);
Xiao Guangrongfeda8052015-09-09 14:05:55 +08008697 u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08008698
Sheng Yang4e47c7a2009-12-18 16:48:47 +08008699 if (vmx_rdtscp_supported()) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08008700 bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu);
8701 if (!rdtscp_enabled)
Xiao Guangrongfeda8052015-09-09 14:05:55 +08008702 secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP;
Xiao Guangrongf36201e2015-09-09 14:05:53 +08008703
Paolo Bonzini8b972652015-09-15 17:34:42 +02008704 if (nested) {
Xiao Guangrong1cea0ce2015-09-09 14:05:57 +08008705 if (rdtscp_enabled)
Paolo Bonzini8b972652015-09-15 17:34:42 +02008706 vmx->nested.nested_vmx_secondary_ctls_high |=
8707 SECONDARY_EXEC_RDTSCP;
8708 else
8709 vmx->nested.nested_vmx_secondary_ctls_high &=
8710 ~SECONDARY_EXEC_RDTSCP;
8711 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08008712 }
Mao, Junjiead756a12012-07-02 01:18:48 +00008713
Mao, Junjiead756a12012-07-02 01:18:48 +00008714 /* Exposing INVPCID only when PCID is exposed */
8715 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
8716 if (vmx_invpcid_supported() &&
Xiao Guangrong29541bb2015-09-09 14:05:54 +08008717 (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) ||
8718 !guest_cpuid_has_pcid(vcpu))) {
Xiao Guangrongfeda8052015-09-09 14:05:55 +08008719 secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Xiao Guangrong29541bb2015-09-09 14:05:54 +08008720
Mao, Junjiead756a12012-07-02 01:18:48 +00008721 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00008722 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00008723 }
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08008724
Xiao Guangrongfeda8052015-09-09 14:05:55 +08008725 vmcs_set_secondary_exec_control(secondary_exec_ctl);
8726
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08008727 if (static_cpu_has(X86_FEATURE_PCOMMIT) && nested) {
8728 if (guest_cpuid_has_pcommit(vcpu))
8729 vmx->nested.nested_vmx_secondary_ctls_high |=
8730 SECONDARY_EXEC_PCOMMIT;
8731 else
8732 vmx->nested.nested_vmx_secondary_ctls_high &=
8733 ~SECONDARY_EXEC_PCOMMIT;
8734 }
Sheng Yang0e851882009-12-18 16:48:46 +08008735}
8736
Joerg Roedeld4330ef2010-04-22 12:33:11 +02008737static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
8738{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03008739 if (func == 1 && nested)
8740 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02008741}
8742
Yang Zhang25d92082013-08-06 12:00:32 +03008743static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
8744 struct x86_exception *fault)
8745{
Jan Kiszka533558b2014-01-04 18:47:20 +01008746 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8747 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03008748
8749 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01008750 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03008751 else
Jan Kiszka533558b2014-01-04 18:47:20 +01008752 exit_reason = EXIT_REASON_EPT_VIOLATION;
8753 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03008754 vmcs12->guest_physical_address = fault->address;
8755}
8756
Nadav Har'El155a97a2013-08-05 11:07:16 +03008757/* Callbacks for nested_ept_init_mmu_context: */
8758
8759static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
8760{
8761 /* return the page table to be shadowed - in our case, EPT12 */
8762 return get_vmcs12(vcpu)->ept_pointer;
8763}
8764
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02008765static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03008766{
Paolo Bonziniad896af2013-10-02 16:56:14 +02008767 WARN_ON(mmu_is_nested(vcpu));
8768 kvm_init_shadow_ept_mmu(vcpu,
Wincy Vanb9c237b2015-02-03 23:56:30 +08008769 to_vmx(vcpu)->nested.nested_vmx_ept_caps &
8770 VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03008771 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
8772 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
8773 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
8774
8775 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03008776}
8777
8778static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
8779{
8780 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
8781}
8782
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03008783static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
8784 u16 error_code)
8785{
8786 bool inequality, bit;
8787
8788 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
8789 inequality =
8790 (error_code & vmcs12->page_fault_error_code_mask) !=
8791 vmcs12->page_fault_error_code_match;
8792 return inequality ^ bit;
8793}
8794
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03008795static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
8796 struct x86_exception *fault)
8797{
8798 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8799
8800 WARN_ON(!is_guest_mode(vcpu));
8801
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03008802 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01008803 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
8804 vmcs_read32(VM_EXIT_INTR_INFO),
8805 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03008806 else
8807 kvm_inject_page_fault(vcpu, fault);
8808}
8809
Wanpeng Lia2bcba52014-08-21 19:46:49 +08008810static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
8811 struct vmcs12 *vmcs12)
8812{
8813 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eugene Korenevsky90904222015-03-29 23:56:27 +03008814 int maxphyaddr = cpuid_maxphyaddr(vcpu);
Wanpeng Lia2bcba52014-08-21 19:46:49 +08008815
8816 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03008817 if (!PAGE_ALIGNED(vmcs12->apic_access_addr) ||
8818 vmcs12->apic_access_addr >> maxphyaddr)
Wanpeng Lia2bcba52014-08-21 19:46:49 +08008819 return false;
8820
8821 /*
8822 * Translate L1 physical address to host physical
8823 * address for vmcs02. Keep the page pinned, so this
8824 * physical address remains valid. We keep a reference
8825 * to it so we can release it later.
8826 */
8827 if (vmx->nested.apic_access_page) /* shouldn't happen */
8828 nested_release_page(vmx->nested.apic_access_page);
8829 vmx->nested.apic_access_page =
8830 nested_get_page(vcpu, vmcs12->apic_access_addr);
8831 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008832
8833 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03008834 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) ||
8835 vmcs12->virtual_apic_page_addr >> maxphyaddr)
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008836 return false;
8837
8838 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
8839 nested_release_page(vmx->nested.virtual_apic_page);
8840 vmx->nested.virtual_apic_page =
8841 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
8842
8843 /*
8844 * Failing the vm entry is _not_ what the processor does
8845 * but it's basically the only possibility we have.
8846 * We could still enter the guest if CR8 load exits are
8847 * enabled, CR8 store exits are enabled, and virtualize APIC
8848 * access is disabled; in this case the processor would never
8849 * use the TPR shadow and we could simply clear the bit from
8850 * the execution control. But such a configuration is useless,
8851 * so let's keep the code simple.
8852 */
8853 if (!vmx->nested.virtual_apic_page)
8854 return false;
8855 }
8856
Wincy Van705699a2015-02-03 23:58:17 +08008857 if (nested_cpu_has_posted_intr(vmcs12)) {
Eugene Korenevsky90904222015-03-29 23:56:27 +03008858 if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) ||
8859 vmcs12->posted_intr_desc_addr >> maxphyaddr)
Wincy Van705699a2015-02-03 23:58:17 +08008860 return false;
8861
8862 if (vmx->nested.pi_desc_page) { /* shouldn't happen */
8863 kunmap(vmx->nested.pi_desc_page);
8864 nested_release_page(vmx->nested.pi_desc_page);
8865 }
8866 vmx->nested.pi_desc_page =
8867 nested_get_page(vcpu, vmcs12->posted_intr_desc_addr);
8868 if (!vmx->nested.pi_desc_page)
8869 return false;
8870
8871 vmx->nested.pi_desc =
8872 (struct pi_desc *)kmap(vmx->nested.pi_desc_page);
8873 if (!vmx->nested.pi_desc) {
8874 nested_release_page_clean(vmx->nested.pi_desc_page);
8875 return false;
8876 }
8877 vmx->nested.pi_desc =
8878 (struct pi_desc *)((void *)vmx->nested.pi_desc +
8879 (unsigned long)(vmcs12->posted_intr_desc_addr &
8880 (PAGE_SIZE - 1)));
8881 }
8882
Wanpeng Lia2bcba52014-08-21 19:46:49 +08008883 return true;
8884}
8885
Jan Kiszkaf41245002014-03-07 20:03:13 +01008886static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
8887{
8888 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
8889 struct vcpu_vmx *vmx = to_vmx(vcpu);
8890
8891 if (vcpu->arch.virtual_tsc_khz == 0)
8892 return;
8893
8894 /* Make sure short timeouts reliably trigger an immediate vmexit.
8895 * hrtimer_start does not guarantee this. */
8896 if (preemption_timeout <= 1) {
8897 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
8898 return;
8899 }
8900
8901 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
8902 preemption_timeout *= 1000000;
8903 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
8904 hrtimer_start(&vmx->nested.preemption_timer,
8905 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
8906}
8907
Wincy Van3af18d92015-02-03 23:49:31 +08008908static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu,
8909 struct vmcs12 *vmcs12)
8910{
8911 int maxphyaddr;
8912 u64 addr;
8913
8914 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
8915 return 0;
8916
8917 if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) {
8918 WARN_ON(1);
8919 return -EINVAL;
8920 }
8921 maxphyaddr = cpuid_maxphyaddr(vcpu);
8922
8923 if (!PAGE_ALIGNED(vmcs12->msr_bitmap) ||
8924 ((addr + PAGE_SIZE) >> maxphyaddr))
8925 return -EINVAL;
8926
8927 return 0;
8928}
8929
8930/*
8931 * Merge L0's and L1's MSR bitmap, return false to indicate that
8932 * we do not use the hardware.
8933 */
8934static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu,
8935 struct vmcs12 *vmcs12)
8936{
Wincy Van82f0dd42015-02-03 23:57:18 +08008937 int msr;
Wincy Vanf2b93282015-02-03 23:56:03 +08008938 struct page *page;
8939 unsigned long *msr_bitmap;
8940
8941 if (!nested_cpu_has_virt_x2apic_mode(vmcs12))
8942 return false;
8943
8944 page = nested_get_page(vcpu, vmcs12->msr_bitmap);
8945 if (!page) {
8946 WARN_ON(1);
8947 return false;
8948 }
8949 msr_bitmap = (unsigned long *)kmap(page);
8950 if (!msr_bitmap) {
8951 nested_release_page_clean(page);
8952 WARN_ON(1);
8953 return false;
8954 }
8955
8956 if (nested_cpu_has_virt_x2apic_mode(vmcs12)) {
Wincy Van82f0dd42015-02-03 23:57:18 +08008957 if (nested_cpu_has_apic_reg_virt(vmcs12))
8958 for (msr = 0x800; msr <= 0x8ff; msr++)
8959 nested_vmx_disable_intercept_for_msr(
8960 msr_bitmap,
8961 vmx_msr_bitmap_nested,
8962 msr, MSR_TYPE_R);
Wincy Vanf2b93282015-02-03 23:56:03 +08008963 /* TPR is allowed */
8964 nested_vmx_disable_intercept_for_msr(msr_bitmap,
8965 vmx_msr_bitmap_nested,
8966 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
8967 MSR_TYPE_R | MSR_TYPE_W);
Wincy Van608406e2015-02-03 23:57:51 +08008968 if (nested_cpu_has_vid(vmcs12)) {
8969 /* EOI and self-IPI are allowed */
8970 nested_vmx_disable_intercept_for_msr(
8971 msr_bitmap,
8972 vmx_msr_bitmap_nested,
8973 APIC_BASE_MSR + (APIC_EOI >> 4),
8974 MSR_TYPE_W);
8975 nested_vmx_disable_intercept_for_msr(
8976 msr_bitmap,
8977 vmx_msr_bitmap_nested,
8978 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
8979 MSR_TYPE_W);
8980 }
Wincy Van82f0dd42015-02-03 23:57:18 +08008981 } else {
8982 /*
8983 * Enable reading intercept of all the x2apic
8984 * MSRs. We should not rely on vmcs12 to do any
8985 * optimizations here, it may have been modified
8986 * by L1.
8987 */
8988 for (msr = 0x800; msr <= 0x8ff; msr++)
8989 __vmx_enable_intercept_for_msr(
8990 vmx_msr_bitmap_nested,
8991 msr,
8992 MSR_TYPE_R);
8993
Wincy Vanf2b93282015-02-03 23:56:03 +08008994 __vmx_enable_intercept_for_msr(
8995 vmx_msr_bitmap_nested,
8996 APIC_BASE_MSR + (APIC_TASKPRI >> 4),
Wincy Van82f0dd42015-02-03 23:57:18 +08008997 MSR_TYPE_W);
Wincy Van608406e2015-02-03 23:57:51 +08008998 __vmx_enable_intercept_for_msr(
8999 vmx_msr_bitmap_nested,
9000 APIC_BASE_MSR + (APIC_EOI >> 4),
9001 MSR_TYPE_W);
9002 __vmx_enable_intercept_for_msr(
9003 vmx_msr_bitmap_nested,
9004 APIC_BASE_MSR + (APIC_SELF_IPI >> 4),
9005 MSR_TYPE_W);
Wincy Van82f0dd42015-02-03 23:57:18 +08009006 }
Wincy Vanf2b93282015-02-03 23:56:03 +08009007 kunmap(page);
9008 nested_release_page_clean(page);
9009
9010 return true;
9011}
9012
9013static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu,
9014 struct vmcs12 *vmcs12)
9015{
Wincy Van82f0dd42015-02-03 23:57:18 +08009016 if (!nested_cpu_has_virt_x2apic_mode(vmcs12) &&
Wincy Van608406e2015-02-03 23:57:51 +08009017 !nested_cpu_has_apic_reg_virt(vmcs12) &&
Wincy Van705699a2015-02-03 23:58:17 +08009018 !nested_cpu_has_vid(vmcs12) &&
9019 !nested_cpu_has_posted_intr(vmcs12))
Wincy Vanf2b93282015-02-03 23:56:03 +08009020 return 0;
9021
9022 /*
9023 * If virtualize x2apic mode is enabled,
9024 * virtualize apic access must be disabled.
9025 */
Wincy Van82f0dd42015-02-03 23:57:18 +08009026 if (nested_cpu_has_virt_x2apic_mode(vmcs12) &&
9027 nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
Wincy Vanf2b93282015-02-03 23:56:03 +08009028 return -EINVAL;
9029
Wincy Van608406e2015-02-03 23:57:51 +08009030 /*
9031 * If virtual interrupt delivery is enabled,
9032 * we must exit on external interrupts.
9033 */
9034 if (nested_cpu_has_vid(vmcs12) &&
9035 !nested_exit_on_intr(vcpu))
9036 return -EINVAL;
9037
Wincy Van705699a2015-02-03 23:58:17 +08009038 /*
9039 * bits 15:8 should be zero in posted_intr_nv,
9040 * the descriptor address has been already checked
9041 * in nested_get_vmcs12_pages.
9042 */
9043 if (nested_cpu_has_posted_intr(vmcs12) &&
9044 (!nested_cpu_has_vid(vmcs12) ||
9045 !nested_exit_intr_ack_set(vcpu) ||
9046 vmcs12->posted_intr_nv & 0xff00))
9047 return -EINVAL;
9048
Wincy Vanf2b93282015-02-03 23:56:03 +08009049 /* tpr shadow is needed by all apicv features. */
9050 if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
9051 return -EINVAL;
9052
9053 return 0;
Wincy Van3af18d92015-02-03 23:49:31 +08009054}
9055
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009056static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
9057 unsigned long count_field,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009058 unsigned long addr_field)
Wincy Vanff651cb2014-12-11 08:52:58 +03009059{
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009060 int maxphyaddr;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009061 u64 count, addr;
9062
9063 if (vmcs12_read_any(vcpu, count_field, &count) ||
9064 vmcs12_read_any(vcpu, addr_field, &addr)) {
9065 WARN_ON(1);
9066 return -EINVAL;
9067 }
9068 if (count == 0)
9069 return 0;
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009070 maxphyaddr = cpuid_maxphyaddr(vcpu);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009071 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
9072 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
9073 pr_warn_ratelimited(
9074 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
9075 addr_field, maxphyaddr, count, addr);
9076 return -EINVAL;
9077 }
9078 return 0;
9079}
9080
9081static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
9082 struct vmcs12 *vmcs12)
9083{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009084 if (vmcs12->vm_exit_msr_load_count == 0 &&
9085 vmcs12->vm_exit_msr_store_count == 0 &&
9086 vmcs12->vm_entry_msr_load_count == 0)
9087 return 0; /* Fast path */
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009088 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009089 VM_EXIT_MSR_LOAD_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009090 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009091 VM_EXIT_MSR_STORE_ADDR) ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009092 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
Eugene Korenevsky92d71bc2015-03-29 23:56:44 +03009093 VM_ENTRY_MSR_LOAD_ADDR))
Wincy Vanff651cb2014-12-11 08:52:58 +03009094 return -EINVAL;
9095 return 0;
9096}
9097
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009098static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
9099 struct vmx_msr_entry *e)
9100{
9101 /* x2APIC MSR accesses are not allowed */
Jan Kiszka8a9781f2015-05-04 08:32:32 +02009102 if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8)
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009103 return -EINVAL;
9104 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
9105 e->index == MSR_IA32_UCODE_REV)
9106 return -EINVAL;
9107 if (e->reserved != 0)
9108 return -EINVAL;
9109 return 0;
9110}
9111
9112static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
9113 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03009114{
9115 if (e->index == MSR_FS_BASE ||
9116 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009117 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
9118 nested_vmx_msr_check_common(vcpu, e))
9119 return -EINVAL;
9120 return 0;
9121}
9122
9123static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
9124 struct vmx_msr_entry *e)
9125{
9126 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
9127 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03009128 return -EINVAL;
9129 return 0;
9130}
9131
9132/*
9133 * Load guest's/host's msr at nested entry/exit.
9134 * return 0 for success, entry index for failure.
9135 */
9136static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9137{
9138 u32 i;
9139 struct vmx_msr_entry e;
9140 struct msr_data msr;
9141
9142 msr.host_initiated = false;
9143 for (i = 0; i < count; i++) {
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009144 if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e),
9145 &e, sizeof(e))) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009146 pr_warn_ratelimited(
9147 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9148 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009149 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009150 }
9151 if (nested_vmx_load_msr_check(vcpu, &e)) {
9152 pr_warn_ratelimited(
9153 "%s check failed (%u, 0x%x, 0x%x)\n",
9154 __func__, i, e.index, e.reserved);
9155 goto fail;
9156 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009157 msr.index = e.index;
9158 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009159 if (kvm_set_msr(vcpu, &msr)) {
9160 pr_warn_ratelimited(
9161 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
9162 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +03009163 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009164 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009165 }
9166 return 0;
9167fail:
9168 return i + 1;
9169}
9170
9171static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
9172{
9173 u32 i;
9174 struct vmx_msr_entry e;
9175
9176 for (i = 0; i < count; i++) {
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009177 struct msr_data msr_info;
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009178 if (kvm_vcpu_read_guest(vcpu,
9179 gpa + i * sizeof(e),
9180 &e, 2 * sizeof(u32))) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009181 pr_warn_ratelimited(
9182 "%s cannot read MSR entry (%u, 0x%08llx)\n",
9183 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03009184 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009185 }
9186 if (nested_vmx_store_msr_check(vcpu, &e)) {
9187 pr_warn_ratelimited(
9188 "%s check failed (%u, 0x%x, 0x%x)\n",
9189 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +03009190 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009191 }
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009192 msr_info.host_initiated = false;
9193 msr_info.index = e.index;
9194 if (kvm_get_msr(vcpu, &msr_info)) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009195 pr_warn_ratelimited(
9196 "%s cannot read MSR (%u, 0x%x)\n",
9197 __func__, i, e.index);
9198 return -EINVAL;
9199 }
Paolo Bonzini54bf36a2015-04-08 15:39:23 +02009200 if (kvm_vcpu_write_guest(vcpu,
9201 gpa + i * sizeof(e) +
9202 offsetof(struct vmx_msr_entry, value),
9203 &msr_info.data, sizeof(msr_info.data))) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009204 pr_warn_ratelimited(
9205 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
Paolo Bonzini609e36d2015-04-08 15:30:38 +02009206 __func__, i, e.index, msr_info.data);
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009207 return -EINVAL;
9208 }
Wincy Vanff651cb2014-12-11 08:52:58 +03009209 }
9210 return 0;
9211}
9212
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009213/*
9214 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
9215 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +08009216 * 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 +03009217 * guest in a way that will both be appropriate to L1's requests, and our
9218 * needs. In addition to modifying the active vmcs (which is vmcs02), this
9219 * function also has additional necessary side-effects, like setting various
9220 * vcpu->arch fields.
9221 */
9222static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9223{
9224 struct vcpu_vmx *vmx = to_vmx(vcpu);
9225 u32 exec_control;
9226
9227 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
9228 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
9229 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
9230 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
9231 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
9232 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
9233 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
9234 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
9235 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
9236 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
9237 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
9238 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
9239 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
9240 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
9241 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
9242 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
9243 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
9244 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
9245 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
9246 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
9247 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
9248 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
9249 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
9250 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
9251 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
9252 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
9253 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
9254 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
9255 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
9256 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
9257 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
9258 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
9259 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
9260 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
9261 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
9262 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
9263
Jan Kiszka2996fca2014-06-16 13:59:43 +02009264 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
9265 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
9266 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
9267 } else {
9268 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
9269 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
9270 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009271 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
9272 vmcs12->vm_entry_intr_info_field);
9273 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
9274 vmcs12->vm_entry_exception_error_code);
9275 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
9276 vmcs12->vm_entry_instruction_len);
9277 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
9278 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009279 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +03009280 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009281 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
9282 vmcs12->guest_pending_dbg_exceptions);
9283 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
9284 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
9285
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009286 if (nested_cpu_has_xsaves(vmcs12))
9287 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009288 vmcs_write64(VMCS_LINK_POINTER, -1ull);
9289
Jan Kiszkaf41245002014-03-07 20:03:13 +01009290 exec_control = vmcs12->pin_based_vm_exec_control;
9291 exec_control |= vmcs_config.pin_based_exec_ctrl;
Wincy Van705699a2015-02-03 23:58:17 +08009292 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
9293
9294 if (nested_cpu_has_posted_intr(vmcs12)) {
9295 /*
9296 * Note that we use L0's vector here and in
9297 * vmx_deliver_nested_posted_interrupt.
9298 */
9299 vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv;
9300 vmx->nested.pi_pending = false;
9301 vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
9302 vmcs_write64(POSTED_INTR_DESC_ADDR,
9303 page_to_phys(vmx->nested.pi_desc_page) +
9304 (unsigned long)(vmcs12->posted_intr_desc_addr &
9305 (PAGE_SIZE - 1)));
9306 } else
9307 exec_control &= ~PIN_BASED_POSTED_INTR;
9308
Jan Kiszkaf41245002014-03-07 20:03:13 +01009309 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009310
Jan Kiszkaf41245002014-03-07 20:03:13 +01009311 vmx->nested.preemption_timer_expired = false;
9312 if (nested_cpu_has_preemption_timer(vmcs12))
9313 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +01009314
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009315 /*
9316 * Whether page-faults are trapped is determined by a combination of
9317 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
9318 * If enable_ept, L0 doesn't care about page faults and we should
9319 * set all of these to L1's desires. However, if !enable_ept, L0 does
9320 * care about (at least some) page faults, and because it is not easy
9321 * (if at all possible?) to merge L0 and L1's desires, we simply ask
9322 * to exit on each and every L2 page fault. This is done by setting
9323 * MASK=MATCH=0 and (see below) EB.PF=1.
9324 * Note that below we don't need special code to set EB.PF beyond the
9325 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
9326 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
9327 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
9328 *
9329 * A problem with this approach (when !enable_ept) is that L1 may be
9330 * injected with more page faults than it asked for. This could have
9331 * caused problems, but in practice existing hypervisors don't care.
9332 * To fix this, we will need to emulate the PFEC checking (on the L1
9333 * page tables), using walk_addr(), when injecting PFs to L1.
9334 */
9335 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
9336 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
9337 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
9338 enable_ept ? vmcs12->page_fault_error_code_match : 0);
9339
9340 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf41245002014-03-07 20:03:13 +01009341 exec_control = vmx_secondary_exec_control(vmx);
Xiao Guangronge2821622015-09-09 14:05:52 +08009342
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009343 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009344 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszkab3a2a902015-03-23 19:27:19 +01009345 SECONDARY_EXEC_RDTSCP |
Paolo Bonzini696dfd92014-05-07 11:20:54 +02009346 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Xiao Guangrong8b3e34e2015-09-09 14:05:51 +08009347 SECONDARY_EXEC_APIC_REGISTER_VIRT |
9348 SECONDARY_EXEC_PCOMMIT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009349 if (nested_cpu_has(vmcs12,
9350 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
9351 exec_control |= vmcs12->secondary_vm_exec_control;
9352
9353 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
9354 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009355 * If translation failed, no matter: This feature asks
9356 * to exit when accessing the given address, and if it
9357 * can never be accessed, this feature won't do
9358 * anything anyway.
9359 */
9360 if (!vmx->nested.apic_access_page)
9361 exec_control &=
9362 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
9363 else
9364 vmcs_write64(APIC_ACCESS_ADDR,
9365 page_to_phys(vmx->nested.apic_access_page));
Wincy Vanf2b93282015-02-03 23:56:03 +08009366 } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) &&
Paolo Bonzini35754c92015-07-29 12:05:37 +02009367 cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
Jan Kiszkaca3f2572013-12-16 12:55:46 +01009368 exec_control |=
9369 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +08009370 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009371 }
9372
Wincy Van608406e2015-02-03 23:57:51 +08009373 if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
9374 vmcs_write64(EOI_EXIT_BITMAP0,
9375 vmcs12->eoi_exit_bitmap0);
9376 vmcs_write64(EOI_EXIT_BITMAP1,
9377 vmcs12->eoi_exit_bitmap1);
9378 vmcs_write64(EOI_EXIT_BITMAP2,
9379 vmcs12->eoi_exit_bitmap2);
9380 vmcs_write64(EOI_EXIT_BITMAP3,
9381 vmcs12->eoi_exit_bitmap3);
9382 vmcs_write16(GUEST_INTR_STATUS,
9383 vmcs12->guest_intr_status);
9384 }
9385
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009386 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
9387 }
9388
9389
9390 /*
9391 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
9392 * Some constant fields are set here by vmx_set_constant_host_state().
9393 * Other fields are different per CPU, and will be set later when
9394 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
9395 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08009396 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009397
9398 /*
9399 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
9400 * entry, but only if the current (host) sp changed from the value
9401 * we wrote last (vmx->host_rsp). This cache is no longer relevant
9402 * if we switch vmcs, and rather than hold a separate cache per vmcs,
9403 * here we just force the write to happen on entry.
9404 */
9405 vmx->host_rsp = 0;
9406
9407 exec_control = vmx_exec_control(vmx); /* L0's desires */
9408 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
9409 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
9410 exec_control &= ~CPU_BASED_TPR_SHADOW;
9411 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009412
9413 if (exec_control & CPU_BASED_TPR_SHADOW) {
9414 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
9415 page_to_phys(vmx->nested.virtual_apic_page));
9416 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
9417 }
9418
Wincy Van3af18d92015-02-03 23:49:31 +08009419 if (cpu_has_vmx_msr_bitmap() &&
Wincy Van670125b2015-03-04 14:31:56 +08009420 exec_control & CPU_BASED_USE_MSR_BITMAPS) {
9421 nested_vmx_merge_msr_bitmap(vcpu, vmcs12);
9422 /* MSR_BITMAP will be set by following vmx_set_efer. */
Wincy Van3af18d92015-02-03 23:49:31 +08009423 } else
9424 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
9425
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009426 /*
Wincy Van3af18d92015-02-03 23:49:31 +08009427 * Merging of IO bitmap not currently supported.
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009428 * Rather, exit every time.
9429 */
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009430 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
9431 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
9432
9433 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
9434
9435 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
9436 * bitwise-or of what L1 wants to trap for L2, and what we want to
9437 * trap. Note that CR0.TS also needs updating - we do this later.
9438 */
9439 update_exception_bitmap(vcpu);
9440 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
9441 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
9442
Nadav Har'El8049d652013-08-05 11:07:06 +03009443 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
9444 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
9445 * bits are further modified by vmx_set_efer() below.
9446 */
Jan Kiszkaf41245002014-03-07 20:03:13 +01009447 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +03009448
9449 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
9450 * emulated by vmx_set_efer(), below.
9451 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02009452 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +03009453 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
9454 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009455 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
9456
Jan Kiszka44811c02013-08-04 17:17:27 +02009457 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009458 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02009459 vcpu->arch.pat = vmcs12->guest_ia32_pat;
9460 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009461 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
9462
9463
9464 set_cr4_guest_host_mask(vmx);
9465
Paolo Bonzini36be0b92014-02-24 12:30:04 +01009466 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
9467 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
9468
Nadav Har'El27fc51b2011-08-02 15:54:52 +03009469 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
9470 vmcs_write64(TSC_OFFSET,
9471 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
9472 else
9473 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009474
9475 if (enable_vpid) {
9476 /*
9477 * Trivially support vpid by letting L2s share their parent
9478 * L1's vpid. TODO: move to a more elaborate solution, giving
9479 * each L2 its own vpid and exposing the vpid feature to L1.
9480 */
9481 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
9482 vmx_flush_tlb(vcpu);
9483 }
9484
Nadav Har'El155a97a2013-08-05 11:07:16 +03009485 if (nested_cpu_has_ept(vmcs12)) {
9486 kvm_mmu_unload(vcpu);
9487 nested_ept_init_mmu_context(vcpu);
9488 }
9489
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009490 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
9491 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +02009492 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009493 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
9494 else
9495 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
9496 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
9497 vmx_set_efer(vcpu, vcpu->arch.efer);
9498
9499 /*
9500 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
9501 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
9502 * The CR0_READ_SHADOW is what L2 should have expected to read given
9503 * the specifications by L1; It's not enough to take
9504 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
9505 * have more bits than L1 expected.
9506 */
9507 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
9508 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
9509
9510 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
9511 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
9512
9513 /* shadow page tables on either EPT or shadow page tables */
9514 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
9515 kvm_mmu_reset_context(vcpu);
9516
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009517 if (!enable_ept)
9518 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
9519
Nadav Har'El3633cfc2013-08-05 11:07:07 +03009520 /*
9521 * L1 may access the L2's PDPTR, so save them to construct vmcs12
9522 */
9523 if (enable_ept) {
9524 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
9525 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
9526 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
9527 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
9528 }
9529
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03009530 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
9531 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
9532}
9533
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009534/*
9535 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
9536 * for running an L2 nested guest.
9537 */
9538static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
9539{
9540 struct vmcs12 *vmcs12;
9541 struct vcpu_vmx *vmx = to_vmx(vcpu);
9542 int cpu;
9543 struct loaded_vmcs *vmcs02;
Jan Kiszka384bb782013-04-20 10:52:36 +02009544 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +03009545 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009546
9547 if (!nested_vmx_check_permission(vcpu) ||
9548 !nested_vmx_check_vmcs12(vcpu))
9549 return 1;
9550
9551 skip_emulated_instruction(vcpu);
9552 vmcs12 = get_vmcs12(vcpu);
9553
Abel Gordon012f83c2013-04-18 14:39:25 +03009554 if (enable_shadow_vmcs)
9555 copy_shadow_to_vmcs12(vmx);
9556
Nadav Har'El7c177932011-05-25 23:12:04 +03009557 /*
9558 * The nested entry process starts with enforcing various prerequisites
9559 * on vmcs12 as required by the Intel SDM, and act appropriately when
9560 * they fail: As the SDM explains, some conditions should cause the
9561 * instruction to fail, while others will cause the instruction to seem
9562 * to succeed, but return an EXIT_REASON_INVALID_STATE.
9563 * To speed up the normal (success) code path, we should avoid checking
9564 * for misconfigurations which will anyway be caught by the processor
9565 * when using the merged vmcs02.
9566 */
9567 if (vmcs12->launch_state == launch) {
9568 nested_vmx_failValid(vcpu,
9569 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
9570 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
9571 return 1;
9572 }
9573
Jan Kiszka6dfacad2013-12-04 08:58:54 +01009574 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
9575 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +02009576 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
9577 return 1;
9578 }
9579
Wincy Van3af18d92015-02-03 23:49:31 +08009580 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +03009581 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
9582 return 1;
9583 }
9584
Wincy Van3af18d92015-02-03 23:49:31 +08009585 if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +03009586 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
9587 return 1;
9588 }
9589
Wincy Vanf2b93282015-02-03 23:56:03 +08009590 if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) {
9591 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
9592 return 1;
9593 }
9594
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03009595 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
9596 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
9597 return 1;
9598 }
9599
Nadav Har'El7c177932011-05-25 23:12:04 +03009600 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009601 vmx->nested.nested_vmx_true_procbased_ctls_low,
9602 vmx->nested.nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03009603 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009604 vmx->nested.nested_vmx_secondary_ctls_low,
9605 vmx->nested.nested_vmx_secondary_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03009606 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009607 vmx->nested.nested_vmx_pinbased_ctls_low,
9608 vmx->nested.nested_vmx_pinbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03009609 !vmx_control_verify(vmcs12->vm_exit_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009610 vmx->nested.nested_vmx_true_exit_ctls_low,
9611 vmx->nested.nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03009612 !vmx_control_verify(vmcs12->vm_entry_controls,
Wincy Vanb9c237b2015-02-03 23:56:30 +08009613 vmx->nested.nested_vmx_true_entry_ctls_low,
9614 vmx->nested.nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +03009615 {
9616 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
9617 return 1;
9618 }
9619
9620 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
9621 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
9622 nested_vmx_failValid(vcpu,
9623 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
9624 return 1;
9625 }
9626
Wincy Vanb9c237b2015-02-03 23:56:30 +08009627 if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03009628 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
9629 nested_vmx_entry_failure(vcpu, vmcs12,
9630 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
9631 return 1;
9632 }
9633 if (vmcs12->vmcs_link_pointer != -1ull) {
9634 nested_vmx_entry_failure(vcpu, vmcs12,
9635 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
9636 return 1;
9637 }
9638
9639 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +02009640 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +02009641 * are performed on the field for the IA32_EFER MSR:
9642 * - Bits reserved in the IA32_EFER MSR must be 0.
9643 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
9644 * the IA-32e mode guest VM-exit control. It must also be identical
9645 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
9646 * CR0.PG) is 1.
9647 */
9648 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
9649 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
9650 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
9651 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
9652 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
9653 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
9654 nested_vmx_entry_failure(vcpu, vmcs12,
9655 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
9656 return 1;
9657 }
9658 }
9659
9660 /*
9661 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
9662 * IA32_EFER MSR must be 0 in the field for that register. In addition,
9663 * the values of the LMA and LME bits in the field must each be that of
9664 * the host address-space size VM-exit control.
9665 */
9666 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
9667 ia32e = (vmcs12->vm_exit_controls &
9668 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
9669 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
9670 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
9671 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
9672 nested_vmx_entry_failure(vcpu, vmcs12,
9673 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
9674 return 1;
9675 }
9676 }
9677
9678 /*
Nadav Har'El7c177932011-05-25 23:12:04 +03009679 * We're finally done with prerequisite checking, and can start with
9680 * the nested entry.
9681 */
9682
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009683 vmcs02 = nested_get_current_vmcs02(vmx);
9684 if (!vmcs02)
9685 return -ENOMEM;
9686
9687 enter_guest_mode(vcpu);
9688
9689 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
9690
Jan Kiszka2996fca2014-06-16 13:59:43 +02009691 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
9692 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
9693
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009694 cpu = get_cpu();
9695 vmx->loaded_vmcs = vmcs02;
9696 vmx_vcpu_put(vcpu);
9697 vmx_vcpu_load(vcpu, cpu);
9698 vcpu->cpu = cpu;
9699 put_cpu();
9700
Jan Kiszka36c3cc42013-02-23 22:35:37 +01009701 vmx_segment_cache_clear(vmx);
9702
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009703 prepare_vmcs02(vcpu, vmcs12);
9704
Wincy Vanff651cb2014-12-11 08:52:58 +03009705 msr_entry_idx = nested_vmx_load_msr(vcpu,
9706 vmcs12->vm_entry_msr_load_addr,
9707 vmcs12->vm_entry_msr_load_count);
9708 if (msr_entry_idx) {
9709 leave_guest_mode(vcpu);
9710 vmx_load_vmcs01(vcpu);
9711 nested_vmx_entry_failure(vcpu, vmcs12,
9712 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
9713 return 1;
9714 }
9715
9716 vmcs12->launch_state = 1;
9717
Jan Kiszka6dfacad2013-12-04 08:58:54 +01009718 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
Joel Schopp5cb56052015-03-02 13:43:31 -06009719 return kvm_vcpu_halt(vcpu);
Jan Kiszka6dfacad2013-12-04 08:58:54 +01009720
Jan Kiszka7af40ad32014-01-04 18:47:23 +01009721 vmx->nested.nested_run_pending = 1;
9722
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009723 /*
9724 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
9725 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
9726 * returned as far as L1 is concerned. It will only return (and set
9727 * the success flag) when L2 exits (see nested_vmx_vmexit()).
9728 */
9729 return 1;
9730}
9731
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009732/*
9733 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
9734 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
9735 * This function returns the new value we should put in vmcs12.guest_cr0.
9736 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
9737 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
9738 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
9739 * didn't trap the bit, because if L1 did, so would L0).
9740 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
9741 * been modified by L2, and L1 knows it. So just leave the old value of
9742 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
9743 * isn't relevant, because if L0 traps this bit it can set it to anything.
9744 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
9745 * changed these bits, and therefore they need to be updated, but L0
9746 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
9747 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
9748 */
9749static inline unsigned long
9750vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9751{
9752 return
9753 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
9754 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
9755 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
9756 vcpu->arch.cr0_guest_owned_bits));
9757}
9758
9759static inline unsigned long
9760vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9761{
9762 return
9763 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
9764 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
9765 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
9766 vcpu->arch.cr4_guest_owned_bits));
9767}
9768
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009769static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
9770 struct vmcs12 *vmcs12)
9771{
9772 u32 idt_vectoring;
9773 unsigned int nr;
9774
Gleb Natapov851eb6672013-09-25 12:51:34 +03009775 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009776 nr = vcpu->arch.exception.nr;
9777 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
9778
9779 if (kvm_exception_is_soft(nr)) {
9780 vmcs12->vm_exit_instruction_len =
9781 vcpu->arch.event_exit_inst_len;
9782 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
9783 } else
9784 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
9785
9786 if (vcpu->arch.exception.has_error_code) {
9787 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
9788 vmcs12->idt_vectoring_error_code =
9789 vcpu->arch.exception.error_code;
9790 }
9791
9792 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +01009793 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009794 vmcs12->idt_vectoring_info_field =
9795 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
9796 } else if (vcpu->arch.interrupt.pending) {
9797 nr = vcpu->arch.interrupt.nr;
9798 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
9799
9800 if (vcpu->arch.interrupt.soft) {
9801 idt_vectoring |= INTR_TYPE_SOFT_INTR;
9802 vmcs12->vm_entry_instruction_len =
9803 vcpu->arch.event_exit_inst_len;
9804 } else
9805 idt_vectoring |= INTR_TYPE_EXT_INTR;
9806
9807 vmcs12->idt_vectoring_info_field = idt_vectoring;
9808 }
9809}
9810
Jan Kiszkab6b8a142014-03-07 20:03:12 +01009811static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
9812{
9813 struct vcpu_vmx *vmx = to_vmx(vcpu);
9814
Jan Kiszkaf41245002014-03-07 20:03:13 +01009815 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
9816 vmx->nested.preemption_timer_expired) {
9817 if (vmx->nested.nested_run_pending)
9818 return -EBUSY;
9819 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
9820 return 0;
9821 }
9822
Jan Kiszkab6b8a142014-03-07 20:03:12 +01009823 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +01009824 if (vmx->nested.nested_run_pending ||
9825 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01009826 return -EBUSY;
9827 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
9828 NMI_VECTOR | INTR_TYPE_NMI_INTR |
9829 INTR_INFO_VALID_MASK, 0);
9830 /*
9831 * The NMI-triggered VM exit counts as injection:
9832 * clear this one and block further NMIs.
9833 */
9834 vcpu->arch.nmi_pending = 0;
9835 vmx_set_nmi_mask(vcpu, true);
9836 return 0;
9837 }
9838
9839 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
9840 nested_exit_on_intr(vcpu)) {
9841 if (vmx->nested.nested_run_pending)
9842 return -EBUSY;
9843 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
Wincy Van705699a2015-02-03 23:58:17 +08009844 return 0;
Jan Kiszkab6b8a142014-03-07 20:03:12 +01009845 }
9846
Wincy Van705699a2015-02-03 23:58:17 +08009847 return vmx_complete_nested_posted_interrupt(vcpu);
Jan Kiszkab6b8a142014-03-07 20:03:12 +01009848}
9849
Jan Kiszkaf41245002014-03-07 20:03:13 +01009850static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
9851{
9852 ktime_t remaining =
9853 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
9854 u64 value;
9855
9856 if (ktime_to_ns(remaining) <= 0)
9857 return 0;
9858
9859 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
9860 do_div(value, 1000000);
9861 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9862}
9863
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009864/*
9865 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
9866 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
9867 * and this function updates it to reflect the changes to the guest state while
9868 * L2 was running (and perhaps made some exits which were handled directly by L0
9869 * without going back to L1), and to reflect the exit reason.
9870 * Note that we do not have to copy here all VMCS fields, just those that
9871 * could have changed by the L2 guest or the exit - i.e., the guest-state and
9872 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
9873 * which already writes to vmcs12 directly.
9874 */
Jan Kiszka533558b2014-01-04 18:47:20 +01009875static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
9876 u32 exit_reason, u32 exit_intr_info,
9877 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009878{
9879 /* update guest state fields: */
9880 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
9881 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
9882
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009883 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
9884 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
9885 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
9886
9887 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
9888 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
9889 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
9890 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
9891 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
9892 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
9893 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
9894 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
9895 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
9896 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
9897 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
9898 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
9899 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
9900 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
9901 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
9902 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
9903 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
9904 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
9905 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
9906 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
9907 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
9908 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
9909 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
9910 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
9911 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
9912 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
9913 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
9914 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
9915 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
9916 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
9917 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
9918 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
9919 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
9920 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
9921 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
9922 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
9923
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009924 vmcs12->guest_interruptibility_info =
9925 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
9926 vmcs12->guest_pending_dbg_exceptions =
9927 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +01009928 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
9929 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
9930 else
9931 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009932
Jan Kiszkaf41245002014-03-07 20:03:13 +01009933 if (nested_cpu_has_preemption_timer(vmcs12)) {
9934 if (vmcs12->vm_exit_controls &
9935 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
9936 vmcs12->vmx_preemption_timer_value =
9937 vmx_get_preemption_timer_value(vcpu);
9938 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
9939 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +08009940
Nadav Har'El3633cfc2013-08-05 11:07:07 +03009941 /*
9942 * In some cases (usually, nested EPT), L2 is allowed to change its
9943 * own CR3 without exiting. If it has changed it, we must keep it.
9944 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
9945 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
9946 *
9947 * Additionally, restore L2's PDPTR to vmcs12.
9948 */
9949 if (enable_ept) {
9950 vmcs12->guest_cr3 = vmcs_read64(GUEST_CR3);
9951 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
9952 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
9953 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
9954 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
9955 }
9956
Wincy Van608406e2015-02-03 23:57:51 +08009957 if (nested_cpu_has_vid(vmcs12))
9958 vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS);
9959
Jan Kiszkac18911a2013-03-13 16:06:41 +01009960 vmcs12->vm_entry_controls =
9961 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +02009962 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +01009963
Jan Kiszka2996fca2014-06-16 13:59:43 +02009964 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
9965 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
9966 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
9967 }
9968
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009969 /* TODO: These cannot have changed unless we have MSR bitmaps and
9970 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +02009971 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009972 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +02009973 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
9974 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009975 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
9976 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
9977 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzini36be0b92014-02-24 12:30:04 +01009978 if (vmx_mpx_supported())
9979 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009980 if (nested_cpu_has_xsaves(vmcs12))
9981 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009982
9983 /* update exit information fields: */
9984
Jan Kiszka533558b2014-01-04 18:47:20 +01009985 vmcs12->vm_exit_reason = exit_reason;
9986 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009987
Jan Kiszka533558b2014-01-04 18:47:20 +01009988 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +02009989 if ((vmcs12->vm_exit_intr_info &
9990 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9991 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
9992 vmcs12->vm_exit_intr_error_code =
9993 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009994 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009995 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
9996 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9997
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009998 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
9999 /* vm_entry_intr_info_field is cleared on exit. Emulate this
10000 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010001 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010002
10003 /*
10004 * Transfer the event that L0 or L1 may wanted to inject into
10005 * L2 to IDT_VECTORING_INFO_FIELD.
10006 */
10007 vmcs12_save_pending_event(vcpu, vmcs12);
10008 }
10009
10010 /*
10011 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
10012 * preserved above and would only end up incorrectly in L1.
10013 */
10014 vcpu->arch.nmi_injected = false;
10015 kvm_clear_exception_queue(vcpu);
10016 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010017}
10018
10019/*
10020 * A part of what we need to when the nested L2 guest exits and we want to
10021 * run its L1 parent, is to reset L1's guest state to the host state specified
10022 * in vmcs12.
10023 * This function is to be called not only on normal nested exit, but also on
10024 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
10025 * Failures During or After Loading Guest State").
10026 * This function should be called when the active VMCS is L1's (vmcs01).
10027 */
Jan Kiszka733568f2013-02-23 15:07:47 +010010028static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
10029 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010030{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010031 struct kvm_segment seg;
10032
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010033 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
10034 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +020010035 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010036 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
10037 else
10038 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
10039 vmx_set_efer(vcpu, vcpu->arch.efer);
10040
10041 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
10042 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -070010043 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010044 /*
10045 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
10046 * actually changed, because it depends on the current state of
10047 * fpu_active (which may have changed).
10048 * Note that vmx_set_cr0 refers to efer set above.
10049 */
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +020010050 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010051 /*
10052 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
10053 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
10054 * but we also need to update cr0_guest_host_mask and exception_bitmap.
10055 */
10056 update_exception_bitmap(vcpu);
10057 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
10058 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
10059
10060 /*
10061 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
10062 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
10063 */
10064 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
10065 kvm_set_cr4(vcpu, vmcs12->host_cr4);
10066
Jan Kiszka29bf08f2013-12-28 16:31:52 +010010067 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +030010068
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010069 kvm_set_cr3(vcpu, vmcs12->host_cr3);
10070 kvm_mmu_reset_context(vcpu);
10071
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +030010072 if (!enable_ept)
10073 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
10074
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010075 if (enable_vpid) {
10076 /*
10077 * Trivially support vpid by letting L2s share their parent
10078 * L1's vpid. TODO: move to a more elaborate solution, giving
10079 * each L2 its own vpid and exposing the vpid feature to L1.
10080 */
10081 vmx_flush_tlb(vcpu);
10082 }
10083
10084
10085 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
10086 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
10087 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
10088 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
10089 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010090
Paolo Bonzini36be0b92014-02-24 12:30:04 +010010091 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
10092 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
10093 vmcs_write64(GUEST_BNDCFGS, 0);
10094
Jan Kiszka44811c02013-08-04 17:17:27 +020010095 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010096 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +020010097 vcpu->arch.pat = vmcs12->host_ia32_pat;
10098 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010099 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
10100 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
10101 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010102
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010103 /* Set L1 segment info according to Intel SDM
10104 27.5.2 Loading Host Segment and Descriptor-Table Registers */
10105 seg = (struct kvm_segment) {
10106 .base = 0,
10107 .limit = 0xFFFFFFFF,
10108 .selector = vmcs12->host_cs_selector,
10109 .type = 11,
10110 .present = 1,
10111 .s = 1,
10112 .g = 1
10113 };
10114 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
10115 seg.l = 1;
10116 else
10117 seg.db = 1;
10118 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
10119 seg = (struct kvm_segment) {
10120 .base = 0,
10121 .limit = 0xFFFFFFFF,
10122 .type = 3,
10123 .present = 1,
10124 .s = 1,
10125 .db = 1,
10126 .g = 1
10127 };
10128 seg.selector = vmcs12->host_ds_selector;
10129 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
10130 seg.selector = vmcs12->host_es_selector;
10131 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
10132 seg.selector = vmcs12->host_ss_selector;
10133 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
10134 seg.selector = vmcs12->host_fs_selector;
10135 seg.base = vmcs12->host_fs_base;
10136 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
10137 seg.selector = vmcs12->host_gs_selector;
10138 seg.base = vmcs12->host_gs_base;
10139 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
10140 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +030010141 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +080010142 .limit = 0x67,
10143 .selector = vmcs12->host_tr_selector,
10144 .type = 11,
10145 .present = 1
10146 };
10147 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
10148
Jan Kiszka503cd0c2013-03-03 13:05:44 +010010149 kvm_set_dr(vcpu, 7, 0x400);
10150 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +030010151
Wincy Van3af18d92015-02-03 23:49:31 +080010152 if (cpu_has_vmx_msr_bitmap())
10153 vmx_set_msr_bitmap(vcpu);
10154
Wincy Vanff651cb2014-12-11 08:52:58 +030010155 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
10156 vmcs12->vm_exit_msr_load_count))
10157 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010158}
10159
10160/*
10161 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
10162 * and modify vmcs12 to make it see what it would expect to see there if
10163 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
10164 */
Jan Kiszka533558b2014-01-04 18:47:20 +010010165static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
10166 u32 exit_intr_info,
10167 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010168{
10169 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010170 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
10171
Jan Kiszka5f3d5792013-04-14 12:12:46 +020010172 /* trying to cancel vmlaunch/vmresume is a bug */
10173 WARN_ON_ONCE(vmx->nested.nested_run_pending);
10174
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010175 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +010010176 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
10177 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010178
Wincy Vanff651cb2014-12-11 08:52:58 +030010179 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
10180 vmcs12->vm_exit_msr_store_count))
10181 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
10182
Wanpeng Lif3380ca52014-08-05 12:42:23 +080010183 vmx_load_vmcs01(vcpu);
10184
Bandan Das77b0f5d2014-04-19 18:17:45 -040010185 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
10186 && nested_exit_intr_ack_set(vcpu)) {
10187 int irq = kvm_cpu_get_interrupt(vcpu);
10188 WARN_ON(irq < 0);
10189 vmcs12->vm_exit_intr_info = irq |
10190 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
10191 }
10192
Jan Kiszka542060e2014-01-04 18:47:21 +010010193 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
10194 vmcs12->exit_qualification,
10195 vmcs12->idt_vectoring_info_field,
10196 vmcs12->vm_exit_intr_info,
10197 vmcs12->vm_exit_intr_error_code,
10198 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010199
Gleb Natapov2961e8762013-11-25 15:37:13 +020010200 vm_entry_controls_init(vmx, vmcs_read32(VM_ENTRY_CONTROLS));
10201 vm_exit_controls_init(vmx, vmcs_read32(VM_EXIT_CONTROLS));
Jan Kiszka36c3cc42013-02-23 22:35:37 +010010202 vmx_segment_cache_clear(vmx);
10203
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010204 /* if no vmcs02 cache requested, remove the one we used */
10205 if (VMCS02_POOL_SIZE == 0)
10206 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
10207
10208 load_vmcs12_host_state(vcpu, vmcs12);
10209
Nadav Har'El27fc51b2011-08-02 15:54:52 +030010210 /* Update TSC_OFFSET if TSC was changed while L2 ran */
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010211 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
10212
10213 /* This is needed for same reason as it was needed in prepare_vmcs02 */
10214 vmx->host_rsp = 0;
10215
10216 /* Unpin physical memory we referred to in vmcs02 */
10217 if (vmx->nested.apic_access_page) {
10218 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010219 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010220 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010221 if (vmx->nested.virtual_apic_page) {
10222 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010223 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +080010224 }
Wincy Van705699a2015-02-03 23:58:17 +080010225 if (vmx->nested.pi_desc_page) {
10226 kunmap(vmx->nested.pi_desc_page);
10227 nested_release_page(vmx->nested.pi_desc_page);
10228 vmx->nested.pi_desc_page = NULL;
10229 vmx->nested.pi_desc = NULL;
10230 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010231
10232 /*
Tang Chen38b99172014-09-24 15:57:54 +080010233 * We are now running in L2, mmu_notifier will force to reload the
10234 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
10235 */
10236 kvm_vcpu_reload_apic_access_page(vcpu);
10237
10238 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010239 * Exiting from L2 to L1, we're now back to L1 which thinks it just
10240 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
10241 * success or failure flag accordingly.
10242 */
10243 if (unlikely(vmx->fail)) {
10244 vmx->fail = 0;
10245 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
10246 } else
10247 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010248 if (enable_shadow_vmcs)
10249 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010250
10251 /* in case we halted in L2 */
10252 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +030010253}
10254
Nadav Har'El7c177932011-05-25 23:12:04 +030010255/*
Jan Kiszka42124922014-01-04 18:47:19 +010010256 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
10257 */
10258static void vmx_leave_nested(struct kvm_vcpu *vcpu)
10259{
10260 if (is_guest_mode(vcpu))
Jan Kiszka533558b2014-01-04 18:47:20 +010010261 nested_vmx_vmexit(vcpu, -1, 0, 0);
Jan Kiszka42124922014-01-04 18:47:19 +010010262 free_nested(to_vmx(vcpu));
10263}
10264
10265/*
Nadav Har'El7c177932011-05-25 23:12:04 +030010266 * L1's failure to enter L2 is a subset of a normal exit, as explained in
10267 * 23.7 "VM-entry failures during or after loading guest state" (this also
10268 * lists the acceptable exit-reason and exit-qualification parameters).
10269 * It should only be called before L2 actually succeeded to run, and when
10270 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
10271 */
10272static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
10273 struct vmcs12 *vmcs12,
10274 u32 reason, unsigned long qualification)
10275{
10276 load_vmcs12_host_state(vcpu, vmcs12);
10277 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
10278 vmcs12->exit_qualification = qualification;
10279 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +030010280 if (enable_shadow_vmcs)
10281 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +030010282}
10283
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020010284static int vmx_check_intercept(struct kvm_vcpu *vcpu,
10285 struct x86_instruction_info *info,
10286 enum x86_intercept_stage stage)
10287{
10288 return X86EMUL_CONTINUE;
10289}
10290
Paolo Bonzini48d89b92014-08-26 13:27:46 +020010291static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010292{
Radim Krčmářb4a2d312014-08-21 18:08:08 +020010293 if (ple_gap)
10294 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010295}
10296
Kai Huang843e4332015-01-28 10:54:28 +080010297static void vmx_slot_enable_log_dirty(struct kvm *kvm,
10298 struct kvm_memory_slot *slot)
10299{
10300 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
10301 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
10302}
10303
10304static void vmx_slot_disable_log_dirty(struct kvm *kvm,
10305 struct kvm_memory_slot *slot)
10306{
10307 kvm_mmu_slot_set_dirty(kvm, slot);
10308}
10309
10310static void vmx_flush_log_dirty(struct kvm *kvm)
10311{
10312 kvm_flush_pml_buffers(kvm);
10313}
10314
10315static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
10316 struct kvm_memory_slot *memslot,
10317 gfn_t offset, unsigned long mask)
10318{
10319 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
10320}
10321
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +030010322static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -080010323 .cpu_has_kvm_support = cpu_has_kvm_support,
10324 .disabled_by_bios = vmx_disabled_by_bios,
10325 .hardware_setup = hardware_setup,
10326 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +030010327 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010328 .hardware_enable = hardware_enable,
10329 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +080010330 .cpu_has_accelerated_tpr = report_flexpriority,
Paolo Bonzini6d396b52015-04-01 14:25:33 +020010331 .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010332
10333 .vcpu_create = vmx_create_vcpu,
10334 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +030010335 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010336
Avi Kivity04d2cc72007-09-10 18:10:54 +030010337 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010338 .vcpu_load = vmx_vcpu_load,
10339 .vcpu_put = vmx_vcpu_put,
10340
Jan Kiszkac8639012012-09-21 05:42:55 +020010341 .update_db_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010342 .get_msr = vmx_get_msr,
10343 .set_msr = vmx_set_msr,
10344 .get_segment_base = vmx_get_segment_base,
10345 .get_segment = vmx_get_segment,
10346 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +020010347 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010348 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +020010349 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +020010350 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +030010351 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010352 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010353 .set_cr3 = vmx_set_cr3,
10354 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010355 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010356 .get_idt = vmx_get_idt,
10357 .set_idt = vmx_set_idt,
10358 .get_gdt = vmx_get_gdt,
10359 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +010010360 .get_dr6 = vmx_get_dr6,
10361 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +030010362 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +010010363 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030010364 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010365 .get_rflags = vmx_get_rflags,
10366 .set_rflags = vmx_set_rflags,
Paolo Bonzini0fdd74f2015-05-20 11:33:43 +020010367 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +020010368 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010369
10370 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010371
Avi Kivity6aa8b732006-12-10 02:21:36 -080010372 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +020010373 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010374 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -040010375 .set_interrupt_shadow = vmx_set_interrupt_shadow,
10376 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +020010377 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +030010378 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010379 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +020010380 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +030010381 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +020010382 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010383 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +010010384 .get_nmi_mask = vmx_get_nmi_mask,
10385 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010386 .enable_nmi_window = enable_nmi_window,
10387 .enable_irq_window = enable_irq_window,
10388 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +080010389 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +080010390 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Paolo Bonzinid50ab6c2015-07-29 11:49:59 +020010391 .cpu_uses_apicv = vmx_cpu_uses_apicv,
Yang Zhangc7c9c562013-01-25 10:18:51 +080010392 .load_eoi_exitmap = vmx_load_eoi_exitmap,
10393 .hwapic_irr_update = vmx_hwapic_irr_update,
10394 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +080010395 .sync_pir_to_irr = vmx_sync_pir_to_irr,
10396 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +030010397
Izik Eiduscbc94022007-10-25 00:29:55 +020010398 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +080010399 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +080010400 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -030010401
Avi Kivity586f9602010-11-18 13:09:54 +020010402 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +020010403
Sheng Yang17cc3932010-01-05 19:02:27 +080010404 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +080010405
10406 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +080010407
10408 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +000010409 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +020010410
10411 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +080010412
10413 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -100010414
Joerg Roedel4051b182011-03-25 09:44:49 +010010415 .set_tsc_khz = vmx_set_tsc_khz,
Will Auldba904632012-11-29 12:42:50 -080010416 .read_tsc_offset = vmx_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -100010417 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -100010418 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +010010419 .compute_tsc_offset = vmx_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +030010420 .read_l1_tsc = vmx_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +020010421
10422 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +020010423
10424 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +080010425 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +000010426 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +080010427 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +010010428
10429 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +020010430
10431 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +080010432
10433 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
10434 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
10435 .flush_log_dirty = vmx_flush_log_dirty,
10436 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Wei Huang25462f72015-06-19 15:45:05 +020010437
10438 .pmu_ops = &intel_pmu_ops,
Avi Kivity6aa8b732006-12-10 02:21:36 -080010439};
10440
10441static int __init vmx_init(void)
10442{
Tiejun Chen34a1cd62014-10-28 10:14:48 +080010443 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
10444 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +030010445 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +080010446 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +080010447
Dave Young2965faa2015-09-09 15:38:55 -070010448#ifdef CONFIG_KEXEC_CORE
Zhang Yanfei8f536b72012-12-06 23:43:34 +080010449 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
10450 crash_vmclear_local_loaded_vmcss);
10451#endif
10452
He, Qingfdef3ad2007-04-30 09:45:24 +030010453 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -080010454}
10455
10456static void __exit vmx_exit(void)
10457{
Dave Young2965faa2015-09-09 15:38:55 -070010458#ifdef CONFIG_KEXEC_CORE
Monam Agarwal3b63a432014-03-22 12:28:10 +053010459 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +080010460 synchronize_rcu();
10461#endif
10462
Zhang Xiantaocb498ea2007-11-14 20:39:31 +080010463 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -080010464}
10465
10466module_init(vmx_init)
10467module_exit(vmx_exit)