blob: de5ce82b2436ed692fc1cf573e071d1914abba80 [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>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030031#include <linux/ftrace_event.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 Kiszkaf4124502014-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>
Dexuan Cui2acf9232010-06-10 11:27:12 +080043#include <asm/i387.h>
44#include <asm/xcr.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020045#include <asm/perf_event.h>
Paolo Bonzini81908bf2014-02-21 10:32:27 +010046#include <asm/debugreg.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080047#include <asm/kexec.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080048
Marcelo Tosatti229456f2009-06-17 09:22:14 -030049#include "trace.h"
50
Avi Kivity4ecac3f2008-05-13 13:23:38 +030051#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040052#define __ex_clear(x, reg) \
53 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030054
Avi Kivity6aa8b732006-12-10 02:21:36 -080055MODULE_AUTHOR("Qumranet");
56MODULE_LICENSE("GPL");
57
Josh Triplette9bda3b2012-03-20 23:33:51 -070058static const struct x86_cpu_id vmx_cpu_id[] = {
59 X86_FEATURE_MATCH(X86_FEATURE_VMX),
60 {}
61};
62MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
63
Rusty Russell476bc002012-01-13 09:32:18 +103064static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020065module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080066
Rusty Russell476bc002012-01-13 09:32:18 +103067static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020068module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020069
Rusty Russell476bc002012-01-13 09:32:18 +103070static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020071module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080072
Rusty Russell476bc002012-01-13 09:32:18 +103073static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070074module_param_named(unrestricted_guest,
75 enable_unrestricted_guest, bool, S_IRUGO);
76
Xudong Hao83c3a332012-05-28 19:33:35 +080077static bool __read_mostly enable_ept_ad_bits = 1;
78module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
79
Avi Kivitya27685c2012-06-12 20:30:18 +030080static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020081module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030082
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080084module_param(vmm_exclusive, bool, S_IRUGO);
85
Rusty Russell476bc002012-01-13 09:32:18 +103086static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030087module_param(fasteoi, bool, S_IRUGO);
88
Yang Zhang5a717852013-04-11 19:25:16 +080089static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080090module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080091
Abel Gordonabc4fc52013-04-18 14:35:25 +030092static bool __read_mostly enable_shadow_vmcs = 1;
93module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +030094/*
95 * If nested=1, nested virtualization is supported, i.e., guests may use
96 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
97 * use VMX instructions.
98 */
Rusty Russell476bc002012-01-13 09:32:18 +103099static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +0300100module_param(nested, bool, S_IRUGO);
101
Wanpeng Li20300092014-12-02 19:14:59 +0800102static u64 __read_mostly host_xss;
103
Kai Huang843e4332015-01-28 10:54:28 +0800104static bool __read_mostly enable_pml = 1;
105module_param_named(pml, enable_pml, bool, S_IRUGO);
106
Gleb Natapov50378782013-02-04 16:00:28 +0200107#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
108#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200109#define KVM_VM_CR0_ALWAYS_ON \
110 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200111#define KVM_CR4_GUEST_OWNED_BITS \
112 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
Andy Lutomirski52ce3c22014-10-07 17:16:21 -0700113 | X86_CR4_OSXMMEXCPT | X86_CR4_TSD)
Avi Kivity4c386092009-12-07 12:26:18 +0200114
Avi Kivitycdc0e242009-12-06 17:21:14 +0200115#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
116#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
117
Avi Kivity78ac8b42010-04-08 18:19:35 +0300118#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
119
Jan Kiszkaf4124502014-03-07 20:03:13 +0100120#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
121
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800122/*
123 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
124 * ple_gap: upper bound on the amount of time between two successive
125 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500126 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800127 * ple_window: upper bound on the amount of time a guest is allowed to execute
128 * in a PAUSE loop. Tests indicate that most spinlocks are held for
129 * less than 2^12 cycles
130 * Time is measured based on a counter that runs at the same rate as the TSC,
131 * refer SDM volume 3b section 21.6.13 & 22.1.3.
132 */
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200133#define KVM_VMX_DEFAULT_PLE_GAP 128
134#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
135#define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2
136#define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0
137#define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \
138 INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW
139
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800140static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
141module_param(ple_gap, int, S_IRUGO);
142
143static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
144module_param(ple_window, int, S_IRUGO);
145
Radim Krčmářb4a2d312014-08-21 18:08:08 +0200146/* Default doubles per-vcpu window every exit. */
147static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW;
148module_param(ple_window_grow, int, S_IRUGO);
149
150/* Default resets per-vcpu window every exit to ple_window. */
151static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK;
152module_param(ple_window_shrink, int, S_IRUGO);
153
154/* Default is to compute the maximum so we can never overflow. */
155static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
156static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
157module_param(ple_window_max, int, S_IRUGO);
158
Avi Kivity83287ea422012-09-16 15:10:57 +0300159extern const ulong vmx_return;
160
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200161#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300162#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300163
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400164struct vmcs {
165 u32 revision_id;
166 u32 abort;
167 char data[0];
168};
169
Nadav Har'Eld462b812011-05-24 15:26:10 +0300170/*
171 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
172 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
173 * loaded on this CPU (so we can clear them if the CPU goes down).
174 */
175struct loaded_vmcs {
176 struct vmcs *vmcs;
177 int cpu;
178 int launched;
179 struct list_head loaded_vmcss_on_cpu_link;
180};
181
Avi Kivity26bb0982009-09-07 11:14:12 +0300182struct shared_msr_entry {
183 unsigned index;
184 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200185 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300186};
187
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300188/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300189 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
190 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
191 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
192 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
193 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
194 * More than one of these structures may exist, if L1 runs multiple L2 guests.
195 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
196 * underlying hardware which will be used to run L2.
197 * This structure is packed to ensure that its layout is identical across
198 * machines (necessary for live migration).
199 * If there are changes in this struct, VMCS12_REVISION must be changed.
200 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300201typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300202struct __packed vmcs12 {
203 /* According to the Intel spec, a VMCS region must start with the
204 * following two fields. Then follow implementation-specific data.
205 */
206 u32 revision_id;
207 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300208
Nadav Har'El27d6c862011-05-25 23:06:59 +0300209 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
210 u32 padding[7]; /* room for future expansion */
211
Nadav Har'El22bd0352011-05-25 23:05:57 +0300212 u64 io_bitmap_a;
213 u64 io_bitmap_b;
214 u64 msr_bitmap;
215 u64 vm_exit_msr_store_addr;
216 u64 vm_exit_msr_load_addr;
217 u64 vm_entry_msr_load_addr;
218 u64 tsc_offset;
219 u64 virtual_apic_page_addr;
220 u64 apic_access_addr;
221 u64 ept_pointer;
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800222 u64 xss_exit_bitmap;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300223 u64 guest_physical_address;
224 u64 vmcs_link_pointer;
225 u64 guest_ia32_debugctl;
226 u64 guest_ia32_pat;
227 u64 guest_ia32_efer;
228 u64 guest_ia32_perf_global_ctrl;
229 u64 guest_pdptr0;
230 u64 guest_pdptr1;
231 u64 guest_pdptr2;
232 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100233 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300234 u64 host_ia32_pat;
235 u64 host_ia32_efer;
236 u64 host_ia32_perf_global_ctrl;
237 u64 padding64[8]; /* room for future expansion */
238 /*
239 * To allow migration of L1 (complete with its L2 guests) between
240 * machines of different natural widths (32 or 64 bit), we cannot have
241 * unsigned long fields with no explict size. We use u64 (aliased
242 * natural_width) instead. Luckily, x86 is little-endian.
243 */
244 natural_width cr0_guest_host_mask;
245 natural_width cr4_guest_host_mask;
246 natural_width cr0_read_shadow;
247 natural_width cr4_read_shadow;
248 natural_width cr3_target_value0;
249 natural_width cr3_target_value1;
250 natural_width cr3_target_value2;
251 natural_width cr3_target_value3;
252 natural_width exit_qualification;
253 natural_width guest_linear_address;
254 natural_width guest_cr0;
255 natural_width guest_cr3;
256 natural_width guest_cr4;
257 natural_width guest_es_base;
258 natural_width guest_cs_base;
259 natural_width guest_ss_base;
260 natural_width guest_ds_base;
261 natural_width guest_fs_base;
262 natural_width guest_gs_base;
263 natural_width guest_ldtr_base;
264 natural_width guest_tr_base;
265 natural_width guest_gdtr_base;
266 natural_width guest_idtr_base;
267 natural_width guest_dr7;
268 natural_width guest_rsp;
269 natural_width guest_rip;
270 natural_width guest_rflags;
271 natural_width guest_pending_dbg_exceptions;
272 natural_width guest_sysenter_esp;
273 natural_width guest_sysenter_eip;
274 natural_width host_cr0;
275 natural_width host_cr3;
276 natural_width host_cr4;
277 natural_width host_fs_base;
278 natural_width host_gs_base;
279 natural_width host_tr_base;
280 natural_width host_gdtr_base;
281 natural_width host_idtr_base;
282 natural_width host_ia32_sysenter_esp;
283 natural_width host_ia32_sysenter_eip;
284 natural_width host_rsp;
285 natural_width host_rip;
286 natural_width paddingl[8]; /* room for future expansion */
287 u32 pin_based_vm_exec_control;
288 u32 cpu_based_vm_exec_control;
289 u32 exception_bitmap;
290 u32 page_fault_error_code_mask;
291 u32 page_fault_error_code_match;
292 u32 cr3_target_count;
293 u32 vm_exit_controls;
294 u32 vm_exit_msr_store_count;
295 u32 vm_exit_msr_load_count;
296 u32 vm_entry_controls;
297 u32 vm_entry_msr_load_count;
298 u32 vm_entry_intr_info_field;
299 u32 vm_entry_exception_error_code;
300 u32 vm_entry_instruction_len;
301 u32 tpr_threshold;
302 u32 secondary_vm_exec_control;
303 u32 vm_instruction_error;
304 u32 vm_exit_reason;
305 u32 vm_exit_intr_info;
306 u32 vm_exit_intr_error_code;
307 u32 idt_vectoring_info_field;
308 u32 idt_vectoring_error_code;
309 u32 vm_exit_instruction_len;
310 u32 vmx_instruction_info;
311 u32 guest_es_limit;
312 u32 guest_cs_limit;
313 u32 guest_ss_limit;
314 u32 guest_ds_limit;
315 u32 guest_fs_limit;
316 u32 guest_gs_limit;
317 u32 guest_ldtr_limit;
318 u32 guest_tr_limit;
319 u32 guest_gdtr_limit;
320 u32 guest_idtr_limit;
321 u32 guest_es_ar_bytes;
322 u32 guest_cs_ar_bytes;
323 u32 guest_ss_ar_bytes;
324 u32 guest_ds_ar_bytes;
325 u32 guest_fs_ar_bytes;
326 u32 guest_gs_ar_bytes;
327 u32 guest_ldtr_ar_bytes;
328 u32 guest_tr_ar_bytes;
329 u32 guest_interruptibility_info;
330 u32 guest_activity_state;
331 u32 guest_sysenter_cs;
332 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100333 u32 vmx_preemption_timer_value;
334 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300335 u16 virtual_processor_id;
336 u16 guest_es_selector;
337 u16 guest_cs_selector;
338 u16 guest_ss_selector;
339 u16 guest_ds_selector;
340 u16 guest_fs_selector;
341 u16 guest_gs_selector;
342 u16 guest_ldtr_selector;
343 u16 guest_tr_selector;
344 u16 host_es_selector;
345 u16 host_cs_selector;
346 u16 host_ss_selector;
347 u16 host_ds_selector;
348 u16 host_fs_selector;
349 u16 host_gs_selector;
350 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300351};
352
353/*
354 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
355 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
356 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
357 */
358#define VMCS12_REVISION 0x11e57ed0
359
360/*
361 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
362 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
363 * current implementation, 4K are reserved to avoid future complications.
364 */
365#define VMCS12_SIZE 0x1000
366
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300367/* Used to remember the last vmcs02 used for some recently used vmcs12s */
368struct vmcs02_list {
369 struct list_head list;
370 gpa_t vmptr;
371 struct loaded_vmcs vmcs02;
372};
373
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300374/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300375 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
376 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
377 */
378struct nested_vmx {
379 /* Has the level1 guest done vmxon? */
380 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400381 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300382
383 /* The guest-physical address of the current VMCS L1 keeps for L2 */
384 gpa_t current_vmptr;
385 /* The host-usable pointer to the above */
386 struct page *current_vmcs12_page;
387 struct vmcs12 *current_vmcs12;
Abel Gordon8de48832013-04-18 14:37:25 +0300388 struct vmcs *current_shadow_vmcs;
Abel Gordon012f83c2013-04-18 14:39:25 +0300389 /*
390 * Indicates if the shadow vmcs must be updated with the
391 * data hold by vmcs12
392 */
393 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300394
395 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
396 struct list_head vmcs02_pool;
397 int vmcs02_num;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300398 u64 vmcs01_tsc_offset;
Nadav Har'El644d7112011-05-25 23:12:35 +0300399 /* L2 must run next, and mustn't decide to exit to L1. */
400 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300401 /*
402 * Guest pages referred to in vmcs02 with host-physical pointers, so
403 * we must keep them pinned while L2 runs.
404 */
405 struct page *apic_access_page;
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800406 struct page *virtual_apic_page;
Nadav Har'Elb3897a42013-07-08 19:12:35 +0800407 u64 msr_ia32_feature_control;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100408
409 struct hrtimer preemption_timer;
410 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200411
412 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
413 u64 vmcs01_debugctl;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300414};
415
Yang Zhang01e439b2013-04-11 19:25:12 +0800416#define POSTED_INTR_ON 0
417/* Posted-Interrupt Descriptor */
418struct pi_desc {
419 u32 pir[8]; /* Posted interrupt requested */
420 u32 control; /* bit 0 of control is outstanding notification bit */
421 u32 rsvd[7];
422} __aligned(64);
423
Yang Zhanga20ed542013-04-11 19:25:15 +0800424static bool pi_test_and_set_on(struct pi_desc *pi_desc)
425{
426 return test_and_set_bit(POSTED_INTR_ON,
427 (unsigned long *)&pi_desc->control);
428}
429
430static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
431{
432 return test_and_clear_bit(POSTED_INTR_ON,
433 (unsigned long *)&pi_desc->control);
434}
435
436static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
437{
438 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
439}
440
Marcelo Tosatti7c6a98d2014-12-16 09:08:14 -0500441static int pi_test_pir(int vector, struct pi_desc *pi_desc)
442{
443 return test_bit(vector, (unsigned long *)pi_desc->pir);
444}
445
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400446struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000447 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300448 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300449 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200450 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300451 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200452 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200453 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300454 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400455 int nmsrs;
456 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800457 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400458#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300459 u64 msr_host_kernel_gs_base;
460 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400461#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200462 u32 vm_entry_controls_shadow;
463 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300464 /*
465 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
466 * non-nested (L1) guest, it always points to vmcs01. For a nested
467 * guest (L2), it points to a different VMCS.
468 */
469 struct loaded_vmcs vmcs01;
470 struct loaded_vmcs *loaded_vmcs;
471 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300472 struct msr_autoload {
473 unsigned nr;
474 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
475 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
476 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400477 struct {
478 int loaded;
479 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300480#ifdef CONFIG_X86_64
481 u16 ds_sel, es_sel;
482#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200483 int gs_ldt_reload_needed;
484 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000485 u64 msr_host_bndcfgs;
Andy Lutomirskid974baa2014-10-08 09:02:13 -0700486 unsigned long vmcs_host_cr4; /* May not match real cr4 */
Mike Dayd77c26f2007-10-08 09:02:08 -0400487 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200488 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300489 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300490 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300491 struct kvm_segment segs[8];
492 } rmode;
493 struct {
494 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300495 struct kvm_save_segment {
496 u16 selector;
497 unsigned long base;
498 u32 limit;
499 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300500 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300501 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800502 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300503 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200504
505 /* Support for vnmi-less CPUs */
506 int soft_vnmi_blocked;
507 ktime_t entry_time;
508 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800509 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800510
511 bool rdtscp_enabled;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300512
Yang Zhang01e439b2013-04-11 19:25:12 +0800513 /* Posted interrupt descriptor */
514 struct pi_desc pi_desc;
515
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300516 /* Support for a guest hypervisor (nested VMX) */
517 struct nested_vmx nested;
Radim Krčmářa7653ec2014-08-21 18:08:07 +0200518
519 /* Dynamic PLE window. */
520 int ple_window;
521 bool ple_window_dirty;
Kai Huang843e4332015-01-28 10:54:28 +0800522
523 /* Support for PML */
524#define PML_ENTITY_NUM 512
525 struct page *pml_pg;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400526};
527
Avi Kivity2fb92db2011-04-27 19:42:18 +0300528enum segment_cache_field {
529 SEG_FIELD_SEL = 0,
530 SEG_FIELD_BASE = 1,
531 SEG_FIELD_LIMIT = 2,
532 SEG_FIELD_AR = 3,
533
534 SEG_FIELD_NR = 4
535};
536
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400537static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
538{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000539 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400540}
541
Nadav Har'El22bd0352011-05-25 23:05:57 +0300542#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
543#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
544#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
545 [number##_HIGH] = VMCS12_OFFSET(name)+4
546
Abel Gordon4607c2d2013-04-18 14:35:55 +0300547
Bandan Dasfe2b2012014-04-21 15:20:14 -0400548static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300549 /*
550 * We do NOT shadow fields that are modified when L0
551 * traps and emulates any vmx instruction (e.g. VMPTRLD,
552 * VMXON...) executed by L1.
553 * For example, VM_INSTRUCTION_ERROR is read
554 * by L1 if a vmx instruction fails (part of the error path).
555 * Note the code assumes this logic. If for some reason
556 * we start shadowing these fields then we need to
557 * force a shadow sync when L0 emulates vmx instructions
558 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
559 * by nested_vmx_failValid)
560 */
561 VM_EXIT_REASON,
562 VM_EXIT_INTR_INFO,
563 VM_EXIT_INSTRUCTION_LEN,
564 IDT_VECTORING_INFO_FIELD,
565 IDT_VECTORING_ERROR_CODE,
566 VM_EXIT_INTR_ERROR_CODE,
567 EXIT_QUALIFICATION,
568 GUEST_LINEAR_ADDRESS,
569 GUEST_PHYSICAL_ADDRESS
570};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400571static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300572 ARRAY_SIZE(shadow_read_only_fields);
573
Bandan Dasfe2b2012014-04-21 15:20:14 -0400574static unsigned long shadow_read_write_fields[] = {
Wanpeng Lia7c0b072014-08-21 19:46:50 +0800575 TPR_THRESHOLD,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300576 GUEST_RIP,
577 GUEST_RSP,
578 GUEST_CR0,
579 GUEST_CR3,
580 GUEST_CR4,
581 GUEST_INTERRUPTIBILITY_INFO,
582 GUEST_RFLAGS,
583 GUEST_CS_SELECTOR,
584 GUEST_CS_AR_BYTES,
585 GUEST_CS_LIMIT,
586 GUEST_CS_BASE,
587 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100588 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300589 CR0_GUEST_HOST_MASK,
590 CR0_READ_SHADOW,
591 CR4_READ_SHADOW,
592 TSC_OFFSET,
593 EXCEPTION_BITMAP,
594 CPU_BASED_VM_EXEC_CONTROL,
595 VM_ENTRY_EXCEPTION_ERROR_CODE,
596 VM_ENTRY_INTR_INFO_FIELD,
597 VM_ENTRY_INSTRUCTION_LEN,
598 VM_ENTRY_EXCEPTION_ERROR_CODE,
599 HOST_FS_BASE,
600 HOST_GS_BASE,
601 HOST_FS_SELECTOR,
602 HOST_GS_SELECTOR
603};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400604static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300605 ARRAY_SIZE(shadow_read_write_fields);
606
Mathias Krause772e0312012-08-30 01:30:19 +0200607static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300608 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
609 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
610 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
611 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
612 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
613 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
614 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
615 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
616 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
617 FIELD(HOST_ES_SELECTOR, host_es_selector),
618 FIELD(HOST_CS_SELECTOR, host_cs_selector),
619 FIELD(HOST_SS_SELECTOR, host_ss_selector),
620 FIELD(HOST_DS_SELECTOR, host_ds_selector),
621 FIELD(HOST_FS_SELECTOR, host_fs_selector),
622 FIELD(HOST_GS_SELECTOR, host_gs_selector),
623 FIELD(HOST_TR_SELECTOR, host_tr_selector),
624 FIELD64(IO_BITMAP_A, io_bitmap_a),
625 FIELD64(IO_BITMAP_B, io_bitmap_b),
626 FIELD64(MSR_BITMAP, msr_bitmap),
627 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
628 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
629 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
630 FIELD64(TSC_OFFSET, tsc_offset),
631 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
632 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
633 FIELD64(EPT_POINTER, ept_pointer),
Wanpeng Li81dc01f2014-12-04 19:11:07 +0800634 FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300635 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
636 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
637 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
638 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
639 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
640 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
641 FIELD64(GUEST_PDPTR0, guest_pdptr0),
642 FIELD64(GUEST_PDPTR1, guest_pdptr1),
643 FIELD64(GUEST_PDPTR2, guest_pdptr2),
644 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100645 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300646 FIELD64(HOST_IA32_PAT, host_ia32_pat),
647 FIELD64(HOST_IA32_EFER, host_ia32_efer),
648 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
649 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
650 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
651 FIELD(EXCEPTION_BITMAP, exception_bitmap),
652 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
653 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
654 FIELD(CR3_TARGET_COUNT, cr3_target_count),
655 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
656 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
657 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
658 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
659 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
660 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
661 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
662 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
663 FIELD(TPR_THRESHOLD, tpr_threshold),
664 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
665 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
666 FIELD(VM_EXIT_REASON, vm_exit_reason),
667 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
668 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
669 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
670 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
671 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
672 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
673 FIELD(GUEST_ES_LIMIT, guest_es_limit),
674 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
675 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
676 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
677 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
678 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
679 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
680 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
681 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
682 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
683 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
684 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
685 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
686 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
687 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
688 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
689 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
690 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
691 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
692 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
693 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
694 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100695 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300696 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
697 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
698 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
699 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
700 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
701 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
702 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
703 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
704 FIELD(EXIT_QUALIFICATION, exit_qualification),
705 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
706 FIELD(GUEST_CR0, guest_cr0),
707 FIELD(GUEST_CR3, guest_cr3),
708 FIELD(GUEST_CR4, guest_cr4),
709 FIELD(GUEST_ES_BASE, guest_es_base),
710 FIELD(GUEST_CS_BASE, guest_cs_base),
711 FIELD(GUEST_SS_BASE, guest_ss_base),
712 FIELD(GUEST_DS_BASE, guest_ds_base),
713 FIELD(GUEST_FS_BASE, guest_fs_base),
714 FIELD(GUEST_GS_BASE, guest_gs_base),
715 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
716 FIELD(GUEST_TR_BASE, guest_tr_base),
717 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
718 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
719 FIELD(GUEST_DR7, guest_dr7),
720 FIELD(GUEST_RSP, guest_rsp),
721 FIELD(GUEST_RIP, guest_rip),
722 FIELD(GUEST_RFLAGS, guest_rflags),
723 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
724 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
725 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
726 FIELD(HOST_CR0, host_cr0),
727 FIELD(HOST_CR3, host_cr3),
728 FIELD(HOST_CR4, host_cr4),
729 FIELD(HOST_FS_BASE, host_fs_base),
730 FIELD(HOST_GS_BASE, host_gs_base),
731 FIELD(HOST_TR_BASE, host_tr_base),
732 FIELD(HOST_GDTR_BASE, host_gdtr_base),
733 FIELD(HOST_IDTR_BASE, host_idtr_base),
734 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
735 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
736 FIELD(HOST_RSP, host_rsp),
737 FIELD(HOST_RIP, host_rip),
738};
Nadav Har'El22bd0352011-05-25 23:05:57 +0300739
740static inline short vmcs_field_to_offset(unsigned long field)
741{
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +0100742 BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX);
743
744 if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) ||
745 vmcs_field_to_offset_table[field] == 0)
746 return -ENOENT;
747
Nadav Har'El22bd0352011-05-25 23:05:57 +0300748 return vmcs_field_to_offset_table[field];
749}
750
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300751static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
752{
753 return to_vmx(vcpu)->nested.current_vmcs12;
754}
755
756static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
757{
758 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800759 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300760 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800761
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300762 return page;
763}
764
765static void nested_release_page(struct page *page)
766{
767 kvm_release_page_dirty(page);
768}
769
770static void nested_release_page_clean(struct page *page)
771{
772 kvm_release_page_clean(page);
773}
774
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300775static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800776static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800777static void kvm_cpu_vmxon(u64 addr);
778static void kvm_cpu_vmxoff(void);
Paolo Bonzini93c4adc2014-03-05 23:19:52 +0100779static bool vmx_mpx_supported(void);
Wanpeng Lif53cd632014-12-02 19:14:58 +0800780static bool vmx_xsaves_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200781static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300782static void vmx_set_segment(struct kvm_vcpu *vcpu,
783 struct kvm_segment *var, int seg);
784static void vmx_get_segment(struct kvm_vcpu *vcpu,
785 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200786static bool guest_state_valid(struct kvm_vcpu *vcpu);
787static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yang Zhanga20ed542013-04-11 19:25:15 +0800788static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu);
Abel Gordonc3114422013-04-18 14:38:55 +0300789static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300790static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Tang Chena255d472014-09-16 18:41:58 +0800791static int alloc_identity_pagetable(struct kvm *kvm);
Avi Kivity75880a02007-06-20 11:20:04 +0300792
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793static DEFINE_PER_CPU(struct vmcs *, vmxarea);
794static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300795/*
796 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
797 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
798 */
799static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300800static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200802static unsigned long *vmx_io_bitmap_a;
803static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200804static unsigned long *vmx_msr_bitmap_legacy;
805static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800806static unsigned long *vmx_msr_bitmap_legacy_x2apic;
807static unsigned long *vmx_msr_bitmap_longmode_x2apic;
Abel Gordon4607c2d2013-04-18 14:35:55 +0300808static unsigned long *vmx_vmread_bitmap;
809static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300810
Avi Kivity110312c2010-12-21 12:54:20 +0200811static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200812static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200813
Sheng Yang2384d2b2008-01-17 15:14:33 +0800814static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
815static DEFINE_SPINLOCK(vmx_vpid_lock);
816
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300817static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818 int size;
819 int order;
820 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300821 u32 pin_based_exec_ctrl;
822 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800823 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300824 u32 vmexit_ctrl;
825 u32 vmentry_ctrl;
826} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827
Hannes Ederefff9e52008-11-28 17:02:06 +0100828static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800829 u32 ept;
830 u32 vpid;
831} vmx_capability;
832
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833#define VMX_SEGMENT_FIELD(seg) \
834 [VCPU_SREG_##seg] = { \
835 .selector = GUEST_##seg##_SELECTOR, \
836 .base = GUEST_##seg##_BASE, \
837 .limit = GUEST_##seg##_LIMIT, \
838 .ar_bytes = GUEST_##seg##_AR_BYTES, \
839 }
840
Mathias Krause772e0312012-08-30 01:30:19 +0200841static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842 unsigned selector;
843 unsigned base;
844 unsigned limit;
845 unsigned ar_bytes;
846} kvm_vmx_segment_fields[] = {
847 VMX_SEGMENT_FIELD(CS),
848 VMX_SEGMENT_FIELD(DS),
849 VMX_SEGMENT_FIELD(ES),
850 VMX_SEGMENT_FIELD(FS),
851 VMX_SEGMENT_FIELD(GS),
852 VMX_SEGMENT_FIELD(SS),
853 VMX_SEGMENT_FIELD(TR),
854 VMX_SEGMENT_FIELD(LDTR),
855};
856
Avi Kivity26bb0982009-09-07 11:14:12 +0300857static u64 host_efer;
858
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300859static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
860
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300861/*
Brian Gerst8c065852010-07-17 09:03:26 -0400862 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300863 * away by decrementing the array size.
864 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800866#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300867 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800868#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400869 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871
Gui Jianfeng31299942010-03-15 17:29:09 +0800872static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800873{
874 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
875 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100876 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877}
878
Gui Jianfeng31299942010-03-15 17:29:09 +0800879static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300880{
881 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
882 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100883 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300884}
885
Gui Jianfeng31299942010-03-15 17:29:09 +0800886static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500887{
888 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
889 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100890 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500891}
892
Gui Jianfeng31299942010-03-15 17:29:09 +0800893static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894{
895 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
896 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
897}
898
Gui Jianfeng31299942010-03-15 17:29:09 +0800899static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800900{
901 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
902 INTR_INFO_VALID_MASK)) ==
903 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
904}
905
Gui Jianfeng31299942010-03-15 17:29:09 +0800906static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800907{
Sheng Yang04547152009-04-01 15:52:31 +0800908 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800909}
910
Gui Jianfeng31299942010-03-15 17:29:09 +0800911static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800912{
Sheng Yang04547152009-04-01 15:52:31 +0800913 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800914}
915
Gui Jianfeng31299942010-03-15 17:29:09 +0800916static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800917{
Sheng Yang04547152009-04-01 15:52:31 +0800918 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800919}
920
Gui Jianfeng31299942010-03-15 17:29:09 +0800921static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800922{
Sheng Yang04547152009-04-01 15:52:31 +0800923 return vmcs_config.cpu_based_exec_ctrl &
924 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800925}
926
Avi Kivity774ead32007-12-26 13:57:04 +0200927static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800928{
Sheng Yang04547152009-04-01 15:52:31 +0800929 return vmcs_config.cpu_based_2nd_exec_ctrl &
930 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
931}
932
Yang Zhang8d146952013-01-25 10:18:50 +0800933static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
934{
935 return vmcs_config.cpu_based_2nd_exec_ctrl &
936 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
937}
938
Yang Zhang83d4c282013-01-25 10:18:49 +0800939static inline bool cpu_has_vmx_apic_register_virt(void)
940{
941 return vmcs_config.cpu_based_2nd_exec_ctrl &
942 SECONDARY_EXEC_APIC_REGISTER_VIRT;
943}
944
Yang Zhangc7c9c562013-01-25 10:18:51 +0800945static inline bool cpu_has_vmx_virtual_intr_delivery(void)
946{
947 return vmcs_config.cpu_based_2nd_exec_ctrl &
948 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
949}
950
Yang Zhang01e439b2013-04-11 19:25:12 +0800951static inline bool cpu_has_vmx_posted_intr(void)
952{
953 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
954}
955
956static inline bool cpu_has_vmx_apicv(void)
957{
958 return cpu_has_vmx_apic_register_virt() &&
959 cpu_has_vmx_virtual_intr_delivery() &&
960 cpu_has_vmx_posted_intr();
961}
962
Sheng Yang04547152009-04-01 15:52:31 +0800963static inline bool cpu_has_vmx_flexpriority(void)
964{
965 return cpu_has_vmx_tpr_shadow() &&
966 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800967}
968
Marcelo Tosattie7997942009-06-11 12:07:40 -0300969static inline bool cpu_has_vmx_ept_execute_only(void)
970{
Gui Jianfeng31299942010-03-15 17:29:09 +0800971 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300972}
973
Marcelo Tosattie7997942009-06-11 12:07:40 -0300974static inline bool cpu_has_vmx_ept_2m_page(void)
975{
Gui Jianfeng31299942010-03-15 17:29:09 +0800976 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300977}
978
Sheng Yang878403b2010-01-05 19:02:29 +0800979static inline bool cpu_has_vmx_ept_1g_page(void)
980{
Gui Jianfeng31299942010-03-15 17:29:09 +0800981 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800982}
983
Sheng Yang4bc9b982010-06-02 14:05:24 +0800984static inline bool cpu_has_vmx_ept_4levels(void)
985{
986 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
987}
988
Xudong Hao83c3a332012-05-28 19:33:35 +0800989static inline bool cpu_has_vmx_ept_ad_bits(void)
990{
991 return vmx_capability.ept & VMX_EPT_AD_BIT;
992}
993
Gui Jianfeng31299942010-03-15 17:29:09 +0800994static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800995{
Gui Jianfeng31299942010-03-15 17:29:09 +0800996 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800997}
998
Gui Jianfeng31299942010-03-15 17:29:09 +0800999static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001000{
Gui Jianfeng31299942010-03-15 17:29:09 +08001001 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001002}
1003
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001004static inline bool cpu_has_vmx_invvpid_single(void)
1005{
1006 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
1007}
1008
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001009static inline bool cpu_has_vmx_invvpid_global(void)
1010{
1011 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
1012}
1013
Gui Jianfeng31299942010-03-15 17:29:09 +08001014static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +08001015{
Sheng Yang04547152009-04-01 15:52:31 +08001016 return vmcs_config.cpu_based_2nd_exec_ctrl &
1017 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +08001018}
1019
Gui Jianfeng31299942010-03-15 17:29:09 +08001020static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001021{
1022 return vmcs_config.cpu_based_2nd_exec_ctrl &
1023 SECONDARY_EXEC_UNRESTRICTED_GUEST;
1024}
1025
Gui Jianfeng31299942010-03-15 17:29:09 +08001026static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001027{
1028 return vmcs_config.cpu_based_2nd_exec_ctrl &
1029 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
1030}
1031
Gui Jianfeng31299942010-03-15 17:29:09 +08001032static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001033{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +08001034 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001035}
1036
Gui Jianfeng31299942010-03-15 17:29:09 +08001037static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001038{
Sheng Yang04547152009-04-01 15:52:31 +08001039 return vmcs_config.cpu_based_2nd_exec_ctrl &
1040 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001041}
1042
Gui Jianfeng31299942010-03-15 17:29:09 +08001043static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001044{
1045 return vmcs_config.cpu_based_2nd_exec_ctrl &
1046 SECONDARY_EXEC_RDTSCP;
1047}
1048
Mao, Junjiead756a12012-07-02 01:18:48 +00001049static inline bool cpu_has_vmx_invpcid(void)
1050{
1051 return vmcs_config.cpu_based_2nd_exec_ctrl &
1052 SECONDARY_EXEC_ENABLE_INVPCID;
1053}
1054
Gui Jianfeng31299942010-03-15 17:29:09 +08001055static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001056{
1057 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1058}
1059
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001060static inline bool cpu_has_vmx_wbinvd_exit(void)
1061{
1062 return vmcs_config.cpu_based_2nd_exec_ctrl &
1063 SECONDARY_EXEC_WBINVD_EXITING;
1064}
1065
Abel Gordonabc4fc52013-04-18 14:35:25 +03001066static inline bool cpu_has_vmx_shadow_vmcs(void)
1067{
1068 u64 vmx_msr;
1069 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1070 /* check if the cpu supports writing r/o exit information fields */
1071 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1072 return false;
1073
1074 return vmcs_config.cpu_based_2nd_exec_ctrl &
1075 SECONDARY_EXEC_SHADOW_VMCS;
1076}
1077
Kai Huang843e4332015-01-28 10:54:28 +08001078static inline bool cpu_has_vmx_pml(void)
1079{
1080 return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML;
1081}
1082
Sheng Yang04547152009-04-01 15:52:31 +08001083static inline bool report_flexpriority(void)
1084{
1085 return flexpriority_enabled;
1086}
1087
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001088static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1089{
1090 return vmcs12->cpu_based_vm_exec_control & bit;
1091}
1092
1093static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1094{
1095 return (vmcs12->cpu_based_vm_exec_control &
1096 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1097 (vmcs12->secondary_vm_exec_control & bit);
1098}
1099
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001100static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001101{
1102 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1103}
1104
Jan Kiszkaf4124502014-03-07 20:03:13 +01001105static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1106{
1107 return vmcs12->pin_based_vm_exec_control &
1108 PIN_BASED_VMX_PREEMPTION_TIMER;
1109}
1110
Nadav Har'El155a97a2013-08-05 11:07:16 +03001111static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1112{
1113 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1114}
1115
Wanpeng Li81dc01f2014-12-04 19:11:07 +08001116static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12)
1117{
1118 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) &&
1119 vmx_xsaves_supported();
1120}
1121
Nadav Har'El644d7112011-05-25 23:12:35 +03001122static inline bool is_exception(u32 intr_info)
1123{
1124 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1125 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
1126}
1127
Jan Kiszka533558b2014-01-04 18:47:20 +01001128static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1129 u32 exit_intr_info,
1130 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001131static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1132 struct vmcs12 *vmcs12,
1133 u32 reason, unsigned long qualification);
1134
Rusty Russell8b9cf982007-07-30 16:31:43 +10001135static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001136{
1137 int i;
1138
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001139 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001140 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001141 return i;
1142 return -1;
1143}
1144
Sheng Yang2384d2b2008-01-17 15:14:33 +08001145static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1146{
1147 struct {
1148 u64 vpid : 16;
1149 u64 rsvd : 48;
1150 u64 gva;
1151 } operand = { vpid, 0, gva };
1152
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001153 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001154 /* CF==1 or ZF==1 --> rc = -1 */
1155 "; ja 1f ; ud2 ; 1:"
1156 : : "a"(&operand), "c"(ext) : "cc", "memory");
1157}
1158
Sheng Yang14394422008-04-28 12:24:45 +08001159static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1160{
1161 struct {
1162 u64 eptp, gpa;
1163 } operand = {eptp, gpa};
1164
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001165 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001166 /* CF==1 or ZF==1 --> rc = -1 */
1167 "; ja 1f ; ud2 ; 1:\n"
1168 : : "a" (&operand), "c" (ext) : "cc", "memory");
1169}
1170
Avi Kivity26bb0982009-09-07 11:14:12 +03001171static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001172{
1173 int i;
1174
Rusty Russell8b9cf982007-07-30 16:31:43 +10001175 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001176 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001177 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001178 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001179}
1180
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181static void vmcs_clear(struct vmcs *vmcs)
1182{
1183 u64 phys_addr = __pa(vmcs);
1184 u8 error;
1185
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001186 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001187 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 : "cc", "memory");
1189 if (error)
1190 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1191 vmcs, phys_addr);
1192}
1193
Nadav Har'Eld462b812011-05-24 15:26:10 +03001194static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1195{
1196 vmcs_clear(loaded_vmcs->vmcs);
1197 loaded_vmcs->cpu = -1;
1198 loaded_vmcs->launched = 0;
1199}
1200
Dongxiao Xu7725b892010-05-11 18:29:38 +08001201static void vmcs_load(struct vmcs *vmcs)
1202{
1203 u64 phys_addr = __pa(vmcs);
1204 u8 error;
1205
1206 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001207 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001208 : "cc", "memory");
1209 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001210 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001211 vmcs, phys_addr);
1212}
1213
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001214#ifdef CONFIG_KEXEC
1215/*
1216 * This bitmap is used to indicate whether the vmclear
1217 * operation is enabled on all cpus. All disabled by
1218 * default.
1219 */
1220static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1221
1222static inline void crash_enable_local_vmclear(int cpu)
1223{
1224 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1225}
1226
1227static inline void crash_disable_local_vmclear(int cpu)
1228{
1229 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1230}
1231
1232static inline int crash_local_vmclear_enabled(int cpu)
1233{
1234 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1235}
1236
1237static void crash_vmclear_local_loaded_vmcss(void)
1238{
1239 int cpu = raw_smp_processor_id();
1240 struct loaded_vmcs *v;
1241
1242 if (!crash_local_vmclear_enabled(cpu))
1243 return;
1244
1245 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1246 loaded_vmcss_on_cpu_link)
1247 vmcs_clear(v->vmcs);
1248}
1249#else
1250static inline void crash_enable_local_vmclear(int cpu) { }
1251static inline void crash_disable_local_vmclear(int cpu) { }
1252#endif /* CONFIG_KEXEC */
1253
Nadav Har'Eld462b812011-05-24 15:26:10 +03001254static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001255{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001256 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001257 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001258
Nadav Har'Eld462b812011-05-24 15:26:10 +03001259 if (loaded_vmcs->cpu != cpu)
1260 return; /* vcpu migration can race with cpu offline */
1261 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001263 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001264 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001265
1266 /*
1267 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1268 * is before setting loaded_vmcs->vcpu to -1 which is done in
1269 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1270 * then adds the vmcs into percpu list before it is deleted.
1271 */
1272 smp_wmb();
1273
Nadav Har'Eld462b812011-05-24 15:26:10 +03001274 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001275 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276}
1277
Nadav Har'Eld462b812011-05-24 15:26:10 +03001278static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001279{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001280 int cpu = loaded_vmcs->cpu;
1281
1282 if (cpu != -1)
1283 smp_call_function_single(cpu,
1284 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001285}
1286
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001287static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001288{
1289 if (vmx->vpid == 0)
1290 return;
1291
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001292 if (cpu_has_vmx_invvpid_single())
1293 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001294}
1295
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001296static inline void vpid_sync_vcpu_global(void)
1297{
1298 if (cpu_has_vmx_invvpid_global())
1299 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1300}
1301
1302static inline void vpid_sync_context(struct vcpu_vmx *vmx)
1303{
1304 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001305 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001306 else
1307 vpid_sync_vcpu_global();
1308}
1309
Sheng Yang14394422008-04-28 12:24:45 +08001310static inline void ept_sync_global(void)
1311{
1312 if (cpu_has_vmx_invept_global())
1313 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1314}
1315
1316static inline void ept_sync_context(u64 eptp)
1317{
Avi Kivity089d0342009-03-23 18:26:32 +02001318 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001319 if (cpu_has_vmx_invept_context())
1320 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1321 else
1322 ept_sync_global();
1323 }
1324}
1325
Avi Kivity96304212011-05-15 10:13:13 -04001326static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327{
Avi Kivity5e520e62011-05-15 10:13:12 -04001328 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329
Avi Kivity5e520e62011-05-15 10:13:12 -04001330 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1331 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001332 return value;
1333}
1334
Avi Kivity96304212011-05-15 10:13:13 -04001335static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336{
1337 return vmcs_readl(field);
1338}
1339
Avi Kivity96304212011-05-15 10:13:13 -04001340static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341{
1342 return vmcs_readl(field);
1343}
1344
Avi Kivity96304212011-05-15 10:13:13 -04001345static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346{
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001347#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 return vmcs_readl(field);
1349#else
1350 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
1351#endif
1352}
1353
Avi Kivitye52de1b2007-01-05 16:36:56 -08001354static noinline void vmwrite_error(unsigned long field, unsigned long value)
1355{
1356 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1357 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1358 dump_stack();
1359}
1360
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361static void vmcs_writel(unsigned long field, unsigned long value)
1362{
1363 u8 error;
1364
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001365 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001366 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001367 if (unlikely(error))
1368 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369}
1370
1371static void vmcs_write16(unsigned long field, u16 value)
1372{
1373 vmcs_writel(field, value);
1374}
1375
1376static void vmcs_write32(unsigned long field, u32 value)
1377{
1378 vmcs_writel(field, value);
1379}
1380
1381static void vmcs_write64(unsigned long field, u64 value)
1382{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001384#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 asm volatile ("");
1386 vmcs_writel(field+1, value >> 32);
1387#endif
1388}
1389
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001390static void vmcs_clear_bits(unsigned long field, u32 mask)
1391{
1392 vmcs_writel(field, vmcs_readl(field) & ~mask);
1393}
1394
1395static void vmcs_set_bits(unsigned long field, u32 mask)
1396{
1397 vmcs_writel(field, vmcs_readl(field) | mask);
1398}
1399
Gleb Natapov2961e8762013-11-25 15:37:13 +02001400static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1401{
1402 vmcs_write32(VM_ENTRY_CONTROLS, val);
1403 vmx->vm_entry_controls_shadow = val;
1404}
1405
1406static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1407{
1408 if (vmx->vm_entry_controls_shadow != val)
1409 vm_entry_controls_init(vmx, val);
1410}
1411
1412static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1413{
1414 return vmx->vm_entry_controls_shadow;
1415}
1416
1417
1418static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1419{
1420 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1421}
1422
1423static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1424{
1425 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1426}
1427
1428static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1429{
1430 vmcs_write32(VM_EXIT_CONTROLS, val);
1431 vmx->vm_exit_controls_shadow = val;
1432}
1433
1434static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1435{
1436 if (vmx->vm_exit_controls_shadow != val)
1437 vm_exit_controls_init(vmx, val);
1438}
1439
1440static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1441{
1442 return vmx->vm_exit_controls_shadow;
1443}
1444
1445
1446static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1447{
1448 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1449}
1450
1451static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1452{
1453 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1454}
1455
Avi Kivity2fb92db2011-04-27 19:42:18 +03001456static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1457{
1458 vmx->segment_cache.bitmask = 0;
1459}
1460
1461static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1462 unsigned field)
1463{
1464 bool ret;
1465 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1466
1467 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1468 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1469 vmx->segment_cache.bitmask = 0;
1470 }
1471 ret = vmx->segment_cache.bitmask & mask;
1472 vmx->segment_cache.bitmask |= mask;
1473 return ret;
1474}
1475
1476static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1477{
1478 u16 *p = &vmx->segment_cache.seg[seg].selector;
1479
1480 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1481 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1482 return *p;
1483}
1484
1485static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1486{
1487 ulong *p = &vmx->segment_cache.seg[seg].base;
1488
1489 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1490 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1491 return *p;
1492}
1493
1494static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1495{
1496 u32 *p = &vmx->segment_cache.seg[seg].limit;
1497
1498 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1499 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1500 return *p;
1501}
1502
1503static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1504{
1505 u32 *p = &vmx->segment_cache.seg[seg].ar;
1506
1507 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1508 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1509 return *p;
1510}
1511
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001512static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1513{
1514 u32 eb;
1515
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001516 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1517 (1u << NM_VECTOR) | (1u << DB_VECTOR);
1518 if ((vcpu->guest_debug &
1519 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1520 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1521 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001522 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001523 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001524 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001525 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001526 if (vcpu->fpu_active)
1527 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001528
1529 /* When we are running a nested L2 guest and L1 specified for it a
1530 * certain exception bitmap, we must trap the same exceptions and pass
1531 * them to L1. When running L2, we will only handle the exceptions
1532 * specified above if L1 did not want them.
1533 */
1534 if (is_guest_mode(vcpu))
1535 eb |= get_vmcs12(vcpu)->exception_bitmap;
1536
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001537 vmcs_write32(EXCEPTION_BITMAP, eb);
1538}
1539
Gleb Natapov2961e8762013-11-25 15:37:13 +02001540static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1541 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001542{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001543 vm_entry_controls_clearbit(vmx, entry);
1544 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001545}
1546
Avi Kivity61d2ef22010-04-28 16:40:38 +03001547static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1548{
1549 unsigned i;
1550 struct msr_autoload *m = &vmx->msr_autoload;
1551
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001552 switch (msr) {
1553 case MSR_EFER:
1554 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001555 clear_atomic_switch_msr_special(vmx,
1556 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001557 VM_EXIT_LOAD_IA32_EFER);
1558 return;
1559 }
1560 break;
1561 case MSR_CORE_PERF_GLOBAL_CTRL:
1562 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001563 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001564 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1565 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1566 return;
1567 }
1568 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001569 }
1570
Avi Kivity61d2ef22010-04-28 16:40:38 +03001571 for (i = 0; i < m->nr; ++i)
1572 if (m->guest[i].index == msr)
1573 break;
1574
1575 if (i == m->nr)
1576 return;
1577 --m->nr;
1578 m->guest[i] = m->guest[m->nr];
1579 m->host[i] = m->host[m->nr];
1580 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1581 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1582}
1583
Gleb Natapov2961e8762013-11-25 15:37:13 +02001584static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1585 unsigned long entry, unsigned long exit,
1586 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1587 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001588{
1589 vmcs_write64(guest_val_vmcs, guest_val);
1590 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001591 vm_entry_controls_setbit(vmx, entry);
1592 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001593}
1594
Avi Kivity61d2ef22010-04-28 16:40:38 +03001595static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1596 u64 guest_val, u64 host_val)
1597{
1598 unsigned i;
1599 struct msr_autoload *m = &vmx->msr_autoload;
1600
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001601 switch (msr) {
1602 case MSR_EFER:
1603 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001604 add_atomic_switch_msr_special(vmx,
1605 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001606 VM_EXIT_LOAD_IA32_EFER,
1607 GUEST_IA32_EFER,
1608 HOST_IA32_EFER,
1609 guest_val, host_val);
1610 return;
1611 }
1612 break;
1613 case MSR_CORE_PERF_GLOBAL_CTRL:
1614 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001615 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001616 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1617 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1618 GUEST_IA32_PERF_GLOBAL_CTRL,
1619 HOST_IA32_PERF_GLOBAL_CTRL,
1620 guest_val, host_val);
1621 return;
1622 }
1623 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001624 }
1625
Avi Kivity61d2ef22010-04-28 16:40:38 +03001626 for (i = 0; i < m->nr; ++i)
1627 if (m->guest[i].index == msr)
1628 break;
1629
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001630 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001631 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001632 "Can't add msr %x\n", msr);
1633 return;
1634 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001635 ++m->nr;
1636 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1637 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1638 }
1639
1640 m->guest[i].index = msr;
1641 m->guest[i].value = guest_val;
1642 m->host[i].index = msr;
1643 m->host[i].value = host_val;
1644}
1645
Avi Kivity33ed6322007-05-02 16:54:03 +03001646static void reload_tss(void)
1647{
Avi Kivity33ed6322007-05-02 16:54:03 +03001648 /*
1649 * VT restores TR but not its size. Useless.
1650 */
Christoph Lameter89cbc762014-08-17 12:30:40 -05001651 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001652 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001653
Avi Kivityd3591922010-07-26 18:32:39 +03001654 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001655 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1656 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001657}
1658
Avi Kivity92c0d902009-10-29 11:00:16 +02001659static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001660{
Roel Kluin3a34a882009-08-04 02:08:45 -07001661 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001662 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +03001663
Avi Kivityf6801df2010-01-21 15:31:50 +02001664 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -07001665
Avi Kivity51c6cf62007-08-29 03:48:05 +03001666 /*
Guo Chao0fa06072012-06-28 15:16:19 +08001667 * NX is emulated; LMA and LME handled by hardware; SCE meaningless
Avi Kivity51c6cf62007-08-29 03:48:05 +03001668 * outside long mode
1669 */
1670 ignore_bits = EFER_NX | EFER_SCE;
1671#ifdef CONFIG_X86_64
1672 ignore_bits |= EFER_LMA | EFER_LME;
1673 /* SCE is meaningful only in long mode on Intel */
1674 if (guest_efer & EFER_LMA)
1675 ignore_bits &= ~(u64)EFER_SCE;
1676#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +03001677 guest_efer &= ~ignore_bits;
1678 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +03001679 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +02001680 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03001681
1682 clear_atomic_switch_msr(vmx, MSR_EFER);
Andy Lutomirskif6577a5f2014-11-07 18:25:18 -08001683
1684 /*
1685 * On EPT, we can't emulate NX, so we must switch EFER atomically.
1686 * On CPUs that support "load IA32_EFER", always switch EFER
1687 * atomically, since it's faster than switching it manually.
1688 */
1689 if (cpu_has_load_ia32_efer ||
1690 (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
Avi Kivity84ad33e2010-04-28 16:42:29 +03001691 guest_efer = vmx->vcpu.arch.efer;
1692 if (!(guest_efer & EFER_LMA))
1693 guest_efer &= ~EFER_LME;
Andy Lutomirski54b98bf2014-11-10 11:19:15 -08001694 if (guest_efer != host_efer)
1695 add_atomic_switch_msr(vmx, MSR_EFER,
1696 guest_efer, host_efer);
Avi Kivity84ad33e2010-04-28 16:42:29 +03001697 return false;
1698 }
1699
Avi Kivity26bb0982009-09-07 11:14:12 +03001700 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001701}
1702
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001703static unsigned long segment_base(u16 selector)
1704{
Christoph Lameter89cbc762014-08-17 12:30:40 -05001705 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001706 struct desc_struct *d;
1707 unsigned long table_base;
1708 unsigned long v;
1709
1710 if (!(selector & ~3))
1711 return 0;
1712
Avi Kivityd3591922010-07-26 18:32:39 +03001713 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001714
1715 if (selector & 4) { /* from ldt */
1716 u16 ldt_selector = kvm_read_ldt();
1717
1718 if (!(ldt_selector & ~3))
1719 return 0;
1720
1721 table_base = segment_base(ldt_selector);
1722 }
1723 d = (struct desc_struct *)(table_base + (selector & ~7));
1724 v = get_desc_base(d);
1725#ifdef CONFIG_X86_64
1726 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
1727 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
1728#endif
1729 return v;
1730}
1731
1732static inline unsigned long kvm_read_tr_base(void)
1733{
1734 u16 tr;
1735 asm("str %0" : "=g"(tr));
1736 return segment_base(tr);
1737}
1738
Avi Kivity04d2cc72007-09-10 18:10:54 +03001739static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001740{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001741 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001742 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001743
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001744 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001745 return;
1746
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001747 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001748 /*
1749 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1750 * allow segment selectors with cpl > 0 or ti == 1.
1751 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001752 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02001753 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02001754 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001755 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001756 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001757 vmx->host_state.fs_reload_needed = 0;
1758 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03001759 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001760 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001761 }
Avi Kivity9581d442010-10-19 16:46:55 +02001762 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001763 if (!(vmx->host_state.gs_sel & 7))
1764 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001765 else {
1766 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001767 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001768 }
1769
1770#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03001771 savesegment(ds, vmx->host_state.ds_sel);
1772 savesegment(es, vmx->host_state.es_sel);
1773#endif
1774
1775#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03001776 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1777 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1778#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001779 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1780 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03001781#endif
Avi Kivity707c0872007-05-02 17:33:43 +03001782
1783#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001784 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1785 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03001786 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03001787#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001788 if (boot_cpu_has(X86_FEATURE_MPX))
1789 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03001790 for (i = 0; i < vmx->save_nmsrs; ++i)
1791 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02001792 vmx->guest_msrs[i].data,
1793 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03001794}
1795
Avi Kivitya9b21b62008-06-24 11:48:49 +03001796static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001797{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001798 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001799 return;
1800
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001801 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001802 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02001803#ifdef CONFIG_X86_64
1804 if (is_long_mode(&vmx->vcpu))
1805 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1806#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001807 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001808 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001809#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02001810 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001811#else
1812 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001813#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001814 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02001815 if (vmx->host_state.fs_reload_needed)
1816 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001817#ifdef CONFIG_X86_64
1818 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
1819 loadsegment(ds, vmx->host_state.ds_sel);
1820 loadsegment(es, vmx->host_state.es_sel);
1821 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001822#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001823 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001824#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001825 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001826#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001827 if (vmx->host_state.msr_host_bndcfgs)
1828 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07001829 /*
1830 * If the FPU is not active (through the host task or
1831 * the guest vcpu), then restore the cr0.TS bit.
1832 */
1833 if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded)
1834 stts();
Christoph Lameter89cbc762014-08-17 12:30:40 -05001835 load_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03001836}
1837
Avi Kivitya9b21b62008-06-24 11:48:49 +03001838static void vmx_load_host_state(struct vcpu_vmx *vmx)
1839{
1840 preempt_disable();
1841 __vmx_load_host_state(vmx);
1842 preempt_enable();
1843}
1844
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845/*
1846 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1847 * vcpu mutex is already taken.
1848 */
Avi Kivity15ad7142007-07-11 18:17:21 +03001849static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001851 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001852 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001853
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001854 if (!vmm_exclusive)
1855 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001856 else if (vmx->loaded_vmcs->cpu != cpu)
1857 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858
Nadav Har'Eld462b812011-05-24 15:26:10 +03001859 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1860 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1861 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862 }
1863
Nadav Har'Eld462b812011-05-24 15:26:10 +03001864 if (vmx->loaded_vmcs->cpu != cpu) {
Christoph Lameter89cbc762014-08-17 12:30:40 -05001865 struct desc_ptr *gdt = this_cpu_ptr(&host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866 unsigned long sysenter_esp;
1867
Avi Kivitya8eeb042010-05-10 12:34:53 +03001868 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001869 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001870 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001871
1872 /*
1873 * Read loaded_vmcs->cpu should be before fetching
1874 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1875 * See the comments in __loaded_vmcs_clear().
1876 */
1877 smp_rmb();
1878
Nadav Har'Eld462b812011-05-24 15:26:10 +03001879 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1880 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001881 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001882 local_irq_enable();
1883
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884 /*
1885 * Linux uses per-cpu TSS and GDT, so set these when switching
1886 * processors.
1887 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001888 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03001889 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001890
1891 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1892 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Nadav Har'Eld462b812011-05-24 15:26:10 +03001893 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001894 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001895}
1896
1897static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1898{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001899 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001900 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001901 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1902 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001903 kvm_cpu_vmxoff();
1904 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905}
1906
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001907static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1908{
Avi Kivity81231c62010-01-24 16:26:40 +02001909 ulong cr0;
1910
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001911 if (vcpu->fpu_active)
1912 return;
1913 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001914 cr0 = vmcs_readl(GUEST_CR0);
1915 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1916 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1917 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001918 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001919 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001920 if (is_guest_mode(vcpu))
1921 vcpu->arch.cr0_guest_owned_bits &=
1922 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02001923 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001924}
1925
Avi Kivityedcafe32009-12-30 18:07:40 +02001926static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1927
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001928/*
1929 * Return the cr0 value that a nested guest would read. This is a combination
1930 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1931 * its hypervisor (cr0_read_shadow).
1932 */
1933static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
1934{
1935 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
1936 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
1937}
1938static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
1939{
1940 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
1941 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
1942}
1943
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001944static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1945{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001946 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
1947 * set this *before* calling this function.
1948 */
Avi Kivityedcafe32009-12-30 18:07:40 +02001949 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001950 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001951 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001952 vcpu->arch.cr0_guest_owned_bits = 0;
1953 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001954 if (is_guest_mode(vcpu)) {
1955 /*
1956 * L1's specified read shadow might not contain the TS bit,
1957 * so now that we turned on shadowing of this bit, we need to
1958 * set this bit of the shadow. Like in nested_vmx_run we need
1959 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
1960 * up-to-date here because we just decached cr0.TS (and we'll
1961 * only update vmcs12->guest_cr0 on nested exit).
1962 */
1963 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1964 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
1965 (vcpu->arch.cr0 & X86_CR0_TS);
1966 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
1967 } else
1968 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001969}
1970
Avi Kivity6aa8b732006-12-10 02:21:36 -08001971static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1972{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001973 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001974
Avi Kivity6de12732011-03-07 12:51:22 +02001975 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1976 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1977 rflags = vmcs_readl(GUEST_RFLAGS);
1978 if (to_vmx(vcpu)->rmode.vm86_active) {
1979 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1980 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1981 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1982 }
1983 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001984 }
Avi Kivity6de12732011-03-07 12:51:22 +02001985 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001986}
1987
1988static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1989{
Avi Kivity6de12732011-03-07 12:51:22 +02001990 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1991 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001992 if (to_vmx(vcpu)->rmode.vm86_active) {
1993 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001994 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001995 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996 vmcs_writel(GUEST_RFLAGS, rflags);
1997}
1998
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001999static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002000{
2001 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2002 int ret = 0;
2003
2004 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01002005 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002006 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01002007 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002008
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02002009 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04002010}
2011
2012static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2013{
2014 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2015 u32 interruptibility = interruptibility_old;
2016
2017 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2018
Jan Kiszka48005f62010-02-19 19:38:07 +01002019 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002020 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01002021 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04002022 interruptibility |= GUEST_INTR_STATE_STI;
2023
2024 if ((interruptibility != interruptibility_old))
2025 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2026}
2027
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
2029{
2030 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002032 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002033 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002034 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002035
Glauber Costa2809f5d2009-05-12 16:21:05 -04002036 /* skipping an emulated instruction also counts */
2037 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002038}
2039
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002040/*
2041 * KVM wants to inject page-faults which it got to the guest. This function
2042 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002043 */
Gleb Natapove011c662013-09-25 12:51:35 +03002044static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002045{
2046 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2047
Gleb Natapove011c662013-09-25 12:51:35 +03002048 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002049 return 0;
2050
Jan Kiszka533558b2014-01-04 18:47:20 +01002051 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
2052 vmcs_read32(VM_EXIT_INTR_INFO),
2053 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002054 return 1;
2055}
2056
Avi Kivity298101d2007-11-25 13:41:11 +02002057static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002058 bool has_error_code, u32 error_code,
2059 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002060{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002061 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002062 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002063
Gleb Natapove011c662013-09-25 12:51:35 +03002064 if (!reinject && is_guest_mode(vcpu) &&
2065 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002066 return;
2067
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002068 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002069 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002070 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2071 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002072
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002073 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002074 int inc_eip = 0;
2075 if (kvm_exception_is_soft(nr))
2076 inc_eip = vcpu->arch.event_exit_inst_len;
2077 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002078 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002079 return;
2080 }
2081
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002082 if (kvm_exception_is_soft(nr)) {
2083 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2084 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002085 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2086 } else
2087 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2088
2089 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002090}
2091
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002092static bool vmx_rdtscp_supported(void)
2093{
2094 return cpu_has_vmx_rdtscp();
2095}
2096
Mao, Junjiead756a12012-07-02 01:18:48 +00002097static bool vmx_invpcid_supported(void)
2098{
2099 return cpu_has_vmx_invpcid() && enable_ept;
2100}
2101
Avi Kivity6aa8b732006-12-10 02:21:36 -08002102/*
Eddie Donga75beee2007-05-17 18:55:15 +03002103 * Swap MSR entry in host/guest MSR entry array.
2104 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002105static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002106{
Avi Kivity26bb0982009-09-07 11:14:12 +03002107 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002108
2109 tmp = vmx->guest_msrs[to];
2110 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2111 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002112}
2113
Yang Zhang8d146952013-01-25 10:18:50 +08002114static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2115{
2116 unsigned long *msr_bitmap;
2117
2118 if (irqchip_in_kernel(vcpu->kvm) && apic_x2apic_mode(vcpu->arch.apic)) {
2119 if (is_long_mode(vcpu))
2120 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2121 else
2122 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
2123 } else {
2124 if (is_long_mode(vcpu))
2125 msr_bitmap = vmx_msr_bitmap_longmode;
2126 else
2127 msr_bitmap = vmx_msr_bitmap_legacy;
2128 }
2129
2130 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2131}
2132
Eddie Donga75beee2007-05-17 18:55:15 +03002133/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002134 * Set up the vmcs to automatically save and restore system
2135 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2136 * mode, as fiddling with msrs is very expensive.
2137 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002138static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002139{
Avi Kivity26bb0982009-09-07 11:14:12 +03002140 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002141
Eddie Donga75beee2007-05-17 18:55:15 +03002142 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002143#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002144 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002145 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002146 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002147 move_msr_up(vmx, index, save_nmsrs++);
2148 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002149 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002150 move_msr_up(vmx, index, save_nmsrs++);
2151 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002152 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002153 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002154 index = __find_msr_index(vmx, MSR_TSC_AUX);
2155 if (index >= 0 && vmx->rdtscp_enabled)
2156 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002157 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002158 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002159 * if efer.sce is enabled.
2160 */
Brian Gerst8c065852010-07-17 09:03:26 -04002161 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002162 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002163 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002164 }
Eddie Donga75beee2007-05-17 18:55:15 +03002165#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002166 index = __find_msr_index(vmx, MSR_EFER);
2167 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002168 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002169
Avi Kivity26bb0982009-09-07 11:14:12 +03002170 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002171
Yang Zhang8d146952013-01-25 10:18:50 +08002172 if (cpu_has_vmx_msr_bitmap())
2173 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002174}
2175
2176/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002177 * reads and returns guest's timestamp counter "register"
2178 * guest_tsc = host_tsc + tsc_offset -- 21.3
2179 */
2180static u64 guest_read_tsc(void)
2181{
2182 u64 host_tsc, tsc_offset;
2183
2184 rdtscll(host_tsc);
2185 tsc_offset = vmcs_read64(TSC_OFFSET);
2186 return host_tsc + tsc_offset;
2187}
2188
2189/*
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002190 * Like guest_read_tsc, but always returns L1's notion of the timestamp
2191 * counter, even if a nested guest (L2) is currently running.
2192 */
Paolo Bonzini48d89b92014-08-26 13:27:46 +02002193static u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002194{
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002195 u64 tsc_offset;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002196
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002197 tsc_offset = is_guest_mode(vcpu) ?
2198 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
2199 vmcs_read64(TSC_OFFSET);
2200 return host_tsc + tsc_offset;
2201}
2202
2203/*
Zachary Amsdencc578282012-02-03 15:43:50 -02002204 * Engage any workarounds for mis-matched TSC rates. Currently limited to
2205 * software catchup for faster rates on slower CPUs.
Joerg Roedel4051b182011-03-25 09:44:49 +01002206 */
Zachary Amsdencc578282012-02-03 15:43:50 -02002207static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +01002208{
Zachary Amsdencc578282012-02-03 15:43:50 -02002209 if (!scale)
2210 return;
2211
2212 if (user_tsc_khz > tsc_khz) {
2213 vcpu->arch.tsc_catchup = 1;
2214 vcpu->arch.tsc_always_catchup = 1;
2215 } else
2216 WARN(1, "user requested TSC rate below hardware speed\n");
Joerg Roedel4051b182011-03-25 09:44:49 +01002217}
2218
Will Auldba904632012-11-29 12:42:50 -08002219static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
2220{
2221 return vmcs_read64(TSC_OFFSET);
2222}
2223
Joerg Roedel4051b182011-03-25 09:44:49 +01002224/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002225 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002227static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002229 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002230 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002231 * We're here if L1 chose not to trap WRMSR to TSC. According
2232 * to the spec, this should set L1's TSC; The offset that L1
2233 * set for L2 remains unchanged, and still needs to be added
2234 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002235 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002236 struct vmcs12 *vmcs12;
2237 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
2238 /* recalculate vmcs02.TSC_OFFSET: */
2239 vmcs12 = get_vmcs12(vcpu);
2240 vmcs_write64(TSC_OFFSET, offset +
2241 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2242 vmcs12->tsc_offset : 0));
2243 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002244 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2245 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002246 vmcs_write64(TSC_OFFSET, offset);
2247 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002248}
2249
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002250static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10002251{
2252 u64 offset = vmcs_read64(TSC_OFFSET);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002253
Zachary Amsdene48672f2010-08-19 22:07:23 -10002254 vmcs_write64(TSC_OFFSET, offset + adjustment);
Nadav Har'El79918252011-05-25 23:15:39 +03002255 if (is_guest_mode(vcpu)) {
2256 /* Even when running L2, the adjustment needs to apply to L1 */
2257 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002258 } else
2259 trace_kvm_write_tsc_offset(vcpu->vcpu_id, offset,
2260 offset + adjustment);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002261}
2262
Joerg Roedel857e4092011-03-25 09:44:50 +01002263static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
2264{
2265 return target_tsc - native_read_tsc();
2266}
2267
Nadav Har'El801d3422011-05-25 23:02:23 +03002268static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2269{
2270 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2271 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2272}
2273
2274/*
2275 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2276 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2277 * all guests if the "nested" module option is off, and can also be disabled
2278 * for a single guest by disabling its VMX cpuid bit.
2279 */
2280static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2281{
2282 return nested && guest_cpuid_has_vmx(vcpu);
2283}
2284
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002286 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2287 * returned for the various VMX controls MSRs when nested VMX is enabled.
2288 * The same values should also be used to verify that vmcs12 control fields are
2289 * valid during nested entry from L1 to L2.
2290 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2291 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2292 * bit in the high half is on if the corresponding bit in the control field
2293 * may be on. See also vmx_control_verify().
2294 * TODO: allow these variables to be modified (downgraded) by module options
2295 * or other means.
2296 */
2297static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002298static u32 nested_vmx_true_procbased_ctls_low;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002299static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
2300static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
2301static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002302static u32 nested_vmx_true_exit_ctls_low;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002303static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002304static u32 nested_vmx_true_entry_ctls_low;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002305static u32 nested_vmx_misc_low, nested_vmx_misc_high;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03002306static u32 nested_vmx_ept_caps;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002307static __init void nested_vmx_setup_ctls_msrs(void)
2308{
2309 /*
2310 * Note that as a general rule, the high half of the MSRs (bits in
2311 * the control fields which may be 1) should be initialized by the
2312 * intersection of the underlying hardware's MSR (i.e., features which
2313 * can be supported) and the list of features we want to expose -
2314 * because they are known to be properly supported in our code.
2315 * Also, usually, the low half of the MSRs (bits which must be 1) can
2316 * be set to 0, meaning that L1 may turn off any of these bits. The
2317 * reason is that if one of these bits is necessary, it will appear
2318 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2319 * fields of vmcs01 and vmcs02, will turn these bits off - and
2320 * nested_vmx_exit_handled() will not pass related exits to L1.
2321 * These rules have exceptions below.
2322 */
2323
2324 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002325 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
2326 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high);
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002327 nested_vmx_pinbased_ctls_low |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2328 nested_vmx_pinbased_ctls_high &= PIN_BASED_EXT_INTR_MASK |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002329 PIN_BASED_NMI_EXITING | PIN_BASED_VIRTUAL_NMIS;
2330 nested_vmx_pinbased_ctls_high |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002331 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002332
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002333 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002334 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
2335 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high);
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002336 nested_vmx_exit_ctls_low = VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002337
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002338 nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002339#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002340 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002341#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002342 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
2343 nested_vmx_exit_ctls_high |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
2344 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002345 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2346
Paolo Bonzini36be0b92014-02-24 12:30:04 +01002347 if (vmx_mpx_supported())
2348 nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002349
Jan Kiszka2996fca2014-06-16 13:59:43 +02002350 /* We support free control of debug control saving. */
2351 nested_vmx_true_exit_ctls_low = nested_vmx_exit_ctls_low &
2352 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2353
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002354 /* entry controls */
2355 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
2356 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002357 nested_vmx_entry_ctls_low = VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002358 nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002359#ifdef CONFIG_X86_64
2360 VM_ENTRY_IA32E_MODE |
2361#endif
2362 VM_ENTRY_LOAD_IA32_PAT;
Nadav Har'El8049d652013-08-05 11:07:06 +03002363 nested_vmx_entry_ctls_high |= (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR |
2364 VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzini36be0b92014-02-24 12:30:04 +01002365 if (vmx_mpx_supported())
2366 nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002367
Jan Kiszka2996fca2014-06-16 13:59:43 +02002368 /* We support free control of debug control loading. */
2369 nested_vmx_true_entry_ctls_low = nested_vmx_entry_ctls_low &
2370 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2371
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002372 /* cpu-based controls */
2373 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
2374 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002375 nested_vmx_procbased_ctls_low = CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002376 nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002377 CPU_BASED_VIRTUAL_INTR_PENDING |
2378 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002379 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2380 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2381 CPU_BASED_CR3_STORE_EXITING |
2382#ifdef CONFIG_X86_64
2383 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2384#endif
2385 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
2386 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
Avi Kivitydbcb4e72012-08-13 15:38:22 +03002387 CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING |
Wanpeng Lia7c0b072014-08-21 19:46:50 +08002388 CPU_BASED_PAUSE_EXITING | CPU_BASED_TPR_SHADOW |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002389 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2390 /*
2391 * We can allow some features even when not supported by the
2392 * hardware. For example, L1 can specify an MSR bitmap - and we
2393 * can use it to avoid exits to L1 - even when L0 runs L2
2394 * without MSR bitmaps.
2395 */
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002396 nested_vmx_procbased_ctls_high |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
2397 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002398
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002399 /* We support free control of CR3 access interception. */
2400 nested_vmx_true_procbased_ctls_low = nested_vmx_procbased_ctls_low &
2401 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2402
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002403 /* secondary cpu-based controls */
2404 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
2405 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
2406 nested_vmx_secondary_ctls_low = 0;
2407 nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002408 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Wanpeng Li81dc01f2014-12-04 19:11:07 +08002409 SECONDARY_EXEC_WBINVD_EXITING |
2410 SECONDARY_EXEC_XSAVES;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002411
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002412 if (enable_ept) {
2413 /* nested EPT: emulate EPT also to L1 */
Bandan Das78051e32014-12-06 20:32:16 +05302414 nested_vmx_secondary_ctls_high |= SECONDARY_EXEC_ENABLE_EPT |
2415 SECONDARY_EXEC_UNRESTRICTED_GUEST;
Jan Kiszkaca72d972013-08-06 10:39:55 +02002416 nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002417 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2418 VMX_EPT_INVEPT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002419 nested_vmx_ept_caps &= vmx_capability.ept;
2420 /*
Bandan Das4b855072014-04-19 18:17:44 -04002421 * For nested guests, we don't do anything specific
2422 * for single context invalidation. Hence, only advertise
2423 * support for global context invalidation.
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002424 */
Bandan Das4b855072014-04-19 18:17:44 -04002425 nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002426 } else
2427 nested_vmx_ept_caps = 0;
2428
Jan Kiszkac18911a2013-03-13 16:06:41 +01002429 /* miscellaneous data */
2430 rdmsr(MSR_IA32_VMX_MISC, nested_vmx_misc_low, nested_vmx_misc_high);
Jan Kiszkaf4124502014-03-07 20:03:13 +01002431 nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2432 nested_vmx_misc_low |= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
2433 VMX_MISC_ACTIVITY_HLT;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002434 nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002435}
2436
2437static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2438{
2439 /*
2440 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2441 */
2442 return ((control & high) | low) == control;
2443}
2444
2445static inline u64 vmx_control_msr(u32 low, u32 high)
2446{
2447 return low | ((u64)high << 32);
2448}
2449
Jan Kiszkacae50132014-01-04 18:47:22 +01002450/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002451static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2452{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002453 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002454 case MSR_IA32_VMX_BASIC:
2455 /*
2456 * This MSR reports some information about VMX support. We
2457 * should return information about the VMX we emulate for the
2458 * guest, and the VMCS structure we give it - not about the
2459 * VMX support of the underlying hardware.
2460 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002461 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002462 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2463 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2464 break;
2465 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2466 case MSR_IA32_VMX_PINBASED_CTLS:
2467 *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
2468 nested_vmx_pinbased_ctls_high);
2469 break;
2470 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02002471 *pdata = vmx_control_msr(nested_vmx_true_procbased_ctls_low,
2472 nested_vmx_procbased_ctls_high);
2473 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002474 case MSR_IA32_VMX_PROCBASED_CTLS:
2475 *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
2476 nested_vmx_procbased_ctls_high);
2477 break;
2478 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Jan Kiszka2996fca2014-06-16 13:59:43 +02002479 *pdata = vmx_control_msr(nested_vmx_true_exit_ctls_low,
2480 nested_vmx_exit_ctls_high);
2481 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002482 case MSR_IA32_VMX_EXIT_CTLS:
2483 *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
2484 nested_vmx_exit_ctls_high);
2485 break;
2486 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Jan Kiszka2996fca2014-06-16 13:59:43 +02002487 *pdata = vmx_control_msr(nested_vmx_true_entry_ctls_low,
2488 nested_vmx_entry_ctls_high);
2489 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002490 case MSR_IA32_VMX_ENTRY_CTLS:
2491 *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
2492 nested_vmx_entry_ctls_high);
2493 break;
2494 case MSR_IA32_VMX_MISC:
Jan Kiszkac18911a2013-03-13 16:06:41 +01002495 *pdata = vmx_control_msr(nested_vmx_misc_low,
2496 nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002497 break;
2498 /*
2499 * These MSRs specify bits which the guest must keep fixed (on or off)
2500 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2501 * We picked the standard core2 setting.
2502 */
2503#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2504#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2505 case MSR_IA32_VMX_CR0_FIXED0:
2506 *pdata = VMXON_CR0_ALWAYSON;
2507 break;
2508 case MSR_IA32_VMX_CR0_FIXED1:
2509 *pdata = -1ULL;
2510 break;
2511 case MSR_IA32_VMX_CR4_FIXED0:
2512 *pdata = VMXON_CR4_ALWAYSON;
2513 break;
2514 case MSR_IA32_VMX_CR4_FIXED1:
2515 *pdata = -1ULL;
2516 break;
2517 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02002518 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002519 break;
2520 case MSR_IA32_VMX_PROCBASED_CTLS2:
2521 *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
2522 nested_vmx_secondary_ctls_high);
2523 break;
2524 case MSR_IA32_VMX_EPT_VPID_CAP:
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002525 /* Currently, no nested vpid support */
2526 *pdata = nested_vmx_ept_caps;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002527 break;
2528 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002529 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002530 }
2531
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002532 return 0;
2533}
2534
2535/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536 * Reads an msr value (of 'msr_index') into 'pdata'.
2537 * Returns 0 on success, non-0 otherwise.
2538 * Assumes vcpu_load() was already called.
2539 */
2540static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2541{
2542 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03002543 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544
2545 if (!pdata) {
2546 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
2547 return -EINVAL;
2548 }
2549
2550 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002551#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552 case MSR_FS_BASE:
2553 data = vmcs_readl(GUEST_FS_BASE);
2554 break;
2555 case MSR_GS_BASE:
2556 data = vmcs_readl(GUEST_GS_BASE);
2557 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002558 case MSR_KERNEL_GS_BASE:
2559 vmx_load_host_state(to_vmx(vcpu));
2560 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
2561 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002562#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002563 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002564 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302565 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566 data = guest_read_tsc();
2567 break;
2568 case MSR_IA32_SYSENTER_CS:
2569 data = vmcs_read32(GUEST_SYSENTER_CS);
2570 break;
2571 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002572 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002573 break;
2574 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002575 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002577 case MSR_IA32_BNDCFGS:
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002578 if (!vmx_mpx_supported())
2579 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002580 data = vmcs_read64(GUEST_BNDCFGS);
2581 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002582 case MSR_IA32_FEATURE_CONTROL:
2583 if (!nested_vmx_allowed(vcpu))
2584 return 1;
2585 data = to_vmx(vcpu)->nested.msr_ia32_feature_control;
2586 break;
2587 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2588 if (!nested_vmx_allowed(vcpu))
2589 return 1;
2590 return vmx_get_vmx_msr(vcpu, msr_index, pdata);
Wanpeng Li20300092014-12-02 19:14:59 +08002591 case MSR_IA32_XSS:
2592 if (!vmx_xsaves_supported())
2593 return 1;
2594 data = vcpu->arch.ia32_xss;
2595 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002596 case MSR_TSC_AUX:
2597 if (!to_vmx(vcpu)->rdtscp_enabled)
2598 return 1;
2599 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002601 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002602 if (msr) {
2603 data = msr->data;
2604 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002605 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002606 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607 }
2608
2609 *pdata = data;
2610 return 0;
2611}
2612
Jan Kiszkacae50132014-01-04 18:47:22 +01002613static void vmx_leave_nested(struct kvm_vcpu *vcpu);
2614
Avi Kivity6aa8b732006-12-10 02:21:36 -08002615/*
2616 * Writes msr value into into the appropriate "register".
2617 * Returns 0 on success, non-0 otherwise.
2618 * Assumes vcpu_load() was already called.
2619 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002620static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002622 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002623 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002624 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002625 u32 msr_index = msr_info->index;
2626 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03002627
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002629 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002630 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002631 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002632#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002634 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635 vmcs_writel(GUEST_FS_BASE, data);
2636 break;
2637 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002638 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639 vmcs_writel(GUEST_GS_BASE, data);
2640 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002641 case MSR_KERNEL_GS_BASE:
2642 vmx_load_host_state(vmx);
2643 vmx->msr_guest_kernel_gs_base = data;
2644 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002645#endif
2646 case MSR_IA32_SYSENTER_CS:
2647 vmcs_write32(GUEST_SYSENTER_CS, data);
2648 break;
2649 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002650 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002651 break;
2652 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002653 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002654 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002655 case MSR_IA32_BNDCFGS:
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002656 if (!vmx_mpx_supported())
2657 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002658 vmcs_write64(GUEST_BNDCFGS, data);
2659 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302660 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08002661 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002663 case MSR_IA32_CR_PAT:
2664 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Amit45666542014-09-18 22:39:44 +03002665 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2666 return 1;
Sheng Yang468d4722008-10-09 16:01:55 +08002667 vmcs_write64(GUEST_IA32_PAT, data);
2668 vcpu->arch.pat = data;
2669 break;
2670 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002671 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002672 break;
Will Auldba904632012-11-29 12:42:50 -08002673 case MSR_IA32_TSC_ADJUST:
2674 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002675 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002676 case MSR_IA32_FEATURE_CONTROL:
2677 if (!nested_vmx_allowed(vcpu) ||
2678 (to_vmx(vcpu)->nested.msr_ia32_feature_control &
2679 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
2680 return 1;
2681 vmx->nested.msr_ia32_feature_control = data;
2682 if (msr_info->host_initiated && data == 0)
2683 vmx_leave_nested(vcpu);
2684 break;
2685 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2686 return 1; /* they are read-only */
Wanpeng Li20300092014-12-02 19:14:59 +08002687 case MSR_IA32_XSS:
2688 if (!vmx_xsaves_supported())
2689 return 1;
2690 /*
2691 * The only supported bit as of Skylake is bit 8, but
2692 * it is not supported on KVM.
2693 */
2694 if (data != 0)
2695 return 1;
2696 vcpu->arch.ia32_xss = data;
2697 if (vcpu->arch.ia32_xss != host_xss)
2698 add_atomic_switch_msr(vmx, MSR_IA32_XSS,
2699 vcpu->arch.ia32_xss, host_xss);
2700 else
2701 clear_atomic_switch_msr(vmx, MSR_IA32_XSS);
2702 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002703 case MSR_TSC_AUX:
2704 if (!vmx->rdtscp_enabled)
2705 return 1;
2706 /* Check reserved bit, higher 32 bits should be zero */
2707 if ((data >> 32) != 0)
2708 return 1;
2709 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002711 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002712 if (msr) {
Andy Honig8b3c3102014-08-27 11:16:44 -07002713 u64 old_msr_data = msr->data;
Avi Kivity3bab1f52006-12-29 16:49:48 -08002714 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002715 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2716 preempt_disable();
Andy Honig8b3c3102014-08-27 11:16:44 -07002717 ret = kvm_set_shared_msr(msr->index, msr->data,
2718 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03002719 preempt_enable();
Andy Honig8b3c3102014-08-27 11:16:44 -07002720 if (ret)
2721 msr->data = old_msr_data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002722 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002723 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002725 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726 }
2727
Eddie Dong2cc51562007-05-21 07:28:09 +03002728 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729}
2730
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002731static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002733 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2734 switch (reg) {
2735 case VCPU_REGS_RSP:
2736 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2737 break;
2738 case VCPU_REGS_RIP:
2739 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2740 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002741 case VCPU_EXREG_PDPTR:
2742 if (enable_ept)
2743 ept_save_pdptrs(vcpu);
2744 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002745 default:
2746 break;
2747 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748}
2749
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750static __init int cpu_has_kvm_support(void)
2751{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002752 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753}
2754
2755static __init int vmx_disabled_by_bios(void)
2756{
2757 u64 msr;
2758
2759 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04002760 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08002761 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04002762 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2763 && tboot_enabled())
2764 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08002765 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04002766 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08002767 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08002768 && !tboot_enabled()) {
2769 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08002770 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04002771 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08002772 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08002773 /* launched w/o TXT and VMX disabled */
2774 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2775 && !tboot_enabled())
2776 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04002777 }
2778
2779 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780}
2781
Dongxiao Xu7725b892010-05-11 18:29:38 +08002782static void kvm_cpu_vmxon(u64 addr)
2783{
2784 asm volatile (ASM_VMX_VMXON_RAX
2785 : : "a"(&addr), "m"(addr)
2786 : "memory", "cc");
2787}
2788
Radim Krčmář13a34e02014-08-28 15:13:03 +02002789static int hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790{
2791 int cpu = raw_smp_processor_id();
2792 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04002793 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794
Alexander Graf10474ae2009-09-15 11:37:46 +02002795 if (read_cr4() & X86_CR4_VMXE)
2796 return -EBUSY;
2797
Nadav Har'Eld462b812011-05-24 15:26:10 +03002798 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002799
2800 /*
2801 * Now we can enable the vmclear operation in kdump
2802 * since the loaded_vmcss_on_cpu list on this cpu
2803 * has been initialized.
2804 *
2805 * Though the cpu is not in VMX operation now, there
2806 * is no problem to enable the vmclear operation
2807 * for the loaded_vmcss_on_cpu list is empty!
2808 */
2809 crash_enable_local_vmclear(cpu);
2810
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04002812
2813 test_bits = FEATURE_CONTROL_LOCKED;
2814 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2815 if (tboot_enabled())
2816 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2817
2818 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04002820 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2821 }
Rusty Russell66aee912007-07-17 23:34:16 +10002822 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02002823
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002824 if (vmm_exclusive) {
2825 kvm_cpu_vmxon(phys_addr);
2826 ept_sync_global();
2827 }
Alexander Graf10474ae2009-09-15 11:37:46 +02002828
Christoph Lameter89cbc762014-08-17 12:30:40 -05002829 native_store_gdt(this_cpu_ptr(&host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03002830
Alexander Graf10474ae2009-09-15 11:37:46 +02002831 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832}
2833
Nadav Har'Eld462b812011-05-24 15:26:10 +03002834static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002835{
2836 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002837 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002838
Nadav Har'Eld462b812011-05-24 15:26:10 +03002839 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2840 loaded_vmcss_on_cpu_link)
2841 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002842}
2843
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002844
2845/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2846 * tricks.
2847 */
2848static void kvm_cpu_vmxoff(void)
2849{
2850 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002851}
2852
Radim Krčmář13a34e02014-08-28 15:13:03 +02002853static void hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002855 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002856 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002857 kvm_cpu_vmxoff();
2858 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08002859 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860}
2861
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002862static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002863 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864{
2865 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002866 u32 ctl = ctl_min | ctl_opt;
2867
2868 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2869
2870 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2871 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2872
2873 /* Ensure minimum (required) set of control bits are supported. */
2874 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002875 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002876
2877 *result = ctl;
2878 return 0;
2879}
2880
Avi Kivity110312c2010-12-21 12:54:20 +02002881static __init bool allow_1_setting(u32 msr, u32 ctl)
2882{
2883 u32 vmx_msr_low, vmx_msr_high;
2884
2885 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2886 return vmx_msr_high & ctl;
2887}
2888
Yang, Sheng002c7f72007-07-31 14:23:01 +03002889static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002890{
2891 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002892 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002893 u32 _pin_based_exec_control = 0;
2894 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002895 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002896 u32 _vmexit_control = 0;
2897 u32 _vmentry_control = 0;
2898
Raghavendra K T10166742012-02-07 23:19:20 +05302899 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002900#ifdef CONFIG_X86_64
2901 CPU_BASED_CR8_LOAD_EXITING |
2902 CPU_BASED_CR8_STORE_EXITING |
2903#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002904 CPU_BASED_CR3_LOAD_EXITING |
2905 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002906 CPU_BASED_USE_IO_BITMAPS |
2907 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03002908 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08002909 CPU_BASED_MWAIT_EXITING |
2910 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002911 CPU_BASED_INVLPG_EXITING |
2912 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002913
Sheng Yangf78e0e22007-10-29 09:40:42 +08002914 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002915 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002916 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002917 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2918 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002919 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002920#ifdef CONFIG_X86_64
2921 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2922 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2923 ~CPU_BASED_CR8_STORE_EXITING;
2924#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002925 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002926 min2 = 0;
2927 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002928 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002929 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002930 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002931 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002932 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002933 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00002934 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002935 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002936 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03002937 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
Wanpeng Li20300092014-12-02 19:14:59 +08002938 SECONDARY_EXEC_SHADOW_VMCS |
Kai Huang843e4332015-01-28 10:54:28 +08002939 SECONDARY_EXEC_XSAVES |
2940 SECONDARY_EXEC_ENABLE_PML;
Sheng Yangd56f5462008-04-25 10:13:16 +08002941 if (adjust_vmx_controls(min2, opt2,
2942 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002943 &_cpu_based_2nd_exec_control) < 0)
2944 return -EIO;
2945 }
2946#ifndef CONFIG_X86_64
2947 if (!(_cpu_based_2nd_exec_control &
2948 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2949 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2950#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002951
2952 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2953 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002954 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002955 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2956 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002957
Sheng Yangd56f5462008-04-25 10:13:16 +08002958 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002959 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2960 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002961 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2962 CPU_BASED_CR3_STORE_EXITING |
2963 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08002964 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
2965 vmx_capability.ept, vmx_capability.vpid);
2966 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002967
Paolo Bonzini81908bf2014-02-21 10:32:27 +01002968 min = VM_EXIT_SAVE_DEBUG_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002969#ifdef CONFIG_X86_64
2970 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2971#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08002972 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002973 VM_EXIT_ACK_INTR_ON_EXIT | VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002974 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2975 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002976 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002977
Yang Zhang01e439b2013-04-11 19:25:12 +08002978 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2979 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR;
2980 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2981 &_pin_based_exec_control) < 0)
2982 return -EIO;
2983
2984 if (!(_cpu_based_2nd_exec_control &
2985 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) ||
2986 !(_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT))
2987 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2988
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002989 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002990 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002991 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2992 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002993 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002995 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002996
2997 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2998 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002999 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003000
3001#ifdef CONFIG_X86_64
3002 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3003 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03003004 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003005#endif
3006
3007 /* Require Write-Back (WB) memory type for VMCS accesses. */
3008 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003009 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003010
Yang, Sheng002c7f72007-07-31 14:23:01 +03003011 vmcs_conf->size = vmx_msr_high & 0x1fff;
3012 vmcs_conf->order = get_order(vmcs_config.size);
3013 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003014
Yang, Sheng002c7f72007-07-31 14:23:01 +03003015 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3016 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003017 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003018 vmcs_conf->vmexit_ctrl = _vmexit_control;
3019 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003020
Avi Kivity110312c2010-12-21 12:54:20 +02003021 cpu_has_load_ia32_efer =
3022 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3023 VM_ENTRY_LOAD_IA32_EFER)
3024 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3025 VM_EXIT_LOAD_IA32_EFER);
3026
Gleb Natapov8bf00a52011-10-05 14:01:22 +02003027 cpu_has_load_perf_global_ctrl =
3028 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
3029 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
3030 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
3031 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
3032
3033 /*
3034 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
3035 * but due to arrata below it can't be used. Workaround is to use
3036 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3037 *
3038 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
3039 *
3040 * AAK155 (model 26)
3041 * AAP115 (model 30)
3042 * AAT100 (model 37)
3043 * BC86,AAY89,BD102 (model 44)
3044 * BA97 (model 46)
3045 *
3046 */
3047 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
3048 switch (boot_cpu_data.x86_model) {
3049 case 26:
3050 case 30:
3051 case 37:
3052 case 44:
3053 case 46:
3054 cpu_has_load_perf_global_ctrl = false;
3055 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3056 "does not work properly. Using workaround\n");
3057 break;
3058 default:
3059 break;
3060 }
3061 }
3062
Wanpeng Li20300092014-12-02 19:14:59 +08003063 if (cpu_has_xsaves)
3064 rdmsrl(MSR_IA32_XSS, host_xss);
3065
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003066 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08003067}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068
3069static struct vmcs *alloc_vmcs_cpu(int cpu)
3070{
3071 int node = cpu_to_node(cpu);
3072 struct page *pages;
3073 struct vmcs *vmcs;
3074
Mel Gorman6484eb32009-06-16 15:31:54 -07003075 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076 if (!pages)
3077 return NULL;
3078 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003079 memset(vmcs, 0, vmcs_config.size);
3080 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 return vmcs;
3082}
3083
3084static struct vmcs *alloc_vmcs(void)
3085{
Ingo Molnard3b2c332007-01-05 16:36:23 -08003086 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003087}
3088
3089static void free_vmcs(struct vmcs *vmcs)
3090{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003091 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092}
3093
Nadav Har'Eld462b812011-05-24 15:26:10 +03003094/*
3095 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3096 */
3097static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3098{
3099 if (!loaded_vmcs->vmcs)
3100 return;
3101 loaded_vmcs_clear(loaded_vmcs);
3102 free_vmcs(loaded_vmcs->vmcs);
3103 loaded_vmcs->vmcs = NULL;
3104}
3105
Sam Ravnborg39959582007-06-01 00:47:13 -07003106static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003107{
3108 int cpu;
3109
Zachary Amsden3230bb42009-09-29 11:38:37 -10003110 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003111 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003112 per_cpu(vmxarea, cpu) = NULL;
3113 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003114}
3115
Bandan Dasfe2b2012014-04-21 15:20:14 -04003116static void init_vmcs_shadow_fields(void)
3117{
3118 int i, j;
3119
3120 /* No checks for read only fields yet */
3121
3122 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3123 switch (shadow_read_write_fields[i]) {
3124 case GUEST_BNDCFGS:
3125 if (!vmx_mpx_supported())
3126 continue;
3127 break;
3128 default:
3129 break;
3130 }
3131
3132 if (j < i)
3133 shadow_read_write_fields[j] =
3134 shadow_read_write_fields[i];
3135 j++;
3136 }
3137 max_shadow_read_write_fields = j;
3138
3139 /* shadowed fields guest access without vmexit */
3140 for (i = 0; i < max_shadow_read_write_fields; i++) {
3141 clear_bit(shadow_read_write_fields[i],
3142 vmx_vmwrite_bitmap);
3143 clear_bit(shadow_read_write_fields[i],
3144 vmx_vmread_bitmap);
3145 }
3146 for (i = 0; i < max_shadow_read_only_fields; i++)
3147 clear_bit(shadow_read_only_fields[i],
3148 vmx_vmread_bitmap);
3149}
3150
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151static __init int alloc_kvm_area(void)
3152{
3153 int cpu;
3154
Zachary Amsden3230bb42009-09-29 11:38:37 -10003155 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156 struct vmcs *vmcs;
3157
3158 vmcs = alloc_vmcs_cpu(cpu);
3159 if (!vmcs) {
3160 free_kvm_area();
3161 return -ENOMEM;
3162 }
3163
3164 per_cpu(vmxarea, cpu) = vmcs;
3165 }
3166 return 0;
3167}
3168
Gleb Natapov14168782013-01-21 15:36:49 +02003169static bool emulation_required(struct kvm_vcpu *vcpu)
3170{
3171 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3172}
3173
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003174static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003175 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003177 if (!emulate_invalid_guest_state) {
3178 /*
3179 * CS and SS RPL should be equal during guest entry according
3180 * to VMX spec, but in reality it is not always so. Since vcpu
3181 * is in the middle of the transition from real mode to
3182 * protected mode it is safe to assume that RPL 0 is a good
3183 * default value.
3184 */
3185 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
3186 save->selector &= ~SELECTOR_RPL_MASK;
3187 save->dpl = save->selector & SELECTOR_RPL_MASK;
3188 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003189 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003190 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191}
3192
3193static void enter_pmode(struct kvm_vcpu *vcpu)
3194{
3195 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003196 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003197
Gleb Natapovd99e4152012-12-20 16:57:45 +02003198 /*
3199 * Update real mode segment cache. It may be not up-to-date if sement
3200 * register was written while vcpu was in a guest mode.
3201 */
3202 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3203 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3204 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3205 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3206 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3207 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3208
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003209 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210
Avi Kivity2fb92db2011-04-27 19:42:18 +03003211 vmx_segment_cache_clear(vmx);
3212
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003213 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214
3215 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003216 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3217 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218 vmcs_writel(GUEST_RFLAGS, flags);
3219
Rusty Russell66aee912007-07-17 23:34:16 +10003220 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3221 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222
3223 update_exception_bitmap(vcpu);
3224
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003225 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3226 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3227 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3228 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3229 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3230 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231}
3232
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003233static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003234{
Mathias Krause772e0312012-08-30 01:30:19 +02003235 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003236 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237
Gleb Natapovd99e4152012-12-20 16:57:45 +02003238 var.dpl = 0x3;
3239 if (seg == VCPU_SREG_CS)
3240 var.type = 0x3;
3241
3242 if (!emulate_invalid_guest_state) {
3243 var.selector = var.base >> 4;
3244 var.base = var.base & 0xffff0;
3245 var.limit = 0xffff;
3246 var.g = 0;
3247 var.db = 0;
3248 var.present = 1;
3249 var.s = 1;
3250 var.l = 0;
3251 var.unusable = 0;
3252 var.type = 0x3;
3253 var.avl = 0;
3254 if (save->base & 0xf)
3255 printk_once(KERN_WARNING "kvm: segment base is not "
3256 "paragraph aligned when entering "
3257 "protected mode (seg=%d)", seg);
3258 }
3259
3260 vmcs_write16(sf->selector, var.selector);
3261 vmcs_write32(sf->base, var.base);
3262 vmcs_write32(sf->limit, var.limit);
3263 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003264}
3265
3266static void enter_rmode(struct kvm_vcpu *vcpu)
3267{
3268 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003269 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003270
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003271 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3272 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3273 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3274 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3275 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003276 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3277 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003278
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003279 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003280
Gleb Natapov776e58e2011-03-13 12:34:27 +02003281 /*
3282 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003283 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003284 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003285 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003286 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3287 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003288
Avi Kivity2fb92db2011-04-27 19:42:18 +03003289 vmx_segment_cache_clear(vmx);
3290
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003291 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003292 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3294
3295 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003296 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003298 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003299
3300 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003301 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003302 update_exception_bitmap(vcpu);
3303
Gleb Natapovd99e4152012-12-20 16:57:45 +02003304 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3305 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3306 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3307 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3308 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3309 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003310
Eddie Dong8668a3c2007-10-10 14:26:45 +08003311 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003312}
3313
Amit Shah401d10d2009-02-20 22:53:37 +05303314static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3315{
3316 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003317 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3318
3319 if (!msr)
3320 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303321
Avi Kivity44ea2b12009-09-06 15:55:37 +03003322 /*
3323 * Force kernel_gs_base reloading before EFER changes, as control
3324 * of this msr depends on is_long_mode().
3325 */
3326 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003327 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303328 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003329 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303330 msr->data = efer;
3331 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003332 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303333
3334 msr->data = efer & ~EFER_LME;
3335 }
3336 setup_msrs(vmx);
3337}
3338
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003339#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003340
3341static void enter_lmode(struct kvm_vcpu *vcpu)
3342{
3343 u32 guest_tr_ar;
3344
Avi Kivity2fb92db2011-04-27 19:42:18 +03003345 vmx_segment_cache_clear(to_vmx(vcpu));
3346
Avi Kivity6aa8b732006-12-10 02:21:36 -08003347 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
3348 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003349 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3350 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003351 vmcs_write32(GUEST_TR_AR_BYTES,
3352 (guest_tr_ar & ~AR_TYPE_MASK)
3353 | AR_TYPE_BUSY_64_TSS);
3354 }
Avi Kivityda38f432010-07-06 11:30:49 +03003355 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003356}
3357
3358static void exit_lmode(struct kvm_vcpu *vcpu)
3359{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003360 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003361 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362}
3363
3364#endif
3365
Sheng Yang2384d2b2008-01-17 15:14:33 +08003366static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3367{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08003368 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003369 if (enable_ept) {
3370 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3371 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003372 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003373 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003374}
3375
Avi Kivitye8467fd2009-12-29 18:43:06 +02003376static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3377{
3378 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3379
3380 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3381 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3382}
3383
Avi Kivityaff48ba2010-12-05 18:56:11 +02003384static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3385{
3386 if (enable_ept && is_paging(vcpu))
3387 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3388 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3389}
3390
Anthony Liguori25c4c272007-04-27 09:29:21 +03003391static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003392{
Avi Kivityfc78f512009-12-07 12:16:48 +02003393 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3394
3395 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3396 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003397}
3398
Sheng Yang14394422008-04-28 12:24:45 +08003399static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3400{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003401 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3402
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003403 if (!test_bit(VCPU_EXREG_PDPTR,
3404 (unsigned long *)&vcpu->arch.regs_dirty))
3405 return;
3406
Sheng Yang14394422008-04-28 12:24:45 +08003407 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003408 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3409 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3410 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3411 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003412 }
3413}
3414
Avi Kivity8f5d5492009-05-31 18:41:29 +03003415static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3416{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003417 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3418
Avi Kivity8f5d5492009-05-31 18:41:29 +03003419 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003420 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3421 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3422 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3423 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003424 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003425
3426 __set_bit(VCPU_EXREG_PDPTR,
3427 (unsigned long *)&vcpu->arch.regs_avail);
3428 __set_bit(VCPU_EXREG_PDPTR,
3429 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003430}
3431
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003432static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003433
3434static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3435 unsigned long cr0,
3436 struct kvm_vcpu *vcpu)
3437{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003438 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3439 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003440 if (!(cr0 & X86_CR0_PG)) {
3441 /* From paging/starting to nonpaging */
3442 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003443 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003444 (CPU_BASED_CR3_LOAD_EXITING |
3445 CPU_BASED_CR3_STORE_EXITING));
3446 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003447 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003448 } else if (!is_paging(vcpu)) {
3449 /* From nonpaging to paging */
3450 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003451 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003452 ~(CPU_BASED_CR3_LOAD_EXITING |
3453 CPU_BASED_CR3_STORE_EXITING));
3454 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003455 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003456 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003457
3458 if (!(cr0 & X86_CR0_WP))
3459 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003460}
3461
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3463{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003464 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003465 unsigned long hw_cr0;
3466
Gleb Natapov50378782013-02-04 16:00:28 +02003467 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003468 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003469 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003470 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003471 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003472
Gleb Natapov218e7632013-01-21 15:36:45 +02003473 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3474 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003475
Gleb Natapov218e7632013-01-21 15:36:45 +02003476 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3477 enter_rmode(vcpu);
3478 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003480#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003481 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003482 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003483 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003484 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003485 exit_lmode(vcpu);
3486 }
3487#endif
3488
Avi Kivity089d0342009-03-23 18:26:32 +02003489 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003490 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3491
Avi Kivity02daab22009-12-30 12:40:26 +02003492 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003493 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003494
Avi Kivity6aa8b732006-12-10 02:21:36 -08003495 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003496 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003497 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003498
3499 /* depends on vcpu->arch.cr0 to be set to a new value */
3500 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003501}
3502
Sheng Yang14394422008-04-28 12:24:45 +08003503static u64 construct_eptp(unsigned long root_hpa)
3504{
3505 u64 eptp;
3506
3507 /* TODO write the value reading from MSR */
3508 eptp = VMX_EPT_DEFAULT_MT |
3509 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003510 if (enable_ept_ad_bits)
3511 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003512 eptp |= (root_hpa & PAGE_MASK);
3513
3514 return eptp;
3515}
3516
Avi Kivity6aa8b732006-12-10 02:21:36 -08003517static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3518{
Sheng Yang14394422008-04-28 12:24:45 +08003519 unsigned long guest_cr3;
3520 u64 eptp;
3521
3522 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003523 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003524 eptp = construct_eptp(cr3);
3525 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02003526 if (is_paging(vcpu) || is_guest_mode(vcpu))
3527 guest_cr3 = kvm_read_cr3(vcpu);
3528 else
3529 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02003530 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003531 }
3532
Sheng Yang2384d2b2008-01-17 15:14:33 +08003533 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003534 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003535}
3536
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003537static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003538{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003539 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08003540 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
3541
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003542 if (cr4 & X86_CR4_VMXE) {
3543 /*
3544 * To use VMXON (and later other VMX instructions), a guest
3545 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3546 * So basically the check on whether to allow nested VMX
3547 * is here.
3548 */
3549 if (!nested_vmx_allowed(vcpu))
3550 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003551 }
3552 if (to_vmx(vcpu)->nested.vmxon &&
3553 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003554 return 1;
3555
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003556 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02003557 if (enable_ept) {
3558 if (!is_paging(vcpu)) {
3559 hw_cr4 &= ~X86_CR4_PAE;
3560 hw_cr4 |= X86_CR4_PSE;
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003561 /*
Feng Wue1e746b2014-04-01 17:46:35 +08003562 * SMEP/SMAP is disabled if CPU is in non-paging mode
3563 * in hardware. However KVM always uses paging mode to
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003564 * emulate guest non-paging mode with TDP.
Feng Wue1e746b2014-04-01 17:46:35 +08003565 * To emulate this behavior, SMEP/SMAP needs to be
3566 * manually disabled when guest switches to non-paging
3567 * mode.
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003568 */
Feng Wue1e746b2014-04-01 17:46:35 +08003569 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP);
Avi Kivitybc230082009-12-08 12:14:42 +02003570 } else if (!(cr4 & X86_CR4_PAE)) {
3571 hw_cr4 &= ~X86_CR4_PAE;
3572 }
3573 }
Sheng Yang14394422008-04-28 12:24:45 +08003574
3575 vmcs_writel(CR4_READ_SHADOW, cr4);
3576 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003577 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003578}
3579
Avi Kivity6aa8b732006-12-10 02:21:36 -08003580static void vmx_get_segment(struct kvm_vcpu *vcpu,
3581 struct kvm_segment *var, int seg)
3582{
Avi Kivitya9179492011-01-03 14:28:52 +02003583 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003584 u32 ar;
3585
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003586 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003587 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003588 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003589 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003590 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003591 var->base = vmx_read_guest_seg_base(vmx, seg);
3592 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3593 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003594 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003595 var->base = vmx_read_guest_seg_base(vmx, seg);
3596 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3597 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3598 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003599 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003600 var->type = ar & 15;
3601 var->s = (ar >> 4) & 1;
3602 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003603 /*
3604 * Some userspaces do not preserve unusable property. Since usable
3605 * segment has to be present according to VMX spec we can use present
3606 * property to amend userspace bug by making unusable segment always
3607 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3608 * segment as unusable.
3609 */
3610 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003611 var->avl = (ar >> 12) & 1;
3612 var->l = (ar >> 13) & 1;
3613 var->db = (ar >> 14) & 1;
3614 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003615}
3616
Avi Kivitya9179492011-01-03 14:28:52 +02003617static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3618{
Avi Kivitya9179492011-01-03 14:28:52 +02003619 struct kvm_segment s;
3620
3621 if (to_vmx(vcpu)->rmode.vm86_active) {
3622 vmx_get_segment(vcpu, &s, seg);
3623 return s.base;
3624 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003625 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003626}
3627
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003628static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003629{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003630 struct vcpu_vmx *vmx = to_vmx(vcpu);
3631
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003632 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003633 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003634 else {
3635 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
3636 return AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02003637 }
Avi Kivity69c73022011-03-07 15:26:44 +02003638}
3639
Avi Kivity653e3102007-05-07 10:55:37 +03003640static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003641{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003642 u32 ar;
3643
Avi Kivityf0495f92012-06-07 17:06:10 +03003644 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003645 ar = 1 << 16;
3646 else {
3647 ar = var->type & 15;
3648 ar |= (var->s & 1) << 4;
3649 ar |= (var->dpl & 3) << 5;
3650 ar |= (var->present & 1) << 7;
3651 ar |= (var->avl & 1) << 12;
3652 ar |= (var->l & 1) << 13;
3653 ar |= (var->db & 1) << 14;
3654 ar |= (var->g & 1) << 15;
3655 }
Avi Kivity653e3102007-05-07 10:55:37 +03003656
3657 return ar;
3658}
3659
3660static void vmx_set_segment(struct kvm_vcpu *vcpu,
3661 struct kvm_segment *var, int seg)
3662{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003663 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003664 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003665
Avi Kivity2fb92db2011-04-27 19:42:18 +03003666 vmx_segment_cache_clear(vmx);
3667
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003668 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3669 vmx->rmode.segs[seg] = *var;
3670 if (seg == VCPU_SREG_TR)
3671 vmcs_write16(sf->selector, var->selector);
3672 else if (var->s)
3673 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003674 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003675 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003676
Avi Kivity653e3102007-05-07 10:55:37 +03003677 vmcs_writel(sf->base, var->base);
3678 vmcs_write32(sf->limit, var->limit);
3679 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003680
3681 /*
3682 * Fix the "Accessed" bit in AR field of segment registers for older
3683 * qemu binaries.
3684 * IA32 arch specifies that at the time of processor reset the
3685 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003686 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003687 * state vmexit when "unrestricted guest" mode is turned on.
3688 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3689 * tree. Newer qemu binaries with that qemu fix would not need this
3690 * kvm hack.
3691 */
3692 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003693 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003694
Gleb Natapovf924d662012-12-12 19:10:55 +02003695 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003696
3697out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01003698 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003699}
3700
Avi Kivity6aa8b732006-12-10 02:21:36 -08003701static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3702{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003703 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003704
3705 *db = (ar >> 14) & 1;
3706 *l = (ar >> 13) & 1;
3707}
3708
Gleb Natapov89a27f42010-02-16 10:51:48 +02003709static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003710{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003711 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3712 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003713}
3714
Gleb Natapov89a27f42010-02-16 10:51:48 +02003715static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003716{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003717 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3718 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003719}
3720
Gleb Natapov89a27f42010-02-16 10:51:48 +02003721static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003722{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003723 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3724 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003725}
3726
Gleb Natapov89a27f42010-02-16 10:51:48 +02003727static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003728{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003729 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3730 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003731}
3732
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003733static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3734{
3735 struct kvm_segment var;
3736 u32 ar;
3737
3738 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003739 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003740 if (seg == VCPU_SREG_CS)
3741 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003742 ar = vmx_segment_access_rights(&var);
3743
3744 if (var.base != (var.selector << 4))
3745 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003746 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003747 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003748 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003749 return false;
3750
3751 return true;
3752}
3753
3754static bool code_segment_valid(struct kvm_vcpu *vcpu)
3755{
3756 struct kvm_segment cs;
3757 unsigned int cs_rpl;
3758
3759 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3760 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
3761
Avi Kivity1872a3f2009-01-04 23:26:52 +02003762 if (cs.unusable)
3763 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003764 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
3765 return false;
3766 if (!cs.s)
3767 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003768 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003769 if (cs.dpl > cs_rpl)
3770 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003771 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003772 if (cs.dpl != cs_rpl)
3773 return false;
3774 }
3775 if (!cs.present)
3776 return false;
3777
3778 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3779 return true;
3780}
3781
3782static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3783{
3784 struct kvm_segment ss;
3785 unsigned int ss_rpl;
3786
3787 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3788 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
3789
Avi Kivity1872a3f2009-01-04 23:26:52 +02003790 if (ss.unusable)
3791 return true;
3792 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003793 return false;
3794 if (!ss.s)
3795 return false;
3796 if (ss.dpl != ss_rpl) /* DPL != RPL */
3797 return false;
3798 if (!ss.present)
3799 return false;
3800
3801 return true;
3802}
3803
3804static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3805{
3806 struct kvm_segment var;
3807 unsigned int rpl;
3808
3809 vmx_get_segment(vcpu, &var, seg);
3810 rpl = var.selector & SELECTOR_RPL_MASK;
3811
Avi Kivity1872a3f2009-01-04 23:26:52 +02003812 if (var.unusable)
3813 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003814 if (!var.s)
3815 return false;
3816 if (!var.present)
3817 return false;
3818 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
3819 if (var.dpl < rpl) /* DPL < RPL */
3820 return false;
3821 }
3822
3823 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3824 * rights flags
3825 */
3826 return true;
3827}
3828
3829static bool tr_valid(struct kvm_vcpu *vcpu)
3830{
3831 struct kvm_segment tr;
3832
3833 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3834
Avi Kivity1872a3f2009-01-04 23:26:52 +02003835 if (tr.unusable)
3836 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003837 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3838 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003839 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003840 return false;
3841 if (!tr.present)
3842 return false;
3843
3844 return true;
3845}
3846
3847static bool ldtr_valid(struct kvm_vcpu *vcpu)
3848{
3849 struct kvm_segment ldtr;
3850
3851 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3852
Avi Kivity1872a3f2009-01-04 23:26:52 +02003853 if (ldtr.unusable)
3854 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003855 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3856 return false;
3857 if (ldtr.type != 2)
3858 return false;
3859 if (!ldtr.present)
3860 return false;
3861
3862 return true;
3863}
3864
3865static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3866{
3867 struct kvm_segment cs, ss;
3868
3869 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3870 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3871
3872 return ((cs.selector & SELECTOR_RPL_MASK) ==
3873 (ss.selector & SELECTOR_RPL_MASK));
3874}
3875
3876/*
3877 * Check if guest state is valid. Returns true if valid, false if
3878 * not.
3879 * We assume that registers are always usable
3880 */
3881static bool guest_state_valid(struct kvm_vcpu *vcpu)
3882{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02003883 if (enable_unrestricted_guest)
3884 return true;
3885
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003886 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003887 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003888 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3889 return false;
3890 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3891 return false;
3892 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3893 return false;
3894 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3895 return false;
3896 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3897 return false;
3898 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3899 return false;
3900 } else {
3901 /* protected mode guest state checks */
3902 if (!cs_ss_rpl_check(vcpu))
3903 return false;
3904 if (!code_segment_valid(vcpu))
3905 return false;
3906 if (!stack_segment_valid(vcpu))
3907 return false;
3908 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3909 return false;
3910 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3911 return false;
3912 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3913 return false;
3914 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3915 return false;
3916 if (!tr_valid(vcpu))
3917 return false;
3918 if (!ldtr_valid(vcpu))
3919 return false;
3920 }
3921 /* TODO:
3922 * - Add checks on RIP
3923 * - Add checks on RFLAGS
3924 */
3925
3926 return true;
3927}
3928
Mike Dayd77c26f2007-10-08 09:02:08 -04003929static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003930{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003931 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003932 u16 data = 0;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003933 int idx, r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003934
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003935 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003936 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003937 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3938 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003939 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003940 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003941 r = kvm_write_guest_page(kvm, fn++, &data,
3942 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003943 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003944 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003945 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3946 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003947 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003948 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3949 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003950 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003951 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003952 r = kvm_write_guest_page(kvm, fn, &data,
3953 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3954 sizeof(u8));
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003955out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003956 srcu_read_unlock(&kvm->srcu, idx);
Paolo Bonzini1f755a82014-09-16 13:37:40 +02003957 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003958}
3959
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003960static int init_rmode_identity_map(struct kvm *kvm)
3961{
Tang Chenf51770e2014-09-16 18:41:59 +08003962 int i, idx, r = 0;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003963 pfn_t identity_map_pfn;
3964 u32 tmp;
3965
Avi Kivity089d0342009-03-23 18:26:32 +02003966 if (!enable_ept)
Tang Chenf51770e2014-09-16 18:41:59 +08003967 return 0;
Tang Chena255d472014-09-16 18:41:58 +08003968
3969 /* Protect kvm->arch.ept_identity_pagetable_done. */
3970 mutex_lock(&kvm->slots_lock);
3971
Tang Chenf51770e2014-09-16 18:41:59 +08003972 if (likely(kvm->arch.ept_identity_pagetable_done))
Tang Chena255d472014-09-16 18:41:58 +08003973 goto out2;
Tang Chena255d472014-09-16 18:41:58 +08003974
Sheng Yangb927a3c2009-07-21 10:42:48 +08003975 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Tang Chena255d472014-09-16 18:41:58 +08003976
3977 r = alloc_identity_pagetable(kvm);
Tang Chenf51770e2014-09-16 18:41:59 +08003978 if (r < 0)
Tang Chena255d472014-09-16 18:41:58 +08003979 goto out2;
3980
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003981 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003982 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3983 if (r < 0)
3984 goto out;
3985 /* Set up identity-mapping pagetable for EPT in real mode */
3986 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3987 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3988 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3989 r = kvm_write_guest_page(kvm, identity_map_pfn,
3990 &tmp, i * sizeof(tmp), sizeof(tmp));
3991 if (r < 0)
3992 goto out;
3993 }
3994 kvm->arch.ept_identity_pagetable_done = true;
Tang Chenf51770e2014-09-16 18:41:59 +08003995
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003996out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003997 srcu_read_unlock(&kvm->srcu, idx);
Tang Chena255d472014-09-16 18:41:58 +08003998
3999out2:
4000 mutex_unlock(&kvm->slots_lock);
Tang Chenf51770e2014-09-16 18:41:59 +08004001 return r;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004002}
4003
Avi Kivity6aa8b732006-12-10 02:21:36 -08004004static void seg_setup(int seg)
4005{
Mathias Krause772e0312012-08-30 01:30:19 +02004006 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004007 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004008
4009 vmcs_write16(sf->selector, 0);
4010 vmcs_writel(sf->base, 0);
4011 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004012 ar = 0x93;
4013 if (seg == VCPU_SREG_CS)
4014 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07004015
4016 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004017}
4018
Sheng Yangf78e0e22007-10-29 09:40:42 +08004019static int alloc_apic_access_page(struct kvm *kvm)
4020{
Xiao Guangrong44841412012-09-07 14:14:20 +08004021 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004022 struct kvm_userspace_memory_region kvm_userspace_mem;
4023 int r = 0;
4024
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004025 mutex_lock(&kvm->slots_lock);
Tang Chenc24ae0d2014-09-24 15:57:58 +08004026 if (kvm->arch.apic_access_page_done)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004027 goto out;
4028 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
4029 kvm_userspace_mem.flags = 0;
Tang Chen73a6d942014-09-11 13:38:00 +08004030 kvm_userspace_mem.guest_phys_addr = APIC_DEFAULT_PHYS_BASE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004031 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09004032 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004033 if (r)
4034 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004035
Tang Chen73a6d942014-09-11 13:38:00 +08004036 page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
Xiao Guangrong44841412012-09-07 14:14:20 +08004037 if (is_error_page(page)) {
4038 r = -EFAULT;
4039 goto out;
4040 }
4041
Tang Chenc24ae0d2014-09-24 15:57:58 +08004042 /*
4043 * Do not pin the page in memory, so that memory hot-unplug
4044 * is able to migrate it.
4045 */
4046 put_page(page);
4047 kvm->arch.apic_access_page_done = true;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004048out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004049 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004050 return r;
4051}
4052
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004053static int alloc_identity_pagetable(struct kvm *kvm)
4054{
Tang Chena255d472014-09-16 18:41:58 +08004055 /* Called with kvm->slots_lock held. */
4056
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004057 struct kvm_userspace_memory_region kvm_userspace_mem;
4058 int r = 0;
4059
Tang Chena255d472014-09-16 18:41:58 +08004060 BUG_ON(kvm->arch.ept_identity_pagetable_done);
4061
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004062 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
4063 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004064 kvm_userspace_mem.guest_phys_addr =
4065 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004066 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09004067 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004068
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004069 return r;
4070}
4071
Sheng Yang2384d2b2008-01-17 15:14:33 +08004072static void allocate_vpid(struct vcpu_vmx *vmx)
4073{
4074 int vpid;
4075
4076 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02004077 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004078 return;
4079 spin_lock(&vmx_vpid_lock);
4080 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
4081 if (vpid < VMX_NR_VPIDS) {
4082 vmx->vpid = vpid;
4083 __set_bit(vpid, vmx_vpid_bitmap);
4084 }
4085 spin_unlock(&vmx_vpid_lock);
4086}
4087
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004088static void free_vpid(struct vcpu_vmx *vmx)
4089{
4090 if (!enable_vpid)
4091 return;
4092 spin_lock(&vmx_vpid_lock);
4093 if (vmx->vpid != 0)
4094 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
4095 spin_unlock(&vmx_vpid_lock);
4096}
4097
Yang Zhang8d146952013-01-25 10:18:50 +08004098#define MSR_TYPE_R 1
4099#define MSR_TYPE_W 2
4100static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4101 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004102{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004103 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004104
4105 if (!cpu_has_vmx_msr_bitmap())
4106 return;
4107
4108 /*
4109 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4110 * have the write-low and read-high bitmap offsets the wrong way round.
4111 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4112 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004113 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004114 if (type & MSR_TYPE_R)
4115 /* read-low */
4116 __clear_bit(msr, msr_bitmap + 0x000 / f);
4117
4118 if (type & MSR_TYPE_W)
4119 /* write-low */
4120 __clear_bit(msr, msr_bitmap + 0x800 / f);
4121
Sheng Yang25c5f222008-03-28 13:18:56 +08004122 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4123 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004124 if (type & MSR_TYPE_R)
4125 /* read-high */
4126 __clear_bit(msr, msr_bitmap + 0x400 / f);
4127
4128 if (type & MSR_TYPE_W)
4129 /* write-high */
4130 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4131
4132 }
4133}
4134
4135static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4136 u32 msr, int type)
4137{
4138 int f = sizeof(unsigned long);
4139
4140 if (!cpu_has_vmx_msr_bitmap())
4141 return;
4142
4143 /*
4144 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4145 * have the write-low and read-high bitmap offsets the wrong way round.
4146 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4147 */
4148 if (msr <= 0x1fff) {
4149 if (type & MSR_TYPE_R)
4150 /* read-low */
4151 __set_bit(msr, msr_bitmap + 0x000 / f);
4152
4153 if (type & MSR_TYPE_W)
4154 /* write-low */
4155 __set_bit(msr, msr_bitmap + 0x800 / f);
4156
4157 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4158 msr &= 0x1fff;
4159 if (type & MSR_TYPE_R)
4160 /* read-high */
4161 __set_bit(msr, msr_bitmap + 0x400 / f);
4162
4163 if (type & MSR_TYPE_W)
4164 /* write-high */
4165 __set_bit(msr, msr_bitmap + 0xc00 / f);
4166
Sheng Yang25c5f222008-03-28 13:18:56 +08004167 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004168}
4169
Avi Kivity58972972009-02-24 22:26:47 +02004170static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4171{
4172 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004173 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4174 msr, MSR_TYPE_R | MSR_TYPE_W);
4175 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4176 msr, MSR_TYPE_R | MSR_TYPE_W);
4177}
4178
4179static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
4180{
4181 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4182 msr, MSR_TYPE_R);
4183 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4184 msr, MSR_TYPE_R);
4185}
4186
4187static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
4188{
4189 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4190 msr, MSR_TYPE_R);
4191 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4192 msr, MSR_TYPE_R);
4193}
4194
4195static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
4196{
4197 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4198 msr, MSR_TYPE_W);
4199 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4200 msr, MSR_TYPE_W);
Avi Kivity58972972009-02-24 22:26:47 +02004201}
4202
Yang Zhang01e439b2013-04-11 19:25:12 +08004203static int vmx_vm_has_apicv(struct kvm *kvm)
4204{
4205 return enable_apicv && irqchip_in_kernel(kvm);
4206}
4207
Avi Kivity6aa8b732006-12-10 02:21:36 -08004208/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004209 * Send interrupt to vcpu via posted interrupt way.
4210 * 1. If target vcpu is running(non-root mode), send posted interrupt
4211 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4212 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4213 * interrupt from PIR in next vmentry.
4214 */
4215static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4216{
4217 struct vcpu_vmx *vmx = to_vmx(vcpu);
4218 int r;
4219
4220 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4221 return;
4222
4223 r = pi_test_and_set_on(&vmx->pi_desc);
4224 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang, Yang Z6ffbbbb2013-04-17 23:11:54 -03004225#ifdef CONFIG_SMP
Yang Zhanga20ed542013-04-11 19:25:15 +08004226 if (!r && (vcpu->mode == IN_GUEST_MODE))
4227 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4228 POSTED_INTR_VECTOR);
4229 else
Zhang, Yang Z6ffbbbb2013-04-17 23:11:54 -03004230#endif
Yang Zhanga20ed542013-04-11 19:25:15 +08004231 kvm_vcpu_kick(vcpu);
4232}
4233
4234static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4235{
4236 struct vcpu_vmx *vmx = to_vmx(vcpu);
4237
4238 if (!pi_test_and_clear_on(&vmx->pi_desc))
4239 return;
4240
4241 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4242}
4243
4244static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu)
4245{
4246 return;
4247}
4248
Avi Kivity6aa8b732006-12-10 02:21:36 -08004249/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004250 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4251 * will not change in the lifetime of the guest.
4252 * Note that host-state that does change is set elsewhere. E.g., host-state
4253 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4254 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004255static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004256{
4257 u32 low32, high32;
4258 unsigned long tmpl;
4259 struct desc_ptr dt;
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004260 unsigned long cr4;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004261
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07004262 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004263 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4264
Andy Lutomirskid974baa2014-10-08 09:02:13 -07004265 /* Save the most likely value for this task's CR4 in the VMCS. */
4266 cr4 = read_cr4();
4267 vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
4268 vmx->host_state.vmcs_host_cr4 = cr4;
4269
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004270 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004271#ifdef CONFIG_X86_64
4272 /*
4273 * Load null selectors, so we can avoid reloading them in
4274 * __vmx_load_host_state(), in case userspace uses the null selectors
4275 * too (the expected case).
4276 */
4277 vmcs_write16(HOST_DS_SELECTOR, 0);
4278 vmcs_write16(HOST_ES_SELECTOR, 0);
4279#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004280 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4281 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004282#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004283 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4284 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4285
4286 native_store_idt(&dt);
4287 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004288 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004289
Avi Kivity83287ea422012-09-16 15:10:57 +03004290 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004291
4292 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4293 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4294 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4295 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4296
4297 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4298 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4299 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4300 }
4301}
4302
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004303static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4304{
4305 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4306 if (enable_ept)
4307 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004308 if (is_guest_mode(&vmx->vcpu))
4309 vmx->vcpu.arch.cr4_guest_owned_bits &=
4310 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004311 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4312}
4313
Yang Zhang01e439b2013-04-11 19:25:12 +08004314static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4315{
4316 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4317
4318 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4319 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
4320 return pin_based_exec_ctrl;
4321}
4322
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004323static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4324{
4325 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01004326
4327 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4328 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4329
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004330 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
4331 exec_control &= ~CPU_BASED_TPR_SHADOW;
4332#ifdef CONFIG_X86_64
4333 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4334 CPU_BASED_CR8_LOAD_EXITING;
4335#endif
4336 }
4337 if (!enable_ept)
4338 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4339 CPU_BASED_CR3_LOAD_EXITING |
4340 CPU_BASED_INVLPG_EXITING;
4341 return exec_control;
4342}
4343
4344static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4345{
4346 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
4347 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4348 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4349 if (vmx->vpid == 0)
4350 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4351 if (!enable_ept) {
4352 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4353 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00004354 /* Enable INVPCID for non-ept guests may cause performance regression. */
4355 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004356 }
4357 if (!enable_unrestricted_guest)
4358 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4359 if (!ple_gap)
4360 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Yang Zhangc7c9c562013-01-25 10:18:51 +08004361 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4362 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4363 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004364 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03004365 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4366 (handle_vmptrld).
4367 We can NOT enable shadow_vmcs here because we don't have yet
4368 a current VMCS12
4369 */
4370 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Kai Huang843e4332015-01-28 10:54:28 +08004371 /* PML is enabled/disabled in creating/destorying vcpu */
4372 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
4373
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004374 return exec_control;
4375}
4376
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004377static void ept_set_mmio_spte_mask(void)
4378{
4379 /*
4380 * EPT Misconfigurations can be generated if the value of bits 2:0
4381 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08004382 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004383 * spte.
4384 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08004385 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004386}
4387
Wanpeng Lif53cd632014-12-02 19:14:58 +08004388#define VMX_XSS_EXIT_BITMAP 0
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004389/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004390 * Sets up the vmcs for emulated real mode.
4391 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004392static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004393{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004394#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004395 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004396#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004398
Avi Kivity6aa8b732006-12-10 02:21:36 -08004399 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004400 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4401 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004402
Abel Gordon4607c2d2013-04-18 14:35:55 +03004403 if (enable_shadow_vmcs) {
4404 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
4405 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
4406 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004407 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02004408 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08004409
Avi Kivity6aa8b732006-12-10 02:21:36 -08004410 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4411
Avi Kivity6aa8b732006-12-10 02:21:36 -08004412 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08004413 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004414
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004415 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004416
Sheng Yang83ff3b92007-11-21 14:33:25 +08004417 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004418 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4419 vmx_secondary_exec_control(vmx));
Sheng Yang83ff3b92007-11-21 14:33:25 +08004420 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004421
Yang Zhang01e439b2013-04-11 19:25:12 +08004422 if (vmx_vm_has_apicv(vmx->vcpu.kvm)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004423 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4424 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4425 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4426 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4427
4428 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004429
4430 vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
4431 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004432 }
4433
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004434 if (ple_gap) {
4435 vmcs_write32(PLE_GAP, ple_gap);
Radim Krčmářa7653ec2014-08-21 18:08:07 +02004436 vmx->ple_window = ple_window;
4437 vmx->ple_window_dirty = true;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004438 }
4439
Xiao Guangrongc3707952011-07-12 03:28:04 +08004440 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4441 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004442 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4443
Avi Kivity9581d442010-10-19 16:46:55 +02004444 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4445 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004446 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004447#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004448 rdmsrl(MSR_FS_BASE, a);
4449 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
4450 rdmsrl(MSR_GS_BASE, a);
4451 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
4452#else
4453 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4454 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
4455#endif
4456
Eddie Dong2cc51562007-05-21 07:28:09 +03004457 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4458 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004459 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03004460 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004461 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004462
Sheng Yang468d4722008-10-09 16:01:55 +08004463 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004464 u32 msr_low, msr_high;
4465 u64 host_pat;
Sheng Yang468d4722008-10-09 16:01:55 +08004466 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
4467 host_pat = msr_low | ((u64) msr_high << 32);
4468 /* Write the default value follow host pat */
4469 vmcs_write64(GUEST_IA32_PAT, host_pat);
4470 /* Keep arch.pat sync with GUEST_IA32_PAT */
4471 vmx->vcpu.arch.pat = host_pat;
4472 }
4473
Paolo Bonzini03916db2014-07-24 14:21:57 +02004474 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004475 u32 index = vmx_msr_index[i];
4476 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004477 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004478
4479 if (rdmsr_safe(index, &data_low, &data_high) < 0)
4480 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08004481 if (wrmsr_safe(index, data_low, data_high) < 0)
4482 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03004483 vmx->guest_msrs[j].index = i;
4484 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02004485 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004486 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004487 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004488
Gleb Natapov2961e8762013-11-25 15:37:13 +02004489
4490 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004491
4492 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02004493 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004494
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004495 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004496 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004497
Wanpeng Lif53cd632014-12-02 19:14:58 +08004498 if (vmx_xsaves_supported())
4499 vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
4500
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004501 return 0;
4502}
4503
Jan Kiszka57f252f2013-03-12 10:20:24 +01004504static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004505{
4506 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004507 struct msr_data apic_base_msr;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004508
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004509 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004510
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004511 vmx->soft_vnmi_blocked = 0;
4512
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004513 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004514 kvm_set_cr8(&vmx->vcpu, 0);
Tang Chen73a6d942014-09-11 13:38:00 +08004515 apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004516 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Jan Kiszka58cb6282014-01-24 16:48:44 +01004517 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4518 apic_base_msr.host_initiated = true;
4519 kvm_set_apic_base(&vmx->vcpu, &apic_base_msr);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004520
Avi Kivity2fb92db2011-04-27 19:42:18 +03004521 vmx_segment_cache_clear(vmx);
4522
Avi Kivity5706be02008-08-20 15:07:31 +03004523 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004524 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzini04b66832013-03-19 16:30:26 +01004525 vmcs_write32(GUEST_CS_BASE, 0xffff0000);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004526
4527 seg_setup(VCPU_SREG_DS);
4528 seg_setup(VCPU_SREG_ES);
4529 seg_setup(VCPU_SREG_FS);
4530 seg_setup(VCPU_SREG_GS);
4531 seg_setup(VCPU_SREG_SS);
4532
4533 vmcs_write16(GUEST_TR_SELECTOR, 0);
4534 vmcs_writel(GUEST_TR_BASE, 0);
4535 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4536 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4537
4538 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4539 vmcs_writel(GUEST_LDTR_BASE, 0);
4540 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4541 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4542
4543 vmcs_write32(GUEST_SYSENTER_CS, 0);
4544 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4545 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4546
4547 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01004548 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004549
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004550 vmcs_writel(GUEST_GDTR_BASE, 0);
4551 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4552
4553 vmcs_writel(GUEST_IDTR_BASE, 0);
4554 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4555
Anthony Liguori443381a2010-12-06 10:53:38 -06004556 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004557 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
4558 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
4559
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004560 /* Special registers */
4561 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4562
4563 setup_msrs(vmx);
4564
Avi Kivity6aa8b732006-12-10 02:21:36 -08004565 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4566
Sheng Yangf78e0e22007-10-29 09:40:42 +08004567 if (cpu_has_vmx_tpr_shadow()) {
4568 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
4569 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
4570 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09004571 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004572 vmcs_write32(TPR_THRESHOLD, 0);
4573 }
4574
Paolo Bonzinia73896c2014-11-02 07:54:30 +01004575 kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004576
Yang Zhang01e439b2013-04-11 19:25:12 +08004577 if (vmx_vm_has_apicv(vcpu->kvm))
4578 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
4579
Sheng Yang2384d2b2008-01-17 15:14:33 +08004580 if (vmx->vpid != 0)
4581 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4582
Eduardo Habkostfa400522009-10-24 02:49:58 -02004583 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004584 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004585 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004586 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004587 vmx_fpu_activate(&vmx->vcpu);
4588 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004589
Gui Jianfengb9d762f2010-06-07 10:32:29 +08004590 vpid_sync_context(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004591}
4592
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004593/*
4594 * In nested virtualization, check if L1 asked to exit on external interrupts.
4595 * For most existing hypervisors, this will always return true.
4596 */
4597static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4598{
4599 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4600 PIN_BASED_EXT_INTR_MASK;
4601}
4602
Bandan Das77b0f5d2014-04-19 18:17:45 -04004603/*
4604 * In nested virtualization, check if L1 has set
4605 * VM_EXIT_ACK_INTR_ON_EXIT
4606 */
4607static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
4608{
4609 return get_vmcs12(vcpu)->vm_exit_controls &
4610 VM_EXIT_ACK_INTR_ON_EXIT;
4611}
4612
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004613static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
4614{
4615 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4616 PIN_BASED_NMI_EXITING;
4617}
4618
Jan Kiszkac9a79532014-03-07 20:03:15 +01004619static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004620{
4621 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02004622
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004623 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4624 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
4625 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4626}
4627
Jan Kiszkac9a79532014-03-07 20:03:15 +01004628static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004629{
4630 u32 cpu_based_vm_exec_control;
4631
Jan Kiszkac9a79532014-03-07 20:03:15 +01004632 if (!cpu_has_virtual_nmis() ||
4633 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
4634 enable_irq_window(vcpu);
4635 return;
4636 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004637
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004638 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4639 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
4640 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4641}
4642
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004643static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004644{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004645 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004646 uint32_t intr;
4647 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004648
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004649 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004650
Avi Kivityfa89a812008-09-01 15:57:51 +03004651 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004652 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004653 int inc_eip = 0;
4654 if (vcpu->arch.interrupt.soft)
4655 inc_eip = vcpu->arch.event_exit_inst_len;
4656 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004657 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004658 return;
4659 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004660 intr = irq | INTR_INFO_VALID_MASK;
4661 if (vcpu->arch.interrupt.soft) {
4662 intr |= INTR_TYPE_SOFT_INTR;
4663 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4664 vmx->vcpu.arch.event_exit_inst_len);
4665 } else
4666 intr |= INTR_TYPE_EXT_INTR;
4667 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03004668}
4669
Sheng Yangf08864b2008-05-15 18:23:25 +08004670static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4671{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004672 struct vcpu_vmx *vmx = to_vmx(vcpu);
4673
Nadav Har'El0b6ac342011-05-25 23:13:36 +03004674 if (is_guest_mode(vcpu))
4675 return;
4676
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004677 if (!cpu_has_virtual_nmis()) {
4678 /*
4679 * Tracking the NMI-blocked state in software is built upon
4680 * finding the next open IRQ window. This, in turn, depends on
4681 * well-behaving guests: They have to keep IRQs disabled at
4682 * least as long as the NMI handler runs. Otherwise we may
4683 * cause NMI nesting, maybe breaking the guest. But as this is
4684 * highly unlikely, we can live with the residual risk.
4685 */
4686 vmx->soft_vnmi_blocked = 1;
4687 vmx->vnmi_blocked_time = 0;
4688 }
4689
Jan Kiszka487b3912008-09-26 09:30:56 +02004690 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02004691 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004692 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004693 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004694 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004695 return;
4696 }
Sheng Yangf08864b2008-05-15 18:23:25 +08004697 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4698 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08004699}
4700
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004701static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
4702{
4703 if (!cpu_has_virtual_nmis())
4704 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02004705 if (to_vmx(vcpu)->nmi_known_unmasked)
4706 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03004707 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004708}
4709
4710static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4711{
4712 struct vcpu_vmx *vmx = to_vmx(vcpu);
4713
4714 if (!cpu_has_virtual_nmis()) {
4715 if (vmx->soft_vnmi_blocked != masked) {
4716 vmx->soft_vnmi_blocked = masked;
4717 vmx->vnmi_blocked_time = 0;
4718 }
4719 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02004720 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004721 if (masked)
4722 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4723 GUEST_INTR_STATE_NMI);
4724 else
4725 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4726 GUEST_INTR_STATE_NMI);
4727 }
4728}
4729
Jan Kiszka2505dc92013-04-14 12:12:47 +02004730static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
4731{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004732 if (to_vmx(vcpu)->nested.nested_run_pending)
4733 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004734
Jan Kiszka2505dc92013-04-14 12:12:47 +02004735 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
4736 return 0;
4737
4738 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4739 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4740 | GUEST_INTR_STATE_NMI));
4741}
4742
Gleb Natapov78646122009-03-23 12:12:11 +02004743static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4744{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004745 return (!to_vmx(vcpu)->nested.nested_run_pending &&
4746 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03004747 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4748 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02004749}
4750
Izik Eiduscbc94022007-10-25 00:29:55 +02004751static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4752{
4753 int ret;
4754 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08004755 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02004756 .guest_phys_addr = addr,
4757 .memory_size = PAGE_SIZE * 3,
4758 .flags = 0,
4759 };
4760
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09004761 ret = kvm_set_memory_region(kvm, &tss_mem);
Izik Eiduscbc94022007-10-25 00:29:55 +02004762 if (ret)
4763 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004764 kvm->arch.tss_addr = addr;
Paolo Bonzini1f755a82014-09-16 13:37:40 +02004765 return init_rmode_tss(kvm);
Izik Eiduscbc94022007-10-25 00:29:55 +02004766}
4767
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004768static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004769{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004770 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004771 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01004772 /*
4773 * Update instruction length as we may reinject the exception
4774 * from user space while in guest debugging mode.
4775 */
4776 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4777 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004778 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004779 return false;
4780 /* fall through */
4781 case DB_VECTOR:
4782 if (vcpu->guest_debug &
4783 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4784 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004785 /* fall through */
4786 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004787 case OF_VECTOR:
4788 case BR_VECTOR:
4789 case UD_VECTOR:
4790 case DF_VECTOR:
4791 case SS_VECTOR:
4792 case GP_VECTOR:
4793 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004794 return true;
4795 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004796 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004797 return false;
4798}
4799
4800static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4801 int vec, u32 err_code)
4802{
4803 /*
4804 * Instruction with address size override prefix opcode 0x67
4805 * Cause the #SS fault with 0 error code in VM86 mode.
4806 */
4807 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
4808 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
4809 if (vcpu->arch.halt_request) {
4810 vcpu->arch.halt_request = 0;
4811 return kvm_emulate_halt(vcpu);
4812 }
4813 return 1;
4814 }
4815 return 0;
4816 }
4817
4818 /*
4819 * Forward all other exceptions that are valid in real mode.
4820 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4821 * the required debugging infrastructure rework.
4822 */
4823 kvm_queue_exception(vcpu, vec);
4824 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004825}
4826
Andi Kleena0861c02009-06-08 17:37:09 +08004827/*
4828 * Trigger machine check on the host. We assume all the MSRs are already set up
4829 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4830 * We pass a fake environment to the machine check handler because we want
4831 * the guest to be always treated like user space, no matter what context
4832 * it used internally.
4833 */
4834static void kvm_machine_check(void)
4835{
4836#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4837 struct pt_regs regs = {
4838 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4839 .flags = X86_EFLAGS_IF,
4840 };
4841
4842 do_machine_check(&regs, 0);
4843#endif
4844}
4845
Avi Kivity851ba692009-08-24 11:10:17 +03004846static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004847{
4848 /* already handled by vcpu_run */
4849 return 1;
4850}
4851
Avi Kivity851ba692009-08-24 11:10:17 +03004852static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004853{
Avi Kivity1155f762007-11-22 11:30:47 +02004854 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004855 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004856 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004857 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004858 u32 vect_info;
4859 enum emulation_result er;
4860
Avi Kivity1155f762007-11-22 11:30:47 +02004861 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02004862 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004863
Andi Kleena0861c02009-06-08 17:37:09 +08004864 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03004865 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004866
Jan Kiszkae4a41882008-09-26 09:30:46 +02004867 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02004868 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004869
4870 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03004871 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004872 return 1;
4873 }
4874
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004875 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01004876 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004877 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02004878 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004879 return 1;
4880 }
4881
Avi Kivity6aa8b732006-12-10 02:21:36 -08004882 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004883 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004884 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004885
4886 /*
4887 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4888 * MMIO, it is better to report an internal error.
4889 * See the comments in vmx_handle_exit.
4890 */
4891 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4892 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4893 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4894 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
4895 vcpu->run->internal.ndata = 2;
4896 vcpu->run->internal.data[0] = vect_info;
4897 vcpu->run->internal.data[1] = intr_info;
4898 return 0;
4899 }
4900
Avi Kivity6aa8b732006-12-10 02:21:36 -08004901 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08004902 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02004903 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004904 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004905 trace_kvm_page_fault(cr2, error_code);
4906
Gleb Natapov3298b752009-05-11 13:35:46 +03004907 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03004908 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01004909 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004910 }
4911
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004912 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004913
4914 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4915 return handle_rmode_exception(vcpu, ex_no, error_code);
4916
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004917 switch (ex_no) {
4918 case DB_VECTOR:
4919 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4920 if (!(vcpu->guest_debug &
4921 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01004922 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03004923 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01004924 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
4925 skip_emulated_instruction(vcpu);
4926
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004927 kvm_queue_exception(vcpu, DB_VECTOR);
4928 return 1;
4929 }
4930 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4931 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4932 /* fall through */
4933 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01004934 /*
4935 * Update instruction length as we may reinject #BP from
4936 * user space while in guest debugging mode. Reading it for
4937 * #DB as well causes no harm, it is not used in that case.
4938 */
4939 vmx->vcpu.arch.event_exit_inst_len =
4940 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004941 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004942 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004943 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4944 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004945 break;
4946 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004947 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4948 kvm_run->ex.exception = ex_no;
4949 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004950 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004951 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004952 return 0;
4953}
4954
Avi Kivity851ba692009-08-24 11:10:17 +03004955static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004956{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004957 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004958 return 1;
4959}
4960
Avi Kivity851ba692009-08-24 11:10:17 +03004961static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004962{
Avi Kivity851ba692009-08-24 11:10:17 +03004963 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08004964 return 0;
4965}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004966
Avi Kivity851ba692009-08-24 11:10:17 +03004967static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004968{
He, Qingbfdaab02007-09-12 14:18:28 +08004969 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01004970 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004971 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004972
He, Qingbfdaab02007-09-12 14:18:28 +08004973 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02004974 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004975 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004976
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004977 ++vcpu->stat.io_exits;
4978
4979 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01004980 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004981
4982 port = exit_qualification >> 16;
4983 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01004984 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004985
4986 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004987}
4988
Ingo Molnar102d8322007-02-19 14:37:47 +02004989static void
4990vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4991{
4992 /*
4993 * Patch in the VMCALL instruction:
4994 */
4995 hypercall[0] = 0x0f;
4996 hypercall[1] = 0x01;
4997 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004998}
4999
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005000static bool nested_cr0_valid(struct vmcs12 *vmcs12, unsigned long val)
5001{
5002 unsigned long always_on = VMXON_CR0_ALWAYSON;
5003
5004 if (nested_vmx_secondary_ctls_high &
5005 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
5006 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
5007 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
5008 return (val & always_on) == always_on;
5009}
5010
Guo Chao0fa06072012-06-28 15:16:19 +08005011/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005012static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5013{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005014 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005015 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5016 unsigned long orig_val = val;
5017
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005018 /*
5019 * We get here when L2 changed cr0 in a way that did not change
5020 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005021 * but did change L0 shadowed bits. So we first calculate the
5022 * effective cr0 value that L1 would like to write into the
5023 * hardware. It consists of the L2-owned bits from the new
5024 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005025 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005026 val = (val & ~vmcs12->cr0_guest_host_mask) |
5027 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5028
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02005029 if (!nested_cr0_valid(vmcs12, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005030 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005031
5032 if (kvm_set_cr0(vcpu, val))
5033 return 1;
5034 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005035 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005036 } else {
5037 if (to_vmx(vcpu)->nested.vmxon &&
5038 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5039 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005040 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005041 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005042}
5043
5044static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5045{
5046 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005047 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5048 unsigned long orig_val = val;
5049
5050 /* analogously to handle_set_cr0 */
5051 val = (val & ~vmcs12->cr4_guest_host_mask) |
5052 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5053 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005054 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005055 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005056 return 0;
5057 } else
5058 return kvm_set_cr4(vcpu, val);
5059}
5060
5061/* called to set cr0 as approriate for clts instruction exit. */
5062static void handle_clts(struct kvm_vcpu *vcpu)
5063{
5064 if (is_guest_mode(vcpu)) {
5065 /*
5066 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5067 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5068 * just pretend it's off (also in arch.cr0 for fpu_activate).
5069 */
5070 vmcs_writel(CR0_READ_SHADOW,
5071 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5072 vcpu->arch.cr0 &= ~X86_CR0_TS;
5073 } else
5074 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5075}
5076
Avi Kivity851ba692009-08-24 11:10:17 +03005077static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005078{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005079 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005080 int cr;
5081 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005082 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005083
He, Qingbfdaab02007-09-12 14:18:28 +08005084 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005085 cr = exit_qualification & 15;
5086 reg = (exit_qualification >> 8) & 15;
5087 switch ((exit_qualification >> 4) & 3) {
5088 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005089 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005090 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005091 switch (cr) {
5092 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005093 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005094 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005095 return 1;
5096 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005097 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005098 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005099 return 1;
5100 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005101 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005102 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005103 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005104 case 8: {
5105 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005106 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005107 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005108 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005109 if (irqchip_in_kernel(vcpu->kvm))
5110 return 1;
5111 if (cr8_prev <= cr8)
5112 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005113 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005114 return 0;
5115 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005116 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005117 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005118 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005119 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005120 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005121 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005122 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005123 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005124 case 1: /*mov from cr*/
5125 switch (cr) {
5126 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005127 val = kvm_read_cr3(vcpu);
5128 kvm_register_write(vcpu, reg, val);
5129 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005130 skip_emulated_instruction(vcpu);
5131 return 1;
5132 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005133 val = kvm_get_cr8(vcpu);
5134 kvm_register_write(vcpu, reg, val);
5135 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005136 skip_emulated_instruction(vcpu);
5137 return 1;
5138 }
5139 break;
5140 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005141 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005142 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005143 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005144
5145 skip_emulated_instruction(vcpu);
5146 return 1;
5147 default:
5148 break;
5149 }
Avi Kivity851ba692009-08-24 11:10:17 +03005150 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005151 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005152 (int)(exit_qualification >> 4) & 3, cr);
5153 return 0;
5154}
5155
Avi Kivity851ba692009-08-24 11:10:17 +03005156static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005157{
He, Qingbfdaab02007-09-12 14:18:28 +08005158 unsigned long exit_qualification;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005159 int dr, dr7, reg;
5160
5161 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5162 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5163
5164 /* First, if DR does not exist, trigger UD */
5165 if (!kvm_require_dr(vcpu, dr))
5166 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005167
Jan Kiszkaf2483412010-01-20 18:20:20 +01005168 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005169 if (!kvm_require_cpl(vcpu, 0))
5170 return 1;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005171 dr7 = vmcs_readl(GUEST_DR7);
5172 if (dr7 & DR7_GD) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005173 /*
5174 * As the vm-exit takes precedence over the debug trap, we
5175 * need to emulate the latter, either for the host or the
5176 * guest debugging itself.
5177 */
5178 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005179 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
Nadav Amit16f8a6f2014-10-03 01:10:05 +03005180 vcpu->run->debug.arch.dr7 = dr7;
Nadav Amit82b32772014-11-02 11:54:45 +02005181 vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03005182 vcpu->run->debug.arch.exception = DB_VECTOR;
5183 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005184 return 0;
5185 } else {
Nadav Amit7305eb52014-11-02 11:54:44 +02005186 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005187 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005188 kvm_queue_exception(vcpu, DB_VECTOR);
5189 return 1;
5190 }
5191 }
5192
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005193 if (vcpu->guest_debug == 0) {
5194 u32 cpu_based_vm_exec_control;
5195
5196 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5197 cpu_based_vm_exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5198 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5199
5200 /*
5201 * No more DR vmexits; force a reload of the debug registers
5202 * and reenter on this instruction. The next vmexit will
5203 * retrieve the full state of the debug registers.
5204 */
5205 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5206 return 1;
5207 }
5208
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005209 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5210 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005211 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005212
5213 if (kvm_get_dr(vcpu, dr, &val))
5214 return 1;
5215 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005216 } else
Nadav Amit57773922014-06-18 17:19:23 +03005217 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005218 return 1;
5219
Avi Kivity6aa8b732006-12-10 02:21:36 -08005220 skip_emulated_instruction(vcpu);
5221 return 1;
5222}
5223
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005224static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5225{
5226 return vcpu->arch.dr6;
5227}
5228
5229static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5230{
5231}
5232
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005233static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5234{
5235 u32 cpu_based_vm_exec_control;
5236
5237 get_debugreg(vcpu->arch.db[0], 0);
5238 get_debugreg(vcpu->arch.db[1], 1);
5239 get_debugreg(vcpu->arch.db[2], 2);
5240 get_debugreg(vcpu->arch.db[3], 3);
5241 get_debugreg(vcpu->arch.dr6, 6);
5242 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5243
5244 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
5245
5246 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5247 cpu_based_vm_exec_control |= CPU_BASED_MOV_DR_EXITING;
5248 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5249}
5250
Gleb Natapov020df072010-04-13 10:05:23 +03005251static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5252{
5253 vmcs_writel(GUEST_DR7, val);
5254}
5255
Avi Kivity851ba692009-08-24 11:10:17 +03005256static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005257{
Avi Kivity06465c52007-02-28 20:46:53 +02005258 kvm_emulate_cpuid(vcpu);
5259 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005260}
5261
Avi Kivity851ba692009-08-24 11:10:17 +03005262static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005263{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005264 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08005265 u64 data;
5266
5267 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02005268 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005269 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005270 return 1;
5271 }
5272
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005273 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005274
Avi Kivity6aa8b732006-12-10 02:21:36 -08005275 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005276 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
5277 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005278 skip_emulated_instruction(vcpu);
5279 return 1;
5280}
5281
Avi Kivity851ba692009-08-24 11:10:17 +03005282static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005283{
Will Auld8fe8ab42012-11-29 12:42:12 -08005284 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005285 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5286 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5287 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005288
Will Auld8fe8ab42012-11-29 12:42:12 -08005289 msr.data = data;
5290 msr.index = ecx;
5291 msr.host_initiated = false;
Nadav Amit854e8bb2014-09-16 03:24:05 +03005292 if (kvm_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005293 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005294 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005295 return 1;
5296 }
5297
Avi Kivity59200272010-01-25 19:47:02 +02005298 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005299 skip_emulated_instruction(vcpu);
5300 return 1;
5301}
5302
Avi Kivity851ba692009-08-24 11:10:17 +03005303static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005304{
Avi Kivity3842d132010-07-27 12:30:24 +03005305 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005306 return 1;
5307}
5308
Avi Kivity851ba692009-08-24 11:10:17 +03005309static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005310{
Eddie Dong85f455f2007-07-06 12:20:49 +03005311 u32 cpu_based_vm_exec_control;
5312
5313 /* clear pending irq */
5314 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5315 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5316 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005317
Avi Kivity3842d132010-07-27 12:30:24 +03005318 kvm_make_request(KVM_REQ_EVENT, vcpu);
5319
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005320 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005321
Dor Laorc1150d82007-01-05 16:36:24 -08005322 /*
5323 * If the user space waits to inject interrupts, exit as soon as
5324 * possible
5325 */
Gleb Natapov80618232009-04-21 17:44:56 +03005326 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03005327 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03005328 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03005329 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08005330 return 0;
5331 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005332 return 1;
5333}
5334
Avi Kivity851ba692009-08-24 11:10:17 +03005335static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005336{
5337 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03005338 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005339}
5340
Avi Kivity851ba692009-08-24 11:10:17 +03005341static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005342{
Dor Laor510043d2007-02-19 18:25:43 +02005343 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005344 kvm_emulate_hypercall(vcpu);
5345 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02005346}
5347
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005348static int handle_invd(struct kvm_vcpu *vcpu)
5349{
Andre Przywara51d8b662010-12-21 11:12:02 +01005350 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005351}
5352
Avi Kivity851ba692009-08-24 11:10:17 +03005353static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005354{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005355 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005356
5357 kvm_mmu_invlpg(vcpu, exit_qualification);
5358 skip_emulated_instruction(vcpu);
5359 return 1;
5360}
5361
Avi Kivityfee84b02011-11-10 14:57:25 +02005362static int handle_rdpmc(struct kvm_vcpu *vcpu)
5363{
5364 int err;
5365
5366 err = kvm_rdpmc(vcpu);
5367 kvm_complete_insn_gp(vcpu, err);
5368
5369 return 1;
5370}
5371
Avi Kivity851ba692009-08-24 11:10:17 +03005372static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005373{
5374 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005375 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005376 return 1;
5377}
5378
Dexuan Cui2acf9232010-06-10 11:27:12 +08005379static int handle_xsetbv(struct kvm_vcpu *vcpu)
5380{
5381 u64 new_bv = kvm_read_edx_eax(vcpu);
5382 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5383
5384 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5385 skip_emulated_instruction(vcpu);
5386 return 1;
5387}
5388
Wanpeng Lif53cd632014-12-02 19:14:58 +08005389static int handle_xsaves(struct kvm_vcpu *vcpu)
5390{
5391 skip_emulated_instruction(vcpu);
5392 WARN(1, "this should never happen\n");
5393 return 1;
5394}
5395
5396static int handle_xrstors(struct kvm_vcpu *vcpu)
5397{
5398 skip_emulated_instruction(vcpu);
5399 WARN(1, "this should never happen\n");
5400 return 1;
5401}
5402
Avi Kivity851ba692009-08-24 11:10:17 +03005403static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005404{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005405 if (likely(fasteoi)) {
5406 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5407 int access_type, offset;
5408
5409 access_type = exit_qualification & APIC_ACCESS_TYPE;
5410 offset = exit_qualification & APIC_ACCESS_OFFSET;
5411 /*
5412 * Sane guest uses MOV to write EOI, with written value
5413 * not cared. So make a short-circuit here by avoiding
5414 * heavy instruction emulation.
5415 */
5416 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5417 (offset == APIC_EOI)) {
5418 kvm_lapic_set_eoi(vcpu);
5419 skip_emulated_instruction(vcpu);
5420 return 1;
5421 }
5422 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005423 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005424}
5425
Yang Zhangc7c9c562013-01-25 10:18:51 +08005426static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5427{
5428 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5429 int vector = exit_qualification & 0xff;
5430
5431 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5432 kvm_apic_set_eoi_accelerated(vcpu, vector);
5433 return 1;
5434}
5435
Yang Zhang83d4c282013-01-25 10:18:49 +08005436static int handle_apic_write(struct kvm_vcpu *vcpu)
5437{
5438 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5439 u32 offset = exit_qualification & 0xfff;
5440
5441 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5442 kvm_apic_write_nodecode(vcpu, offset);
5443 return 1;
5444}
5445
Avi Kivity851ba692009-08-24 11:10:17 +03005446static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005447{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005448 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005449 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005450 bool has_error_code = false;
5451 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005452 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005453 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005454
5455 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005456 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005457 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005458
5459 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5460
5461 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005462 if (reason == TASK_SWITCH_GATE && idt_v) {
5463 switch (type) {
5464 case INTR_TYPE_NMI_INTR:
5465 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005466 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005467 break;
5468 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005469 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005470 kvm_clear_interrupt_queue(vcpu);
5471 break;
5472 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005473 if (vmx->idt_vectoring_info &
5474 VECTORING_INFO_DELIVER_CODE_MASK) {
5475 has_error_code = true;
5476 error_code =
5477 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5478 }
5479 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005480 case INTR_TYPE_SOFT_EXCEPTION:
5481 kvm_clear_exception_queue(vcpu);
5482 break;
5483 default:
5484 break;
5485 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005486 }
Izik Eidus37817f22008-03-24 23:14:53 +02005487 tss_selector = exit_qualification;
5488
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005489 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5490 type != INTR_TYPE_EXT_INTR &&
5491 type != INTR_TYPE_NMI_INTR))
5492 skip_emulated_instruction(vcpu);
5493
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005494 if (kvm_task_switch(vcpu, tss_selector,
5495 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
5496 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03005497 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5498 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5499 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005500 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03005501 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005502
5503 /* clear all local breakpoint enable flags */
Nadav Amit0e8a09962014-10-03 01:10:02 +03005504 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~0x155);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005505
5506 /*
5507 * TODO: What about debug traps on tss switch?
5508 * Are we supposed to inject them and update dr6?
5509 */
5510
5511 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02005512}
5513
Avi Kivity851ba692009-08-24 11:10:17 +03005514static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005515{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005516 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005517 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005518 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005519 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08005520
Sheng Yangf9c617f2009-03-25 10:08:52 +08005521 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08005522
Sheng Yang14394422008-04-28 12:24:45 +08005523 gla_validity = (exit_qualification >> 7) & 0x3;
5524 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
5525 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
5526 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
5527 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08005528 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08005529 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
5530 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03005531 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5532 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03005533 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08005534 }
5535
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005536 /*
5537 * EPT violation happened while executing iret from NMI,
5538 * "blocked by NMI" bit has to be set before next VM entry.
5539 * There are errata that may cause this bit to not be set:
5540 * AAK134, BY25.
5541 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005542 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
5543 cpu_has_virtual_nmis() &&
5544 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005545 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5546
Sheng Yang14394422008-04-28 12:24:45 +08005547 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005548 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005549
5550 /* It is a write fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08005551 error_code = exit_qualification & PFERR_WRITE_MASK;
Yang Zhang25d92082013-08-06 12:00:32 +03005552 /* It is a fetch fault? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08005553 error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005554 /* ept page table is present? */
Tiejun Chen81ed33e2014-11-18 17:12:56 +08005555 error_code |= (exit_qualification >> 3) & PFERR_PRESENT_MASK;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005556
Yang Zhang25d92082013-08-06 12:00:32 +03005557 vcpu->arch.exit_qualification = exit_qualification;
5558
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005559 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005560}
5561
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005562static u64 ept_rsvd_mask(u64 spte, int level)
5563{
5564 int i;
5565 u64 mask = 0;
5566
5567 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
5568 mask |= (1ULL << i);
5569
Wanpeng Lia32e8452014-08-20 15:31:53 +08005570 if (level == 4)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005571 /* bits 7:3 reserved */
5572 mask |= 0xf8;
Wanpeng Lia32e8452014-08-20 15:31:53 +08005573 else if (spte & (1ULL << 7))
5574 /*
5575 * 1GB/2MB page, bits 29:12 or 20:12 reserved respectively,
5576 * level == 1 if the hypervisor is using the ignored bit 7.
5577 */
5578 mask |= (PAGE_SIZE << ((level - 1) * 9)) - PAGE_SIZE;
5579 else if (level > 1)
5580 /* bits 6:3 reserved */
5581 mask |= 0x78;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005582
5583 return mask;
5584}
5585
5586static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
5587 int level)
5588{
5589 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
5590
5591 /* 010b (write-only) */
5592 WARN_ON((spte & 0x7) == 0x2);
5593
5594 /* 110b (write/execute) */
5595 WARN_ON((spte & 0x7) == 0x6);
5596
5597 /* 100b (execute-only) and value not supported by logical processor */
5598 if (!cpu_has_vmx_ept_execute_only())
5599 WARN_ON((spte & 0x7) == 0x4);
5600
5601 /* not 000b */
5602 if ((spte & 0x7)) {
5603 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
5604
5605 if (rsvd_bits != 0) {
5606 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
5607 __func__, rsvd_bits);
5608 WARN_ON(1);
5609 }
5610
Wanpeng Lia32e8452014-08-20 15:31:53 +08005611 /* bits 5:3 are _not_ reserved for large page or leaf page */
5612 if ((rsvd_bits & 0x38) == 0) {
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005613 u64 ept_mem_type = (spte & 0x38) >> 3;
5614
5615 if (ept_mem_type == 2 || ept_mem_type == 3 ||
5616 ept_mem_type == 7) {
5617 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
5618 __func__, ept_mem_type);
5619 WARN_ON(1);
5620 }
5621 }
5622 }
5623}
5624
Avi Kivity851ba692009-08-24 11:10:17 +03005625static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005626{
5627 u64 sptes[4];
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005628 int nr_sptes, i, ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005629 gpa_t gpa;
5630
5631 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005632 if (!kvm_io_bus_write(vcpu->kvm, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
5633 skip_emulated_instruction(vcpu);
5634 return 1;
5635 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005636
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005637 ret = handle_mmio_page_fault_common(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005638 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005639 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
5640 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005641
5642 if (unlikely(ret == RET_MMIO_PF_INVALID))
5643 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
5644
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005645 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005646 return 1;
5647
5648 /* It is the real ept misconfig */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005649 printk(KERN_ERR "EPT: Misconfiguration.\n");
5650 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
5651
5652 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
5653
5654 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
5655 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
5656
Avi Kivity851ba692009-08-24 11:10:17 +03005657 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5658 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005659
5660 return 0;
5661}
5662
Avi Kivity851ba692009-08-24 11:10:17 +03005663static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005664{
5665 u32 cpu_based_vm_exec_control;
5666
5667 /* clear pending NMI */
5668 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5669 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
5670 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5671 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005672 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005673
5674 return 1;
5675}
5676
Mohammed Gamal80ced182009-09-01 12:48:18 +02005677static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005678{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005679 struct vcpu_vmx *vmx = to_vmx(vcpu);
5680 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005681 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02005682 u32 cpu_exec_ctrl;
5683 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005684 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005685
5686 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5687 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005688
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005689 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03005690 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005691 return handle_interrupt_window(&vmx->vcpu);
5692
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005693 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
5694 return 1;
5695
Gleb Natapov991eebf2013-04-11 12:10:51 +03005696 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005697
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005698 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02005699 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005700 ret = 0;
5701 goto out;
5702 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005703
Avi Kivityde5f70e2012-06-12 20:22:28 +03005704 if (err != EMULATE_DONE) {
5705 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5706 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5707 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005708 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03005709 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005710
Gleb Natapov8d76c492013-05-08 18:38:44 +03005711 if (vcpu->arch.halt_request) {
5712 vcpu->arch.halt_request = 0;
5713 ret = kvm_emulate_halt(vcpu);
5714 goto out;
5715 }
5716
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005717 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02005718 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005719 if (need_resched())
5720 schedule();
5721 }
5722
Mohammed Gamal80ced182009-09-01 12:48:18 +02005723out:
5724 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005725}
5726
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005727static int __grow_ple_window(int val)
5728{
5729 if (ple_window_grow < 1)
5730 return ple_window;
5731
5732 val = min(val, ple_window_actual_max);
5733
5734 if (ple_window_grow < ple_window)
5735 val *= ple_window_grow;
5736 else
5737 val += ple_window_grow;
5738
5739 return val;
5740}
5741
5742static int __shrink_ple_window(int val, int modifier, int minimum)
5743{
5744 if (modifier < 1)
5745 return ple_window;
5746
5747 if (modifier < ple_window)
5748 val /= modifier;
5749 else
5750 val -= modifier;
5751
5752 return max(val, minimum);
5753}
5754
5755static void grow_ple_window(struct kvm_vcpu *vcpu)
5756{
5757 struct vcpu_vmx *vmx = to_vmx(vcpu);
5758 int old = vmx->ple_window;
5759
5760 vmx->ple_window = __grow_ple_window(old);
5761
5762 if (vmx->ple_window != old)
5763 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02005764
5765 trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005766}
5767
5768static void shrink_ple_window(struct kvm_vcpu *vcpu)
5769{
5770 struct vcpu_vmx *vmx = to_vmx(vcpu);
5771 int old = vmx->ple_window;
5772
5773 vmx->ple_window = __shrink_ple_window(old,
5774 ple_window_shrink, ple_window);
5775
5776 if (vmx->ple_window != old)
5777 vmx->ple_window_dirty = true;
Radim Krčmář7b462682014-08-21 18:08:09 +02005778
5779 trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old);
Radim Krčmářb4a2d312014-08-21 18:08:08 +02005780}
5781
5782/*
5783 * ple_window_actual_max is computed to be one grow_ple_window() below
5784 * ple_window_max. (See __grow_ple_window for the reason.)
5785 * This prevents overflows, because ple_window_max is int.
5786 * ple_window_max effectively rounded down to a multiple of ple_window_grow in
5787 * this process.
5788 * ple_window_max is also prevented from setting vmx->ple_window < ple_window.
5789 */
5790static void update_ple_window_actual_max(void)
5791{
5792 ple_window_actual_max =
5793 __shrink_ple_window(max(ple_window_max, ple_window),
5794 ple_window_grow, INT_MIN);
5795}
5796
Tiejun Chenf2c76482014-10-28 10:14:47 +08005797static __init int hardware_setup(void)
5798{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08005799 int r = -ENOMEM, i, msr;
5800
5801 rdmsrl_safe(MSR_EFER, &host_efer);
5802
5803 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
5804 kvm_define_shared_msr(i, vmx_msr_index[i]);
5805
5806 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
5807 if (!vmx_io_bitmap_a)
5808 return r;
5809
5810 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
5811 if (!vmx_io_bitmap_b)
5812 goto out;
5813
5814 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
5815 if (!vmx_msr_bitmap_legacy)
5816 goto out1;
5817
5818 vmx_msr_bitmap_legacy_x2apic =
5819 (unsigned long *)__get_free_page(GFP_KERNEL);
5820 if (!vmx_msr_bitmap_legacy_x2apic)
5821 goto out2;
5822
5823 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
5824 if (!vmx_msr_bitmap_longmode)
5825 goto out3;
5826
5827 vmx_msr_bitmap_longmode_x2apic =
5828 (unsigned long *)__get_free_page(GFP_KERNEL);
5829 if (!vmx_msr_bitmap_longmode_x2apic)
5830 goto out4;
5831 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
5832 if (!vmx_vmread_bitmap)
5833 goto out5;
5834
5835 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
5836 if (!vmx_vmwrite_bitmap)
5837 goto out6;
5838
5839 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
5840 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
5841
5842 /*
5843 * Allow direct access to the PC debug port (it is often used for I/O
5844 * delays, but the vmexits simply slow things down).
5845 */
5846 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
5847 clear_bit(0x80, vmx_io_bitmap_a);
5848
5849 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
5850
5851 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
5852 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
5853
Tiejun Chen34a1cd62014-10-28 10:14:48 +08005854 if (setup_vmcs_config(&vmcs_config) < 0) {
5855 r = -EIO;
5856 goto out7;
Tiejun Chenbaa03522014-12-23 16:21:11 +08005857 }
Tiejun Chenf2c76482014-10-28 10:14:47 +08005858
5859 if (boot_cpu_has(X86_FEATURE_NX))
5860 kvm_enable_efer_bits(EFER_NX);
5861
5862 if (!cpu_has_vmx_vpid())
5863 enable_vpid = 0;
5864 if (!cpu_has_vmx_shadow_vmcs())
5865 enable_shadow_vmcs = 0;
5866 if (enable_shadow_vmcs)
5867 init_vmcs_shadow_fields();
5868
5869 if (!cpu_has_vmx_ept() ||
5870 !cpu_has_vmx_ept_4levels()) {
5871 enable_ept = 0;
5872 enable_unrestricted_guest = 0;
5873 enable_ept_ad_bits = 0;
5874 }
5875
5876 if (!cpu_has_vmx_ept_ad_bits())
5877 enable_ept_ad_bits = 0;
5878
5879 if (!cpu_has_vmx_unrestricted_guest())
5880 enable_unrestricted_guest = 0;
5881
5882 if (!cpu_has_vmx_flexpriority()) {
5883 flexpriority_enabled = 0;
5884
5885 /*
5886 * set_apic_access_page_addr() is used to reload apic access
5887 * page upon invalidation. No need to do anything if the
5888 * processor does not have the APIC_ACCESS_ADDR VMCS field.
5889 */
5890 kvm_x86_ops->set_apic_access_page_addr = NULL;
5891 }
5892
5893 if (!cpu_has_vmx_tpr_shadow())
5894 kvm_x86_ops->update_cr8_intercept = NULL;
5895
5896 if (enable_ept && !cpu_has_vmx_ept_2m_page())
5897 kvm_disable_largepages();
5898
5899 if (!cpu_has_vmx_ple())
5900 ple_gap = 0;
5901
5902 if (!cpu_has_vmx_apicv())
5903 enable_apicv = 0;
5904
5905 if (enable_apicv)
5906 kvm_x86_ops->update_cr8_intercept = NULL;
5907 else {
5908 kvm_x86_ops->hwapic_irr_update = NULL;
Tiejun Chenb4eef9b2014-12-22 10:32:57 +01005909 kvm_x86_ops->hwapic_isr_update = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08005910 kvm_x86_ops->deliver_posted_interrupt = NULL;
Marcelo Tosatti7c6a98d2014-12-16 09:08:14 -05005911 kvm_x86_ops->test_posted_interrupt = NULL;
Tiejun Chenf2c76482014-10-28 10:14:47 +08005912 kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy;
5913 }
5914
5915 if (nested)
5916 nested_vmx_setup_ctls_msrs();
5917
Tiejun Chenbaa03522014-12-23 16:21:11 +08005918 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
5919 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
5920 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
5921 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
5922 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
5923 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
5924 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
5925
5926 memcpy(vmx_msr_bitmap_legacy_x2apic,
5927 vmx_msr_bitmap_legacy, PAGE_SIZE);
5928 memcpy(vmx_msr_bitmap_longmode_x2apic,
5929 vmx_msr_bitmap_longmode, PAGE_SIZE);
5930
5931 if (enable_apicv) {
5932 for (msr = 0x800; msr <= 0x8ff; msr++)
5933 vmx_disable_intercept_msr_read_x2apic(msr);
5934
5935 /* According SDM, in x2apic mode, the whole id reg is used.
5936 * But in KVM, it only use the highest eight bits. Need to
5937 * intercept it */
5938 vmx_enable_intercept_msr_read_x2apic(0x802);
5939 /* TMCCT */
5940 vmx_enable_intercept_msr_read_x2apic(0x839);
5941 /* TPR */
5942 vmx_disable_intercept_msr_write_x2apic(0x808);
5943 /* EOI */
5944 vmx_disable_intercept_msr_write_x2apic(0x80b);
5945 /* SELF-IPI */
5946 vmx_disable_intercept_msr_write_x2apic(0x83f);
5947 }
5948
5949 if (enable_ept) {
5950 kvm_mmu_set_mask_ptes(0ull,
5951 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
5952 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
5953 0ull, VMX_EPT_EXECUTABLE_MASK);
5954 ept_set_mmio_spte_mask();
5955 kvm_enable_tdp();
5956 } else
5957 kvm_disable_tdp();
5958
5959 update_ple_window_actual_max();
5960
Kai Huang843e4332015-01-28 10:54:28 +08005961 /*
5962 * Only enable PML when hardware supports PML feature, and both EPT
5963 * and EPT A/D bit features are enabled -- PML depends on them to work.
5964 */
5965 if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
5966 enable_pml = 0;
5967
5968 if (!enable_pml) {
5969 kvm_x86_ops->slot_enable_log_dirty = NULL;
5970 kvm_x86_ops->slot_disable_log_dirty = NULL;
5971 kvm_x86_ops->flush_log_dirty = NULL;
5972 kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
5973 }
5974
Tiejun Chenf2c76482014-10-28 10:14:47 +08005975 return alloc_kvm_area();
Tiejun Chen34a1cd62014-10-28 10:14:48 +08005976
5977out7:
5978 free_page((unsigned long)vmx_vmwrite_bitmap);
5979out6:
5980 free_page((unsigned long)vmx_vmread_bitmap);
5981out5:
5982 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
5983out4:
5984 free_page((unsigned long)vmx_msr_bitmap_longmode);
5985out3:
5986 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
5987out2:
5988 free_page((unsigned long)vmx_msr_bitmap_legacy);
5989out1:
5990 free_page((unsigned long)vmx_io_bitmap_b);
5991out:
5992 free_page((unsigned long)vmx_io_bitmap_a);
5993
5994 return r;
Tiejun Chenf2c76482014-10-28 10:14:47 +08005995}
5996
5997static __exit void hardware_unsetup(void)
5998{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08005999 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
6000 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
6001 free_page((unsigned long)vmx_msr_bitmap_legacy);
6002 free_page((unsigned long)vmx_msr_bitmap_longmode);
6003 free_page((unsigned long)vmx_io_bitmap_b);
6004 free_page((unsigned long)vmx_io_bitmap_a);
6005 free_page((unsigned long)vmx_vmwrite_bitmap);
6006 free_page((unsigned long)vmx_vmread_bitmap);
6007
Tiejun Chenf2c76482014-10-28 10:14:47 +08006008 free_kvm_area();
6009}
6010
Avi Kivity6aa8b732006-12-10 02:21:36 -08006011/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006012 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6013 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6014 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03006015static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006016{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02006017 if (ple_gap)
6018 grow_ple_window(vcpu);
6019
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006020 skip_emulated_instruction(vcpu);
6021 kvm_vcpu_on_spin(vcpu);
6022
6023 return 1;
6024}
6025
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006026static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08006027{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006028 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08006029 return 1;
6030}
6031
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006032static int handle_mwait(struct kvm_vcpu *vcpu)
6033{
6034 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6035 return handle_nop(vcpu);
6036}
6037
6038static int handle_monitor(struct kvm_vcpu *vcpu)
6039{
6040 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6041 return handle_nop(vcpu);
6042}
6043
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006044/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006045 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
6046 * We could reuse a single VMCS for all the L2 guests, but we also want the
6047 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
6048 * allows keeping them loaded on the processor, and in the future will allow
6049 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
6050 * every entry if they never change.
6051 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
6052 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
6053 *
6054 * The following functions allocate and free a vmcs02 in this pool.
6055 */
6056
6057/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
6058static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
6059{
6060 struct vmcs02_list *item;
6061 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6062 if (item->vmptr == vmx->nested.current_vmptr) {
6063 list_move(&item->list, &vmx->nested.vmcs02_pool);
6064 return &item->vmcs02;
6065 }
6066
6067 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
6068 /* Recycle the least recently used VMCS. */
6069 item = list_entry(vmx->nested.vmcs02_pool.prev,
6070 struct vmcs02_list, list);
6071 item->vmptr = vmx->nested.current_vmptr;
6072 list_move(&item->list, &vmx->nested.vmcs02_pool);
6073 return &item->vmcs02;
6074 }
6075
6076 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02006077 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006078 if (!item)
6079 return NULL;
6080 item->vmcs02.vmcs = alloc_vmcs();
6081 if (!item->vmcs02.vmcs) {
6082 kfree(item);
6083 return NULL;
6084 }
6085 loaded_vmcs_init(&item->vmcs02);
6086 item->vmptr = vmx->nested.current_vmptr;
6087 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
6088 vmx->nested.vmcs02_num++;
6089 return &item->vmcs02;
6090}
6091
6092/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
6093static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
6094{
6095 struct vmcs02_list *item;
6096 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
6097 if (item->vmptr == vmptr) {
6098 free_loaded_vmcs(&item->vmcs02);
6099 list_del(&item->list);
6100 kfree(item);
6101 vmx->nested.vmcs02_num--;
6102 return;
6103 }
6104}
6105
6106/*
6107 * Free all VMCSs saved for this vcpu, except the one pointed by
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006108 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
6109 * must be &vmx->vmcs01.
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006110 */
6111static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
6112{
6113 struct vmcs02_list *item, *n;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006114
6115 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006116 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006117 /*
6118 * Something will leak if the above WARN triggers. Better than
6119 * a use-after-free.
6120 */
6121 if (vmx->loaded_vmcs == &item->vmcs02)
6122 continue;
6123
6124 free_loaded_vmcs(&item->vmcs02);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006125 list_del(&item->list);
6126 kfree(item);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02006127 vmx->nested.vmcs02_num--;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006128 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006129}
6130
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006131/*
6132 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
6133 * set the success or error code of an emulated VMX instruction, as specified
6134 * by Vol 2B, VMX Instruction Reference, "Conventions".
6135 */
6136static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
6137{
6138 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
6139 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6140 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
6141}
6142
6143static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
6144{
6145 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6146 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
6147 X86_EFLAGS_SF | X86_EFLAGS_OF))
6148 | X86_EFLAGS_CF);
6149}
6150
Abel Gordon145c28d2013-04-18 14:36:55 +03006151static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08006152 u32 vm_instruction_error)
6153{
6154 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
6155 /*
6156 * failValid writes the error number to the current VMCS, which
6157 * can't be done there isn't a current VMCS.
6158 */
6159 nested_vmx_failInvalid(vcpu);
6160 return;
6161 }
6162 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
6163 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
6164 X86_EFLAGS_SF | X86_EFLAGS_OF))
6165 | X86_EFLAGS_ZF);
6166 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
6167 /*
6168 * We don't need to force a shadow sync because
6169 * VM_INSTRUCTION_ERROR is not shadowed
6170 */
6171}
Abel Gordon145c28d2013-04-18 14:36:55 +03006172
Wincy Vanff651cb2014-12-11 08:52:58 +03006173static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator)
6174{
6175 /* TODO: not to reset guest simply here. */
6176 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6177 pr_warn("kvm: nested vmx abort, indicator %d\n", indicator);
6178}
6179
Jan Kiszkaf4124502014-03-07 20:03:13 +01006180static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
6181{
6182 struct vcpu_vmx *vmx =
6183 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
6184
6185 vmx->nested.preemption_timer_expired = true;
6186 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6187 kvm_vcpu_kick(&vmx->vcpu);
6188
6189 return HRTIMER_NORESTART;
6190}
6191
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006192/*
Bandan Das19677e32014-05-06 02:19:15 -04006193 * Decode the memory-address operand of a vmx instruction, as recorded on an
6194 * exit caused by such an instruction (run by a guest hypervisor).
6195 * On success, returns 0. When the operand is invalid, returns 1 and throws
6196 * #UD or #GP.
6197 */
6198static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
6199 unsigned long exit_qualification,
6200 u32 vmx_instruction_info, gva_t *ret)
6201{
6202 /*
6203 * According to Vol. 3B, "Information for VM Exits Due to Instruction
6204 * Execution", on an exit, vmx_instruction_info holds most of the
6205 * addressing components of the operand. Only the displacement part
6206 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
6207 * For how an actual address is calculated from all these components,
6208 * refer to Vol. 1, "Operand Addressing".
6209 */
6210 int scaling = vmx_instruction_info & 3;
6211 int addr_size = (vmx_instruction_info >> 7) & 7;
6212 bool is_reg = vmx_instruction_info & (1u << 10);
6213 int seg_reg = (vmx_instruction_info >> 15) & 7;
6214 int index_reg = (vmx_instruction_info >> 18) & 0xf;
6215 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
6216 int base_reg = (vmx_instruction_info >> 23) & 0xf;
6217 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
6218
6219 if (is_reg) {
6220 kvm_queue_exception(vcpu, UD_VECTOR);
6221 return 1;
6222 }
6223
6224 /* Addr = segment_base + offset */
6225 /* offset = base + [index * scale] + displacement */
6226 *ret = vmx_get_segment_base(vcpu, seg_reg);
6227 if (base_is_valid)
6228 *ret += kvm_register_read(vcpu, base_reg);
6229 if (index_is_valid)
6230 *ret += kvm_register_read(vcpu, index_reg)<<scaling;
6231 *ret += exit_qualification; /* holds the displacement */
6232
6233 if (addr_size == 1) /* 32 bit */
6234 *ret &= 0xffffffff;
6235
6236 /*
6237 * TODO: throw #GP (and return 1) in various cases that the VM*
6238 * instructions require it - e.g., offset beyond segment limit,
6239 * unusable or unreadable/unwritable segment, non-canonical 64-bit
6240 * address, and so on. Currently these are not checked.
6241 */
6242 return 0;
6243}
6244
6245/*
Bandan Das3573e222014-05-06 02:19:16 -04006246 * This function performs the various checks including
6247 * - if it's 4KB aligned
6248 * - No bits beyond the physical address width are set
6249 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04006250 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04006251 */
Bandan Das4291b582014-05-06 02:19:18 -04006252static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
6253 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04006254{
6255 gva_t gva;
6256 gpa_t vmptr;
6257 struct x86_exception e;
6258 struct page *page;
6259 struct vcpu_vmx *vmx = to_vmx(vcpu);
6260 int maxphyaddr = cpuid_maxphyaddr(vcpu);
6261
6262 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
6263 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
6264 return 1;
6265
6266 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6267 sizeof(vmptr), &e)) {
6268 kvm_inject_page_fault(vcpu, &e);
6269 return 1;
6270 }
6271
6272 switch (exit_reason) {
6273 case EXIT_REASON_VMON:
6274 /*
6275 * SDM 3: 24.11.5
6276 * The first 4 bytes of VMXON region contain the supported
6277 * VMCS revision identifier
6278 *
6279 * Note - IA32_VMX_BASIC[48] will never be 1
6280 * for the nested case;
6281 * which replaces physical address width with 32
6282 *
6283 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006284 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04006285 nested_vmx_failInvalid(vcpu);
6286 skip_emulated_instruction(vcpu);
6287 return 1;
6288 }
6289
6290 page = nested_get_page(vcpu, vmptr);
6291 if (page == NULL ||
6292 *(u32 *)kmap(page) != VMCS12_REVISION) {
6293 nested_vmx_failInvalid(vcpu);
6294 kunmap(page);
6295 skip_emulated_instruction(vcpu);
6296 return 1;
6297 }
6298 kunmap(page);
6299 vmx->nested.vmxon_ptr = vmptr;
6300 break;
Bandan Das4291b582014-05-06 02:19:18 -04006301 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006302 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006303 nested_vmx_failValid(vcpu,
6304 VMXERR_VMCLEAR_INVALID_ADDRESS);
6305 skip_emulated_instruction(vcpu);
6306 return 1;
6307 }
Bandan Das3573e222014-05-06 02:19:16 -04006308
Bandan Das4291b582014-05-06 02:19:18 -04006309 if (vmptr == vmx->nested.vmxon_ptr) {
6310 nested_vmx_failValid(vcpu,
6311 VMXERR_VMCLEAR_VMXON_POINTER);
6312 skip_emulated_instruction(vcpu);
6313 return 1;
6314 }
6315 break;
6316 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02006317 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04006318 nested_vmx_failValid(vcpu,
6319 VMXERR_VMPTRLD_INVALID_ADDRESS);
6320 skip_emulated_instruction(vcpu);
6321 return 1;
6322 }
6323
6324 if (vmptr == vmx->nested.vmxon_ptr) {
6325 nested_vmx_failValid(vcpu,
6326 VMXERR_VMCLEAR_VMXON_POINTER);
6327 skip_emulated_instruction(vcpu);
6328 return 1;
6329 }
6330 break;
Bandan Das3573e222014-05-06 02:19:16 -04006331 default:
6332 return 1; /* shouldn't happen */
6333 }
6334
Bandan Das4291b582014-05-06 02:19:18 -04006335 if (vmpointer)
6336 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04006337 return 0;
6338}
6339
6340/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006341 * Emulate the VMXON instruction.
6342 * Currently, we just remember that VMX is active, and do not save or even
6343 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
6344 * do not currently need to store anything in that guest-allocated memory
6345 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
6346 * argument is different from the VMXON pointer (which the spec says they do).
6347 */
6348static int handle_vmon(struct kvm_vcpu *vcpu)
6349{
6350 struct kvm_segment cs;
6351 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03006352 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006353 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
6354 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006355
6356 /* The Intel VMX Instruction Reference lists a bunch of bits that
6357 * are prerequisite to running VMXON, most notably cr4.VMXE must be
6358 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
6359 * Otherwise, we should fail with #UD. We test these now:
6360 */
6361 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
6362 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
6363 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
6364 kvm_queue_exception(vcpu, UD_VECTOR);
6365 return 1;
6366 }
6367
6368 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6369 if (is_long_mode(vcpu) && !cs.l) {
6370 kvm_queue_exception(vcpu, UD_VECTOR);
6371 return 1;
6372 }
6373
6374 if (vmx_get_cpl(vcpu)) {
6375 kvm_inject_gp(vcpu, 0);
6376 return 1;
6377 }
Bandan Das3573e222014-05-06 02:19:16 -04006378
Bandan Das4291b582014-05-06 02:19:18 -04006379 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04006380 return 1;
6381
Abel Gordon145c28d2013-04-18 14:36:55 +03006382 if (vmx->nested.vmxon) {
6383 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
6384 skip_emulated_instruction(vcpu);
6385 return 1;
6386 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006387
6388 if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
6389 != VMXON_NEEDED_FEATURES) {
6390 kvm_inject_gp(vcpu, 0);
6391 return 1;
6392 }
6393
Abel Gordon8de48832013-04-18 14:37:25 +03006394 if (enable_shadow_vmcs) {
6395 shadow_vmcs = alloc_vmcs();
6396 if (!shadow_vmcs)
6397 return -ENOMEM;
6398 /* mark vmcs as shadow */
6399 shadow_vmcs->revision_id |= (1u << 31);
6400 /* init shadow vmcs */
6401 vmcs_clear(shadow_vmcs);
6402 vmx->nested.current_shadow_vmcs = shadow_vmcs;
6403 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006404
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006405 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
6406 vmx->nested.vmcs02_num = 0;
6407
Jan Kiszkaf4124502014-03-07 20:03:13 +01006408 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
6409 HRTIMER_MODE_REL);
6410 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
6411
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006412 vmx->nested.vmxon = true;
6413
6414 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08006415 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006416 return 1;
6417}
6418
6419/*
6420 * Intel's VMX Instruction Reference specifies a common set of prerequisites
6421 * for running VMX instructions (except VMXON, whose prerequisites are
6422 * slightly different). It also specifies what exception to inject otherwise.
6423 */
6424static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
6425{
6426 struct kvm_segment cs;
6427 struct vcpu_vmx *vmx = to_vmx(vcpu);
6428
6429 if (!vmx->nested.vmxon) {
6430 kvm_queue_exception(vcpu, UD_VECTOR);
6431 return 0;
6432 }
6433
6434 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6435 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
6436 (is_long_mode(vcpu) && !cs.l)) {
6437 kvm_queue_exception(vcpu, UD_VECTOR);
6438 return 0;
6439 }
6440
6441 if (vmx_get_cpl(vcpu)) {
6442 kvm_inject_gp(vcpu, 0);
6443 return 0;
6444 }
6445
6446 return 1;
6447}
6448
Abel Gordone7953d72013-04-18 14:37:55 +03006449static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
6450{
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006451 u32 exec_control;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006452 if (vmx->nested.current_vmptr == -1ull)
6453 return;
6454
6455 /* current_vmptr and current_vmcs12 are always set/reset together */
6456 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
6457 return;
6458
Abel Gordon012f83c2013-04-18 14:39:25 +03006459 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006460 /* copy to memory all shadowed fields in case
6461 they were modified */
6462 copy_shadow_to_vmcs12(vmx);
6463 vmx->nested.sync_shadow_vmcs = false;
6464 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6465 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
6466 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
6467 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03006468 }
Abel Gordone7953d72013-04-18 14:37:55 +03006469 kunmap(vmx->nested.current_vmcs12_page);
6470 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006471 vmx->nested.current_vmptr = -1ull;
6472 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03006473}
6474
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006475/*
6476 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
6477 * just stops using VMX.
6478 */
6479static void free_nested(struct vcpu_vmx *vmx)
6480{
6481 if (!vmx->nested.vmxon)
6482 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006483
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006484 vmx->nested.vmxon = false;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006485 nested_release_vmcs12(vmx);
Abel Gordone7953d72013-04-18 14:37:55 +03006486 if (enable_shadow_vmcs)
6487 free_vmcs(vmx->nested.current_shadow_vmcs);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006488 /* Unpin physical memory we referred to in current vmcs02 */
6489 if (vmx->nested.apic_access_page) {
6490 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02006491 vmx->nested.apic_access_page = NULL;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006492 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006493 if (vmx->nested.virtual_apic_page) {
6494 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02006495 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08006496 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006497
6498 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006499}
6500
6501/* Emulate the VMXOFF instruction */
6502static int handle_vmoff(struct kvm_vcpu *vcpu)
6503{
6504 if (!nested_vmx_check_permission(vcpu))
6505 return 1;
6506 free_nested(to_vmx(vcpu));
6507 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08006508 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006509 return 1;
6510}
6511
Nadav Har'El27d6c862011-05-25 23:06:59 +03006512/* Emulate the VMCLEAR instruction */
6513static int handle_vmclear(struct kvm_vcpu *vcpu)
6514{
6515 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03006516 gpa_t vmptr;
6517 struct vmcs12 *vmcs12;
6518 struct page *page;
Nadav Har'El27d6c862011-05-25 23:06:59 +03006519
6520 if (!nested_vmx_check_permission(vcpu))
6521 return 1;
6522
Bandan Das4291b582014-05-06 02:19:18 -04006523 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03006524 return 1;
6525
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006526 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03006527 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03006528
6529 page = nested_get_page(vcpu, vmptr);
6530 if (page == NULL) {
6531 /*
6532 * For accurate processor emulation, VMCLEAR beyond available
6533 * physical memory should do nothing at all. However, it is
6534 * possible that a nested vmx bug, not a guest hypervisor bug,
6535 * resulted in this case, so let's shut down before doing any
6536 * more damage:
6537 */
6538 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6539 return 1;
6540 }
6541 vmcs12 = kmap(page);
6542 vmcs12->launch_state = 0;
6543 kunmap(page);
6544 nested_release_page(page);
6545
6546 nested_free_vmcs02(vmx, vmptr);
6547
6548 skip_emulated_instruction(vcpu);
6549 nested_vmx_succeed(vcpu);
6550 return 1;
6551}
6552
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006553static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
6554
6555/* Emulate the VMLAUNCH instruction */
6556static int handle_vmlaunch(struct kvm_vcpu *vcpu)
6557{
6558 return nested_vmx_run(vcpu, true);
6559}
6560
6561/* Emulate the VMRESUME instruction */
6562static int handle_vmresume(struct kvm_vcpu *vcpu)
6563{
6564
6565 return nested_vmx_run(vcpu, false);
6566}
6567
Nadav Har'El49f705c2011-05-25 23:08:30 +03006568enum vmcs_field_type {
6569 VMCS_FIELD_TYPE_U16 = 0,
6570 VMCS_FIELD_TYPE_U64 = 1,
6571 VMCS_FIELD_TYPE_U32 = 2,
6572 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
6573};
6574
6575static inline int vmcs_field_type(unsigned long field)
6576{
6577 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
6578 return VMCS_FIELD_TYPE_U32;
6579 return (field >> 13) & 0x3 ;
6580}
6581
6582static inline int vmcs_field_readonly(unsigned long field)
6583{
6584 return (((field >> 10) & 0x3) == 1);
6585}
6586
6587/*
6588 * Read a vmcs12 field. Since these can have varying lengths and we return
6589 * one type, we chose the biggest type (u64) and zero-extend the return value
6590 * to that size. Note that the caller, handle_vmread, might need to use only
6591 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
6592 * 64-bit fields are to be returned).
6593 */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006594static inline int vmcs12_read_any(struct kvm_vcpu *vcpu,
6595 unsigned long field, u64 *ret)
Nadav Har'El49f705c2011-05-25 23:08:30 +03006596{
6597 short offset = vmcs_field_to_offset(field);
6598 char *p;
6599
6600 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006601 return offset;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006602
6603 p = ((char *)(get_vmcs12(vcpu))) + offset;
6604
6605 switch (vmcs_field_type(field)) {
6606 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6607 *ret = *((natural_width *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006608 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006609 case VMCS_FIELD_TYPE_U16:
6610 *ret = *((u16 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006611 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006612 case VMCS_FIELD_TYPE_U32:
6613 *ret = *((u32 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006614 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006615 case VMCS_FIELD_TYPE_U64:
6616 *ret = *((u64 *)p);
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006617 return 0;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006618 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006619 WARN_ON(1);
6620 return -ENOENT;
Nadav Har'El49f705c2011-05-25 23:08:30 +03006621 }
6622}
6623
Abel Gordon20b97fe2013-04-18 14:36:25 +03006624
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006625static inline int vmcs12_write_any(struct kvm_vcpu *vcpu,
6626 unsigned long field, u64 field_value){
Abel Gordon20b97fe2013-04-18 14:36:25 +03006627 short offset = vmcs_field_to_offset(field);
6628 char *p = ((char *) get_vmcs12(vcpu)) + offset;
6629 if (offset < 0)
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006630 return offset;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006631
6632 switch (vmcs_field_type(field)) {
6633 case VMCS_FIELD_TYPE_U16:
6634 *(u16 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006635 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006636 case VMCS_FIELD_TYPE_U32:
6637 *(u32 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006638 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006639 case VMCS_FIELD_TYPE_U64:
6640 *(u64 *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006641 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006642 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6643 *(natural_width *)p = field_value;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006644 return 0;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006645 default:
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006646 WARN_ON(1);
6647 return -ENOENT;
Abel Gordon20b97fe2013-04-18 14:36:25 +03006648 }
6649
6650}
6651
Abel Gordon16f5b902013-04-18 14:38:25 +03006652static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
6653{
6654 int i;
6655 unsigned long field;
6656 u64 field_value;
6657 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02006658 const unsigned long *fields = shadow_read_write_fields;
6659 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03006660
Jan Kiszka282da872014-10-08 18:05:39 +02006661 preempt_disable();
6662
Abel Gordon16f5b902013-04-18 14:38:25 +03006663 vmcs_load(shadow_vmcs);
6664
6665 for (i = 0; i < num_fields; i++) {
6666 field = fields[i];
6667 switch (vmcs_field_type(field)) {
6668 case VMCS_FIELD_TYPE_U16:
6669 field_value = vmcs_read16(field);
6670 break;
6671 case VMCS_FIELD_TYPE_U32:
6672 field_value = vmcs_read32(field);
6673 break;
6674 case VMCS_FIELD_TYPE_U64:
6675 field_value = vmcs_read64(field);
6676 break;
6677 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6678 field_value = vmcs_readl(field);
6679 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006680 default:
6681 WARN_ON(1);
6682 continue;
Abel Gordon16f5b902013-04-18 14:38:25 +03006683 }
6684 vmcs12_write_any(&vmx->vcpu, field, field_value);
6685 }
6686
6687 vmcs_clear(shadow_vmcs);
6688 vmcs_load(vmx->loaded_vmcs->vmcs);
Jan Kiszka282da872014-10-08 18:05:39 +02006689
6690 preempt_enable();
Abel Gordon16f5b902013-04-18 14:38:25 +03006691}
6692
Abel Gordonc3114422013-04-18 14:38:55 +03006693static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
6694{
Mathias Krausec2bae892013-06-26 20:36:21 +02006695 const unsigned long *fields[] = {
6696 shadow_read_write_fields,
6697 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03006698 };
Mathias Krausec2bae892013-06-26 20:36:21 +02006699 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03006700 max_shadow_read_write_fields,
6701 max_shadow_read_only_fields
6702 };
6703 int i, q;
6704 unsigned long field;
6705 u64 field_value = 0;
6706 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
6707
6708 vmcs_load(shadow_vmcs);
6709
Mathias Krausec2bae892013-06-26 20:36:21 +02006710 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03006711 for (i = 0; i < max_fields[q]; i++) {
6712 field = fields[q][i];
6713 vmcs12_read_any(&vmx->vcpu, field, &field_value);
6714
6715 switch (vmcs_field_type(field)) {
6716 case VMCS_FIELD_TYPE_U16:
6717 vmcs_write16(field, (u16)field_value);
6718 break;
6719 case VMCS_FIELD_TYPE_U32:
6720 vmcs_write32(field, (u32)field_value);
6721 break;
6722 case VMCS_FIELD_TYPE_U64:
6723 vmcs_write64(field, (u64)field_value);
6724 break;
6725 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6726 vmcs_writel(field, (long)field_value);
6727 break;
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006728 default:
6729 WARN_ON(1);
6730 break;
Abel Gordonc3114422013-04-18 14:38:55 +03006731 }
6732 }
6733 }
6734
6735 vmcs_clear(shadow_vmcs);
6736 vmcs_load(vmx->loaded_vmcs->vmcs);
6737}
6738
Nadav Har'El49f705c2011-05-25 23:08:30 +03006739/*
6740 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
6741 * used before) all generate the same failure when it is missing.
6742 */
6743static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
6744{
6745 struct vcpu_vmx *vmx = to_vmx(vcpu);
6746 if (vmx->nested.current_vmptr == -1ull) {
6747 nested_vmx_failInvalid(vcpu);
6748 skip_emulated_instruction(vcpu);
6749 return 0;
6750 }
6751 return 1;
6752}
6753
6754static int handle_vmread(struct kvm_vcpu *vcpu)
6755{
6756 unsigned long field;
6757 u64 field_value;
6758 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6759 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6760 gva_t gva = 0;
6761
6762 if (!nested_vmx_check_permission(vcpu) ||
6763 !nested_vmx_check_vmcs12(vcpu))
6764 return 1;
6765
6766 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03006767 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03006768 /* Read the field, zero-extended to a u64 field_value */
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006769 if (vmcs12_read_any(vcpu, field, &field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03006770 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
6771 skip_emulated_instruction(vcpu);
6772 return 1;
6773 }
6774 /*
6775 * Now copy part of this value to register or memory, as requested.
6776 * Note that the number of bits actually copied is 32 or 64 depending
6777 * on the guest's mode (32 or 64 bit), not on the given field's length.
6778 */
6779 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03006780 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03006781 field_value);
6782 } else {
6783 if (get_vmx_mem_address(vcpu, exit_qualification,
6784 vmx_instruction_info, &gva))
6785 return 1;
6786 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
6787 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
6788 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
6789 }
6790
6791 nested_vmx_succeed(vcpu);
6792 skip_emulated_instruction(vcpu);
6793 return 1;
6794}
6795
6796
6797static int handle_vmwrite(struct kvm_vcpu *vcpu)
6798{
6799 unsigned long field;
6800 gva_t gva;
6801 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6802 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03006803 /* The value to write might be 32 or 64 bits, depending on L1's long
6804 * mode, and eventually we need to write that into a field of several
6805 * possible lengths. The code below first zero-extends the value to 64
6806 * bit (field_value), and then copies only the approriate number of
6807 * bits into the vmcs12 field.
6808 */
6809 u64 field_value = 0;
6810 struct x86_exception e;
6811
6812 if (!nested_vmx_check_permission(vcpu) ||
6813 !nested_vmx_check_vmcs12(vcpu))
6814 return 1;
6815
6816 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03006817 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03006818 (((vmx_instruction_info) >> 3) & 0xf));
6819 else {
6820 if (get_vmx_mem_address(vcpu, exit_qualification,
6821 vmx_instruction_info, &gva))
6822 return 1;
6823 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03006824 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03006825 kvm_inject_page_fault(vcpu, &e);
6826 return 1;
6827 }
6828 }
6829
6830
Nadav Amit27e6fb52014-06-18 17:19:26 +03006831 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03006832 if (vmcs_field_readonly(field)) {
6833 nested_vmx_failValid(vcpu,
6834 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
6835 skip_emulated_instruction(vcpu);
6836 return 1;
6837 }
6838
Paolo Bonzinia2ae9df2014-11-04 18:31:19 +01006839 if (vmcs12_write_any(vcpu, field, field_value) < 0) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03006840 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
6841 skip_emulated_instruction(vcpu);
6842 return 1;
6843 }
6844
6845 nested_vmx_succeed(vcpu);
6846 skip_emulated_instruction(vcpu);
6847 return 1;
6848}
6849
Nadav Har'El63846662011-05-25 23:07:29 +03006850/* Emulate the VMPTRLD instruction */
6851static int handle_vmptrld(struct kvm_vcpu *vcpu)
6852{
6853 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03006854 gpa_t vmptr;
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006855 u32 exec_control;
Nadav Har'El63846662011-05-25 23:07:29 +03006856
6857 if (!nested_vmx_check_permission(vcpu))
6858 return 1;
6859
Bandan Das4291b582014-05-06 02:19:18 -04006860 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03006861 return 1;
6862
Nadav Har'El63846662011-05-25 23:07:29 +03006863 if (vmx->nested.current_vmptr != vmptr) {
6864 struct vmcs12 *new_vmcs12;
6865 struct page *page;
6866 page = nested_get_page(vcpu, vmptr);
6867 if (page == NULL) {
6868 nested_vmx_failInvalid(vcpu);
6869 skip_emulated_instruction(vcpu);
6870 return 1;
6871 }
6872 new_vmcs12 = kmap(page);
6873 if (new_vmcs12->revision_id != VMCS12_REVISION) {
6874 kunmap(page);
6875 nested_release_page_clean(page);
6876 nested_vmx_failValid(vcpu,
6877 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
6878 skip_emulated_instruction(vcpu);
6879 return 1;
6880 }
Nadav Har'El63846662011-05-25 23:07:29 +03006881
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006882 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03006883 vmx->nested.current_vmptr = vmptr;
6884 vmx->nested.current_vmcs12 = new_vmcs12;
6885 vmx->nested.current_vmcs12_page = page;
Abel Gordon012f83c2013-04-18 14:39:25 +03006886 if (enable_shadow_vmcs) {
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006887 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6888 exec_control |= SECONDARY_EXEC_SHADOW_VMCS;
6889 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
6890 vmcs_write64(VMCS_LINK_POINTER,
6891 __pa(vmx->nested.current_shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03006892 vmx->nested.sync_shadow_vmcs = true;
6893 }
Nadav Har'El63846662011-05-25 23:07:29 +03006894 }
6895
6896 nested_vmx_succeed(vcpu);
6897 skip_emulated_instruction(vcpu);
6898 return 1;
6899}
6900
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006901/* Emulate the VMPTRST instruction */
6902static int handle_vmptrst(struct kvm_vcpu *vcpu)
6903{
6904 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6905 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6906 gva_t vmcs_gva;
6907 struct x86_exception e;
6908
6909 if (!nested_vmx_check_permission(vcpu))
6910 return 1;
6911
6912 if (get_vmx_mem_address(vcpu, exit_qualification,
6913 vmx_instruction_info, &vmcs_gva))
6914 return 1;
6915 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
6916 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
6917 (void *)&to_vmx(vcpu)->nested.current_vmptr,
6918 sizeof(u64), &e)) {
6919 kvm_inject_page_fault(vcpu, &e);
6920 return 1;
6921 }
6922 nested_vmx_succeed(vcpu);
6923 skip_emulated_instruction(vcpu);
6924 return 1;
6925}
6926
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006927/* Emulate the INVEPT instruction */
6928static int handle_invept(struct kvm_vcpu *vcpu)
6929{
6930 u32 vmx_instruction_info, types;
6931 unsigned long type;
6932 gva_t gva;
6933 struct x86_exception e;
6934 struct {
6935 u64 eptp, gpa;
6936 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006937
6938 if (!(nested_vmx_secondary_ctls_high & SECONDARY_EXEC_ENABLE_EPT) ||
6939 !(nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
6940 kvm_queue_exception(vcpu, UD_VECTOR);
6941 return 1;
6942 }
6943
6944 if (!nested_vmx_check_permission(vcpu))
6945 return 1;
6946
6947 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
6948 kvm_queue_exception(vcpu, UD_VECTOR);
6949 return 1;
6950 }
6951
6952 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03006953 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006954
6955 types = (nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
6956
6957 if (!(types & (1UL << type))) {
6958 nested_vmx_failValid(vcpu,
6959 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
6960 return 1;
6961 }
6962
6963 /* According to the Intel VMX instruction reference, the memory
6964 * operand is read even if it isn't needed (e.g., for type==global)
6965 */
6966 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
6967 vmx_instruction_info, &gva))
6968 return 1;
6969 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
6970 sizeof(operand), &e)) {
6971 kvm_inject_page_fault(vcpu, &e);
6972 return 1;
6973 }
6974
6975 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006976 case VMX_EPT_EXTENT_GLOBAL:
6977 kvm_mmu_sync_roots(vcpu);
Liang Chen77c39132014-09-18 12:38:37 -04006978 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006979 nested_vmx_succeed(vcpu);
6980 break;
6981 default:
Bandan Das4b855072014-04-19 18:17:44 -04006982 /* Trap single context invalidation invept calls */
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006983 BUG_ON(1);
6984 break;
6985 }
6986
6987 skip_emulated_instruction(vcpu);
6988 return 1;
6989}
6990
Petr Matouseka642fc32014-09-23 20:22:30 +02006991static int handle_invvpid(struct kvm_vcpu *vcpu)
6992{
6993 kvm_queue_exception(vcpu, UD_VECTOR);
6994 return 1;
6995}
6996
Marcelo Tosatti7c6a98d2014-12-16 09:08:14 -05006997static bool vmx_test_pir(struct kvm_vcpu *vcpu, int vector)
6998{
6999 struct vcpu_vmx *vmx = to_vmx(vcpu);
7000
7001 return pi_test_pir(vector, &vmx->pi_desc);
7002}
7003
Kai Huang843e4332015-01-28 10:54:28 +08007004static int handle_pml_full(struct kvm_vcpu *vcpu)
7005{
7006 unsigned long exit_qualification;
7007
7008 trace_kvm_pml_full(vcpu->vcpu_id);
7009
7010 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7011
7012 /*
7013 * PML buffer FULL happened while executing iret from NMI,
7014 * "blocked by NMI" bit has to be set before next VM entry.
7015 */
7016 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
7017 cpu_has_virtual_nmis() &&
7018 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
7019 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7020 GUEST_INTR_STATE_NMI);
7021
7022 /*
7023 * PML buffer already flushed at beginning of VMEXIT. Nothing to do
7024 * here.., and there's no userspace involvement needed for PML.
7025 */
7026 return 1;
7027}
7028
Nadav Har'El0140cae2011-05-25 23:06:28 +03007029/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08007030 * The exit handlers return 1 if the exit was handled fully and guest execution
7031 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
7032 * to be done to userspace and return 0.
7033 */
Mathias Krause772e0312012-08-30 01:30:19 +02007034static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007035 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
7036 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08007037 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08007038 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007039 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007040 [EXIT_REASON_CR_ACCESS] = handle_cr,
7041 [EXIT_REASON_DR_ACCESS] = handle_dr,
7042 [EXIT_REASON_CPUID] = handle_cpuid,
7043 [EXIT_REASON_MSR_READ] = handle_rdmsr,
7044 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
7045 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
7046 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02007047 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03007048 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02007049 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02007050 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03007051 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007052 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03007053 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03007054 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007055 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007056 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03007057 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03007058 [EXIT_REASON_VMOFF] = handle_vmoff,
7059 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08007060 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
7061 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08007062 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007063 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02007064 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08007065 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02007066 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08007067 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03007068 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
7069 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08007070 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04007071 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
7072 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03007073 [EXIT_REASON_INVEPT] = handle_invept,
Petr Matouseka642fc32014-09-23 20:22:30 +02007074 [EXIT_REASON_INVVPID] = handle_invvpid,
Wanpeng Lif53cd632014-12-02 19:14:58 +08007075 [EXIT_REASON_XSAVES] = handle_xsaves,
7076 [EXIT_REASON_XRSTORS] = handle_xrstors,
Kai Huang843e4332015-01-28 10:54:28 +08007077 [EXIT_REASON_PML_FULL] = handle_pml_full,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007078};
7079
7080static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04007081 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007082
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007083static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
7084 struct vmcs12 *vmcs12)
7085{
7086 unsigned long exit_qualification;
7087 gpa_t bitmap, last_bitmap;
7088 unsigned int port;
7089 int size;
7090 u8 b;
7091
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007092 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05007093 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007094
7095 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7096
7097 port = exit_qualification >> 16;
7098 size = (exit_qualification & 7) + 1;
7099
7100 last_bitmap = (gpa_t)-1;
7101 b = -1;
7102
7103 while (size > 0) {
7104 if (port < 0x8000)
7105 bitmap = vmcs12->io_bitmap_a;
7106 else if (port < 0x10000)
7107 bitmap = vmcs12->io_bitmap_b;
7108 else
7109 return 1;
7110 bitmap += (port & 0x7fff) / 8;
7111
7112 if (last_bitmap != bitmap)
7113 if (kvm_read_guest(vcpu->kvm, bitmap, &b, 1))
7114 return 1;
7115 if (b & (1 << (port & 7)))
7116 return 1;
7117
7118 port++;
7119 size--;
7120 last_bitmap = bitmap;
7121 }
7122
7123 return 0;
7124}
7125
Nadav Har'El644d7112011-05-25 23:12:35 +03007126/*
7127 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
7128 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
7129 * disinterest in the current event (read or write a specific MSR) by using an
7130 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
7131 */
7132static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
7133 struct vmcs12 *vmcs12, u32 exit_reason)
7134{
7135 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
7136 gpa_t bitmap;
7137
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01007138 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Nadav Har'El644d7112011-05-25 23:12:35 +03007139 return 1;
7140
7141 /*
7142 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
7143 * for the four combinations of read/write and low/high MSR numbers.
7144 * First we need to figure out which of the four to use:
7145 */
7146 bitmap = vmcs12->msr_bitmap;
7147 if (exit_reason == EXIT_REASON_MSR_WRITE)
7148 bitmap += 2048;
7149 if (msr_index >= 0xc0000000) {
7150 msr_index -= 0xc0000000;
7151 bitmap += 1024;
7152 }
7153
7154 /* Then read the msr_index'th bit from this bitmap: */
7155 if (msr_index < 1024*8) {
7156 unsigned char b;
Jan Kiszkabd31a7f2013-02-14 19:46:27 +01007157 if (kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1))
7158 return 1;
Nadav Har'El644d7112011-05-25 23:12:35 +03007159 return 1 & (b >> (msr_index & 7));
7160 } else
7161 return 1; /* let L1 handle the wrong parameter */
7162}
7163
7164/*
7165 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
7166 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
7167 * intercept (via guest_host_mask etc.) the current event.
7168 */
7169static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
7170 struct vmcs12 *vmcs12)
7171{
7172 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7173 int cr = exit_qualification & 15;
7174 int reg = (exit_qualification >> 8) & 15;
Nadav Amit1e32c072014-06-18 17:19:25 +03007175 unsigned long val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03007176
7177 switch ((exit_qualification >> 4) & 3) {
7178 case 0: /* mov to cr */
7179 switch (cr) {
7180 case 0:
7181 if (vmcs12->cr0_guest_host_mask &
7182 (val ^ vmcs12->cr0_read_shadow))
7183 return 1;
7184 break;
7185 case 3:
7186 if ((vmcs12->cr3_target_count >= 1 &&
7187 vmcs12->cr3_target_value0 == val) ||
7188 (vmcs12->cr3_target_count >= 2 &&
7189 vmcs12->cr3_target_value1 == val) ||
7190 (vmcs12->cr3_target_count >= 3 &&
7191 vmcs12->cr3_target_value2 == val) ||
7192 (vmcs12->cr3_target_count >= 4 &&
7193 vmcs12->cr3_target_value3 == val))
7194 return 0;
7195 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
7196 return 1;
7197 break;
7198 case 4:
7199 if (vmcs12->cr4_guest_host_mask &
7200 (vmcs12->cr4_read_shadow ^ val))
7201 return 1;
7202 break;
7203 case 8:
7204 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
7205 return 1;
7206 break;
7207 }
7208 break;
7209 case 2: /* clts */
7210 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
7211 (vmcs12->cr0_read_shadow & X86_CR0_TS))
7212 return 1;
7213 break;
7214 case 1: /* mov from cr */
7215 switch (cr) {
7216 case 3:
7217 if (vmcs12->cpu_based_vm_exec_control &
7218 CPU_BASED_CR3_STORE_EXITING)
7219 return 1;
7220 break;
7221 case 8:
7222 if (vmcs12->cpu_based_vm_exec_control &
7223 CPU_BASED_CR8_STORE_EXITING)
7224 return 1;
7225 break;
7226 }
7227 break;
7228 case 3: /* lmsw */
7229 /*
7230 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
7231 * cr0. Other attempted changes are ignored, with no exit.
7232 */
7233 if (vmcs12->cr0_guest_host_mask & 0xe &
7234 (val ^ vmcs12->cr0_read_shadow))
7235 return 1;
7236 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
7237 !(vmcs12->cr0_read_shadow & 0x1) &&
7238 (val & 0x1))
7239 return 1;
7240 break;
7241 }
7242 return 0;
7243}
7244
7245/*
7246 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
7247 * should handle it ourselves in L0 (and then continue L2). Only call this
7248 * when in is_guest_mode (L2).
7249 */
7250static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
7251{
Nadav Har'El644d7112011-05-25 23:12:35 +03007252 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7253 struct vcpu_vmx *vmx = to_vmx(vcpu);
7254 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01007255 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03007256
Jan Kiszka542060e2014-01-04 18:47:21 +01007257 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
7258 vmcs_readl(EXIT_QUALIFICATION),
7259 vmx->idt_vectoring_info,
7260 intr_info,
7261 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
7262 KVM_ISA_VMX);
7263
Nadav Har'El644d7112011-05-25 23:12:35 +03007264 if (vmx->nested.nested_run_pending)
7265 return 0;
7266
7267 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02007268 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
7269 vmcs_read32(VM_INSTRUCTION_ERROR));
Nadav Har'El644d7112011-05-25 23:12:35 +03007270 return 1;
7271 }
7272
7273 switch (exit_reason) {
7274 case EXIT_REASON_EXCEPTION_NMI:
7275 if (!is_exception(intr_info))
7276 return 0;
7277 else if (is_page_fault(intr_info))
7278 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01007279 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01007280 !(vmcs12->guest_cr0 & X86_CR0_TS))
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01007281 return 0;
Nadav Har'El644d7112011-05-25 23:12:35 +03007282 return vmcs12->exception_bitmap &
7283 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
7284 case EXIT_REASON_EXTERNAL_INTERRUPT:
7285 return 0;
7286 case EXIT_REASON_TRIPLE_FAULT:
7287 return 1;
7288 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02007289 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03007290 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02007291 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03007292 case EXIT_REASON_TASK_SWITCH:
7293 return 1;
7294 case EXIT_REASON_CPUID:
Marcelo Tosattibc613492014-09-18 18:24:57 -03007295 if (kvm_register_read(vcpu, VCPU_REGS_RAX) == 0xa)
7296 return 0;
Nadav Har'El644d7112011-05-25 23:12:35 +03007297 return 1;
7298 case EXIT_REASON_HLT:
7299 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
7300 case EXIT_REASON_INVD:
7301 return 1;
7302 case EXIT_REASON_INVLPG:
7303 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
7304 case EXIT_REASON_RDPMC:
7305 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
7306 case EXIT_REASON_RDTSC:
7307 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
7308 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
7309 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
7310 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
7311 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
7312 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Petr Matouseka642fc32014-09-23 20:22:30 +02007313 case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID:
Nadav Har'El644d7112011-05-25 23:12:35 +03007314 /*
7315 * VMX instructions trap unconditionally. This allows L1 to
7316 * emulate them for its L2 guest, i.e., allows 3-level nesting!
7317 */
7318 return 1;
7319 case EXIT_REASON_CR_ACCESS:
7320 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
7321 case EXIT_REASON_DR_ACCESS:
7322 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
7323 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01007324 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03007325 case EXIT_REASON_MSR_READ:
7326 case EXIT_REASON_MSR_WRITE:
7327 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
7328 case EXIT_REASON_INVALID_STATE:
7329 return 1;
7330 case EXIT_REASON_MWAIT_INSTRUCTION:
7331 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
7332 case EXIT_REASON_MONITOR_INSTRUCTION:
7333 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
7334 case EXIT_REASON_PAUSE_INSTRUCTION:
7335 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
7336 nested_cpu_has2(vmcs12,
7337 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
7338 case EXIT_REASON_MCE_DURING_VMENTRY:
7339 return 0;
7340 case EXIT_REASON_TPR_BELOW_THRESHOLD:
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007341 return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW);
Nadav Har'El644d7112011-05-25 23:12:35 +03007342 case EXIT_REASON_APIC_ACCESS:
7343 return nested_cpu_has2(vmcs12,
7344 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
7345 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03007346 /*
7347 * L0 always deals with the EPT violation. If nested EPT is
7348 * used, and the nested mmu code discovers that the address is
7349 * missing in the guest EPT table (EPT12), the EPT violation
7350 * will be injected with nested_ept_inject_page_fault()
7351 */
7352 return 0;
Nadav Har'El644d7112011-05-25 23:12:35 +03007353 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03007354 /*
7355 * L2 never uses directly L1's EPT, but rather L0's own EPT
7356 * table (shadow on EPT) or a merged EPT table that L0 built
7357 * (EPT on EPT). So any problems with the structure of the
7358 * table is L0's fault.
7359 */
Nadav Har'El644d7112011-05-25 23:12:35 +03007360 return 0;
7361 case EXIT_REASON_WBINVD:
7362 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
7363 case EXIT_REASON_XSETBV:
7364 return 1;
Wanpeng Li81dc01f2014-12-04 19:11:07 +08007365 case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS:
7366 /*
7367 * This should never happen, since it is not possible to
7368 * set XSS to a non-zero value---neither in L1 nor in L2.
7369 * If if it were, XSS would have to be checked against
7370 * the XSS exit bitmap in vmcs12.
7371 */
7372 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES);
Nadav Har'El644d7112011-05-25 23:12:35 +03007373 default:
7374 return 1;
7375 }
7376}
7377
Avi Kivity586f9602010-11-18 13:09:54 +02007378static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
7379{
7380 *info1 = vmcs_readl(EXIT_QUALIFICATION);
7381 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
7382}
7383
Kai Huang843e4332015-01-28 10:54:28 +08007384static int vmx_enable_pml(struct vcpu_vmx *vmx)
7385{
7386 struct page *pml_pg;
7387 u32 exec_control;
7388
7389 pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO);
7390 if (!pml_pg)
7391 return -ENOMEM;
7392
7393 vmx->pml_pg = pml_pg;
7394
7395 vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
7396 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
7397
7398 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7399 exec_control |= SECONDARY_EXEC_ENABLE_PML;
7400 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
7401
7402 return 0;
7403}
7404
7405static void vmx_disable_pml(struct vcpu_vmx *vmx)
7406{
7407 u32 exec_control;
7408
7409 ASSERT(vmx->pml_pg);
7410 __free_page(vmx->pml_pg);
7411 vmx->pml_pg = NULL;
7412
7413 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7414 exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
7415 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
7416}
7417
7418static void vmx_flush_pml_buffer(struct vcpu_vmx *vmx)
7419{
7420 struct kvm *kvm = vmx->vcpu.kvm;
7421 u64 *pml_buf;
7422 u16 pml_idx;
7423
7424 pml_idx = vmcs_read16(GUEST_PML_INDEX);
7425
7426 /* Do nothing if PML buffer is empty */
7427 if (pml_idx == (PML_ENTITY_NUM - 1))
7428 return;
7429
7430 /* PML index always points to next available PML buffer entity */
7431 if (pml_idx >= PML_ENTITY_NUM)
7432 pml_idx = 0;
7433 else
7434 pml_idx++;
7435
7436 pml_buf = page_address(vmx->pml_pg);
7437 for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
7438 u64 gpa;
7439
7440 gpa = pml_buf[pml_idx];
7441 WARN_ON(gpa & (PAGE_SIZE - 1));
7442 mark_page_dirty(kvm, gpa >> PAGE_SHIFT);
7443 }
7444
7445 /* reset PML index */
7446 vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
7447}
7448
7449/*
7450 * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
7451 * Called before reporting dirty_bitmap to userspace.
7452 */
7453static void kvm_flush_pml_buffers(struct kvm *kvm)
7454{
7455 int i;
7456 struct kvm_vcpu *vcpu;
7457 /*
7458 * We only need to kick vcpu out of guest mode here, as PML buffer
7459 * is flushed at beginning of all VMEXITs, and it's obvious that only
7460 * vcpus running in guest are possible to have unflushed GPAs in PML
7461 * buffer.
7462 */
7463 kvm_for_each_vcpu(i, vcpu, kvm)
7464 kvm_vcpu_kick(vcpu);
7465}
7466
Avi Kivity6aa8b732006-12-10 02:21:36 -08007467/*
7468 * The guest has exited. See if we can fix it or if we need userspace
7469 * assistance.
7470 */
Avi Kivity851ba692009-08-24 11:10:17 +03007471static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007472{
Avi Kivity29bd8a72007-09-10 17:27:03 +03007473 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08007474 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02007475 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03007476
Kai Huang843e4332015-01-28 10:54:28 +08007477 /*
7478 * Flush logged GPAs PML buffer, this will make dirty_bitmap more
7479 * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
7480 * querying dirty_bitmap, we only need to kick all vcpus out of guest
7481 * mode as if vcpus is in root mode, the PML buffer must has been
7482 * flushed already.
7483 */
7484 if (enable_pml)
7485 vmx_flush_pml_buffer(vmx);
7486
Mohammed Gamal80ced182009-09-01 12:48:18 +02007487 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02007488 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02007489 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01007490
Nadav Har'El644d7112011-05-25 23:12:35 +03007491 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01007492 nested_vmx_vmexit(vcpu, exit_reason,
7493 vmcs_read32(VM_EXIT_INTR_INFO),
7494 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03007495 return 1;
7496 }
7497
Mohammed Gamal51207022010-05-31 22:40:54 +03007498 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
7499 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
7500 vcpu->run->fail_entry.hardware_entry_failure_reason
7501 = exit_reason;
7502 return 0;
7503 }
7504
Avi Kivity29bd8a72007-09-10 17:27:03 +03007505 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03007506 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
7507 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03007508 = vmcs_read32(VM_INSTRUCTION_ERROR);
7509 return 0;
7510 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007511
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08007512 /*
7513 * Note:
7514 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
7515 * delivery event since it indicates guest is accessing MMIO.
7516 * The vm-exit can be triggered again after return to guest that
7517 * will cause infinite loop.
7518 */
Mike Dayd77c26f2007-10-08 09:02:08 -04007519 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08007520 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02007521 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08007522 exit_reason != EXIT_REASON_TASK_SWITCH)) {
7523 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7524 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
7525 vcpu->run->internal.ndata = 2;
7526 vcpu->run->internal.data[0] = vectoring_info;
7527 vcpu->run->internal.data[1] = exit_reason;
7528 return 0;
7529 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007530
Nadav Har'El644d7112011-05-25 23:12:35 +03007531 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
7532 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03007533 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03007534 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007535 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007536 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01007537 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007538 /*
7539 * This CPU don't support us in finding the end of an
7540 * NMI-blocked window if the guest runs with IRQs
7541 * disabled. So we pull the trigger after 1 s of
7542 * futile waiting, but inform the user about this.
7543 */
7544 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
7545 "state on VCPU %d after 1 s timeout\n",
7546 __func__, vcpu->vcpu_id);
7547 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007548 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007549 }
7550
Avi Kivity6aa8b732006-12-10 02:21:36 -08007551 if (exit_reason < kvm_vmx_max_exit_handlers
7552 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03007553 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007554 else {
Michael S. Tsirkin2bc19dc2014-09-18 16:21:16 +03007555 WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason);
7556 kvm_queue_exception(vcpu, UD_VECTOR);
7557 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007558 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007559}
7560
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007561static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007562{
Wanpeng Lia7c0b072014-08-21 19:46:50 +08007563 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7564
7565 if (is_guest_mode(vcpu) &&
7566 nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
7567 return;
7568
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007569 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007570 vmcs_write32(TPR_THRESHOLD, 0);
7571 return;
7572 }
7573
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007574 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007575}
7576
Yang Zhang8d146952013-01-25 10:18:50 +08007577static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
7578{
7579 u32 sec_exec_control;
7580
7581 /*
7582 * There is not point to enable virtualize x2apic without enable
7583 * apicv
7584 */
Yang Zhangc7c9c562013-01-25 10:18:51 +08007585 if (!cpu_has_vmx_virtualize_x2apic_mode() ||
7586 !vmx_vm_has_apicv(vcpu->kvm))
Yang Zhang8d146952013-01-25 10:18:50 +08007587 return;
7588
7589 if (!vm_need_tpr_shadow(vcpu->kvm))
7590 return;
7591
7592 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7593
7594 if (set) {
7595 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7596 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
7597 } else {
7598 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
7599 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7600 }
7601 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
7602
7603 vmx_set_msr_bitmap(vcpu);
7604}
7605
Tang Chen38b99172014-09-24 15:57:54 +08007606static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
7607{
7608 struct vcpu_vmx *vmx = to_vmx(vcpu);
7609
7610 /*
7611 * Currently we do not handle the nested case where L2 has an
7612 * APIC access page of its own; that page is still pinned.
7613 * Hence, we skip the case where the VCPU is in guest mode _and_
7614 * L1 prepared an APIC access page for L2.
7615 *
7616 * For the case where L1 and L2 share the same APIC access page
7617 * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear
7618 * in the vmcs12), this function will only update either the vmcs01
7619 * or the vmcs02. If the former, the vmcs02 will be updated by
7620 * prepare_vmcs02. If the latter, the vmcs01 will be updated in
7621 * the next L2->L1 exit.
7622 */
7623 if (!is_guest_mode(vcpu) ||
7624 !nested_cpu_has2(vmx->nested.current_vmcs12,
7625 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
7626 vmcs_write64(APIC_ACCESS_ADDR, hpa);
7627}
7628
Yang Zhangc7c9c562013-01-25 10:18:51 +08007629static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
7630{
7631 u16 status;
7632 u8 old;
7633
Yang Zhangc7c9c562013-01-25 10:18:51 +08007634 if (isr == -1)
7635 isr = 0;
7636
7637 status = vmcs_read16(GUEST_INTR_STATUS);
7638 old = status >> 8;
7639 if (isr != old) {
7640 status &= 0xff;
7641 status |= isr << 8;
7642 vmcs_write16(GUEST_INTR_STATUS, status);
7643 }
7644}
7645
7646static void vmx_set_rvi(int vector)
7647{
7648 u16 status;
7649 u8 old;
7650
Wei Wang4114c272014-11-05 10:53:43 +08007651 if (vector == -1)
7652 vector = 0;
7653
Yang Zhangc7c9c562013-01-25 10:18:51 +08007654 status = vmcs_read16(GUEST_INTR_STATUS);
7655 old = (u8)status & 0xff;
7656 if ((u8)vector != old) {
7657 status &= ~0xff;
7658 status |= (u8)vector;
7659 vmcs_write16(GUEST_INTR_STATUS, status);
7660 }
7661}
7662
7663static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
7664{
Wanpeng Li963fee12014-07-17 19:03:00 +08007665 if (!is_guest_mode(vcpu)) {
7666 vmx_set_rvi(max_irr);
7667 return;
7668 }
7669
Wei Wang4114c272014-11-05 10:53:43 +08007670 if (max_irr == -1)
7671 return;
7672
Wanpeng Li963fee12014-07-17 19:03:00 +08007673 /*
Wei Wang4114c272014-11-05 10:53:43 +08007674 * In guest mode. If a vmexit is needed, vmx_check_nested_events
7675 * handles it.
7676 */
7677 if (nested_exit_on_intr(vcpu))
7678 return;
7679
7680 /*
7681 * Else, fall back to pre-APICv interrupt injection since L2
Wanpeng Li963fee12014-07-17 19:03:00 +08007682 * is run without virtual interrupt delivery.
7683 */
7684 if (!kvm_event_needs_reinjection(vcpu) &&
7685 vmx_interrupt_allowed(vcpu)) {
7686 kvm_queue_interrupt(vcpu, max_irr, false);
7687 vmx_inject_irq(vcpu);
7688 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08007689}
7690
7691static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
7692{
Yang Zhang3d81bc72013-04-11 19:25:13 +08007693 if (!vmx_vm_has_apicv(vcpu->kvm))
7694 return;
7695
Yang Zhangc7c9c562013-01-25 10:18:51 +08007696 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
7697 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
7698 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
7699 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
7700}
7701
Avi Kivity51aa01d2010-07-20 14:31:20 +03007702static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03007703{
Avi Kivity00eba012011-03-07 17:24:54 +02007704 u32 exit_intr_info;
7705
7706 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
7707 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
7708 return;
7709
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007710 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02007711 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08007712
7713 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02007714 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08007715 kvm_machine_check();
7716
Gleb Natapov20f65982009-05-11 13:35:55 +03007717 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02007718 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007719 (exit_intr_info & INTR_INFO_VALID_MASK)) {
7720 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03007721 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007722 kvm_after_handle_nmi(&vmx->vcpu);
7723 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03007724}
Gleb Natapov20f65982009-05-11 13:35:55 +03007725
Yang Zhanga547c6d2013-04-11 19:25:10 +08007726static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
7727{
7728 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7729
7730 /*
7731 * If external interrupt exists, IF bit is set in rflags/eflags on the
7732 * interrupt stack frame, and interrupt will be enabled on a return
7733 * from interrupt handler.
7734 */
7735 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
7736 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
7737 unsigned int vector;
7738 unsigned long entry;
7739 gate_desc *desc;
7740 struct vcpu_vmx *vmx = to_vmx(vcpu);
7741#ifdef CONFIG_X86_64
7742 unsigned long tmp;
7743#endif
7744
7745 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
7746 desc = (gate_desc *)vmx->host_idt_base + vector;
7747 entry = gate_offset(*desc);
7748 asm volatile(
7749#ifdef CONFIG_X86_64
7750 "mov %%" _ASM_SP ", %[sp]\n\t"
7751 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
7752 "push $%c[ss]\n\t"
7753 "push %[sp]\n\t"
7754#endif
7755 "pushf\n\t"
7756 "orl $0x200, (%%" _ASM_SP ")\n\t"
7757 __ASM_SIZE(push) " $%c[cs]\n\t"
7758 "call *%[entry]\n\t"
7759 :
7760#ifdef CONFIG_X86_64
7761 [sp]"=&r"(tmp)
7762#endif
7763 :
7764 [entry]"r"(entry),
7765 [ss]"i"(__KERNEL_DS),
7766 [cs]"i"(__KERNEL_CS)
7767 );
7768 } else
7769 local_irq_enable();
7770}
7771
Liu, Jinsongda8999d2014-02-24 10:55:46 +00007772static bool vmx_mpx_supported(void)
7773{
7774 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
7775 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
7776}
7777
Wanpeng Li55412b22014-12-02 19:21:30 +08007778static bool vmx_xsaves_supported(void)
7779{
7780 return vmcs_config.cpu_based_2nd_exec_ctrl &
7781 SECONDARY_EXEC_XSAVES;
7782}
7783
Avi Kivity51aa01d2010-07-20 14:31:20 +03007784static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
7785{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007786 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03007787 bool unblock_nmi;
7788 u8 vector;
7789 bool idtv_info_valid;
7790
7791 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03007792
Avi Kivitycf393f72008-07-01 16:20:21 +03007793 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02007794 if (vmx->nmi_known_unmasked)
7795 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007796 /*
7797 * Can't use vmx->exit_intr_info since we're not sure what
7798 * the exit reason is.
7799 */
7800 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03007801 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
7802 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
7803 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007804 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03007805 * Re-set bit "block by NMI" before VM entry if vmexit caused by
7806 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007807 * SDM 3: 23.2.2 (September 2008)
7808 * Bit 12 is undefined in any of the following cases:
7809 * If the VM exit sets the valid bit in the IDT-vectoring
7810 * information field.
7811 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03007812 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007813 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
7814 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03007815 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7816 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02007817 else
7818 vmx->nmi_known_unmasked =
7819 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
7820 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007821 } else if (unlikely(vmx->soft_vnmi_blocked))
7822 vmx->vnmi_blocked_time +=
7823 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03007824}
7825
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007826static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03007827 u32 idt_vectoring_info,
7828 int instr_len_field,
7829 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03007830{
Avi Kivity51aa01d2010-07-20 14:31:20 +03007831 u8 vector;
7832 int type;
7833 bool idtv_info_valid;
7834
7835 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03007836
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007837 vcpu->arch.nmi_injected = false;
7838 kvm_clear_exception_queue(vcpu);
7839 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007840
7841 if (!idtv_info_valid)
7842 return;
7843
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007844 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03007845
Avi Kivity668f6122008-07-02 09:28:55 +03007846 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
7847 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03007848
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007849 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03007850 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007851 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03007852 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007853 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03007854 * Clear bit "block by NMI" before VM entry if a NMI
7855 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03007856 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007857 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007858 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03007859 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007860 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007861 /* fall through */
7862 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03007863 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03007864 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03007865 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03007866 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03007867 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007868 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007869 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007870 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007871 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03007872 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007873 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007874 break;
7875 default:
7876 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03007877 }
Avi Kivitycf393f72008-07-01 16:20:21 +03007878}
7879
Avi Kivity83422e12010-07-20 14:43:23 +03007880static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
7881{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007882 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03007883 VM_EXIT_INSTRUCTION_LEN,
7884 IDT_VECTORING_ERROR_CODE);
7885}
7886
Avi Kivityb463a6f2010-07-20 15:06:17 +03007887static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
7888{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007889 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007890 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
7891 VM_ENTRY_INSTRUCTION_LEN,
7892 VM_ENTRY_EXCEPTION_ERROR_CODE);
7893
7894 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
7895}
7896
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007897static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
7898{
7899 int i, nr_msrs;
7900 struct perf_guest_switch_msr *msrs;
7901
7902 msrs = perf_guest_get_msrs(&nr_msrs);
7903
7904 if (!msrs)
7905 return;
7906
7907 for (i = 0; i < nr_msrs; i++)
7908 if (msrs[i].host == msrs[i].guest)
7909 clear_atomic_switch_msr(vmx, msrs[i].msr);
7910 else
7911 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
7912 msrs[i].host);
7913}
7914
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08007915static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007916{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007917 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andy Lutomirskid974baa2014-10-08 09:02:13 -07007918 unsigned long debugctlmsr, cr4;
Avi Kivity104f2262010-11-18 13:12:52 +02007919
7920 /* Record the guest's net vcpu time for enforced NMI injections. */
7921 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
7922 vmx->entry_time = ktime_get();
7923
7924 /* Don't enter VMX if guest state is invalid, let the exit handler
7925 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02007926 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02007927 return;
7928
Radim Krčmářa7653ec2014-08-21 18:08:07 +02007929 if (vmx->ple_window_dirty) {
7930 vmx->ple_window_dirty = false;
7931 vmcs_write32(PLE_WINDOW, vmx->ple_window);
7932 }
7933
Abel Gordon012f83c2013-04-18 14:39:25 +03007934 if (vmx->nested.sync_shadow_vmcs) {
7935 copy_vmcs12_to_shadow(vmx);
7936 vmx->nested.sync_shadow_vmcs = false;
7937 }
7938
Avi Kivity104f2262010-11-18 13:12:52 +02007939 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
7940 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
7941 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
7942 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
7943
Andy Lutomirskid974baa2014-10-08 09:02:13 -07007944 cr4 = read_cr4();
7945 if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) {
7946 vmcs_writel(HOST_CR4, cr4);
7947 vmx->host_state.vmcs_host_cr4 = cr4;
7948 }
7949
Avi Kivity104f2262010-11-18 13:12:52 +02007950 /* When single-stepping over STI and MOV SS, we must clear the
7951 * corresponding interruptibility bits in the guest state. Otherwise
7952 * vmentry fails as it then expects bit 14 (BS) in pending debug
7953 * exceptions being set, but that's not correct for the guest debugging
7954 * case. */
7955 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
7956 vmx_set_interrupt_shadow(vcpu, 0);
7957
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007958 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03007959 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007960
Nadav Har'Eld462b812011-05-24 15:26:10 +03007961 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02007962 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08007963 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007964 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
7965 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
7966 "push %%" _ASM_CX " \n\t"
7967 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03007968 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007969 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03007970 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03007971 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007972 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007973 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
7974 "mov %%cr2, %%" _ASM_DX " \n\t"
7975 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007976 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007977 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007978 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007979 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02007980 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007981 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007982 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
7983 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
7984 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
7985 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
7986 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
7987 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08007988#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02007989 "mov %c[r8](%0), %%r8 \n\t"
7990 "mov %c[r9](%0), %%r9 \n\t"
7991 "mov %c[r10](%0), %%r10 \n\t"
7992 "mov %c[r11](%0), %%r11 \n\t"
7993 "mov %c[r12](%0), %%r12 \n\t"
7994 "mov %c[r13](%0), %%r13 \n\t"
7995 "mov %c[r14](%0), %%r14 \n\t"
7996 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007997#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03007998 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03007999
Avi Kivity6aa8b732006-12-10 02:21:36 -08008000 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03008001 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03008002 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008003 "jmp 2f \n\t"
8004 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
8005 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08008006 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03008007 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02008008 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008009 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
8010 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
8011 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
8012 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
8013 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
8014 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
8015 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008016#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02008017 "mov %%r8, %c[r8](%0) \n\t"
8018 "mov %%r9, %c[r9](%0) \n\t"
8019 "mov %%r10, %c[r10](%0) \n\t"
8020 "mov %%r11, %c[r11](%0) \n\t"
8021 "mov %%r12, %c[r12](%0) \n\t"
8022 "mov %%r13, %c[r13](%0) \n\t"
8023 "mov %%r14, %c[r14](%0) \n\t"
8024 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08008025#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03008026 "mov %%cr2, %%" _ASM_AX " \n\t"
8027 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03008028
Avi Kivityb188c81f2012-09-16 15:10:58 +03008029 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02008030 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03008031 ".pushsection .rodata \n\t"
8032 ".global vmx_return \n\t"
8033 "vmx_return: " _ASM_PTR " 2b \n\t"
8034 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02008035 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03008036 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02008037 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03008038 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008039 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
8040 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
8041 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
8042 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
8043 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
8044 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
8045 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08008046#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08008047 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
8048 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
8049 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
8050 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
8051 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
8052 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
8053 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
8054 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08008055#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02008056 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
8057 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02008058 : "cc", "memory"
8059#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03008060 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008061 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03008062#else
8063 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02008064#endif
8065 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08008066
Gleb Natapov2a7921b2012-08-12 16:12:29 +03008067 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
8068 if (debugctlmsr)
8069 update_debugctlmsr(debugctlmsr);
8070
Avi Kivityaa67f602012-08-01 16:48:03 +03008071#ifndef CONFIG_X86_64
8072 /*
8073 * The sysexit path does not restore ds/es, so we must set them to
8074 * a reasonable value ourselves.
8075 *
8076 * We can't defer this to vmx_load_host_state() since that function
8077 * may be executed in interrupt context, which saves and restore segments
8078 * around it, nullifying its effect.
8079 */
8080 loadsegment(ds, __USER_DS);
8081 loadsegment(es, __USER_DS);
8082#endif
8083
Avi Kivity6de4f3a2009-05-31 22:58:47 +03008084 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02008085 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02008086 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03008087 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02008088 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008089 vcpu->arch.regs_dirty = 0;
8090
Avi Kivity1155f762007-11-22 11:30:47 +02008091 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
8092
Nadav Har'Eld462b812011-05-24 15:26:10 +03008093 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02008094
Avi Kivity51aa01d2010-07-20 14:31:20 +03008095 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02008096 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
Avi Kivity51aa01d2010-07-20 14:31:20 +03008097
Gleb Natapove0b890d2013-09-25 12:51:33 +03008098 /*
8099 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
8100 * we did not inject a still-pending event to L1 now because of
8101 * nested_run_pending, we need to re-enable this bit.
8102 */
8103 if (vmx->nested.nested_run_pending)
8104 kvm_make_request(KVM_REQ_EVENT, vcpu);
8105
8106 vmx->nested.nested_run_pending = 0;
8107
Avi Kivity51aa01d2010-07-20 14:31:20 +03008108 vmx_complete_atomic_exit(vmx);
8109 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03008110 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008111}
8112
Paolo Bonzini4fa77342014-07-17 12:25:16 +02008113static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
8114{
8115 struct vcpu_vmx *vmx = to_vmx(vcpu);
8116 int cpu;
8117
8118 if (vmx->loaded_vmcs == &vmx->vmcs01)
8119 return;
8120
8121 cpu = get_cpu();
8122 vmx->loaded_vmcs = &vmx->vmcs01;
8123 vmx_vcpu_put(vcpu);
8124 vmx_vcpu_load(vcpu, cpu);
8125 vcpu->cpu = cpu;
8126 put_cpu();
8127}
8128
Avi Kivity6aa8b732006-12-10 02:21:36 -08008129static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
8130{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008131 struct vcpu_vmx *vmx = to_vmx(vcpu);
8132
Kai Huang843e4332015-01-28 10:54:28 +08008133 if (enable_pml)
8134 vmx_disable_pml(vmx);
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08008135 free_vpid(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02008136 leave_guest_mode(vcpu);
8137 vmx_load_vmcs01(vcpu);
Marcelo Tosatti26a865f2014-01-03 17:00:51 -02008138 free_nested(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02008139 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008140 kfree(vmx->guest_msrs);
8141 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10008142 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008143}
8144
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008145static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08008146{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008147 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10008148 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03008149 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008150
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008151 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008152 return ERR_PTR(-ENOMEM);
8153
Sheng Yang2384d2b2008-01-17 15:14:33 +08008154 allocate_vpid(vmx);
8155
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008156 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
8157 if (err)
8158 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08008159
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008160 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02008161 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
8162 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03008163
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02008164 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008165 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008166 goto uninit_vcpu;
8167 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08008168
Nadav Har'Eld462b812011-05-24 15:26:10 +03008169 vmx->loaded_vmcs = &vmx->vmcs01;
8170 vmx->loaded_vmcs->vmcs = alloc_vmcs();
8171 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008172 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03008173 if (!vmm_exclusive)
8174 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
8175 loaded_vmcs_init(vmx->loaded_vmcs);
8176 if (!vmm_exclusive)
8177 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04008178
Avi Kivity15ad7142007-07-11 18:17:21 +03008179 cpu = get_cpu();
8180 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10008181 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10008182 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008183 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03008184 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008185 if (err)
8186 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02008187 if (vm_need_virtualize_apic_accesses(kvm)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02008188 err = alloc_apic_access_page(kvm);
8189 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02008190 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02008191 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08008192
Sheng Yangb927a3c2009-07-21 10:42:48 +08008193 if (enable_ept) {
8194 if (!kvm->arch.ept_identity_map_addr)
8195 kvm->arch.ept_identity_map_addr =
8196 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Tang Chenf51770e2014-09-16 18:41:59 +08008197 err = init_rmode_identity_map(kvm);
8198 if (err)
Gleb Natapov93ea5382011-02-21 12:07:59 +02008199 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08008200 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08008201
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03008202 vmx->nested.current_vmptr = -1ull;
8203 vmx->nested.current_vmcs12 = NULL;
8204
Kai Huang843e4332015-01-28 10:54:28 +08008205 /*
8206 * If PML is turned on, failure on enabling PML just results in failure
8207 * of creating the vcpu, therefore we can simplify PML logic (by
8208 * avoiding dealing with cases, such as enabling PML partially on vcpus
8209 * for the guest, etc.
8210 */
8211 if (enable_pml) {
8212 err = vmx_enable_pml(vmx);
8213 if (err)
8214 goto free_vmcs;
8215 }
8216
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008217 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08008218
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008219free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08008220 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008221free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008222 kfree(vmx->guest_msrs);
8223uninit_vcpu:
8224 kvm_vcpu_uninit(&vmx->vcpu);
8225free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08008226 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10008227 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10008228 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08008229}
8230
Yang, Sheng002c7f72007-07-31 14:23:01 +03008231static void __init vmx_check_processor_compat(void *rtn)
8232{
8233 struct vmcs_config vmcs_conf;
8234
8235 *(int *)rtn = 0;
8236 if (setup_vmcs_config(&vmcs_conf) < 0)
8237 *(int *)rtn = -EIO;
8238 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
8239 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
8240 smp_processor_id());
8241 *(int *)rtn = -EIO;
8242 }
8243}
8244
Sheng Yang67253af2008-04-25 10:20:22 +08008245static int get_ept_level(void)
8246{
8247 return VMX_EPT_DEFAULT_GAW + 1;
8248}
8249
Sheng Yang4b12f0d2009-04-27 20:35:42 +08008250static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08008251{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08008252 u64 ret;
8253
Sheng Yang522c68c2009-04-27 20:35:43 +08008254 /* For VT-d and EPT combination
8255 * 1. MMIO: always map as UC
8256 * 2. EPT with VT-d:
8257 * a. VT-d without snooping control feature: can't guarantee the
8258 * result, try to trust guest.
8259 * b. VT-d with snooping control feature: snooping control feature of
8260 * VT-d engine can guarantee the cache correctness. Just set it
8261 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08008262 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08008263 * consistent with host MTRR
8264 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08008265 if (is_mmio)
8266 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06008267 else if (kvm_arch_has_noncoherent_dma(vcpu->kvm))
Sheng Yang522c68c2009-04-27 20:35:43 +08008268 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
8269 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08008270 else
Sheng Yang522c68c2009-04-27 20:35:43 +08008271 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08008272 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08008273
8274 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08008275}
8276
Sheng Yang17cc3932010-01-05 19:02:27 +08008277static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02008278{
Sheng Yang878403b2010-01-05 19:02:29 +08008279 if (enable_ept && !cpu_has_vmx_ept_1g_page())
8280 return PT_DIRECTORY_LEVEL;
8281 else
8282 /* For shadow and EPT supported 1GB page */
8283 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02008284}
8285
Sheng Yang0e851882009-12-18 16:48:46 +08008286static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
8287{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08008288 struct kvm_cpuid_entry2 *best;
8289 struct vcpu_vmx *vmx = to_vmx(vcpu);
8290 u32 exec_control;
8291
8292 vmx->rdtscp_enabled = false;
8293 if (vmx_rdtscp_supported()) {
8294 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8295 if (exec_control & SECONDARY_EXEC_RDTSCP) {
8296 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
8297 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
8298 vmx->rdtscp_enabled = true;
8299 else {
8300 exec_control &= ~SECONDARY_EXEC_RDTSCP;
8301 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
8302 exec_control);
8303 }
8304 }
8305 }
Mao, Junjiead756a12012-07-02 01:18:48 +00008306
Mao, Junjiead756a12012-07-02 01:18:48 +00008307 /* Exposing INVPCID only when PCID is exposed */
8308 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
8309 if (vmx_invpcid_supported() &&
Ren, Yongjie4f977042012-09-07 07:36:59 +00008310 best && (best->ebx & bit(X86_FEATURE_INVPCID)) &&
Mao, Junjiead756a12012-07-02 01:18:48 +00008311 guest_cpuid_has_pcid(vcpu)) {
Takashi Iwai29282fd2012-11-09 15:20:17 +01008312 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Mao, Junjiead756a12012-07-02 01:18:48 +00008313 exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
8314 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
8315 exec_control);
8316 } else {
Takashi Iwai29282fd2012-11-09 15:20:17 +01008317 if (cpu_has_secondary_exec_ctrls()) {
8318 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
8319 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
8320 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
8321 exec_control);
8322 }
Mao, Junjiead756a12012-07-02 01:18:48 +00008323 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00008324 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00008325 }
Sheng Yang0e851882009-12-18 16:48:46 +08008326}
8327
Joerg Roedeld4330ef2010-04-22 12:33:11 +02008328static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
8329{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03008330 if (func == 1 && nested)
8331 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02008332}
8333
Yang Zhang25d92082013-08-06 12:00:32 +03008334static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
8335 struct x86_exception *fault)
8336{
Jan Kiszka533558b2014-01-04 18:47:20 +01008337 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8338 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03008339
8340 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01008341 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03008342 else
Jan Kiszka533558b2014-01-04 18:47:20 +01008343 exit_reason = EXIT_REASON_EPT_VIOLATION;
8344 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03008345 vmcs12->guest_physical_address = fault->address;
8346}
8347
Nadav Har'El155a97a2013-08-05 11:07:16 +03008348/* Callbacks for nested_ept_init_mmu_context: */
8349
8350static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
8351{
8352 /* return the page table to be shadowed - in our case, EPT12 */
8353 return get_vmcs12(vcpu)->ept_pointer;
8354}
8355
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02008356static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03008357{
Paolo Bonziniad896af2013-10-02 16:56:14 +02008358 WARN_ON(mmu_is_nested(vcpu));
8359 kvm_init_shadow_ept_mmu(vcpu,
Nadav Har'El155a97a2013-08-05 11:07:16 +03008360 nested_vmx_ept_caps & VMX_EPT_EXECUTE_ONLY_BIT);
Nadav Har'El155a97a2013-08-05 11:07:16 +03008361 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
8362 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
8363 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
8364
8365 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03008366}
8367
8368static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
8369{
8370 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
8371}
8372
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03008373static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
8374 u16 error_code)
8375{
8376 bool inequality, bit;
8377
8378 bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0;
8379 inequality =
8380 (error_code & vmcs12->page_fault_error_code_mask) !=
8381 vmcs12->page_fault_error_code_match;
8382 return inequality ^ bit;
8383}
8384
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03008385static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
8386 struct x86_exception *fault)
8387{
8388 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8389
8390 WARN_ON(!is_guest_mode(vcpu));
8391
Eugene Korenevsky19d5f102014-12-16 22:35:53 +03008392 if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code))
Jan Kiszka533558b2014-01-04 18:47:20 +01008393 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
8394 vmcs_read32(VM_EXIT_INTR_INFO),
8395 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03008396 else
8397 kvm_inject_page_fault(vcpu, fault);
8398}
8399
Wanpeng Lia2bcba52014-08-21 19:46:49 +08008400static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu,
8401 struct vmcs12 *vmcs12)
8402{
8403 struct vcpu_vmx *vmx = to_vmx(vcpu);
8404
8405 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) {
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008406 /* TODO: Also verify bits beyond physical address width are 0 */
Wanpeng Lia2bcba52014-08-21 19:46:49 +08008407 if (!PAGE_ALIGNED(vmcs12->apic_access_addr))
Wanpeng Lia2bcba52014-08-21 19:46:49 +08008408 return false;
8409
8410 /*
8411 * Translate L1 physical address to host physical
8412 * address for vmcs02. Keep the page pinned, so this
8413 * physical address remains valid. We keep a reference
8414 * to it so we can release it later.
8415 */
8416 if (vmx->nested.apic_access_page) /* shouldn't happen */
8417 nested_release_page(vmx->nested.apic_access_page);
8418 vmx->nested.apic_access_page =
8419 nested_get_page(vcpu, vmcs12->apic_access_addr);
8420 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008421
8422 if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) {
8423 /* TODO: Also verify bits beyond physical address width are 0 */
8424 if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr))
8425 return false;
8426
8427 if (vmx->nested.virtual_apic_page) /* shouldn't happen */
8428 nested_release_page(vmx->nested.virtual_apic_page);
8429 vmx->nested.virtual_apic_page =
8430 nested_get_page(vcpu, vmcs12->virtual_apic_page_addr);
8431
8432 /*
8433 * Failing the vm entry is _not_ what the processor does
8434 * but it's basically the only possibility we have.
8435 * We could still enter the guest if CR8 load exits are
8436 * enabled, CR8 store exits are enabled, and virtualize APIC
8437 * access is disabled; in this case the processor would never
8438 * use the TPR shadow and we could simply clear the bit from
8439 * the execution control. But such a configuration is useless,
8440 * so let's keep the code simple.
8441 */
8442 if (!vmx->nested.virtual_apic_page)
8443 return false;
8444 }
8445
Wanpeng Lia2bcba52014-08-21 19:46:49 +08008446 return true;
8447}
8448
Jan Kiszkaf4124502014-03-07 20:03:13 +01008449static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
8450{
8451 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
8452 struct vcpu_vmx *vmx = to_vmx(vcpu);
8453
8454 if (vcpu->arch.virtual_tsc_khz == 0)
8455 return;
8456
8457 /* Make sure short timeouts reliably trigger an immediate vmexit.
8458 * hrtimer_start does not guarantee this. */
8459 if (preemption_timeout <= 1) {
8460 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
8461 return;
8462 }
8463
8464 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
8465 preemption_timeout *= 1000000;
8466 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
8467 hrtimer_start(&vmx->nested.preemption_timer,
8468 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
8469}
8470
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03008471static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu,
8472 unsigned long count_field,
8473 unsigned long addr_field,
8474 int maxphyaddr)
Wincy Vanff651cb2014-12-11 08:52:58 +03008475{
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03008476 u64 count, addr;
8477
8478 if (vmcs12_read_any(vcpu, count_field, &count) ||
8479 vmcs12_read_any(vcpu, addr_field, &addr)) {
8480 WARN_ON(1);
8481 return -EINVAL;
8482 }
8483 if (count == 0)
8484 return 0;
8485 if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr ||
8486 (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) {
8487 pr_warn_ratelimited(
8488 "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)",
8489 addr_field, maxphyaddr, count, addr);
8490 return -EINVAL;
8491 }
8492 return 0;
8493}
8494
8495static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu,
8496 struct vmcs12 *vmcs12)
8497{
8498 int maxphyaddr;
8499
8500 if (vmcs12->vm_exit_msr_load_count == 0 &&
8501 vmcs12->vm_exit_msr_store_count == 0 &&
8502 vmcs12->vm_entry_msr_load_count == 0)
8503 return 0; /* Fast path */
8504 maxphyaddr = cpuid_maxphyaddr(vcpu);
8505 if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT,
8506 VM_EXIT_MSR_LOAD_ADDR, maxphyaddr) ||
8507 nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT,
8508 VM_EXIT_MSR_STORE_ADDR, maxphyaddr) ||
8509 nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT,
8510 VM_ENTRY_MSR_LOAD_ADDR, maxphyaddr))
Wincy Vanff651cb2014-12-11 08:52:58 +03008511 return -EINVAL;
8512 return 0;
8513}
8514
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03008515static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu,
8516 struct vmx_msr_entry *e)
8517{
8518 /* x2APIC MSR accesses are not allowed */
8519 if (apic_x2apic_mode(vcpu->arch.apic) && e->index >> 8 == 0x8)
8520 return -EINVAL;
8521 if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */
8522 e->index == MSR_IA32_UCODE_REV)
8523 return -EINVAL;
8524 if (e->reserved != 0)
8525 return -EINVAL;
8526 return 0;
8527}
8528
8529static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu,
8530 struct vmx_msr_entry *e)
Wincy Vanff651cb2014-12-11 08:52:58 +03008531{
8532 if (e->index == MSR_FS_BASE ||
8533 e->index == MSR_GS_BASE ||
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03008534 e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */
8535 nested_vmx_msr_check_common(vcpu, e))
8536 return -EINVAL;
8537 return 0;
8538}
8539
8540static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu,
8541 struct vmx_msr_entry *e)
8542{
8543 if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */
8544 nested_vmx_msr_check_common(vcpu, e))
Wincy Vanff651cb2014-12-11 08:52:58 +03008545 return -EINVAL;
8546 return 0;
8547}
8548
8549/*
8550 * Load guest's/host's msr at nested entry/exit.
8551 * return 0 for success, entry index for failure.
8552 */
8553static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
8554{
8555 u32 i;
8556 struct vmx_msr_entry e;
8557 struct msr_data msr;
8558
8559 msr.host_initiated = false;
8560 for (i = 0; i < count; i++) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03008561 if (kvm_read_guest(vcpu->kvm, gpa + i * sizeof(e),
8562 &e, sizeof(e))) {
8563 pr_warn_ratelimited(
8564 "%s cannot read MSR entry (%u, 0x%08llx)\n",
8565 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03008566 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03008567 }
8568 if (nested_vmx_load_msr_check(vcpu, &e)) {
8569 pr_warn_ratelimited(
8570 "%s check failed (%u, 0x%x, 0x%x)\n",
8571 __func__, i, e.index, e.reserved);
8572 goto fail;
8573 }
Wincy Vanff651cb2014-12-11 08:52:58 +03008574 msr.index = e.index;
8575 msr.data = e.value;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03008576 if (kvm_set_msr(vcpu, &msr)) {
8577 pr_warn_ratelimited(
8578 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
8579 __func__, i, e.index, e.value);
Wincy Vanff651cb2014-12-11 08:52:58 +03008580 goto fail;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03008581 }
Wincy Vanff651cb2014-12-11 08:52:58 +03008582 }
8583 return 0;
8584fail:
8585 return i + 1;
8586}
8587
8588static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count)
8589{
8590 u32 i;
8591 struct vmx_msr_entry e;
8592
8593 for (i = 0; i < count; i++) {
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03008594 if (kvm_read_guest(vcpu->kvm,
8595 gpa + i * sizeof(e),
8596 &e, 2 * sizeof(u32))) {
8597 pr_warn_ratelimited(
8598 "%s cannot read MSR entry (%u, 0x%08llx)\n",
8599 __func__, i, gpa + i * sizeof(e));
Wincy Vanff651cb2014-12-11 08:52:58 +03008600 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03008601 }
8602 if (nested_vmx_store_msr_check(vcpu, &e)) {
8603 pr_warn_ratelimited(
8604 "%s check failed (%u, 0x%x, 0x%x)\n",
8605 __func__, i, e.index, e.reserved);
Wincy Vanff651cb2014-12-11 08:52:58 +03008606 return -EINVAL;
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03008607 }
8608 if (kvm_get_msr(vcpu, e.index, &e.value)) {
8609 pr_warn_ratelimited(
8610 "%s cannot read MSR (%u, 0x%x)\n",
8611 __func__, i, e.index);
8612 return -EINVAL;
8613 }
8614 if (kvm_write_guest(vcpu->kvm,
8615 gpa + i * sizeof(e) +
Wincy Vanff651cb2014-12-11 08:52:58 +03008616 offsetof(struct vmx_msr_entry, value),
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03008617 &e.value, sizeof(e.value))) {
8618 pr_warn_ratelimited(
8619 "%s cannot write MSR (%u, 0x%x, 0x%llx)\n",
8620 __func__, i, e.index, e.value);
8621 return -EINVAL;
8622 }
Wincy Vanff651cb2014-12-11 08:52:58 +03008623 }
8624 return 0;
8625}
8626
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008627/*
8628 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
8629 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
Tiejun Chenb4619662014-09-22 10:31:38 +08008630 * 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 +03008631 * guest in a way that will both be appropriate to L1's requests, and our
8632 * needs. In addition to modifying the active vmcs (which is vmcs02), this
8633 * function also has additional necessary side-effects, like setting various
8634 * vcpu->arch fields.
8635 */
8636static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
8637{
8638 struct vcpu_vmx *vmx = to_vmx(vcpu);
8639 u32 exec_control;
8640
8641 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
8642 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
8643 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
8644 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
8645 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
8646 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
8647 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
8648 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
8649 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
8650 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
8651 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
8652 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
8653 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
8654 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
8655 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
8656 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
8657 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
8658 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
8659 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
8660 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
8661 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
8662 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
8663 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
8664 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
8665 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
8666 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
8667 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
8668 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
8669 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
8670 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
8671 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
8672 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
8673 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
8674 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
8675 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
8676 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
8677
Jan Kiszka2996fca2014-06-16 13:59:43 +02008678 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
8679 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
8680 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
8681 } else {
8682 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
8683 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
8684 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008685 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
8686 vmcs12->vm_entry_intr_info_field);
8687 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
8688 vmcs12->vm_entry_exception_error_code);
8689 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
8690 vmcs12->vm_entry_instruction_len);
8691 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
8692 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008693 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +03008694 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008695 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
8696 vmcs12->guest_pending_dbg_exceptions);
8697 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
8698 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
8699
Wanpeng Li81dc01f2014-12-04 19:11:07 +08008700 if (nested_cpu_has_xsaves(vmcs12))
8701 vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008702 vmcs_write64(VMCS_LINK_POINTER, -1ull);
8703
Jan Kiszkaf4124502014-03-07 20:03:13 +01008704 exec_control = vmcs12->pin_based_vm_exec_control;
8705 exec_control |= vmcs_config.pin_based_exec_ctrl;
Paolo Bonzini696dfd92014-05-07 11:20:54 +02008706 exec_control &= ~(PIN_BASED_VMX_PREEMPTION_TIMER |
8707 PIN_BASED_POSTED_INTR);
Jan Kiszkaf4124502014-03-07 20:03:13 +01008708 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008709
Jan Kiszkaf4124502014-03-07 20:03:13 +01008710 vmx->nested.preemption_timer_expired = false;
8711 if (nested_cpu_has_preemption_timer(vmcs12))
8712 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +01008713
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008714 /*
8715 * Whether page-faults are trapped is determined by a combination of
8716 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
8717 * If enable_ept, L0 doesn't care about page faults and we should
8718 * set all of these to L1's desires. However, if !enable_ept, L0 does
8719 * care about (at least some) page faults, and because it is not easy
8720 * (if at all possible?) to merge L0 and L1's desires, we simply ask
8721 * to exit on each and every L2 page fault. This is done by setting
8722 * MASK=MATCH=0 and (see below) EB.PF=1.
8723 * Note that below we don't need special code to set EB.PF beyond the
8724 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
8725 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
8726 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
8727 *
8728 * A problem with this approach (when !enable_ept) is that L1 may be
8729 * injected with more page faults than it asked for. This could have
8730 * caused problems, but in practice existing hypervisors don't care.
8731 * To fix this, we will need to emulate the PFEC checking (on the L1
8732 * page tables), using walk_addr(), when injecting PFs to L1.
8733 */
8734 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
8735 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
8736 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
8737 enable_ept ? vmcs12->page_fault_error_code_match : 0);
8738
8739 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +01008740 exec_control = vmx_secondary_exec_control(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008741 if (!vmx->rdtscp_enabled)
8742 exec_control &= ~SECONDARY_EXEC_RDTSCP;
8743 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +02008744 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
8745 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
8746 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008747 if (nested_cpu_has(vmcs12,
8748 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
8749 exec_control |= vmcs12->secondary_vm_exec_control;
8750
8751 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
8752 /*
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008753 * If translation failed, no matter: This feature asks
8754 * to exit when accessing the given address, and if it
8755 * can never be accessed, this feature won't do
8756 * anything anyway.
8757 */
8758 if (!vmx->nested.apic_access_page)
8759 exec_control &=
8760 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
8761 else
8762 vmcs_write64(APIC_ACCESS_ADDR,
8763 page_to_phys(vmx->nested.apic_access_page));
Jan Kiszkaca3f2572013-12-16 12:55:46 +01008764 } else if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) {
8765 exec_control |=
8766 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Tang Chen38b99172014-09-24 15:57:54 +08008767 kvm_vcpu_reload_apic_access_page(vcpu);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008768 }
8769
8770 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
8771 }
8772
8773
8774 /*
8775 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
8776 * Some constant fields are set here by vmx_set_constant_host_state().
8777 * Other fields are different per CPU, and will be set later when
8778 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
8779 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08008780 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008781
8782 /*
8783 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
8784 * entry, but only if the current (host) sp changed from the value
8785 * we wrote last (vmx->host_rsp). This cache is no longer relevant
8786 * if we switch vmcs, and rather than hold a separate cache per vmcs,
8787 * here we just force the write to happen on entry.
8788 */
8789 vmx->host_rsp = 0;
8790
8791 exec_control = vmx_exec_control(vmx); /* L0's desires */
8792 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
8793 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
8794 exec_control &= ~CPU_BASED_TPR_SHADOW;
8795 exec_control |= vmcs12->cpu_based_vm_exec_control;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08008796
8797 if (exec_control & CPU_BASED_TPR_SHADOW) {
8798 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
8799 page_to_phys(vmx->nested.virtual_apic_page));
8800 vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold);
8801 }
8802
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008803 /*
8804 * Merging of IO and MSR bitmaps not currently supported.
8805 * Rather, exit every time.
8806 */
8807 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
8808 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
8809 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
8810
8811 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
8812
8813 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
8814 * bitwise-or of what L1 wants to trap for L2, and what we want to
8815 * trap. Note that CR0.TS also needs updating - we do this later.
8816 */
8817 update_exception_bitmap(vcpu);
8818 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
8819 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
8820
Nadav Har'El8049d652013-08-05 11:07:06 +03008821 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
8822 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
8823 * bits are further modified by vmx_set_efer() below.
8824 */
Jan Kiszkaf4124502014-03-07 20:03:13 +01008825 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +03008826
8827 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
8828 * emulated by vmx_set_efer(), below.
8829 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02008830 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +03008831 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
8832 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008833 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
8834
Jan Kiszka44811c02013-08-04 17:17:27 +02008835 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008836 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02008837 vcpu->arch.pat = vmcs12->guest_ia32_pat;
8838 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008839 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
8840
8841
8842 set_cr4_guest_host_mask(vmx);
8843
Paolo Bonzini36be0b92014-02-24 12:30:04 +01008844 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
8845 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
8846
Nadav Har'El27fc51b2011-08-02 15:54:52 +03008847 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
8848 vmcs_write64(TSC_OFFSET,
8849 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
8850 else
8851 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008852
8853 if (enable_vpid) {
8854 /*
8855 * Trivially support vpid by letting L2s share their parent
8856 * L1's vpid. TODO: move to a more elaborate solution, giving
8857 * each L2 its own vpid and exposing the vpid feature to L1.
8858 */
8859 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
8860 vmx_flush_tlb(vcpu);
8861 }
8862
Nadav Har'El155a97a2013-08-05 11:07:16 +03008863 if (nested_cpu_has_ept(vmcs12)) {
8864 kvm_mmu_unload(vcpu);
8865 nested_ept_init_mmu_context(vcpu);
8866 }
8867
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008868 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
8869 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +02008870 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008871 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
8872 else
8873 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
8874 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
8875 vmx_set_efer(vcpu, vcpu->arch.efer);
8876
8877 /*
8878 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
8879 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
8880 * The CR0_READ_SHADOW is what L2 should have expected to read given
8881 * the specifications by L1; It's not enough to take
8882 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
8883 * have more bits than L1 expected.
8884 */
8885 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
8886 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
8887
8888 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
8889 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
8890
8891 /* shadow page tables on either EPT or shadow page tables */
8892 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
8893 kvm_mmu_reset_context(vcpu);
8894
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03008895 if (!enable_ept)
8896 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
8897
Nadav Har'El3633cfc2013-08-05 11:07:07 +03008898 /*
8899 * L1 may access the L2's PDPTR, so save them to construct vmcs12
8900 */
8901 if (enable_ept) {
8902 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
8903 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
8904 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
8905 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
8906 }
8907
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008908 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
8909 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
8910}
8911
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008912/*
8913 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
8914 * for running an L2 nested guest.
8915 */
8916static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
8917{
8918 struct vmcs12 *vmcs12;
8919 struct vcpu_vmx *vmx = to_vmx(vcpu);
8920 int cpu;
8921 struct loaded_vmcs *vmcs02;
Jan Kiszka384bb782013-04-20 10:52:36 +02008922 bool ia32e;
Wincy Vanff651cb2014-12-11 08:52:58 +03008923 u32 msr_entry_idx;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008924
8925 if (!nested_vmx_check_permission(vcpu) ||
8926 !nested_vmx_check_vmcs12(vcpu))
8927 return 1;
8928
8929 skip_emulated_instruction(vcpu);
8930 vmcs12 = get_vmcs12(vcpu);
8931
Abel Gordon012f83c2013-04-18 14:39:25 +03008932 if (enable_shadow_vmcs)
8933 copy_shadow_to_vmcs12(vmx);
8934
Nadav Har'El7c177932011-05-25 23:12:04 +03008935 /*
8936 * The nested entry process starts with enforcing various prerequisites
8937 * on vmcs12 as required by the Intel SDM, and act appropriately when
8938 * they fail: As the SDM explains, some conditions should cause the
8939 * instruction to fail, while others will cause the instruction to seem
8940 * to succeed, but return an EXIT_REASON_INVALID_STATE.
8941 * To speed up the normal (success) code path, we should avoid checking
8942 * for misconfigurations which will anyway be caught by the processor
8943 * when using the merged vmcs02.
8944 */
8945 if (vmcs12->launch_state == launch) {
8946 nested_vmx_failValid(vcpu,
8947 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
8948 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
8949 return 1;
8950 }
8951
Jan Kiszka6dfacad2013-12-04 08:58:54 +01008952 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
8953 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +02008954 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8955 return 1;
8956 }
8957
Nadav Har'El7c177932011-05-25 23:12:04 +03008958 if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02008959 !PAGE_ALIGNED(vmcs12->msr_bitmap)) {
Nadav Har'El7c177932011-05-25 23:12:04 +03008960 /*TODO: Also verify bits beyond physical address width are 0*/
8961 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8962 return 1;
8963 }
8964
Wanpeng Lia2bcba52014-08-21 19:46:49 +08008965 if (!nested_get_vmcs12_pages(vcpu, vmcs12)) {
Nadav Har'El7c177932011-05-25 23:12:04 +03008966 /*TODO: Also verify bits beyond physical address width are 0*/
8967 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8968 return 1;
8969 }
8970
Eugene Korenevskye9ac0332014-12-11 08:53:27 +03008971 if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) {
8972 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8973 return 1;
8974 }
8975
Nadav Har'El7c177932011-05-25 23:12:04 +03008976 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Jan Kiszka3dcdf3ec2014-06-16 13:59:41 +02008977 nested_vmx_true_procbased_ctls_low,
8978 nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03008979 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
8980 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
8981 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
8982 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
8983 !vmx_control_verify(vmcs12->vm_exit_controls,
Jan Kiszka2996fca2014-06-16 13:59:43 +02008984 nested_vmx_true_exit_ctls_low,
8985 nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03008986 !vmx_control_verify(vmcs12->vm_entry_controls,
Jan Kiszka2996fca2014-06-16 13:59:43 +02008987 nested_vmx_true_entry_ctls_low,
8988 nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +03008989 {
8990 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8991 return 1;
8992 }
8993
8994 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
8995 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
8996 nested_vmx_failValid(vcpu,
8997 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
8998 return 1;
8999 }
9000
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02009001 if (!nested_cr0_valid(vmcs12, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03009002 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
9003 nested_vmx_entry_failure(vcpu, vmcs12,
9004 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
9005 return 1;
9006 }
9007 if (vmcs12->vmcs_link_pointer != -1ull) {
9008 nested_vmx_entry_failure(vcpu, vmcs12,
9009 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
9010 return 1;
9011 }
9012
9013 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +02009014 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +02009015 * are performed on the field for the IA32_EFER MSR:
9016 * - Bits reserved in the IA32_EFER MSR must be 0.
9017 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
9018 * the IA-32e mode guest VM-exit control. It must also be identical
9019 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
9020 * CR0.PG) is 1.
9021 */
9022 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
9023 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
9024 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
9025 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
9026 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
9027 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
9028 nested_vmx_entry_failure(vcpu, vmcs12,
9029 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
9030 return 1;
9031 }
9032 }
9033
9034 /*
9035 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
9036 * IA32_EFER MSR must be 0 in the field for that register. In addition,
9037 * the values of the LMA and LME bits in the field must each be that of
9038 * the host address-space size VM-exit control.
9039 */
9040 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
9041 ia32e = (vmcs12->vm_exit_controls &
9042 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
9043 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
9044 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
9045 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
9046 nested_vmx_entry_failure(vcpu, vmcs12,
9047 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
9048 return 1;
9049 }
9050 }
9051
9052 /*
Nadav Har'El7c177932011-05-25 23:12:04 +03009053 * We're finally done with prerequisite checking, and can start with
9054 * the nested entry.
9055 */
9056
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009057 vmcs02 = nested_get_current_vmcs02(vmx);
9058 if (!vmcs02)
9059 return -ENOMEM;
9060
9061 enter_guest_mode(vcpu);
9062
9063 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
9064
Jan Kiszka2996fca2014-06-16 13:59:43 +02009065 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
9066 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
9067
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009068 cpu = get_cpu();
9069 vmx->loaded_vmcs = vmcs02;
9070 vmx_vcpu_put(vcpu);
9071 vmx_vcpu_load(vcpu, cpu);
9072 vcpu->cpu = cpu;
9073 put_cpu();
9074
Jan Kiszka36c3cc42013-02-23 22:35:37 +01009075 vmx_segment_cache_clear(vmx);
9076
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009077 prepare_vmcs02(vcpu, vmcs12);
9078
Wincy Vanff651cb2014-12-11 08:52:58 +03009079 msr_entry_idx = nested_vmx_load_msr(vcpu,
9080 vmcs12->vm_entry_msr_load_addr,
9081 vmcs12->vm_entry_msr_load_count);
9082 if (msr_entry_idx) {
9083 leave_guest_mode(vcpu);
9084 vmx_load_vmcs01(vcpu);
9085 nested_vmx_entry_failure(vcpu, vmcs12,
9086 EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx);
9087 return 1;
9088 }
9089
9090 vmcs12->launch_state = 1;
9091
Jan Kiszka6dfacad2013-12-04 08:58:54 +01009092 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
9093 return kvm_emulate_halt(vcpu);
9094
Jan Kiszka7af40ad32014-01-04 18:47:23 +01009095 vmx->nested.nested_run_pending = 1;
9096
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03009097 /*
9098 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
9099 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
9100 * returned as far as L1 is concerned. It will only return (and set
9101 * the success flag) when L2 exits (see nested_vmx_vmexit()).
9102 */
9103 return 1;
9104}
9105
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009106/*
9107 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
9108 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
9109 * This function returns the new value we should put in vmcs12.guest_cr0.
9110 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
9111 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
9112 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
9113 * didn't trap the bit, because if L1 did, so would L0).
9114 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
9115 * been modified by L2, and L1 knows it. So just leave the old value of
9116 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
9117 * isn't relevant, because if L0 traps this bit it can set it to anything.
9118 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
9119 * changed these bits, and therefore they need to be updated, but L0
9120 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
9121 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
9122 */
9123static inline unsigned long
9124vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9125{
9126 return
9127 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
9128 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
9129 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
9130 vcpu->arch.cr0_guest_owned_bits));
9131}
9132
9133static inline unsigned long
9134vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
9135{
9136 return
9137 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
9138 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
9139 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
9140 vcpu->arch.cr4_guest_owned_bits));
9141}
9142
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009143static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
9144 struct vmcs12 *vmcs12)
9145{
9146 u32 idt_vectoring;
9147 unsigned int nr;
9148
Gleb Natapov851eb6672013-09-25 12:51:34 +03009149 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009150 nr = vcpu->arch.exception.nr;
9151 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
9152
9153 if (kvm_exception_is_soft(nr)) {
9154 vmcs12->vm_exit_instruction_len =
9155 vcpu->arch.event_exit_inst_len;
9156 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
9157 } else
9158 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
9159
9160 if (vcpu->arch.exception.has_error_code) {
9161 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
9162 vmcs12->idt_vectoring_error_code =
9163 vcpu->arch.exception.error_code;
9164 }
9165
9166 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +01009167 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009168 vmcs12->idt_vectoring_info_field =
9169 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
9170 } else if (vcpu->arch.interrupt.pending) {
9171 nr = vcpu->arch.interrupt.nr;
9172 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
9173
9174 if (vcpu->arch.interrupt.soft) {
9175 idt_vectoring |= INTR_TYPE_SOFT_INTR;
9176 vmcs12->vm_entry_instruction_len =
9177 vcpu->arch.event_exit_inst_len;
9178 } else
9179 idt_vectoring |= INTR_TYPE_EXT_INTR;
9180
9181 vmcs12->idt_vectoring_info_field = idt_vectoring;
9182 }
9183}
9184
Jan Kiszkab6b8a142014-03-07 20:03:12 +01009185static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
9186{
9187 struct vcpu_vmx *vmx = to_vmx(vcpu);
9188
Jan Kiszkaf4124502014-03-07 20:03:13 +01009189 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
9190 vmx->nested.preemption_timer_expired) {
9191 if (vmx->nested.nested_run_pending)
9192 return -EBUSY;
9193 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
9194 return 0;
9195 }
9196
Jan Kiszkab6b8a142014-03-07 20:03:12 +01009197 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +01009198 if (vmx->nested.nested_run_pending ||
9199 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01009200 return -EBUSY;
9201 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
9202 NMI_VECTOR | INTR_TYPE_NMI_INTR |
9203 INTR_INFO_VALID_MASK, 0);
9204 /*
9205 * The NMI-triggered VM exit counts as injection:
9206 * clear this one and block further NMIs.
9207 */
9208 vcpu->arch.nmi_pending = 0;
9209 vmx_set_nmi_mask(vcpu, true);
9210 return 0;
9211 }
9212
9213 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
9214 nested_exit_on_intr(vcpu)) {
9215 if (vmx->nested.nested_run_pending)
9216 return -EBUSY;
9217 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
9218 }
9219
9220 return 0;
9221}
9222
Jan Kiszkaf4124502014-03-07 20:03:13 +01009223static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
9224{
9225 ktime_t remaining =
9226 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
9227 u64 value;
9228
9229 if (ktime_to_ns(remaining) <= 0)
9230 return 0;
9231
9232 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
9233 do_div(value, 1000000);
9234 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
9235}
9236
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009237/*
9238 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
9239 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
9240 * and this function updates it to reflect the changes to the guest state while
9241 * L2 was running (and perhaps made some exits which were handled directly by L0
9242 * without going back to L1), and to reflect the exit reason.
9243 * Note that we do not have to copy here all VMCS fields, just those that
9244 * could have changed by the L2 guest or the exit - i.e., the guest-state and
9245 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
9246 * which already writes to vmcs12 directly.
9247 */
Jan Kiszka533558b2014-01-04 18:47:20 +01009248static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
9249 u32 exit_reason, u32 exit_intr_info,
9250 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009251{
9252 /* update guest state fields: */
9253 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
9254 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
9255
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009256 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
9257 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
9258 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
9259
9260 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
9261 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
9262 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
9263 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
9264 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
9265 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
9266 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
9267 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
9268 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
9269 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
9270 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
9271 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
9272 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
9273 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
9274 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
9275 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
9276 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
9277 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
9278 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
9279 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
9280 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
9281 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
9282 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
9283 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
9284 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
9285 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
9286 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
9287 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
9288 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
9289 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
9290 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
9291 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
9292 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
9293 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
9294 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
9295 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
9296
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009297 vmcs12->guest_interruptibility_info =
9298 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
9299 vmcs12->guest_pending_dbg_exceptions =
9300 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +01009301 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
9302 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
9303 else
9304 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009305
Jan Kiszkaf4124502014-03-07 20:03:13 +01009306 if (nested_cpu_has_preemption_timer(vmcs12)) {
9307 if (vmcs12->vm_exit_controls &
9308 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
9309 vmcs12->vmx_preemption_timer_value =
9310 vmx_get_preemption_timer_value(vcpu);
9311 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
9312 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +08009313
Nadav Har'El3633cfc2013-08-05 11:07:07 +03009314 /*
9315 * In some cases (usually, nested EPT), L2 is allowed to change its
9316 * own CR3 without exiting. If it has changed it, we must keep it.
9317 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
9318 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
9319 *
9320 * Additionally, restore L2's PDPTR to vmcs12.
9321 */
9322 if (enable_ept) {
9323 vmcs12->guest_cr3 = vmcs_read64(GUEST_CR3);
9324 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
9325 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
9326 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
9327 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
9328 }
9329
Jan Kiszkac18911a2013-03-13 16:06:41 +01009330 vmcs12->vm_entry_controls =
9331 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +02009332 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +01009333
Jan Kiszka2996fca2014-06-16 13:59:43 +02009334 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
9335 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
9336 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
9337 }
9338
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009339 /* TODO: These cannot have changed unless we have MSR bitmaps and
9340 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +02009341 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009342 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +02009343 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
9344 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009345 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
9346 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
9347 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzini36be0b92014-02-24 12:30:04 +01009348 if (vmx_mpx_supported())
9349 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Wanpeng Li81dc01f2014-12-04 19:11:07 +08009350 if (nested_cpu_has_xsaves(vmcs12))
9351 vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009352
9353 /* update exit information fields: */
9354
Jan Kiszka533558b2014-01-04 18:47:20 +01009355 vmcs12->vm_exit_reason = exit_reason;
9356 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009357
Jan Kiszka533558b2014-01-04 18:47:20 +01009358 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +02009359 if ((vmcs12->vm_exit_intr_info &
9360 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
9361 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
9362 vmcs12->vm_exit_intr_error_code =
9363 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009364 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009365 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
9366 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
9367
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009368 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
9369 /* vm_entry_intr_info_field is cleared on exit. Emulate this
9370 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009371 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009372
9373 /*
9374 * Transfer the event that L0 or L1 may wanted to inject into
9375 * L2 to IDT_VECTORING_INFO_FIELD.
9376 */
9377 vmcs12_save_pending_event(vcpu, vmcs12);
9378 }
9379
9380 /*
9381 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
9382 * preserved above and would only end up incorrectly in L1.
9383 */
9384 vcpu->arch.nmi_injected = false;
9385 kvm_clear_exception_queue(vcpu);
9386 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009387}
9388
9389/*
9390 * A part of what we need to when the nested L2 guest exits and we want to
9391 * run its L1 parent, is to reset L1's guest state to the host state specified
9392 * in vmcs12.
9393 * This function is to be called not only on normal nested exit, but also on
9394 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
9395 * Failures During or After Loading Guest State").
9396 * This function should be called when the active VMCS is L1's (vmcs01).
9397 */
Jan Kiszka733568f2013-02-23 15:07:47 +01009398static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
9399 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009400{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08009401 struct kvm_segment seg;
9402
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009403 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
9404 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +02009405 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009406 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
9407 else
9408 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
9409 vmx_set_efer(vcpu, vcpu->arch.efer);
9410
9411 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
9412 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -07009413 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009414 /*
9415 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
9416 * actually changed, because it depends on the current state of
9417 * fpu_active (which may have changed).
9418 * Note that vmx_set_cr0 refers to efer set above.
9419 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +02009420 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009421 /*
9422 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
9423 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
9424 * but we also need to update cr0_guest_host_mask and exception_bitmap.
9425 */
9426 update_exception_bitmap(vcpu);
9427 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
9428 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
9429
9430 /*
9431 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
9432 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
9433 */
9434 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
9435 kvm_set_cr4(vcpu, vmcs12->host_cr4);
9436
Jan Kiszka29bf08f2013-12-28 16:31:52 +01009437 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +03009438
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009439 kvm_set_cr3(vcpu, vmcs12->host_cr3);
9440 kvm_mmu_reset_context(vcpu);
9441
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03009442 if (!enable_ept)
9443 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
9444
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009445 if (enable_vpid) {
9446 /*
9447 * Trivially support vpid by letting L2s share their parent
9448 * L1's vpid. TODO: move to a more elaborate solution, giving
9449 * each L2 its own vpid and exposing the vpid feature to L1.
9450 */
9451 vmx_flush_tlb(vcpu);
9452 }
9453
9454
9455 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
9456 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
9457 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
9458 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
9459 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009460
Paolo Bonzini36be0b92014-02-24 12:30:04 +01009461 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
9462 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
9463 vmcs_write64(GUEST_BNDCFGS, 0);
9464
Jan Kiszka44811c02013-08-04 17:17:27 +02009465 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009466 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02009467 vcpu->arch.pat = vmcs12->host_ia32_pat;
9468 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009469 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
9470 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
9471 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +01009472
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08009473 /* Set L1 segment info according to Intel SDM
9474 27.5.2 Loading Host Segment and Descriptor-Table Registers */
9475 seg = (struct kvm_segment) {
9476 .base = 0,
9477 .limit = 0xFFFFFFFF,
9478 .selector = vmcs12->host_cs_selector,
9479 .type = 11,
9480 .present = 1,
9481 .s = 1,
9482 .g = 1
9483 };
9484 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
9485 seg.l = 1;
9486 else
9487 seg.db = 1;
9488 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
9489 seg = (struct kvm_segment) {
9490 .base = 0,
9491 .limit = 0xFFFFFFFF,
9492 .type = 3,
9493 .present = 1,
9494 .s = 1,
9495 .db = 1,
9496 .g = 1
9497 };
9498 seg.selector = vmcs12->host_ds_selector;
9499 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
9500 seg.selector = vmcs12->host_es_selector;
9501 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
9502 seg.selector = vmcs12->host_ss_selector;
9503 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
9504 seg.selector = vmcs12->host_fs_selector;
9505 seg.base = vmcs12->host_fs_base;
9506 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
9507 seg.selector = vmcs12->host_gs_selector;
9508 seg.base = vmcs12->host_gs_base;
9509 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
9510 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +03009511 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08009512 .limit = 0x67,
9513 .selector = vmcs12->host_tr_selector,
9514 .type = 11,
9515 .present = 1
9516 };
9517 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
9518
Jan Kiszka503cd0c2013-03-03 13:05:44 +01009519 kvm_set_dr(vcpu, 7, 0x400);
9520 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Wincy Vanff651cb2014-12-11 08:52:58 +03009521
9522 if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr,
9523 vmcs12->vm_exit_msr_load_count))
9524 nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009525}
9526
9527/*
9528 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
9529 * and modify vmcs12 to make it see what it would expect to see there if
9530 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
9531 */
Jan Kiszka533558b2014-01-04 18:47:20 +01009532static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
9533 u32 exit_intr_info,
9534 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009535{
9536 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009537 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
9538
Jan Kiszka5f3d5792013-04-14 12:12:46 +02009539 /* trying to cancel vmlaunch/vmresume is a bug */
9540 WARN_ON_ONCE(vmx->nested.nested_run_pending);
9541
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009542 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +01009543 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
9544 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009545
Wincy Vanff651cb2014-12-11 08:52:58 +03009546 if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr,
9547 vmcs12->vm_exit_msr_store_count))
9548 nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL);
9549
Wanpeng Lif3380ca2014-08-05 12:42:23 +08009550 vmx_load_vmcs01(vcpu);
9551
Bandan Das77b0f5d2014-04-19 18:17:45 -04009552 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
9553 && nested_exit_intr_ack_set(vcpu)) {
9554 int irq = kvm_cpu_get_interrupt(vcpu);
9555 WARN_ON(irq < 0);
9556 vmcs12->vm_exit_intr_info = irq |
9557 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
9558 }
9559
Jan Kiszka542060e2014-01-04 18:47:21 +01009560 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
9561 vmcs12->exit_qualification,
9562 vmcs12->idt_vectoring_info_field,
9563 vmcs12->vm_exit_intr_info,
9564 vmcs12->vm_exit_intr_error_code,
9565 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009566
Gleb Natapov2961e8762013-11-25 15:37:13 +02009567 vm_entry_controls_init(vmx, vmcs_read32(VM_ENTRY_CONTROLS));
9568 vm_exit_controls_init(vmx, vmcs_read32(VM_EXIT_CONTROLS));
Jan Kiszka36c3cc42013-02-23 22:35:37 +01009569 vmx_segment_cache_clear(vmx);
9570
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009571 /* if no vmcs02 cache requested, remove the one we used */
9572 if (VMCS02_POOL_SIZE == 0)
9573 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
9574
9575 load_vmcs12_host_state(vcpu, vmcs12);
9576
Nadav Har'El27fc51b2011-08-02 15:54:52 +03009577 /* Update TSC_OFFSET if TSC was changed while L2 ran */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009578 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
9579
9580 /* This is needed for same reason as it was needed in prepare_vmcs02 */
9581 vmx->host_rsp = 0;
9582
9583 /* Unpin physical memory we referred to in vmcs02 */
9584 if (vmx->nested.apic_access_page) {
9585 nested_release_page(vmx->nested.apic_access_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02009586 vmx->nested.apic_access_page = NULL;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009587 }
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009588 if (vmx->nested.virtual_apic_page) {
9589 nested_release_page(vmx->nested.virtual_apic_page);
Paolo Bonzini48d89b92014-08-26 13:27:46 +02009590 vmx->nested.virtual_apic_page = NULL;
Wanpeng Lia7c0b072014-08-21 19:46:50 +08009591 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009592
9593 /*
Tang Chen38b99172014-09-24 15:57:54 +08009594 * We are now running in L2, mmu_notifier will force to reload the
9595 * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1.
9596 */
9597 kvm_vcpu_reload_apic_access_page(vcpu);
9598
9599 /*
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009600 * Exiting from L2 to L1, we're now back to L1 which thinks it just
9601 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
9602 * success or failure flag accordingly.
9603 */
9604 if (unlikely(vmx->fail)) {
9605 vmx->fail = 0;
9606 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
9607 } else
9608 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +03009609 if (enable_shadow_vmcs)
9610 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +01009611
9612 /* in case we halted in L2 */
9613 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03009614}
9615
Nadav Har'El7c177932011-05-25 23:12:04 +03009616/*
Jan Kiszka42124922014-01-04 18:47:19 +01009617 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
9618 */
9619static void vmx_leave_nested(struct kvm_vcpu *vcpu)
9620{
9621 if (is_guest_mode(vcpu))
Jan Kiszka533558b2014-01-04 18:47:20 +01009622 nested_vmx_vmexit(vcpu, -1, 0, 0);
Jan Kiszka42124922014-01-04 18:47:19 +01009623 free_nested(to_vmx(vcpu));
9624}
9625
9626/*
Nadav Har'El7c177932011-05-25 23:12:04 +03009627 * L1's failure to enter L2 is a subset of a normal exit, as explained in
9628 * 23.7 "VM-entry failures during or after loading guest state" (this also
9629 * lists the acceptable exit-reason and exit-qualification parameters).
9630 * It should only be called before L2 actually succeeded to run, and when
9631 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
9632 */
9633static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
9634 struct vmcs12 *vmcs12,
9635 u32 reason, unsigned long qualification)
9636{
9637 load_vmcs12_host_state(vcpu, vmcs12);
9638 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
9639 vmcs12->exit_qualification = qualification;
9640 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +03009641 if (enable_shadow_vmcs)
9642 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +03009643}
9644
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02009645static int vmx_check_intercept(struct kvm_vcpu *vcpu,
9646 struct x86_instruction_info *info,
9647 enum x86_intercept_stage stage)
9648{
9649 return X86EMUL_CONTINUE;
9650}
9651
Paolo Bonzini48d89b92014-08-26 13:27:46 +02009652static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
Radim Krčmářae97a3b2014-08-21 18:08:06 +02009653{
Radim Krčmářb4a2d312014-08-21 18:08:08 +02009654 if (ple_gap)
9655 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02009656}
9657
Kai Huang843e4332015-01-28 10:54:28 +08009658static void vmx_slot_enable_log_dirty(struct kvm *kvm,
9659 struct kvm_memory_slot *slot)
9660{
9661 kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
9662 kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
9663}
9664
9665static void vmx_slot_disable_log_dirty(struct kvm *kvm,
9666 struct kvm_memory_slot *slot)
9667{
9668 kvm_mmu_slot_set_dirty(kvm, slot);
9669}
9670
9671static void vmx_flush_log_dirty(struct kvm *kvm)
9672{
9673 kvm_flush_pml_buffers(kvm);
9674}
9675
9676static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
9677 struct kvm_memory_slot *memslot,
9678 gfn_t offset, unsigned long mask)
9679{
9680 kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
9681}
9682
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03009683static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08009684 .cpu_has_kvm_support = cpu_has_kvm_support,
9685 .disabled_by_bios = vmx_disabled_by_bios,
9686 .hardware_setup = hardware_setup,
9687 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03009688 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009689 .hardware_enable = hardware_enable,
9690 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08009691 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009692
9693 .vcpu_create = vmx_create_vcpu,
9694 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03009695 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009696
Avi Kivity04d2cc72007-09-10 18:10:54 +03009697 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009698 .vcpu_load = vmx_vcpu_load,
9699 .vcpu_put = vmx_vcpu_put,
9700
Jan Kiszkac8639012012-09-21 05:42:55 +02009701 .update_db_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009702 .get_msr = vmx_get_msr,
9703 .set_msr = vmx_set_msr,
9704 .get_segment_base = vmx_get_segment_base,
9705 .get_segment = vmx_get_segment,
9706 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02009707 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009708 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02009709 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02009710 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03009711 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009712 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009713 .set_cr3 = vmx_set_cr3,
9714 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009715 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009716 .get_idt = vmx_get_idt,
9717 .set_idt = vmx_set_idt,
9718 .get_gdt = vmx_get_gdt,
9719 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01009720 .get_dr6 = vmx_get_dr6,
9721 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03009722 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01009723 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03009724 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009725 .get_rflags = vmx_get_rflags,
9726 .set_rflags = vmx_set_rflags,
Avi Kivity02daab22009-12-30 12:40:26 +02009727 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009728
9729 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009730
Avi Kivity6aa8b732006-12-10 02:21:36 -08009731 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02009732 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009733 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04009734 .set_interrupt_shadow = vmx_set_interrupt_shadow,
9735 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02009736 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03009737 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009738 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02009739 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03009740 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02009741 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009742 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01009743 .get_nmi_mask = vmx_get_nmi_mask,
9744 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009745 .enable_nmi_window = enable_nmi_window,
9746 .enable_irq_window = enable_irq_window,
9747 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08009748 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Tang Chen38b99172014-09-24 15:57:54 +08009749 .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
Yang Zhangc7c9c562013-01-25 10:18:51 +08009750 .vm_has_apicv = vmx_vm_has_apicv,
9751 .load_eoi_exitmap = vmx_load_eoi_exitmap,
9752 .hwapic_irr_update = vmx_hwapic_irr_update,
9753 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +08009754 .sync_pir_to_irr = vmx_sync_pir_to_irr,
9755 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Marcelo Tosatti7c6a98d2014-12-16 09:08:14 -05009756 .test_posted_interrupt = vmx_test_pir,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03009757
Izik Eiduscbc94022007-10-25 00:29:55 +02009758 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08009759 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08009760 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03009761
Avi Kivity586f9602010-11-18 13:09:54 +02009762 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02009763
Sheng Yang17cc3932010-01-05 19:02:27 +08009764 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08009765
9766 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08009767
9768 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00009769 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02009770
9771 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08009772
9773 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10009774
Joerg Roedel4051b182011-03-25 09:44:49 +01009775 .set_tsc_khz = vmx_set_tsc_khz,
Will Auldba904632012-11-29 12:42:50 -08009776 .read_tsc_offset = vmx_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10009777 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10009778 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01009779 .compute_tsc_offset = vmx_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03009780 .read_l1_tsc = vmx_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02009781
9782 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02009783
9784 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08009785 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009786 .mpx_supported = vmx_mpx_supported,
Wanpeng Li55412b22014-12-02 19:21:30 +08009787 .xsaves_supported = vmx_xsaves_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01009788
9789 .check_nested_events = vmx_check_nested_events,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02009790
9791 .sched_in = vmx_sched_in,
Kai Huang843e4332015-01-28 10:54:28 +08009792
9793 .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
9794 .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
9795 .flush_log_dirty = vmx_flush_log_dirty,
9796 .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
Avi Kivity6aa8b732006-12-10 02:21:36 -08009797};
9798
9799static int __init vmx_init(void)
9800{
Tiejun Chen34a1cd62014-10-28 10:14:48 +08009801 int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
9802 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03009803 if (r)
Tiejun Chen34a1cd62014-10-28 10:14:48 +08009804 return r;
Sheng Yang25c5f222008-03-28 13:18:56 +08009805
Zhang Yanfei8f536b72012-12-06 23:43:34 +08009806#ifdef CONFIG_KEXEC
9807 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
9808 crash_vmclear_local_loaded_vmcss);
9809#endif
9810
He, Qingfdef3ad2007-04-30 09:45:24 +03009811 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009812}
9813
9814static void __exit vmx_exit(void)
9815{
Zhang Yanfei8f536b72012-12-06 23:43:34 +08009816#ifdef CONFIG_KEXEC
Monam Agarwal3b63a432014-03-22 12:28:10 +05309817 RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08009818 synchronize_rcu();
9819#endif
9820
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08009821 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08009822}
9823
9824module_init(vmx_init)
9825module_exit(vmx_exit)