blob: 6a216e4762c707eb9b349af6a2453db4b00bb502 [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
Gleb Natapov50378782013-02-04 16:00:28 +0200102#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
103#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200104#define KVM_VM_CR0_ALWAYS_ON \
105 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200106#define KVM_CR4_GUEST_OWNED_BITS \
107 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
108 | X86_CR4_OSXMMEXCPT)
109
Avi Kivitycdc0e242009-12-06 17:21:14 +0200110#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
111#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
112
Avi Kivity78ac8b42010-04-08 18:19:35 +0300113#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
114
Jan Kiszkaf4124502014-03-07 20:03:13 +0100115#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
116
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800117/*
118 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
119 * ple_gap: upper bound on the amount of time between two successive
120 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500121 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800122 * ple_window: upper bound on the amount of time a guest is allowed to execute
123 * in a PAUSE loop. Tests indicate that most spinlocks are held for
124 * less than 2^12 cycles
125 * Time is measured based on a counter that runs at the same rate as the TSC,
126 * refer SDM volume 3b section 21.6.13 & 22.1.3.
127 */
Rik van Riel00c25bc2011-01-04 09:51:33 -0500128#define KVM_VMX_DEFAULT_PLE_GAP 128
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800129#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
130static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
131module_param(ple_gap, int, S_IRUGO);
132
133static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
134module_param(ple_window, int, S_IRUGO);
135
Avi Kivity83287ea422012-09-16 15:10:57 +0300136extern const ulong vmx_return;
137
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200138#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300139#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300140
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400141struct vmcs {
142 u32 revision_id;
143 u32 abort;
144 char data[0];
145};
146
Nadav Har'Eld462b812011-05-24 15:26:10 +0300147/*
148 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
149 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
150 * loaded on this CPU (so we can clear them if the CPU goes down).
151 */
152struct loaded_vmcs {
153 struct vmcs *vmcs;
154 int cpu;
155 int launched;
156 struct list_head loaded_vmcss_on_cpu_link;
157};
158
Avi Kivity26bb0982009-09-07 11:14:12 +0300159struct shared_msr_entry {
160 unsigned index;
161 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200162 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300163};
164
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300165/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300166 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
167 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
168 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
169 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
170 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
171 * More than one of these structures may exist, if L1 runs multiple L2 guests.
172 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
173 * underlying hardware which will be used to run L2.
174 * This structure is packed to ensure that its layout is identical across
175 * machines (necessary for live migration).
176 * If there are changes in this struct, VMCS12_REVISION must be changed.
177 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300178typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300179struct __packed vmcs12 {
180 /* According to the Intel spec, a VMCS region must start with the
181 * following two fields. Then follow implementation-specific data.
182 */
183 u32 revision_id;
184 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300185
Nadav Har'El27d6c862011-05-25 23:06:59 +0300186 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
187 u32 padding[7]; /* room for future expansion */
188
Nadav Har'El22bd0352011-05-25 23:05:57 +0300189 u64 io_bitmap_a;
190 u64 io_bitmap_b;
191 u64 msr_bitmap;
192 u64 vm_exit_msr_store_addr;
193 u64 vm_exit_msr_load_addr;
194 u64 vm_entry_msr_load_addr;
195 u64 tsc_offset;
196 u64 virtual_apic_page_addr;
197 u64 apic_access_addr;
198 u64 ept_pointer;
199 u64 guest_physical_address;
200 u64 vmcs_link_pointer;
201 u64 guest_ia32_debugctl;
202 u64 guest_ia32_pat;
203 u64 guest_ia32_efer;
204 u64 guest_ia32_perf_global_ctrl;
205 u64 guest_pdptr0;
206 u64 guest_pdptr1;
207 u64 guest_pdptr2;
208 u64 guest_pdptr3;
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100209 u64 guest_bndcfgs;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300210 u64 host_ia32_pat;
211 u64 host_ia32_efer;
212 u64 host_ia32_perf_global_ctrl;
213 u64 padding64[8]; /* room for future expansion */
214 /*
215 * To allow migration of L1 (complete with its L2 guests) between
216 * machines of different natural widths (32 or 64 bit), we cannot have
217 * unsigned long fields with no explict size. We use u64 (aliased
218 * natural_width) instead. Luckily, x86 is little-endian.
219 */
220 natural_width cr0_guest_host_mask;
221 natural_width cr4_guest_host_mask;
222 natural_width cr0_read_shadow;
223 natural_width cr4_read_shadow;
224 natural_width cr3_target_value0;
225 natural_width cr3_target_value1;
226 natural_width cr3_target_value2;
227 natural_width cr3_target_value3;
228 natural_width exit_qualification;
229 natural_width guest_linear_address;
230 natural_width guest_cr0;
231 natural_width guest_cr3;
232 natural_width guest_cr4;
233 natural_width guest_es_base;
234 natural_width guest_cs_base;
235 natural_width guest_ss_base;
236 natural_width guest_ds_base;
237 natural_width guest_fs_base;
238 natural_width guest_gs_base;
239 natural_width guest_ldtr_base;
240 natural_width guest_tr_base;
241 natural_width guest_gdtr_base;
242 natural_width guest_idtr_base;
243 natural_width guest_dr7;
244 natural_width guest_rsp;
245 natural_width guest_rip;
246 natural_width guest_rflags;
247 natural_width guest_pending_dbg_exceptions;
248 natural_width guest_sysenter_esp;
249 natural_width guest_sysenter_eip;
250 natural_width host_cr0;
251 natural_width host_cr3;
252 natural_width host_cr4;
253 natural_width host_fs_base;
254 natural_width host_gs_base;
255 natural_width host_tr_base;
256 natural_width host_gdtr_base;
257 natural_width host_idtr_base;
258 natural_width host_ia32_sysenter_esp;
259 natural_width host_ia32_sysenter_eip;
260 natural_width host_rsp;
261 natural_width host_rip;
262 natural_width paddingl[8]; /* room for future expansion */
263 u32 pin_based_vm_exec_control;
264 u32 cpu_based_vm_exec_control;
265 u32 exception_bitmap;
266 u32 page_fault_error_code_mask;
267 u32 page_fault_error_code_match;
268 u32 cr3_target_count;
269 u32 vm_exit_controls;
270 u32 vm_exit_msr_store_count;
271 u32 vm_exit_msr_load_count;
272 u32 vm_entry_controls;
273 u32 vm_entry_msr_load_count;
274 u32 vm_entry_intr_info_field;
275 u32 vm_entry_exception_error_code;
276 u32 vm_entry_instruction_len;
277 u32 tpr_threshold;
278 u32 secondary_vm_exec_control;
279 u32 vm_instruction_error;
280 u32 vm_exit_reason;
281 u32 vm_exit_intr_info;
282 u32 vm_exit_intr_error_code;
283 u32 idt_vectoring_info_field;
284 u32 idt_vectoring_error_code;
285 u32 vm_exit_instruction_len;
286 u32 vmx_instruction_info;
287 u32 guest_es_limit;
288 u32 guest_cs_limit;
289 u32 guest_ss_limit;
290 u32 guest_ds_limit;
291 u32 guest_fs_limit;
292 u32 guest_gs_limit;
293 u32 guest_ldtr_limit;
294 u32 guest_tr_limit;
295 u32 guest_gdtr_limit;
296 u32 guest_idtr_limit;
297 u32 guest_es_ar_bytes;
298 u32 guest_cs_ar_bytes;
299 u32 guest_ss_ar_bytes;
300 u32 guest_ds_ar_bytes;
301 u32 guest_fs_ar_bytes;
302 u32 guest_gs_ar_bytes;
303 u32 guest_ldtr_ar_bytes;
304 u32 guest_tr_ar_bytes;
305 u32 guest_interruptibility_info;
306 u32 guest_activity_state;
307 u32 guest_sysenter_cs;
308 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100309 u32 vmx_preemption_timer_value;
310 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300311 u16 virtual_processor_id;
312 u16 guest_es_selector;
313 u16 guest_cs_selector;
314 u16 guest_ss_selector;
315 u16 guest_ds_selector;
316 u16 guest_fs_selector;
317 u16 guest_gs_selector;
318 u16 guest_ldtr_selector;
319 u16 guest_tr_selector;
320 u16 host_es_selector;
321 u16 host_cs_selector;
322 u16 host_ss_selector;
323 u16 host_ds_selector;
324 u16 host_fs_selector;
325 u16 host_gs_selector;
326 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300327};
328
329/*
330 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
331 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
332 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
333 */
334#define VMCS12_REVISION 0x11e57ed0
335
336/*
337 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
338 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
339 * current implementation, 4K are reserved to avoid future complications.
340 */
341#define VMCS12_SIZE 0x1000
342
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300343/* Used to remember the last vmcs02 used for some recently used vmcs12s */
344struct vmcs02_list {
345 struct list_head list;
346 gpa_t vmptr;
347 struct loaded_vmcs vmcs02;
348};
349
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300350/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300351 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
352 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
353 */
354struct nested_vmx {
355 /* Has the level1 guest done vmxon? */
356 bool vmxon;
Bandan Das3573e222014-05-06 02:19:16 -0400357 gpa_t vmxon_ptr;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300358
359 /* The guest-physical address of the current VMCS L1 keeps for L2 */
360 gpa_t current_vmptr;
361 /* The host-usable pointer to the above */
362 struct page *current_vmcs12_page;
363 struct vmcs12 *current_vmcs12;
Abel Gordon8de48832013-04-18 14:37:25 +0300364 struct vmcs *current_shadow_vmcs;
Abel Gordon012f83c2013-04-18 14:39:25 +0300365 /*
366 * Indicates if the shadow vmcs must be updated with the
367 * data hold by vmcs12
368 */
369 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300370
371 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
372 struct list_head vmcs02_pool;
373 int vmcs02_num;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300374 u64 vmcs01_tsc_offset;
Nadav Har'El644d7112011-05-25 23:12:35 +0300375 /* L2 must run next, and mustn't decide to exit to L1. */
376 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300377 /*
378 * Guest pages referred to in vmcs02 with host-physical pointers, so
379 * we must keep them pinned while L2 runs.
380 */
381 struct page *apic_access_page;
Nadav Har'Elb3897a42013-07-08 19:12:35 +0800382 u64 msr_ia32_feature_control;
Jan Kiszkaf4124502014-03-07 20:03:13 +0100383
384 struct hrtimer preemption_timer;
385 bool preemption_timer_expired;
Jan Kiszka2996fca2014-06-16 13:59:43 +0200386
387 /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */
388 u64 vmcs01_debugctl;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300389};
390
Yang Zhang01e439b2013-04-11 19:25:12 +0800391#define POSTED_INTR_ON 0
392/* Posted-Interrupt Descriptor */
393struct pi_desc {
394 u32 pir[8]; /* Posted interrupt requested */
395 u32 control; /* bit 0 of control is outstanding notification bit */
396 u32 rsvd[7];
397} __aligned(64);
398
Yang Zhanga20ed542013-04-11 19:25:15 +0800399static bool pi_test_and_set_on(struct pi_desc *pi_desc)
400{
401 return test_and_set_bit(POSTED_INTR_ON,
402 (unsigned long *)&pi_desc->control);
403}
404
405static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
406{
407 return test_and_clear_bit(POSTED_INTR_ON,
408 (unsigned long *)&pi_desc->control);
409}
410
411static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
412{
413 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
414}
415
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400416struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000417 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300418 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300419 u8 fail;
Avi Kivity9d58b932011-03-07 16:52:07 +0200420 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300421 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200422 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200423 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300424 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400425 int nmsrs;
426 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800427 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400428#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300429 u64 msr_host_kernel_gs_base;
430 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400431#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200432 u32 vm_entry_controls_shadow;
433 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300434 /*
435 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
436 * non-nested (L1) guest, it always points to vmcs01. For a nested
437 * guest (L2), it points to a different VMCS.
438 */
439 struct loaded_vmcs vmcs01;
440 struct loaded_vmcs *loaded_vmcs;
441 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300442 struct msr_autoload {
443 unsigned nr;
444 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
445 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
446 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400447 struct {
448 int loaded;
449 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300450#ifdef CONFIG_X86_64
451 u16 ds_sel, es_sel;
452#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200453 int gs_ldt_reload_needed;
454 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000455 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400456 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200457 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300458 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300459 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300460 struct kvm_segment segs[8];
461 } rmode;
462 struct {
463 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300464 struct kvm_save_segment {
465 u16 selector;
466 unsigned long base;
467 u32 limit;
468 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300469 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300470 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800471 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300472 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200473
474 /* Support for vnmi-less CPUs */
475 int soft_vnmi_blocked;
476 ktime_t entry_time;
477 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800478 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800479
480 bool rdtscp_enabled;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300481
Yang Zhang01e439b2013-04-11 19:25:12 +0800482 /* Posted interrupt descriptor */
483 struct pi_desc pi_desc;
484
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300485 /* Support for a guest hypervisor (nested VMX) */
486 struct nested_vmx nested;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400487};
488
Avi Kivity2fb92db2011-04-27 19:42:18 +0300489enum segment_cache_field {
490 SEG_FIELD_SEL = 0,
491 SEG_FIELD_BASE = 1,
492 SEG_FIELD_LIMIT = 2,
493 SEG_FIELD_AR = 3,
494
495 SEG_FIELD_NR = 4
496};
497
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400498static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
499{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000500 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400501}
502
Nadav Har'El22bd0352011-05-25 23:05:57 +0300503#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
504#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
505#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
506 [number##_HIGH] = VMCS12_OFFSET(name)+4
507
Abel Gordon4607c2d2013-04-18 14:35:55 +0300508
Bandan Dasfe2b2012014-04-21 15:20:14 -0400509static unsigned long shadow_read_only_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300510 /*
511 * We do NOT shadow fields that are modified when L0
512 * traps and emulates any vmx instruction (e.g. VMPTRLD,
513 * VMXON...) executed by L1.
514 * For example, VM_INSTRUCTION_ERROR is read
515 * by L1 if a vmx instruction fails (part of the error path).
516 * Note the code assumes this logic. If for some reason
517 * we start shadowing these fields then we need to
518 * force a shadow sync when L0 emulates vmx instructions
519 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
520 * by nested_vmx_failValid)
521 */
522 VM_EXIT_REASON,
523 VM_EXIT_INTR_INFO,
524 VM_EXIT_INSTRUCTION_LEN,
525 IDT_VECTORING_INFO_FIELD,
526 IDT_VECTORING_ERROR_CODE,
527 VM_EXIT_INTR_ERROR_CODE,
528 EXIT_QUALIFICATION,
529 GUEST_LINEAR_ADDRESS,
530 GUEST_PHYSICAL_ADDRESS
531};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400532static int max_shadow_read_only_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300533 ARRAY_SIZE(shadow_read_only_fields);
534
Bandan Dasfe2b2012014-04-21 15:20:14 -0400535static unsigned long shadow_read_write_fields[] = {
Abel Gordon4607c2d2013-04-18 14:35:55 +0300536 GUEST_RIP,
537 GUEST_RSP,
538 GUEST_CR0,
539 GUEST_CR3,
540 GUEST_CR4,
541 GUEST_INTERRUPTIBILITY_INFO,
542 GUEST_RFLAGS,
543 GUEST_CS_SELECTOR,
544 GUEST_CS_AR_BYTES,
545 GUEST_CS_LIMIT,
546 GUEST_CS_BASE,
547 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100548 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300549 CR0_GUEST_HOST_MASK,
550 CR0_READ_SHADOW,
551 CR4_READ_SHADOW,
552 TSC_OFFSET,
553 EXCEPTION_BITMAP,
554 CPU_BASED_VM_EXEC_CONTROL,
555 VM_ENTRY_EXCEPTION_ERROR_CODE,
556 VM_ENTRY_INTR_INFO_FIELD,
557 VM_ENTRY_INSTRUCTION_LEN,
558 VM_ENTRY_EXCEPTION_ERROR_CODE,
559 HOST_FS_BASE,
560 HOST_GS_BASE,
561 HOST_FS_SELECTOR,
562 HOST_GS_SELECTOR
563};
Bandan Dasfe2b2012014-04-21 15:20:14 -0400564static int max_shadow_read_write_fields =
Abel Gordon4607c2d2013-04-18 14:35:55 +0300565 ARRAY_SIZE(shadow_read_write_fields);
566
Mathias Krause772e0312012-08-30 01:30:19 +0200567static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300568 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
569 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
570 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
571 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
572 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
573 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
574 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
575 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
576 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
577 FIELD(HOST_ES_SELECTOR, host_es_selector),
578 FIELD(HOST_CS_SELECTOR, host_cs_selector),
579 FIELD(HOST_SS_SELECTOR, host_ss_selector),
580 FIELD(HOST_DS_SELECTOR, host_ds_selector),
581 FIELD(HOST_FS_SELECTOR, host_fs_selector),
582 FIELD(HOST_GS_SELECTOR, host_gs_selector),
583 FIELD(HOST_TR_SELECTOR, host_tr_selector),
584 FIELD64(IO_BITMAP_A, io_bitmap_a),
585 FIELD64(IO_BITMAP_B, io_bitmap_b),
586 FIELD64(MSR_BITMAP, msr_bitmap),
587 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
588 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
589 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
590 FIELD64(TSC_OFFSET, tsc_offset),
591 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
592 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
593 FIELD64(EPT_POINTER, ept_pointer),
594 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
595 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
596 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
597 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
598 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
599 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
600 FIELD64(GUEST_PDPTR0, guest_pdptr0),
601 FIELD64(GUEST_PDPTR1, guest_pdptr1),
602 FIELD64(GUEST_PDPTR2, guest_pdptr2),
603 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100604 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300605 FIELD64(HOST_IA32_PAT, host_ia32_pat),
606 FIELD64(HOST_IA32_EFER, host_ia32_efer),
607 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
608 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
609 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
610 FIELD(EXCEPTION_BITMAP, exception_bitmap),
611 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
612 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
613 FIELD(CR3_TARGET_COUNT, cr3_target_count),
614 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
615 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
616 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
617 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
618 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
619 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
620 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
621 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
622 FIELD(TPR_THRESHOLD, tpr_threshold),
623 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
624 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
625 FIELD(VM_EXIT_REASON, vm_exit_reason),
626 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
627 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
628 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
629 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
630 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
631 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
632 FIELD(GUEST_ES_LIMIT, guest_es_limit),
633 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
634 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
635 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
636 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
637 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
638 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
639 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
640 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
641 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
642 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
643 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
644 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
645 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
646 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
647 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
648 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
649 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
650 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
651 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
652 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
653 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100654 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300655 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
656 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
657 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
658 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
659 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
660 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
661 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
662 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
663 FIELD(EXIT_QUALIFICATION, exit_qualification),
664 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
665 FIELD(GUEST_CR0, guest_cr0),
666 FIELD(GUEST_CR3, guest_cr3),
667 FIELD(GUEST_CR4, guest_cr4),
668 FIELD(GUEST_ES_BASE, guest_es_base),
669 FIELD(GUEST_CS_BASE, guest_cs_base),
670 FIELD(GUEST_SS_BASE, guest_ss_base),
671 FIELD(GUEST_DS_BASE, guest_ds_base),
672 FIELD(GUEST_FS_BASE, guest_fs_base),
673 FIELD(GUEST_GS_BASE, guest_gs_base),
674 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
675 FIELD(GUEST_TR_BASE, guest_tr_base),
676 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
677 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
678 FIELD(GUEST_DR7, guest_dr7),
679 FIELD(GUEST_RSP, guest_rsp),
680 FIELD(GUEST_RIP, guest_rip),
681 FIELD(GUEST_RFLAGS, guest_rflags),
682 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
683 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
684 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
685 FIELD(HOST_CR0, host_cr0),
686 FIELD(HOST_CR3, host_cr3),
687 FIELD(HOST_CR4, host_cr4),
688 FIELD(HOST_FS_BASE, host_fs_base),
689 FIELD(HOST_GS_BASE, host_gs_base),
690 FIELD(HOST_TR_BASE, host_tr_base),
691 FIELD(HOST_GDTR_BASE, host_gdtr_base),
692 FIELD(HOST_IDTR_BASE, host_idtr_base),
693 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
694 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
695 FIELD(HOST_RSP, host_rsp),
696 FIELD(HOST_RIP, host_rip),
697};
698static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table);
699
700static inline short vmcs_field_to_offset(unsigned long field)
701{
702 if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0)
703 return -1;
704 return vmcs_field_to_offset_table[field];
705}
706
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300707static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
708{
709 return to_vmx(vcpu)->nested.current_vmcs12;
710}
711
712static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
713{
714 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800715 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300716 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800717
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300718 return page;
719}
720
721static void nested_release_page(struct page *page)
722{
723 kvm_release_page_dirty(page);
724}
725
726static void nested_release_page_clean(struct page *page)
727{
728 kvm_release_page_clean(page);
729}
730
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300731static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800732static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800733static void kvm_cpu_vmxon(u64 addr);
734static void kvm_cpu_vmxoff(void);
Paolo Bonzini93c4adc2014-03-05 23:19:52 +0100735static bool vmx_mpx_supported(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200736static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300737static void vmx_set_segment(struct kvm_vcpu *vcpu,
738 struct kvm_segment *var, int seg);
739static void vmx_get_segment(struct kvm_vcpu *vcpu,
740 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200741static bool guest_state_valid(struct kvm_vcpu *vcpu);
742static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yang Zhanga20ed542013-04-11 19:25:15 +0800743static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu);
Abel Gordonc3114422013-04-18 14:38:55 +0300744static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300745static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Avi Kivity75880a02007-06-20 11:20:04 +0300746
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747static DEFINE_PER_CPU(struct vmcs *, vmxarea);
748static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300749/*
750 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
751 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
752 */
753static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300754static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200756static unsigned long *vmx_io_bitmap_a;
757static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200758static unsigned long *vmx_msr_bitmap_legacy;
759static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800760static unsigned long *vmx_msr_bitmap_legacy_x2apic;
761static unsigned long *vmx_msr_bitmap_longmode_x2apic;
Abel Gordon4607c2d2013-04-18 14:35:55 +0300762static unsigned long *vmx_vmread_bitmap;
763static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300764
Avi Kivity110312c2010-12-21 12:54:20 +0200765static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200766static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200767
Sheng Yang2384d2b2008-01-17 15:14:33 +0800768static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
769static DEFINE_SPINLOCK(vmx_vpid_lock);
770
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300771static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772 int size;
773 int order;
774 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300775 u32 pin_based_exec_ctrl;
776 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800777 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300778 u32 vmexit_ctrl;
779 u32 vmentry_ctrl;
780} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781
Hannes Ederefff9e52008-11-28 17:02:06 +0100782static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800783 u32 ept;
784 u32 vpid;
785} vmx_capability;
786
Avi Kivity6aa8b732006-12-10 02:21:36 -0800787#define VMX_SEGMENT_FIELD(seg) \
788 [VCPU_SREG_##seg] = { \
789 .selector = GUEST_##seg##_SELECTOR, \
790 .base = GUEST_##seg##_BASE, \
791 .limit = GUEST_##seg##_LIMIT, \
792 .ar_bytes = GUEST_##seg##_AR_BYTES, \
793 }
794
Mathias Krause772e0312012-08-30 01:30:19 +0200795static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 unsigned selector;
797 unsigned base;
798 unsigned limit;
799 unsigned ar_bytes;
800} kvm_vmx_segment_fields[] = {
801 VMX_SEGMENT_FIELD(CS),
802 VMX_SEGMENT_FIELD(DS),
803 VMX_SEGMENT_FIELD(ES),
804 VMX_SEGMENT_FIELD(FS),
805 VMX_SEGMENT_FIELD(GS),
806 VMX_SEGMENT_FIELD(SS),
807 VMX_SEGMENT_FIELD(TR),
808 VMX_SEGMENT_FIELD(LDTR),
809};
810
Avi Kivity26bb0982009-09-07 11:14:12 +0300811static u64 host_efer;
812
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300813static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
814
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300815/*
Brian Gerst8c065852010-07-17 09:03:26 -0400816 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300817 * away by decrementing the array size.
818 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800820#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300821 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400823 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825
Gui Jianfeng31299942010-03-15 17:29:09 +0800826static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800827{
828 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
829 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100830 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800831}
832
Gui Jianfeng31299942010-03-15 17:29:09 +0800833static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300834{
835 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
836 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100837 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300838}
839
Gui Jianfeng31299942010-03-15 17:29:09 +0800840static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500841{
842 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
843 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100844 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500845}
846
Gui Jianfeng31299942010-03-15 17:29:09 +0800847static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800848{
849 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
850 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
851}
852
Gui Jianfeng31299942010-03-15 17:29:09 +0800853static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800854{
855 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
856 INTR_INFO_VALID_MASK)) ==
857 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
858}
859
Gui Jianfeng31299942010-03-15 17:29:09 +0800860static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800861{
Sheng Yang04547152009-04-01 15:52:31 +0800862 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800863}
864
Gui Jianfeng31299942010-03-15 17:29:09 +0800865static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800866{
Sheng Yang04547152009-04-01 15:52:31 +0800867 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800868}
869
Gui Jianfeng31299942010-03-15 17:29:09 +0800870static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800871{
Sheng Yang04547152009-04-01 15:52:31 +0800872 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800873}
874
Gui Jianfeng31299942010-03-15 17:29:09 +0800875static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800876{
Sheng Yang04547152009-04-01 15:52:31 +0800877 return vmcs_config.cpu_based_exec_ctrl &
878 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800879}
880
Avi Kivity774ead32007-12-26 13:57:04 +0200881static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800882{
Sheng Yang04547152009-04-01 15:52:31 +0800883 return vmcs_config.cpu_based_2nd_exec_ctrl &
884 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
885}
886
Yang Zhang8d146952013-01-25 10:18:50 +0800887static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
888{
889 return vmcs_config.cpu_based_2nd_exec_ctrl &
890 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
891}
892
Yang Zhang83d4c282013-01-25 10:18:49 +0800893static inline bool cpu_has_vmx_apic_register_virt(void)
894{
895 return vmcs_config.cpu_based_2nd_exec_ctrl &
896 SECONDARY_EXEC_APIC_REGISTER_VIRT;
897}
898
Yang Zhangc7c9c562013-01-25 10:18:51 +0800899static inline bool cpu_has_vmx_virtual_intr_delivery(void)
900{
901 return vmcs_config.cpu_based_2nd_exec_ctrl &
902 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
903}
904
Yang Zhang01e439b2013-04-11 19:25:12 +0800905static inline bool cpu_has_vmx_posted_intr(void)
906{
907 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
908}
909
910static inline bool cpu_has_vmx_apicv(void)
911{
912 return cpu_has_vmx_apic_register_virt() &&
913 cpu_has_vmx_virtual_intr_delivery() &&
914 cpu_has_vmx_posted_intr();
915}
916
Sheng Yang04547152009-04-01 15:52:31 +0800917static inline bool cpu_has_vmx_flexpriority(void)
918{
919 return cpu_has_vmx_tpr_shadow() &&
920 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800921}
922
Marcelo Tosattie7997942009-06-11 12:07:40 -0300923static inline bool cpu_has_vmx_ept_execute_only(void)
924{
Gui Jianfeng31299942010-03-15 17:29:09 +0800925 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300926}
927
928static inline bool cpu_has_vmx_eptp_uncacheable(void)
929{
Gui Jianfeng31299942010-03-15 17:29:09 +0800930 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300931}
932
933static inline bool cpu_has_vmx_eptp_writeback(void)
934{
Gui Jianfeng31299942010-03-15 17:29:09 +0800935 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300936}
937
938static inline bool cpu_has_vmx_ept_2m_page(void)
939{
Gui Jianfeng31299942010-03-15 17:29:09 +0800940 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300941}
942
Sheng Yang878403b2010-01-05 19:02:29 +0800943static inline bool cpu_has_vmx_ept_1g_page(void)
944{
Gui Jianfeng31299942010-03-15 17:29:09 +0800945 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800946}
947
Sheng Yang4bc9b982010-06-02 14:05:24 +0800948static inline bool cpu_has_vmx_ept_4levels(void)
949{
950 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
951}
952
Xudong Hao83c3a332012-05-28 19:33:35 +0800953static inline bool cpu_has_vmx_ept_ad_bits(void)
954{
955 return vmx_capability.ept & VMX_EPT_AD_BIT;
956}
957
Gui Jianfeng31299942010-03-15 17:29:09 +0800958static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800959{
Gui Jianfeng31299942010-03-15 17:29:09 +0800960 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800961}
962
Gui Jianfeng31299942010-03-15 17:29:09 +0800963static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800964{
Gui Jianfeng31299942010-03-15 17:29:09 +0800965 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800966}
967
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800968static inline bool cpu_has_vmx_invvpid_single(void)
969{
970 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
971}
972
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800973static inline bool cpu_has_vmx_invvpid_global(void)
974{
975 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
976}
977
Gui Jianfeng31299942010-03-15 17:29:09 +0800978static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800979{
Sheng Yang04547152009-04-01 15:52:31 +0800980 return vmcs_config.cpu_based_2nd_exec_ctrl &
981 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800982}
983
Gui Jianfeng31299942010-03-15 17:29:09 +0800984static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700985{
986 return vmcs_config.cpu_based_2nd_exec_ctrl &
987 SECONDARY_EXEC_UNRESTRICTED_GUEST;
988}
989
Gui Jianfeng31299942010-03-15 17:29:09 +0800990static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800991{
992 return vmcs_config.cpu_based_2nd_exec_ctrl &
993 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
994}
995
Gui Jianfeng31299942010-03-15 17:29:09 +0800996static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800997{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800998 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800999}
1000
Gui Jianfeng31299942010-03-15 17:29:09 +08001001static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001002{
Sheng Yang04547152009-04-01 15:52:31 +08001003 return vmcs_config.cpu_based_2nd_exec_ctrl &
1004 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001005}
1006
Gui Jianfeng31299942010-03-15 17:29:09 +08001007static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001008{
1009 return vmcs_config.cpu_based_2nd_exec_ctrl &
1010 SECONDARY_EXEC_RDTSCP;
1011}
1012
Mao, Junjiead756a12012-07-02 01:18:48 +00001013static inline bool cpu_has_vmx_invpcid(void)
1014{
1015 return vmcs_config.cpu_based_2nd_exec_ctrl &
1016 SECONDARY_EXEC_ENABLE_INVPCID;
1017}
1018
Gui Jianfeng31299942010-03-15 17:29:09 +08001019static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001020{
1021 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1022}
1023
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001024static inline bool cpu_has_vmx_wbinvd_exit(void)
1025{
1026 return vmcs_config.cpu_based_2nd_exec_ctrl &
1027 SECONDARY_EXEC_WBINVD_EXITING;
1028}
1029
Abel Gordonabc4fc52013-04-18 14:35:25 +03001030static inline bool cpu_has_vmx_shadow_vmcs(void)
1031{
1032 u64 vmx_msr;
1033 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1034 /* check if the cpu supports writing r/o exit information fields */
1035 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1036 return false;
1037
1038 return vmcs_config.cpu_based_2nd_exec_ctrl &
1039 SECONDARY_EXEC_SHADOW_VMCS;
1040}
1041
Sheng Yang04547152009-04-01 15:52:31 +08001042static inline bool report_flexpriority(void)
1043{
1044 return flexpriority_enabled;
1045}
1046
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001047static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1048{
1049 return vmcs12->cpu_based_vm_exec_control & bit;
1050}
1051
1052static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1053{
1054 return (vmcs12->cpu_based_vm_exec_control &
1055 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1056 (vmcs12->secondary_vm_exec_control & bit);
1057}
1058
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001059static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001060{
1061 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1062}
1063
Jan Kiszkaf4124502014-03-07 20:03:13 +01001064static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1065{
1066 return vmcs12->pin_based_vm_exec_control &
1067 PIN_BASED_VMX_PREEMPTION_TIMER;
1068}
1069
Nadav Har'El155a97a2013-08-05 11:07:16 +03001070static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1071{
1072 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1073}
1074
Nadav Har'El644d7112011-05-25 23:12:35 +03001075static inline bool is_exception(u32 intr_info)
1076{
1077 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1078 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
1079}
1080
Jan Kiszka533558b2014-01-04 18:47:20 +01001081static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1082 u32 exit_intr_info,
1083 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001084static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1085 struct vmcs12 *vmcs12,
1086 u32 reason, unsigned long qualification);
1087
Rusty Russell8b9cf982007-07-30 16:31:43 +10001088static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001089{
1090 int i;
1091
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001092 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001093 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001094 return i;
1095 return -1;
1096}
1097
Sheng Yang2384d2b2008-01-17 15:14:33 +08001098static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1099{
1100 struct {
1101 u64 vpid : 16;
1102 u64 rsvd : 48;
1103 u64 gva;
1104 } operand = { vpid, 0, gva };
1105
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001106 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001107 /* CF==1 or ZF==1 --> rc = -1 */
1108 "; ja 1f ; ud2 ; 1:"
1109 : : "a"(&operand), "c"(ext) : "cc", "memory");
1110}
1111
Sheng Yang14394422008-04-28 12:24:45 +08001112static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1113{
1114 struct {
1115 u64 eptp, gpa;
1116 } operand = {eptp, gpa};
1117
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001118 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001119 /* CF==1 or ZF==1 --> rc = -1 */
1120 "; ja 1f ; ud2 ; 1:\n"
1121 : : "a" (&operand), "c" (ext) : "cc", "memory");
1122}
1123
Avi Kivity26bb0982009-09-07 11:14:12 +03001124static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001125{
1126 int i;
1127
Rusty Russell8b9cf982007-07-30 16:31:43 +10001128 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001129 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001130 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001131 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001132}
1133
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134static void vmcs_clear(struct vmcs *vmcs)
1135{
1136 u64 phys_addr = __pa(vmcs);
1137 u8 error;
1138
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001139 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001140 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001141 : "cc", "memory");
1142 if (error)
1143 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1144 vmcs, phys_addr);
1145}
1146
Nadav Har'Eld462b812011-05-24 15:26:10 +03001147static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1148{
1149 vmcs_clear(loaded_vmcs->vmcs);
1150 loaded_vmcs->cpu = -1;
1151 loaded_vmcs->launched = 0;
1152}
1153
Dongxiao Xu7725b892010-05-11 18:29:38 +08001154static void vmcs_load(struct vmcs *vmcs)
1155{
1156 u64 phys_addr = __pa(vmcs);
1157 u8 error;
1158
1159 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001160 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001161 : "cc", "memory");
1162 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001163 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001164 vmcs, phys_addr);
1165}
1166
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001167#ifdef CONFIG_KEXEC
1168/*
1169 * This bitmap is used to indicate whether the vmclear
1170 * operation is enabled on all cpus. All disabled by
1171 * default.
1172 */
1173static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1174
1175static inline void crash_enable_local_vmclear(int cpu)
1176{
1177 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1178}
1179
1180static inline void crash_disable_local_vmclear(int cpu)
1181{
1182 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1183}
1184
1185static inline int crash_local_vmclear_enabled(int cpu)
1186{
1187 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1188}
1189
1190static void crash_vmclear_local_loaded_vmcss(void)
1191{
1192 int cpu = raw_smp_processor_id();
1193 struct loaded_vmcs *v;
1194
1195 if (!crash_local_vmclear_enabled(cpu))
1196 return;
1197
1198 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1199 loaded_vmcss_on_cpu_link)
1200 vmcs_clear(v->vmcs);
1201}
1202#else
1203static inline void crash_enable_local_vmclear(int cpu) { }
1204static inline void crash_disable_local_vmclear(int cpu) { }
1205#endif /* CONFIG_KEXEC */
1206
Nadav Har'Eld462b812011-05-24 15:26:10 +03001207static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001209 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001210 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211
Nadav Har'Eld462b812011-05-24 15:26:10 +03001212 if (loaded_vmcs->cpu != cpu)
1213 return; /* vcpu migration can race with cpu offline */
1214 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001215 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001216 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001217 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001218
1219 /*
1220 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1221 * is before setting loaded_vmcs->vcpu to -1 which is done in
1222 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1223 * then adds the vmcs into percpu list before it is deleted.
1224 */
1225 smp_wmb();
1226
Nadav Har'Eld462b812011-05-24 15:26:10 +03001227 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001228 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229}
1230
Nadav Har'Eld462b812011-05-24 15:26:10 +03001231static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001232{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001233 int cpu = loaded_vmcs->cpu;
1234
1235 if (cpu != -1)
1236 smp_call_function_single(cpu,
1237 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001238}
1239
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001240static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001241{
1242 if (vmx->vpid == 0)
1243 return;
1244
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001245 if (cpu_has_vmx_invvpid_single())
1246 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001247}
1248
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001249static inline void vpid_sync_vcpu_global(void)
1250{
1251 if (cpu_has_vmx_invvpid_global())
1252 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1253}
1254
1255static inline void vpid_sync_context(struct vcpu_vmx *vmx)
1256{
1257 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001258 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001259 else
1260 vpid_sync_vcpu_global();
1261}
1262
Sheng Yang14394422008-04-28 12:24:45 +08001263static inline void ept_sync_global(void)
1264{
1265 if (cpu_has_vmx_invept_global())
1266 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1267}
1268
1269static inline void ept_sync_context(u64 eptp)
1270{
Avi Kivity089d0342009-03-23 18:26:32 +02001271 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001272 if (cpu_has_vmx_invept_context())
1273 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1274 else
1275 ept_sync_global();
1276 }
1277}
1278
Avi Kivity96304212011-05-15 10:13:13 -04001279static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280{
Avi Kivity5e520e62011-05-15 10:13:12 -04001281 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282
Avi Kivity5e520e62011-05-15 10:13:12 -04001283 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1284 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285 return value;
1286}
1287
Avi Kivity96304212011-05-15 10:13:13 -04001288static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289{
1290 return vmcs_readl(field);
1291}
1292
Avi Kivity96304212011-05-15 10:13:13 -04001293static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294{
1295 return vmcs_readl(field);
1296}
1297
Avi Kivity96304212011-05-15 10:13:13 -04001298static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299{
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001300#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301 return vmcs_readl(field);
1302#else
1303 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
1304#endif
1305}
1306
Avi Kivitye52de1b2007-01-05 16:36:56 -08001307static noinline void vmwrite_error(unsigned long field, unsigned long value)
1308{
1309 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1310 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1311 dump_stack();
1312}
1313
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314static void vmcs_writel(unsigned long field, unsigned long value)
1315{
1316 u8 error;
1317
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001318 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001319 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001320 if (unlikely(error))
1321 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322}
1323
1324static void vmcs_write16(unsigned long field, u16 value)
1325{
1326 vmcs_writel(field, value);
1327}
1328
1329static void vmcs_write32(unsigned long field, u32 value)
1330{
1331 vmcs_writel(field, value);
1332}
1333
1334static void vmcs_write64(unsigned long field, u64 value)
1335{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001337#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338 asm volatile ("");
1339 vmcs_writel(field+1, value >> 32);
1340#endif
1341}
1342
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001343static void vmcs_clear_bits(unsigned long field, u32 mask)
1344{
1345 vmcs_writel(field, vmcs_readl(field) & ~mask);
1346}
1347
1348static void vmcs_set_bits(unsigned long field, u32 mask)
1349{
1350 vmcs_writel(field, vmcs_readl(field) | mask);
1351}
1352
Gleb Natapov2961e8762013-11-25 15:37:13 +02001353static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1354{
1355 vmcs_write32(VM_ENTRY_CONTROLS, val);
1356 vmx->vm_entry_controls_shadow = val;
1357}
1358
1359static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1360{
1361 if (vmx->vm_entry_controls_shadow != val)
1362 vm_entry_controls_init(vmx, val);
1363}
1364
1365static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1366{
1367 return vmx->vm_entry_controls_shadow;
1368}
1369
1370
1371static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1372{
1373 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1374}
1375
1376static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1377{
1378 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1379}
1380
1381static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1382{
1383 vmcs_write32(VM_EXIT_CONTROLS, val);
1384 vmx->vm_exit_controls_shadow = val;
1385}
1386
1387static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1388{
1389 if (vmx->vm_exit_controls_shadow != val)
1390 vm_exit_controls_init(vmx, val);
1391}
1392
1393static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1394{
1395 return vmx->vm_exit_controls_shadow;
1396}
1397
1398
1399static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1400{
1401 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1402}
1403
1404static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1405{
1406 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1407}
1408
Avi Kivity2fb92db2011-04-27 19:42:18 +03001409static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1410{
1411 vmx->segment_cache.bitmask = 0;
1412}
1413
1414static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1415 unsigned field)
1416{
1417 bool ret;
1418 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1419
1420 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1421 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1422 vmx->segment_cache.bitmask = 0;
1423 }
1424 ret = vmx->segment_cache.bitmask & mask;
1425 vmx->segment_cache.bitmask |= mask;
1426 return ret;
1427}
1428
1429static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1430{
1431 u16 *p = &vmx->segment_cache.seg[seg].selector;
1432
1433 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1434 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1435 return *p;
1436}
1437
1438static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1439{
1440 ulong *p = &vmx->segment_cache.seg[seg].base;
1441
1442 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1443 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1444 return *p;
1445}
1446
1447static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1448{
1449 u32 *p = &vmx->segment_cache.seg[seg].limit;
1450
1451 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1452 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1453 return *p;
1454}
1455
1456static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1457{
1458 u32 *p = &vmx->segment_cache.seg[seg].ar;
1459
1460 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1461 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1462 return *p;
1463}
1464
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001465static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1466{
1467 u32 eb;
1468
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001469 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1470 (1u << NM_VECTOR) | (1u << DB_VECTOR);
1471 if ((vcpu->guest_debug &
1472 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1473 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1474 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001475 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001476 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001477 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001478 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001479 if (vcpu->fpu_active)
1480 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001481
1482 /* When we are running a nested L2 guest and L1 specified for it a
1483 * certain exception bitmap, we must trap the same exceptions and pass
1484 * them to L1. When running L2, we will only handle the exceptions
1485 * specified above if L1 did not want them.
1486 */
1487 if (is_guest_mode(vcpu))
1488 eb |= get_vmcs12(vcpu)->exception_bitmap;
1489
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001490 vmcs_write32(EXCEPTION_BITMAP, eb);
1491}
1492
Gleb Natapov2961e8762013-11-25 15:37:13 +02001493static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1494 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001495{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001496 vm_entry_controls_clearbit(vmx, entry);
1497 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001498}
1499
Avi Kivity61d2ef22010-04-28 16:40:38 +03001500static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1501{
1502 unsigned i;
1503 struct msr_autoload *m = &vmx->msr_autoload;
1504
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001505 switch (msr) {
1506 case MSR_EFER:
1507 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001508 clear_atomic_switch_msr_special(vmx,
1509 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001510 VM_EXIT_LOAD_IA32_EFER);
1511 return;
1512 }
1513 break;
1514 case MSR_CORE_PERF_GLOBAL_CTRL:
1515 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001516 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001517 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1518 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1519 return;
1520 }
1521 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001522 }
1523
Avi Kivity61d2ef22010-04-28 16:40:38 +03001524 for (i = 0; i < m->nr; ++i)
1525 if (m->guest[i].index == msr)
1526 break;
1527
1528 if (i == m->nr)
1529 return;
1530 --m->nr;
1531 m->guest[i] = m->guest[m->nr];
1532 m->host[i] = m->host[m->nr];
1533 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1534 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1535}
1536
Gleb Natapov2961e8762013-11-25 15:37:13 +02001537static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1538 unsigned long entry, unsigned long exit,
1539 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1540 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001541{
1542 vmcs_write64(guest_val_vmcs, guest_val);
1543 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001544 vm_entry_controls_setbit(vmx, entry);
1545 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001546}
1547
Avi Kivity61d2ef22010-04-28 16:40:38 +03001548static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1549 u64 guest_val, u64 host_val)
1550{
1551 unsigned i;
1552 struct msr_autoload *m = &vmx->msr_autoload;
1553
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001554 switch (msr) {
1555 case MSR_EFER:
1556 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001557 add_atomic_switch_msr_special(vmx,
1558 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001559 VM_EXIT_LOAD_IA32_EFER,
1560 GUEST_IA32_EFER,
1561 HOST_IA32_EFER,
1562 guest_val, host_val);
1563 return;
1564 }
1565 break;
1566 case MSR_CORE_PERF_GLOBAL_CTRL:
1567 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001568 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001569 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1570 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1571 GUEST_IA32_PERF_GLOBAL_CTRL,
1572 HOST_IA32_PERF_GLOBAL_CTRL,
1573 guest_val, host_val);
1574 return;
1575 }
1576 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001577 }
1578
Avi Kivity61d2ef22010-04-28 16:40:38 +03001579 for (i = 0; i < m->nr; ++i)
1580 if (m->guest[i].index == msr)
1581 break;
1582
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001583 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001584 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001585 "Can't add msr %x\n", msr);
1586 return;
1587 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001588 ++m->nr;
1589 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1590 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1591 }
1592
1593 m->guest[i].index = msr;
1594 m->guest[i].value = guest_val;
1595 m->host[i].index = msr;
1596 m->host[i].value = host_val;
1597}
1598
Avi Kivity33ed6322007-05-02 16:54:03 +03001599static void reload_tss(void)
1600{
Avi Kivity33ed6322007-05-02 16:54:03 +03001601 /*
1602 * VT restores TR but not its size. Useless.
1603 */
Avi Kivityd3591922010-07-26 18:32:39 +03001604 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001605 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001606
Avi Kivityd3591922010-07-26 18:32:39 +03001607 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001608 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1609 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001610}
1611
Avi Kivity92c0d902009-10-29 11:00:16 +02001612static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001613{
Roel Kluin3a34a882009-08-04 02:08:45 -07001614 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001615 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +03001616
Avi Kivityf6801df2010-01-21 15:31:50 +02001617 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -07001618
Avi Kivity51c6cf62007-08-29 03:48:05 +03001619 /*
Guo Chao0fa06072012-06-28 15:16:19 +08001620 * NX is emulated; LMA and LME handled by hardware; SCE meaningless
Avi Kivity51c6cf62007-08-29 03:48:05 +03001621 * outside long mode
1622 */
1623 ignore_bits = EFER_NX | EFER_SCE;
1624#ifdef CONFIG_X86_64
1625 ignore_bits |= EFER_LMA | EFER_LME;
1626 /* SCE is meaningful only in long mode on Intel */
1627 if (guest_efer & EFER_LMA)
1628 ignore_bits &= ~(u64)EFER_SCE;
1629#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +03001630 guest_efer &= ~ignore_bits;
1631 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +03001632 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +02001633 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03001634
1635 clear_atomic_switch_msr(vmx, MSR_EFER);
1636 /* On ept, can't emulate nx, and must switch nx atomically */
1637 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
1638 guest_efer = vmx->vcpu.arch.efer;
1639 if (!(guest_efer & EFER_LMA))
1640 guest_efer &= ~EFER_LME;
1641 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
1642 return false;
1643 }
1644
Avi Kivity26bb0982009-09-07 11:14:12 +03001645 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001646}
1647
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001648static unsigned long segment_base(u16 selector)
1649{
Avi Kivityd3591922010-07-26 18:32:39 +03001650 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001651 struct desc_struct *d;
1652 unsigned long table_base;
1653 unsigned long v;
1654
1655 if (!(selector & ~3))
1656 return 0;
1657
Avi Kivityd3591922010-07-26 18:32:39 +03001658 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001659
1660 if (selector & 4) { /* from ldt */
1661 u16 ldt_selector = kvm_read_ldt();
1662
1663 if (!(ldt_selector & ~3))
1664 return 0;
1665
1666 table_base = segment_base(ldt_selector);
1667 }
1668 d = (struct desc_struct *)(table_base + (selector & ~7));
1669 v = get_desc_base(d);
1670#ifdef CONFIG_X86_64
1671 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
1672 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
1673#endif
1674 return v;
1675}
1676
1677static inline unsigned long kvm_read_tr_base(void)
1678{
1679 u16 tr;
1680 asm("str %0" : "=g"(tr));
1681 return segment_base(tr);
1682}
1683
Avi Kivity04d2cc72007-09-10 18:10:54 +03001684static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001685{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001686 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001687 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001688
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001689 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001690 return;
1691
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001692 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001693 /*
1694 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1695 * allow segment selectors with cpl > 0 or ti == 1.
1696 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001697 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02001698 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02001699 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001700 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001701 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001702 vmx->host_state.fs_reload_needed = 0;
1703 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03001704 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001705 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001706 }
Avi Kivity9581d442010-10-19 16:46:55 +02001707 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001708 if (!(vmx->host_state.gs_sel & 7))
1709 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001710 else {
1711 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001712 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001713 }
1714
1715#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03001716 savesegment(ds, vmx->host_state.ds_sel);
1717 savesegment(es, vmx->host_state.es_sel);
1718#endif
1719
1720#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03001721 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1722 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1723#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001724 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1725 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03001726#endif
Avi Kivity707c0872007-05-02 17:33:43 +03001727
1728#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001729 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1730 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03001731 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03001732#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001733 if (boot_cpu_has(X86_FEATURE_MPX))
1734 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03001735 for (i = 0; i < vmx->save_nmsrs; ++i)
1736 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02001737 vmx->guest_msrs[i].data,
1738 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03001739}
1740
Avi Kivitya9b21b62008-06-24 11:48:49 +03001741static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001742{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001743 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001744 return;
1745
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001746 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001747 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02001748#ifdef CONFIG_X86_64
1749 if (is_long_mode(&vmx->vcpu))
1750 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1751#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001752 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001753 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001754#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02001755 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001756#else
1757 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001758#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001759 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02001760 if (vmx->host_state.fs_reload_needed)
1761 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001762#ifdef CONFIG_X86_64
1763 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
1764 loadsegment(ds, vmx->host_state.ds_sel);
1765 loadsegment(es, vmx->host_state.es_sel);
1766 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001767#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001768 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001769#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001770 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001771#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001772 if (vmx->host_state.msr_host_bndcfgs)
1773 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07001774 /*
1775 * If the FPU is not active (through the host task or
1776 * the guest vcpu), then restore the cr0.TS bit.
1777 */
1778 if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded)
1779 stts();
Avi Kivity3444d7d2010-07-26 18:32:38 +03001780 load_gdt(&__get_cpu_var(host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03001781}
1782
Avi Kivitya9b21b62008-06-24 11:48:49 +03001783static void vmx_load_host_state(struct vcpu_vmx *vmx)
1784{
1785 preempt_disable();
1786 __vmx_load_host_state(vmx);
1787 preempt_enable();
1788}
1789
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790/*
1791 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1792 * vcpu mutex is already taken.
1793 */
Avi Kivity15ad7142007-07-11 18:17:21 +03001794static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001795{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001796 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001797 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001799 if (!vmm_exclusive)
1800 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001801 else if (vmx->loaded_vmcs->cpu != cpu)
1802 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803
Nadav Har'Eld462b812011-05-24 15:26:10 +03001804 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1805 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1806 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807 }
1808
Nadav Har'Eld462b812011-05-24 15:26:10 +03001809 if (vmx->loaded_vmcs->cpu != cpu) {
Avi Kivityd3591922010-07-26 18:32:39 +03001810 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001811 unsigned long sysenter_esp;
1812
Avi Kivitya8eeb042010-05-10 12:34:53 +03001813 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001814 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001815 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001816
1817 /*
1818 * Read loaded_vmcs->cpu should be before fetching
1819 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1820 * See the comments in __loaded_vmcs_clear().
1821 */
1822 smp_rmb();
1823
Nadav Har'Eld462b812011-05-24 15:26:10 +03001824 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1825 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001826 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001827 local_irq_enable();
1828
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829 /*
1830 * Linux uses per-cpu TSS and GDT, so set these when switching
1831 * processors.
1832 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001833 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03001834 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835
1836 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1837 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Nadav Har'Eld462b812011-05-24 15:26:10 +03001838 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840}
1841
1842static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1843{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001844 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001845 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001846 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1847 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001848 kvm_cpu_vmxoff();
1849 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850}
1851
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001852static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1853{
Avi Kivity81231c62010-01-24 16:26:40 +02001854 ulong cr0;
1855
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001856 if (vcpu->fpu_active)
1857 return;
1858 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001859 cr0 = vmcs_readl(GUEST_CR0);
1860 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1861 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1862 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001863 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001864 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001865 if (is_guest_mode(vcpu))
1866 vcpu->arch.cr0_guest_owned_bits &=
1867 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02001868 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001869}
1870
Avi Kivityedcafe32009-12-30 18:07:40 +02001871static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1872
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001873/*
1874 * Return the cr0 value that a nested guest would read. This is a combination
1875 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1876 * its hypervisor (cr0_read_shadow).
1877 */
1878static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
1879{
1880 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
1881 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
1882}
1883static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
1884{
1885 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
1886 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
1887}
1888
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001889static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1890{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001891 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
1892 * set this *before* calling this function.
1893 */
Avi Kivityedcafe32009-12-30 18:07:40 +02001894 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001895 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001896 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001897 vcpu->arch.cr0_guest_owned_bits = 0;
1898 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001899 if (is_guest_mode(vcpu)) {
1900 /*
1901 * L1's specified read shadow might not contain the TS bit,
1902 * so now that we turned on shadowing of this bit, we need to
1903 * set this bit of the shadow. Like in nested_vmx_run we need
1904 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
1905 * up-to-date here because we just decached cr0.TS (and we'll
1906 * only update vmcs12->guest_cr0 on nested exit).
1907 */
1908 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1909 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
1910 (vcpu->arch.cr0 & X86_CR0_TS);
1911 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
1912 } else
1913 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001914}
1915
Avi Kivity6aa8b732006-12-10 02:21:36 -08001916static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1917{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001918 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001919
Avi Kivity6de12732011-03-07 12:51:22 +02001920 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1921 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1922 rflags = vmcs_readl(GUEST_RFLAGS);
1923 if (to_vmx(vcpu)->rmode.vm86_active) {
1924 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1925 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1926 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1927 }
1928 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001929 }
Avi Kivity6de12732011-03-07 12:51:22 +02001930 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931}
1932
1933static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1934{
Avi Kivity6de12732011-03-07 12:51:22 +02001935 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1936 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001937 if (to_vmx(vcpu)->rmode.vm86_active) {
1938 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001939 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001940 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941 vmcs_writel(GUEST_RFLAGS, rflags);
1942}
1943
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001944static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001945{
1946 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1947 int ret = 0;
1948
1949 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001950 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001951 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001952 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001953
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +02001954 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001955}
1956
1957static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1958{
1959 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1960 u32 interruptibility = interruptibility_old;
1961
1962 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1963
Jan Kiszka48005f62010-02-19 19:38:07 +01001964 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001965 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001966 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001967 interruptibility |= GUEST_INTR_STATE_STI;
1968
1969 if ((interruptibility != interruptibility_old))
1970 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1971}
1972
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1974{
1975 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001976
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001977 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001979 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001980
Glauber Costa2809f5d2009-05-12 16:21:05 -04001981 /* skipping an emulated instruction also counts */
1982 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001983}
1984
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001985/*
1986 * KVM wants to inject page-faults which it got to the guest. This function
1987 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001988 */
Gleb Natapove011c662013-09-25 12:51:35 +03001989static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001990{
1991 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1992
Gleb Natapove011c662013-09-25 12:51:35 +03001993 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001994 return 0;
1995
Jan Kiszka533558b2014-01-04 18:47:20 +01001996 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
1997 vmcs_read32(VM_EXIT_INTR_INFO),
1998 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001999 return 1;
2000}
2001
Avi Kivity298101d2007-11-25 13:41:11 +02002002static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002003 bool has_error_code, u32 error_code,
2004 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002005{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002006 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002007 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002008
Gleb Natapove011c662013-09-25 12:51:35 +03002009 if (!reinject && is_guest_mode(vcpu) &&
2010 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002011 return;
2012
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002013 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002014 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002015 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2016 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002017
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002018 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002019 int inc_eip = 0;
2020 if (kvm_exception_is_soft(nr))
2021 inc_eip = vcpu->arch.event_exit_inst_len;
2022 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002023 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002024 return;
2025 }
2026
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002027 if (kvm_exception_is_soft(nr)) {
2028 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2029 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002030 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2031 } else
2032 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2033
2034 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002035}
2036
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002037static bool vmx_rdtscp_supported(void)
2038{
2039 return cpu_has_vmx_rdtscp();
2040}
2041
Mao, Junjiead756a12012-07-02 01:18:48 +00002042static bool vmx_invpcid_supported(void)
2043{
2044 return cpu_has_vmx_invpcid() && enable_ept;
2045}
2046
Avi Kivity6aa8b732006-12-10 02:21:36 -08002047/*
Eddie Donga75beee2007-05-17 18:55:15 +03002048 * Swap MSR entry in host/guest MSR entry array.
2049 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002050static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002051{
Avi Kivity26bb0982009-09-07 11:14:12 +03002052 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002053
2054 tmp = vmx->guest_msrs[to];
2055 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2056 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002057}
2058
Yang Zhang8d146952013-01-25 10:18:50 +08002059static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2060{
2061 unsigned long *msr_bitmap;
2062
2063 if (irqchip_in_kernel(vcpu->kvm) && apic_x2apic_mode(vcpu->arch.apic)) {
2064 if (is_long_mode(vcpu))
2065 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2066 else
2067 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
2068 } else {
2069 if (is_long_mode(vcpu))
2070 msr_bitmap = vmx_msr_bitmap_longmode;
2071 else
2072 msr_bitmap = vmx_msr_bitmap_legacy;
2073 }
2074
2075 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2076}
2077
Eddie Donga75beee2007-05-17 18:55:15 +03002078/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002079 * Set up the vmcs to automatically save and restore system
2080 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2081 * mode, as fiddling with msrs is very expensive.
2082 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002083static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002084{
Avi Kivity26bb0982009-09-07 11:14:12 +03002085 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002086
Eddie Donga75beee2007-05-17 18:55:15 +03002087 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002088#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002089 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002090 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002091 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002092 move_msr_up(vmx, index, save_nmsrs++);
2093 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002094 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002095 move_msr_up(vmx, index, save_nmsrs++);
2096 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002097 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002098 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002099 index = __find_msr_index(vmx, MSR_TSC_AUX);
2100 if (index >= 0 && vmx->rdtscp_enabled)
2101 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002102 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002103 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002104 * if efer.sce is enabled.
2105 */
Brian Gerst8c065852010-07-17 09:03:26 -04002106 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002107 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002108 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002109 }
Eddie Donga75beee2007-05-17 18:55:15 +03002110#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002111 index = __find_msr_index(vmx, MSR_EFER);
2112 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002113 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002114
Avi Kivity26bb0982009-09-07 11:14:12 +03002115 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002116
Yang Zhang8d146952013-01-25 10:18:50 +08002117 if (cpu_has_vmx_msr_bitmap())
2118 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002119}
2120
2121/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122 * reads and returns guest's timestamp counter "register"
2123 * guest_tsc = host_tsc + tsc_offset -- 21.3
2124 */
2125static u64 guest_read_tsc(void)
2126{
2127 u64 host_tsc, tsc_offset;
2128
2129 rdtscll(host_tsc);
2130 tsc_offset = vmcs_read64(TSC_OFFSET);
2131 return host_tsc + tsc_offset;
2132}
2133
2134/*
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002135 * Like guest_read_tsc, but always returns L1's notion of the timestamp
2136 * counter, even if a nested guest (L2) is currently running.
2137 */
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002138u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002139{
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002140 u64 tsc_offset;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002141
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002142 tsc_offset = is_guest_mode(vcpu) ?
2143 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
2144 vmcs_read64(TSC_OFFSET);
2145 return host_tsc + tsc_offset;
2146}
2147
2148/*
Zachary Amsdencc578282012-02-03 15:43:50 -02002149 * Engage any workarounds for mis-matched TSC rates. Currently limited to
2150 * software catchup for faster rates on slower CPUs.
Joerg Roedel4051b182011-03-25 09:44:49 +01002151 */
Zachary Amsdencc578282012-02-03 15:43:50 -02002152static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +01002153{
Zachary Amsdencc578282012-02-03 15:43:50 -02002154 if (!scale)
2155 return;
2156
2157 if (user_tsc_khz > tsc_khz) {
2158 vcpu->arch.tsc_catchup = 1;
2159 vcpu->arch.tsc_always_catchup = 1;
2160 } else
2161 WARN(1, "user requested TSC rate below hardware speed\n");
Joerg Roedel4051b182011-03-25 09:44:49 +01002162}
2163
Will Auldba904632012-11-29 12:42:50 -08002164static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
2165{
2166 return vmcs_read64(TSC_OFFSET);
2167}
2168
Joerg Roedel4051b182011-03-25 09:44:49 +01002169/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002170 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002172static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002174 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002175 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002176 * We're here if L1 chose not to trap WRMSR to TSC. According
2177 * to the spec, this should set L1's TSC; The offset that L1
2178 * set for L2 remains unchanged, and still needs to be added
2179 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002180 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002181 struct vmcs12 *vmcs12;
2182 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
2183 /* recalculate vmcs02.TSC_OFFSET: */
2184 vmcs12 = get_vmcs12(vcpu);
2185 vmcs_write64(TSC_OFFSET, offset +
2186 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2187 vmcs12->tsc_offset : 0));
2188 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002189 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2190 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002191 vmcs_write64(TSC_OFFSET, offset);
2192 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193}
2194
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002195static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10002196{
2197 u64 offset = vmcs_read64(TSC_OFFSET);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002198
Zachary Amsdene48672f2010-08-19 22:07:23 -10002199 vmcs_write64(TSC_OFFSET, offset + adjustment);
Nadav Har'El79918252011-05-25 23:15:39 +03002200 if (is_guest_mode(vcpu)) {
2201 /* Even when running L2, the adjustment needs to apply to L1 */
2202 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002203 } else
2204 trace_kvm_write_tsc_offset(vcpu->vcpu_id, offset,
2205 offset + adjustment);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002206}
2207
Joerg Roedel857e4092011-03-25 09:44:50 +01002208static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
2209{
2210 return target_tsc - native_read_tsc();
2211}
2212
Nadav Har'El801d3422011-05-25 23:02:23 +03002213static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2214{
2215 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2216 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2217}
2218
2219/*
2220 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2221 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2222 * all guests if the "nested" module option is off, and can also be disabled
2223 * for a single guest by disabling its VMX cpuid bit.
2224 */
2225static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2226{
2227 return nested && guest_cpuid_has_vmx(vcpu);
2228}
2229
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002231 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2232 * returned for the various VMX controls MSRs when nested VMX is enabled.
2233 * The same values should also be used to verify that vmcs12 control fields are
2234 * valid during nested entry from L1 to L2.
2235 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2236 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2237 * bit in the high half is on if the corresponding bit in the control field
2238 * may be on. See also vmx_control_verify().
2239 * TODO: allow these variables to be modified (downgraded) by module options
2240 * or other means.
2241 */
2242static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002243static u32 nested_vmx_true_procbased_ctls_low;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002244static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
2245static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
2246static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002247static u32 nested_vmx_true_exit_ctls_low;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002248static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
Jan Kiszka2996fca2014-06-16 13:59:43 +02002249static u32 nested_vmx_true_entry_ctls_low;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002250static u32 nested_vmx_misc_low, nested_vmx_misc_high;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03002251static u32 nested_vmx_ept_caps;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002252static __init void nested_vmx_setup_ctls_msrs(void)
2253{
2254 /*
2255 * Note that as a general rule, the high half of the MSRs (bits in
2256 * the control fields which may be 1) should be initialized by the
2257 * intersection of the underlying hardware's MSR (i.e., features which
2258 * can be supported) and the list of features we want to expose -
2259 * because they are known to be properly supported in our code.
2260 * Also, usually, the low half of the MSRs (bits which must be 1) can
2261 * be set to 0, meaning that L1 may turn off any of these bits. The
2262 * reason is that if one of these bits is necessary, it will appear
2263 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2264 * fields of vmcs01 and vmcs02, will turn these bits off - and
2265 * nested_vmx_exit_handled() will not pass related exits to L1.
2266 * These rules have exceptions below.
2267 */
2268
2269 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002270 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
2271 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high);
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002272 nested_vmx_pinbased_ctls_low |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2273 nested_vmx_pinbased_ctls_high &= PIN_BASED_EXT_INTR_MASK |
Jan Kiszkaf4124502014-03-07 20:03:13 +01002274 PIN_BASED_NMI_EXITING | PIN_BASED_VIRTUAL_NMIS;
2275 nested_vmx_pinbased_ctls_high |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002276 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002277
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002278 /* exit controls */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002279 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
2280 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high);
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002281 nested_vmx_exit_ctls_low = VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Bandan Dase0ba1a62014-04-19 18:17:46 -04002282
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002283 nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002284#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002285 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002286#endif
Jan Kiszkaf4124502014-03-07 20:03:13 +01002287 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
2288 nested_vmx_exit_ctls_high |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
2289 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Bandan Dase0ba1a62014-04-19 18:17:46 -04002290 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT;
2291
Paolo Bonzini36be0b92014-02-24 12:30:04 +01002292 if (vmx_mpx_supported())
2293 nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002294
Jan Kiszka2996fca2014-06-16 13:59:43 +02002295 /* We support free control of debug control saving. */
2296 nested_vmx_true_exit_ctls_low = nested_vmx_exit_ctls_low &
2297 ~VM_EXIT_SAVE_DEBUG_CONTROLS;
2298
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002299 /* entry controls */
2300 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
2301 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002302 nested_vmx_entry_ctls_low = VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002303 nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002304#ifdef CONFIG_X86_64
2305 VM_ENTRY_IA32E_MODE |
2306#endif
2307 VM_ENTRY_LOAD_IA32_PAT;
Nadav Har'El8049d652013-08-05 11:07:06 +03002308 nested_vmx_entry_ctls_high |= (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR |
2309 VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzini36be0b92014-02-24 12:30:04 +01002310 if (vmx_mpx_supported())
2311 nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002312
Jan Kiszka2996fca2014-06-16 13:59:43 +02002313 /* We support free control of debug control loading. */
2314 nested_vmx_true_entry_ctls_low = nested_vmx_entry_ctls_low &
2315 ~VM_ENTRY_LOAD_DEBUG_CONTROLS;
2316
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002317 /* cpu-based controls */
2318 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
2319 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002320 nested_vmx_procbased_ctls_low = CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002321 nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002322 CPU_BASED_VIRTUAL_INTR_PENDING |
2323 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002324 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2325 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2326 CPU_BASED_CR3_STORE_EXITING |
2327#ifdef CONFIG_X86_64
2328 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2329#endif
2330 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
2331 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
Avi Kivitydbcb4e72012-08-13 15:38:22 +03002332 CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING |
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002333 CPU_BASED_PAUSE_EXITING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002334 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2335 /*
2336 * We can allow some features even when not supported by the
2337 * hardware. For example, L1 can specify an MSR bitmap - and we
2338 * can use it to avoid exits to L1 - even when L0 runs L2
2339 * without MSR bitmaps.
2340 */
Jan Kiszka560b7ee2014-06-16 13:59:42 +02002341 nested_vmx_procbased_ctls_high |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
2342 CPU_BASED_USE_MSR_BITMAPS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002343
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002344 /* We support free control of CR3 access interception. */
2345 nested_vmx_true_procbased_ctls_low = nested_vmx_procbased_ctls_low &
2346 ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING);
2347
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002348 /* secondary cpu-based controls */
2349 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
2350 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
2351 nested_vmx_secondary_ctls_low = 0;
2352 nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002353 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02002354 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002355 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002356
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002357 if (enable_ept) {
2358 /* nested EPT: emulate EPT also to L1 */
2359 nested_vmx_secondary_ctls_high |= SECONDARY_EXEC_ENABLE_EPT;
Jan Kiszkaca72d972013-08-06 10:39:55 +02002360 nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002361 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2362 VMX_EPT_INVEPT_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002363 nested_vmx_ept_caps &= vmx_capability.ept;
2364 /*
Bandan Das4b855072014-04-19 18:17:44 -04002365 * For nested guests, we don't do anything specific
2366 * for single context invalidation. Hence, only advertise
2367 * support for global context invalidation.
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002368 */
Bandan Das4b855072014-04-19 18:17:44 -04002369 nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT;
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002370 } else
2371 nested_vmx_ept_caps = 0;
2372
Jan Kiszkac18911a2013-03-13 16:06:41 +01002373 /* miscellaneous data */
2374 rdmsr(MSR_IA32_VMX_MISC, nested_vmx_misc_low, nested_vmx_misc_high);
Jan Kiszkaf4124502014-03-07 20:03:13 +01002375 nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2376 nested_vmx_misc_low |= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
2377 VMX_MISC_ACTIVITY_HLT;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002378 nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002379}
2380
2381static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2382{
2383 /*
2384 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2385 */
2386 return ((control & high) | low) == control;
2387}
2388
2389static inline u64 vmx_control_msr(u32 low, u32 high)
2390{
2391 return low | ((u64)high << 32);
2392}
2393
Jan Kiszkacae50132014-01-04 18:47:22 +01002394/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002395static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2396{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002397 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002398 case MSR_IA32_VMX_BASIC:
2399 /*
2400 * This MSR reports some information about VMX support. We
2401 * should return information about the VMX we emulate for the
2402 * guest, and the VMCS structure we give it - not about the
2403 * VMX support of the underlying hardware.
2404 */
Jan Kiszka3dbcd8d2014-06-16 13:59:40 +02002405 *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002406 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2407 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2408 break;
2409 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2410 case MSR_IA32_VMX_PINBASED_CTLS:
2411 *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
2412 nested_vmx_pinbased_ctls_high);
2413 break;
2414 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02002415 *pdata = vmx_control_msr(nested_vmx_true_procbased_ctls_low,
2416 nested_vmx_procbased_ctls_high);
2417 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002418 case MSR_IA32_VMX_PROCBASED_CTLS:
2419 *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
2420 nested_vmx_procbased_ctls_high);
2421 break;
2422 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
Jan Kiszka2996fca2014-06-16 13:59:43 +02002423 *pdata = vmx_control_msr(nested_vmx_true_exit_ctls_low,
2424 nested_vmx_exit_ctls_high);
2425 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002426 case MSR_IA32_VMX_EXIT_CTLS:
2427 *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
2428 nested_vmx_exit_ctls_high);
2429 break;
2430 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
Jan Kiszka2996fca2014-06-16 13:59:43 +02002431 *pdata = vmx_control_msr(nested_vmx_true_entry_ctls_low,
2432 nested_vmx_entry_ctls_high);
2433 break;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002434 case MSR_IA32_VMX_ENTRY_CTLS:
2435 *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
2436 nested_vmx_entry_ctls_high);
2437 break;
2438 case MSR_IA32_VMX_MISC:
Jan Kiszkac18911a2013-03-13 16:06:41 +01002439 *pdata = vmx_control_msr(nested_vmx_misc_low,
2440 nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002441 break;
2442 /*
2443 * These MSRs specify bits which the guest must keep fixed (on or off)
2444 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2445 * We picked the standard core2 setting.
2446 */
2447#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2448#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2449 case MSR_IA32_VMX_CR0_FIXED0:
2450 *pdata = VMXON_CR0_ALWAYSON;
2451 break;
2452 case MSR_IA32_VMX_CR0_FIXED1:
2453 *pdata = -1ULL;
2454 break;
2455 case MSR_IA32_VMX_CR4_FIXED0:
2456 *pdata = VMXON_CR4_ALWAYSON;
2457 break;
2458 case MSR_IA32_VMX_CR4_FIXED1:
2459 *pdata = -1ULL;
2460 break;
2461 case MSR_IA32_VMX_VMCS_ENUM:
Jan Kiszka53814172014-06-16 13:59:44 +02002462 *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002463 break;
2464 case MSR_IA32_VMX_PROCBASED_CTLS2:
2465 *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
2466 nested_vmx_secondary_ctls_high);
2467 break;
2468 case MSR_IA32_VMX_EPT_VPID_CAP:
Nadav Har'Elafa61f72013-08-07 14:59:22 +02002469 /* Currently, no nested vpid support */
2470 *pdata = nested_vmx_ept_caps;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002471 break;
2472 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002473 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002474 }
2475
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002476 return 0;
2477}
2478
2479/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480 * Reads an msr value (of 'msr_index') into 'pdata'.
2481 * Returns 0 on success, non-0 otherwise.
2482 * Assumes vcpu_load() was already called.
2483 */
2484static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2485{
2486 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03002487 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002488
2489 if (!pdata) {
2490 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
2491 return -EINVAL;
2492 }
2493
2494 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002495#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496 case MSR_FS_BASE:
2497 data = vmcs_readl(GUEST_FS_BASE);
2498 break;
2499 case MSR_GS_BASE:
2500 data = vmcs_readl(GUEST_GS_BASE);
2501 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002502 case MSR_KERNEL_GS_BASE:
2503 vmx_load_host_state(to_vmx(vcpu));
2504 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
2505 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002506#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002508 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302509 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510 data = guest_read_tsc();
2511 break;
2512 case MSR_IA32_SYSENTER_CS:
2513 data = vmcs_read32(GUEST_SYSENTER_CS);
2514 break;
2515 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002516 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517 break;
2518 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002519 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002521 case MSR_IA32_BNDCFGS:
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002522 if (!vmx_mpx_supported())
2523 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002524 data = vmcs_read64(GUEST_BNDCFGS);
2525 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002526 case MSR_IA32_FEATURE_CONTROL:
2527 if (!nested_vmx_allowed(vcpu))
2528 return 1;
2529 data = to_vmx(vcpu)->nested.msr_ia32_feature_control;
2530 break;
2531 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2532 if (!nested_vmx_allowed(vcpu))
2533 return 1;
2534 return vmx_get_vmx_msr(vcpu, msr_index, pdata);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002535 case MSR_TSC_AUX:
2536 if (!to_vmx(vcpu)->rdtscp_enabled)
2537 return 1;
2538 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002539 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002540 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002541 if (msr) {
2542 data = msr->data;
2543 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002545 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 }
2547
2548 *pdata = data;
2549 return 0;
2550}
2551
Jan Kiszkacae50132014-01-04 18:47:22 +01002552static void vmx_leave_nested(struct kvm_vcpu *vcpu);
2553
Avi Kivity6aa8b732006-12-10 02:21:36 -08002554/*
2555 * Writes msr value into into the appropriate "register".
2556 * Returns 0 on success, non-0 otherwise.
2557 * Assumes vcpu_load() was already called.
2558 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002559static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002560{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002561 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002562 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002563 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002564 u32 msr_index = msr_info->index;
2565 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03002566
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002568 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002569 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002570 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002571#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002573 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002574 vmcs_writel(GUEST_FS_BASE, data);
2575 break;
2576 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002577 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002578 vmcs_writel(GUEST_GS_BASE, data);
2579 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002580 case MSR_KERNEL_GS_BASE:
2581 vmx_load_host_state(vmx);
2582 vmx->msr_guest_kernel_gs_base = data;
2583 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002584#endif
2585 case MSR_IA32_SYSENTER_CS:
2586 vmcs_write32(GUEST_SYSENTER_CS, data);
2587 break;
2588 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002589 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590 break;
2591 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002592 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002593 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002594 case MSR_IA32_BNDCFGS:
Paolo Bonzini93c4adc2014-03-05 23:19:52 +01002595 if (!vmx_mpx_supported())
2596 return 1;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002597 vmcs_write64(GUEST_BNDCFGS, data);
2598 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302599 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08002600 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002602 case MSR_IA32_CR_PAT:
2603 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2604 vmcs_write64(GUEST_IA32_PAT, data);
2605 vcpu->arch.pat = data;
2606 break;
2607 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002608 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002609 break;
Will Auldba904632012-11-29 12:42:50 -08002610 case MSR_IA32_TSC_ADJUST:
2611 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002612 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002613 case MSR_IA32_FEATURE_CONTROL:
2614 if (!nested_vmx_allowed(vcpu) ||
2615 (to_vmx(vcpu)->nested.msr_ia32_feature_control &
2616 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
2617 return 1;
2618 vmx->nested.msr_ia32_feature_control = data;
2619 if (msr_info->host_initiated && data == 0)
2620 vmx_leave_nested(vcpu);
2621 break;
2622 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2623 return 1; /* they are read-only */
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002624 case MSR_TSC_AUX:
2625 if (!vmx->rdtscp_enabled)
2626 return 1;
2627 /* Check reserved bit, higher 32 bits should be zero */
2628 if ((data >> 32) != 0)
2629 return 1;
2630 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002632 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002633 if (msr) {
2634 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002635 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2636 preempt_disable();
Avi Kivity9ee73972012-03-06 14:16:33 +02002637 kvm_set_shared_msr(msr->index, msr->data,
2638 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03002639 preempt_enable();
2640 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002641 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002642 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002643 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 }
2645
Eddie Dong2cc51562007-05-21 07:28:09 +03002646 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647}
2648
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002649static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002651 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2652 switch (reg) {
2653 case VCPU_REGS_RSP:
2654 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2655 break;
2656 case VCPU_REGS_RIP:
2657 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2658 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002659 case VCPU_EXREG_PDPTR:
2660 if (enable_ept)
2661 ept_save_pdptrs(vcpu);
2662 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002663 default:
2664 break;
2665 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002666}
2667
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668static __init int cpu_has_kvm_support(void)
2669{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002670 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671}
2672
2673static __init int vmx_disabled_by_bios(void)
2674{
2675 u64 msr;
2676
2677 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04002678 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08002679 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04002680 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2681 && tboot_enabled())
2682 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08002683 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04002684 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08002685 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08002686 && !tboot_enabled()) {
2687 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08002688 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04002689 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08002690 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08002691 /* launched w/o TXT and VMX disabled */
2692 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2693 && !tboot_enabled())
2694 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04002695 }
2696
2697 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698}
2699
Dongxiao Xu7725b892010-05-11 18:29:38 +08002700static void kvm_cpu_vmxon(u64 addr)
2701{
2702 asm volatile (ASM_VMX_VMXON_RAX
2703 : : "a"(&addr), "m"(addr)
2704 : "memory", "cc");
2705}
2706
Alexander Graf10474ae2009-09-15 11:37:46 +02002707static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708{
2709 int cpu = raw_smp_processor_id();
2710 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04002711 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002712
Alexander Graf10474ae2009-09-15 11:37:46 +02002713 if (read_cr4() & X86_CR4_VMXE)
2714 return -EBUSY;
2715
Nadav Har'Eld462b812011-05-24 15:26:10 +03002716 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002717
2718 /*
2719 * Now we can enable the vmclear operation in kdump
2720 * since the loaded_vmcss_on_cpu list on this cpu
2721 * has been initialized.
2722 *
2723 * Though the cpu is not in VMX operation now, there
2724 * is no problem to enable the vmclear operation
2725 * for the loaded_vmcss_on_cpu list is empty!
2726 */
2727 crash_enable_local_vmclear(cpu);
2728
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04002730
2731 test_bits = FEATURE_CONTROL_LOCKED;
2732 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2733 if (tboot_enabled())
2734 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2735
2736 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04002738 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2739 }
Rusty Russell66aee912007-07-17 23:34:16 +10002740 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02002741
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002742 if (vmm_exclusive) {
2743 kvm_cpu_vmxon(phys_addr);
2744 ept_sync_global();
2745 }
Alexander Graf10474ae2009-09-15 11:37:46 +02002746
Konrad Rzeszutek Wilk357d1222013-04-05 16:42:23 -04002747 native_store_gdt(&__get_cpu_var(host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03002748
Alexander Graf10474ae2009-09-15 11:37:46 +02002749 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750}
2751
Nadav Har'Eld462b812011-05-24 15:26:10 +03002752static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002753{
2754 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002755 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002756
Nadav Har'Eld462b812011-05-24 15:26:10 +03002757 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2758 loaded_vmcss_on_cpu_link)
2759 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002760}
2761
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002762
2763/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2764 * tricks.
2765 */
2766static void kvm_cpu_vmxoff(void)
2767{
2768 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002769}
2770
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771static void hardware_disable(void *garbage)
2772{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002773 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002774 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002775 kvm_cpu_vmxoff();
2776 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08002777 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002778}
2779
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002780static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002781 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002782{
2783 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002784 u32 ctl = ctl_min | ctl_opt;
2785
2786 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2787
2788 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2789 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2790
2791 /* Ensure minimum (required) set of control bits are supported. */
2792 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002793 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002794
2795 *result = ctl;
2796 return 0;
2797}
2798
Avi Kivity110312c2010-12-21 12:54:20 +02002799static __init bool allow_1_setting(u32 msr, u32 ctl)
2800{
2801 u32 vmx_msr_low, vmx_msr_high;
2802
2803 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2804 return vmx_msr_high & ctl;
2805}
2806
Yang, Sheng002c7f72007-07-31 14:23:01 +03002807static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002808{
2809 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002810 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002811 u32 _pin_based_exec_control = 0;
2812 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002813 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002814 u32 _vmexit_control = 0;
2815 u32 _vmentry_control = 0;
2816
Raghavendra K T10166742012-02-07 23:19:20 +05302817 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002818#ifdef CONFIG_X86_64
2819 CPU_BASED_CR8_LOAD_EXITING |
2820 CPU_BASED_CR8_STORE_EXITING |
2821#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002822 CPU_BASED_CR3_LOAD_EXITING |
2823 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002824 CPU_BASED_USE_IO_BITMAPS |
2825 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03002826 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08002827 CPU_BASED_MWAIT_EXITING |
2828 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002829 CPU_BASED_INVLPG_EXITING |
2830 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002831
Sheng Yangf78e0e22007-10-29 09:40:42 +08002832 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002833 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002834 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002835 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2836 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002837 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002838#ifdef CONFIG_X86_64
2839 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2840 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2841 ~CPU_BASED_CR8_STORE_EXITING;
2842#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002843 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002844 min2 = 0;
2845 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002846 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002847 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002848 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002849 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002850 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002851 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00002852 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002853 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002854 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03002855 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2856 SECONDARY_EXEC_SHADOW_VMCS;
Sheng Yangd56f5462008-04-25 10:13:16 +08002857 if (adjust_vmx_controls(min2, opt2,
2858 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002859 &_cpu_based_2nd_exec_control) < 0)
2860 return -EIO;
2861 }
2862#ifndef CONFIG_X86_64
2863 if (!(_cpu_based_2nd_exec_control &
2864 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2865 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2866#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002867
2868 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2869 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002870 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002871 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2872 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002873
Sheng Yangd56f5462008-04-25 10:13:16 +08002874 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002875 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2876 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002877 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2878 CPU_BASED_CR3_STORE_EXITING |
2879 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08002880 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
2881 vmx_capability.ept, vmx_capability.vpid);
2882 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002883
Paolo Bonzini81908bf2014-02-21 10:32:27 +01002884 min = VM_EXIT_SAVE_DEBUG_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002885#ifdef CONFIG_X86_64
2886 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2887#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08002888 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002889 VM_EXIT_ACK_INTR_ON_EXIT | VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002890 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2891 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002892 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002893
Yang Zhang01e439b2013-04-11 19:25:12 +08002894 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2895 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR;
2896 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2897 &_pin_based_exec_control) < 0)
2898 return -EIO;
2899
2900 if (!(_cpu_based_2nd_exec_control &
2901 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) ||
2902 !(_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT))
2903 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2904
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002905 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002906 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002907 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2908 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002909 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002910
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002911 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002912
2913 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2914 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002915 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002916
2917#ifdef CONFIG_X86_64
2918 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2919 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002920 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002921#endif
2922
2923 /* Require Write-Back (WB) memory type for VMCS accesses. */
2924 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002925 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002926
Yang, Sheng002c7f72007-07-31 14:23:01 +03002927 vmcs_conf->size = vmx_msr_high & 0x1fff;
2928 vmcs_conf->order = get_order(vmcs_config.size);
2929 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002930
Yang, Sheng002c7f72007-07-31 14:23:01 +03002931 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2932 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002933 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002934 vmcs_conf->vmexit_ctrl = _vmexit_control;
2935 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002936
Avi Kivity110312c2010-12-21 12:54:20 +02002937 cpu_has_load_ia32_efer =
2938 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2939 VM_ENTRY_LOAD_IA32_EFER)
2940 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2941 VM_EXIT_LOAD_IA32_EFER);
2942
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002943 cpu_has_load_perf_global_ctrl =
2944 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2945 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
2946 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2947 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2948
2949 /*
2950 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
2951 * but due to arrata below it can't be used. Workaround is to use
2952 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2953 *
2954 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
2955 *
2956 * AAK155 (model 26)
2957 * AAP115 (model 30)
2958 * AAT100 (model 37)
2959 * BC86,AAY89,BD102 (model 44)
2960 * BA97 (model 46)
2961 *
2962 */
2963 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
2964 switch (boot_cpu_data.x86_model) {
2965 case 26:
2966 case 30:
2967 case 37:
2968 case 44:
2969 case 46:
2970 cpu_has_load_perf_global_ctrl = false;
2971 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2972 "does not work properly. Using workaround\n");
2973 break;
2974 default:
2975 break;
2976 }
2977 }
2978
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002979 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002980}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981
2982static struct vmcs *alloc_vmcs_cpu(int cpu)
2983{
2984 int node = cpu_to_node(cpu);
2985 struct page *pages;
2986 struct vmcs *vmcs;
2987
Mel Gorman6484eb32009-06-16 15:31:54 -07002988 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989 if (!pages)
2990 return NULL;
2991 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002992 memset(vmcs, 0, vmcs_config.size);
2993 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994 return vmcs;
2995}
2996
2997static struct vmcs *alloc_vmcs(void)
2998{
Ingo Molnard3b2c332007-01-05 16:36:23 -08002999 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08003000}
3001
3002static void free_vmcs(struct vmcs *vmcs)
3003{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003004 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003005}
3006
Nadav Har'Eld462b812011-05-24 15:26:10 +03003007/*
3008 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3009 */
3010static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3011{
3012 if (!loaded_vmcs->vmcs)
3013 return;
3014 loaded_vmcs_clear(loaded_vmcs);
3015 free_vmcs(loaded_vmcs->vmcs);
3016 loaded_vmcs->vmcs = NULL;
3017}
3018
Sam Ravnborg39959582007-06-01 00:47:13 -07003019static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003020{
3021 int cpu;
3022
Zachary Amsden3230bb42009-09-29 11:38:37 -10003023 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003024 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003025 per_cpu(vmxarea, cpu) = NULL;
3026 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003027}
3028
Bandan Dasfe2b2012014-04-21 15:20:14 -04003029static void init_vmcs_shadow_fields(void)
3030{
3031 int i, j;
3032
3033 /* No checks for read only fields yet */
3034
3035 for (i = j = 0; i < max_shadow_read_write_fields; i++) {
3036 switch (shadow_read_write_fields[i]) {
3037 case GUEST_BNDCFGS:
3038 if (!vmx_mpx_supported())
3039 continue;
3040 break;
3041 default:
3042 break;
3043 }
3044
3045 if (j < i)
3046 shadow_read_write_fields[j] =
3047 shadow_read_write_fields[i];
3048 j++;
3049 }
3050 max_shadow_read_write_fields = j;
3051
3052 /* shadowed fields guest access without vmexit */
3053 for (i = 0; i < max_shadow_read_write_fields; i++) {
3054 clear_bit(shadow_read_write_fields[i],
3055 vmx_vmwrite_bitmap);
3056 clear_bit(shadow_read_write_fields[i],
3057 vmx_vmread_bitmap);
3058 }
3059 for (i = 0; i < max_shadow_read_only_fields; i++)
3060 clear_bit(shadow_read_only_fields[i],
3061 vmx_vmread_bitmap);
3062}
3063
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064static __init int alloc_kvm_area(void)
3065{
3066 int cpu;
3067
Zachary Amsden3230bb42009-09-29 11:38:37 -10003068 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003069 struct vmcs *vmcs;
3070
3071 vmcs = alloc_vmcs_cpu(cpu);
3072 if (!vmcs) {
3073 free_kvm_area();
3074 return -ENOMEM;
3075 }
3076
3077 per_cpu(vmxarea, cpu) = vmcs;
3078 }
3079 return 0;
3080}
3081
3082static __init int hardware_setup(void)
3083{
Yang, Sheng002c7f72007-07-31 14:23:01 +03003084 if (setup_vmcs_config(&vmcs_config) < 0)
3085 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01003086
3087 if (boot_cpu_has(X86_FEATURE_NX))
3088 kvm_enable_efer_bits(EFER_NX);
3089
Sheng Yang93ba03c2009-04-01 15:52:32 +08003090 if (!cpu_has_vmx_vpid())
3091 enable_vpid = 0;
Abel Gordonabc4fc52013-04-18 14:35:25 +03003092 if (!cpu_has_vmx_shadow_vmcs())
3093 enable_shadow_vmcs = 0;
Bandan Dasfe2b2012014-04-21 15:20:14 -04003094 if (enable_shadow_vmcs)
3095 init_vmcs_shadow_fields();
Sheng Yang93ba03c2009-04-01 15:52:32 +08003096
Sheng Yang4bc9b982010-06-02 14:05:24 +08003097 if (!cpu_has_vmx_ept() ||
3098 !cpu_has_vmx_ept_4levels()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08003099 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003100 enable_unrestricted_guest = 0;
Xudong Hao83c3a332012-05-28 19:33:35 +08003101 enable_ept_ad_bits = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003102 }
3103
Xudong Hao83c3a332012-05-28 19:33:35 +08003104 if (!cpu_has_vmx_ept_ad_bits())
3105 enable_ept_ad_bits = 0;
3106
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003107 if (!cpu_has_vmx_unrestricted_guest())
3108 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08003109
3110 if (!cpu_has_vmx_flexpriority())
3111 flexpriority_enabled = 0;
3112
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003113 if (!cpu_has_vmx_tpr_shadow())
3114 kvm_x86_ops->update_cr8_intercept = NULL;
3115
Marcelo Tosatti54dee992009-06-11 12:07:44 -03003116 if (enable_ept && !cpu_has_vmx_ept_2m_page())
3117 kvm_disable_largepages();
3118
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003119 if (!cpu_has_vmx_ple())
3120 ple_gap = 0;
3121
Yang Zhang01e439b2013-04-11 19:25:12 +08003122 if (!cpu_has_vmx_apicv())
3123 enable_apicv = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08003124
Yang Zhang01e439b2013-04-11 19:25:12 +08003125 if (enable_apicv)
Yang Zhangc7c9c562013-01-25 10:18:51 +08003126 kvm_x86_ops->update_cr8_intercept = NULL;
Yang Zhanga20ed542013-04-11 19:25:15 +08003127 else {
Yang Zhangc7c9c562013-01-25 10:18:51 +08003128 kvm_x86_ops->hwapic_irr_update = NULL;
Yang Zhanga20ed542013-04-11 19:25:15 +08003129 kvm_x86_ops->deliver_posted_interrupt = NULL;
3130 kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy;
3131 }
Yang Zhang83d4c282013-01-25 10:18:49 +08003132
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003133 if (nested)
3134 nested_vmx_setup_ctls_msrs();
3135
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136 return alloc_kvm_area();
3137}
3138
3139static __exit void hardware_unsetup(void)
3140{
3141 free_kvm_area();
3142}
3143
Gleb Natapov14168782013-01-21 15:36:49 +02003144static bool emulation_required(struct kvm_vcpu *vcpu)
3145{
3146 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3147}
3148
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003149static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003150 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003152 if (!emulate_invalid_guest_state) {
3153 /*
3154 * CS and SS RPL should be equal during guest entry according
3155 * to VMX spec, but in reality it is not always so. Since vcpu
3156 * is in the middle of the transition from real mode to
3157 * protected mode it is safe to assume that RPL 0 is a good
3158 * default value.
3159 */
3160 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
3161 save->selector &= ~SELECTOR_RPL_MASK;
3162 save->dpl = save->selector & SELECTOR_RPL_MASK;
3163 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003164 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003165 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003166}
3167
3168static void enter_pmode(struct kvm_vcpu *vcpu)
3169{
3170 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003171 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003172
Gleb Natapovd99e4152012-12-20 16:57:45 +02003173 /*
3174 * Update real mode segment cache. It may be not up-to-date if sement
3175 * register was written while vcpu was in a guest mode.
3176 */
3177 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3178 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3179 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3180 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3181 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3182 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3183
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003184 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185
Avi Kivity2fb92db2011-04-27 19:42:18 +03003186 vmx_segment_cache_clear(vmx);
3187
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003188 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003189
3190 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003191 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3192 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003193 vmcs_writel(GUEST_RFLAGS, flags);
3194
Rusty Russell66aee912007-07-17 23:34:16 +10003195 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3196 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003197
3198 update_exception_bitmap(vcpu);
3199
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003200 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3201 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3202 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3203 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3204 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3205 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206}
3207
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003208static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209{
Mathias Krause772e0312012-08-30 01:30:19 +02003210 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003211 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003212
Gleb Natapovd99e4152012-12-20 16:57:45 +02003213 var.dpl = 0x3;
3214 if (seg == VCPU_SREG_CS)
3215 var.type = 0x3;
3216
3217 if (!emulate_invalid_guest_state) {
3218 var.selector = var.base >> 4;
3219 var.base = var.base & 0xffff0;
3220 var.limit = 0xffff;
3221 var.g = 0;
3222 var.db = 0;
3223 var.present = 1;
3224 var.s = 1;
3225 var.l = 0;
3226 var.unusable = 0;
3227 var.type = 0x3;
3228 var.avl = 0;
3229 if (save->base & 0xf)
3230 printk_once(KERN_WARNING "kvm: segment base is not "
3231 "paragraph aligned when entering "
3232 "protected mode (seg=%d)", seg);
3233 }
3234
3235 vmcs_write16(sf->selector, var.selector);
3236 vmcs_write32(sf->base, var.base);
3237 vmcs_write32(sf->limit, var.limit);
3238 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003239}
3240
3241static void enter_rmode(struct kvm_vcpu *vcpu)
3242{
3243 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003244 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003246 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3247 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3248 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3249 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3250 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003251 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3252 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003253
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003254 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003255
Gleb Natapov776e58e2011-03-13 12:34:27 +02003256 /*
3257 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003258 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003259 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003260 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003261 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3262 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003263
Avi Kivity2fb92db2011-04-27 19:42:18 +03003264 vmx_segment_cache_clear(vmx);
3265
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003266 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003268 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3269
3270 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003271 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003272
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003273 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003274
3275 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003276 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277 update_exception_bitmap(vcpu);
3278
Gleb Natapovd99e4152012-12-20 16:57:45 +02003279 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3280 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3281 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3282 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3283 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3284 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003285
Eddie Dong8668a3c2007-10-10 14:26:45 +08003286 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003287}
3288
Amit Shah401d10d2009-02-20 22:53:37 +05303289static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3290{
3291 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003292 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3293
3294 if (!msr)
3295 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303296
Avi Kivity44ea2b12009-09-06 15:55:37 +03003297 /*
3298 * Force kernel_gs_base reloading before EFER changes, as control
3299 * of this msr depends on is_long_mode().
3300 */
3301 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003302 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303303 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003304 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303305 msr->data = efer;
3306 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003307 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303308
3309 msr->data = efer & ~EFER_LME;
3310 }
3311 setup_msrs(vmx);
3312}
3313
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003314#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003315
3316static void enter_lmode(struct kvm_vcpu *vcpu)
3317{
3318 u32 guest_tr_ar;
3319
Avi Kivity2fb92db2011-04-27 19:42:18 +03003320 vmx_segment_cache_clear(to_vmx(vcpu));
3321
Avi Kivity6aa8b732006-12-10 02:21:36 -08003322 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
3323 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003324 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3325 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003326 vmcs_write32(GUEST_TR_AR_BYTES,
3327 (guest_tr_ar & ~AR_TYPE_MASK)
3328 | AR_TYPE_BUSY_64_TSS);
3329 }
Avi Kivityda38f432010-07-06 11:30:49 +03003330 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003331}
3332
3333static void exit_lmode(struct kvm_vcpu *vcpu)
3334{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003335 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003336 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003337}
3338
3339#endif
3340
Sheng Yang2384d2b2008-01-17 15:14:33 +08003341static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3342{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08003343 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003344 if (enable_ept) {
3345 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3346 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003347 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003348 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003349}
3350
Avi Kivitye8467fd2009-12-29 18:43:06 +02003351static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3352{
3353 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3354
3355 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3356 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3357}
3358
Avi Kivityaff48ba2010-12-05 18:56:11 +02003359static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3360{
3361 if (enable_ept && is_paging(vcpu))
3362 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3363 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3364}
3365
Anthony Liguori25c4c272007-04-27 09:29:21 +03003366static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003367{
Avi Kivityfc78f512009-12-07 12:16:48 +02003368 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3369
3370 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3371 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003372}
3373
Sheng Yang14394422008-04-28 12:24:45 +08003374static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3375{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003376 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3377
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003378 if (!test_bit(VCPU_EXREG_PDPTR,
3379 (unsigned long *)&vcpu->arch.regs_dirty))
3380 return;
3381
Sheng Yang14394422008-04-28 12:24:45 +08003382 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003383 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3384 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3385 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3386 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003387 }
3388}
3389
Avi Kivity8f5d5492009-05-31 18:41:29 +03003390static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3391{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003392 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3393
Avi Kivity8f5d5492009-05-31 18:41:29 +03003394 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003395 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3396 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3397 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3398 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003399 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003400
3401 __set_bit(VCPU_EXREG_PDPTR,
3402 (unsigned long *)&vcpu->arch.regs_avail);
3403 __set_bit(VCPU_EXREG_PDPTR,
3404 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003405}
3406
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003407static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003408
3409static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3410 unsigned long cr0,
3411 struct kvm_vcpu *vcpu)
3412{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003413 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3414 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003415 if (!(cr0 & X86_CR0_PG)) {
3416 /* From paging/starting to nonpaging */
3417 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003418 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003419 (CPU_BASED_CR3_LOAD_EXITING |
3420 CPU_BASED_CR3_STORE_EXITING));
3421 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003422 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003423 } else if (!is_paging(vcpu)) {
3424 /* From nonpaging to paging */
3425 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003426 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003427 ~(CPU_BASED_CR3_LOAD_EXITING |
3428 CPU_BASED_CR3_STORE_EXITING));
3429 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003430 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003431 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003432
3433 if (!(cr0 & X86_CR0_WP))
3434 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003435}
3436
Avi Kivity6aa8b732006-12-10 02:21:36 -08003437static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3438{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003439 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003440 unsigned long hw_cr0;
3441
Gleb Natapov50378782013-02-04 16:00:28 +02003442 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003443 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003444 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003445 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003446 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003447
Gleb Natapov218e7632013-01-21 15:36:45 +02003448 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3449 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003450
Gleb Natapov218e7632013-01-21 15:36:45 +02003451 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3452 enter_rmode(vcpu);
3453 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003454
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003455#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003456 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003457 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003458 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003459 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003460 exit_lmode(vcpu);
3461 }
3462#endif
3463
Avi Kivity089d0342009-03-23 18:26:32 +02003464 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003465 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3466
Avi Kivity02daab22009-12-30 12:40:26 +02003467 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003468 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003469
Avi Kivity6aa8b732006-12-10 02:21:36 -08003470 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003471 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003472 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003473
3474 /* depends on vcpu->arch.cr0 to be set to a new value */
3475 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476}
3477
Sheng Yang14394422008-04-28 12:24:45 +08003478static u64 construct_eptp(unsigned long root_hpa)
3479{
3480 u64 eptp;
3481
3482 /* TODO write the value reading from MSR */
3483 eptp = VMX_EPT_DEFAULT_MT |
3484 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003485 if (enable_ept_ad_bits)
3486 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003487 eptp |= (root_hpa & PAGE_MASK);
3488
3489 return eptp;
3490}
3491
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3493{
Sheng Yang14394422008-04-28 12:24:45 +08003494 unsigned long guest_cr3;
3495 u64 eptp;
3496
3497 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003498 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003499 eptp = construct_eptp(cr3);
3500 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02003501 if (is_paging(vcpu) || is_guest_mode(vcpu))
3502 guest_cr3 = kvm_read_cr3(vcpu);
3503 else
3504 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02003505 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003506 }
3507
Sheng Yang2384d2b2008-01-17 15:14:33 +08003508 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003509 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510}
3511
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003512static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003514 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08003515 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
3516
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003517 if (cr4 & X86_CR4_VMXE) {
3518 /*
3519 * To use VMXON (and later other VMX instructions), a guest
3520 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3521 * So basically the check on whether to allow nested VMX
3522 * is here.
3523 */
3524 if (!nested_vmx_allowed(vcpu))
3525 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003526 }
3527 if (to_vmx(vcpu)->nested.vmxon &&
3528 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003529 return 1;
3530
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003531 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02003532 if (enable_ept) {
3533 if (!is_paging(vcpu)) {
3534 hw_cr4 &= ~X86_CR4_PAE;
3535 hw_cr4 |= X86_CR4_PSE;
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003536 /*
Feng Wue1e746b2014-04-01 17:46:35 +08003537 * SMEP/SMAP is disabled if CPU is in non-paging mode
3538 * in hardware. However KVM always uses paging mode to
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003539 * emulate guest non-paging mode with TDP.
Feng Wue1e746b2014-04-01 17:46:35 +08003540 * To emulate this behavior, SMEP/SMAP needs to be
3541 * manually disabled when guest switches to non-paging
3542 * mode.
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003543 */
Feng Wue1e746b2014-04-01 17:46:35 +08003544 hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP);
Avi Kivitybc230082009-12-08 12:14:42 +02003545 } else if (!(cr4 & X86_CR4_PAE)) {
3546 hw_cr4 &= ~X86_CR4_PAE;
3547 }
3548 }
Sheng Yang14394422008-04-28 12:24:45 +08003549
3550 vmcs_writel(CR4_READ_SHADOW, cr4);
3551 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003552 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003553}
3554
Avi Kivity6aa8b732006-12-10 02:21:36 -08003555static void vmx_get_segment(struct kvm_vcpu *vcpu,
3556 struct kvm_segment *var, int seg)
3557{
Avi Kivitya9179492011-01-03 14:28:52 +02003558 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003559 u32 ar;
3560
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003561 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003562 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003563 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003564 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003565 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003566 var->base = vmx_read_guest_seg_base(vmx, seg);
3567 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3568 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003569 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003570 var->base = vmx_read_guest_seg_base(vmx, seg);
3571 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3572 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3573 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003574 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003575 var->type = ar & 15;
3576 var->s = (ar >> 4) & 1;
3577 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003578 /*
3579 * Some userspaces do not preserve unusable property. Since usable
3580 * segment has to be present according to VMX spec we can use present
3581 * property to amend userspace bug by making unusable segment always
3582 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3583 * segment as unusable.
3584 */
3585 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003586 var->avl = (ar >> 12) & 1;
3587 var->l = (ar >> 13) & 1;
3588 var->db = (ar >> 14) & 1;
3589 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003590}
3591
Avi Kivitya9179492011-01-03 14:28:52 +02003592static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3593{
Avi Kivitya9179492011-01-03 14:28:52 +02003594 struct kvm_segment s;
3595
3596 if (to_vmx(vcpu)->rmode.vm86_active) {
3597 vmx_get_segment(vcpu, &s, seg);
3598 return s.base;
3599 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003600 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003601}
3602
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003603static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003604{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003605 struct vcpu_vmx *vmx = to_vmx(vcpu);
3606
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003607 if (unlikely(vmx->rmode.vm86_active))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003608 return 0;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02003609 else {
3610 int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
3611 return AR_DPL(ar);
Avi Kivity69c73022011-03-07 15:26:44 +02003612 }
Avi Kivity69c73022011-03-07 15:26:44 +02003613}
3614
Avi Kivity653e3102007-05-07 10:55:37 +03003615static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003616{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003617 u32 ar;
3618
Avi Kivityf0495f92012-06-07 17:06:10 +03003619 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003620 ar = 1 << 16;
3621 else {
3622 ar = var->type & 15;
3623 ar |= (var->s & 1) << 4;
3624 ar |= (var->dpl & 3) << 5;
3625 ar |= (var->present & 1) << 7;
3626 ar |= (var->avl & 1) << 12;
3627 ar |= (var->l & 1) << 13;
3628 ar |= (var->db & 1) << 14;
3629 ar |= (var->g & 1) << 15;
3630 }
Avi Kivity653e3102007-05-07 10:55:37 +03003631
3632 return ar;
3633}
3634
3635static void vmx_set_segment(struct kvm_vcpu *vcpu,
3636 struct kvm_segment *var, int seg)
3637{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003638 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003639 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003640
Avi Kivity2fb92db2011-04-27 19:42:18 +03003641 vmx_segment_cache_clear(vmx);
3642
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003643 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3644 vmx->rmode.segs[seg] = *var;
3645 if (seg == VCPU_SREG_TR)
3646 vmcs_write16(sf->selector, var->selector);
3647 else if (var->s)
3648 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003649 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003650 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003651
Avi Kivity653e3102007-05-07 10:55:37 +03003652 vmcs_writel(sf->base, var->base);
3653 vmcs_write32(sf->limit, var->limit);
3654 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003655
3656 /*
3657 * Fix the "Accessed" bit in AR field of segment registers for older
3658 * qemu binaries.
3659 * IA32 arch specifies that at the time of processor reset the
3660 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003661 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003662 * state vmexit when "unrestricted guest" mode is turned on.
3663 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3664 * tree. Newer qemu binaries with that qemu fix would not need this
3665 * kvm hack.
3666 */
3667 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003668 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003669
Gleb Natapovf924d662012-12-12 19:10:55 +02003670 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003671
3672out:
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01003673 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003674}
3675
Avi Kivity6aa8b732006-12-10 02:21:36 -08003676static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3677{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003678 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003679
3680 *db = (ar >> 14) & 1;
3681 *l = (ar >> 13) & 1;
3682}
3683
Gleb Natapov89a27f42010-02-16 10:51:48 +02003684static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003685{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003686 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3687 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003688}
3689
Gleb Natapov89a27f42010-02-16 10:51:48 +02003690static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003691{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003692 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3693 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003694}
3695
Gleb Natapov89a27f42010-02-16 10:51:48 +02003696static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003697{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003698 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3699 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003700}
3701
Gleb Natapov89a27f42010-02-16 10:51:48 +02003702static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003703{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003704 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3705 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003706}
3707
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003708static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3709{
3710 struct kvm_segment var;
3711 u32 ar;
3712
3713 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003714 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003715 if (seg == VCPU_SREG_CS)
3716 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003717 ar = vmx_segment_access_rights(&var);
3718
3719 if (var.base != (var.selector << 4))
3720 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003721 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003722 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003723 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003724 return false;
3725
3726 return true;
3727}
3728
3729static bool code_segment_valid(struct kvm_vcpu *vcpu)
3730{
3731 struct kvm_segment cs;
3732 unsigned int cs_rpl;
3733
3734 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3735 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
3736
Avi Kivity1872a3f2009-01-04 23:26:52 +02003737 if (cs.unusable)
3738 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003739 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
3740 return false;
3741 if (!cs.s)
3742 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003743 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003744 if (cs.dpl > cs_rpl)
3745 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003746 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003747 if (cs.dpl != cs_rpl)
3748 return false;
3749 }
3750 if (!cs.present)
3751 return false;
3752
3753 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3754 return true;
3755}
3756
3757static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3758{
3759 struct kvm_segment ss;
3760 unsigned int ss_rpl;
3761
3762 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3763 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
3764
Avi Kivity1872a3f2009-01-04 23:26:52 +02003765 if (ss.unusable)
3766 return true;
3767 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003768 return false;
3769 if (!ss.s)
3770 return false;
3771 if (ss.dpl != ss_rpl) /* DPL != RPL */
3772 return false;
3773 if (!ss.present)
3774 return false;
3775
3776 return true;
3777}
3778
3779static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3780{
3781 struct kvm_segment var;
3782 unsigned int rpl;
3783
3784 vmx_get_segment(vcpu, &var, seg);
3785 rpl = var.selector & SELECTOR_RPL_MASK;
3786
Avi Kivity1872a3f2009-01-04 23:26:52 +02003787 if (var.unusable)
3788 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003789 if (!var.s)
3790 return false;
3791 if (!var.present)
3792 return false;
3793 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
3794 if (var.dpl < rpl) /* DPL < RPL */
3795 return false;
3796 }
3797
3798 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3799 * rights flags
3800 */
3801 return true;
3802}
3803
3804static bool tr_valid(struct kvm_vcpu *vcpu)
3805{
3806 struct kvm_segment tr;
3807
3808 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3809
Avi Kivity1872a3f2009-01-04 23:26:52 +02003810 if (tr.unusable)
3811 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003812 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3813 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003814 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003815 return false;
3816 if (!tr.present)
3817 return false;
3818
3819 return true;
3820}
3821
3822static bool ldtr_valid(struct kvm_vcpu *vcpu)
3823{
3824 struct kvm_segment ldtr;
3825
3826 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3827
Avi Kivity1872a3f2009-01-04 23:26:52 +02003828 if (ldtr.unusable)
3829 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003830 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3831 return false;
3832 if (ldtr.type != 2)
3833 return false;
3834 if (!ldtr.present)
3835 return false;
3836
3837 return true;
3838}
3839
3840static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3841{
3842 struct kvm_segment cs, ss;
3843
3844 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3845 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3846
3847 return ((cs.selector & SELECTOR_RPL_MASK) ==
3848 (ss.selector & SELECTOR_RPL_MASK));
3849}
3850
3851/*
3852 * Check if guest state is valid. Returns true if valid, false if
3853 * not.
3854 * We assume that registers are always usable
3855 */
3856static bool guest_state_valid(struct kvm_vcpu *vcpu)
3857{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02003858 if (enable_unrestricted_guest)
3859 return true;
3860
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003861 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003862 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003863 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3864 return false;
3865 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3866 return false;
3867 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3868 return false;
3869 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3870 return false;
3871 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3872 return false;
3873 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3874 return false;
3875 } else {
3876 /* protected mode guest state checks */
3877 if (!cs_ss_rpl_check(vcpu))
3878 return false;
3879 if (!code_segment_valid(vcpu))
3880 return false;
3881 if (!stack_segment_valid(vcpu))
3882 return false;
3883 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3884 return false;
3885 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3886 return false;
3887 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3888 return false;
3889 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3890 return false;
3891 if (!tr_valid(vcpu))
3892 return false;
3893 if (!ldtr_valid(vcpu))
3894 return false;
3895 }
3896 /* TODO:
3897 * - Add checks on RIP
3898 * - Add checks on RFLAGS
3899 */
3900
3901 return true;
3902}
3903
Mike Dayd77c26f2007-10-08 09:02:08 -04003904static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003905{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003906 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003907 u16 data = 0;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003908 int r, idx, ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003909
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003910 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003911 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003912 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3913 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003914 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003915 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003916 r = kvm_write_guest_page(kvm, fn++, &data,
3917 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003918 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003919 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003920 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3921 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003922 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003923 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3924 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003925 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003926 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003927 r = kvm_write_guest_page(kvm, fn, &data,
3928 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3929 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02003930 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003931 goto out;
3932
3933 ret = 1;
3934out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003935 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003936 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003937}
3938
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003939static int init_rmode_identity_map(struct kvm *kvm)
3940{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003941 int i, idx, r, ret;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003942 pfn_t identity_map_pfn;
3943 u32 tmp;
3944
Avi Kivity089d0342009-03-23 18:26:32 +02003945 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003946 return 1;
3947 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
3948 printk(KERN_ERR "EPT: identity-mapping pagetable "
3949 "haven't been allocated!\n");
3950 return 0;
3951 }
3952 if (likely(kvm->arch.ept_identity_pagetable_done))
3953 return 1;
3954 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003955 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003956 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003957 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3958 if (r < 0)
3959 goto out;
3960 /* Set up identity-mapping pagetable for EPT in real mode */
3961 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3962 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3963 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3964 r = kvm_write_guest_page(kvm, identity_map_pfn,
3965 &tmp, i * sizeof(tmp), sizeof(tmp));
3966 if (r < 0)
3967 goto out;
3968 }
3969 kvm->arch.ept_identity_pagetable_done = true;
3970 ret = 1;
3971out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003972 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003973 return ret;
3974}
3975
Avi Kivity6aa8b732006-12-10 02:21:36 -08003976static void seg_setup(int seg)
3977{
Mathias Krause772e0312012-08-30 01:30:19 +02003978 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003979 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003980
3981 vmcs_write16(sf->selector, 0);
3982 vmcs_writel(sf->base, 0);
3983 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003984 ar = 0x93;
3985 if (seg == VCPU_SREG_CS)
3986 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003987
3988 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003989}
3990
Sheng Yangf78e0e22007-10-29 09:40:42 +08003991static int alloc_apic_access_page(struct kvm *kvm)
3992{
Xiao Guangrong44841412012-09-07 14:14:20 +08003993 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003994 struct kvm_userspace_memory_region kvm_userspace_mem;
3995 int r = 0;
3996
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003997 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003998 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003999 goto out;
4000 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
4001 kvm_userspace_mem.flags = 0;
4002 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
4003 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09004004 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004005 if (r)
4006 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02004007
Xiao Guangrong44841412012-09-07 14:14:20 +08004008 page = gfn_to_page(kvm, 0xfee00);
4009 if (is_error_page(page)) {
4010 r = -EFAULT;
4011 goto out;
4012 }
4013
4014 kvm->arch.apic_access_page = page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004015out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004016 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08004017 return r;
4018}
4019
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004020static int alloc_identity_pagetable(struct kvm *kvm)
4021{
Xiao Guangrong44841412012-09-07 14:14:20 +08004022 struct page *page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004023 struct kvm_userspace_memory_region kvm_userspace_mem;
4024 int r = 0;
4025
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004026 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004027 if (kvm->arch.ept_identity_pagetable)
4028 goto out;
4029 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
4030 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004031 kvm_userspace_mem.guest_phys_addr =
4032 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004033 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09004034 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004035 if (r)
4036 goto out;
4037
Xiao Guangrong44841412012-09-07 14:14:20 +08004038 page = gfn_to_page(kvm, kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
4039 if (is_error_page(page)) {
4040 r = -EFAULT;
4041 goto out;
4042 }
4043
4044 kvm->arch.ept_identity_pagetable = page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004045out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004046 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004047 return r;
4048}
4049
Sheng Yang2384d2b2008-01-17 15:14:33 +08004050static void allocate_vpid(struct vcpu_vmx *vmx)
4051{
4052 int vpid;
4053
4054 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02004055 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004056 return;
4057 spin_lock(&vmx_vpid_lock);
4058 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
4059 if (vpid < VMX_NR_VPIDS) {
4060 vmx->vpid = vpid;
4061 __set_bit(vpid, vmx_vpid_bitmap);
4062 }
4063 spin_unlock(&vmx_vpid_lock);
4064}
4065
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004066static void free_vpid(struct vcpu_vmx *vmx)
4067{
4068 if (!enable_vpid)
4069 return;
4070 spin_lock(&vmx_vpid_lock);
4071 if (vmx->vpid != 0)
4072 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
4073 spin_unlock(&vmx_vpid_lock);
4074}
4075
Yang Zhang8d146952013-01-25 10:18:50 +08004076#define MSR_TYPE_R 1
4077#define MSR_TYPE_W 2
4078static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4079 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004080{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004081 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004082
4083 if (!cpu_has_vmx_msr_bitmap())
4084 return;
4085
4086 /*
4087 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4088 * have the write-low and read-high bitmap offsets the wrong way round.
4089 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4090 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004091 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004092 if (type & MSR_TYPE_R)
4093 /* read-low */
4094 __clear_bit(msr, msr_bitmap + 0x000 / f);
4095
4096 if (type & MSR_TYPE_W)
4097 /* write-low */
4098 __clear_bit(msr, msr_bitmap + 0x800 / f);
4099
Sheng Yang25c5f222008-03-28 13:18:56 +08004100 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4101 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004102 if (type & MSR_TYPE_R)
4103 /* read-high */
4104 __clear_bit(msr, msr_bitmap + 0x400 / f);
4105
4106 if (type & MSR_TYPE_W)
4107 /* write-high */
4108 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4109
4110 }
4111}
4112
4113static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4114 u32 msr, int type)
4115{
4116 int f = sizeof(unsigned long);
4117
4118 if (!cpu_has_vmx_msr_bitmap())
4119 return;
4120
4121 /*
4122 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4123 * have the write-low and read-high bitmap offsets the wrong way round.
4124 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4125 */
4126 if (msr <= 0x1fff) {
4127 if (type & MSR_TYPE_R)
4128 /* read-low */
4129 __set_bit(msr, msr_bitmap + 0x000 / f);
4130
4131 if (type & MSR_TYPE_W)
4132 /* write-low */
4133 __set_bit(msr, msr_bitmap + 0x800 / f);
4134
4135 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4136 msr &= 0x1fff;
4137 if (type & MSR_TYPE_R)
4138 /* read-high */
4139 __set_bit(msr, msr_bitmap + 0x400 / f);
4140
4141 if (type & MSR_TYPE_W)
4142 /* write-high */
4143 __set_bit(msr, msr_bitmap + 0xc00 / f);
4144
Sheng Yang25c5f222008-03-28 13:18:56 +08004145 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004146}
4147
Avi Kivity58972972009-02-24 22:26:47 +02004148static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4149{
4150 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004151 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4152 msr, MSR_TYPE_R | MSR_TYPE_W);
4153 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4154 msr, MSR_TYPE_R | MSR_TYPE_W);
4155}
4156
4157static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
4158{
4159 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4160 msr, MSR_TYPE_R);
4161 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4162 msr, MSR_TYPE_R);
4163}
4164
4165static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
4166{
4167 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4168 msr, MSR_TYPE_R);
4169 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4170 msr, MSR_TYPE_R);
4171}
4172
4173static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
4174{
4175 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4176 msr, MSR_TYPE_W);
4177 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4178 msr, MSR_TYPE_W);
Avi Kivity58972972009-02-24 22:26:47 +02004179}
4180
Yang Zhang01e439b2013-04-11 19:25:12 +08004181static int vmx_vm_has_apicv(struct kvm *kvm)
4182{
4183 return enable_apicv && irqchip_in_kernel(kvm);
4184}
4185
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004187 * Send interrupt to vcpu via posted interrupt way.
4188 * 1. If target vcpu is running(non-root mode), send posted interrupt
4189 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4190 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4191 * interrupt from PIR in next vmentry.
4192 */
4193static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4194{
4195 struct vcpu_vmx *vmx = to_vmx(vcpu);
4196 int r;
4197
4198 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4199 return;
4200
4201 r = pi_test_and_set_on(&vmx->pi_desc);
4202 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang, Yang Z6ffbbbb2013-04-17 23:11:54 -03004203#ifdef CONFIG_SMP
Yang Zhanga20ed542013-04-11 19:25:15 +08004204 if (!r && (vcpu->mode == IN_GUEST_MODE))
4205 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4206 POSTED_INTR_VECTOR);
4207 else
Zhang, Yang Z6ffbbbb2013-04-17 23:11:54 -03004208#endif
Yang Zhanga20ed542013-04-11 19:25:15 +08004209 kvm_vcpu_kick(vcpu);
4210}
4211
4212static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4213{
4214 struct vcpu_vmx *vmx = to_vmx(vcpu);
4215
4216 if (!pi_test_and_clear_on(&vmx->pi_desc))
4217 return;
4218
4219 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4220}
4221
4222static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu)
4223{
4224 return;
4225}
4226
Avi Kivity6aa8b732006-12-10 02:21:36 -08004227/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004228 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4229 * will not change in the lifetime of the guest.
4230 * Note that host-state that does change is set elsewhere. E.g., host-state
4231 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4232 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004233static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004234{
4235 u32 low32, high32;
4236 unsigned long tmpl;
4237 struct desc_ptr dt;
4238
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07004239 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004240 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
4241 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4242
4243 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004244#ifdef CONFIG_X86_64
4245 /*
4246 * Load null selectors, so we can avoid reloading them in
4247 * __vmx_load_host_state(), in case userspace uses the null selectors
4248 * too (the expected case).
4249 */
4250 vmcs_write16(HOST_DS_SELECTOR, 0);
4251 vmcs_write16(HOST_ES_SELECTOR, 0);
4252#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004253 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4254 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004255#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004256 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4257 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4258
4259 native_store_idt(&dt);
4260 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004261 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004262
Avi Kivity83287ea422012-09-16 15:10:57 +03004263 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004264
4265 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4266 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4267 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4268 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4269
4270 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4271 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4272 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4273 }
4274}
4275
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004276static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4277{
4278 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4279 if (enable_ept)
4280 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004281 if (is_guest_mode(&vmx->vcpu))
4282 vmx->vcpu.arch.cr4_guest_owned_bits &=
4283 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004284 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4285}
4286
Yang Zhang01e439b2013-04-11 19:25:12 +08004287static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4288{
4289 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4290
4291 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4292 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
4293 return pin_based_exec_ctrl;
4294}
4295
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004296static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4297{
4298 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01004299
4300 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4301 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4302
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004303 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
4304 exec_control &= ~CPU_BASED_TPR_SHADOW;
4305#ifdef CONFIG_X86_64
4306 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4307 CPU_BASED_CR8_LOAD_EXITING;
4308#endif
4309 }
4310 if (!enable_ept)
4311 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4312 CPU_BASED_CR3_LOAD_EXITING |
4313 CPU_BASED_INVLPG_EXITING;
4314 return exec_control;
4315}
4316
4317static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4318{
4319 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
4320 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4321 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4322 if (vmx->vpid == 0)
4323 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4324 if (!enable_ept) {
4325 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4326 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00004327 /* Enable INVPCID for non-ept guests may cause performance regression. */
4328 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004329 }
4330 if (!enable_unrestricted_guest)
4331 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4332 if (!ple_gap)
4333 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Yang Zhangc7c9c562013-01-25 10:18:51 +08004334 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4335 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4336 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004337 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03004338 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4339 (handle_vmptrld).
4340 We can NOT enable shadow_vmcs here because we don't have yet
4341 a current VMCS12
4342 */
4343 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004344 return exec_control;
4345}
4346
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004347static void ept_set_mmio_spte_mask(void)
4348{
4349 /*
4350 * EPT Misconfigurations can be generated if the value of bits 2:0
4351 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08004352 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004353 * spte.
4354 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08004355 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004356}
4357
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004358/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004359 * Sets up the vmcs for emulated real mode.
4360 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004361static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004362{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004363#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004364 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004365#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004366 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004367
Avi Kivity6aa8b732006-12-10 02:21:36 -08004368 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004369 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4370 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004371
Abel Gordon4607c2d2013-04-18 14:35:55 +03004372 if (enable_shadow_vmcs) {
4373 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
4374 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
4375 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004376 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02004377 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08004378
Avi Kivity6aa8b732006-12-10 02:21:36 -08004379 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4380
Avi Kivity6aa8b732006-12-10 02:21:36 -08004381 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08004382 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004383
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004384 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004385
Sheng Yang83ff3b92007-11-21 14:33:25 +08004386 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004387 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4388 vmx_secondary_exec_control(vmx));
Sheng Yang83ff3b92007-11-21 14:33:25 +08004389 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004390
Yang Zhang01e439b2013-04-11 19:25:12 +08004391 if (vmx_vm_has_apicv(vmx->vcpu.kvm)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004392 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4393 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4394 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4395 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4396
4397 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004398
4399 vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
4400 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004401 }
4402
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004403 if (ple_gap) {
4404 vmcs_write32(PLE_GAP, ple_gap);
4405 vmcs_write32(PLE_WINDOW, ple_window);
4406 }
4407
Xiao Guangrongc3707952011-07-12 03:28:04 +08004408 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4409 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004410 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4411
Avi Kivity9581d442010-10-19 16:46:55 +02004412 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4413 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004414 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004415#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004416 rdmsrl(MSR_FS_BASE, a);
4417 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
4418 rdmsrl(MSR_GS_BASE, a);
4419 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
4420#else
4421 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4422 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
4423#endif
4424
Eddie Dong2cc51562007-05-21 07:28:09 +03004425 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4426 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004427 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03004428 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004429 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004430
Sheng Yang468d4722008-10-09 16:01:55 +08004431 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004432 u32 msr_low, msr_high;
4433 u64 host_pat;
Sheng Yang468d4722008-10-09 16:01:55 +08004434 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
4435 host_pat = msr_low | ((u64) msr_high << 32);
4436 /* Write the default value follow host pat */
4437 vmcs_write64(GUEST_IA32_PAT, host_pat);
4438 /* Keep arch.pat sync with GUEST_IA32_PAT */
4439 vmx->vcpu.arch.pat = host_pat;
4440 }
4441
Paolo Bonzini03916db2014-07-24 14:21:57 +02004442 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004443 u32 index = vmx_msr_index[i];
4444 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004445 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004446
4447 if (rdmsr_safe(index, &data_low, &data_high) < 0)
4448 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08004449 if (wrmsr_safe(index, data_low, data_high) < 0)
4450 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03004451 vmx->guest_msrs[j].index = i;
4452 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02004453 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004454 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004455 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004456
Gleb Natapov2961e8762013-11-25 15:37:13 +02004457
4458 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004459
4460 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02004461 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004462
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004463 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004464 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004465
4466 return 0;
4467}
4468
Jan Kiszka57f252f2013-03-12 10:20:24 +01004469static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004470{
4471 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004472 struct msr_data apic_base_msr;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004473
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004474 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004475
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004476 vmx->soft_vnmi_blocked = 0;
4477
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004478 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004479 kvm_set_cr8(&vmx->vcpu, 0);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004480 apic_base_msr.data = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004481 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Jan Kiszka58cb6282014-01-24 16:48:44 +01004482 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4483 apic_base_msr.host_initiated = true;
4484 kvm_set_apic_base(&vmx->vcpu, &apic_base_msr);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004485
Avi Kivity2fb92db2011-04-27 19:42:18 +03004486 vmx_segment_cache_clear(vmx);
4487
Avi Kivity5706be02008-08-20 15:07:31 +03004488 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004489 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzini04b66832013-03-19 16:30:26 +01004490 vmcs_write32(GUEST_CS_BASE, 0xffff0000);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004491
4492 seg_setup(VCPU_SREG_DS);
4493 seg_setup(VCPU_SREG_ES);
4494 seg_setup(VCPU_SREG_FS);
4495 seg_setup(VCPU_SREG_GS);
4496 seg_setup(VCPU_SREG_SS);
4497
4498 vmcs_write16(GUEST_TR_SELECTOR, 0);
4499 vmcs_writel(GUEST_TR_BASE, 0);
4500 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4501 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4502
4503 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4504 vmcs_writel(GUEST_LDTR_BASE, 0);
4505 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4506 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4507
4508 vmcs_write32(GUEST_SYSENTER_CS, 0);
4509 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4510 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4511
4512 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01004513 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004514
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004515 vmcs_writel(GUEST_GDTR_BASE, 0);
4516 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4517
4518 vmcs_writel(GUEST_IDTR_BASE, 0);
4519 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4520
Anthony Liguori443381a2010-12-06 10:53:38 -06004521 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004522 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
4523 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
4524
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004525 /* Special registers */
4526 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4527
4528 setup_msrs(vmx);
4529
Avi Kivity6aa8b732006-12-10 02:21:36 -08004530 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4531
Sheng Yangf78e0e22007-10-29 09:40:42 +08004532 if (cpu_has_vmx_tpr_shadow()) {
4533 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
4534 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
4535 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09004536 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004537 vmcs_write32(TPR_THRESHOLD, 0);
4538 }
4539
4540 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4541 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004542 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004543
Yang Zhang01e439b2013-04-11 19:25:12 +08004544 if (vmx_vm_has_apicv(vcpu->kvm))
4545 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
4546
Sheng Yang2384d2b2008-01-17 15:14:33 +08004547 if (vmx->vpid != 0)
4548 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4549
Eduardo Habkostfa400522009-10-24 02:49:58 -02004550 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004551 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004552 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004553 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004554 vmx_fpu_activate(&vmx->vcpu);
4555 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004556
Gui Jianfengb9d762f2010-06-07 10:32:29 +08004557 vpid_sync_context(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004558}
4559
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004560/*
4561 * In nested virtualization, check if L1 asked to exit on external interrupts.
4562 * For most existing hypervisors, this will always return true.
4563 */
4564static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4565{
4566 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4567 PIN_BASED_EXT_INTR_MASK;
4568}
4569
Bandan Das77b0f5d2014-04-19 18:17:45 -04004570/*
4571 * In nested virtualization, check if L1 has set
4572 * VM_EXIT_ACK_INTR_ON_EXIT
4573 */
4574static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu)
4575{
4576 return get_vmcs12(vcpu)->vm_exit_controls &
4577 VM_EXIT_ACK_INTR_ON_EXIT;
4578}
4579
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004580static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
4581{
4582 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4583 PIN_BASED_NMI_EXITING;
4584}
4585
Jan Kiszkac9a79532014-03-07 20:03:15 +01004586static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004587{
4588 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02004589
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004590 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4591 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
4592 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4593}
4594
Jan Kiszkac9a79532014-03-07 20:03:15 +01004595static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004596{
4597 u32 cpu_based_vm_exec_control;
4598
Jan Kiszkac9a79532014-03-07 20:03:15 +01004599 if (!cpu_has_virtual_nmis() ||
4600 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
4601 enable_irq_window(vcpu);
4602 return;
4603 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004604
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004605 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4606 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
4607 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4608}
4609
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004610static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004611{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004612 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004613 uint32_t intr;
4614 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004615
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004616 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004617
Avi Kivityfa89a812008-09-01 15:57:51 +03004618 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004619 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004620 int inc_eip = 0;
4621 if (vcpu->arch.interrupt.soft)
4622 inc_eip = vcpu->arch.event_exit_inst_len;
4623 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004624 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004625 return;
4626 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004627 intr = irq | INTR_INFO_VALID_MASK;
4628 if (vcpu->arch.interrupt.soft) {
4629 intr |= INTR_TYPE_SOFT_INTR;
4630 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4631 vmx->vcpu.arch.event_exit_inst_len);
4632 } else
4633 intr |= INTR_TYPE_EXT_INTR;
4634 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03004635}
4636
Sheng Yangf08864b2008-05-15 18:23:25 +08004637static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4638{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004639 struct vcpu_vmx *vmx = to_vmx(vcpu);
4640
Nadav Har'El0b6ac342011-05-25 23:13:36 +03004641 if (is_guest_mode(vcpu))
4642 return;
4643
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004644 if (!cpu_has_virtual_nmis()) {
4645 /*
4646 * Tracking the NMI-blocked state in software is built upon
4647 * finding the next open IRQ window. This, in turn, depends on
4648 * well-behaving guests: They have to keep IRQs disabled at
4649 * least as long as the NMI handler runs. Otherwise we may
4650 * cause NMI nesting, maybe breaking the guest. But as this is
4651 * highly unlikely, we can live with the residual risk.
4652 */
4653 vmx->soft_vnmi_blocked = 1;
4654 vmx->vnmi_blocked_time = 0;
4655 }
4656
Jan Kiszka487b3912008-09-26 09:30:56 +02004657 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02004658 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004659 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004660 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004661 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004662 return;
4663 }
Sheng Yangf08864b2008-05-15 18:23:25 +08004664 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4665 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08004666}
4667
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004668static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
4669{
4670 if (!cpu_has_virtual_nmis())
4671 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02004672 if (to_vmx(vcpu)->nmi_known_unmasked)
4673 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03004674 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004675}
4676
4677static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4678{
4679 struct vcpu_vmx *vmx = to_vmx(vcpu);
4680
4681 if (!cpu_has_virtual_nmis()) {
4682 if (vmx->soft_vnmi_blocked != masked) {
4683 vmx->soft_vnmi_blocked = masked;
4684 vmx->vnmi_blocked_time = 0;
4685 }
4686 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02004687 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004688 if (masked)
4689 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4690 GUEST_INTR_STATE_NMI);
4691 else
4692 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4693 GUEST_INTR_STATE_NMI);
4694 }
4695}
4696
Jan Kiszka2505dc92013-04-14 12:12:47 +02004697static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
4698{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004699 if (to_vmx(vcpu)->nested.nested_run_pending)
4700 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004701
Jan Kiszka2505dc92013-04-14 12:12:47 +02004702 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
4703 return 0;
4704
4705 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4706 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4707 | GUEST_INTR_STATE_NMI));
4708}
4709
Gleb Natapov78646122009-03-23 12:12:11 +02004710static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4711{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004712 return (!to_vmx(vcpu)->nested.nested_run_pending &&
4713 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03004714 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4715 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02004716}
4717
Izik Eiduscbc94022007-10-25 00:29:55 +02004718static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4719{
4720 int ret;
4721 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08004722 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02004723 .guest_phys_addr = addr,
4724 .memory_size = PAGE_SIZE * 3,
4725 .flags = 0,
4726 };
4727
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09004728 ret = kvm_set_memory_region(kvm, &tss_mem);
Izik Eiduscbc94022007-10-25 00:29:55 +02004729 if (ret)
4730 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004731 kvm->arch.tss_addr = addr;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004732 if (!init_rmode_tss(kvm))
4733 return -ENOMEM;
4734
Izik Eiduscbc94022007-10-25 00:29:55 +02004735 return 0;
4736}
4737
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004738static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004739{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004740 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004741 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01004742 /*
4743 * Update instruction length as we may reinject the exception
4744 * from user space while in guest debugging mode.
4745 */
4746 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4747 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004748 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004749 return false;
4750 /* fall through */
4751 case DB_VECTOR:
4752 if (vcpu->guest_debug &
4753 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4754 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004755 /* fall through */
4756 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004757 case OF_VECTOR:
4758 case BR_VECTOR:
4759 case UD_VECTOR:
4760 case DF_VECTOR:
4761 case SS_VECTOR:
4762 case GP_VECTOR:
4763 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004764 return true;
4765 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004766 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004767 return false;
4768}
4769
4770static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4771 int vec, u32 err_code)
4772{
4773 /*
4774 * Instruction with address size override prefix opcode 0x67
4775 * Cause the #SS fault with 0 error code in VM86 mode.
4776 */
4777 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
4778 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
4779 if (vcpu->arch.halt_request) {
4780 vcpu->arch.halt_request = 0;
4781 return kvm_emulate_halt(vcpu);
4782 }
4783 return 1;
4784 }
4785 return 0;
4786 }
4787
4788 /*
4789 * Forward all other exceptions that are valid in real mode.
4790 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4791 * the required debugging infrastructure rework.
4792 */
4793 kvm_queue_exception(vcpu, vec);
4794 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004795}
4796
Andi Kleena0861c02009-06-08 17:37:09 +08004797/*
4798 * Trigger machine check on the host. We assume all the MSRs are already set up
4799 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4800 * We pass a fake environment to the machine check handler because we want
4801 * the guest to be always treated like user space, no matter what context
4802 * it used internally.
4803 */
4804static void kvm_machine_check(void)
4805{
4806#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4807 struct pt_regs regs = {
4808 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4809 .flags = X86_EFLAGS_IF,
4810 };
4811
4812 do_machine_check(&regs, 0);
4813#endif
4814}
4815
Avi Kivity851ba692009-08-24 11:10:17 +03004816static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004817{
4818 /* already handled by vcpu_run */
4819 return 1;
4820}
4821
Avi Kivity851ba692009-08-24 11:10:17 +03004822static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004823{
Avi Kivity1155f762007-11-22 11:30:47 +02004824 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004825 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004826 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004827 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004828 u32 vect_info;
4829 enum emulation_result er;
4830
Avi Kivity1155f762007-11-22 11:30:47 +02004831 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02004832 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004833
Andi Kleena0861c02009-06-08 17:37:09 +08004834 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03004835 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004836
Jan Kiszkae4a41882008-09-26 09:30:46 +02004837 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02004838 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004839
4840 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03004841 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004842 return 1;
4843 }
4844
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004845 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01004846 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004847 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02004848 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004849 return 1;
4850 }
4851
Avi Kivity6aa8b732006-12-10 02:21:36 -08004852 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004853 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004854 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004855
4856 /*
4857 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4858 * MMIO, it is better to report an internal error.
4859 * See the comments in vmx_handle_exit.
4860 */
4861 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4862 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4863 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4864 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
4865 vcpu->run->internal.ndata = 2;
4866 vcpu->run->internal.data[0] = vect_info;
4867 vcpu->run->internal.data[1] = intr_info;
4868 return 0;
4869 }
4870
Avi Kivity6aa8b732006-12-10 02:21:36 -08004871 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08004872 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02004873 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004874 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004875 trace_kvm_page_fault(cr2, error_code);
4876
Gleb Natapov3298b752009-05-11 13:35:46 +03004877 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03004878 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01004879 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004880 }
4881
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004882 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004883
4884 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4885 return handle_rmode_exception(vcpu, ex_no, error_code);
4886
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004887 switch (ex_no) {
4888 case DB_VECTOR:
4889 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4890 if (!(vcpu->guest_debug &
4891 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01004892 vcpu->arch.dr6 &= ~15;
Nadav Amit6f43ed02014-07-15 17:37:46 +03004893 vcpu->arch.dr6 |= dr6 | DR6_RTM;
Huw Daviesfd2a4452014-04-16 10:02:51 +01004894 if (!(dr6 & ~DR6_RESERVED)) /* icebp */
4895 skip_emulated_instruction(vcpu);
4896
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004897 kvm_queue_exception(vcpu, DB_VECTOR);
4898 return 1;
4899 }
4900 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4901 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4902 /* fall through */
4903 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01004904 /*
4905 * Update instruction length as we may reinject #BP from
4906 * user space while in guest debugging mode. Reading it for
4907 * #DB as well causes no harm, it is not used in that case.
4908 */
4909 vmx->vcpu.arch.event_exit_inst_len =
4910 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004911 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004912 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004913 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4914 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004915 break;
4916 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004917 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4918 kvm_run->ex.exception = ex_no;
4919 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004920 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004921 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004922 return 0;
4923}
4924
Avi Kivity851ba692009-08-24 11:10:17 +03004925static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004926{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004927 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004928 return 1;
4929}
4930
Avi Kivity851ba692009-08-24 11:10:17 +03004931static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004932{
Avi Kivity851ba692009-08-24 11:10:17 +03004933 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08004934 return 0;
4935}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004936
Avi Kivity851ba692009-08-24 11:10:17 +03004937static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004938{
He, Qingbfdaab02007-09-12 14:18:28 +08004939 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01004940 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004941 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004942
He, Qingbfdaab02007-09-12 14:18:28 +08004943 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02004944 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004945 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004946
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004947 ++vcpu->stat.io_exits;
4948
4949 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01004950 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004951
4952 port = exit_qualification >> 16;
4953 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01004954 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004955
4956 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004957}
4958
Ingo Molnar102d8322007-02-19 14:37:47 +02004959static void
4960vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4961{
4962 /*
4963 * Patch in the VMCALL instruction:
4964 */
4965 hypercall[0] = 0x0f;
4966 hypercall[1] = 0x01;
4967 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004968}
4969
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02004970static bool nested_cr0_valid(struct vmcs12 *vmcs12, unsigned long val)
4971{
4972 unsigned long always_on = VMXON_CR0_ALWAYSON;
4973
4974 if (nested_vmx_secondary_ctls_high &
4975 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4976 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4977 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
4978 return (val & always_on) == always_on;
4979}
4980
Guo Chao0fa06072012-06-28 15:16:19 +08004981/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004982static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4983{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004984 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004985 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4986 unsigned long orig_val = val;
4987
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004988 /*
4989 * We get here when L2 changed cr0 in a way that did not change
4990 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004991 * but did change L0 shadowed bits. So we first calculate the
4992 * effective cr0 value that L1 would like to write into the
4993 * hardware. It consists of the L2-owned bits from the new
4994 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004995 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004996 val = (val & ~vmcs12->cr0_guest_host_mask) |
4997 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4998
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02004999 if (!nested_cr0_valid(vmcs12, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005000 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005001
5002 if (kvm_set_cr0(vcpu, val))
5003 return 1;
5004 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005005 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005006 } else {
5007 if (to_vmx(vcpu)->nested.vmxon &&
5008 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
5009 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005010 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005011 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005012}
5013
5014static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5015{
5016 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005017 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5018 unsigned long orig_val = val;
5019
5020 /* analogously to handle_set_cr0 */
5021 val = (val & ~vmcs12->cr4_guest_host_mask) |
5022 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5023 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005024 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01005025 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005026 return 0;
5027 } else
5028 return kvm_set_cr4(vcpu, val);
5029}
5030
5031/* called to set cr0 as approriate for clts instruction exit. */
5032static void handle_clts(struct kvm_vcpu *vcpu)
5033{
5034 if (is_guest_mode(vcpu)) {
5035 /*
5036 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
5037 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
5038 * just pretend it's off (also in arch.cr0 for fpu_activate).
5039 */
5040 vmcs_writel(CR0_READ_SHADOW,
5041 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
5042 vcpu->arch.cr0 &= ~X86_CR0_TS;
5043 } else
5044 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5045}
5046
Avi Kivity851ba692009-08-24 11:10:17 +03005047static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005048{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005049 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005050 int cr;
5051 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03005052 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005053
He, Qingbfdaab02007-09-12 14:18:28 +08005054 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005055 cr = exit_qualification & 15;
5056 reg = (exit_qualification >> 8) & 15;
5057 switch ((exit_qualification >> 4) & 3) {
5058 case 0: /* mov to cr */
Nadav Amit1e32c072014-06-18 17:19:25 +03005059 val = kvm_register_readl(vcpu, reg);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005060 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005061 switch (cr) {
5062 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005063 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005064 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005065 return 1;
5066 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005067 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005068 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005069 return 1;
5070 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005071 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005072 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005073 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005074 case 8: {
5075 u8 cr8_prev = kvm_get_cr8(vcpu);
Nadav Amit1e32c072014-06-18 17:19:25 +03005076 u8 cr8 = (u8)val;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005077 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005078 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005079 if (irqchip_in_kernel(vcpu->kvm))
5080 return 1;
5081 if (cr8_prev <= cr8)
5082 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005083 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005084 return 0;
5085 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005086 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005087 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005088 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005089 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005090 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005091 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005092 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005093 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005094 case 1: /*mov from cr*/
5095 switch (cr) {
5096 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005097 val = kvm_read_cr3(vcpu);
5098 kvm_register_write(vcpu, reg, val);
5099 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100 skip_emulated_instruction(vcpu);
5101 return 1;
5102 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005103 val = kvm_get_cr8(vcpu);
5104 kvm_register_write(vcpu, reg, val);
5105 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005106 skip_emulated_instruction(vcpu);
5107 return 1;
5108 }
5109 break;
5110 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005111 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005112 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005113 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005114
5115 skip_emulated_instruction(vcpu);
5116 return 1;
5117 default:
5118 break;
5119 }
Avi Kivity851ba692009-08-24 11:10:17 +03005120 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005121 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005122 (int)(exit_qualification >> 4) & 3, cr);
5123 return 0;
5124}
5125
Avi Kivity851ba692009-08-24 11:10:17 +03005126static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005127{
He, Qingbfdaab02007-09-12 14:18:28 +08005128 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005129 int dr, reg;
5130
Jan Kiszkaf2483412010-01-20 18:20:20 +01005131 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005132 if (!kvm_require_cpl(vcpu, 0))
5133 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005134 dr = vmcs_readl(GUEST_DR7);
5135 if (dr & DR7_GD) {
5136 /*
5137 * As the vm-exit takes precedence over the debug trap, we
5138 * need to emulate the latter, either for the host or the
5139 * guest debugging itself.
5140 */
5141 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005142 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
5143 vcpu->run->debug.arch.dr7 = dr;
5144 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005145 vmcs_readl(GUEST_CS_BASE) +
5146 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03005147 vcpu->run->debug.arch.exception = DB_VECTOR;
5148 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005149 return 0;
5150 } else {
5151 vcpu->arch.dr7 &= ~DR7_GD;
Nadav Amit6f43ed02014-07-15 17:37:46 +03005152 vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005153 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
5154 kvm_queue_exception(vcpu, DB_VECTOR);
5155 return 1;
5156 }
5157 }
5158
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005159 if (vcpu->guest_debug == 0) {
5160 u32 cpu_based_vm_exec_control;
5161
5162 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5163 cpu_based_vm_exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5164 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5165
5166 /*
5167 * No more DR vmexits; force a reload of the debug registers
5168 * and reenter on this instruction. The next vmexit will
5169 * retrieve the full state of the debug registers.
5170 */
5171 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5172 return 1;
5173 }
5174
He, Qingbfdaab02007-09-12 14:18:28 +08005175 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005176 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5177 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5178 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005179 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005180
5181 if (kvm_get_dr(vcpu, dr, &val))
5182 return 1;
5183 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005184 } else
Nadav Amit57773922014-06-18 17:19:23 +03005185 if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005186 return 1;
5187
Avi Kivity6aa8b732006-12-10 02:21:36 -08005188 skip_emulated_instruction(vcpu);
5189 return 1;
5190}
5191
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005192static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5193{
5194 return vcpu->arch.dr6;
5195}
5196
5197static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5198{
5199}
5200
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005201static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5202{
5203 u32 cpu_based_vm_exec_control;
5204
5205 get_debugreg(vcpu->arch.db[0], 0);
5206 get_debugreg(vcpu->arch.db[1], 1);
5207 get_debugreg(vcpu->arch.db[2], 2);
5208 get_debugreg(vcpu->arch.db[3], 3);
5209 get_debugreg(vcpu->arch.dr6, 6);
5210 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5211
5212 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
5213
5214 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5215 cpu_based_vm_exec_control |= CPU_BASED_MOV_DR_EXITING;
5216 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5217}
5218
Gleb Natapov020df072010-04-13 10:05:23 +03005219static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5220{
5221 vmcs_writel(GUEST_DR7, val);
5222}
5223
Avi Kivity851ba692009-08-24 11:10:17 +03005224static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005225{
Avi Kivity06465c52007-02-28 20:46:53 +02005226 kvm_emulate_cpuid(vcpu);
5227 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005228}
5229
Avi Kivity851ba692009-08-24 11:10:17 +03005230static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005231{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005232 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08005233 u64 data;
5234
5235 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02005236 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005237 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005238 return 1;
5239 }
5240
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005241 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005242
Avi Kivity6aa8b732006-12-10 02:21:36 -08005243 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005244 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
5245 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005246 skip_emulated_instruction(vcpu);
5247 return 1;
5248}
5249
Avi Kivity851ba692009-08-24 11:10:17 +03005250static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005251{
Will Auld8fe8ab42012-11-29 12:42:12 -08005252 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005253 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5254 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5255 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005256
Will Auld8fe8ab42012-11-29 12:42:12 -08005257 msr.data = data;
5258 msr.index = ecx;
5259 msr.host_initiated = false;
5260 if (vmx_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005261 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005262 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005263 return 1;
5264 }
5265
Avi Kivity59200272010-01-25 19:47:02 +02005266 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005267 skip_emulated_instruction(vcpu);
5268 return 1;
5269}
5270
Avi Kivity851ba692009-08-24 11:10:17 +03005271static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005272{
Avi Kivity3842d132010-07-27 12:30:24 +03005273 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005274 return 1;
5275}
5276
Avi Kivity851ba692009-08-24 11:10:17 +03005277static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005278{
Eddie Dong85f455f2007-07-06 12:20:49 +03005279 u32 cpu_based_vm_exec_control;
5280
5281 /* clear pending irq */
5282 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5283 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5284 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005285
Avi Kivity3842d132010-07-27 12:30:24 +03005286 kvm_make_request(KVM_REQ_EVENT, vcpu);
5287
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005288 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005289
Dor Laorc1150d82007-01-05 16:36:24 -08005290 /*
5291 * If the user space waits to inject interrupts, exit as soon as
5292 * possible
5293 */
Gleb Natapov80618232009-04-21 17:44:56 +03005294 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03005295 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03005296 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03005297 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08005298 return 0;
5299 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005300 return 1;
5301}
5302
Avi Kivity851ba692009-08-24 11:10:17 +03005303static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005304{
5305 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03005306 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005307}
5308
Avi Kivity851ba692009-08-24 11:10:17 +03005309static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005310{
Dor Laor510043d2007-02-19 18:25:43 +02005311 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005312 kvm_emulate_hypercall(vcpu);
5313 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02005314}
5315
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005316static int handle_invd(struct kvm_vcpu *vcpu)
5317{
Andre Przywara51d8b662010-12-21 11:12:02 +01005318 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005319}
5320
Avi Kivity851ba692009-08-24 11:10:17 +03005321static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005322{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005323 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005324
5325 kvm_mmu_invlpg(vcpu, exit_qualification);
5326 skip_emulated_instruction(vcpu);
5327 return 1;
5328}
5329
Avi Kivityfee84b02011-11-10 14:57:25 +02005330static int handle_rdpmc(struct kvm_vcpu *vcpu)
5331{
5332 int err;
5333
5334 err = kvm_rdpmc(vcpu);
5335 kvm_complete_insn_gp(vcpu, err);
5336
5337 return 1;
5338}
5339
Avi Kivity851ba692009-08-24 11:10:17 +03005340static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005341{
5342 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005343 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005344 return 1;
5345}
5346
Dexuan Cui2acf9232010-06-10 11:27:12 +08005347static int handle_xsetbv(struct kvm_vcpu *vcpu)
5348{
5349 u64 new_bv = kvm_read_edx_eax(vcpu);
5350 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5351
5352 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5353 skip_emulated_instruction(vcpu);
5354 return 1;
5355}
5356
Avi Kivity851ba692009-08-24 11:10:17 +03005357static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005358{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005359 if (likely(fasteoi)) {
5360 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5361 int access_type, offset;
5362
5363 access_type = exit_qualification & APIC_ACCESS_TYPE;
5364 offset = exit_qualification & APIC_ACCESS_OFFSET;
5365 /*
5366 * Sane guest uses MOV to write EOI, with written value
5367 * not cared. So make a short-circuit here by avoiding
5368 * heavy instruction emulation.
5369 */
5370 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5371 (offset == APIC_EOI)) {
5372 kvm_lapic_set_eoi(vcpu);
5373 skip_emulated_instruction(vcpu);
5374 return 1;
5375 }
5376 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005377 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005378}
5379
Yang Zhangc7c9c562013-01-25 10:18:51 +08005380static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5381{
5382 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5383 int vector = exit_qualification & 0xff;
5384
5385 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5386 kvm_apic_set_eoi_accelerated(vcpu, vector);
5387 return 1;
5388}
5389
Yang Zhang83d4c282013-01-25 10:18:49 +08005390static int handle_apic_write(struct kvm_vcpu *vcpu)
5391{
5392 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5393 u32 offset = exit_qualification & 0xfff;
5394
5395 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5396 kvm_apic_write_nodecode(vcpu, offset);
5397 return 1;
5398}
5399
Avi Kivity851ba692009-08-24 11:10:17 +03005400static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005401{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005402 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005403 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005404 bool has_error_code = false;
5405 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005406 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005407 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005408
5409 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005410 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005411 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005412
5413 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5414
5415 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005416 if (reason == TASK_SWITCH_GATE && idt_v) {
5417 switch (type) {
5418 case INTR_TYPE_NMI_INTR:
5419 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005420 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005421 break;
5422 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005423 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005424 kvm_clear_interrupt_queue(vcpu);
5425 break;
5426 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005427 if (vmx->idt_vectoring_info &
5428 VECTORING_INFO_DELIVER_CODE_MASK) {
5429 has_error_code = true;
5430 error_code =
5431 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5432 }
5433 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005434 case INTR_TYPE_SOFT_EXCEPTION:
5435 kvm_clear_exception_queue(vcpu);
5436 break;
5437 default:
5438 break;
5439 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005440 }
Izik Eidus37817f22008-03-24 23:14:53 +02005441 tss_selector = exit_qualification;
5442
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005443 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5444 type != INTR_TYPE_EXT_INTR &&
5445 type != INTR_TYPE_NMI_INTR))
5446 skip_emulated_instruction(vcpu);
5447
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005448 if (kvm_task_switch(vcpu, tss_selector,
5449 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
5450 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03005451 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5452 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5453 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005454 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03005455 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005456
5457 /* clear all local breakpoint enable flags */
Nadav Amit1f854112014-05-19 09:50:50 +03005458 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~0x55);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005459
5460 /*
5461 * TODO: What about debug traps on tss switch?
5462 * Are we supposed to inject them and update dr6?
5463 */
5464
5465 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02005466}
5467
Avi Kivity851ba692009-08-24 11:10:17 +03005468static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005469{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005470 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005471 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005472 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005473 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08005474
Sheng Yangf9c617f2009-03-25 10:08:52 +08005475 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08005476
Sheng Yang14394422008-04-28 12:24:45 +08005477 gla_validity = (exit_qualification >> 7) & 0x3;
5478 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
5479 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
5480 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
5481 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08005482 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08005483 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
5484 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03005485 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5486 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03005487 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08005488 }
5489
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005490 /*
5491 * EPT violation happened while executing iret from NMI,
5492 * "blocked by NMI" bit has to be set before next VM entry.
5493 * There are errata that may cause this bit to not be set:
5494 * AAK134, BY25.
5495 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005496 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
5497 cpu_has_virtual_nmis() &&
5498 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005499 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5500
Sheng Yang14394422008-04-28 12:24:45 +08005501 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005502 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005503
5504 /* It is a write fault? */
5505 error_code = exit_qualification & (1U << 1);
Yang Zhang25d92082013-08-06 12:00:32 +03005506 /* It is a fetch fault? */
5507 error_code |= (exit_qualification & (1U << 2)) << 2;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005508 /* ept page table is present? */
5509 error_code |= (exit_qualification >> 3) & 0x1;
5510
Yang Zhang25d92082013-08-06 12:00:32 +03005511 vcpu->arch.exit_qualification = exit_qualification;
5512
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005513 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005514}
5515
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005516static u64 ept_rsvd_mask(u64 spte, int level)
5517{
5518 int i;
5519 u64 mask = 0;
5520
5521 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
5522 mask |= (1ULL << i);
5523
5524 if (level > 2)
5525 /* bits 7:3 reserved */
5526 mask |= 0xf8;
5527 else if (level == 2) {
5528 if (spte & (1ULL << 7))
5529 /* 2MB ref, bits 20:12 reserved */
5530 mask |= 0x1ff000;
5531 else
5532 /* bits 6:3 reserved */
5533 mask |= 0x78;
5534 }
5535
5536 return mask;
5537}
5538
5539static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
5540 int level)
5541{
5542 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
5543
5544 /* 010b (write-only) */
5545 WARN_ON((spte & 0x7) == 0x2);
5546
5547 /* 110b (write/execute) */
5548 WARN_ON((spte & 0x7) == 0x6);
5549
5550 /* 100b (execute-only) and value not supported by logical processor */
5551 if (!cpu_has_vmx_ept_execute_only())
5552 WARN_ON((spte & 0x7) == 0x4);
5553
5554 /* not 000b */
5555 if ((spte & 0x7)) {
5556 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
5557
5558 if (rsvd_bits != 0) {
5559 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
5560 __func__, rsvd_bits);
5561 WARN_ON(1);
5562 }
5563
5564 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
5565 u64 ept_mem_type = (spte & 0x38) >> 3;
5566
5567 if (ept_mem_type == 2 || ept_mem_type == 3 ||
5568 ept_mem_type == 7) {
5569 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
5570 __func__, ept_mem_type);
5571 WARN_ON(1);
5572 }
5573 }
5574 }
5575}
5576
Avi Kivity851ba692009-08-24 11:10:17 +03005577static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005578{
5579 u64 sptes[4];
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005580 int nr_sptes, i, ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005581 gpa_t gpa;
5582
5583 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Michael S. Tsirkin68c3b4d2014-03-31 21:50:44 +03005584 if (!kvm_io_bus_write(vcpu->kvm, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
5585 skip_emulated_instruction(vcpu);
5586 return 1;
5587 }
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005588
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005589 ret = handle_mmio_page_fault_common(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005590 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005591 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
5592 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005593
5594 if (unlikely(ret == RET_MMIO_PF_INVALID))
5595 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
5596
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005597 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005598 return 1;
5599
5600 /* It is the real ept misconfig */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005601 printk(KERN_ERR "EPT: Misconfiguration.\n");
5602 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
5603
5604 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
5605
5606 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
5607 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
5608
Avi Kivity851ba692009-08-24 11:10:17 +03005609 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5610 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005611
5612 return 0;
5613}
5614
Avi Kivity851ba692009-08-24 11:10:17 +03005615static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005616{
5617 u32 cpu_based_vm_exec_control;
5618
5619 /* clear pending NMI */
5620 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5621 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
5622 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5623 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005624 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005625
5626 return 1;
5627}
5628
Mohammed Gamal80ced182009-09-01 12:48:18 +02005629static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005630{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005631 struct vcpu_vmx *vmx = to_vmx(vcpu);
5632 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005633 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02005634 u32 cpu_exec_ctrl;
5635 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005636 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005637
5638 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5639 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005640
Paolo Bonzini98eb2f82014-03-27 09:51:52 +01005641 while (vmx->emulation_required && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03005642 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005643 return handle_interrupt_window(&vmx->vcpu);
5644
Avi Kivityde87dcd2012-06-12 20:21:38 +03005645 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
5646 return 1;
5647
Gleb Natapov991eebf2013-04-11 12:10:51 +03005648 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005649
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005650 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02005651 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005652 ret = 0;
5653 goto out;
5654 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005655
Avi Kivityde5f70e2012-06-12 20:22:28 +03005656 if (err != EMULATE_DONE) {
5657 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5658 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5659 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005660 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03005661 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005662
Gleb Natapov8d76c492013-05-08 18:38:44 +03005663 if (vcpu->arch.halt_request) {
5664 vcpu->arch.halt_request = 0;
5665 ret = kvm_emulate_halt(vcpu);
5666 goto out;
5667 }
5668
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005669 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02005670 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005671 if (need_resched())
5672 schedule();
5673 }
5674
Mohammed Gamal80ced182009-09-01 12:48:18 +02005675out:
5676 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005677}
5678
Avi Kivity6aa8b732006-12-10 02:21:36 -08005679/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005680 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5681 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5682 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005683static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005684{
5685 skip_emulated_instruction(vcpu);
5686 kvm_vcpu_on_spin(vcpu);
5687
5688 return 1;
5689}
5690
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005691static int handle_nop(struct kvm_vcpu *vcpu)
Sheng Yang59708672009-12-15 13:29:54 +08005692{
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005693 skip_emulated_instruction(vcpu);
Sheng Yang59708672009-12-15 13:29:54 +08005694 return 1;
5695}
5696
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04005697static int handle_mwait(struct kvm_vcpu *vcpu)
5698{
5699 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
5700 return handle_nop(vcpu);
5701}
5702
5703static int handle_monitor(struct kvm_vcpu *vcpu)
5704{
5705 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
5706 return handle_nop(vcpu);
5707}
5708
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005709/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005710 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
5711 * We could reuse a single VMCS for all the L2 guests, but we also want the
5712 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
5713 * allows keeping them loaded on the processor, and in the future will allow
5714 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
5715 * every entry if they never change.
5716 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
5717 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
5718 *
5719 * The following functions allocate and free a vmcs02 in this pool.
5720 */
5721
5722/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
5723static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
5724{
5725 struct vmcs02_list *item;
5726 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5727 if (item->vmptr == vmx->nested.current_vmptr) {
5728 list_move(&item->list, &vmx->nested.vmcs02_pool);
5729 return &item->vmcs02;
5730 }
5731
5732 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
5733 /* Recycle the least recently used VMCS. */
5734 item = list_entry(vmx->nested.vmcs02_pool.prev,
5735 struct vmcs02_list, list);
5736 item->vmptr = vmx->nested.current_vmptr;
5737 list_move(&item->list, &vmx->nested.vmcs02_pool);
5738 return &item->vmcs02;
5739 }
5740
5741 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02005742 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005743 if (!item)
5744 return NULL;
5745 item->vmcs02.vmcs = alloc_vmcs();
5746 if (!item->vmcs02.vmcs) {
5747 kfree(item);
5748 return NULL;
5749 }
5750 loaded_vmcs_init(&item->vmcs02);
5751 item->vmptr = vmx->nested.current_vmptr;
5752 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
5753 vmx->nested.vmcs02_num++;
5754 return &item->vmcs02;
5755}
5756
5757/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
5758static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
5759{
5760 struct vmcs02_list *item;
5761 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5762 if (item->vmptr == vmptr) {
5763 free_loaded_vmcs(&item->vmcs02);
5764 list_del(&item->list);
5765 kfree(item);
5766 vmx->nested.vmcs02_num--;
5767 return;
5768 }
5769}
5770
5771/*
5772 * Free all VMCSs saved for this vcpu, except the one pointed by
Paolo Bonzini4fa77342014-07-17 12:25:16 +02005773 * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs
5774 * must be &vmx->vmcs01.
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005775 */
5776static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
5777{
5778 struct vmcs02_list *item, *n;
Paolo Bonzini4fa77342014-07-17 12:25:16 +02005779
5780 WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005781 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
Paolo Bonzini4fa77342014-07-17 12:25:16 +02005782 /*
5783 * Something will leak if the above WARN triggers. Better than
5784 * a use-after-free.
5785 */
5786 if (vmx->loaded_vmcs == &item->vmcs02)
5787 continue;
5788
5789 free_loaded_vmcs(&item->vmcs02);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005790 list_del(&item->list);
5791 kfree(item);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02005792 vmx->nested.vmcs02_num--;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005793 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005794}
5795
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08005796/*
5797 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
5798 * set the success or error code of an emulated VMX instruction, as specified
5799 * by Vol 2B, VMX Instruction Reference, "Conventions".
5800 */
5801static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
5802{
5803 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
5804 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5805 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
5806}
5807
5808static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
5809{
5810 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5811 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
5812 X86_EFLAGS_SF | X86_EFLAGS_OF))
5813 | X86_EFLAGS_CF);
5814}
5815
Abel Gordon145c28d2013-04-18 14:36:55 +03005816static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08005817 u32 vm_instruction_error)
5818{
5819 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
5820 /*
5821 * failValid writes the error number to the current VMCS, which
5822 * can't be done there isn't a current VMCS.
5823 */
5824 nested_vmx_failInvalid(vcpu);
5825 return;
5826 }
5827 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5828 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5829 X86_EFLAGS_SF | X86_EFLAGS_OF))
5830 | X86_EFLAGS_ZF);
5831 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
5832 /*
5833 * We don't need to force a shadow sync because
5834 * VM_INSTRUCTION_ERROR is not shadowed
5835 */
5836}
Abel Gordon145c28d2013-04-18 14:36:55 +03005837
Jan Kiszkaf4124502014-03-07 20:03:13 +01005838static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
5839{
5840 struct vcpu_vmx *vmx =
5841 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
5842
5843 vmx->nested.preemption_timer_expired = true;
5844 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
5845 kvm_vcpu_kick(&vmx->vcpu);
5846
5847 return HRTIMER_NORESTART;
5848}
5849
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005850/*
Bandan Das19677e32014-05-06 02:19:15 -04005851 * Decode the memory-address operand of a vmx instruction, as recorded on an
5852 * exit caused by such an instruction (run by a guest hypervisor).
5853 * On success, returns 0. When the operand is invalid, returns 1 and throws
5854 * #UD or #GP.
5855 */
5856static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
5857 unsigned long exit_qualification,
5858 u32 vmx_instruction_info, gva_t *ret)
5859{
5860 /*
5861 * According to Vol. 3B, "Information for VM Exits Due to Instruction
5862 * Execution", on an exit, vmx_instruction_info holds most of the
5863 * addressing components of the operand. Only the displacement part
5864 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
5865 * For how an actual address is calculated from all these components,
5866 * refer to Vol. 1, "Operand Addressing".
5867 */
5868 int scaling = vmx_instruction_info & 3;
5869 int addr_size = (vmx_instruction_info >> 7) & 7;
5870 bool is_reg = vmx_instruction_info & (1u << 10);
5871 int seg_reg = (vmx_instruction_info >> 15) & 7;
5872 int index_reg = (vmx_instruction_info >> 18) & 0xf;
5873 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
5874 int base_reg = (vmx_instruction_info >> 23) & 0xf;
5875 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
5876
5877 if (is_reg) {
5878 kvm_queue_exception(vcpu, UD_VECTOR);
5879 return 1;
5880 }
5881
5882 /* Addr = segment_base + offset */
5883 /* offset = base + [index * scale] + displacement */
5884 *ret = vmx_get_segment_base(vcpu, seg_reg);
5885 if (base_is_valid)
5886 *ret += kvm_register_read(vcpu, base_reg);
5887 if (index_is_valid)
5888 *ret += kvm_register_read(vcpu, index_reg)<<scaling;
5889 *ret += exit_qualification; /* holds the displacement */
5890
5891 if (addr_size == 1) /* 32 bit */
5892 *ret &= 0xffffffff;
5893
5894 /*
5895 * TODO: throw #GP (and return 1) in various cases that the VM*
5896 * instructions require it - e.g., offset beyond segment limit,
5897 * unusable or unreadable/unwritable segment, non-canonical 64-bit
5898 * address, and so on. Currently these are not checked.
5899 */
5900 return 0;
5901}
5902
5903/*
Bandan Das3573e222014-05-06 02:19:16 -04005904 * This function performs the various checks including
5905 * - if it's 4KB aligned
5906 * - No bits beyond the physical address width are set
5907 * - Returns 0 on success or else 1
Bandan Das4291b582014-05-06 02:19:18 -04005908 * (Intel SDM Section 30.3)
Bandan Das3573e222014-05-06 02:19:16 -04005909 */
Bandan Das4291b582014-05-06 02:19:18 -04005910static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason,
5911 gpa_t *vmpointer)
Bandan Das3573e222014-05-06 02:19:16 -04005912{
5913 gva_t gva;
5914 gpa_t vmptr;
5915 struct x86_exception e;
5916 struct page *page;
5917 struct vcpu_vmx *vmx = to_vmx(vcpu);
5918 int maxphyaddr = cpuid_maxphyaddr(vcpu);
5919
5920 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5921 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5922 return 1;
5923
5924 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5925 sizeof(vmptr), &e)) {
5926 kvm_inject_page_fault(vcpu, &e);
5927 return 1;
5928 }
5929
5930 switch (exit_reason) {
5931 case EXIT_REASON_VMON:
5932 /*
5933 * SDM 3: 24.11.5
5934 * The first 4 bytes of VMXON region contain the supported
5935 * VMCS revision identifier
5936 *
5937 * Note - IA32_VMX_BASIC[48] will never be 1
5938 * for the nested case;
5939 * which replaces physical address width with 32
5940 *
5941 */
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02005942 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das3573e222014-05-06 02:19:16 -04005943 nested_vmx_failInvalid(vcpu);
5944 skip_emulated_instruction(vcpu);
5945 return 1;
5946 }
5947
5948 page = nested_get_page(vcpu, vmptr);
5949 if (page == NULL ||
5950 *(u32 *)kmap(page) != VMCS12_REVISION) {
5951 nested_vmx_failInvalid(vcpu);
5952 kunmap(page);
5953 skip_emulated_instruction(vcpu);
5954 return 1;
5955 }
5956 kunmap(page);
5957 vmx->nested.vmxon_ptr = vmptr;
5958 break;
Bandan Das4291b582014-05-06 02:19:18 -04005959 case EXIT_REASON_VMCLEAR:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02005960 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04005961 nested_vmx_failValid(vcpu,
5962 VMXERR_VMCLEAR_INVALID_ADDRESS);
5963 skip_emulated_instruction(vcpu);
5964 return 1;
5965 }
Bandan Das3573e222014-05-06 02:19:16 -04005966
Bandan Das4291b582014-05-06 02:19:18 -04005967 if (vmptr == vmx->nested.vmxon_ptr) {
5968 nested_vmx_failValid(vcpu,
5969 VMXERR_VMCLEAR_VMXON_POINTER);
5970 skip_emulated_instruction(vcpu);
5971 return 1;
5972 }
5973 break;
5974 case EXIT_REASON_VMPTRLD:
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02005975 if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) {
Bandan Das4291b582014-05-06 02:19:18 -04005976 nested_vmx_failValid(vcpu,
5977 VMXERR_VMPTRLD_INVALID_ADDRESS);
5978 skip_emulated_instruction(vcpu);
5979 return 1;
5980 }
5981
5982 if (vmptr == vmx->nested.vmxon_ptr) {
5983 nested_vmx_failValid(vcpu,
5984 VMXERR_VMCLEAR_VMXON_POINTER);
5985 skip_emulated_instruction(vcpu);
5986 return 1;
5987 }
5988 break;
Bandan Das3573e222014-05-06 02:19:16 -04005989 default:
5990 return 1; /* shouldn't happen */
5991 }
5992
Bandan Das4291b582014-05-06 02:19:18 -04005993 if (vmpointer)
5994 *vmpointer = vmptr;
Bandan Das3573e222014-05-06 02:19:16 -04005995 return 0;
5996}
5997
5998/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005999 * Emulate the VMXON instruction.
6000 * Currently, we just remember that VMX is active, and do not save or even
6001 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
6002 * do not currently need to store anything in that guest-allocated memory
6003 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
6004 * argument is different from the VMXON pointer (which the spec says they do).
6005 */
6006static int handle_vmon(struct kvm_vcpu *vcpu)
6007{
6008 struct kvm_segment cs;
6009 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03006010 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006011 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
6012 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006013
6014 /* The Intel VMX Instruction Reference lists a bunch of bits that
6015 * are prerequisite to running VMXON, most notably cr4.VMXE must be
6016 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
6017 * Otherwise, we should fail with #UD. We test these now:
6018 */
6019 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
6020 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
6021 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
6022 kvm_queue_exception(vcpu, UD_VECTOR);
6023 return 1;
6024 }
6025
6026 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6027 if (is_long_mode(vcpu) && !cs.l) {
6028 kvm_queue_exception(vcpu, UD_VECTOR);
6029 return 1;
6030 }
6031
6032 if (vmx_get_cpl(vcpu)) {
6033 kvm_inject_gp(vcpu, 0);
6034 return 1;
6035 }
Bandan Das3573e222014-05-06 02:19:16 -04006036
Bandan Das4291b582014-05-06 02:19:18 -04006037 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL))
Bandan Das3573e222014-05-06 02:19:16 -04006038 return 1;
6039
Abel Gordon145c28d2013-04-18 14:36:55 +03006040 if (vmx->nested.vmxon) {
6041 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
6042 skip_emulated_instruction(vcpu);
6043 return 1;
6044 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08006045
6046 if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
6047 != VMXON_NEEDED_FEATURES) {
6048 kvm_inject_gp(vcpu, 0);
6049 return 1;
6050 }
6051
Abel Gordon8de48832013-04-18 14:37:25 +03006052 if (enable_shadow_vmcs) {
6053 shadow_vmcs = alloc_vmcs();
6054 if (!shadow_vmcs)
6055 return -ENOMEM;
6056 /* mark vmcs as shadow */
6057 shadow_vmcs->revision_id |= (1u << 31);
6058 /* init shadow vmcs */
6059 vmcs_clear(shadow_vmcs);
6060 vmx->nested.current_shadow_vmcs = shadow_vmcs;
6061 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006062
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006063 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
6064 vmx->nested.vmcs02_num = 0;
6065
Jan Kiszkaf4124502014-03-07 20:03:13 +01006066 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
6067 HRTIMER_MODE_REL);
6068 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
6069
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006070 vmx->nested.vmxon = true;
6071
6072 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08006073 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006074 return 1;
6075}
6076
6077/*
6078 * Intel's VMX Instruction Reference specifies a common set of prerequisites
6079 * for running VMX instructions (except VMXON, whose prerequisites are
6080 * slightly different). It also specifies what exception to inject otherwise.
6081 */
6082static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
6083{
6084 struct kvm_segment cs;
6085 struct vcpu_vmx *vmx = to_vmx(vcpu);
6086
6087 if (!vmx->nested.vmxon) {
6088 kvm_queue_exception(vcpu, UD_VECTOR);
6089 return 0;
6090 }
6091
6092 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
6093 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
6094 (is_long_mode(vcpu) && !cs.l)) {
6095 kvm_queue_exception(vcpu, UD_VECTOR);
6096 return 0;
6097 }
6098
6099 if (vmx_get_cpl(vcpu)) {
6100 kvm_inject_gp(vcpu, 0);
6101 return 0;
6102 }
6103
6104 return 1;
6105}
6106
Abel Gordone7953d72013-04-18 14:37:55 +03006107static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
6108{
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006109 u32 exec_control;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006110 if (vmx->nested.current_vmptr == -1ull)
6111 return;
6112
6113 /* current_vmptr and current_vmcs12 are always set/reset together */
6114 if (WARN_ON(vmx->nested.current_vmcs12 == NULL))
6115 return;
6116
Abel Gordon012f83c2013-04-18 14:39:25 +03006117 if (enable_shadow_vmcs) {
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006118 /* copy to memory all shadowed fields in case
6119 they were modified */
6120 copy_shadow_to_vmcs12(vmx);
6121 vmx->nested.sync_shadow_vmcs = false;
6122 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6123 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
6124 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
6125 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03006126 }
Abel Gordone7953d72013-04-18 14:37:55 +03006127 kunmap(vmx->nested.current_vmcs12_page);
6128 nested_release_page(vmx->nested.current_vmcs12_page);
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006129 vmx->nested.current_vmptr = -1ull;
6130 vmx->nested.current_vmcs12 = NULL;
Abel Gordone7953d72013-04-18 14:37:55 +03006131}
6132
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006133/*
6134 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
6135 * just stops using VMX.
6136 */
6137static void free_nested(struct vcpu_vmx *vmx)
6138{
6139 if (!vmx->nested.vmxon)
6140 return;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006141
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006142 vmx->nested.vmxon = false;
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006143 nested_release_vmcs12(vmx);
Abel Gordone7953d72013-04-18 14:37:55 +03006144 if (enable_shadow_vmcs)
6145 free_vmcs(vmx->nested.current_shadow_vmcs);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006146 /* Unpin physical memory we referred to in current vmcs02 */
6147 if (vmx->nested.apic_access_page) {
6148 nested_release_page(vmx->nested.apic_access_page);
6149 vmx->nested.apic_access_page = 0;
6150 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03006151
6152 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006153}
6154
6155/* Emulate the VMXOFF instruction */
6156static int handle_vmoff(struct kvm_vcpu *vcpu)
6157{
6158 if (!nested_vmx_check_permission(vcpu))
6159 return 1;
6160 free_nested(to_vmx(vcpu));
6161 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08006162 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006163 return 1;
6164}
6165
Nadav Har'El27d6c862011-05-25 23:06:59 +03006166/* Emulate the VMCLEAR instruction */
6167static int handle_vmclear(struct kvm_vcpu *vcpu)
6168{
6169 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El27d6c862011-05-25 23:06:59 +03006170 gpa_t vmptr;
6171 struct vmcs12 *vmcs12;
6172 struct page *page;
Nadav Har'El27d6c862011-05-25 23:06:59 +03006173
6174 if (!nested_vmx_check_permission(vcpu))
6175 return 1;
6176
Bandan Das4291b582014-05-06 02:19:18 -04006177 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr))
Nadav Har'El27d6c862011-05-25 23:06:59 +03006178 return 1;
6179
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006180 if (vmptr == vmx->nested.current_vmptr)
Abel Gordone7953d72013-04-18 14:37:55 +03006181 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03006182
6183 page = nested_get_page(vcpu, vmptr);
6184 if (page == NULL) {
6185 /*
6186 * For accurate processor emulation, VMCLEAR beyond available
6187 * physical memory should do nothing at all. However, it is
6188 * possible that a nested vmx bug, not a guest hypervisor bug,
6189 * resulted in this case, so let's shut down before doing any
6190 * more damage:
6191 */
6192 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6193 return 1;
6194 }
6195 vmcs12 = kmap(page);
6196 vmcs12->launch_state = 0;
6197 kunmap(page);
6198 nested_release_page(page);
6199
6200 nested_free_vmcs02(vmx, vmptr);
6201
6202 skip_emulated_instruction(vcpu);
6203 nested_vmx_succeed(vcpu);
6204 return 1;
6205}
6206
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006207static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
6208
6209/* Emulate the VMLAUNCH instruction */
6210static int handle_vmlaunch(struct kvm_vcpu *vcpu)
6211{
6212 return nested_vmx_run(vcpu, true);
6213}
6214
6215/* Emulate the VMRESUME instruction */
6216static int handle_vmresume(struct kvm_vcpu *vcpu)
6217{
6218
6219 return nested_vmx_run(vcpu, false);
6220}
6221
Nadav Har'El49f705c2011-05-25 23:08:30 +03006222enum vmcs_field_type {
6223 VMCS_FIELD_TYPE_U16 = 0,
6224 VMCS_FIELD_TYPE_U64 = 1,
6225 VMCS_FIELD_TYPE_U32 = 2,
6226 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
6227};
6228
6229static inline int vmcs_field_type(unsigned long field)
6230{
6231 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
6232 return VMCS_FIELD_TYPE_U32;
6233 return (field >> 13) & 0x3 ;
6234}
6235
6236static inline int vmcs_field_readonly(unsigned long field)
6237{
6238 return (((field >> 10) & 0x3) == 1);
6239}
6240
6241/*
6242 * Read a vmcs12 field. Since these can have varying lengths and we return
6243 * one type, we chose the biggest type (u64) and zero-extend the return value
6244 * to that size. Note that the caller, handle_vmread, might need to use only
6245 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
6246 * 64-bit fields are to be returned).
6247 */
6248static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
6249 unsigned long field, u64 *ret)
6250{
6251 short offset = vmcs_field_to_offset(field);
6252 char *p;
6253
6254 if (offset < 0)
6255 return 0;
6256
6257 p = ((char *)(get_vmcs12(vcpu))) + offset;
6258
6259 switch (vmcs_field_type(field)) {
6260 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6261 *ret = *((natural_width *)p);
6262 return 1;
6263 case VMCS_FIELD_TYPE_U16:
6264 *ret = *((u16 *)p);
6265 return 1;
6266 case VMCS_FIELD_TYPE_U32:
6267 *ret = *((u32 *)p);
6268 return 1;
6269 case VMCS_FIELD_TYPE_U64:
6270 *ret = *((u64 *)p);
6271 return 1;
6272 default:
6273 return 0; /* can never happen. */
6274 }
6275}
6276
Abel Gordon20b97fe2013-04-18 14:36:25 +03006277
6278static inline bool vmcs12_write_any(struct kvm_vcpu *vcpu,
6279 unsigned long field, u64 field_value){
6280 short offset = vmcs_field_to_offset(field);
6281 char *p = ((char *) get_vmcs12(vcpu)) + offset;
6282 if (offset < 0)
6283 return false;
6284
6285 switch (vmcs_field_type(field)) {
6286 case VMCS_FIELD_TYPE_U16:
6287 *(u16 *)p = field_value;
6288 return true;
6289 case VMCS_FIELD_TYPE_U32:
6290 *(u32 *)p = field_value;
6291 return true;
6292 case VMCS_FIELD_TYPE_U64:
6293 *(u64 *)p = field_value;
6294 return true;
6295 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6296 *(natural_width *)p = field_value;
6297 return true;
6298 default:
6299 return false; /* can never happen. */
6300 }
6301
6302}
6303
Abel Gordon16f5b902013-04-18 14:38:25 +03006304static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
6305{
6306 int i;
6307 unsigned long field;
6308 u64 field_value;
6309 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02006310 const unsigned long *fields = shadow_read_write_fields;
6311 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03006312
6313 vmcs_load(shadow_vmcs);
6314
6315 for (i = 0; i < num_fields; i++) {
6316 field = fields[i];
6317 switch (vmcs_field_type(field)) {
6318 case VMCS_FIELD_TYPE_U16:
6319 field_value = vmcs_read16(field);
6320 break;
6321 case VMCS_FIELD_TYPE_U32:
6322 field_value = vmcs_read32(field);
6323 break;
6324 case VMCS_FIELD_TYPE_U64:
6325 field_value = vmcs_read64(field);
6326 break;
6327 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6328 field_value = vmcs_readl(field);
6329 break;
6330 }
6331 vmcs12_write_any(&vmx->vcpu, field, field_value);
6332 }
6333
6334 vmcs_clear(shadow_vmcs);
6335 vmcs_load(vmx->loaded_vmcs->vmcs);
6336}
6337
Abel Gordonc3114422013-04-18 14:38:55 +03006338static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
6339{
Mathias Krausec2bae892013-06-26 20:36:21 +02006340 const unsigned long *fields[] = {
6341 shadow_read_write_fields,
6342 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03006343 };
Mathias Krausec2bae892013-06-26 20:36:21 +02006344 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03006345 max_shadow_read_write_fields,
6346 max_shadow_read_only_fields
6347 };
6348 int i, q;
6349 unsigned long field;
6350 u64 field_value = 0;
6351 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
6352
6353 vmcs_load(shadow_vmcs);
6354
Mathias Krausec2bae892013-06-26 20:36:21 +02006355 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03006356 for (i = 0; i < max_fields[q]; i++) {
6357 field = fields[q][i];
6358 vmcs12_read_any(&vmx->vcpu, field, &field_value);
6359
6360 switch (vmcs_field_type(field)) {
6361 case VMCS_FIELD_TYPE_U16:
6362 vmcs_write16(field, (u16)field_value);
6363 break;
6364 case VMCS_FIELD_TYPE_U32:
6365 vmcs_write32(field, (u32)field_value);
6366 break;
6367 case VMCS_FIELD_TYPE_U64:
6368 vmcs_write64(field, (u64)field_value);
6369 break;
6370 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6371 vmcs_writel(field, (long)field_value);
6372 break;
6373 }
6374 }
6375 }
6376
6377 vmcs_clear(shadow_vmcs);
6378 vmcs_load(vmx->loaded_vmcs->vmcs);
6379}
6380
Nadav Har'El49f705c2011-05-25 23:08:30 +03006381/*
6382 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
6383 * used before) all generate the same failure when it is missing.
6384 */
6385static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
6386{
6387 struct vcpu_vmx *vmx = to_vmx(vcpu);
6388 if (vmx->nested.current_vmptr == -1ull) {
6389 nested_vmx_failInvalid(vcpu);
6390 skip_emulated_instruction(vcpu);
6391 return 0;
6392 }
6393 return 1;
6394}
6395
6396static int handle_vmread(struct kvm_vcpu *vcpu)
6397{
6398 unsigned long field;
6399 u64 field_value;
6400 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6401 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6402 gva_t gva = 0;
6403
6404 if (!nested_vmx_check_permission(vcpu) ||
6405 !nested_vmx_check_vmcs12(vcpu))
6406 return 1;
6407
6408 /* Decode instruction info and find the field to read */
Nadav Amit27e6fb52014-06-18 17:19:26 +03006409 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03006410 /* Read the field, zero-extended to a u64 field_value */
6411 if (!vmcs12_read_any(vcpu, field, &field_value)) {
6412 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
6413 skip_emulated_instruction(vcpu);
6414 return 1;
6415 }
6416 /*
6417 * Now copy part of this value to register or memory, as requested.
6418 * Note that the number of bits actually copied is 32 or 64 depending
6419 * on the guest's mode (32 or 64 bit), not on the given field's length.
6420 */
6421 if (vmx_instruction_info & (1u << 10)) {
Nadav Amit27e6fb52014-06-18 17:19:26 +03006422 kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
Nadav Har'El49f705c2011-05-25 23:08:30 +03006423 field_value);
6424 } else {
6425 if (get_vmx_mem_address(vcpu, exit_qualification,
6426 vmx_instruction_info, &gva))
6427 return 1;
6428 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
6429 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
6430 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
6431 }
6432
6433 nested_vmx_succeed(vcpu);
6434 skip_emulated_instruction(vcpu);
6435 return 1;
6436}
6437
6438
6439static int handle_vmwrite(struct kvm_vcpu *vcpu)
6440{
6441 unsigned long field;
6442 gva_t gva;
6443 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6444 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03006445 /* The value to write might be 32 or 64 bits, depending on L1's long
6446 * mode, and eventually we need to write that into a field of several
6447 * possible lengths. The code below first zero-extends the value to 64
6448 * bit (field_value), and then copies only the approriate number of
6449 * bits into the vmcs12 field.
6450 */
6451 u64 field_value = 0;
6452 struct x86_exception e;
6453
6454 if (!nested_vmx_check_permission(vcpu) ||
6455 !nested_vmx_check_vmcs12(vcpu))
6456 return 1;
6457
6458 if (vmx_instruction_info & (1u << 10))
Nadav Amit27e6fb52014-06-18 17:19:26 +03006459 field_value = kvm_register_readl(vcpu,
Nadav Har'El49f705c2011-05-25 23:08:30 +03006460 (((vmx_instruction_info) >> 3) & 0xf));
6461 else {
6462 if (get_vmx_mem_address(vcpu, exit_qualification,
6463 vmx_instruction_info, &gva))
6464 return 1;
6465 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
Nadav Amit27e6fb52014-06-18 17:19:26 +03006466 &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03006467 kvm_inject_page_fault(vcpu, &e);
6468 return 1;
6469 }
6470 }
6471
6472
Nadav Amit27e6fb52014-06-18 17:19:26 +03006473 field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
Nadav Har'El49f705c2011-05-25 23:08:30 +03006474 if (vmcs_field_readonly(field)) {
6475 nested_vmx_failValid(vcpu,
6476 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
6477 skip_emulated_instruction(vcpu);
6478 return 1;
6479 }
6480
Abel Gordon20b97fe2013-04-18 14:36:25 +03006481 if (!vmcs12_write_any(vcpu, field, field_value)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03006482 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
6483 skip_emulated_instruction(vcpu);
6484 return 1;
6485 }
6486
6487 nested_vmx_succeed(vcpu);
6488 skip_emulated_instruction(vcpu);
6489 return 1;
6490}
6491
Nadav Har'El63846662011-05-25 23:07:29 +03006492/* Emulate the VMPTRLD instruction */
6493static int handle_vmptrld(struct kvm_vcpu *vcpu)
6494{
6495 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El63846662011-05-25 23:07:29 +03006496 gpa_t vmptr;
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006497 u32 exec_control;
Nadav Har'El63846662011-05-25 23:07:29 +03006498
6499 if (!nested_vmx_check_permission(vcpu))
6500 return 1;
6501
Bandan Das4291b582014-05-06 02:19:18 -04006502 if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr))
Nadav Har'El63846662011-05-25 23:07:29 +03006503 return 1;
6504
Nadav Har'El63846662011-05-25 23:07:29 +03006505 if (vmx->nested.current_vmptr != vmptr) {
6506 struct vmcs12 *new_vmcs12;
6507 struct page *page;
6508 page = nested_get_page(vcpu, vmptr);
6509 if (page == NULL) {
6510 nested_vmx_failInvalid(vcpu);
6511 skip_emulated_instruction(vcpu);
6512 return 1;
6513 }
6514 new_vmcs12 = kmap(page);
6515 if (new_vmcs12->revision_id != VMCS12_REVISION) {
6516 kunmap(page);
6517 nested_release_page_clean(page);
6518 nested_vmx_failValid(vcpu,
6519 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
6520 skip_emulated_instruction(vcpu);
6521 return 1;
6522 }
Nadav Har'El63846662011-05-25 23:07:29 +03006523
Paolo Bonzini9a2a05b2014-07-17 11:55:46 +02006524 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03006525 vmx->nested.current_vmptr = vmptr;
6526 vmx->nested.current_vmcs12 = new_vmcs12;
6527 vmx->nested.current_vmcs12_page = page;
Abel Gordon012f83c2013-04-18 14:39:25 +03006528 if (enable_shadow_vmcs) {
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006529 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6530 exec_control |= SECONDARY_EXEC_SHADOW_VMCS;
6531 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
6532 vmcs_write64(VMCS_LINK_POINTER,
6533 __pa(vmx->nested.current_shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03006534 vmx->nested.sync_shadow_vmcs = true;
6535 }
Nadav Har'El63846662011-05-25 23:07:29 +03006536 }
6537
6538 nested_vmx_succeed(vcpu);
6539 skip_emulated_instruction(vcpu);
6540 return 1;
6541}
6542
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006543/* Emulate the VMPTRST instruction */
6544static int handle_vmptrst(struct kvm_vcpu *vcpu)
6545{
6546 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6547 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6548 gva_t vmcs_gva;
6549 struct x86_exception e;
6550
6551 if (!nested_vmx_check_permission(vcpu))
6552 return 1;
6553
6554 if (get_vmx_mem_address(vcpu, exit_qualification,
6555 vmx_instruction_info, &vmcs_gva))
6556 return 1;
6557 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
6558 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
6559 (void *)&to_vmx(vcpu)->nested.current_vmptr,
6560 sizeof(u64), &e)) {
6561 kvm_inject_page_fault(vcpu, &e);
6562 return 1;
6563 }
6564 nested_vmx_succeed(vcpu);
6565 skip_emulated_instruction(vcpu);
6566 return 1;
6567}
6568
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006569/* Emulate the INVEPT instruction */
6570static int handle_invept(struct kvm_vcpu *vcpu)
6571{
6572 u32 vmx_instruction_info, types;
6573 unsigned long type;
6574 gva_t gva;
6575 struct x86_exception e;
6576 struct {
6577 u64 eptp, gpa;
6578 } operand;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006579
6580 if (!(nested_vmx_secondary_ctls_high & SECONDARY_EXEC_ENABLE_EPT) ||
6581 !(nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
6582 kvm_queue_exception(vcpu, UD_VECTOR);
6583 return 1;
6584 }
6585
6586 if (!nested_vmx_check_permission(vcpu))
6587 return 1;
6588
6589 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
6590 kvm_queue_exception(vcpu, UD_VECTOR);
6591 return 1;
6592 }
6593
6594 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Amit27e6fb52014-06-18 17:19:26 +03006595 type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006596
6597 types = (nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
6598
6599 if (!(types & (1UL << type))) {
6600 nested_vmx_failValid(vcpu,
6601 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
6602 return 1;
6603 }
6604
6605 /* According to the Intel VMX instruction reference, the memory
6606 * operand is read even if it isn't needed (e.g., for type==global)
6607 */
6608 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
6609 vmx_instruction_info, &gva))
6610 return 1;
6611 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
6612 sizeof(operand), &e)) {
6613 kvm_inject_page_fault(vcpu, &e);
6614 return 1;
6615 }
6616
6617 switch (type) {
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006618 case VMX_EPT_EXTENT_GLOBAL:
6619 kvm_mmu_sync_roots(vcpu);
6620 kvm_mmu_flush_tlb(vcpu);
6621 nested_vmx_succeed(vcpu);
6622 break;
6623 default:
Bandan Das4b855072014-04-19 18:17:44 -04006624 /* Trap single context invalidation invept calls */
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006625 BUG_ON(1);
6626 break;
6627 }
6628
6629 skip_emulated_instruction(vcpu);
6630 return 1;
6631}
6632
Nadav Har'El0140cae2011-05-25 23:06:28 +03006633/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006634 * The exit handlers return 1 if the exit was handled fully and guest execution
6635 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
6636 * to be done to userspace and return 0.
6637 */
Mathias Krause772e0312012-08-30 01:30:19 +02006638static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006639 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
6640 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08006641 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08006642 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006643 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006644 [EXIT_REASON_CR_ACCESS] = handle_cr,
6645 [EXIT_REASON_DR_ACCESS] = handle_dr,
6646 [EXIT_REASON_CPUID] = handle_cpuid,
6647 [EXIT_REASON_MSR_READ] = handle_rdmsr,
6648 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
6649 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
6650 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006651 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03006652 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02006653 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02006654 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03006655 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006656 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03006657 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006658 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03006659 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006660 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03006661 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006662 [EXIT_REASON_VMOFF] = handle_vmoff,
6663 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08006664 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
6665 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08006666 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08006667 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02006668 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08006669 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02006670 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08006671 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006672 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
6673 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006674 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04006675 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
6676 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006677 [EXIT_REASON_INVEPT] = handle_invept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006678};
6679
6680static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04006681 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006682
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006683static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
6684 struct vmcs12 *vmcs12)
6685{
6686 unsigned long exit_qualification;
6687 gpa_t bitmap, last_bitmap;
6688 unsigned int port;
6689 int size;
6690 u8 b;
6691
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006692 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05006693 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006694
6695 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6696
6697 port = exit_qualification >> 16;
6698 size = (exit_qualification & 7) + 1;
6699
6700 last_bitmap = (gpa_t)-1;
6701 b = -1;
6702
6703 while (size > 0) {
6704 if (port < 0x8000)
6705 bitmap = vmcs12->io_bitmap_a;
6706 else if (port < 0x10000)
6707 bitmap = vmcs12->io_bitmap_b;
6708 else
6709 return 1;
6710 bitmap += (port & 0x7fff) / 8;
6711
6712 if (last_bitmap != bitmap)
6713 if (kvm_read_guest(vcpu->kvm, bitmap, &b, 1))
6714 return 1;
6715 if (b & (1 << (port & 7)))
6716 return 1;
6717
6718 port++;
6719 size--;
6720 last_bitmap = bitmap;
6721 }
6722
6723 return 0;
6724}
6725
Nadav Har'El644d7112011-05-25 23:12:35 +03006726/*
6727 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
6728 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
6729 * disinterest in the current event (read or write a specific MSR) by using an
6730 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
6731 */
6732static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
6733 struct vmcs12 *vmcs12, u32 exit_reason)
6734{
6735 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
6736 gpa_t bitmap;
6737
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01006738 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Nadav Har'El644d7112011-05-25 23:12:35 +03006739 return 1;
6740
6741 /*
6742 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
6743 * for the four combinations of read/write and low/high MSR numbers.
6744 * First we need to figure out which of the four to use:
6745 */
6746 bitmap = vmcs12->msr_bitmap;
6747 if (exit_reason == EXIT_REASON_MSR_WRITE)
6748 bitmap += 2048;
6749 if (msr_index >= 0xc0000000) {
6750 msr_index -= 0xc0000000;
6751 bitmap += 1024;
6752 }
6753
6754 /* Then read the msr_index'th bit from this bitmap: */
6755 if (msr_index < 1024*8) {
6756 unsigned char b;
Jan Kiszkabd31a7f2013-02-14 19:46:27 +01006757 if (kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1))
6758 return 1;
Nadav Har'El644d7112011-05-25 23:12:35 +03006759 return 1 & (b >> (msr_index & 7));
6760 } else
6761 return 1; /* let L1 handle the wrong parameter */
6762}
6763
6764/*
6765 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
6766 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
6767 * intercept (via guest_host_mask etc.) the current event.
6768 */
6769static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
6770 struct vmcs12 *vmcs12)
6771{
6772 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6773 int cr = exit_qualification & 15;
6774 int reg = (exit_qualification >> 8) & 15;
Nadav Amit1e32c072014-06-18 17:19:25 +03006775 unsigned long val = kvm_register_readl(vcpu, reg);
Nadav Har'El644d7112011-05-25 23:12:35 +03006776
6777 switch ((exit_qualification >> 4) & 3) {
6778 case 0: /* mov to cr */
6779 switch (cr) {
6780 case 0:
6781 if (vmcs12->cr0_guest_host_mask &
6782 (val ^ vmcs12->cr0_read_shadow))
6783 return 1;
6784 break;
6785 case 3:
6786 if ((vmcs12->cr3_target_count >= 1 &&
6787 vmcs12->cr3_target_value0 == val) ||
6788 (vmcs12->cr3_target_count >= 2 &&
6789 vmcs12->cr3_target_value1 == val) ||
6790 (vmcs12->cr3_target_count >= 3 &&
6791 vmcs12->cr3_target_value2 == val) ||
6792 (vmcs12->cr3_target_count >= 4 &&
6793 vmcs12->cr3_target_value3 == val))
6794 return 0;
6795 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
6796 return 1;
6797 break;
6798 case 4:
6799 if (vmcs12->cr4_guest_host_mask &
6800 (vmcs12->cr4_read_shadow ^ val))
6801 return 1;
6802 break;
6803 case 8:
6804 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
6805 return 1;
6806 break;
6807 }
6808 break;
6809 case 2: /* clts */
6810 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
6811 (vmcs12->cr0_read_shadow & X86_CR0_TS))
6812 return 1;
6813 break;
6814 case 1: /* mov from cr */
6815 switch (cr) {
6816 case 3:
6817 if (vmcs12->cpu_based_vm_exec_control &
6818 CPU_BASED_CR3_STORE_EXITING)
6819 return 1;
6820 break;
6821 case 8:
6822 if (vmcs12->cpu_based_vm_exec_control &
6823 CPU_BASED_CR8_STORE_EXITING)
6824 return 1;
6825 break;
6826 }
6827 break;
6828 case 3: /* lmsw */
6829 /*
6830 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
6831 * cr0. Other attempted changes are ignored, with no exit.
6832 */
6833 if (vmcs12->cr0_guest_host_mask & 0xe &
6834 (val ^ vmcs12->cr0_read_shadow))
6835 return 1;
6836 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
6837 !(vmcs12->cr0_read_shadow & 0x1) &&
6838 (val & 0x1))
6839 return 1;
6840 break;
6841 }
6842 return 0;
6843}
6844
6845/*
6846 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
6847 * should handle it ourselves in L0 (and then continue L2). Only call this
6848 * when in is_guest_mode (L2).
6849 */
6850static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
6851{
Nadav Har'El644d7112011-05-25 23:12:35 +03006852 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6853 struct vcpu_vmx *vmx = to_vmx(vcpu);
6854 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01006855 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03006856
Jan Kiszka542060e2014-01-04 18:47:21 +01006857 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
6858 vmcs_readl(EXIT_QUALIFICATION),
6859 vmx->idt_vectoring_info,
6860 intr_info,
6861 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
6862 KVM_ISA_VMX);
6863
Nadav Har'El644d7112011-05-25 23:12:35 +03006864 if (vmx->nested.nested_run_pending)
6865 return 0;
6866
6867 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02006868 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
6869 vmcs_read32(VM_INSTRUCTION_ERROR));
Nadav Har'El644d7112011-05-25 23:12:35 +03006870 return 1;
6871 }
6872
6873 switch (exit_reason) {
6874 case EXIT_REASON_EXCEPTION_NMI:
6875 if (!is_exception(intr_info))
6876 return 0;
6877 else if (is_page_fault(intr_info))
6878 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01006879 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01006880 !(vmcs12->guest_cr0 & X86_CR0_TS))
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01006881 return 0;
Nadav Har'El644d7112011-05-25 23:12:35 +03006882 return vmcs12->exception_bitmap &
6883 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
6884 case EXIT_REASON_EXTERNAL_INTERRUPT:
6885 return 0;
6886 case EXIT_REASON_TRIPLE_FAULT:
6887 return 1;
6888 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02006889 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03006890 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02006891 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03006892 case EXIT_REASON_TASK_SWITCH:
6893 return 1;
6894 case EXIT_REASON_CPUID:
6895 return 1;
6896 case EXIT_REASON_HLT:
6897 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
6898 case EXIT_REASON_INVD:
6899 return 1;
6900 case EXIT_REASON_INVLPG:
6901 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
6902 case EXIT_REASON_RDPMC:
6903 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
6904 case EXIT_REASON_RDTSC:
6905 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
6906 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
6907 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
6908 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
6909 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
6910 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006911 case EXIT_REASON_INVEPT:
Nadav Har'El644d7112011-05-25 23:12:35 +03006912 /*
6913 * VMX instructions trap unconditionally. This allows L1 to
6914 * emulate them for its L2 guest, i.e., allows 3-level nesting!
6915 */
6916 return 1;
6917 case EXIT_REASON_CR_ACCESS:
6918 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
6919 case EXIT_REASON_DR_ACCESS:
6920 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
6921 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006922 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03006923 case EXIT_REASON_MSR_READ:
6924 case EXIT_REASON_MSR_WRITE:
6925 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
6926 case EXIT_REASON_INVALID_STATE:
6927 return 1;
6928 case EXIT_REASON_MWAIT_INSTRUCTION:
6929 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
6930 case EXIT_REASON_MONITOR_INSTRUCTION:
6931 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
6932 case EXIT_REASON_PAUSE_INSTRUCTION:
6933 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
6934 nested_cpu_has2(vmcs12,
6935 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
6936 case EXIT_REASON_MCE_DURING_VMENTRY:
6937 return 0;
6938 case EXIT_REASON_TPR_BELOW_THRESHOLD:
6939 return 1;
6940 case EXIT_REASON_APIC_ACCESS:
6941 return nested_cpu_has2(vmcs12,
6942 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
6943 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03006944 /*
6945 * L0 always deals with the EPT violation. If nested EPT is
6946 * used, and the nested mmu code discovers that the address is
6947 * missing in the guest EPT table (EPT12), the EPT violation
6948 * will be injected with nested_ept_inject_page_fault()
6949 */
6950 return 0;
Nadav Har'El644d7112011-05-25 23:12:35 +03006951 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03006952 /*
6953 * L2 never uses directly L1's EPT, but rather L0's own EPT
6954 * table (shadow on EPT) or a merged EPT table that L0 built
6955 * (EPT on EPT). So any problems with the structure of the
6956 * table is L0's fault.
6957 */
Nadav Har'El644d7112011-05-25 23:12:35 +03006958 return 0;
6959 case EXIT_REASON_WBINVD:
6960 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
6961 case EXIT_REASON_XSETBV:
6962 return 1;
6963 default:
6964 return 1;
6965 }
6966}
6967
Avi Kivity586f9602010-11-18 13:09:54 +02006968static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
6969{
6970 *info1 = vmcs_readl(EXIT_QUALIFICATION);
6971 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
6972}
6973
Avi Kivity6aa8b732006-12-10 02:21:36 -08006974/*
6975 * The guest has exited. See if we can fix it or if we need userspace
6976 * assistance.
6977 */
Avi Kivity851ba692009-08-24 11:10:17 +03006978static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006979{
Avi Kivity29bd8a72007-09-10 17:27:03 +03006980 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006981 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02006982 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03006983
Mohammed Gamal80ced182009-09-01 12:48:18 +02006984 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02006985 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02006986 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006987
Nadav Har'El644d7112011-05-25 23:12:35 +03006988 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01006989 nested_vmx_vmexit(vcpu, exit_reason,
6990 vmcs_read32(VM_EXIT_INTR_INFO),
6991 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03006992 return 1;
6993 }
6994
Mohammed Gamal51207022010-05-31 22:40:54 +03006995 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
6996 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6997 vcpu->run->fail_entry.hardware_entry_failure_reason
6998 = exit_reason;
6999 return 0;
7000 }
7001
Avi Kivity29bd8a72007-09-10 17:27:03 +03007002 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03007003 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
7004 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03007005 = vmcs_read32(VM_INSTRUCTION_ERROR);
7006 return 0;
7007 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08007008
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08007009 /*
7010 * Note:
7011 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
7012 * delivery event since it indicates guest is accessing MMIO.
7013 * The vm-exit can be triggered again after return to guest that
7014 * will cause infinite loop.
7015 */
Mike Dayd77c26f2007-10-08 09:02:08 -04007016 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08007017 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02007018 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08007019 exit_reason != EXIT_REASON_TASK_SWITCH)) {
7020 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
7021 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
7022 vcpu->run->internal.ndata = 2;
7023 vcpu->run->internal.data[0] = vectoring_info;
7024 vcpu->run->internal.data[1] = exit_reason;
7025 return 0;
7026 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007027
Nadav Har'El644d7112011-05-25 23:12:35 +03007028 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
7029 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03007030 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03007031 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007032 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007033 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01007034 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007035 /*
7036 * This CPU don't support us in finding the end of an
7037 * NMI-blocked window if the guest runs with IRQs
7038 * disabled. So we pull the trigger after 1 s of
7039 * futile waiting, but inform the user about this.
7040 */
7041 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
7042 "state on VCPU %d after 1 s timeout\n",
7043 __func__, vcpu->vcpu_id);
7044 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007045 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007046 }
7047
Avi Kivity6aa8b732006-12-10 02:21:36 -08007048 if (exit_reason < kvm_vmx_max_exit_handlers
7049 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03007050 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007051 else {
Avi Kivity851ba692009-08-24 11:10:17 +03007052 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
7053 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007054 }
7055 return 0;
7056}
7057
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007058static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007059{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007060 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007061 vmcs_write32(TPR_THRESHOLD, 0);
7062 return;
7063 }
7064
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007065 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08007066}
7067
Yang Zhang8d146952013-01-25 10:18:50 +08007068static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
7069{
7070 u32 sec_exec_control;
7071
7072 /*
7073 * There is not point to enable virtualize x2apic without enable
7074 * apicv
7075 */
Yang Zhangc7c9c562013-01-25 10:18:51 +08007076 if (!cpu_has_vmx_virtualize_x2apic_mode() ||
7077 !vmx_vm_has_apicv(vcpu->kvm))
Yang Zhang8d146952013-01-25 10:18:50 +08007078 return;
7079
7080 if (!vm_need_tpr_shadow(vcpu->kvm))
7081 return;
7082
7083 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7084
7085 if (set) {
7086 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7087 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
7088 } else {
7089 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
7090 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7091 }
7092 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
7093
7094 vmx_set_msr_bitmap(vcpu);
7095}
7096
Yang Zhangc7c9c562013-01-25 10:18:51 +08007097static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
7098{
7099 u16 status;
7100 u8 old;
7101
7102 if (!vmx_vm_has_apicv(kvm))
7103 return;
7104
7105 if (isr == -1)
7106 isr = 0;
7107
7108 status = vmcs_read16(GUEST_INTR_STATUS);
7109 old = status >> 8;
7110 if (isr != old) {
7111 status &= 0xff;
7112 status |= isr << 8;
7113 vmcs_write16(GUEST_INTR_STATUS, status);
7114 }
7115}
7116
7117static void vmx_set_rvi(int vector)
7118{
7119 u16 status;
7120 u8 old;
7121
7122 status = vmcs_read16(GUEST_INTR_STATUS);
7123 old = (u8)status & 0xff;
7124 if ((u8)vector != old) {
7125 status &= ~0xff;
7126 status |= (u8)vector;
7127 vmcs_write16(GUEST_INTR_STATUS, status);
7128 }
7129}
7130
7131static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
7132{
7133 if (max_irr == -1)
7134 return;
7135
Wanpeng Li963fee12014-07-17 19:03:00 +08007136 /*
7137 * If a vmexit is needed, vmx_check_nested_events handles it.
7138 */
7139 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu))
7140 return;
7141
7142 if (!is_guest_mode(vcpu)) {
7143 vmx_set_rvi(max_irr);
7144 return;
7145 }
7146
7147 /*
7148 * Fall back to pre-APICv interrupt injection since L2
7149 * is run without virtual interrupt delivery.
7150 */
7151 if (!kvm_event_needs_reinjection(vcpu) &&
7152 vmx_interrupt_allowed(vcpu)) {
7153 kvm_queue_interrupt(vcpu, max_irr, false);
7154 vmx_inject_irq(vcpu);
7155 }
Yang Zhangc7c9c562013-01-25 10:18:51 +08007156}
7157
7158static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
7159{
Yang Zhang3d81bc72013-04-11 19:25:13 +08007160 if (!vmx_vm_has_apicv(vcpu->kvm))
7161 return;
7162
Yang Zhangc7c9c562013-01-25 10:18:51 +08007163 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
7164 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
7165 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
7166 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
7167}
7168
Avi Kivity51aa01d2010-07-20 14:31:20 +03007169static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03007170{
Avi Kivity00eba012011-03-07 17:24:54 +02007171 u32 exit_intr_info;
7172
7173 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
7174 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
7175 return;
7176
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007177 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02007178 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08007179
7180 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02007181 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08007182 kvm_machine_check();
7183
Gleb Natapov20f65982009-05-11 13:35:55 +03007184 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02007185 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007186 (exit_intr_info & INTR_INFO_VALID_MASK)) {
7187 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03007188 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007189 kvm_after_handle_nmi(&vmx->vcpu);
7190 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03007191}
Gleb Natapov20f65982009-05-11 13:35:55 +03007192
Yang Zhanga547c6d2013-04-11 19:25:10 +08007193static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
7194{
7195 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7196
7197 /*
7198 * If external interrupt exists, IF bit is set in rflags/eflags on the
7199 * interrupt stack frame, and interrupt will be enabled on a return
7200 * from interrupt handler.
7201 */
7202 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
7203 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
7204 unsigned int vector;
7205 unsigned long entry;
7206 gate_desc *desc;
7207 struct vcpu_vmx *vmx = to_vmx(vcpu);
7208#ifdef CONFIG_X86_64
7209 unsigned long tmp;
7210#endif
7211
7212 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
7213 desc = (gate_desc *)vmx->host_idt_base + vector;
7214 entry = gate_offset(*desc);
7215 asm volatile(
7216#ifdef CONFIG_X86_64
7217 "mov %%" _ASM_SP ", %[sp]\n\t"
7218 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
7219 "push $%c[ss]\n\t"
7220 "push %[sp]\n\t"
7221#endif
7222 "pushf\n\t"
7223 "orl $0x200, (%%" _ASM_SP ")\n\t"
7224 __ASM_SIZE(push) " $%c[cs]\n\t"
7225 "call *%[entry]\n\t"
7226 :
7227#ifdef CONFIG_X86_64
7228 [sp]"=&r"(tmp)
7229#endif
7230 :
7231 [entry]"r"(entry),
7232 [ss]"i"(__KERNEL_DS),
7233 [cs]"i"(__KERNEL_CS)
7234 );
7235 } else
7236 local_irq_enable();
7237}
7238
Liu, Jinsongda8999d2014-02-24 10:55:46 +00007239static bool vmx_mpx_supported(void)
7240{
7241 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
7242 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
7243}
7244
Avi Kivity51aa01d2010-07-20 14:31:20 +03007245static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
7246{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007247 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03007248 bool unblock_nmi;
7249 u8 vector;
7250 bool idtv_info_valid;
7251
7252 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03007253
Avi Kivitycf393f72008-07-01 16:20:21 +03007254 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02007255 if (vmx->nmi_known_unmasked)
7256 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007257 /*
7258 * Can't use vmx->exit_intr_info since we're not sure what
7259 * the exit reason is.
7260 */
7261 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03007262 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
7263 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
7264 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007265 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03007266 * Re-set bit "block by NMI" before VM entry if vmexit caused by
7267 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007268 * SDM 3: 23.2.2 (September 2008)
7269 * Bit 12 is undefined in any of the following cases:
7270 * If the VM exit sets the valid bit in the IDT-vectoring
7271 * information field.
7272 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03007273 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007274 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
7275 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03007276 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7277 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02007278 else
7279 vmx->nmi_known_unmasked =
7280 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
7281 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007282 } else if (unlikely(vmx->soft_vnmi_blocked))
7283 vmx->vnmi_blocked_time +=
7284 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03007285}
7286
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007287static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03007288 u32 idt_vectoring_info,
7289 int instr_len_field,
7290 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03007291{
Avi Kivity51aa01d2010-07-20 14:31:20 +03007292 u8 vector;
7293 int type;
7294 bool idtv_info_valid;
7295
7296 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03007297
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007298 vcpu->arch.nmi_injected = false;
7299 kvm_clear_exception_queue(vcpu);
7300 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007301
7302 if (!idtv_info_valid)
7303 return;
7304
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007305 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03007306
Avi Kivity668f6122008-07-02 09:28:55 +03007307 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
7308 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03007309
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007310 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03007311 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007312 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03007313 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007314 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03007315 * Clear bit "block by NMI" before VM entry if a NMI
7316 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03007317 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007318 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007319 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03007320 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007321 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007322 /* fall through */
7323 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03007324 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03007325 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03007326 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03007327 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03007328 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007329 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007330 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007331 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007332 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03007333 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007334 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007335 break;
7336 default:
7337 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03007338 }
Avi Kivitycf393f72008-07-01 16:20:21 +03007339}
7340
Avi Kivity83422e12010-07-20 14:43:23 +03007341static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
7342{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007343 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03007344 VM_EXIT_INSTRUCTION_LEN,
7345 IDT_VECTORING_ERROR_CODE);
7346}
7347
Avi Kivityb463a6f2010-07-20 15:06:17 +03007348static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
7349{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007350 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007351 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
7352 VM_ENTRY_INSTRUCTION_LEN,
7353 VM_ENTRY_EXCEPTION_ERROR_CODE);
7354
7355 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
7356}
7357
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007358static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
7359{
7360 int i, nr_msrs;
7361 struct perf_guest_switch_msr *msrs;
7362
7363 msrs = perf_guest_get_msrs(&nr_msrs);
7364
7365 if (!msrs)
7366 return;
7367
7368 for (i = 0; i < nr_msrs; i++)
7369 if (msrs[i].host == msrs[i].guest)
7370 clear_atomic_switch_msr(vmx, msrs[i].msr);
7371 else
7372 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
7373 msrs[i].host);
7374}
7375
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08007376static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007377{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007378 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03007379 unsigned long debugctlmsr;
Avi Kivity104f2262010-11-18 13:12:52 +02007380
7381 /* Record the guest's net vcpu time for enforced NMI injections. */
7382 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
7383 vmx->entry_time = ktime_get();
7384
7385 /* Don't enter VMX if guest state is invalid, let the exit handler
7386 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02007387 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02007388 return;
7389
Abel Gordon012f83c2013-04-18 14:39:25 +03007390 if (vmx->nested.sync_shadow_vmcs) {
7391 copy_vmcs12_to_shadow(vmx);
7392 vmx->nested.sync_shadow_vmcs = false;
7393 }
7394
Avi Kivity104f2262010-11-18 13:12:52 +02007395 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
7396 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
7397 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
7398 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
7399
7400 /* When single-stepping over STI and MOV SS, we must clear the
7401 * corresponding interruptibility bits in the guest state. Otherwise
7402 * vmentry fails as it then expects bit 14 (BS) in pending debug
7403 * exceptions being set, but that's not correct for the guest debugging
7404 * case. */
7405 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
7406 vmx_set_interrupt_shadow(vcpu, 0);
7407
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007408 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03007409 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007410
Nadav Har'Eld462b812011-05-24 15:26:10 +03007411 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02007412 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08007413 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007414 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
7415 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
7416 "push %%" _ASM_CX " \n\t"
7417 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03007418 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007419 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03007420 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03007421 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007422 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007423 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
7424 "mov %%cr2, %%" _ASM_DX " \n\t"
7425 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007426 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007427 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007428 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007429 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02007430 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007431 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007432 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
7433 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
7434 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
7435 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
7436 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
7437 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08007438#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02007439 "mov %c[r8](%0), %%r8 \n\t"
7440 "mov %c[r9](%0), %%r9 \n\t"
7441 "mov %c[r10](%0), %%r10 \n\t"
7442 "mov %c[r11](%0), %%r11 \n\t"
7443 "mov %c[r12](%0), %%r12 \n\t"
7444 "mov %c[r13](%0), %%r13 \n\t"
7445 "mov %c[r14](%0), %%r14 \n\t"
7446 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007447#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03007448 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03007449
Avi Kivity6aa8b732006-12-10 02:21:36 -08007450 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03007451 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03007452 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03007453 "jmp 2f \n\t"
7454 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
7455 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08007456 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007457 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02007458 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007459 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
7460 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
7461 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
7462 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
7463 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
7464 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
7465 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08007466#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02007467 "mov %%r8, %c[r8](%0) \n\t"
7468 "mov %%r9, %c[r9](%0) \n\t"
7469 "mov %%r10, %c[r10](%0) \n\t"
7470 "mov %%r11, %c[r11](%0) \n\t"
7471 "mov %%r12, %c[r12](%0) \n\t"
7472 "mov %%r13, %c[r13](%0) \n\t"
7473 "mov %%r14, %c[r14](%0) \n\t"
7474 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007475#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03007476 "mov %%cr2, %%" _ASM_AX " \n\t"
7477 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03007478
Avi Kivityb188c81f2012-09-16 15:10:58 +03007479 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02007480 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03007481 ".pushsection .rodata \n\t"
7482 ".global vmx_return \n\t"
7483 "vmx_return: " _ASM_PTR " 2b \n\t"
7484 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02007485 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03007486 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02007487 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03007488 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007489 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
7490 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
7491 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
7492 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
7493 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
7494 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
7495 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08007496#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007497 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
7498 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
7499 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
7500 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
7501 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
7502 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
7503 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
7504 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08007505#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02007506 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
7507 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02007508 : "cc", "memory"
7509#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03007510 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02007511 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007512#else
7513 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02007514#endif
7515 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08007516
Gleb Natapov2a7921b2012-08-12 16:12:29 +03007517 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
7518 if (debugctlmsr)
7519 update_debugctlmsr(debugctlmsr);
7520
Avi Kivityaa67f602012-08-01 16:48:03 +03007521#ifndef CONFIG_X86_64
7522 /*
7523 * The sysexit path does not restore ds/es, so we must set them to
7524 * a reasonable value ourselves.
7525 *
7526 * We can't defer this to vmx_load_host_state() since that function
7527 * may be executed in interrupt context, which saves and restore segments
7528 * around it, nullifying its effect.
7529 */
7530 loadsegment(ds, __USER_DS);
7531 loadsegment(es, __USER_DS);
7532#endif
7533
Avi Kivity6de4f3a2009-05-31 22:58:47 +03007534 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02007535 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02007536 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03007537 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02007538 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007539 vcpu->arch.regs_dirty = 0;
7540
Avi Kivity1155f762007-11-22 11:30:47 +02007541 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
7542
Nadav Har'Eld462b812011-05-24 15:26:10 +03007543 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02007544
Avi Kivity51aa01d2010-07-20 14:31:20 +03007545 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02007546 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
Avi Kivity51aa01d2010-07-20 14:31:20 +03007547
Gleb Natapove0b890d2013-09-25 12:51:33 +03007548 /*
7549 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
7550 * we did not inject a still-pending event to L1 now because of
7551 * nested_run_pending, we need to re-enable this bit.
7552 */
7553 if (vmx->nested.nested_run_pending)
7554 kvm_make_request(KVM_REQ_EVENT, vcpu);
7555
7556 vmx->nested.nested_run_pending = 0;
7557
Avi Kivity51aa01d2010-07-20 14:31:20 +03007558 vmx_complete_atomic_exit(vmx);
7559 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03007560 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007561}
7562
Paolo Bonzini4fa77342014-07-17 12:25:16 +02007563static void vmx_load_vmcs01(struct kvm_vcpu *vcpu)
7564{
7565 struct vcpu_vmx *vmx = to_vmx(vcpu);
7566 int cpu;
7567
7568 if (vmx->loaded_vmcs == &vmx->vmcs01)
7569 return;
7570
7571 cpu = get_cpu();
7572 vmx->loaded_vmcs = &vmx->vmcs01;
7573 vmx_vcpu_put(vcpu);
7574 vmx_vcpu_load(vcpu, cpu);
7575 vcpu->cpu = cpu;
7576 put_cpu();
7577}
7578
Avi Kivity6aa8b732006-12-10 02:21:36 -08007579static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
7580{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007581 struct vcpu_vmx *vmx = to_vmx(vcpu);
7582
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08007583 free_vpid(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02007584 leave_guest_mode(vcpu);
7585 vmx_load_vmcs01(vcpu);
Marcelo Tosatti26a865f2014-01-03 17:00:51 -02007586 free_nested(vmx);
Paolo Bonzini4fa77342014-07-17 12:25:16 +02007587 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007588 kfree(vmx->guest_msrs);
7589 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10007590 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007591}
7592
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007593static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007594{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007595 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10007596 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03007597 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007598
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007599 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007600 return ERR_PTR(-ENOMEM);
7601
Sheng Yang2384d2b2008-01-17 15:14:33 +08007602 allocate_vpid(vmx);
7603
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007604 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
7605 if (err)
7606 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08007607
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007608 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paolo Bonzini03916db2014-07-24 14:21:57 +02007609 BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
7610 > PAGE_SIZE);
Nadav Amit0123be42014-07-24 15:06:56 +03007611
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02007612 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007613 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007614 goto uninit_vcpu;
7615 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08007616
Nadav Har'Eld462b812011-05-24 15:26:10 +03007617 vmx->loaded_vmcs = &vmx->vmcs01;
7618 vmx->loaded_vmcs->vmcs = alloc_vmcs();
7619 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007620 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03007621 if (!vmm_exclusive)
7622 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
7623 loaded_vmcs_init(vmx->loaded_vmcs);
7624 if (!vmm_exclusive)
7625 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007626
Avi Kivity15ad7142007-07-11 18:17:21 +03007627 cpu = get_cpu();
7628 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10007629 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10007630 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007631 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03007632 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007633 if (err)
7634 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02007635 if (vm_need_virtualize_apic_accesses(kvm)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02007636 err = alloc_apic_access_page(kvm);
7637 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02007638 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02007639 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08007640
Sheng Yangb927a3c2009-07-21 10:42:48 +08007641 if (enable_ept) {
7642 if (!kvm->arch.ept_identity_map_addr)
7643 kvm->arch.ept_identity_map_addr =
7644 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Gleb Natapov93ea5382011-02-21 12:07:59 +02007645 err = -ENOMEM;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007646 if (alloc_identity_pagetable(kvm) != 0)
7647 goto free_vmcs;
Gleb Natapov93ea5382011-02-21 12:07:59 +02007648 if (!init_rmode_identity_map(kvm))
7649 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08007650 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007651
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03007652 vmx->nested.current_vmptr = -1ull;
7653 vmx->nested.current_vmcs12 = NULL;
7654
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007655 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08007656
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007657free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08007658 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007659free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007660 kfree(vmx->guest_msrs);
7661uninit_vcpu:
7662 kvm_vcpu_uninit(&vmx->vcpu);
7663free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08007664 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10007665 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007666 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007667}
7668
Yang, Sheng002c7f72007-07-31 14:23:01 +03007669static void __init vmx_check_processor_compat(void *rtn)
7670{
7671 struct vmcs_config vmcs_conf;
7672
7673 *(int *)rtn = 0;
7674 if (setup_vmcs_config(&vmcs_conf) < 0)
7675 *(int *)rtn = -EIO;
7676 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
7677 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
7678 smp_processor_id());
7679 *(int *)rtn = -EIO;
7680 }
7681}
7682
Sheng Yang67253af2008-04-25 10:20:22 +08007683static int get_ept_level(void)
7684{
7685 return VMX_EPT_DEFAULT_GAW + 1;
7686}
7687
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007688static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08007689{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007690 u64 ret;
7691
Sheng Yang522c68c2009-04-27 20:35:43 +08007692 /* For VT-d and EPT combination
7693 * 1. MMIO: always map as UC
7694 * 2. EPT with VT-d:
7695 * a. VT-d without snooping control feature: can't guarantee the
7696 * result, try to trust guest.
7697 * b. VT-d with snooping control feature: snooping control feature of
7698 * VT-d engine can guarantee the cache correctness. Just set it
7699 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08007700 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08007701 * consistent with host MTRR
7702 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007703 if (is_mmio)
7704 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06007705 else if (kvm_arch_has_noncoherent_dma(vcpu->kvm))
Sheng Yang522c68c2009-04-27 20:35:43 +08007706 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
7707 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007708 else
Sheng Yang522c68c2009-04-27 20:35:43 +08007709 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08007710 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007711
7712 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08007713}
7714
Sheng Yang17cc3932010-01-05 19:02:27 +08007715static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02007716{
Sheng Yang878403b2010-01-05 19:02:29 +08007717 if (enable_ept && !cpu_has_vmx_ept_1g_page())
7718 return PT_DIRECTORY_LEVEL;
7719 else
7720 /* For shadow and EPT supported 1GB page */
7721 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02007722}
7723
Sheng Yang0e851882009-12-18 16:48:46 +08007724static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
7725{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007726 struct kvm_cpuid_entry2 *best;
7727 struct vcpu_vmx *vmx = to_vmx(vcpu);
7728 u32 exec_control;
7729
7730 vmx->rdtscp_enabled = false;
7731 if (vmx_rdtscp_supported()) {
7732 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7733 if (exec_control & SECONDARY_EXEC_RDTSCP) {
7734 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
7735 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
7736 vmx->rdtscp_enabled = true;
7737 else {
7738 exec_control &= ~SECONDARY_EXEC_RDTSCP;
7739 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7740 exec_control);
7741 }
7742 }
7743 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007744
Mao, Junjiead756a12012-07-02 01:18:48 +00007745 /* Exposing INVPCID only when PCID is exposed */
7746 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
7747 if (vmx_invpcid_supported() &&
Ren, Yongjie4f977042012-09-07 07:36:59 +00007748 best && (best->ebx & bit(X86_FEATURE_INVPCID)) &&
Mao, Junjiead756a12012-07-02 01:18:48 +00007749 guest_cpuid_has_pcid(vcpu)) {
Takashi Iwai29282fd2012-11-09 15:20:17 +01007750 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Mao, Junjiead756a12012-07-02 01:18:48 +00007751 exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
7752 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7753 exec_control);
7754 } else {
Takashi Iwai29282fd2012-11-09 15:20:17 +01007755 if (cpu_has_secondary_exec_ctrls()) {
7756 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7757 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
7758 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7759 exec_control);
7760 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007761 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00007762 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00007763 }
Sheng Yang0e851882009-12-18 16:48:46 +08007764}
7765
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007766static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
7767{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03007768 if (func == 1 && nested)
7769 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007770}
7771
Yang Zhang25d92082013-08-06 12:00:32 +03007772static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
7773 struct x86_exception *fault)
7774{
Jan Kiszka533558b2014-01-04 18:47:20 +01007775 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7776 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03007777
7778 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01007779 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03007780 else
Jan Kiszka533558b2014-01-04 18:47:20 +01007781 exit_reason = EXIT_REASON_EPT_VIOLATION;
7782 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03007783 vmcs12->guest_physical_address = fault->address;
7784}
7785
Nadav Har'El155a97a2013-08-05 11:07:16 +03007786/* Callbacks for nested_ept_init_mmu_context: */
7787
7788static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
7789{
7790 /* return the page table to be shadowed - in our case, EPT12 */
7791 return get_vmcs12(vcpu)->ept_pointer;
7792}
7793
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02007794static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03007795{
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02007796 kvm_init_shadow_ept_mmu(vcpu, &vcpu->arch.mmu,
Nadav Har'El155a97a2013-08-05 11:07:16 +03007797 nested_vmx_ept_caps & VMX_EPT_EXECUTE_ONLY_BIT);
7798
7799 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
7800 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
7801 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
7802
7803 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03007804}
7805
7806static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
7807{
7808 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
7809}
7810
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03007811static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
7812 struct x86_exception *fault)
7813{
7814 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7815
7816 WARN_ON(!is_guest_mode(vcpu));
7817
7818 /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
7819 if (vmcs12->exception_bitmap & (1u << PF_VECTOR))
Jan Kiszka533558b2014-01-04 18:47:20 +01007820 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
7821 vmcs_read32(VM_EXIT_INTR_INFO),
7822 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03007823 else
7824 kvm_inject_page_fault(vcpu, fault);
7825}
7826
Jan Kiszkaf4124502014-03-07 20:03:13 +01007827static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
7828{
7829 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
7830 struct vcpu_vmx *vmx = to_vmx(vcpu);
7831
7832 if (vcpu->arch.virtual_tsc_khz == 0)
7833 return;
7834
7835 /* Make sure short timeouts reliably trigger an immediate vmexit.
7836 * hrtimer_start does not guarantee this. */
7837 if (preemption_timeout <= 1) {
7838 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
7839 return;
7840 }
7841
7842 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
7843 preemption_timeout *= 1000000;
7844 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
7845 hrtimer_start(&vmx->nested.preemption_timer,
7846 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
7847}
7848
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007849/*
7850 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
7851 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
7852 * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2
7853 * guest in a way that will both be appropriate to L1's requests, and our
7854 * needs. In addition to modifying the active vmcs (which is vmcs02), this
7855 * function also has additional necessary side-effects, like setting various
7856 * vcpu->arch fields.
7857 */
7858static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7859{
7860 struct vcpu_vmx *vmx = to_vmx(vcpu);
7861 u32 exec_control;
7862
7863 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
7864 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
7865 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
7866 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
7867 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
7868 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
7869 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
7870 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
7871 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
7872 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
7873 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
7874 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
7875 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
7876 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
7877 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
7878 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
7879 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
7880 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
7881 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
7882 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
7883 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
7884 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
7885 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
7886 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
7887 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
7888 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
7889 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
7890 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
7891 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
7892 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
7893 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
7894 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
7895 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
7896 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
7897 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
7898 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
7899
Jan Kiszka2996fca2014-06-16 13:59:43 +02007900 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) {
7901 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
7902 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
7903 } else {
7904 kvm_set_dr(vcpu, 7, vcpu->arch.dr7);
7905 vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl);
7906 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007907 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
7908 vmcs12->vm_entry_intr_info_field);
7909 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
7910 vmcs12->vm_entry_exception_error_code);
7911 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
7912 vmcs12->vm_entry_instruction_len);
7913 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
7914 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007915 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Gleb Natapov63fbf592013-07-28 18:31:06 +03007916 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007917 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
7918 vmcs12->guest_pending_dbg_exceptions);
7919 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
7920 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
7921
7922 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7923
Jan Kiszkaf4124502014-03-07 20:03:13 +01007924 exec_control = vmcs12->pin_based_vm_exec_control;
7925 exec_control |= vmcs_config.pin_based_exec_ctrl;
Paolo Bonzini696dfd92014-05-07 11:20:54 +02007926 exec_control &= ~(PIN_BASED_VMX_PREEMPTION_TIMER |
7927 PIN_BASED_POSTED_INTR);
Jan Kiszkaf4124502014-03-07 20:03:13 +01007928 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007929
Jan Kiszkaf4124502014-03-07 20:03:13 +01007930 vmx->nested.preemption_timer_expired = false;
7931 if (nested_cpu_has_preemption_timer(vmcs12))
7932 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +01007933
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007934 /*
7935 * Whether page-faults are trapped is determined by a combination of
7936 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
7937 * If enable_ept, L0 doesn't care about page faults and we should
7938 * set all of these to L1's desires. However, if !enable_ept, L0 does
7939 * care about (at least some) page faults, and because it is not easy
7940 * (if at all possible?) to merge L0 and L1's desires, we simply ask
7941 * to exit on each and every L2 page fault. This is done by setting
7942 * MASK=MATCH=0 and (see below) EB.PF=1.
7943 * Note that below we don't need special code to set EB.PF beyond the
7944 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
7945 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
7946 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
7947 *
7948 * A problem with this approach (when !enable_ept) is that L1 may be
7949 * injected with more page faults than it asked for. This could have
7950 * caused problems, but in practice existing hypervisors don't care.
7951 * To fix this, we will need to emulate the PFEC checking (on the L1
7952 * page tables), using walk_addr(), when injecting PFs to L1.
7953 */
7954 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
7955 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
7956 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
7957 enable_ept ? vmcs12->page_fault_error_code_match : 0);
7958
7959 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf4124502014-03-07 20:03:13 +01007960 exec_control = vmx_secondary_exec_control(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007961 if (!vmx->rdtscp_enabled)
7962 exec_control &= ~SECONDARY_EXEC_RDTSCP;
7963 /* Take the following fields only from vmcs12 */
Paolo Bonzini696dfd92014-05-07 11:20:54 +02007964 exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
7965 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
7966 SECONDARY_EXEC_APIC_REGISTER_VIRT);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007967 if (nested_cpu_has(vmcs12,
7968 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
7969 exec_control |= vmcs12->secondary_vm_exec_control;
7970
7971 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
7972 /*
7973 * Translate L1 physical address to host physical
7974 * address for vmcs02. Keep the page pinned, so this
7975 * physical address remains valid. We keep a reference
7976 * to it so we can release it later.
7977 */
7978 if (vmx->nested.apic_access_page) /* shouldn't happen */
7979 nested_release_page(vmx->nested.apic_access_page);
7980 vmx->nested.apic_access_page =
7981 nested_get_page(vcpu, vmcs12->apic_access_addr);
7982 /*
7983 * If translation failed, no matter: This feature asks
7984 * to exit when accessing the given address, and if it
7985 * can never be accessed, this feature won't do
7986 * anything anyway.
7987 */
7988 if (!vmx->nested.apic_access_page)
7989 exec_control &=
7990 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7991 else
7992 vmcs_write64(APIC_ACCESS_ADDR,
7993 page_to_phys(vmx->nested.apic_access_page));
Jan Kiszkaca3f2572013-12-16 12:55:46 +01007994 } else if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) {
7995 exec_control |=
7996 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7997 vmcs_write64(APIC_ACCESS_ADDR,
7998 page_to_phys(vcpu->kvm->arch.apic_access_page));
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007999 }
8000
8001 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
8002 }
8003
8004
8005 /*
8006 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
8007 * Some constant fields are set here by vmx_set_constant_host_state().
8008 * Other fields are different per CPU, and will be set later when
8009 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
8010 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08008011 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008012
8013 /*
8014 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
8015 * entry, but only if the current (host) sp changed from the value
8016 * we wrote last (vmx->host_rsp). This cache is no longer relevant
8017 * if we switch vmcs, and rather than hold a separate cache per vmcs,
8018 * here we just force the write to happen on entry.
8019 */
8020 vmx->host_rsp = 0;
8021
8022 exec_control = vmx_exec_control(vmx); /* L0's desires */
8023 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
8024 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
8025 exec_control &= ~CPU_BASED_TPR_SHADOW;
8026 exec_control |= vmcs12->cpu_based_vm_exec_control;
8027 /*
8028 * Merging of IO and MSR bitmaps not currently supported.
8029 * Rather, exit every time.
8030 */
8031 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
8032 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
8033 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
8034
8035 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
8036
8037 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
8038 * bitwise-or of what L1 wants to trap for L2, and what we want to
8039 * trap. Note that CR0.TS also needs updating - we do this later.
8040 */
8041 update_exception_bitmap(vcpu);
8042 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
8043 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
8044
Nadav Har'El8049d652013-08-05 11:07:06 +03008045 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
8046 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
8047 * bits are further modified by vmx_set_efer() below.
8048 */
Jan Kiszkaf4124502014-03-07 20:03:13 +01008049 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +03008050
8051 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
8052 * emulated by vmx_set_efer(), below.
8053 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02008054 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +03008055 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
8056 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008057 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
8058
Jan Kiszka44811c02013-08-04 17:17:27 +02008059 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008060 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02008061 vcpu->arch.pat = vmcs12->guest_ia32_pat;
8062 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008063 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
8064
8065
8066 set_cr4_guest_host_mask(vmx);
8067
Paolo Bonzini36be0b92014-02-24 12:30:04 +01008068 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
8069 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
8070
Nadav Har'El27fc51b2011-08-02 15:54:52 +03008071 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
8072 vmcs_write64(TSC_OFFSET,
8073 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
8074 else
8075 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008076
8077 if (enable_vpid) {
8078 /*
8079 * Trivially support vpid by letting L2s share their parent
8080 * L1's vpid. TODO: move to a more elaborate solution, giving
8081 * each L2 its own vpid and exposing the vpid feature to L1.
8082 */
8083 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
8084 vmx_flush_tlb(vcpu);
8085 }
8086
Nadav Har'El155a97a2013-08-05 11:07:16 +03008087 if (nested_cpu_has_ept(vmcs12)) {
8088 kvm_mmu_unload(vcpu);
8089 nested_ept_init_mmu_context(vcpu);
8090 }
8091
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008092 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
8093 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +02008094 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008095 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
8096 else
8097 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
8098 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
8099 vmx_set_efer(vcpu, vcpu->arch.efer);
8100
8101 /*
8102 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
8103 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
8104 * The CR0_READ_SHADOW is what L2 should have expected to read given
8105 * the specifications by L1; It's not enough to take
8106 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
8107 * have more bits than L1 expected.
8108 */
8109 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
8110 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
8111
8112 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
8113 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
8114
8115 /* shadow page tables on either EPT or shadow page tables */
8116 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
8117 kvm_mmu_reset_context(vcpu);
8118
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03008119 if (!enable_ept)
8120 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
8121
Nadav Har'El3633cfc2013-08-05 11:07:07 +03008122 /*
8123 * L1 may access the L2's PDPTR, so save them to construct vmcs12
8124 */
8125 if (enable_ept) {
8126 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
8127 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
8128 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
8129 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
8130 }
8131
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03008132 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
8133 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
8134}
8135
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008136/*
8137 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
8138 * for running an L2 nested guest.
8139 */
8140static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
8141{
8142 struct vmcs12 *vmcs12;
8143 struct vcpu_vmx *vmx = to_vmx(vcpu);
8144 int cpu;
8145 struct loaded_vmcs *vmcs02;
Jan Kiszka384bb782013-04-20 10:52:36 +02008146 bool ia32e;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008147
8148 if (!nested_vmx_check_permission(vcpu) ||
8149 !nested_vmx_check_vmcs12(vcpu))
8150 return 1;
8151
8152 skip_emulated_instruction(vcpu);
8153 vmcs12 = get_vmcs12(vcpu);
8154
Abel Gordon012f83c2013-04-18 14:39:25 +03008155 if (enable_shadow_vmcs)
8156 copy_shadow_to_vmcs12(vmx);
8157
Nadav Har'El7c177932011-05-25 23:12:04 +03008158 /*
8159 * The nested entry process starts with enforcing various prerequisites
8160 * on vmcs12 as required by the Intel SDM, and act appropriately when
8161 * they fail: As the SDM explains, some conditions should cause the
8162 * instruction to fail, while others will cause the instruction to seem
8163 * to succeed, but return an EXIT_REASON_INVALID_STATE.
8164 * To speed up the normal (success) code path, we should avoid checking
8165 * for misconfigurations which will anyway be caught by the processor
8166 * when using the merged vmcs02.
8167 */
8168 if (vmcs12->launch_state == launch) {
8169 nested_vmx_failValid(vcpu,
8170 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
8171 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
8172 return 1;
8173 }
8174
Jan Kiszka6dfacad2013-12-04 08:58:54 +01008175 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
8176 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +02008177 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8178 return 1;
8179 }
8180
Nadav Har'El7c177932011-05-25 23:12:04 +03008181 if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02008182 !PAGE_ALIGNED(vmcs12->msr_bitmap)) {
Nadav Har'El7c177932011-05-25 23:12:04 +03008183 /*TODO: Also verify bits beyond physical address width are 0*/
8184 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8185 return 1;
8186 }
8187
8188 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
Fabian Frederickbc39c4d2014-06-14 23:44:29 +02008189 !PAGE_ALIGNED(vmcs12->apic_access_addr)) {
Nadav Har'El7c177932011-05-25 23:12:04 +03008190 /*TODO: Also verify bits beyond physical address width are 0*/
8191 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8192 return 1;
8193 }
8194
8195 if (vmcs12->vm_entry_msr_load_count > 0 ||
8196 vmcs12->vm_exit_msr_load_count > 0 ||
8197 vmcs12->vm_exit_msr_store_count > 0) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008198 pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n",
8199 __func__);
Nadav Har'El7c177932011-05-25 23:12:04 +03008200 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8201 return 1;
8202 }
8203
8204 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
Jan Kiszka3dcdf3e2014-06-16 13:59:41 +02008205 nested_vmx_true_procbased_ctls_low,
8206 nested_vmx_procbased_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03008207 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
8208 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
8209 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
8210 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
8211 !vmx_control_verify(vmcs12->vm_exit_controls,
Jan Kiszka2996fca2014-06-16 13:59:43 +02008212 nested_vmx_true_exit_ctls_low,
8213 nested_vmx_exit_ctls_high) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03008214 !vmx_control_verify(vmcs12->vm_entry_controls,
Jan Kiszka2996fca2014-06-16 13:59:43 +02008215 nested_vmx_true_entry_ctls_low,
8216 nested_vmx_entry_ctls_high))
Nadav Har'El7c177932011-05-25 23:12:04 +03008217 {
8218 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8219 return 1;
8220 }
8221
8222 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
8223 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
8224 nested_vmx_failValid(vcpu,
8225 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
8226 return 1;
8227 }
8228
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02008229 if (!nested_cr0_valid(vmcs12, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03008230 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
8231 nested_vmx_entry_failure(vcpu, vmcs12,
8232 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
8233 return 1;
8234 }
8235 if (vmcs12->vmcs_link_pointer != -1ull) {
8236 nested_vmx_entry_failure(vcpu, vmcs12,
8237 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
8238 return 1;
8239 }
8240
8241 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +02008242 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +02008243 * are performed on the field for the IA32_EFER MSR:
8244 * - Bits reserved in the IA32_EFER MSR must be 0.
8245 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
8246 * the IA-32e mode guest VM-exit control. It must also be identical
8247 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
8248 * CR0.PG) is 1.
8249 */
8250 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
8251 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
8252 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
8253 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
8254 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
8255 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
8256 nested_vmx_entry_failure(vcpu, vmcs12,
8257 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
8258 return 1;
8259 }
8260 }
8261
8262 /*
8263 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
8264 * IA32_EFER MSR must be 0 in the field for that register. In addition,
8265 * the values of the LMA and LME bits in the field must each be that of
8266 * the host address-space size VM-exit control.
8267 */
8268 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
8269 ia32e = (vmcs12->vm_exit_controls &
8270 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
8271 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
8272 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
8273 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
8274 nested_vmx_entry_failure(vcpu, vmcs12,
8275 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
8276 return 1;
8277 }
8278 }
8279
8280 /*
Nadav Har'El7c177932011-05-25 23:12:04 +03008281 * We're finally done with prerequisite checking, and can start with
8282 * the nested entry.
8283 */
8284
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008285 vmcs02 = nested_get_current_vmcs02(vmx);
8286 if (!vmcs02)
8287 return -ENOMEM;
8288
8289 enter_guest_mode(vcpu);
8290
8291 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
8292
Jan Kiszka2996fca2014-06-16 13:59:43 +02008293 if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS))
8294 vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
8295
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008296 cpu = get_cpu();
8297 vmx->loaded_vmcs = vmcs02;
8298 vmx_vcpu_put(vcpu);
8299 vmx_vcpu_load(vcpu, cpu);
8300 vcpu->cpu = cpu;
8301 put_cpu();
8302
Jan Kiszka36c3cc42013-02-23 22:35:37 +01008303 vmx_segment_cache_clear(vmx);
8304
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008305 vmcs12->launch_state = 1;
8306
8307 prepare_vmcs02(vcpu, vmcs12);
8308
Jan Kiszka6dfacad2013-12-04 08:58:54 +01008309 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
8310 return kvm_emulate_halt(vcpu);
8311
Jan Kiszka7af40ad32014-01-04 18:47:23 +01008312 vmx->nested.nested_run_pending = 1;
8313
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008314 /*
8315 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
8316 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
8317 * returned as far as L1 is concerned. It will only return (and set
8318 * the success flag) when L2 exits (see nested_vmx_vmexit()).
8319 */
8320 return 1;
8321}
8322
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008323/*
8324 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
8325 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
8326 * This function returns the new value we should put in vmcs12.guest_cr0.
8327 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
8328 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
8329 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
8330 * didn't trap the bit, because if L1 did, so would L0).
8331 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
8332 * been modified by L2, and L1 knows it. So just leave the old value of
8333 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
8334 * isn't relevant, because if L0 traps this bit it can set it to anything.
8335 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
8336 * changed these bits, and therefore they need to be updated, but L0
8337 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
8338 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
8339 */
8340static inline unsigned long
8341vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
8342{
8343 return
8344 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
8345 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
8346 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
8347 vcpu->arch.cr0_guest_owned_bits));
8348}
8349
8350static inline unsigned long
8351vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
8352{
8353 return
8354 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
8355 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
8356 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
8357 vcpu->arch.cr4_guest_owned_bits));
8358}
8359
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008360static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
8361 struct vmcs12 *vmcs12)
8362{
8363 u32 idt_vectoring;
8364 unsigned int nr;
8365
Gleb Natapov851eb6672013-09-25 12:51:34 +03008366 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008367 nr = vcpu->arch.exception.nr;
8368 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
8369
8370 if (kvm_exception_is_soft(nr)) {
8371 vmcs12->vm_exit_instruction_len =
8372 vcpu->arch.event_exit_inst_len;
8373 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
8374 } else
8375 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
8376
8377 if (vcpu->arch.exception.has_error_code) {
8378 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
8379 vmcs12->idt_vectoring_error_code =
8380 vcpu->arch.exception.error_code;
8381 }
8382
8383 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +01008384 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008385 vmcs12->idt_vectoring_info_field =
8386 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
8387 } else if (vcpu->arch.interrupt.pending) {
8388 nr = vcpu->arch.interrupt.nr;
8389 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
8390
8391 if (vcpu->arch.interrupt.soft) {
8392 idt_vectoring |= INTR_TYPE_SOFT_INTR;
8393 vmcs12->vm_entry_instruction_len =
8394 vcpu->arch.event_exit_inst_len;
8395 } else
8396 idt_vectoring |= INTR_TYPE_EXT_INTR;
8397
8398 vmcs12->idt_vectoring_info_field = idt_vectoring;
8399 }
8400}
8401
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008402static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
8403{
8404 struct vcpu_vmx *vmx = to_vmx(vcpu);
8405
Jan Kiszkaf4124502014-03-07 20:03:13 +01008406 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
8407 vmx->nested.preemption_timer_expired) {
8408 if (vmx->nested.nested_run_pending)
8409 return -EBUSY;
8410 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
8411 return 0;
8412 }
8413
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008414 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +01008415 if (vmx->nested.nested_run_pending ||
8416 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008417 return -EBUSY;
8418 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
8419 NMI_VECTOR | INTR_TYPE_NMI_INTR |
8420 INTR_INFO_VALID_MASK, 0);
8421 /*
8422 * The NMI-triggered VM exit counts as injection:
8423 * clear this one and block further NMIs.
8424 */
8425 vcpu->arch.nmi_pending = 0;
8426 vmx_set_nmi_mask(vcpu, true);
8427 return 0;
8428 }
8429
8430 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
8431 nested_exit_on_intr(vcpu)) {
8432 if (vmx->nested.nested_run_pending)
8433 return -EBUSY;
8434 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
8435 }
8436
8437 return 0;
8438}
8439
Jan Kiszkaf4124502014-03-07 20:03:13 +01008440static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
8441{
8442 ktime_t remaining =
8443 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
8444 u64 value;
8445
8446 if (ktime_to_ns(remaining) <= 0)
8447 return 0;
8448
8449 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
8450 do_div(value, 1000000);
8451 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
8452}
8453
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008454/*
8455 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
8456 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
8457 * and this function updates it to reflect the changes to the guest state while
8458 * L2 was running (and perhaps made some exits which were handled directly by L0
8459 * without going back to L1), and to reflect the exit reason.
8460 * Note that we do not have to copy here all VMCS fields, just those that
8461 * could have changed by the L2 guest or the exit - i.e., the guest-state and
8462 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
8463 * which already writes to vmcs12 directly.
8464 */
Jan Kiszka533558b2014-01-04 18:47:20 +01008465static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
8466 u32 exit_reason, u32 exit_intr_info,
8467 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008468{
8469 /* update guest state fields: */
8470 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
8471 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
8472
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008473 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
8474 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
8475 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
8476
8477 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
8478 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
8479 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
8480 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
8481 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
8482 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
8483 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
8484 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
8485 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
8486 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
8487 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
8488 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
8489 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
8490 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
8491 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
8492 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
8493 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
8494 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
8495 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
8496 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
8497 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
8498 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
8499 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
8500 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
8501 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
8502 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
8503 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
8504 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
8505 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
8506 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
8507 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
8508 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
8509 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
8510 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
8511 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
8512 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
8513
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008514 vmcs12->guest_interruptibility_info =
8515 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
8516 vmcs12->guest_pending_dbg_exceptions =
8517 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +01008518 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
8519 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
8520 else
8521 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008522
Jan Kiszkaf4124502014-03-07 20:03:13 +01008523 if (nested_cpu_has_preemption_timer(vmcs12)) {
8524 if (vmcs12->vm_exit_controls &
8525 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
8526 vmcs12->vmx_preemption_timer_value =
8527 vmx_get_preemption_timer_value(vcpu);
8528 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
8529 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +08008530
Nadav Har'El3633cfc2013-08-05 11:07:07 +03008531 /*
8532 * In some cases (usually, nested EPT), L2 is allowed to change its
8533 * own CR3 without exiting. If it has changed it, we must keep it.
8534 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
8535 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
8536 *
8537 * Additionally, restore L2's PDPTR to vmcs12.
8538 */
8539 if (enable_ept) {
8540 vmcs12->guest_cr3 = vmcs_read64(GUEST_CR3);
8541 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
8542 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
8543 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
8544 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
8545 }
8546
Jan Kiszkac18911a2013-03-13 16:06:41 +01008547 vmcs12->vm_entry_controls =
8548 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +02008549 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +01008550
Jan Kiszka2996fca2014-06-16 13:59:43 +02008551 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) {
8552 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
8553 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
8554 }
8555
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008556 /* TODO: These cannot have changed unless we have MSR bitmaps and
8557 * the relevant bit asks not to trap the change */
Jan Kiszkab8c07d52013-04-06 13:51:21 +02008558 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008559 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +02008560 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
8561 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008562 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
8563 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
8564 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzini36be0b92014-02-24 12:30:04 +01008565 if (vmx_mpx_supported())
8566 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008567
8568 /* update exit information fields: */
8569
Jan Kiszka533558b2014-01-04 18:47:20 +01008570 vmcs12->vm_exit_reason = exit_reason;
8571 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008572
Jan Kiszka533558b2014-01-04 18:47:20 +01008573 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +02008574 if ((vmcs12->vm_exit_intr_info &
8575 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8576 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
8577 vmcs12->vm_exit_intr_error_code =
8578 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008579 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008580 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
8581 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8582
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008583 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
8584 /* vm_entry_intr_info_field is cleared on exit. Emulate this
8585 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008586 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008587
8588 /*
8589 * Transfer the event that L0 or L1 may wanted to inject into
8590 * L2 to IDT_VECTORING_INFO_FIELD.
8591 */
8592 vmcs12_save_pending_event(vcpu, vmcs12);
8593 }
8594
8595 /*
8596 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
8597 * preserved above and would only end up incorrectly in L1.
8598 */
8599 vcpu->arch.nmi_injected = false;
8600 kvm_clear_exception_queue(vcpu);
8601 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008602}
8603
8604/*
8605 * A part of what we need to when the nested L2 guest exits and we want to
8606 * run its L1 parent, is to reset L1's guest state to the host state specified
8607 * in vmcs12.
8608 * This function is to be called not only on normal nested exit, but also on
8609 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
8610 * Failures During or After Loading Guest State").
8611 * This function should be called when the active VMCS is L1's (vmcs01).
8612 */
Jan Kiszka733568f2013-02-23 15:07:47 +01008613static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
8614 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008615{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08008616 struct kvm_segment seg;
8617
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008618 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
8619 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +02008620 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008621 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
8622 else
8623 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
8624 vmx_set_efer(vcpu, vcpu->arch.efer);
8625
8626 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
8627 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -07008628 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008629 /*
8630 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
8631 * actually changed, because it depends on the current state of
8632 * fpu_active (which may have changed).
8633 * Note that vmx_set_cr0 refers to efer set above.
8634 */
Jan Kiszka9e3e4db2013-09-03 21:11:45 +02008635 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008636 /*
8637 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
8638 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
8639 * but we also need to update cr0_guest_host_mask and exception_bitmap.
8640 */
8641 update_exception_bitmap(vcpu);
8642 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
8643 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
8644
8645 /*
8646 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
8647 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
8648 */
8649 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
8650 kvm_set_cr4(vcpu, vmcs12->host_cr4);
8651
Jan Kiszka29bf08f2013-12-28 16:31:52 +01008652 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +03008653
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008654 kvm_set_cr3(vcpu, vmcs12->host_cr3);
8655 kvm_mmu_reset_context(vcpu);
8656
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03008657 if (!enable_ept)
8658 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
8659
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008660 if (enable_vpid) {
8661 /*
8662 * Trivially support vpid by letting L2s share their parent
8663 * L1's vpid. TODO: move to a more elaborate solution, giving
8664 * each L2 its own vpid and exposing the vpid feature to L1.
8665 */
8666 vmx_flush_tlb(vcpu);
8667 }
8668
8669
8670 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
8671 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
8672 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
8673 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
8674 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008675
Paolo Bonzini36be0b92014-02-24 12:30:04 +01008676 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
8677 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
8678 vmcs_write64(GUEST_BNDCFGS, 0);
8679
Jan Kiszka44811c02013-08-04 17:17:27 +02008680 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008681 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02008682 vcpu->arch.pat = vmcs12->host_ia32_pat;
8683 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008684 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
8685 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
8686 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +01008687
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08008688 /* Set L1 segment info according to Intel SDM
8689 27.5.2 Loading Host Segment and Descriptor-Table Registers */
8690 seg = (struct kvm_segment) {
8691 .base = 0,
8692 .limit = 0xFFFFFFFF,
8693 .selector = vmcs12->host_cs_selector,
8694 .type = 11,
8695 .present = 1,
8696 .s = 1,
8697 .g = 1
8698 };
8699 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
8700 seg.l = 1;
8701 else
8702 seg.db = 1;
8703 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
8704 seg = (struct kvm_segment) {
8705 .base = 0,
8706 .limit = 0xFFFFFFFF,
8707 .type = 3,
8708 .present = 1,
8709 .s = 1,
8710 .db = 1,
8711 .g = 1
8712 };
8713 seg.selector = vmcs12->host_ds_selector;
8714 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
8715 seg.selector = vmcs12->host_es_selector;
8716 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
8717 seg.selector = vmcs12->host_ss_selector;
8718 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
8719 seg.selector = vmcs12->host_fs_selector;
8720 seg.base = vmcs12->host_fs_base;
8721 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
8722 seg.selector = vmcs12->host_gs_selector;
8723 seg.base = vmcs12->host_gs_base;
8724 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
8725 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +03008726 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08008727 .limit = 0x67,
8728 .selector = vmcs12->host_tr_selector,
8729 .type = 11,
8730 .present = 1
8731 };
8732 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
8733
Jan Kiszka503cd0c2013-03-03 13:05:44 +01008734 kvm_set_dr(vcpu, 7, 0x400);
8735 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008736}
8737
8738/*
8739 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
8740 * and modify vmcs12 to make it see what it would expect to see there if
8741 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
8742 */
Jan Kiszka533558b2014-01-04 18:47:20 +01008743static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
8744 u32 exit_intr_info,
8745 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008746{
8747 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008748 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8749
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008750 /* trying to cancel vmlaunch/vmresume is a bug */
8751 WARN_ON_ONCE(vmx->nested.nested_run_pending);
8752
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008753 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +01008754 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
8755 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008756
Wanpeng Lif3380ca2014-08-05 12:42:23 +08008757 vmx_load_vmcs01(vcpu);
8758
Bandan Das77b0f5d2014-04-19 18:17:45 -04008759 if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
8760 && nested_exit_intr_ack_set(vcpu)) {
8761 int irq = kvm_cpu_get_interrupt(vcpu);
8762 WARN_ON(irq < 0);
8763 vmcs12->vm_exit_intr_info = irq |
8764 INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR;
8765 }
8766
Jan Kiszka542060e2014-01-04 18:47:21 +01008767 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
8768 vmcs12->exit_qualification,
8769 vmcs12->idt_vectoring_info_field,
8770 vmcs12->vm_exit_intr_info,
8771 vmcs12->vm_exit_intr_error_code,
8772 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008773
Gleb Natapov2961e8762013-11-25 15:37:13 +02008774 vm_entry_controls_init(vmx, vmcs_read32(VM_ENTRY_CONTROLS));
8775 vm_exit_controls_init(vmx, vmcs_read32(VM_EXIT_CONTROLS));
Jan Kiszka36c3cc42013-02-23 22:35:37 +01008776 vmx_segment_cache_clear(vmx);
8777
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008778 /* if no vmcs02 cache requested, remove the one we used */
8779 if (VMCS02_POOL_SIZE == 0)
8780 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
8781
8782 load_vmcs12_host_state(vcpu, vmcs12);
8783
Nadav Har'El27fc51b2011-08-02 15:54:52 +03008784 /* Update TSC_OFFSET if TSC was changed while L2 ran */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008785 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
8786
8787 /* This is needed for same reason as it was needed in prepare_vmcs02 */
8788 vmx->host_rsp = 0;
8789
8790 /* Unpin physical memory we referred to in vmcs02 */
8791 if (vmx->nested.apic_access_page) {
8792 nested_release_page(vmx->nested.apic_access_page);
8793 vmx->nested.apic_access_page = 0;
8794 }
8795
8796 /*
8797 * Exiting from L2 to L1, we're now back to L1 which thinks it just
8798 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
8799 * success or failure flag accordingly.
8800 */
8801 if (unlikely(vmx->fail)) {
8802 vmx->fail = 0;
8803 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
8804 } else
8805 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +03008806 if (enable_shadow_vmcs)
8807 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008808
8809 /* in case we halted in L2 */
8810 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008811}
8812
Nadav Har'El7c177932011-05-25 23:12:04 +03008813/*
Jan Kiszka42124922014-01-04 18:47:19 +01008814 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
8815 */
8816static void vmx_leave_nested(struct kvm_vcpu *vcpu)
8817{
8818 if (is_guest_mode(vcpu))
Jan Kiszka533558b2014-01-04 18:47:20 +01008819 nested_vmx_vmexit(vcpu, -1, 0, 0);
Jan Kiszka42124922014-01-04 18:47:19 +01008820 free_nested(to_vmx(vcpu));
8821}
8822
8823/*
Nadav Har'El7c177932011-05-25 23:12:04 +03008824 * L1's failure to enter L2 is a subset of a normal exit, as explained in
8825 * 23.7 "VM-entry failures during or after loading guest state" (this also
8826 * lists the acceptable exit-reason and exit-qualification parameters).
8827 * It should only be called before L2 actually succeeded to run, and when
8828 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
8829 */
8830static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
8831 struct vmcs12 *vmcs12,
8832 u32 reason, unsigned long qualification)
8833{
8834 load_vmcs12_host_state(vcpu, vmcs12);
8835 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
8836 vmcs12->exit_qualification = qualification;
8837 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +03008838 if (enable_shadow_vmcs)
8839 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +03008840}
8841
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02008842static int vmx_check_intercept(struct kvm_vcpu *vcpu,
8843 struct x86_instruction_info *info,
8844 enum x86_intercept_stage stage)
8845{
8846 return X86EMUL_CONTINUE;
8847}
8848
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03008849static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008850 .cpu_has_kvm_support = cpu_has_kvm_support,
8851 .disabled_by_bios = vmx_disabled_by_bios,
8852 .hardware_setup = hardware_setup,
8853 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03008854 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008855 .hardware_enable = hardware_enable,
8856 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08008857 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008858
8859 .vcpu_create = vmx_create_vcpu,
8860 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03008861 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008862
Avi Kivity04d2cc72007-09-10 18:10:54 +03008863 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008864 .vcpu_load = vmx_vcpu_load,
8865 .vcpu_put = vmx_vcpu_put,
8866
Jan Kiszkac8639012012-09-21 05:42:55 +02008867 .update_db_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008868 .get_msr = vmx_get_msr,
8869 .set_msr = vmx_set_msr,
8870 .get_segment_base = vmx_get_segment_base,
8871 .get_segment = vmx_get_segment,
8872 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02008873 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008874 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02008875 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02008876 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03008877 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008878 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008879 .set_cr3 = vmx_set_cr3,
8880 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008881 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008882 .get_idt = vmx_get_idt,
8883 .set_idt = vmx_set_idt,
8884 .get_gdt = vmx_get_gdt,
8885 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01008886 .get_dr6 = vmx_get_dr6,
8887 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03008888 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01008889 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008890 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008891 .get_rflags = vmx_get_rflags,
8892 .set_rflags = vmx_set_rflags,
Avi Kivity02daab22009-12-30 12:40:26 +02008893 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008894
8895 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008896
Avi Kivity6aa8b732006-12-10 02:21:36 -08008897 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02008898 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008899 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04008900 .set_interrupt_shadow = vmx_set_interrupt_shadow,
8901 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02008902 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03008903 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008904 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02008905 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008906 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02008907 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008908 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01008909 .get_nmi_mask = vmx_get_nmi_mask,
8910 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008911 .enable_nmi_window = enable_nmi_window,
8912 .enable_irq_window = enable_irq_window,
8913 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08008914 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008915 .vm_has_apicv = vmx_vm_has_apicv,
8916 .load_eoi_exitmap = vmx_load_eoi_exitmap,
8917 .hwapic_irr_update = vmx_hwapic_irr_update,
8918 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +08008919 .sync_pir_to_irr = vmx_sync_pir_to_irr,
8920 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008921
Izik Eiduscbc94022007-10-25 00:29:55 +02008922 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08008923 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08008924 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008925
Avi Kivity586f9602010-11-18 13:09:54 +02008926 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02008927
Sheng Yang17cc3932010-01-05 19:02:27 +08008928 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08008929
8930 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08008931
8932 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00008933 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02008934
8935 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08008936
8937 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10008938
Joerg Roedel4051b182011-03-25 09:44:49 +01008939 .set_tsc_khz = vmx_set_tsc_khz,
Will Auldba904632012-11-29 12:42:50 -08008940 .read_tsc_offset = vmx_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10008941 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10008942 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01008943 .compute_tsc_offset = vmx_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03008944 .read_l1_tsc = vmx_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02008945
8946 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02008947
8948 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08008949 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008950 .mpx_supported = vmx_mpx_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008951
8952 .check_nested_events = vmx_check_nested_events,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008953};
8954
8955static int __init vmx_init(void)
8956{
Yang Zhang8d146952013-01-25 10:18:50 +08008957 int r, i, msr;
Avi Kivity26bb0982009-09-07 11:14:12 +03008958
8959 rdmsrl_safe(MSR_EFER, &host_efer);
8960
Paolo Bonzini03916db2014-07-24 14:21:57 +02008961 for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03008962 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03008963
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008964 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03008965 if (!vmx_io_bitmap_a)
8966 return -ENOMEM;
8967
Guo Chao2106a542012-06-15 11:31:56 +08008968 r = -ENOMEM;
8969
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008970 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008971 if (!vmx_io_bitmap_b)
He, Qingfdef3ad2007-04-30 09:45:24 +03008972 goto out;
He, Qingfdef3ad2007-04-30 09:45:24 +03008973
Avi Kivity58972972009-02-24 22:26:47 +02008974 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008975 if (!vmx_msr_bitmap_legacy)
Sheng Yang25c5f222008-03-28 13:18:56 +08008976 goto out1;
Guo Chao2106a542012-06-15 11:31:56 +08008977
Yang Zhang8d146952013-01-25 10:18:50 +08008978 vmx_msr_bitmap_legacy_x2apic =
8979 (unsigned long *)__get_free_page(GFP_KERNEL);
8980 if (!vmx_msr_bitmap_legacy_x2apic)
8981 goto out2;
Sheng Yang25c5f222008-03-28 13:18:56 +08008982
Avi Kivity58972972009-02-24 22:26:47 +02008983 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008984 if (!vmx_msr_bitmap_longmode)
Yang Zhang8d146952013-01-25 10:18:50 +08008985 goto out3;
Guo Chao2106a542012-06-15 11:31:56 +08008986
Yang Zhang8d146952013-01-25 10:18:50 +08008987 vmx_msr_bitmap_longmode_x2apic =
8988 (unsigned long *)__get_free_page(GFP_KERNEL);
8989 if (!vmx_msr_bitmap_longmode_x2apic)
8990 goto out4;
Abel Gordon4607c2d2013-04-18 14:35:55 +03008991 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
8992 if (!vmx_vmread_bitmap)
8993 goto out5;
8994
8995 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
8996 if (!vmx_vmwrite_bitmap)
8997 goto out6;
8998
8999 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
9000 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
Avi Kivity58972972009-02-24 22:26:47 +02009001
He, Qingfdef3ad2007-04-30 09:45:24 +03009002 /*
9003 * Allow direct access to the PC debug port (it is often used for I/O
9004 * delays, but the vmexits simply slow things down).
9005 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02009006 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
9007 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03009008
Avi Kivity3e7c73e2009-02-24 21:46:19 +02009009 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03009010
Avi Kivity58972972009-02-24 22:26:47 +02009011 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
9012 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08009013
Sheng Yang2384d2b2008-01-17 15:14:33 +08009014 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
9015
Avi Kivity0ee75be2010-04-28 15:39:01 +03009016 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
9017 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03009018 if (r)
Abel Gordon4607c2d2013-04-18 14:35:55 +03009019 goto out7;
Sheng Yang25c5f222008-03-28 13:18:56 +08009020
Zhang Yanfei8f536b72012-12-06 23:43:34 +08009021#ifdef CONFIG_KEXEC
9022 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
9023 crash_vmclear_local_loaded_vmcss);
9024#endif
9025
Avi Kivity58972972009-02-24 22:26:47 +02009026 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
9027 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
9028 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
9029 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
9030 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
9031 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
Liu, Jinsongda8999d2014-02-24 10:55:46 +00009032 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
9033
Yang Zhang8d146952013-01-25 10:18:50 +08009034 memcpy(vmx_msr_bitmap_legacy_x2apic,
9035 vmx_msr_bitmap_legacy, PAGE_SIZE);
9036 memcpy(vmx_msr_bitmap_longmode_x2apic,
9037 vmx_msr_bitmap_longmode, PAGE_SIZE);
9038
Yang Zhang01e439b2013-04-11 19:25:12 +08009039 if (enable_apicv) {
Yang Zhang8d146952013-01-25 10:18:50 +08009040 for (msr = 0x800; msr <= 0x8ff; msr++)
9041 vmx_disable_intercept_msr_read_x2apic(msr);
9042
9043 /* According SDM, in x2apic mode, the whole id reg is used.
9044 * But in KVM, it only use the highest eight bits. Need to
9045 * intercept it */
9046 vmx_enable_intercept_msr_read_x2apic(0x802);
9047 /* TMCCT */
9048 vmx_enable_intercept_msr_read_x2apic(0x839);
9049 /* TPR */
9050 vmx_disable_intercept_msr_write_x2apic(0x808);
Yang Zhangc7c9c562013-01-25 10:18:51 +08009051 /* EOI */
9052 vmx_disable_intercept_msr_write_x2apic(0x80b);
9053 /* SELF-IPI */
9054 vmx_disable_intercept_msr_write_x2apic(0x83f);
Yang Zhang8d146952013-01-25 10:18:50 +08009055 }
He, Qingfdef3ad2007-04-30 09:45:24 +03009056
Avi Kivity089d0342009-03-23 18:26:32 +02009057 if (enable_ept) {
Xudong Hao3f6d8c82012-05-22 11:23:15 +08009058 kvm_mmu_set_mask_ptes(0ull,
9059 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
9060 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
9061 0ull, VMX_EPT_EXECUTABLE_MASK);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08009062 ept_set_mmio_spte_mask();
Sheng Yang5fdbcb92008-07-16 09:25:40 +08009063 kvm_enable_tdp();
9064 } else
9065 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08009066
He, Qingfdef3ad2007-04-30 09:45:24 +03009067 return 0;
9068
Abel Gordon4607c2d2013-04-18 14:35:55 +03009069out7:
9070 free_page((unsigned long)vmx_vmwrite_bitmap);
9071out6:
9072 free_page((unsigned long)vmx_vmread_bitmap);
Yang Zhang458f2122013-04-08 15:26:33 +08009073out5:
9074 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Yang Zhang8d146952013-01-25 10:18:50 +08009075out4:
Avi Kivity58972972009-02-24 22:26:47 +02009076 free_page((unsigned long)vmx_msr_bitmap_longmode);
Yang Zhang8d146952013-01-25 10:18:50 +08009077out3:
9078 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
Sheng Yang25c5f222008-03-28 13:18:56 +08009079out2:
Avi Kivity58972972009-02-24 22:26:47 +02009080 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03009081out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02009082 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03009083out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02009084 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03009085 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08009086}
9087
9088static void __exit vmx_exit(void)
9089{
Yang Zhang8d146952013-01-25 10:18:50 +08009090 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
9091 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Avi Kivity58972972009-02-24 22:26:47 +02009092 free_page((unsigned long)vmx_msr_bitmap_legacy);
9093 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02009094 free_page((unsigned long)vmx_io_bitmap_b);
9095 free_page((unsigned long)vmx_io_bitmap_a);
Abel Gordon4607c2d2013-04-18 14:35:55 +03009096 free_page((unsigned long)vmx_vmwrite_bitmap);
9097 free_page((unsigned long)vmx_vmread_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03009098
Zhang Yanfei8f536b72012-12-06 23:43:34 +08009099#ifdef CONFIG_KEXEC
9100 rcu_assign_pointer(crash_vmclear_loaded_vmcss, NULL);
9101 synchronize_rcu();
9102#endif
9103
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08009104 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08009105}
9106
9107module_init(vmx_init)
9108module_exit(vmx_exit)