blob: c95bea17fc1ec78d7fd08f1d9e655ab209e18386 [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 Kiszkaf41245002014-03-07 20:03:13 +010034#include <linux/hrtimer.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030035#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030036#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040037
Avi Kivity6aa8b732006-12-10 02:21:36 -080038#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080039#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020040#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020041#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080042#include <asm/mce.h>
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 Kiszkaf41245002014-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;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300357
358 /* The guest-physical address of the current VMCS L1 keeps for L2 */
359 gpa_t current_vmptr;
360 /* The host-usable pointer to the above */
361 struct page *current_vmcs12_page;
362 struct vmcs12 *current_vmcs12;
Abel Gordon8de48832013-04-18 14:37:25 +0300363 struct vmcs *current_shadow_vmcs;
Abel Gordon012f83c2013-04-18 14:39:25 +0300364 /*
365 * Indicates if the shadow vmcs must be updated with the
366 * data hold by vmcs12
367 */
368 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300369
370 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
371 struct list_head vmcs02_pool;
372 int vmcs02_num;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300373 u64 vmcs01_tsc_offset;
Nadav Har'El644d7112011-05-25 23:12:35 +0300374 /* L2 must run next, and mustn't decide to exit to L1. */
375 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300376 /*
377 * Guest pages referred to in vmcs02 with host-physical pointers, so
378 * we must keep them pinned while L2 runs.
379 */
380 struct page *apic_access_page;
Nadav Har'Elb3897a42013-07-08 19:12:35 +0800381 u64 msr_ia32_feature_control;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100382
383 struct hrtimer preemption_timer;
384 bool preemption_timer_expired;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300385};
386
Yang Zhang01e439b2013-04-11 19:25:12 +0800387#define POSTED_INTR_ON 0
388/* Posted-Interrupt Descriptor */
389struct pi_desc {
390 u32 pir[8]; /* Posted interrupt requested */
391 u32 control; /* bit 0 of control is outstanding notification bit */
392 u32 rsvd[7];
393} __aligned(64);
394
Yang Zhanga20ed542013-04-11 19:25:15 +0800395static bool pi_test_and_set_on(struct pi_desc *pi_desc)
396{
397 return test_and_set_bit(POSTED_INTR_ON,
398 (unsigned long *)&pi_desc->control);
399}
400
401static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
402{
403 return test_and_clear_bit(POSTED_INTR_ON,
404 (unsigned long *)&pi_desc->control);
405}
406
407static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
408{
409 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
410}
411
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400412struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000413 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300414 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300415 u8 fail;
Avi Kivity69c73022011-03-07 15:26:44 +0200416 u8 cpl;
Avi Kivity9d58b932011-03-07 16:52:07 +0200417 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300418 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200419 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200420 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300421 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400422 int nmsrs;
423 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800424 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400425#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300426 u64 msr_host_kernel_gs_base;
427 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400428#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200429 u32 vm_entry_controls_shadow;
430 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300431 /*
432 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
433 * non-nested (L1) guest, it always points to vmcs01. For a nested
434 * guest (L2), it points to a different VMCS.
435 */
436 struct loaded_vmcs vmcs01;
437 struct loaded_vmcs *loaded_vmcs;
438 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300439 struct msr_autoload {
440 unsigned nr;
441 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
442 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
443 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400444 struct {
445 int loaded;
446 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300447#ifdef CONFIG_X86_64
448 u16 ds_sel, es_sel;
449#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200450 int gs_ldt_reload_needed;
451 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000452 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400453 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200454 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300455 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300456 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300457 struct kvm_segment segs[8];
458 } rmode;
459 struct {
460 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300461 struct kvm_save_segment {
462 u16 selector;
463 unsigned long base;
464 u32 limit;
465 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300466 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300467 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800468 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300469 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200470
471 /* Support for vnmi-less CPUs */
472 int soft_vnmi_blocked;
473 ktime_t entry_time;
474 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800475 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800476
477 bool rdtscp_enabled;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300478
Yang Zhang01e439b2013-04-11 19:25:12 +0800479 /* Posted interrupt descriptor */
480 struct pi_desc pi_desc;
481
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300482 /* Support for a guest hypervisor (nested VMX) */
483 struct nested_vmx nested;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400484};
485
Avi Kivity2fb92db2011-04-27 19:42:18 +0300486enum segment_cache_field {
487 SEG_FIELD_SEL = 0,
488 SEG_FIELD_BASE = 1,
489 SEG_FIELD_LIMIT = 2,
490 SEG_FIELD_AR = 3,
491
492 SEG_FIELD_NR = 4
493};
494
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400495static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
496{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000497 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400498}
499
Nadav Har'El22bd0352011-05-25 23:05:57 +0300500#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
501#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
502#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
503 [number##_HIGH] = VMCS12_OFFSET(name)+4
504
Abel Gordon4607c2d2013-04-18 14:35:55 +0300505
506static const unsigned long shadow_read_only_fields[] = {
507 /*
508 * We do NOT shadow fields that are modified when L0
509 * traps and emulates any vmx instruction (e.g. VMPTRLD,
510 * VMXON...) executed by L1.
511 * For example, VM_INSTRUCTION_ERROR is read
512 * by L1 if a vmx instruction fails (part of the error path).
513 * Note the code assumes this logic. If for some reason
514 * we start shadowing these fields then we need to
515 * force a shadow sync when L0 emulates vmx instructions
516 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
517 * by nested_vmx_failValid)
518 */
519 VM_EXIT_REASON,
520 VM_EXIT_INTR_INFO,
521 VM_EXIT_INSTRUCTION_LEN,
522 IDT_VECTORING_INFO_FIELD,
523 IDT_VECTORING_ERROR_CODE,
524 VM_EXIT_INTR_ERROR_CODE,
525 EXIT_QUALIFICATION,
526 GUEST_LINEAR_ADDRESS,
527 GUEST_PHYSICAL_ADDRESS
528};
529static const int max_shadow_read_only_fields =
530 ARRAY_SIZE(shadow_read_only_fields);
531
532static const unsigned long shadow_read_write_fields[] = {
533 GUEST_RIP,
534 GUEST_RSP,
535 GUEST_CR0,
536 GUEST_CR3,
537 GUEST_CR4,
538 GUEST_INTERRUPTIBILITY_INFO,
539 GUEST_RFLAGS,
540 GUEST_CS_SELECTOR,
541 GUEST_CS_AR_BYTES,
542 GUEST_CS_LIMIT,
543 GUEST_CS_BASE,
544 GUEST_ES_BASE,
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100545 GUEST_BNDCFGS,
Abel Gordon4607c2d2013-04-18 14:35:55 +0300546 CR0_GUEST_HOST_MASK,
547 CR0_READ_SHADOW,
548 CR4_READ_SHADOW,
549 TSC_OFFSET,
550 EXCEPTION_BITMAP,
551 CPU_BASED_VM_EXEC_CONTROL,
552 VM_ENTRY_EXCEPTION_ERROR_CODE,
553 VM_ENTRY_INTR_INFO_FIELD,
554 VM_ENTRY_INSTRUCTION_LEN,
555 VM_ENTRY_EXCEPTION_ERROR_CODE,
556 HOST_FS_BASE,
557 HOST_GS_BASE,
558 HOST_FS_SELECTOR,
559 HOST_GS_SELECTOR
560};
561static const int max_shadow_read_write_fields =
562 ARRAY_SIZE(shadow_read_write_fields);
563
Mathias Krause772e0312012-08-30 01:30:19 +0200564static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300565 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
566 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
567 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
568 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
569 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
570 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
571 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
572 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
573 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
574 FIELD(HOST_ES_SELECTOR, host_es_selector),
575 FIELD(HOST_CS_SELECTOR, host_cs_selector),
576 FIELD(HOST_SS_SELECTOR, host_ss_selector),
577 FIELD(HOST_DS_SELECTOR, host_ds_selector),
578 FIELD(HOST_FS_SELECTOR, host_fs_selector),
579 FIELD(HOST_GS_SELECTOR, host_gs_selector),
580 FIELD(HOST_TR_SELECTOR, host_tr_selector),
581 FIELD64(IO_BITMAP_A, io_bitmap_a),
582 FIELD64(IO_BITMAP_B, io_bitmap_b),
583 FIELD64(MSR_BITMAP, msr_bitmap),
584 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
585 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
586 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
587 FIELD64(TSC_OFFSET, tsc_offset),
588 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
589 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
590 FIELD64(EPT_POINTER, ept_pointer),
591 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
592 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
593 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
594 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
595 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
596 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
597 FIELD64(GUEST_PDPTR0, guest_pdptr0),
598 FIELD64(GUEST_PDPTR1, guest_pdptr1),
599 FIELD64(GUEST_PDPTR2, guest_pdptr2),
600 FIELD64(GUEST_PDPTR3, guest_pdptr3),
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100601 FIELD64(GUEST_BNDCFGS, guest_bndcfgs),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300602 FIELD64(HOST_IA32_PAT, host_ia32_pat),
603 FIELD64(HOST_IA32_EFER, host_ia32_efer),
604 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
605 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
606 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
607 FIELD(EXCEPTION_BITMAP, exception_bitmap),
608 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
609 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
610 FIELD(CR3_TARGET_COUNT, cr3_target_count),
611 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
612 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
613 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
614 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
615 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
616 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
617 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
618 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
619 FIELD(TPR_THRESHOLD, tpr_threshold),
620 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
621 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
622 FIELD(VM_EXIT_REASON, vm_exit_reason),
623 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
624 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
625 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
626 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
627 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
628 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
629 FIELD(GUEST_ES_LIMIT, guest_es_limit),
630 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
631 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
632 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
633 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
634 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
635 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
636 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
637 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
638 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
639 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
640 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
641 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
642 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
643 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
644 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
645 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
646 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
647 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
648 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
649 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
650 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100651 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300652 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
653 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
654 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
655 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
656 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
657 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
658 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
659 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
660 FIELD(EXIT_QUALIFICATION, exit_qualification),
661 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
662 FIELD(GUEST_CR0, guest_cr0),
663 FIELD(GUEST_CR3, guest_cr3),
664 FIELD(GUEST_CR4, guest_cr4),
665 FIELD(GUEST_ES_BASE, guest_es_base),
666 FIELD(GUEST_CS_BASE, guest_cs_base),
667 FIELD(GUEST_SS_BASE, guest_ss_base),
668 FIELD(GUEST_DS_BASE, guest_ds_base),
669 FIELD(GUEST_FS_BASE, guest_fs_base),
670 FIELD(GUEST_GS_BASE, guest_gs_base),
671 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
672 FIELD(GUEST_TR_BASE, guest_tr_base),
673 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
674 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
675 FIELD(GUEST_DR7, guest_dr7),
676 FIELD(GUEST_RSP, guest_rsp),
677 FIELD(GUEST_RIP, guest_rip),
678 FIELD(GUEST_RFLAGS, guest_rflags),
679 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
680 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
681 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
682 FIELD(HOST_CR0, host_cr0),
683 FIELD(HOST_CR3, host_cr3),
684 FIELD(HOST_CR4, host_cr4),
685 FIELD(HOST_FS_BASE, host_fs_base),
686 FIELD(HOST_GS_BASE, host_gs_base),
687 FIELD(HOST_TR_BASE, host_tr_base),
688 FIELD(HOST_GDTR_BASE, host_gdtr_base),
689 FIELD(HOST_IDTR_BASE, host_idtr_base),
690 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
691 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
692 FIELD(HOST_RSP, host_rsp),
693 FIELD(HOST_RIP, host_rip),
694};
695static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table);
696
697static inline short vmcs_field_to_offset(unsigned long field)
698{
699 if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0)
700 return -1;
701 return vmcs_field_to_offset_table[field];
702}
703
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300704static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
705{
706 return to_vmx(vcpu)->nested.current_vmcs12;
707}
708
709static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
710{
711 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800712 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300713 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800714
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300715 return page;
716}
717
718static void nested_release_page(struct page *page)
719{
720 kvm_release_page_dirty(page);
721}
722
723static void nested_release_page_clean(struct page *page)
724{
725 kvm_release_page_clean(page);
726}
727
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300728static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800729static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800730static void kvm_cpu_vmxon(u64 addr);
731static void kvm_cpu_vmxoff(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200732static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300733static void vmx_set_segment(struct kvm_vcpu *vcpu,
734 struct kvm_segment *var, int seg);
735static void vmx_get_segment(struct kvm_vcpu *vcpu,
736 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200737static bool guest_state_valid(struct kvm_vcpu *vcpu);
738static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yang Zhanga20ed542013-04-11 19:25:15 +0800739static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu);
Abel Gordonc3114422013-04-18 14:38:55 +0300740static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300741static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Paolo Bonzini36be0b92014-02-24 12:30:04 +0100742static bool vmx_mpx_supported(void);
Avi Kivity75880a02007-06-20 11:20:04 +0300743
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744static DEFINE_PER_CPU(struct vmcs *, vmxarea);
745static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300746/*
747 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
748 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
749 */
750static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300751static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800752
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200753static unsigned long *vmx_io_bitmap_a;
754static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200755static unsigned long *vmx_msr_bitmap_legacy;
756static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800757static unsigned long *vmx_msr_bitmap_legacy_x2apic;
758static unsigned long *vmx_msr_bitmap_longmode_x2apic;
Abel Gordon4607c2d2013-04-18 14:35:55 +0300759static unsigned long *vmx_vmread_bitmap;
760static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300761
Avi Kivity110312c2010-12-21 12:54:20 +0200762static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200763static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200764
Sheng Yang2384d2b2008-01-17 15:14:33 +0800765static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
766static DEFINE_SPINLOCK(vmx_vpid_lock);
767
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300768static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769 int size;
770 int order;
771 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300772 u32 pin_based_exec_ctrl;
773 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800774 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300775 u32 vmexit_ctrl;
776 u32 vmentry_ctrl;
777} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778
Hannes Ederefff9e52008-11-28 17:02:06 +0100779static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800780 u32 ept;
781 u32 vpid;
782} vmx_capability;
783
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784#define VMX_SEGMENT_FIELD(seg) \
785 [VCPU_SREG_##seg] = { \
786 .selector = GUEST_##seg##_SELECTOR, \
787 .base = GUEST_##seg##_BASE, \
788 .limit = GUEST_##seg##_LIMIT, \
789 .ar_bytes = GUEST_##seg##_AR_BYTES, \
790 }
791
Mathias Krause772e0312012-08-30 01:30:19 +0200792static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793 unsigned selector;
794 unsigned base;
795 unsigned limit;
796 unsigned ar_bytes;
797} kvm_vmx_segment_fields[] = {
798 VMX_SEGMENT_FIELD(CS),
799 VMX_SEGMENT_FIELD(DS),
800 VMX_SEGMENT_FIELD(ES),
801 VMX_SEGMENT_FIELD(FS),
802 VMX_SEGMENT_FIELD(GS),
803 VMX_SEGMENT_FIELD(SS),
804 VMX_SEGMENT_FIELD(TR),
805 VMX_SEGMENT_FIELD(LDTR),
806};
807
Avi Kivity26bb0982009-09-07 11:14:12 +0300808static u64 host_efer;
809
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300810static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
811
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300812/*
Brian Gerst8c065852010-07-17 09:03:26 -0400813 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300814 * away by decrementing the array size.
815 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800817#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300818 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400820 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200822#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823
Gui Jianfeng31299942010-03-15 17:29:09 +0800824static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825{
826 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
827 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100828 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829}
830
Gui Jianfeng31299942010-03-15 17:29:09 +0800831static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300832{
833 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
834 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100835 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300836}
837
Gui Jianfeng31299942010-03-15 17:29:09 +0800838static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500839{
840 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
841 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100842 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500843}
844
Gui Jianfeng31299942010-03-15 17:29:09 +0800845static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846{
847 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
848 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
849}
850
Gui Jianfeng31299942010-03-15 17:29:09 +0800851static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800852{
853 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
854 INTR_INFO_VALID_MASK)) ==
855 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
856}
857
Gui Jianfeng31299942010-03-15 17:29:09 +0800858static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800859{
Sheng Yang04547152009-04-01 15:52:31 +0800860 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800861}
862
Gui Jianfeng31299942010-03-15 17:29:09 +0800863static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800864{
Sheng Yang04547152009-04-01 15:52:31 +0800865 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800866}
867
Gui Jianfeng31299942010-03-15 17:29:09 +0800868static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800869{
Sheng Yang04547152009-04-01 15:52:31 +0800870 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800871}
872
Gui Jianfeng31299942010-03-15 17:29:09 +0800873static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800874{
Sheng Yang04547152009-04-01 15:52:31 +0800875 return vmcs_config.cpu_based_exec_ctrl &
876 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800877}
878
Avi Kivity774ead32007-12-26 13:57:04 +0200879static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800880{
Sheng Yang04547152009-04-01 15:52:31 +0800881 return vmcs_config.cpu_based_2nd_exec_ctrl &
882 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
883}
884
Yang Zhang8d146952013-01-25 10:18:50 +0800885static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
886{
887 return vmcs_config.cpu_based_2nd_exec_ctrl &
888 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
889}
890
Yang Zhang83d4c282013-01-25 10:18:49 +0800891static inline bool cpu_has_vmx_apic_register_virt(void)
892{
893 return vmcs_config.cpu_based_2nd_exec_ctrl &
894 SECONDARY_EXEC_APIC_REGISTER_VIRT;
895}
896
Yang Zhangc7c9c562013-01-25 10:18:51 +0800897static inline bool cpu_has_vmx_virtual_intr_delivery(void)
898{
899 return vmcs_config.cpu_based_2nd_exec_ctrl &
900 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
901}
902
Yang Zhang01e439b2013-04-11 19:25:12 +0800903static inline bool cpu_has_vmx_posted_intr(void)
904{
905 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
906}
907
908static inline bool cpu_has_vmx_apicv(void)
909{
910 return cpu_has_vmx_apic_register_virt() &&
911 cpu_has_vmx_virtual_intr_delivery() &&
912 cpu_has_vmx_posted_intr();
913}
914
Sheng Yang04547152009-04-01 15:52:31 +0800915static inline bool cpu_has_vmx_flexpriority(void)
916{
917 return cpu_has_vmx_tpr_shadow() &&
918 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800919}
920
Marcelo Tosattie7997942009-06-11 12:07:40 -0300921static inline bool cpu_has_vmx_ept_execute_only(void)
922{
Gui Jianfeng31299942010-03-15 17:29:09 +0800923 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300924}
925
926static inline bool cpu_has_vmx_eptp_uncacheable(void)
927{
Gui Jianfeng31299942010-03-15 17:29:09 +0800928 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300929}
930
931static inline bool cpu_has_vmx_eptp_writeback(void)
932{
Gui Jianfeng31299942010-03-15 17:29:09 +0800933 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300934}
935
936static inline bool cpu_has_vmx_ept_2m_page(void)
937{
Gui Jianfeng31299942010-03-15 17:29:09 +0800938 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300939}
940
Sheng Yang878403b2010-01-05 19:02:29 +0800941static inline bool cpu_has_vmx_ept_1g_page(void)
942{
Gui Jianfeng31299942010-03-15 17:29:09 +0800943 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800944}
945
Sheng Yang4bc9b982010-06-02 14:05:24 +0800946static inline bool cpu_has_vmx_ept_4levels(void)
947{
948 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
949}
950
Xudong Hao83c3a332012-05-28 19:33:35 +0800951static inline bool cpu_has_vmx_ept_ad_bits(void)
952{
953 return vmx_capability.ept & VMX_EPT_AD_BIT;
954}
955
Gui Jianfeng31299942010-03-15 17:29:09 +0800956static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800957{
Gui Jianfeng31299942010-03-15 17:29:09 +0800958 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800959}
960
Gui Jianfeng31299942010-03-15 17:29:09 +0800961static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800962{
Gui Jianfeng31299942010-03-15 17:29:09 +0800963 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800964}
965
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800966static inline bool cpu_has_vmx_invvpid_single(void)
967{
968 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
969}
970
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800971static inline bool cpu_has_vmx_invvpid_global(void)
972{
973 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
974}
975
Gui Jianfeng31299942010-03-15 17:29:09 +0800976static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800977{
Sheng Yang04547152009-04-01 15:52:31 +0800978 return vmcs_config.cpu_based_2nd_exec_ctrl &
979 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800980}
981
Gui Jianfeng31299942010-03-15 17:29:09 +0800982static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700983{
984 return vmcs_config.cpu_based_2nd_exec_ctrl &
985 SECONDARY_EXEC_UNRESTRICTED_GUEST;
986}
987
Gui Jianfeng31299942010-03-15 17:29:09 +0800988static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800989{
990 return vmcs_config.cpu_based_2nd_exec_ctrl &
991 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
992}
993
Gui Jianfeng31299942010-03-15 17:29:09 +0800994static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800995{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800996 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800997}
998
Gui Jianfeng31299942010-03-15 17:29:09 +0800999static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001000{
Sheng Yang04547152009-04-01 15:52:31 +08001001 return vmcs_config.cpu_based_2nd_exec_ctrl &
1002 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001003}
1004
Gui Jianfeng31299942010-03-15 17:29:09 +08001005static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001006{
1007 return vmcs_config.cpu_based_2nd_exec_ctrl &
1008 SECONDARY_EXEC_RDTSCP;
1009}
1010
Mao, Junjiead756a12012-07-02 01:18:48 +00001011static inline bool cpu_has_vmx_invpcid(void)
1012{
1013 return vmcs_config.cpu_based_2nd_exec_ctrl &
1014 SECONDARY_EXEC_ENABLE_INVPCID;
1015}
1016
Gui Jianfeng31299942010-03-15 17:29:09 +08001017static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001018{
1019 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1020}
1021
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001022static inline bool cpu_has_vmx_wbinvd_exit(void)
1023{
1024 return vmcs_config.cpu_based_2nd_exec_ctrl &
1025 SECONDARY_EXEC_WBINVD_EXITING;
1026}
1027
Abel Gordonabc4fc52013-04-18 14:35:25 +03001028static inline bool cpu_has_vmx_shadow_vmcs(void)
1029{
1030 u64 vmx_msr;
1031 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1032 /* check if the cpu supports writing r/o exit information fields */
1033 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1034 return false;
1035
1036 return vmcs_config.cpu_based_2nd_exec_ctrl &
1037 SECONDARY_EXEC_SHADOW_VMCS;
1038}
1039
Sheng Yang04547152009-04-01 15:52:31 +08001040static inline bool report_flexpriority(void)
1041{
1042 return flexpriority_enabled;
1043}
1044
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001045static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1046{
1047 return vmcs12->cpu_based_vm_exec_control & bit;
1048}
1049
1050static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1051{
1052 return (vmcs12->cpu_based_vm_exec_control &
1053 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1054 (vmcs12->secondary_vm_exec_control & bit);
1055}
1056
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001057static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001058{
1059 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1060}
1061
Jan Kiszkaf41245002014-03-07 20:03:13 +01001062static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1063{
1064 return vmcs12->pin_based_vm_exec_control &
1065 PIN_BASED_VMX_PREEMPTION_TIMER;
1066}
1067
Nadav Har'El155a97a2013-08-05 11:07:16 +03001068static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1069{
1070 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1071}
1072
Nadav Har'El644d7112011-05-25 23:12:35 +03001073static inline bool is_exception(u32 intr_info)
1074{
1075 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1076 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
1077}
1078
Jan Kiszka533558b2014-01-04 18:47:20 +01001079static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1080 u32 exit_intr_info,
1081 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001082static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1083 struct vmcs12 *vmcs12,
1084 u32 reason, unsigned long qualification);
1085
Rusty Russell8b9cf982007-07-30 16:31:43 +10001086static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001087{
1088 int i;
1089
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001090 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001091 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001092 return i;
1093 return -1;
1094}
1095
Sheng Yang2384d2b2008-01-17 15:14:33 +08001096static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1097{
1098 struct {
1099 u64 vpid : 16;
1100 u64 rsvd : 48;
1101 u64 gva;
1102 } operand = { vpid, 0, gva };
1103
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001104 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001105 /* CF==1 or ZF==1 --> rc = -1 */
1106 "; ja 1f ; ud2 ; 1:"
1107 : : "a"(&operand), "c"(ext) : "cc", "memory");
1108}
1109
Sheng Yang14394422008-04-28 12:24:45 +08001110static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1111{
1112 struct {
1113 u64 eptp, gpa;
1114 } operand = {eptp, gpa};
1115
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001116 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001117 /* CF==1 or ZF==1 --> rc = -1 */
1118 "; ja 1f ; ud2 ; 1:\n"
1119 : : "a" (&operand), "c" (ext) : "cc", "memory");
1120}
1121
Avi Kivity26bb0982009-09-07 11:14:12 +03001122static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001123{
1124 int i;
1125
Rusty Russell8b9cf982007-07-30 16:31:43 +10001126 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001127 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001128 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001129 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001130}
1131
Avi Kivity6aa8b732006-12-10 02:21:36 -08001132static void vmcs_clear(struct vmcs *vmcs)
1133{
1134 u64 phys_addr = __pa(vmcs);
1135 u8 error;
1136
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001137 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001138 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139 : "cc", "memory");
1140 if (error)
1141 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1142 vmcs, phys_addr);
1143}
1144
Nadav Har'Eld462b812011-05-24 15:26:10 +03001145static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1146{
1147 vmcs_clear(loaded_vmcs->vmcs);
1148 loaded_vmcs->cpu = -1;
1149 loaded_vmcs->launched = 0;
1150}
1151
Dongxiao Xu7725b892010-05-11 18:29:38 +08001152static void vmcs_load(struct vmcs *vmcs)
1153{
1154 u64 phys_addr = __pa(vmcs);
1155 u8 error;
1156
1157 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001158 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001159 : "cc", "memory");
1160 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001161 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001162 vmcs, phys_addr);
1163}
1164
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001165#ifdef CONFIG_KEXEC
1166/*
1167 * This bitmap is used to indicate whether the vmclear
1168 * operation is enabled on all cpus. All disabled by
1169 * default.
1170 */
1171static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1172
1173static inline void crash_enable_local_vmclear(int cpu)
1174{
1175 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1176}
1177
1178static inline void crash_disable_local_vmclear(int cpu)
1179{
1180 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1181}
1182
1183static inline int crash_local_vmclear_enabled(int cpu)
1184{
1185 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1186}
1187
1188static void crash_vmclear_local_loaded_vmcss(void)
1189{
1190 int cpu = raw_smp_processor_id();
1191 struct loaded_vmcs *v;
1192
1193 if (!crash_local_vmclear_enabled(cpu))
1194 return;
1195
1196 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1197 loaded_vmcss_on_cpu_link)
1198 vmcs_clear(v->vmcs);
1199}
1200#else
1201static inline void crash_enable_local_vmclear(int cpu) { }
1202static inline void crash_disable_local_vmclear(int cpu) { }
1203#endif /* CONFIG_KEXEC */
1204
Nadav Har'Eld462b812011-05-24 15:26:10 +03001205static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001206{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001207 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001208 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001209
Nadav Har'Eld462b812011-05-24 15:26:10 +03001210 if (loaded_vmcs->cpu != cpu)
1211 return; /* vcpu migration can race with cpu offline */
1212 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001214 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001215 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001216
1217 /*
1218 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1219 * is before setting loaded_vmcs->vcpu to -1 which is done in
1220 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1221 * then adds the vmcs into percpu list before it is deleted.
1222 */
1223 smp_wmb();
1224
Nadav Har'Eld462b812011-05-24 15:26:10 +03001225 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001226 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227}
1228
Nadav Har'Eld462b812011-05-24 15:26:10 +03001229static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001230{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001231 int cpu = loaded_vmcs->cpu;
1232
1233 if (cpu != -1)
1234 smp_call_function_single(cpu,
1235 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001236}
1237
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001238static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001239{
1240 if (vmx->vpid == 0)
1241 return;
1242
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001243 if (cpu_has_vmx_invvpid_single())
1244 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001245}
1246
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001247static inline void vpid_sync_vcpu_global(void)
1248{
1249 if (cpu_has_vmx_invvpid_global())
1250 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1251}
1252
1253static inline void vpid_sync_context(struct vcpu_vmx *vmx)
1254{
1255 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001256 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001257 else
1258 vpid_sync_vcpu_global();
1259}
1260
Sheng Yang14394422008-04-28 12:24:45 +08001261static inline void ept_sync_global(void)
1262{
1263 if (cpu_has_vmx_invept_global())
1264 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1265}
1266
1267static inline void ept_sync_context(u64 eptp)
1268{
Avi Kivity089d0342009-03-23 18:26:32 +02001269 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001270 if (cpu_has_vmx_invept_context())
1271 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1272 else
1273 ept_sync_global();
1274 }
1275}
1276
Avi Kivity96304212011-05-15 10:13:13 -04001277static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278{
Avi Kivity5e520e62011-05-15 10:13:12 -04001279 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280
Avi Kivity5e520e62011-05-15 10:13:12 -04001281 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1282 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283 return value;
1284}
1285
Avi Kivity96304212011-05-15 10:13:13 -04001286static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001287{
1288 return vmcs_readl(field);
1289}
1290
Avi Kivity96304212011-05-15 10:13:13 -04001291static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292{
1293 return vmcs_readl(field);
1294}
1295
Avi Kivity96304212011-05-15 10:13:13 -04001296static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297{
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001298#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299 return vmcs_readl(field);
1300#else
1301 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
1302#endif
1303}
1304
Avi Kivitye52de1b2007-01-05 16:36:56 -08001305static noinline void vmwrite_error(unsigned long field, unsigned long value)
1306{
1307 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1308 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1309 dump_stack();
1310}
1311
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312static void vmcs_writel(unsigned long field, unsigned long value)
1313{
1314 u8 error;
1315
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001316 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001317 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001318 if (unlikely(error))
1319 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320}
1321
1322static void vmcs_write16(unsigned long field, u16 value)
1323{
1324 vmcs_writel(field, value);
1325}
1326
1327static void vmcs_write32(unsigned long field, u32 value)
1328{
1329 vmcs_writel(field, value);
1330}
1331
1332static void vmcs_write64(unsigned long field, u64 value)
1333{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001334 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001335#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 asm volatile ("");
1337 vmcs_writel(field+1, value >> 32);
1338#endif
1339}
1340
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001341static void vmcs_clear_bits(unsigned long field, u32 mask)
1342{
1343 vmcs_writel(field, vmcs_readl(field) & ~mask);
1344}
1345
1346static void vmcs_set_bits(unsigned long field, u32 mask)
1347{
1348 vmcs_writel(field, vmcs_readl(field) | mask);
1349}
1350
Gleb Natapov2961e8762013-11-25 15:37:13 +02001351static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1352{
1353 vmcs_write32(VM_ENTRY_CONTROLS, val);
1354 vmx->vm_entry_controls_shadow = val;
1355}
1356
1357static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1358{
1359 if (vmx->vm_entry_controls_shadow != val)
1360 vm_entry_controls_init(vmx, val);
1361}
1362
1363static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1364{
1365 return vmx->vm_entry_controls_shadow;
1366}
1367
1368
1369static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1370{
1371 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1372}
1373
1374static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1375{
1376 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val);
1377}
1378
1379static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1380{
1381 vmcs_write32(VM_EXIT_CONTROLS, val);
1382 vmx->vm_exit_controls_shadow = val;
1383}
1384
1385static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1386{
1387 if (vmx->vm_exit_controls_shadow != val)
1388 vm_exit_controls_init(vmx, val);
1389}
1390
1391static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1392{
1393 return vmx->vm_exit_controls_shadow;
1394}
1395
1396
1397static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1398{
1399 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1400}
1401
1402static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1403{
1404 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1405}
1406
Avi Kivity2fb92db2011-04-27 19:42:18 +03001407static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1408{
1409 vmx->segment_cache.bitmask = 0;
1410}
1411
1412static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1413 unsigned field)
1414{
1415 bool ret;
1416 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1417
1418 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1419 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1420 vmx->segment_cache.bitmask = 0;
1421 }
1422 ret = vmx->segment_cache.bitmask & mask;
1423 vmx->segment_cache.bitmask |= mask;
1424 return ret;
1425}
1426
1427static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1428{
1429 u16 *p = &vmx->segment_cache.seg[seg].selector;
1430
1431 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1432 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1433 return *p;
1434}
1435
1436static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1437{
1438 ulong *p = &vmx->segment_cache.seg[seg].base;
1439
1440 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1441 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1442 return *p;
1443}
1444
1445static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1446{
1447 u32 *p = &vmx->segment_cache.seg[seg].limit;
1448
1449 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1450 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1451 return *p;
1452}
1453
1454static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1455{
1456 u32 *p = &vmx->segment_cache.seg[seg].ar;
1457
1458 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1459 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1460 return *p;
1461}
1462
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001463static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1464{
1465 u32 eb;
1466
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001467 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1468 (1u << NM_VECTOR) | (1u << DB_VECTOR);
1469 if ((vcpu->guest_debug &
1470 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1471 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1472 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001473 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001474 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001475 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001476 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001477 if (vcpu->fpu_active)
1478 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001479
1480 /* When we are running a nested L2 guest and L1 specified for it a
1481 * certain exception bitmap, we must trap the same exceptions and pass
1482 * them to L1. When running L2, we will only handle the exceptions
1483 * specified above if L1 did not want them.
1484 */
1485 if (is_guest_mode(vcpu))
1486 eb |= get_vmcs12(vcpu)->exception_bitmap;
1487
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001488 vmcs_write32(EXCEPTION_BITMAP, eb);
1489}
1490
Gleb Natapov2961e8762013-11-25 15:37:13 +02001491static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1492 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001493{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001494 vm_entry_controls_clearbit(vmx, entry);
1495 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001496}
1497
Avi Kivity61d2ef22010-04-28 16:40:38 +03001498static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1499{
1500 unsigned i;
1501 struct msr_autoload *m = &vmx->msr_autoload;
1502
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001503 switch (msr) {
1504 case MSR_EFER:
1505 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001506 clear_atomic_switch_msr_special(vmx,
1507 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001508 VM_EXIT_LOAD_IA32_EFER);
1509 return;
1510 }
1511 break;
1512 case MSR_CORE_PERF_GLOBAL_CTRL:
1513 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001514 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001515 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1516 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1517 return;
1518 }
1519 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001520 }
1521
Avi Kivity61d2ef22010-04-28 16:40:38 +03001522 for (i = 0; i < m->nr; ++i)
1523 if (m->guest[i].index == msr)
1524 break;
1525
1526 if (i == m->nr)
1527 return;
1528 --m->nr;
1529 m->guest[i] = m->guest[m->nr];
1530 m->host[i] = m->host[m->nr];
1531 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1532 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1533}
1534
Gleb Natapov2961e8762013-11-25 15:37:13 +02001535static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1536 unsigned long entry, unsigned long exit,
1537 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1538 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001539{
1540 vmcs_write64(guest_val_vmcs, guest_val);
1541 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001542 vm_entry_controls_setbit(vmx, entry);
1543 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001544}
1545
Avi Kivity61d2ef22010-04-28 16:40:38 +03001546static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1547 u64 guest_val, u64 host_val)
1548{
1549 unsigned i;
1550 struct msr_autoload *m = &vmx->msr_autoload;
1551
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001552 switch (msr) {
1553 case MSR_EFER:
1554 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001555 add_atomic_switch_msr_special(vmx,
1556 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001557 VM_EXIT_LOAD_IA32_EFER,
1558 GUEST_IA32_EFER,
1559 HOST_IA32_EFER,
1560 guest_val, host_val);
1561 return;
1562 }
1563 break;
1564 case MSR_CORE_PERF_GLOBAL_CTRL:
1565 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001566 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001567 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1568 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1569 GUEST_IA32_PERF_GLOBAL_CTRL,
1570 HOST_IA32_PERF_GLOBAL_CTRL,
1571 guest_val, host_val);
1572 return;
1573 }
1574 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001575 }
1576
Avi Kivity61d2ef22010-04-28 16:40:38 +03001577 for (i = 0; i < m->nr; ++i)
1578 if (m->guest[i].index == msr)
1579 break;
1580
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001581 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001582 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001583 "Can't add msr %x\n", msr);
1584 return;
1585 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001586 ++m->nr;
1587 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1588 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1589 }
1590
1591 m->guest[i].index = msr;
1592 m->guest[i].value = guest_val;
1593 m->host[i].index = msr;
1594 m->host[i].value = host_val;
1595}
1596
Avi Kivity33ed6322007-05-02 16:54:03 +03001597static void reload_tss(void)
1598{
Avi Kivity33ed6322007-05-02 16:54:03 +03001599 /*
1600 * VT restores TR but not its size. Useless.
1601 */
Avi Kivityd3591922010-07-26 18:32:39 +03001602 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001603 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001604
Avi Kivityd3591922010-07-26 18:32:39 +03001605 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001606 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1607 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001608}
1609
Avi Kivity92c0d902009-10-29 11:00:16 +02001610static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001611{
Roel Kluin3a34a882009-08-04 02:08:45 -07001612 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001613 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +03001614
Avi Kivityf6801df2010-01-21 15:31:50 +02001615 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -07001616
Avi Kivity51c6cf62007-08-29 03:48:05 +03001617 /*
Guo Chao0fa06072012-06-28 15:16:19 +08001618 * NX is emulated; LMA and LME handled by hardware; SCE meaningless
Avi Kivity51c6cf62007-08-29 03:48:05 +03001619 * outside long mode
1620 */
1621 ignore_bits = EFER_NX | EFER_SCE;
1622#ifdef CONFIG_X86_64
1623 ignore_bits |= EFER_LMA | EFER_LME;
1624 /* SCE is meaningful only in long mode on Intel */
1625 if (guest_efer & EFER_LMA)
1626 ignore_bits &= ~(u64)EFER_SCE;
1627#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +03001628 guest_efer &= ~ignore_bits;
1629 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +03001630 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +02001631 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03001632
1633 clear_atomic_switch_msr(vmx, MSR_EFER);
1634 /* On ept, can't emulate nx, and must switch nx atomically */
1635 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
1636 guest_efer = vmx->vcpu.arch.efer;
1637 if (!(guest_efer & EFER_LMA))
1638 guest_efer &= ~EFER_LME;
1639 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
1640 return false;
1641 }
1642
Avi Kivity26bb0982009-09-07 11:14:12 +03001643 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001644}
1645
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001646static unsigned long segment_base(u16 selector)
1647{
Avi Kivityd3591922010-07-26 18:32:39 +03001648 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001649 struct desc_struct *d;
1650 unsigned long table_base;
1651 unsigned long v;
1652
1653 if (!(selector & ~3))
1654 return 0;
1655
Avi Kivityd3591922010-07-26 18:32:39 +03001656 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001657
1658 if (selector & 4) { /* from ldt */
1659 u16 ldt_selector = kvm_read_ldt();
1660
1661 if (!(ldt_selector & ~3))
1662 return 0;
1663
1664 table_base = segment_base(ldt_selector);
1665 }
1666 d = (struct desc_struct *)(table_base + (selector & ~7));
1667 v = get_desc_base(d);
1668#ifdef CONFIG_X86_64
1669 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
1670 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
1671#endif
1672 return v;
1673}
1674
1675static inline unsigned long kvm_read_tr_base(void)
1676{
1677 u16 tr;
1678 asm("str %0" : "=g"(tr));
1679 return segment_base(tr);
1680}
1681
Avi Kivity04d2cc72007-09-10 18:10:54 +03001682static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001683{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001684 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001685 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001686
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001687 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001688 return;
1689
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001690 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001691 /*
1692 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1693 * allow segment selectors with cpl > 0 or ti == 1.
1694 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001695 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02001696 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02001697 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001698 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001699 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001700 vmx->host_state.fs_reload_needed = 0;
1701 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03001702 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001703 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001704 }
Avi Kivity9581d442010-10-19 16:46:55 +02001705 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001706 if (!(vmx->host_state.gs_sel & 7))
1707 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001708 else {
1709 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001710 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001711 }
1712
1713#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03001714 savesegment(ds, vmx->host_state.ds_sel);
1715 savesegment(es, vmx->host_state.es_sel);
1716#endif
1717
1718#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03001719 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1720 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1721#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001722 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1723 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03001724#endif
Avi Kivity707c0872007-05-02 17:33:43 +03001725
1726#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001727 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1728 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03001729 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03001730#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001731 if (boot_cpu_has(X86_FEATURE_MPX))
1732 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03001733 for (i = 0; i < vmx->save_nmsrs; ++i)
1734 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02001735 vmx->guest_msrs[i].data,
1736 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03001737}
1738
Avi Kivitya9b21b62008-06-24 11:48:49 +03001739static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001740{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001741 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001742 return;
1743
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001744 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001745 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02001746#ifdef CONFIG_X86_64
1747 if (is_long_mode(&vmx->vcpu))
1748 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1749#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001750 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001751 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001752#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02001753 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001754#else
1755 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001756#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001757 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02001758 if (vmx->host_state.fs_reload_needed)
1759 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001760#ifdef CONFIG_X86_64
1761 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
1762 loadsegment(ds, vmx->host_state.ds_sel);
1763 loadsegment(es, vmx->host_state.es_sel);
1764 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001765#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001766 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001767#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001768 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001769#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001770 if (vmx->host_state.msr_host_bndcfgs)
1771 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07001772 /*
1773 * If the FPU is not active (through the host task or
1774 * the guest vcpu), then restore the cr0.TS bit.
1775 */
1776 if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded)
1777 stts();
Avi Kivity3444d7d2010-07-26 18:32:38 +03001778 load_gdt(&__get_cpu_var(host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03001779}
1780
Avi Kivitya9b21b62008-06-24 11:48:49 +03001781static void vmx_load_host_state(struct vcpu_vmx *vmx)
1782{
1783 preempt_disable();
1784 __vmx_load_host_state(vmx);
1785 preempt_enable();
1786}
1787
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788/*
1789 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1790 * vcpu mutex is already taken.
1791 */
Avi Kivity15ad7142007-07-11 18:17:21 +03001792static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001794 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001795 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001796
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001797 if (!vmm_exclusive)
1798 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001799 else if (vmx->loaded_vmcs->cpu != cpu)
1800 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001801
Nadav Har'Eld462b812011-05-24 15:26:10 +03001802 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1803 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1804 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001805 }
1806
Nadav Har'Eld462b812011-05-24 15:26:10 +03001807 if (vmx->loaded_vmcs->cpu != cpu) {
Avi Kivityd3591922010-07-26 18:32:39 +03001808 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809 unsigned long sysenter_esp;
1810
Avi Kivitya8eeb042010-05-10 12:34:53 +03001811 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001812 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001813 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001814
1815 /*
1816 * Read loaded_vmcs->cpu should be before fetching
1817 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1818 * See the comments in __loaded_vmcs_clear().
1819 */
1820 smp_rmb();
1821
Nadav Har'Eld462b812011-05-24 15:26:10 +03001822 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1823 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001824 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001825 local_irq_enable();
1826
Avi Kivity6aa8b732006-12-10 02:21:36 -08001827 /*
1828 * Linux uses per-cpu TSS and GDT, so set these when switching
1829 * processors.
1830 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001831 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03001832 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001833
1834 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1835 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Nadav Har'Eld462b812011-05-24 15:26:10 +03001836 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001837 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838}
1839
1840static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1841{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001842 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001843 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001844 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1845 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001846 kvm_cpu_vmxoff();
1847 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001848}
1849
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001850static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1851{
Avi Kivity81231c62010-01-24 16:26:40 +02001852 ulong cr0;
1853
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001854 if (vcpu->fpu_active)
1855 return;
1856 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001857 cr0 = vmcs_readl(GUEST_CR0);
1858 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1859 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1860 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001861 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001862 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001863 if (is_guest_mode(vcpu))
1864 vcpu->arch.cr0_guest_owned_bits &=
1865 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02001866 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001867}
1868
Avi Kivityedcafe32009-12-30 18:07:40 +02001869static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1870
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001871/*
1872 * Return the cr0 value that a nested guest would read. This is a combination
1873 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1874 * its hypervisor (cr0_read_shadow).
1875 */
1876static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
1877{
1878 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
1879 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
1880}
1881static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
1882{
1883 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
1884 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
1885}
1886
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001887static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1888{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001889 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
1890 * set this *before* calling this function.
1891 */
Avi Kivityedcafe32009-12-30 18:07:40 +02001892 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001893 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001894 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001895 vcpu->arch.cr0_guest_owned_bits = 0;
1896 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001897 if (is_guest_mode(vcpu)) {
1898 /*
1899 * L1's specified read shadow might not contain the TS bit,
1900 * so now that we turned on shadowing of this bit, we need to
1901 * set this bit of the shadow. Like in nested_vmx_run we need
1902 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
1903 * up-to-date here because we just decached cr0.TS (and we'll
1904 * only update vmcs12->guest_cr0 on nested exit).
1905 */
1906 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1907 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
1908 (vcpu->arch.cr0 & X86_CR0_TS);
1909 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
1910 } else
1911 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001912}
1913
Avi Kivity6aa8b732006-12-10 02:21:36 -08001914static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1915{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001916 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001917
Avi Kivity6de12732011-03-07 12:51:22 +02001918 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1919 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1920 rflags = vmcs_readl(GUEST_RFLAGS);
1921 if (to_vmx(vcpu)->rmode.vm86_active) {
1922 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1923 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1924 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1925 }
1926 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001927 }
Avi Kivity6de12732011-03-07 12:51:22 +02001928 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001929}
1930
1931static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1932{
Avi Kivity6de12732011-03-07 12:51:22 +02001933 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1934 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001935 if (to_vmx(vcpu)->rmode.vm86_active) {
1936 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001937 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001938 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001939 vmcs_writel(GUEST_RFLAGS, rflags);
1940}
1941
Glauber Costa2809f5d2009-05-12 16:21:05 -04001942static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1943{
1944 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1945 int ret = 0;
1946
1947 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001948 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001949 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001950 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001951
1952 return ret & mask;
1953}
1954
1955static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1956{
1957 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1958 u32 interruptibility = interruptibility_old;
1959
1960 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1961
Jan Kiszka48005f62010-02-19 19:38:07 +01001962 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001963 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001964 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001965 interruptibility |= GUEST_INTR_STATE_STI;
1966
1967 if ((interruptibility != interruptibility_old))
1968 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1969}
1970
Avi Kivity6aa8b732006-12-10 02:21:36 -08001971static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1972{
1973 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001975 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001976 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001977 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978
Glauber Costa2809f5d2009-05-12 16:21:05 -04001979 /* skipping an emulated instruction also counts */
1980 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981}
1982
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001983/*
1984 * KVM wants to inject page-faults which it got to the guest. This function
1985 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001986 */
Gleb Natapove011c662013-09-25 12:51:35 +03001987static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001988{
1989 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1990
Gleb Natapove011c662013-09-25 12:51:35 +03001991 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001992 return 0;
1993
Jan Kiszka533558b2014-01-04 18:47:20 +01001994 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
1995 vmcs_read32(VM_EXIT_INTR_INFO),
1996 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001997 return 1;
1998}
1999
Avi Kivity298101d2007-11-25 13:41:11 +02002000static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02002001 bool has_error_code, u32 error_code,
2002 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02002003{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002004 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002005 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002006
Gleb Natapove011c662013-09-25 12:51:35 +03002007 if (!reinject && is_guest_mode(vcpu) &&
2008 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002009 return;
2010
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002011 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002012 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002013 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2014 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002015
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002016 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002017 int inc_eip = 0;
2018 if (kvm_exception_is_soft(nr))
2019 inc_eip = vcpu->arch.event_exit_inst_len;
2020 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002021 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002022 return;
2023 }
2024
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002025 if (kvm_exception_is_soft(nr)) {
2026 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2027 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002028 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2029 } else
2030 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2031
2032 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002033}
2034
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002035static bool vmx_rdtscp_supported(void)
2036{
2037 return cpu_has_vmx_rdtscp();
2038}
2039
Mao, Junjiead756a12012-07-02 01:18:48 +00002040static bool vmx_invpcid_supported(void)
2041{
2042 return cpu_has_vmx_invpcid() && enable_ept;
2043}
2044
Avi Kivity6aa8b732006-12-10 02:21:36 -08002045/*
Eddie Donga75beee2007-05-17 18:55:15 +03002046 * Swap MSR entry in host/guest MSR entry array.
2047 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002048static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002049{
Avi Kivity26bb0982009-09-07 11:14:12 +03002050 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002051
2052 tmp = vmx->guest_msrs[to];
2053 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2054 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002055}
2056
Yang Zhang8d146952013-01-25 10:18:50 +08002057static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2058{
2059 unsigned long *msr_bitmap;
2060
2061 if (irqchip_in_kernel(vcpu->kvm) && apic_x2apic_mode(vcpu->arch.apic)) {
2062 if (is_long_mode(vcpu))
2063 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2064 else
2065 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
2066 } else {
2067 if (is_long_mode(vcpu))
2068 msr_bitmap = vmx_msr_bitmap_longmode;
2069 else
2070 msr_bitmap = vmx_msr_bitmap_legacy;
2071 }
2072
2073 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2074}
2075
Eddie Donga75beee2007-05-17 18:55:15 +03002076/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002077 * Set up the vmcs to automatically save and restore system
2078 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2079 * mode, as fiddling with msrs is very expensive.
2080 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002081static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002082{
Avi Kivity26bb0982009-09-07 11:14:12 +03002083 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002084
Eddie Donga75beee2007-05-17 18:55:15 +03002085 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002086#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002087 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002088 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002089 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002090 move_msr_up(vmx, index, save_nmsrs++);
2091 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002092 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002093 move_msr_up(vmx, index, save_nmsrs++);
2094 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002095 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002096 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002097 index = __find_msr_index(vmx, MSR_TSC_AUX);
2098 if (index >= 0 && vmx->rdtscp_enabled)
2099 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002100 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002101 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002102 * if efer.sce is enabled.
2103 */
Brian Gerst8c065852010-07-17 09:03:26 -04002104 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002105 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002106 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002107 }
Eddie Donga75beee2007-05-17 18:55:15 +03002108#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002109 index = __find_msr_index(vmx, MSR_EFER);
2110 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002111 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002112
Avi Kivity26bb0982009-09-07 11:14:12 +03002113 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002114
Yang Zhang8d146952013-01-25 10:18:50 +08002115 if (cpu_has_vmx_msr_bitmap())
2116 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002117}
2118
2119/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120 * reads and returns guest's timestamp counter "register"
2121 * guest_tsc = host_tsc + tsc_offset -- 21.3
2122 */
2123static u64 guest_read_tsc(void)
2124{
2125 u64 host_tsc, tsc_offset;
2126
2127 rdtscll(host_tsc);
2128 tsc_offset = vmcs_read64(TSC_OFFSET);
2129 return host_tsc + tsc_offset;
2130}
2131
2132/*
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002133 * Like guest_read_tsc, but always returns L1's notion of the timestamp
2134 * counter, even if a nested guest (L2) is currently running.
2135 */
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002136u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002137{
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002138 u64 tsc_offset;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002139
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002140 tsc_offset = is_guest_mode(vcpu) ?
2141 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
2142 vmcs_read64(TSC_OFFSET);
2143 return host_tsc + tsc_offset;
2144}
2145
2146/*
Zachary Amsdencc578282012-02-03 15:43:50 -02002147 * Engage any workarounds for mis-matched TSC rates. Currently limited to
2148 * software catchup for faster rates on slower CPUs.
Joerg Roedel4051b182011-03-25 09:44:49 +01002149 */
Zachary Amsdencc578282012-02-03 15:43:50 -02002150static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +01002151{
Zachary Amsdencc578282012-02-03 15:43:50 -02002152 if (!scale)
2153 return;
2154
2155 if (user_tsc_khz > tsc_khz) {
2156 vcpu->arch.tsc_catchup = 1;
2157 vcpu->arch.tsc_always_catchup = 1;
2158 } else
2159 WARN(1, "user requested TSC rate below hardware speed\n");
Joerg Roedel4051b182011-03-25 09:44:49 +01002160}
2161
Will Auldba904632012-11-29 12:42:50 -08002162static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
2163{
2164 return vmcs_read64(TSC_OFFSET);
2165}
2166
Joerg Roedel4051b182011-03-25 09:44:49 +01002167/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002168 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002169 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002170static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002171{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002172 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002173 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002174 * We're here if L1 chose not to trap WRMSR to TSC. According
2175 * to the spec, this should set L1's TSC; The offset that L1
2176 * set for L2 remains unchanged, and still needs to be added
2177 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002178 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002179 struct vmcs12 *vmcs12;
2180 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
2181 /* recalculate vmcs02.TSC_OFFSET: */
2182 vmcs12 = get_vmcs12(vcpu);
2183 vmcs_write64(TSC_OFFSET, offset +
2184 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2185 vmcs12->tsc_offset : 0));
2186 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002187 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2188 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002189 vmcs_write64(TSC_OFFSET, offset);
2190 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191}
2192
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002193static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10002194{
2195 u64 offset = vmcs_read64(TSC_OFFSET);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002196
Zachary Amsdene48672f2010-08-19 22:07:23 -10002197 vmcs_write64(TSC_OFFSET, offset + adjustment);
Nadav Har'El79918252011-05-25 23:15:39 +03002198 if (is_guest_mode(vcpu)) {
2199 /* Even when running L2, the adjustment needs to apply to L1 */
2200 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002201 } else
2202 trace_kvm_write_tsc_offset(vcpu->vcpu_id, offset,
2203 offset + adjustment);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002204}
2205
Joerg Roedel857e4092011-03-25 09:44:50 +01002206static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
2207{
2208 return target_tsc - native_read_tsc();
2209}
2210
Nadav Har'El801d3422011-05-25 23:02:23 +03002211static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2212{
2213 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2214 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2215}
2216
2217/*
2218 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2219 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2220 * all guests if the "nested" module option is off, and can also be disabled
2221 * for a single guest by disabling its VMX cpuid bit.
2222 */
2223static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2224{
2225 return nested && guest_cpuid_has_vmx(vcpu);
2226}
2227
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002229 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2230 * returned for the various VMX controls MSRs when nested VMX is enabled.
2231 * The same values should also be used to verify that vmcs12 control fields are
2232 * valid during nested entry from L1 to L2.
2233 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2234 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2235 * bit in the high half is on if the corresponding bit in the control field
2236 * may be on. See also vmx_control_verify().
2237 * TODO: allow these variables to be modified (downgraded) by module options
2238 * or other means.
2239 */
2240static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
2241static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
2242static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
2243static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
2244static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002245static u32 nested_vmx_misc_low, nested_vmx_misc_high;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03002246static u32 nested_vmx_ept_caps;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002247static __init void nested_vmx_setup_ctls_msrs(void)
2248{
2249 /*
2250 * Note that as a general rule, the high half of the MSRs (bits in
2251 * the control fields which may be 1) should be initialized by the
2252 * intersection of the underlying hardware's MSR (i.e., features which
2253 * can be supported) and the list of features we want to expose -
2254 * because they are known to be properly supported in our code.
2255 * Also, usually, the low half of the MSRs (bits which must be 1) can
2256 * be set to 0, meaning that L1 may turn off any of these bits. The
2257 * reason is that if one of these bits is necessary, it will appear
2258 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2259 * fields of vmcs01 and vmcs02, will turn these bits off - and
2260 * nested_vmx_exit_handled() will not pass related exits to L1.
2261 * These rules have exceptions below.
2262 */
2263
2264 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002265 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
2266 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002267 /*
2268 * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
2269 * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
2270 */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002271 nested_vmx_pinbased_ctls_low |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2272 nested_vmx_pinbased_ctls_high &= PIN_BASED_EXT_INTR_MASK |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002273 PIN_BASED_NMI_EXITING | PIN_BASED_VIRTUAL_NMIS;
2274 nested_vmx_pinbased_ctls_high |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002275 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002276
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002277 /*
2278 * Exit controls
2279 * If bit 55 of VMX_BASIC is off, bits 0-8 and 10, 11, 13, 14, 16 and
2280 * 17 must be 1.
2281 */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002282 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
2283 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high);
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002284 nested_vmx_exit_ctls_low = VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb6f12502011-05-25 23:13:06 +03002285 /* Note that guest use of VM_EXIT_ACK_INTR_ON_EXIT is not supported. */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002286 nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002287#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002288 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002289#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002290 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
2291 nested_vmx_exit_ctls_high |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
2292 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +08002293 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER;
Paolo Bonzini36be0b92014-02-24 12:30:04 +01002294 if (vmx_mpx_supported())
2295 nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002296
2297 /* entry controls */
2298 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
2299 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002300 /* If bit 55 of VMX_BASIC is off, bits 0-8 and 12 must be 1. */
2301 nested_vmx_entry_ctls_low = VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002302 nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002303#ifdef CONFIG_X86_64
2304 VM_ENTRY_IA32E_MODE |
2305#endif
2306 VM_ENTRY_LOAD_IA32_PAT;
Nadav Har'El8049d652013-08-05 11:07:06 +03002307 nested_vmx_entry_ctls_high |= (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR |
2308 VM_ENTRY_LOAD_IA32_EFER);
Paolo Bonzini36be0b92014-02-24 12:30:04 +01002309 if (vmx_mpx_supported())
2310 nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
Jan Kiszka57435342013-08-06 10:39:56 +02002311
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002312 /* cpu-based controls */
2313 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
2314 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
2315 nested_vmx_procbased_ctls_low = 0;
2316 nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002317 CPU_BASED_VIRTUAL_INTR_PENDING |
2318 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002319 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2320 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2321 CPU_BASED_CR3_STORE_EXITING |
2322#ifdef CONFIG_X86_64
2323 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2324#endif
2325 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
2326 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
Avi Kivitydbcb4e72012-08-13 15:38:22 +03002327 CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING |
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002328 CPU_BASED_PAUSE_EXITING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002329 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2330 /*
2331 * We can allow some features even when not supported by the
2332 * hardware. For example, L1 can specify an MSR bitmap - and we
2333 * can use it to avoid exits to L1 - even when L0 runs L2
2334 * without MSR bitmaps.
2335 */
2336 nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
2337
2338 /* secondary cpu-based controls */
2339 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
2340 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
2341 nested_vmx_secondary_ctls_low = 0;
2342 nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002343 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02002344 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002345 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002346
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002347 if (enable_ept) {
2348 /* nested EPT: emulate EPT also to L1 */
2349 nested_vmx_secondary_ctls_high |= SECONDARY_EXEC_ENABLE_EPT;
Jan Kiszkaca72d972013-08-06 10:39:55 +02002350 nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002351 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2352 VMX_EPT_INVEPT_BIT;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002353 nested_vmx_ept_caps &= vmx_capability.ept;
2354 /*
2355 * Since invept is completely emulated we support both global
2356 * and context invalidation independent of what host cpu
2357 * supports
2358 */
2359 nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2360 VMX_EPT_EXTENT_CONTEXT_BIT;
2361 } else
2362 nested_vmx_ept_caps = 0;
2363
Jan Kiszkac18911a2013-03-13 16:06:41 +01002364 /* miscellaneous data */
2365 rdmsr(MSR_IA32_VMX_MISC, nested_vmx_misc_low, nested_vmx_misc_high);
Jan Kiszkaf41245002014-03-07 20:03:13 +01002366 nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2367 nested_vmx_misc_low |= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
2368 VMX_MISC_ACTIVITY_HLT;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002369 nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002370}
2371
2372static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2373{
2374 /*
2375 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2376 */
2377 return ((control & high) | low) == control;
2378}
2379
2380static inline u64 vmx_control_msr(u32 low, u32 high)
2381{
2382 return low | ((u64)high << 32);
2383}
2384
Jan Kiszkacae50132014-01-04 18:47:22 +01002385/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002386static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2387{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002388 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002389 case MSR_IA32_VMX_BASIC:
2390 /*
2391 * This MSR reports some information about VMX support. We
2392 * should return information about the VMX we emulate for the
2393 * guest, and the VMCS structure we give it - not about the
2394 * VMX support of the underlying hardware.
2395 */
2396 *pdata = VMCS12_REVISION |
2397 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2398 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2399 break;
2400 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2401 case MSR_IA32_VMX_PINBASED_CTLS:
2402 *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
2403 nested_vmx_pinbased_ctls_high);
2404 break;
2405 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2406 case MSR_IA32_VMX_PROCBASED_CTLS:
2407 *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
2408 nested_vmx_procbased_ctls_high);
2409 break;
2410 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2411 case MSR_IA32_VMX_EXIT_CTLS:
2412 *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
2413 nested_vmx_exit_ctls_high);
2414 break;
2415 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2416 case MSR_IA32_VMX_ENTRY_CTLS:
2417 *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
2418 nested_vmx_entry_ctls_high);
2419 break;
2420 case MSR_IA32_VMX_MISC:
Jan Kiszkac18911a2013-03-13 16:06:41 +01002421 *pdata = vmx_control_msr(nested_vmx_misc_low,
2422 nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002423 break;
2424 /*
2425 * These MSRs specify bits which the guest must keep fixed (on or off)
2426 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2427 * We picked the standard core2 setting.
2428 */
2429#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2430#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2431 case MSR_IA32_VMX_CR0_FIXED0:
2432 *pdata = VMXON_CR0_ALWAYSON;
2433 break;
2434 case MSR_IA32_VMX_CR0_FIXED1:
2435 *pdata = -1ULL;
2436 break;
2437 case MSR_IA32_VMX_CR4_FIXED0:
2438 *pdata = VMXON_CR4_ALWAYSON;
2439 break;
2440 case MSR_IA32_VMX_CR4_FIXED1:
2441 *pdata = -1ULL;
2442 break;
2443 case MSR_IA32_VMX_VMCS_ENUM:
2444 *pdata = 0x1f;
2445 break;
2446 case MSR_IA32_VMX_PROCBASED_CTLS2:
2447 *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
2448 nested_vmx_secondary_ctls_high);
2449 break;
2450 case MSR_IA32_VMX_EPT_VPID_CAP:
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002451 /* Currently, no nested vpid support */
2452 *pdata = nested_vmx_ept_caps;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002453 break;
2454 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002455 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002456 }
2457
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002458 return 0;
2459}
2460
2461/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002462 * Reads an msr value (of 'msr_index') into 'pdata'.
2463 * Returns 0 on success, non-0 otherwise.
2464 * Assumes vcpu_load() was already called.
2465 */
2466static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2467{
2468 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03002469 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002470
2471 if (!pdata) {
2472 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
2473 return -EINVAL;
2474 }
2475
2476 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002477#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002478 case MSR_FS_BASE:
2479 data = vmcs_readl(GUEST_FS_BASE);
2480 break;
2481 case MSR_GS_BASE:
2482 data = vmcs_readl(GUEST_GS_BASE);
2483 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002484 case MSR_KERNEL_GS_BASE:
2485 vmx_load_host_state(to_vmx(vcpu));
2486 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
2487 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002488#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002489 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002490 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302491 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492 data = guest_read_tsc();
2493 break;
2494 case MSR_IA32_SYSENTER_CS:
2495 data = vmcs_read32(GUEST_SYSENTER_CS);
2496 break;
2497 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002498 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002499 break;
2500 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002501 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002502 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002503 case MSR_IA32_BNDCFGS:
2504 data = vmcs_read64(GUEST_BNDCFGS);
2505 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002506 case MSR_IA32_FEATURE_CONTROL:
2507 if (!nested_vmx_allowed(vcpu))
2508 return 1;
2509 data = to_vmx(vcpu)->nested.msr_ia32_feature_control;
2510 break;
2511 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2512 if (!nested_vmx_allowed(vcpu))
2513 return 1;
2514 return vmx_get_vmx_msr(vcpu, msr_index, pdata);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002515 case MSR_TSC_AUX:
2516 if (!to_vmx(vcpu)->rdtscp_enabled)
2517 return 1;
2518 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002519 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002520 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002521 if (msr) {
2522 data = msr->data;
2523 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002525 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526 }
2527
2528 *pdata = data;
2529 return 0;
2530}
2531
Jan Kiszkacae50132014-01-04 18:47:22 +01002532static void vmx_leave_nested(struct kvm_vcpu *vcpu);
2533
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534/*
2535 * Writes msr value into into the appropriate "register".
2536 * Returns 0 on success, non-0 otherwise.
2537 * Assumes vcpu_load() was already called.
2538 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002539static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002541 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002542 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002543 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002544 u32 msr_index = msr_info->index;
2545 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03002546
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002548 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002549 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002550 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002551#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002553 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002554 vmcs_writel(GUEST_FS_BASE, data);
2555 break;
2556 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002557 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002558 vmcs_writel(GUEST_GS_BASE, data);
2559 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002560 case MSR_KERNEL_GS_BASE:
2561 vmx_load_host_state(vmx);
2562 vmx->msr_guest_kernel_gs_base = data;
2563 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564#endif
2565 case MSR_IA32_SYSENTER_CS:
2566 vmcs_write32(GUEST_SYSENTER_CS, data);
2567 break;
2568 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002569 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570 break;
2571 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002572 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002573 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002574 case MSR_IA32_BNDCFGS:
2575 vmcs_write64(GUEST_BNDCFGS, data);
2576 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302577 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08002578 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002579 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002580 case MSR_IA32_CR_PAT:
2581 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2582 vmcs_write64(GUEST_IA32_PAT, data);
2583 vcpu->arch.pat = data;
2584 break;
2585 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002586 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002587 break;
Will Auldba904632012-11-29 12:42:50 -08002588 case MSR_IA32_TSC_ADJUST:
2589 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002590 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002591 case MSR_IA32_FEATURE_CONTROL:
2592 if (!nested_vmx_allowed(vcpu) ||
2593 (to_vmx(vcpu)->nested.msr_ia32_feature_control &
2594 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
2595 return 1;
2596 vmx->nested.msr_ia32_feature_control = data;
2597 if (msr_info->host_initiated && data == 0)
2598 vmx_leave_nested(vcpu);
2599 break;
2600 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2601 return 1; /* they are read-only */
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002602 case MSR_TSC_AUX:
2603 if (!vmx->rdtscp_enabled)
2604 return 1;
2605 /* Check reserved bit, higher 32 bits should be zero */
2606 if ((data >> 32) != 0)
2607 return 1;
2608 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002609 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002610 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002611 if (msr) {
2612 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002613 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2614 preempt_disable();
Avi Kivity9ee73972012-03-06 14:16:33 +02002615 kvm_set_shared_msr(msr->index, msr->data,
2616 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03002617 preempt_enable();
2618 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002619 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002621 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002622 }
2623
Eddie Dong2cc51562007-05-21 07:28:09 +03002624 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625}
2626
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002627static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002629 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2630 switch (reg) {
2631 case VCPU_REGS_RSP:
2632 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2633 break;
2634 case VCPU_REGS_RIP:
2635 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2636 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002637 case VCPU_EXREG_PDPTR:
2638 if (enable_ept)
2639 ept_save_pdptrs(vcpu);
2640 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002641 default:
2642 break;
2643 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644}
2645
Avi Kivity6aa8b732006-12-10 02:21:36 -08002646static __init int cpu_has_kvm_support(void)
2647{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002648 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002649}
2650
2651static __init int vmx_disabled_by_bios(void)
2652{
2653 u64 msr;
2654
2655 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04002656 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08002657 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04002658 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2659 && tboot_enabled())
2660 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08002661 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04002662 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08002663 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08002664 && !tboot_enabled()) {
2665 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08002666 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04002667 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08002668 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08002669 /* launched w/o TXT and VMX disabled */
2670 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2671 && !tboot_enabled())
2672 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04002673 }
2674
2675 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676}
2677
Dongxiao Xu7725b892010-05-11 18:29:38 +08002678static void kvm_cpu_vmxon(u64 addr)
2679{
2680 asm volatile (ASM_VMX_VMXON_RAX
2681 : : "a"(&addr), "m"(addr)
2682 : "memory", "cc");
2683}
2684
Alexander Graf10474ae2009-09-15 11:37:46 +02002685static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002686{
2687 int cpu = raw_smp_processor_id();
2688 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04002689 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002690
Alexander Graf10474ae2009-09-15 11:37:46 +02002691 if (read_cr4() & X86_CR4_VMXE)
2692 return -EBUSY;
2693
Nadav Har'Eld462b812011-05-24 15:26:10 +03002694 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002695
2696 /*
2697 * Now we can enable the vmclear operation in kdump
2698 * since the loaded_vmcss_on_cpu list on this cpu
2699 * has been initialized.
2700 *
2701 * Though the cpu is not in VMX operation now, there
2702 * is no problem to enable the vmclear operation
2703 * for the loaded_vmcss_on_cpu list is empty!
2704 */
2705 crash_enable_local_vmclear(cpu);
2706
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04002708
2709 test_bits = FEATURE_CONTROL_LOCKED;
2710 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2711 if (tboot_enabled())
2712 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2713
2714 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04002716 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2717 }
Rusty Russell66aee912007-07-17 23:34:16 +10002718 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02002719
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002720 if (vmm_exclusive) {
2721 kvm_cpu_vmxon(phys_addr);
2722 ept_sync_global();
2723 }
Alexander Graf10474ae2009-09-15 11:37:46 +02002724
Konrad Rzeszutek Wilk357d1222013-04-05 16:42:23 -04002725 native_store_gdt(&__get_cpu_var(host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03002726
Alexander Graf10474ae2009-09-15 11:37:46 +02002727 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728}
2729
Nadav Har'Eld462b812011-05-24 15:26:10 +03002730static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002731{
2732 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002733 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002734
Nadav Har'Eld462b812011-05-24 15:26:10 +03002735 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2736 loaded_vmcss_on_cpu_link)
2737 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002738}
2739
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002740
2741/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2742 * tricks.
2743 */
2744static void kvm_cpu_vmxoff(void)
2745{
2746 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002747}
2748
Avi Kivity6aa8b732006-12-10 02:21:36 -08002749static void hardware_disable(void *garbage)
2750{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002751 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002752 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002753 kvm_cpu_vmxoff();
2754 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08002755 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002756}
2757
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002758static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002759 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760{
2761 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002762 u32 ctl = ctl_min | ctl_opt;
2763
2764 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2765
2766 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2767 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2768
2769 /* Ensure minimum (required) set of control bits are supported. */
2770 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002771 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002772
2773 *result = ctl;
2774 return 0;
2775}
2776
Avi Kivity110312c2010-12-21 12:54:20 +02002777static __init bool allow_1_setting(u32 msr, u32 ctl)
2778{
2779 u32 vmx_msr_low, vmx_msr_high;
2780
2781 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2782 return vmx_msr_high & ctl;
2783}
2784
Yang, Sheng002c7f72007-07-31 14:23:01 +03002785static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002786{
2787 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002788 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002789 u32 _pin_based_exec_control = 0;
2790 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002791 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002792 u32 _vmexit_control = 0;
2793 u32 _vmentry_control = 0;
2794
Raghavendra K T10166742012-02-07 23:19:20 +05302795 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002796#ifdef CONFIG_X86_64
2797 CPU_BASED_CR8_LOAD_EXITING |
2798 CPU_BASED_CR8_STORE_EXITING |
2799#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002800 CPU_BASED_CR3_LOAD_EXITING |
2801 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002802 CPU_BASED_USE_IO_BITMAPS |
2803 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03002804 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08002805 CPU_BASED_MWAIT_EXITING |
2806 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002807 CPU_BASED_INVLPG_EXITING |
2808 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002809
Sheng Yangf78e0e22007-10-29 09:40:42 +08002810 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002811 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002812 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002813 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2814 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002815 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002816#ifdef CONFIG_X86_64
2817 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2818 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2819 ~CPU_BASED_CR8_STORE_EXITING;
2820#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002821 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002822 min2 = 0;
2823 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002824 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002825 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002826 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002827 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002828 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002829 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00002830 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002831 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002832 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03002833 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2834 SECONDARY_EXEC_SHADOW_VMCS;
Sheng Yangd56f5462008-04-25 10:13:16 +08002835 if (adjust_vmx_controls(min2, opt2,
2836 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002837 &_cpu_based_2nd_exec_control) < 0)
2838 return -EIO;
2839 }
2840#ifndef CONFIG_X86_64
2841 if (!(_cpu_based_2nd_exec_control &
2842 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2843 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2844#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002845
2846 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2847 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002848 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002849 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2850 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002851
Sheng Yangd56f5462008-04-25 10:13:16 +08002852 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002853 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2854 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002855 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2856 CPU_BASED_CR3_STORE_EXITING |
2857 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08002858 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
2859 vmx_capability.ept, vmx_capability.vpid);
2860 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002861
Paolo Bonzini81908bf2014-02-21 10:32:27 +01002862 min = VM_EXIT_SAVE_DEBUG_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002863#ifdef CONFIG_X86_64
2864 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2865#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08002866 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002867 VM_EXIT_ACK_INTR_ON_EXIT | VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002868 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2869 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002870 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002871
Yang Zhang01e439b2013-04-11 19:25:12 +08002872 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2873 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR;
2874 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2875 &_pin_based_exec_control) < 0)
2876 return -EIO;
2877
2878 if (!(_cpu_based_2nd_exec_control &
2879 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) ||
2880 !(_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT))
2881 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2882
Paolo Bonzinic845f9c2014-02-21 10:55:44 +01002883 min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002884 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002885 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2886 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002887 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002889 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002890
2891 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2892 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002893 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002894
2895#ifdef CONFIG_X86_64
2896 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2897 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002898 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002899#endif
2900
2901 /* Require Write-Back (WB) memory type for VMCS accesses. */
2902 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002903 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002904
Yang, Sheng002c7f72007-07-31 14:23:01 +03002905 vmcs_conf->size = vmx_msr_high & 0x1fff;
2906 vmcs_conf->order = get_order(vmcs_config.size);
2907 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002908
Yang, Sheng002c7f72007-07-31 14:23:01 +03002909 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2910 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002911 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002912 vmcs_conf->vmexit_ctrl = _vmexit_control;
2913 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002914
Avi Kivity110312c2010-12-21 12:54:20 +02002915 cpu_has_load_ia32_efer =
2916 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2917 VM_ENTRY_LOAD_IA32_EFER)
2918 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2919 VM_EXIT_LOAD_IA32_EFER);
2920
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002921 cpu_has_load_perf_global_ctrl =
2922 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2923 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
2924 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2925 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2926
2927 /*
2928 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
2929 * but due to arrata below it can't be used. Workaround is to use
2930 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2931 *
2932 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
2933 *
2934 * AAK155 (model 26)
2935 * AAP115 (model 30)
2936 * AAT100 (model 37)
2937 * BC86,AAY89,BD102 (model 44)
2938 * BA97 (model 46)
2939 *
2940 */
2941 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
2942 switch (boot_cpu_data.x86_model) {
2943 case 26:
2944 case 30:
2945 case 37:
2946 case 44:
2947 case 46:
2948 cpu_has_load_perf_global_ctrl = false;
2949 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2950 "does not work properly. Using workaround\n");
2951 break;
2952 default:
2953 break;
2954 }
2955 }
2956
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002957 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002958}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959
2960static struct vmcs *alloc_vmcs_cpu(int cpu)
2961{
2962 int node = cpu_to_node(cpu);
2963 struct page *pages;
2964 struct vmcs *vmcs;
2965
Mel Gorman6484eb32009-06-16 15:31:54 -07002966 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 if (!pages)
2968 return NULL;
2969 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002970 memset(vmcs, 0, vmcs_config.size);
2971 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972 return vmcs;
2973}
2974
2975static struct vmcs *alloc_vmcs(void)
2976{
Ingo Molnard3b2c332007-01-05 16:36:23 -08002977 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978}
2979
2980static void free_vmcs(struct vmcs *vmcs)
2981{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002982 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983}
2984
Nadav Har'Eld462b812011-05-24 15:26:10 +03002985/*
2986 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2987 */
2988static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
2989{
2990 if (!loaded_vmcs->vmcs)
2991 return;
2992 loaded_vmcs_clear(loaded_vmcs);
2993 free_vmcs(loaded_vmcs->vmcs);
2994 loaded_vmcs->vmcs = NULL;
2995}
2996
Sam Ravnborg39959582007-06-01 00:47:13 -07002997static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998{
2999 int cpu;
3000
Zachary Amsden3230bb42009-09-29 11:38:37 -10003001 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003002 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10003003 per_cpu(vmxarea, cpu) = NULL;
3004 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003005}
3006
Avi Kivity6aa8b732006-12-10 02:21:36 -08003007static __init int alloc_kvm_area(void)
3008{
3009 int cpu;
3010
Zachary Amsden3230bb42009-09-29 11:38:37 -10003011 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003012 struct vmcs *vmcs;
3013
3014 vmcs = alloc_vmcs_cpu(cpu);
3015 if (!vmcs) {
3016 free_kvm_area();
3017 return -ENOMEM;
3018 }
3019
3020 per_cpu(vmxarea, cpu) = vmcs;
3021 }
3022 return 0;
3023}
3024
3025static __init int hardware_setup(void)
3026{
Yang, Sheng002c7f72007-07-31 14:23:01 +03003027 if (setup_vmcs_config(&vmcs_config) < 0)
3028 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01003029
3030 if (boot_cpu_has(X86_FEATURE_NX))
3031 kvm_enable_efer_bits(EFER_NX);
3032
Sheng Yang93ba03c2009-04-01 15:52:32 +08003033 if (!cpu_has_vmx_vpid())
3034 enable_vpid = 0;
Abel Gordonabc4fc52013-04-18 14:35:25 +03003035 if (!cpu_has_vmx_shadow_vmcs())
3036 enable_shadow_vmcs = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08003037
Sheng Yang4bc9b982010-06-02 14:05:24 +08003038 if (!cpu_has_vmx_ept() ||
3039 !cpu_has_vmx_ept_4levels()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08003040 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003041 enable_unrestricted_guest = 0;
Xudong Hao83c3a332012-05-28 19:33:35 +08003042 enable_ept_ad_bits = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003043 }
3044
Xudong Hao83c3a332012-05-28 19:33:35 +08003045 if (!cpu_has_vmx_ept_ad_bits())
3046 enable_ept_ad_bits = 0;
3047
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003048 if (!cpu_has_vmx_unrestricted_guest())
3049 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08003050
3051 if (!cpu_has_vmx_flexpriority())
3052 flexpriority_enabled = 0;
3053
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003054 if (!cpu_has_vmx_tpr_shadow())
3055 kvm_x86_ops->update_cr8_intercept = NULL;
3056
Marcelo Tosatti54dee992009-06-11 12:07:44 -03003057 if (enable_ept && !cpu_has_vmx_ept_2m_page())
3058 kvm_disable_largepages();
3059
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003060 if (!cpu_has_vmx_ple())
3061 ple_gap = 0;
3062
Yang Zhang01e439b2013-04-11 19:25:12 +08003063 if (!cpu_has_vmx_apicv())
3064 enable_apicv = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08003065
Yang Zhang01e439b2013-04-11 19:25:12 +08003066 if (enable_apicv)
Yang Zhangc7c9c562013-01-25 10:18:51 +08003067 kvm_x86_ops->update_cr8_intercept = NULL;
Yang Zhanga20ed542013-04-11 19:25:15 +08003068 else {
Yang Zhangc7c9c562013-01-25 10:18:51 +08003069 kvm_x86_ops->hwapic_irr_update = NULL;
Yang Zhanga20ed542013-04-11 19:25:15 +08003070 kvm_x86_ops->deliver_posted_interrupt = NULL;
3071 kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy;
3072 }
Yang Zhang83d4c282013-01-25 10:18:49 +08003073
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003074 if (nested)
3075 nested_vmx_setup_ctls_msrs();
3076
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077 return alloc_kvm_area();
3078}
3079
3080static __exit void hardware_unsetup(void)
3081{
3082 free_kvm_area();
3083}
3084
Gleb Natapov14168782013-01-21 15:36:49 +02003085static bool emulation_required(struct kvm_vcpu *vcpu)
3086{
3087 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3088}
3089
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003090static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003091 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003092{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003093 if (!emulate_invalid_guest_state) {
3094 /*
3095 * CS and SS RPL should be equal during guest entry according
3096 * to VMX spec, but in reality it is not always so. Since vcpu
3097 * is in the middle of the transition from real mode to
3098 * protected mode it is safe to assume that RPL 0 is a good
3099 * default value.
3100 */
3101 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
3102 save->selector &= ~SELECTOR_RPL_MASK;
3103 save->dpl = save->selector & SELECTOR_RPL_MASK;
3104 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003106 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003107}
3108
3109static void enter_pmode(struct kvm_vcpu *vcpu)
3110{
3111 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003112 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113
Gleb Natapovd99e4152012-12-20 16:57:45 +02003114 /*
3115 * Update real mode segment cache. It may be not up-to-date if sement
3116 * register was written while vcpu was in a guest mode.
3117 */
3118 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3119 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3120 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3121 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3122 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3123 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3124
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003125 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003126
Avi Kivity2fb92db2011-04-27 19:42:18 +03003127 vmx_segment_cache_clear(vmx);
3128
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003129 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003130
3131 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003132 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3133 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003134 vmcs_writel(GUEST_RFLAGS, flags);
3135
Rusty Russell66aee912007-07-17 23:34:16 +10003136 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3137 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003138
3139 update_exception_bitmap(vcpu);
3140
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003141 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3142 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3143 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3144 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3145 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3146 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Gleb Natapov1f3141e2013-01-21 15:36:41 +02003147
3148 /* CPL is always 0 when CPU enters protected mode */
3149 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
3150 vmx->cpl = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151}
3152
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003153static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154{
Mathias Krause772e0312012-08-30 01:30:19 +02003155 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003156 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157
Gleb Natapovd99e4152012-12-20 16:57:45 +02003158 var.dpl = 0x3;
3159 if (seg == VCPU_SREG_CS)
3160 var.type = 0x3;
3161
3162 if (!emulate_invalid_guest_state) {
3163 var.selector = var.base >> 4;
3164 var.base = var.base & 0xffff0;
3165 var.limit = 0xffff;
3166 var.g = 0;
3167 var.db = 0;
3168 var.present = 1;
3169 var.s = 1;
3170 var.l = 0;
3171 var.unusable = 0;
3172 var.type = 0x3;
3173 var.avl = 0;
3174 if (save->base & 0xf)
3175 printk_once(KERN_WARNING "kvm: segment base is not "
3176 "paragraph aligned when entering "
3177 "protected mode (seg=%d)", seg);
3178 }
3179
3180 vmcs_write16(sf->selector, var.selector);
3181 vmcs_write32(sf->base, var.base);
3182 vmcs_write32(sf->limit, var.limit);
3183 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003184}
3185
3186static void enter_rmode(struct kvm_vcpu *vcpu)
3187{
3188 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003189 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003190
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003191 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3192 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3193 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3194 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3195 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003196 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3197 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003198
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003199 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003200
Gleb Natapov776e58e2011-03-13 12:34:27 +02003201 /*
3202 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003203 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003204 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003205 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003206 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3207 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003208
Avi Kivity2fb92db2011-04-27 19:42:18 +03003209 vmx_segment_cache_clear(vmx);
3210
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003211 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003212 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3214
3215 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003216 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003218 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219
3220 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003221 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222 update_exception_bitmap(vcpu);
3223
Gleb Natapovd99e4152012-12-20 16:57:45 +02003224 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3225 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3226 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3227 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3228 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3229 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003230
Eddie Dong8668a3c2007-10-10 14:26:45 +08003231 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003232}
3233
Amit Shah401d10d2009-02-20 22:53:37 +05303234static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3235{
3236 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003237 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3238
3239 if (!msr)
3240 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303241
Avi Kivity44ea2b12009-09-06 15:55:37 +03003242 /*
3243 * Force kernel_gs_base reloading before EFER changes, as control
3244 * of this msr depends on is_long_mode().
3245 */
3246 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003247 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303248 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003249 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303250 msr->data = efer;
3251 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003252 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303253
3254 msr->data = efer & ~EFER_LME;
3255 }
3256 setup_msrs(vmx);
3257}
3258
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003259#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003260
3261static void enter_lmode(struct kvm_vcpu *vcpu)
3262{
3263 u32 guest_tr_ar;
3264
Avi Kivity2fb92db2011-04-27 19:42:18 +03003265 vmx_segment_cache_clear(to_vmx(vcpu));
3266
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
3268 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003269 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3270 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271 vmcs_write32(GUEST_TR_AR_BYTES,
3272 (guest_tr_ar & ~AR_TYPE_MASK)
3273 | AR_TYPE_BUSY_64_TSS);
3274 }
Avi Kivityda38f432010-07-06 11:30:49 +03003275 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276}
3277
3278static void exit_lmode(struct kvm_vcpu *vcpu)
3279{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003280 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003281 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003282}
3283
3284#endif
3285
Sheng Yang2384d2b2008-01-17 15:14:33 +08003286static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3287{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08003288 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003289 if (enable_ept) {
3290 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3291 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003292 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003293 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003294}
3295
Avi Kivitye8467fd2009-12-29 18:43:06 +02003296static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3297{
3298 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3299
3300 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3301 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3302}
3303
Avi Kivityaff48ba2010-12-05 18:56:11 +02003304static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3305{
3306 if (enable_ept && is_paging(vcpu))
3307 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3308 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3309}
3310
Anthony Liguori25c4c272007-04-27 09:29:21 +03003311static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003312{
Avi Kivityfc78f512009-12-07 12:16:48 +02003313 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3314
3315 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3316 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003317}
3318
Sheng Yang14394422008-04-28 12:24:45 +08003319static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3320{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003321 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3322
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003323 if (!test_bit(VCPU_EXREG_PDPTR,
3324 (unsigned long *)&vcpu->arch.regs_dirty))
3325 return;
3326
Sheng Yang14394422008-04-28 12:24:45 +08003327 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003328 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3329 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3330 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3331 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003332 }
3333}
3334
Avi Kivity8f5d5492009-05-31 18:41:29 +03003335static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3336{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003337 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3338
Avi Kivity8f5d5492009-05-31 18:41:29 +03003339 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003340 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3341 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3342 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3343 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003344 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003345
3346 __set_bit(VCPU_EXREG_PDPTR,
3347 (unsigned long *)&vcpu->arch.regs_avail);
3348 __set_bit(VCPU_EXREG_PDPTR,
3349 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003350}
3351
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003352static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003353
3354static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3355 unsigned long cr0,
3356 struct kvm_vcpu *vcpu)
3357{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003358 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3359 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003360 if (!(cr0 & X86_CR0_PG)) {
3361 /* From paging/starting to nonpaging */
3362 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003363 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003364 (CPU_BASED_CR3_LOAD_EXITING |
3365 CPU_BASED_CR3_STORE_EXITING));
3366 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003367 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003368 } else if (!is_paging(vcpu)) {
3369 /* From nonpaging to paging */
3370 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003371 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003372 ~(CPU_BASED_CR3_LOAD_EXITING |
3373 CPU_BASED_CR3_STORE_EXITING));
3374 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003375 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003376 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003377
3378 if (!(cr0 & X86_CR0_WP))
3379 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003380}
3381
Avi Kivity6aa8b732006-12-10 02:21:36 -08003382static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3383{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003384 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003385 unsigned long hw_cr0;
3386
Gleb Natapov50378782013-02-04 16:00:28 +02003387 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003388 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003389 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003390 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003391 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003392
Gleb Natapov218e7632013-01-21 15:36:45 +02003393 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3394 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003395
Gleb Natapov218e7632013-01-21 15:36:45 +02003396 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3397 enter_rmode(vcpu);
3398 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003399
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003400#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003401 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003402 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003403 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003404 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405 exit_lmode(vcpu);
3406 }
3407#endif
3408
Avi Kivity089d0342009-03-23 18:26:32 +02003409 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003410 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3411
Avi Kivity02daab22009-12-30 12:40:26 +02003412 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003413 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003414
Avi Kivity6aa8b732006-12-10 02:21:36 -08003415 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003416 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003417 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003418
3419 /* depends on vcpu->arch.cr0 to be set to a new value */
3420 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421}
3422
Sheng Yang14394422008-04-28 12:24:45 +08003423static u64 construct_eptp(unsigned long root_hpa)
3424{
3425 u64 eptp;
3426
3427 /* TODO write the value reading from MSR */
3428 eptp = VMX_EPT_DEFAULT_MT |
3429 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003430 if (enable_ept_ad_bits)
3431 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003432 eptp |= (root_hpa & PAGE_MASK);
3433
3434 return eptp;
3435}
3436
Avi Kivity6aa8b732006-12-10 02:21:36 -08003437static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3438{
Sheng Yang14394422008-04-28 12:24:45 +08003439 unsigned long guest_cr3;
3440 u64 eptp;
3441
3442 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003443 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003444 eptp = construct_eptp(cr3);
3445 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02003446 if (is_paging(vcpu) || is_guest_mode(vcpu))
3447 guest_cr3 = kvm_read_cr3(vcpu);
3448 else
3449 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02003450 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003451 }
3452
Sheng Yang2384d2b2008-01-17 15:14:33 +08003453 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003454 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455}
3456
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003457static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003458{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003459 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08003460 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
3461
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003462 if (cr4 & X86_CR4_VMXE) {
3463 /*
3464 * To use VMXON (and later other VMX instructions), a guest
3465 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3466 * So basically the check on whether to allow nested VMX
3467 * is here.
3468 */
3469 if (!nested_vmx_allowed(vcpu))
3470 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003471 }
3472 if (to_vmx(vcpu)->nested.vmxon &&
3473 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003474 return 1;
3475
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003476 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02003477 if (enable_ept) {
3478 if (!is_paging(vcpu)) {
3479 hw_cr4 &= ~X86_CR4_PAE;
3480 hw_cr4 |= X86_CR4_PSE;
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003481 /*
3482 * SMEP is disabled if CPU is in non-paging mode in
3483 * hardware. However KVM always uses paging mode to
3484 * emulate guest non-paging mode with TDP.
3485 * To emulate this behavior, SMEP needs to be manually
3486 * disabled when guest switches to non-paging mode.
3487 */
3488 hw_cr4 &= ~X86_CR4_SMEP;
Avi Kivitybc230082009-12-08 12:14:42 +02003489 } else if (!(cr4 & X86_CR4_PAE)) {
3490 hw_cr4 &= ~X86_CR4_PAE;
3491 }
3492 }
Sheng Yang14394422008-04-28 12:24:45 +08003493
3494 vmcs_writel(CR4_READ_SHADOW, cr4);
3495 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003496 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003497}
3498
Avi Kivity6aa8b732006-12-10 02:21:36 -08003499static void vmx_get_segment(struct kvm_vcpu *vcpu,
3500 struct kvm_segment *var, int seg)
3501{
Avi Kivitya9179492011-01-03 14:28:52 +02003502 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003503 u32 ar;
3504
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003505 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003506 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003507 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003508 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003509 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003510 var->base = vmx_read_guest_seg_base(vmx, seg);
3511 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3512 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003513 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003514 var->base = vmx_read_guest_seg_base(vmx, seg);
3515 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3516 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3517 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003518 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003519 var->type = ar & 15;
3520 var->s = (ar >> 4) & 1;
3521 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003522 /*
3523 * Some userspaces do not preserve unusable property. Since usable
3524 * segment has to be present according to VMX spec we can use present
3525 * property to amend userspace bug by making unusable segment always
3526 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3527 * segment as unusable.
3528 */
3529 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003530 var->avl = (ar >> 12) & 1;
3531 var->l = (ar >> 13) & 1;
3532 var->db = (ar >> 14) & 1;
3533 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003534}
3535
Avi Kivitya9179492011-01-03 14:28:52 +02003536static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3537{
Avi Kivitya9179492011-01-03 14:28:52 +02003538 struct kvm_segment s;
3539
3540 if (to_vmx(vcpu)->rmode.vm86_active) {
3541 vmx_get_segment(vcpu, &s, seg);
3542 return s.base;
3543 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003544 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003545}
3546
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003547static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003548{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003549 struct vcpu_vmx *vmx = to_vmx(vcpu);
3550
Avi Kivity3eeb3282010-01-21 15:31:48 +02003551 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003552 return 0;
3553
Avi Kivityf4c63e52011-03-07 14:54:28 +02003554 if (!is_long_mode(vcpu)
3555 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
Izik Eidus2e4d2652008-03-24 19:38:34 +02003556 return 3;
3557
Avi Kivity69c73022011-03-07 15:26:44 +02003558 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
3559 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003560 vmx->cpl = vmx_read_guest_seg_selector(vmx, VCPU_SREG_CS) & 3;
Avi Kivity69c73022011-03-07 15:26:44 +02003561 }
Avi Kivityd881e6f2012-06-06 18:36:48 +03003562
3563 return vmx->cpl;
Avi Kivity69c73022011-03-07 15:26:44 +02003564}
3565
3566
Avi Kivity653e3102007-05-07 10:55:37 +03003567static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003568{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003569 u32 ar;
3570
Avi Kivityf0495f92012-06-07 17:06:10 +03003571 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003572 ar = 1 << 16;
3573 else {
3574 ar = var->type & 15;
3575 ar |= (var->s & 1) << 4;
3576 ar |= (var->dpl & 3) << 5;
3577 ar |= (var->present & 1) << 7;
3578 ar |= (var->avl & 1) << 12;
3579 ar |= (var->l & 1) << 13;
3580 ar |= (var->db & 1) << 14;
3581 ar |= (var->g & 1) << 15;
3582 }
Avi Kivity653e3102007-05-07 10:55:37 +03003583
3584 return ar;
3585}
3586
3587static void vmx_set_segment(struct kvm_vcpu *vcpu,
3588 struct kvm_segment *var, int seg)
3589{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003590 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003591 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003592
Avi Kivity2fb92db2011-04-27 19:42:18 +03003593 vmx_segment_cache_clear(vmx);
Gleb Natapov2f143242013-01-21 15:36:42 +02003594 if (seg == VCPU_SREG_CS)
3595 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity2fb92db2011-04-27 19:42:18 +03003596
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003597 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3598 vmx->rmode.segs[seg] = *var;
3599 if (seg == VCPU_SREG_TR)
3600 vmcs_write16(sf->selector, var->selector);
3601 else if (var->s)
3602 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003603 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003604 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003605
Avi Kivity653e3102007-05-07 10:55:37 +03003606 vmcs_writel(sf->base, var->base);
3607 vmcs_write32(sf->limit, var->limit);
3608 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003609
3610 /*
3611 * Fix the "Accessed" bit in AR field of segment registers for older
3612 * qemu binaries.
3613 * IA32 arch specifies that at the time of processor reset the
3614 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003615 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003616 * state vmexit when "unrestricted guest" mode is turned on.
3617 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3618 * tree. Newer qemu binaries with that qemu fix would not need this
3619 * kvm hack.
3620 */
3621 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003622 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003623
Gleb Natapovf924d662012-12-12 19:10:55 +02003624 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003625
3626out:
Gleb Natapov14168782013-01-21 15:36:49 +02003627 vmx->emulation_required |= emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003628}
3629
Avi Kivity6aa8b732006-12-10 02:21:36 -08003630static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3631{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003632 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633
3634 *db = (ar >> 14) & 1;
3635 *l = (ar >> 13) & 1;
3636}
3637
Gleb Natapov89a27f42010-02-16 10:51:48 +02003638static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003639{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003640 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3641 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003642}
3643
Gleb Natapov89a27f42010-02-16 10:51:48 +02003644static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003645{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003646 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3647 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003648}
3649
Gleb Natapov89a27f42010-02-16 10:51:48 +02003650static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003651{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003652 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3653 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003654}
3655
Gleb Natapov89a27f42010-02-16 10:51:48 +02003656static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003657{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003658 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3659 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003660}
3661
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003662static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3663{
3664 struct kvm_segment var;
3665 u32 ar;
3666
3667 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003668 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003669 if (seg == VCPU_SREG_CS)
3670 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003671 ar = vmx_segment_access_rights(&var);
3672
3673 if (var.base != (var.selector << 4))
3674 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003675 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003676 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003677 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003678 return false;
3679
3680 return true;
3681}
3682
3683static bool code_segment_valid(struct kvm_vcpu *vcpu)
3684{
3685 struct kvm_segment cs;
3686 unsigned int cs_rpl;
3687
3688 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3689 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
3690
Avi Kivity1872a3f2009-01-04 23:26:52 +02003691 if (cs.unusable)
3692 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003693 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
3694 return false;
3695 if (!cs.s)
3696 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003697 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003698 if (cs.dpl > cs_rpl)
3699 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003700 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003701 if (cs.dpl != cs_rpl)
3702 return false;
3703 }
3704 if (!cs.present)
3705 return false;
3706
3707 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3708 return true;
3709}
3710
3711static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3712{
3713 struct kvm_segment ss;
3714 unsigned int ss_rpl;
3715
3716 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3717 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
3718
Avi Kivity1872a3f2009-01-04 23:26:52 +02003719 if (ss.unusable)
3720 return true;
3721 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003722 return false;
3723 if (!ss.s)
3724 return false;
3725 if (ss.dpl != ss_rpl) /* DPL != RPL */
3726 return false;
3727 if (!ss.present)
3728 return false;
3729
3730 return true;
3731}
3732
3733static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3734{
3735 struct kvm_segment var;
3736 unsigned int rpl;
3737
3738 vmx_get_segment(vcpu, &var, seg);
3739 rpl = var.selector & SELECTOR_RPL_MASK;
3740
Avi Kivity1872a3f2009-01-04 23:26:52 +02003741 if (var.unusable)
3742 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003743 if (!var.s)
3744 return false;
3745 if (!var.present)
3746 return false;
3747 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
3748 if (var.dpl < rpl) /* DPL < RPL */
3749 return false;
3750 }
3751
3752 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3753 * rights flags
3754 */
3755 return true;
3756}
3757
3758static bool tr_valid(struct kvm_vcpu *vcpu)
3759{
3760 struct kvm_segment tr;
3761
3762 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3763
Avi Kivity1872a3f2009-01-04 23:26:52 +02003764 if (tr.unusable)
3765 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003766 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3767 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003768 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003769 return false;
3770 if (!tr.present)
3771 return false;
3772
3773 return true;
3774}
3775
3776static bool ldtr_valid(struct kvm_vcpu *vcpu)
3777{
3778 struct kvm_segment ldtr;
3779
3780 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3781
Avi Kivity1872a3f2009-01-04 23:26:52 +02003782 if (ldtr.unusable)
3783 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003784 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3785 return false;
3786 if (ldtr.type != 2)
3787 return false;
3788 if (!ldtr.present)
3789 return false;
3790
3791 return true;
3792}
3793
3794static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3795{
3796 struct kvm_segment cs, ss;
3797
3798 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3799 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3800
3801 return ((cs.selector & SELECTOR_RPL_MASK) ==
3802 (ss.selector & SELECTOR_RPL_MASK));
3803}
3804
3805/*
3806 * Check if guest state is valid. Returns true if valid, false if
3807 * not.
3808 * We assume that registers are always usable
3809 */
3810static bool guest_state_valid(struct kvm_vcpu *vcpu)
3811{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02003812 if (enable_unrestricted_guest)
3813 return true;
3814
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003815 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003816 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003817 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3818 return false;
3819 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3820 return false;
3821 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3822 return false;
3823 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3824 return false;
3825 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3826 return false;
3827 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3828 return false;
3829 } else {
3830 /* protected mode guest state checks */
3831 if (!cs_ss_rpl_check(vcpu))
3832 return false;
3833 if (!code_segment_valid(vcpu))
3834 return false;
3835 if (!stack_segment_valid(vcpu))
3836 return false;
3837 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3838 return false;
3839 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3840 return false;
3841 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3842 return false;
3843 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3844 return false;
3845 if (!tr_valid(vcpu))
3846 return false;
3847 if (!ldtr_valid(vcpu))
3848 return false;
3849 }
3850 /* TODO:
3851 * - Add checks on RIP
3852 * - Add checks on RFLAGS
3853 */
3854
3855 return true;
3856}
3857
Mike Dayd77c26f2007-10-08 09:02:08 -04003858static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003859{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003860 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003861 u16 data = 0;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003862 int r, idx, ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003863
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003864 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003865 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003866 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3867 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003868 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003869 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003870 r = kvm_write_guest_page(kvm, fn++, &data,
3871 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003872 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003873 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003874 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3875 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003876 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003877 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3878 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003879 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003880 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003881 r = kvm_write_guest_page(kvm, fn, &data,
3882 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3883 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02003884 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003885 goto out;
3886
3887 ret = 1;
3888out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003889 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003890 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003891}
3892
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003893static int init_rmode_identity_map(struct kvm *kvm)
3894{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003895 int i, idx, r, ret;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003896 pfn_t identity_map_pfn;
3897 u32 tmp;
3898
Avi Kivity089d0342009-03-23 18:26:32 +02003899 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003900 return 1;
3901 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
3902 printk(KERN_ERR "EPT: identity-mapping pagetable "
3903 "haven't been allocated!\n");
3904 return 0;
3905 }
3906 if (likely(kvm->arch.ept_identity_pagetable_done))
3907 return 1;
3908 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003909 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003910 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003911 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3912 if (r < 0)
3913 goto out;
3914 /* Set up identity-mapping pagetable for EPT in real mode */
3915 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3916 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3917 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3918 r = kvm_write_guest_page(kvm, identity_map_pfn,
3919 &tmp, i * sizeof(tmp), sizeof(tmp));
3920 if (r < 0)
3921 goto out;
3922 }
3923 kvm->arch.ept_identity_pagetable_done = true;
3924 ret = 1;
3925out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003926 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003927 return ret;
3928}
3929
Avi Kivity6aa8b732006-12-10 02:21:36 -08003930static void seg_setup(int seg)
3931{
Mathias Krause772e0312012-08-30 01:30:19 +02003932 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003933 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003934
3935 vmcs_write16(sf->selector, 0);
3936 vmcs_writel(sf->base, 0);
3937 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003938 ar = 0x93;
3939 if (seg == VCPU_SREG_CS)
3940 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003941
3942 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003943}
3944
Sheng Yangf78e0e22007-10-29 09:40:42 +08003945static int alloc_apic_access_page(struct kvm *kvm)
3946{
Xiao Guangrong44841412012-09-07 14:14:20 +08003947 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003948 struct kvm_userspace_memory_region kvm_userspace_mem;
3949 int r = 0;
3950
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003951 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003952 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003953 goto out;
3954 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
3955 kvm_userspace_mem.flags = 0;
3956 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
3957 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09003958 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003959 if (r)
3960 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003961
Xiao Guangrong44841412012-09-07 14:14:20 +08003962 page = gfn_to_page(kvm, 0xfee00);
3963 if (is_error_page(page)) {
3964 r = -EFAULT;
3965 goto out;
3966 }
3967
3968 kvm->arch.apic_access_page = page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003969out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003970 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003971 return r;
3972}
3973
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003974static int alloc_identity_pagetable(struct kvm *kvm)
3975{
Xiao Guangrong44841412012-09-07 14:14:20 +08003976 struct page *page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003977 struct kvm_userspace_memory_region kvm_userspace_mem;
3978 int r = 0;
3979
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003980 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003981 if (kvm->arch.ept_identity_pagetable)
3982 goto out;
3983 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
3984 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003985 kvm_userspace_mem.guest_phys_addr =
3986 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003987 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09003988 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003989 if (r)
3990 goto out;
3991
Xiao Guangrong44841412012-09-07 14:14:20 +08003992 page = gfn_to_page(kvm, kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
3993 if (is_error_page(page)) {
3994 r = -EFAULT;
3995 goto out;
3996 }
3997
3998 kvm->arch.ept_identity_pagetable = page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003999out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004000 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004001 return r;
4002}
4003
Sheng Yang2384d2b2008-01-17 15:14:33 +08004004static void allocate_vpid(struct vcpu_vmx *vmx)
4005{
4006 int vpid;
4007
4008 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02004009 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004010 return;
4011 spin_lock(&vmx_vpid_lock);
4012 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
4013 if (vpid < VMX_NR_VPIDS) {
4014 vmx->vpid = vpid;
4015 __set_bit(vpid, vmx_vpid_bitmap);
4016 }
4017 spin_unlock(&vmx_vpid_lock);
4018}
4019
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004020static void free_vpid(struct vcpu_vmx *vmx)
4021{
4022 if (!enable_vpid)
4023 return;
4024 spin_lock(&vmx_vpid_lock);
4025 if (vmx->vpid != 0)
4026 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
4027 spin_unlock(&vmx_vpid_lock);
4028}
4029
Yang Zhang8d146952013-01-25 10:18:50 +08004030#define MSR_TYPE_R 1
4031#define MSR_TYPE_W 2
4032static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4033 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004034{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004035 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004036
4037 if (!cpu_has_vmx_msr_bitmap())
4038 return;
4039
4040 /*
4041 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4042 * have the write-low and read-high bitmap offsets the wrong way round.
4043 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4044 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004045 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004046 if (type & MSR_TYPE_R)
4047 /* read-low */
4048 __clear_bit(msr, msr_bitmap + 0x000 / f);
4049
4050 if (type & MSR_TYPE_W)
4051 /* write-low */
4052 __clear_bit(msr, msr_bitmap + 0x800 / f);
4053
Sheng Yang25c5f222008-03-28 13:18:56 +08004054 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4055 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004056 if (type & MSR_TYPE_R)
4057 /* read-high */
4058 __clear_bit(msr, msr_bitmap + 0x400 / f);
4059
4060 if (type & MSR_TYPE_W)
4061 /* write-high */
4062 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4063
4064 }
4065}
4066
4067static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4068 u32 msr, int type)
4069{
4070 int f = sizeof(unsigned long);
4071
4072 if (!cpu_has_vmx_msr_bitmap())
4073 return;
4074
4075 /*
4076 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4077 * have the write-low and read-high bitmap offsets the wrong way round.
4078 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4079 */
4080 if (msr <= 0x1fff) {
4081 if (type & MSR_TYPE_R)
4082 /* read-low */
4083 __set_bit(msr, msr_bitmap + 0x000 / f);
4084
4085 if (type & MSR_TYPE_W)
4086 /* write-low */
4087 __set_bit(msr, msr_bitmap + 0x800 / f);
4088
4089 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4090 msr &= 0x1fff;
4091 if (type & MSR_TYPE_R)
4092 /* read-high */
4093 __set_bit(msr, msr_bitmap + 0x400 / f);
4094
4095 if (type & MSR_TYPE_W)
4096 /* write-high */
4097 __set_bit(msr, msr_bitmap + 0xc00 / f);
4098
Sheng Yang25c5f222008-03-28 13:18:56 +08004099 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004100}
4101
Avi Kivity58972972009-02-24 22:26:47 +02004102static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4103{
4104 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004105 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4106 msr, MSR_TYPE_R | MSR_TYPE_W);
4107 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4108 msr, MSR_TYPE_R | MSR_TYPE_W);
4109}
4110
4111static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
4112{
4113 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4114 msr, MSR_TYPE_R);
4115 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4116 msr, MSR_TYPE_R);
4117}
4118
4119static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
4120{
4121 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4122 msr, MSR_TYPE_R);
4123 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4124 msr, MSR_TYPE_R);
4125}
4126
4127static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
4128{
4129 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4130 msr, MSR_TYPE_W);
4131 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4132 msr, MSR_TYPE_W);
Avi Kivity58972972009-02-24 22:26:47 +02004133}
4134
Yang Zhang01e439b2013-04-11 19:25:12 +08004135static int vmx_vm_has_apicv(struct kvm *kvm)
4136{
4137 return enable_apicv && irqchip_in_kernel(kvm);
4138}
4139
Avi Kivity6aa8b732006-12-10 02:21:36 -08004140/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004141 * Send interrupt to vcpu via posted interrupt way.
4142 * 1. If target vcpu is running(non-root mode), send posted interrupt
4143 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4144 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4145 * interrupt from PIR in next vmentry.
4146 */
4147static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4148{
4149 struct vcpu_vmx *vmx = to_vmx(vcpu);
4150 int r;
4151
4152 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4153 return;
4154
4155 r = pi_test_and_set_on(&vmx->pi_desc);
4156 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang, Yang Z6ffbbbb2013-04-17 23:11:54 -03004157#ifdef CONFIG_SMP
Yang Zhanga20ed542013-04-11 19:25:15 +08004158 if (!r && (vcpu->mode == IN_GUEST_MODE))
4159 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4160 POSTED_INTR_VECTOR);
4161 else
Zhang, Yang Z6ffbbbb2013-04-17 23:11:54 -03004162#endif
Yang Zhanga20ed542013-04-11 19:25:15 +08004163 kvm_vcpu_kick(vcpu);
4164}
4165
4166static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4167{
4168 struct vcpu_vmx *vmx = to_vmx(vcpu);
4169
4170 if (!pi_test_and_clear_on(&vmx->pi_desc))
4171 return;
4172
4173 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4174}
4175
4176static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu)
4177{
4178 return;
4179}
4180
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004182 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4183 * will not change in the lifetime of the guest.
4184 * Note that host-state that does change is set elsewhere. E.g., host-state
4185 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4186 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004187static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004188{
4189 u32 low32, high32;
4190 unsigned long tmpl;
4191 struct desc_ptr dt;
4192
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07004193 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004194 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
4195 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4196
4197 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004198#ifdef CONFIG_X86_64
4199 /*
4200 * Load null selectors, so we can avoid reloading them in
4201 * __vmx_load_host_state(), in case userspace uses the null selectors
4202 * too (the expected case).
4203 */
4204 vmcs_write16(HOST_DS_SELECTOR, 0);
4205 vmcs_write16(HOST_ES_SELECTOR, 0);
4206#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004207 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4208 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004209#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004210 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4211 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4212
4213 native_store_idt(&dt);
4214 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004215 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004216
Avi Kivity83287ea422012-09-16 15:10:57 +03004217 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004218
4219 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4220 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4221 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4222 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4223
4224 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4225 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4226 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4227 }
4228}
4229
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004230static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4231{
4232 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4233 if (enable_ept)
4234 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004235 if (is_guest_mode(&vmx->vcpu))
4236 vmx->vcpu.arch.cr4_guest_owned_bits &=
4237 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004238 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4239}
4240
Yang Zhang01e439b2013-04-11 19:25:12 +08004241static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4242{
4243 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4244
4245 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4246 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
4247 return pin_based_exec_ctrl;
4248}
4249
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004250static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4251{
4252 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
Paolo Bonzinid16c2932014-02-21 10:36:37 +01004253
4254 if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4255 exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4256
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004257 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
4258 exec_control &= ~CPU_BASED_TPR_SHADOW;
4259#ifdef CONFIG_X86_64
4260 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4261 CPU_BASED_CR8_LOAD_EXITING;
4262#endif
4263 }
4264 if (!enable_ept)
4265 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4266 CPU_BASED_CR3_LOAD_EXITING |
4267 CPU_BASED_INVLPG_EXITING;
4268 return exec_control;
4269}
4270
4271static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4272{
4273 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
4274 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4275 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4276 if (vmx->vpid == 0)
4277 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4278 if (!enable_ept) {
4279 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4280 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00004281 /* Enable INVPCID for non-ept guests may cause performance regression. */
4282 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004283 }
4284 if (!enable_unrestricted_guest)
4285 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4286 if (!ple_gap)
4287 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Yang Zhangc7c9c562013-01-25 10:18:51 +08004288 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4289 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4290 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004291 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03004292 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4293 (handle_vmptrld).
4294 We can NOT enable shadow_vmcs here because we don't have yet
4295 a current VMCS12
4296 */
4297 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004298 return exec_control;
4299}
4300
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004301static void ept_set_mmio_spte_mask(void)
4302{
4303 /*
4304 * EPT Misconfigurations can be generated if the value of bits 2:0
4305 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08004306 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004307 * spte.
4308 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08004309 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004310}
4311
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004312/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004313 * Sets up the vmcs for emulated real mode.
4314 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004315static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004316{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004317#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004318 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004319#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004320 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004321
Avi Kivity6aa8b732006-12-10 02:21:36 -08004322 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004323 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4324 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004325
Abel Gordon4607c2d2013-04-18 14:35:55 +03004326 if (enable_shadow_vmcs) {
4327 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
4328 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
4329 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004330 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02004331 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08004332
Avi Kivity6aa8b732006-12-10 02:21:36 -08004333 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4334
Avi Kivity6aa8b732006-12-10 02:21:36 -08004335 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08004336 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004337
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004338 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004339
Sheng Yang83ff3b92007-11-21 14:33:25 +08004340 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004341 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4342 vmx_secondary_exec_control(vmx));
Sheng Yang83ff3b92007-11-21 14:33:25 +08004343 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004344
Yang Zhang01e439b2013-04-11 19:25:12 +08004345 if (vmx_vm_has_apicv(vmx->vcpu.kvm)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004346 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4347 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4348 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4349 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4350
4351 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004352
4353 vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
4354 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004355 }
4356
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004357 if (ple_gap) {
4358 vmcs_write32(PLE_GAP, ple_gap);
4359 vmcs_write32(PLE_WINDOW, ple_window);
4360 }
4361
Xiao Guangrongc3707952011-07-12 03:28:04 +08004362 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4363 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004364 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4365
Avi Kivity9581d442010-10-19 16:46:55 +02004366 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4367 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004368 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004369#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004370 rdmsrl(MSR_FS_BASE, a);
4371 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
4372 rdmsrl(MSR_GS_BASE, a);
4373 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
4374#else
4375 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4376 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
4377#endif
4378
Eddie Dong2cc51562007-05-21 07:28:09 +03004379 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4380 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004381 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03004382 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004383 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004384
Sheng Yang468d4722008-10-09 16:01:55 +08004385 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004386 u32 msr_low, msr_high;
4387 u64 host_pat;
Sheng Yang468d4722008-10-09 16:01:55 +08004388 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
4389 host_pat = msr_low | ((u64) msr_high << 32);
4390 /* Write the default value follow host pat */
4391 vmcs_write64(GUEST_IA32_PAT, host_pat);
4392 /* Keep arch.pat sync with GUEST_IA32_PAT */
4393 vmx->vcpu.arch.pat = host_pat;
4394 }
4395
Avi Kivity6aa8b732006-12-10 02:21:36 -08004396 for (i = 0; i < NR_VMX_MSR; ++i) {
4397 u32 index = vmx_msr_index[i];
4398 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004399 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004400
4401 if (rdmsr_safe(index, &data_low, &data_high) < 0)
4402 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08004403 if (wrmsr_safe(index, data_low, data_high) < 0)
4404 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03004405 vmx->guest_msrs[j].index = i;
4406 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02004407 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004408 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004410
Gleb Natapov2961e8762013-11-25 15:37:13 +02004411
4412 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004413
4414 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02004415 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004416
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004417 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004418 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004419
4420 return 0;
4421}
4422
Jan Kiszka57f252f2013-03-12 10:20:24 +01004423static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004424{
4425 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004426 struct msr_data apic_base_msr;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004427
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004428 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004429
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004430 vmx->soft_vnmi_blocked = 0;
4431
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004432 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004433 kvm_set_cr8(&vmx->vcpu, 0);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004434 apic_base_msr.data = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004435 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Jan Kiszka58cb6282014-01-24 16:48:44 +01004436 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4437 apic_base_msr.host_initiated = true;
4438 kvm_set_apic_base(&vmx->vcpu, &apic_base_msr);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004439
Avi Kivity2fb92db2011-04-27 19:42:18 +03004440 vmx_segment_cache_clear(vmx);
4441
Avi Kivity5706be02008-08-20 15:07:31 +03004442 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004443 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzini04b66832013-03-19 16:30:26 +01004444 vmcs_write32(GUEST_CS_BASE, 0xffff0000);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004445
4446 seg_setup(VCPU_SREG_DS);
4447 seg_setup(VCPU_SREG_ES);
4448 seg_setup(VCPU_SREG_FS);
4449 seg_setup(VCPU_SREG_GS);
4450 seg_setup(VCPU_SREG_SS);
4451
4452 vmcs_write16(GUEST_TR_SELECTOR, 0);
4453 vmcs_writel(GUEST_TR_BASE, 0);
4454 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4455 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4456
4457 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4458 vmcs_writel(GUEST_LDTR_BASE, 0);
4459 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4460 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4461
4462 vmcs_write32(GUEST_SYSENTER_CS, 0);
4463 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4464 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4465
4466 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01004467 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004468
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004469 vmcs_writel(GUEST_GDTR_BASE, 0);
4470 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4471
4472 vmcs_writel(GUEST_IDTR_BASE, 0);
4473 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4474
Anthony Liguori443381a2010-12-06 10:53:38 -06004475 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004476 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
4477 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
4478
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004479 /* Special registers */
4480 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4481
4482 setup_msrs(vmx);
4483
Avi Kivity6aa8b732006-12-10 02:21:36 -08004484 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4485
Sheng Yangf78e0e22007-10-29 09:40:42 +08004486 if (cpu_has_vmx_tpr_shadow()) {
4487 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
4488 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
4489 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09004490 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004491 vmcs_write32(TPR_THRESHOLD, 0);
4492 }
4493
4494 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4495 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004496 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004497
Yang Zhang01e439b2013-04-11 19:25:12 +08004498 if (vmx_vm_has_apicv(vcpu->kvm))
4499 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
4500
Sheng Yang2384d2b2008-01-17 15:14:33 +08004501 if (vmx->vpid != 0)
4502 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4503
Eduardo Habkostfa400522009-10-24 02:49:58 -02004504 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004505 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004506 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004507 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004508 vmx_fpu_activate(&vmx->vcpu);
4509 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004510
Gui Jianfengb9d762f2010-06-07 10:32:29 +08004511 vpid_sync_context(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512}
4513
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004514/*
4515 * In nested virtualization, check if L1 asked to exit on external interrupts.
4516 * For most existing hypervisors, this will always return true.
4517 */
4518static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4519{
4520 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4521 PIN_BASED_EXT_INTR_MASK;
4522}
4523
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004524static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
4525{
4526 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4527 PIN_BASED_NMI_EXITING;
4528}
4529
Jan Kiszkac9a79532014-03-07 20:03:15 +01004530static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004531{
4532 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02004533
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004534 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4535 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
4536 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4537}
4538
Jan Kiszkac9a79532014-03-07 20:03:15 +01004539static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004540{
4541 u32 cpu_based_vm_exec_control;
4542
Jan Kiszkac9a79532014-03-07 20:03:15 +01004543 if (!cpu_has_virtual_nmis() ||
4544 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
4545 enable_irq_window(vcpu);
4546 return;
4547 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004548
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004549 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4550 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
4551 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4552}
4553
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004554static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004555{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004556 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004557 uint32_t intr;
4558 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004559
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004560 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004561
Avi Kivityfa89a812008-09-01 15:57:51 +03004562 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004563 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004564 int inc_eip = 0;
4565 if (vcpu->arch.interrupt.soft)
4566 inc_eip = vcpu->arch.event_exit_inst_len;
4567 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004568 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004569 return;
4570 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004571 intr = irq | INTR_INFO_VALID_MASK;
4572 if (vcpu->arch.interrupt.soft) {
4573 intr |= INTR_TYPE_SOFT_INTR;
4574 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4575 vmx->vcpu.arch.event_exit_inst_len);
4576 } else
4577 intr |= INTR_TYPE_EXT_INTR;
4578 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03004579}
4580
Sheng Yangf08864b2008-05-15 18:23:25 +08004581static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4582{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004583 struct vcpu_vmx *vmx = to_vmx(vcpu);
4584
Nadav Har'El0b6ac342011-05-25 23:13:36 +03004585 if (is_guest_mode(vcpu))
4586 return;
4587
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004588 if (!cpu_has_virtual_nmis()) {
4589 /*
4590 * Tracking the NMI-blocked state in software is built upon
4591 * finding the next open IRQ window. This, in turn, depends on
4592 * well-behaving guests: They have to keep IRQs disabled at
4593 * least as long as the NMI handler runs. Otherwise we may
4594 * cause NMI nesting, maybe breaking the guest. But as this is
4595 * highly unlikely, we can live with the residual risk.
4596 */
4597 vmx->soft_vnmi_blocked = 1;
4598 vmx->vnmi_blocked_time = 0;
4599 }
4600
Jan Kiszka487b3912008-09-26 09:30:56 +02004601 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02004602 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004603 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004604 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004605 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004606 return;
4607 }
Sheng Yangf08864b2008-05-15 18:23:25 +08004608 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4609 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08004610}
4611
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004612static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
4613{
4614 if (!cpu_has_virtual_nmis())
4615 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02004616 if (to_vmx(vcpu)->nmi_known_unmasked)
4617 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03004618 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004619}
4620
4621static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4622{
4623 struct vcpu_vmx *vmx = to_vmx(vcpu);
4624
4625 if (!cpu_has_virtual_nmis()) {
4626 if (vmx->soft_vnmi_blocked != masked) {
4627 vmx->soft_vnmi_blocked = masked;
4628 vmx->vnmi_blocked_time = 0;
4629 }
4630 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02004631 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004632 if (masked)
4633 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4634 GUEST_INTR_STATE_NMI);
4635 else
4636 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4637 GUEST_INTR_STATE_NMI);
4638 }
4639}
4640
Jan Kiszka2505dc92013-04-14 12:12:47 +02004641static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
4642{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004643 if (to_vmx(vcpu)->nested.nested_run_pending)
4644 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004645
Jan Kiszka2505dc92013-04-14 12:12:47 +02004646 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
4647 return 0;
4648
4649 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4650 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4651 | GUEST_INTR_STATE_NMI));
4652}
4653
Gleb Natapov78646122009-03-23 12:12:11 +02004654static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4655{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004656 return (!to_vmx(vcpu)->nested.nested_run_pending &&
4657 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03004658 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4659 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02004660}
4661
Izik Eiduscbc94022007-10-25 00:29:55 +02004662static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4663{
4664 int ret;
4665 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08004666 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02004667 .guest_phys_addr = addr,
4668 .memory_size = PAGE_SIZE * 3,
4669 .flags = 0,
4670 };
4671
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09004672 ret = kvm_set_memory_region(kvm, &tss_mem);
Izik Eiduscbc94022007-10-25 00:29:55 +02004673 if (ret)
4674 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004675 kvm->arch.tss_addr = addr;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004676 if (!init_rmode_tss(kvm))
4677 return -ENOMEM;
4678
Izik Eiduscbc94022007-10-25 00:29:55 +02004679 return 0;
4680}
4681
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004682static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004683{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004684 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004685 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004686 /*
4687 * Update instruction length as we may reinject the exception
4688 * from user space while in guest debugging mode.
4689 */
4690 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4691 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004692 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004693 return false;
4694 /* fall through */
4695 case DB_VECTOR:
4696 if (vcpu->guest_debug &
4697 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4698 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004699 /* fall through */
4700 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004701 case OF_VECTOR:
4702 case BR_VECTOR:
4703 case UD_VECTOR:
4704 case DF_VECTOR:
4705 case SS_VECTOR:
4706 case GP_VECTOR:
4707 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004708 return true;
4709 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004710 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004711 return false;
4712}
4713
4714static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4715 int vec, u32 err_code)
4716{
4717 /*
4718 * Instruction with address size override prefix opcode 0x67
4719 * Cause the #SS fault with 0 error code in VM86 mode.
4720 */
4721 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
4722 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
4723 if (vcpu->arch.halt_request) {
4724 vcpu->arch.halt_request = 0;
4725 return kvm_emulate_halt(vcpu);
4726 }
4727 return 1;
4728 }
4729 return 0;
4730 }
4731
4732 /*
4733 * Forward all other exceptions that are valid in real mode.
4734 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4735 * the required debugging infrastructure rework.
4736 */
4737 kvm_queue_exception(vcpu, vec);
4738 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004739}
4740
Andi Kleena0861c02009-06-08 17:37:09 +08004741/*
4742 * Trigger machine check on the host. We assume all the MSRs are already set up
4743 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4744 * We pass a fake environment to the machine check handler because we want
4745 * the guest to be always treated like user space, no matter what context
4746 * it used internally.
4747 */
4748static void kvm_machine_check(void)
4749{
4750#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4751 struct pt_regs regs = {
4752 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4753 .flags = X86_EFLAGS_IF,
4754 };
4755
4756 do_machine_check(&regs, 0);
4757#endif
4758}
4759
Avi Kivity851ba692009-08-24 11:10:17 +03004760static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004761{
4762 /* already handled by vcpu_run */
4763 return 1;
4764}
4765
Avi Kivity851ba692009-08-24 11:10:17 +03004766static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004767{
Avi Kivity1155f762007-11-22 11:30:47 +02004768 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004769 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004770 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004771 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004772 u32 vect_info;
4773 enum emulation_result er;
4774
Avi Kivity1155f762007-11-22 11:30:47 +02004775 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02004776 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004777
Andi Kleena0861c02009-06-08 17:37:09 +08004778 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03004779 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004780
Jan Kiszkae4a41882008-09-26 09:30:46 +02004781 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02004782 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004783
4784 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03004785 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004786 return 1;
4787 }
4788
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004789 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01004790 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004791 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02004792 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004793 return 1;
4794 }
4795
Avi Kivity6aa8b732006-12-10 02:21:36 -08004796 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004797 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004798 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004799
4800 /*
4801 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4802 * MMIO, it is better to report an internal error.
4803 * See the comments in vmx_handle_exit.
4804 */
4805 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4806 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4807 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4808 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
4809 vcpu->run->internal.ndata = 2;
4810 vcpu->run->internal.data[0] = vect_info;
4811 vcpu->run->internal.data[1] = intr_info;
4812 return 0;
4813 }
4814
Avi Kivity6aa8b732006-12-10 02:21:36 -08004815 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08004816 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02004817 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004818 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004819 trace_kvm_page_fault(cr2, error_code);
4820
Gleb Natapov3298b752009-05-11 13:35:46 +03004821 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03004822 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01004823 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004824 }
4825
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004826 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004827
4828 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4829 return handle_rmode_exception(vcpu, ex_no, error_code);
4830
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004831 switch (ex_no) {
4832 case DB_VECTOR:
4833 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4834 if (!(vcpu->guest_debug &
4835 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01004836 vcpu->arch.dr6 &= ~15;
4837 vcpu->arch.dr6 |= dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004838 kvm_queue_exception(vcpu, DB_VECTOR);
4839 return 1;
4840 }
4841 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4842 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4843 /* fall through */
4844 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004845 /*
4846 * Update instruction length as we may reinject #BP from
4847 * user space while in guest debugging mode. Reading it for
4848 * #DB as well causes no harm, it is not used in that case.
4849 */
4850 vmx->vcpu.arch.event_exit_inst_len =
4851 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004852 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004853 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004854 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4855 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004856 break;
4857 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004858 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4859 kvm_run->ex.exception = ex_no;
4860 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004861 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004862 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004863 return 0;
4864}
4865
Avi Kivity851ba692009-08-24 11:10:17 +03004866static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004868 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004869 return 1;
4870}
4871
Avi Kivity851ba692009-08-24 11:10:17 +03004872static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004873{
Avi Kivity851ba692009-08-24 11:10:17 +03004874 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08004875 return 0;
4876}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004877
Avi Kivity851ba692009-08-24 11:10:17 +03004878static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004879{
He, Qingbfdaab02007-09-12 14:18:28 +08004880 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01004881 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004882 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004883
He, Qingbfdaab02007-09-12 14:18:28 +08004884 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02004885 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004886 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004887
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004888 ++vcpu->stat.io_exits;
4889
4890 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01004891 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004892
4893 port = exit_qualification >> 16;
4894 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01004895 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004896
4897 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004898}
4899
Ingo Molnar102d8322007-02-19 14:37:47 +02004900static void
4901vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4902{
4903 /*
4904 * Patch in the VMCALL instruction:
4905 */
4906 hypercall[0] = 0x0f;
4907 hypercall[1] = 0x01;
4908 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004909}
4910
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02004911static bool nested_cr0_valid(struct vmcs12 *vmcs12, unsigned long val)
4912{
4913 unsigned long always_on = VMXON_CR0_ALWAYSON;
4914
4915 if (nested_vmx_secondary_ctls_high &
4916 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4917 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4918 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
4919 return (val & always_on) == always_on;
4920}
4921
Guo Chao0fa06072012-06-28 15:16:19 +08004922/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004923static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4924{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004925 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004926 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4927 unsigned long orig_val = val;
4928
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004929 /*
4930 * We get here when L2 changed cr0 in a way that did not change
4931 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004932 * but did change L0 shadowed bits. So we first calculate the
4933 * effective cr0 value that L1 would like to write into the
4934 * hardware. It consists of the L2-owned bits from the new
4935 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004936 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004937 val = (val & ~vmcs12->cr0_guest_host_mask) |
4938 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4939
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02004940 if (!nested_cr0_valid(vmcs12, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004941 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004942
4943 if (kvm_set_cr0(vcpu, val))
4944 return 1;
4945 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004946 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004947 } else {
4948 if (to_vmx(vcpu)->nested.vmxon &&
4949 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
4950 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004951 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004952 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004953}
4954
4955static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4956{
4957 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004958 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4959 unsigned long orig_val = val;
4960
4961 /* analogously to handle_set_cr0 */
4962 val = (val & ~vmcs12->cr4_guest_host_mask) |
4963 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4964 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004965 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004966 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004967 return 0;
4968 } else
4969 return kvm_set_cr4(vcpu, val);
4970}
4971
4972/* called to set cr0 as approriate for clts instruction exit. */
4973static void handle_clts(struct kvm_vcpu *vcpu)
4974{
4975 if (is_guest_mode(vcpu)) {
4976 /*
4977 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
4978 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
4979 * just pretend it's off (also in arch.cr0 for fpu_activate).
4980 */
4981 vmcs_writel(CR0_READ_SHADOW,
4982 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
4983 vcpu->arch.cr0 &= ~X86_CR0_TS;
4984 } else
4985 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
4986}
4987
Avi Kivity851ba692009-08-24 11:10:17 +03004988static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004989{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004990 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004991 int cr;
4992 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004993 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004994
He, Qingbfdaab02007-09-12 14:18:28 +08004995 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996 cr = exit_qualification & 15;
4997 reg = (exit_qualification >> 8) & 15;
4998 switch ((exit_qualification >> 4) & 3) {
4999 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005000 val = kvm_register_read(vcpu, reg);
5001 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005002 switch (cr) {
5003 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005004 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005005 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005006 return 1;
5007 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03005008 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005009 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005010 return 1;
5011 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005012 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005013 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005014 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005015 case 8: {
5016 u8 cr8_prev = kvm_get_cr8(vcpu);
5017 u8 cr8 = kvm_register_read(vcpu, reg);
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005018 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005019 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005020 if (irqchip_in_kernel(vcpu->kvm))
5021 return 1;
5022 if (cr8_prev <= cr8)
5023 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005024 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005025 return 0;
5026 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005027 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005028 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005029 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005030 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005031 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005032 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005033 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005034 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005035 case 1: /*mov from cr*/
5036 switch (cr) {
5037 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005038 val = kvm_read_cr3(vcpu);
5039 kvm_register_write(vcpu, reg, val);
5040 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005041 skip_emulated_instruction(vcpu);
5042 return 1;
5043 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005044 val = kvm_get_cr8(vcpu);
5045 kvm_register_write(vcpu, reg, val);
5046 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005047 skip_emulated_instruction(vcpu);
5048 return 1;
5049 }
5050 break;
5051 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005052 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005053 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005054 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005055
5056 skip_emulated_instruction(vcpu);
5057 return 1;
5058 default:
5059 break;
5060 }
Avi Kivity851ba692009-08-24 11:10:17 +03005061 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005062 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005063 (int)(exit_qualification >> 4) & 3, cr);
5064 return 0;
5065}
5066
Avi Kivity851ba692009-08-24 11:10:17 +03005067static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005068{
He, Qingbfdaab02007-09-12 14:18:28 +08005069 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005070 int dr, reg;
5071
Jan Kiszkaf2483412010-01-20 18:20:20 +01005072 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005073 if (!kvm_require_cpl(vcpu, 0))
5074 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005075 dr = vmcs_readl(GUEST_DR7);
5076 if (dr & DR7_GD) {
5077 /*
5078 * As the vm-exit takes precedence over the debug trap, we
5079 * need to emulate the latter, either for the host or the
5080 * guest debugging itself.
5081 */
5082 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005083 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
5084 vcpu->run->debug.arch.dr7 = dr;
5085 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005086 vmcs_readl(GUEST_CS_BASE) +
5087 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03005088 vcpu->run->debug.arch.exception = DB_VECTOR;
5089 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005090 return 0;
5091 } else {
5092 vcpu->arch.dr7 &= ~DR7_GD;
5093 vcpu->arch.dr6 |= DR6_BD;
5094 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
5095 kvm_queue_exception(vcpu, DB_VECTOR);
5096 return 1;
5097 }
5098 }
5099
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005100 if (vcpu->guest_debug == 0) {
5101 u32 cpu_based_vm_exec_control;
5102
5103 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5104 cpu_based_vm_exec_control &= ~CPU_BASED_MOV_DR_EXITING;
5105 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5106
5107 /*
5108 * No more DR vmexits; force a reload of the debug registers
5109 * and reenter on this instruction. The next vmexit will
5110 * retrieve the full state of the debug registers.
5111 */
5112 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5113 return 1;
5114 }
5115
He, Qingbfdaab02007-09-12 14:18:28 +08005116 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005117 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5118 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5119 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005120 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005121
5122 if (kvm_get_dr(vcpu, dr, &val))
5123 return 1;
5124 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005125 } else
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005126 if (kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]))
5127 return 1;
5128
Avi Kivity6aa8b732006-12-10 02:21:36 -08005129 skip_emulated_instruction(vcpu);
5130 return 1;
5131}
5132
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005133static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5134{
5135 return vcpu->arch.dr6;
5136}
5137
5138static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5139{
5140}
5141
Paolo Bonzini81908bf2014-02-21 10:32:27 +01005142static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5143{
5144 u32 cpu_based_vm_exec_control;
5145
5146 get_debugreg(vcpu->arch.db[0], 0);
5147 get_debugreg(vcpu->arch.db[1], 1);
5148 get_debugreg(vcpu->arch.db[2], 2);
5149 get_debugreg(vcpu->arch.db[3], 3);
5150 get_debugreg(vcpu->arch.dr6, 6);
5151 vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5152
5153 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
5154
5155 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5156 cpu_based_vm_exec_control |= CPU_BASED_MOV_DR_EXITING;
5157 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5158}
5159
Gleb Natapov020df072010-04-13 10:05:23 +03005160static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5161{
5162 vmcs_writel(GUEST_DR7, val);
5163}
5164
Avi Kivity851ba692009-08-24 11:10:17 +03005165static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005166{
Avi Kivity06465c52007-02-28 20:46:53 +02005167 kvm_emulate_cpuid(vcpu);
5168 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005169}
5170
Avi Kivity851ba692009-08-24 11:10:17 +03005171static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005172{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005173 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08005174 u64 data;
5175
5176 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02005177 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005178 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005179 return 1;
5180 }
5181
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005182 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005183
Avi Kivity6aa8b732006-12-10 02:21:36 -08005184 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005185 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
5186 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005187 skip_emulated_instruction(vcpu);
5188 return 1;
5189}
5190
Avi Kivity851ba692009-08-24 11:10:17 +03005191static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005192{
Will Auld8fe8ab42012-11-29 12:42:12 -08005193 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005194 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5195 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5196 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005197
Will Auld8fe8ab42012-11-29 12:42:12 -08005198 msr.data = data;
5199 msr.index = ecx;
5200 msr.host_initiated = false;
5201 if (vmx_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005202 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005203 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005204 return 1;
5205 }
5206
Avi Kivity59200272010-01-25 19:47:02 +02005207 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005208 skip_emulated_instruction(vcpu);
5209 return 1;
5210}
5211
Avi Kivity851ba692009-08-24 11:10:17 +03005212static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005213{
Avi Kivity3842d132010-07-27 12:30:24 +03005214 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005215 return 1;
5216}
5217
Avi Kivity851ba692009-08-24 11:10:17 +03005218static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005219{
Eddie Dong85f455f2007-07-06 12:20:49 +03005220 u32 cpu_based_vm_exec_control;
5221
5222 /* clear pending irq */
5223 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5224 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5225 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005226
Avi Kivity3842d132010-07-27 12:30:24 +03005227 kvm_make_request(KVM_REQ_EVENT, vcpu);
5228
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005229 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005230
Dor Laorc1150d82007-01-05 16:36:24 -08005231 /*
5232 * If the user space waits to inject interrupts, exit as soon as
5233 * possible
5234 */
Gleb Natapov80618232009-04-21 17:44:56 +03005235 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03005236 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03005237 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03005238 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08005239 return 0;
5240 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005241 return 1;
5242}
5243
Avi Kivity851ba692009-08-24 11:10:17 +03005244static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005245{
5246 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03005247 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005248}
5249
Avi Kivity851ba692009-08-24 11:10:17 +03005250static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005251{
Dor Laor510043d2007-02-19 18:25:43 +02005252 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005253 kvm_emulate_hypercall(vcpu);
5254 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02005255}
5256
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005257static int handle_invd(struct kvm_vcpu *vcpu)
5258{
Andre Przywara51d8b662010-12-21 11:12:02 +01005259 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005260}
5261
Avi Kivity851ba692009-08-24 11:10:17 +03005262static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005263{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005264 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005265
5266 kvm_mmu_invlpg(vcpu, exit_qualification);
5267 skip_emulated_instruction(vcpu);
5268 return 1;
5269}
5270
Avi Kivityfee84b02011-11-10 14:57:25 +02005271static int handle_rdpmc(struct kvm_vcpu *vcpu)
5272{
5273 int err;
5274
5275 err = kvm_rdpmc(vcpu);
5276 kvm_complete_insn_gp(vcpu, err);
5277
5278 return 1;
5279}
5280
Avi Kivity851ba692009-08-24 11:10:17 +03005281static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005282{
5283 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005284 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005285 return 1;
5286}
5287
Dexuan Cui2acf9232010-06-10 11:27:12 +08005288static int handle_xsetbv(struct kvm_vcpu *vcpu)
5289{
5290 u64 new_bv = kvm_read_edx_eax(vcpu);
5291 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5292
5293 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5294 skip_emulated_instruction(vcpu);
5295 return 1;
5296}
5297
Avi Kivity851ba692009-08-24 11:10:17 +03005298static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005299{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005300 if (likely(fasteoi)) {
5301 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5302 int access_type, offset;
5303
5304 access_type = exit_qualification & APIC_ACCESS_TYPE;
5305 offset = exit_qualification & APIC_ACCESS_OFFSET;
5306 /*
5307 * Sane guest uses MOV to write EOI, with written value
5308 * not cared. So make a short-circuit here by avoiding
5309 * heavy instruction emulation.
5310 */
5311 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5312 (offset == APIC_EOI)) {
5313 kvm_lapic_set_eoi(vcpu);
5314 skip_emulated_instruction(vcpu);
5315 return 1;
5316 }
5317 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005318 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005319}
5320
Yang Zhangc7c9c562013-01-25 10:18:51 +08005321static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5322{
5323 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5324 int vector = exit_qualification & 0xff;
5325
5326 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5327 kvm_apic_set_eoi_accelerated(vcpu, vector);
5328 return 1;
5329}
5330
Yang Zhang83d4c282013-01-25 10:18:49 +08005331static int handle_apic_write(struct kvm_vcpu *vcpu)
5332{
5333 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5334 u32 offset = exit_qualification & 0xfff;
5335
5336 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5337 kvm_apic_write_nodecode(vcpu, offset);
5338 return 1;
5339}
5340
Avi Kivity851ba692009-08-24 11:10:17 +03005341static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005342{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005343 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005344 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005345 bool has_error_code = false;
5346 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005347 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005348 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005349
5350 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005351 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005352 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005353
5354 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5355
5356 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005357 if (reason == TASK_SWITCH_GATE && idt_v) {
5358 switch (type) {
5359 case INTR_TYPE_NMI_INTR:
5360 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005361 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005362 break;
5363 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005364 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005365 kvm_clear_interrupt_queue(vcpu);
5366 break;
5367 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005368 if (vmx->idt_vectoring_info &
5369 VECTORING_INFO_DELIVER_CODE_MASK) {
5370 has_error_code = true;
5371 error_code =
5372 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5373 }
5374 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005375 case INTR_TYPE_SOFT_EXCEPTION:
5376 kvm_clear_exception_queue(vcpu);
5377 break;
5378 default:
5379 break;
5380 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005381 }
Izik Eidus37817f22008-03-24 23:14:53 +02005382 tss_selector = exit_qualification;
5383
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005384 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5385 type != INTR_TYPE_EXT_INTR &&
5386 type != INTR_TYPE_NMI_INTR))
5387 skip_emulated_instruction(vcpu);
5388
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005389 if (kvm_task_switch(vcpu, tss_selector,
5390 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
5391 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03005392 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5393 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5394 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005395 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03005396 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005397
5398 /* clear all local breakpoint enable flags */
5399 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
5400
5401 /*
5402 * TODO: What about debug traps on tss switch?
5403 * Are we supposed to inject them and update dr6?
5404 */
5405
5406 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02005407}
5408
Avi Kivity851ba692009-08-24 11:10:17 +03005409static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005410{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005411 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005412 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005413 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005414 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08005415
Sheng Yangf9c617f2009-03-25 10:08:52 +08005416 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08005417
Sheng Yang14394422008-04-28 12:24:45 +08005418 gla_validity = (exit_qualification >> 7) & 0x3;
5419 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
5420 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
5421 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
5422 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08005423 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08005424 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
5425 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03005426 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5427 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03005428 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08005429 }
5430
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005431 /*
5432 * EPT violation happened while executing iret from NMI,
5433 * "blocked by NMI" bit has to be set before next VM entry.
5434 * There are errata that may cause this bit to not be set:
5435 * AAK134, BY25.
5436 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005437 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
5438 cpu_has_virtual_nmis() &&
5439 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005440 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5441
Sheng Yang14394422008-04-28 12:24:45 +08005442 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005443 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005444
5445 /* It is a write fault? */
5446 error_code = exit_qualification & (1U << 1);
Yang Zhang25d92082013-08-06 12:00:32 +03005447 /* It is a fetch fault? */
5448 error_code |= (exit_qualification & (1U << 2)) << 2;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005449 /* ept page table is present? */
5450 error_code |= (exit_qualification >> 3) & 0x1;
5451
Yang Zhang25d92082013-08-06 12:00:32 +03005452 vcpu->arch.exit_qualification = exit_qualification;
5453
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005454 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005455}
5456
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005457static u64 ept_rsvd_mask(u64 spte, int level)
5458{
5459 int i;
5460 u64 mask = 0;
5461
5462 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
5463 mask |= (1ULL << i);
5464
5465 if (level > 2)
5466 /* bits 7:3 reserved */
5467 mask |= 0xf8;
5468 else if (level == 2) {
5469 if (spte & (1ULL << 7))
5470 /* 2MB ref, bits 20:12 reserved */
5471 mask |= 0x1ff000;
5472 else
5473 /* bits 6:3 reserved */
5474 mask |= 0x78;
5475 }
5476
5477 return mask;
5478}
5479
5480static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
5481 int level)
5482{
5483 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
5484
5485 /* 010b (write-only) */
5486 WARN_ON((spte & 0x7) == 0x2);
5487
5488 /* 110b (write/execute) */
5489 WARN_ON((spte & 0x7) == 0x6);
5490
5491 /* 100b (execute-only) and value not supported by logical processor */
5492 if (!cpu_has_vmx_ept_execute_only())
5493 WARN_ON((spte & 0x7) == 0x4);
5494
5495 /* not 000b */
5496 if ((spte & 0x7)) {
5497 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
5498
5499 if (rsvd_bits != 0) {
5500 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
5501 __func__, rsvd_bits);
5502 WARN_ON(1);
5503 }
5504
5505 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
5506 u64 ept_mem_type = (spte & 0x38) >> 3;
5507
5508 if (ept_mem_type == 2 || ept_mem_type == 3 ||
5509 ept_mem_type == 7) {
5510 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
5511 __func__, ept_mem_type);
5512 WARN_ON(1);
5513 }
5514 }
5515 }
5516}
5517
Avi Kivity851ba692009-08-24 11:10:17 +03005518static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005519{
5520 u64 sptes[4];
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005521 int nr_sptes, i, ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005522 gpa_t gpa;
5523
5524 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
5525
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005526 ret = handle_mmio_page_fault_common(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005527 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005528 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
5529 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005530
5531 if (unlikely(ret == RET_MMIO_PF_INVALID))
5532 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
5533
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005534 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005535 return 1;
5536
5537 /* It is the real ept misconfig */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005538 printk(KERN_ERR "EPT: Misconfiguration.\n");
5539 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
5540
5541 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
5542
5543 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
5544 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
5545
Avi Kivity851ba692009-08-24 11:10:17 +03005546 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5547 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005548
5549 return 0;
5550}
5551
Avi Kivity851ba692009-08-24 11:10:17 +03005552static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005553{
5554 u32 cpu_based_vm_exec_control;
5555
5556 /* clear pending NMI */
5557 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5558 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
5559 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5560 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005561 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005562
5563 return 1;
5564}
5565
Mohammed Gamal80ced182009-09-01 12:48:18 +02005566static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005567{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005568 struct vcpu_vmx *vmx = to_vmx(vcpu);
5569 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005570 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02005571 u32 cpu_exec_ctrl;
5572 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005573 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005574
5575 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5576 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005577
Avi Kivityb8405c12012-06-07 17:08:48 +03005578 while (!guest_state_valid(vcpu) && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03005579 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005580 return handle_interrupt_window(&vmx->vcpu);
5581
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005582 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
5583 return 1;
5584
Gleb Natapov991eebf2013-04-11 12:10:51 +03005585 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005586
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005587 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02005588 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005589 ret = 0;
5590 goto out;
5591 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005592
Avi Kivityde5f70e2012-06-12 20:22:28 +03005593 if (err != EMULATE_DONE) {
5594 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5595 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5596 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005597 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03005598 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005599
Gleb Natapov8d76c492013-05-08 18:38:44 +03005600 if (vcpu->arch.halt_request) {
5601 vcpu->arch.halt_request = 0;
5602 ret = kvm_emulate_halt(vcpu);
5603 goto out;
5604 }
5605
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005606 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02005607 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005608 if (need_resched())
5609 schedule();
5610 }
5611
Gleb Natapov14168782013-01-21 15:36:49 +02005612 vmx->emulation_required = emulation_required(vcpu);
Mohammed Gamal80ced182009-09-01 12:48:18 +02005613out:
5614 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005615}
5616
Avi Kivity6aa8b732006-12-10 02:21:36 -08005617/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005618 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5619 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5620 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005621static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005622{
5623 skip_emulated_instruction(vcpu);
5624 kvm_vcpu_on_spin(vcpu);
5625
5626 return 1;
5627}
5628
Sheng Yang59708672009-12-15 13:29:54 +08005629static int handle_invalid_op(struct kvm_vcpu *vcpu)
5630{
5631 kvm_queue_exception(vcpu, UD_VECTOR);
5632 return 1;
5633}
5634
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005635/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005636 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
5637 * We could reuse a single VMCS for all the L2 guests, but we also want the
5638 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
5639 * allows keeping them loaded on the processor, and in the future will allow
5640 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
5641 * every entry if they never change.
5642 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
5643 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
5644 *
5645 * The following functions allocate and free a vmcs02 in this pool.
5646 */
5647
5648/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
5649static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
5650{
5651 struct vmcs02_list *item;
5652 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5653 if (item->vmptr == vmx->nested.current_vmptr) {
5654 list_move(&item->list, &vmx->nested.vmcs02_pool);
5655 return &item->vmcs02;
5656 }
5657
5658 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
5659 /* Recycle the least recently used VMCS. */
5660 item = list_entry(vmx->nested.vmcs02_pool.prev,
5661 struct vmcs02_list, list);
5662 item->vmptr = vmx->nested.current_vmptr;
5663 list_move(&item->list, &vmx->nested.vmcs02_pool);
5664 return &item->vmcs02;
5665 }
5666
5667 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02005668 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005669 if (!item)
5670 return NULL;
5671 item->vmcs02.vmcs = alloc_vmcs();
5672 if (!item->vmcs02.vmcs) {
5673 kfree(item);
5674 return NULL;
5675 }
5676 loaded_vmcs_init(&item->vmcs02);
5677 item->vmptr = vmx->nested.current_vmptr;
5678 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
5679 vmx->nested.vmcs02_num++;
5680 return &item->vmcs02;
5681}
5682
5683/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
5684static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
5685{
5686 struct vmcs02_list *item;
5687 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5688 if (item->vmptr == vmptr) {
5689 free_loaded_vmcs(&item->vmcs02);
5690 list_del(&item->list);
5691 kfree(item);
5692 vmx->nested.vmcs02_num--;
5693 return;
5694 }
5695}
5696
5697/*
5698 * Free all VMCSs saved for this vcpu, except the one pointed by
5699 * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
5700 * currently used, if running L2), and vmcs01 when running L2.
5701 */
5702static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
5703{
5704 struct vmcs02_list *item, *n;
5705 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
5706 if (vmx->loaded_vmcs != &item->vmcs02)
5707 free_loaded_vmcs(&item->vmcs02);
5708 list_del(&item->list);
5709 kfree(item);
5710 }
5711 vmx->nested.vmcs02_num = 0;
5712
5713 if (vmx->loaded_vmcs != &vmx->vmcs01)
5714 free_loaded_vmcs(&vmx->vmcs01);
5715}
5716
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08005717/*
5718 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
5719 * set the success or error code of an emulated VMX instruction, as specified
5720 * by Vol 2B, VMX Instruction Reference, "Conventions".
5721 */
5722static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
5723{
5724 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
5725 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5726 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
5727}
5728
5729static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
5730{
5731 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5732 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
5733 X86_EFLAGS_SF | X86_EFLAGS_OF))
5734 | X86_EFLAGS_CF);
5735}
5736
Abel Gordon145c28d2013-04-18 14:36:55 +03005737static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08005738 u32 vm_instruction_error)
5739{
5740 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
5741 /*
5742 * failValid writes the error number to the current VMCS, which
5743 * can't be done there isn't a current VMCS.
5744 */
5745 nested_vmx_failInvalid(vcpu);
5746 return;
5747 }
5748 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5749 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5750 X86_EFLAGS_SF | X86_EFLAGS_OF))
5751 | X86_EFLAGS_ZF);
5752 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
5753 /*
5754 * We don't need to force a shadow sync because
5755 * VM_INSTRUCTION_ERROR is not shadowed
5756 */
5757}
Abel Gordon145c28d2013-04-18 14:36:55 +03005758
Jan Kiszkaf41245002014-03-07 20:03:13 +01005759static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
5760{
5761 struct vcpu_vmx *vmx =
5762 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
5763
5764 vmx->nested.preemption_timer_expired = true;
5765 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
5766 kvm_vcpu_kick(&vmx->vcpu);
5767
5768 return HRTIMER_NORESTART;
5769}
5770
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005771/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005772 * Emulate the VMXON instruction.
5773 * Currently, we just remember that VMX is active, and do not save or even
5774 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
5775 * do not currently need to store anything in that guest-allocated memory
5776 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
5777 * argument is different from the VMXON pointer (which the spec says they do).
5778 */
5779static int handle_vmon(struct kvm_vcpu *vcpu)
5780{
5781 struct kvm_segment cs;
5782 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03005783 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08005784 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
5785 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005786
5787 /* The Intel VMX Instruction Reference lists a bunch of bits that
5788 * are prerequisite to running VMXON, most notably cr4.VMXE must be
5789 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
5790 * Otherwise, we should fail with #UD. We test these now:
5791 */
5792 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
5793 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
5794 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5795 kvm_queue_exception(vcpu, UD_VECTOR);
5796 return 1;
5797 }
5798
5799 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5800 if (is_long_mode(vcpu) && !cs.l) {
5801 kvm_queue_exception(vcpu, UD_VECTOR);
5802 return 1;
5803 }
5804
5805 if (vmx_get_cpl(vcpu)) {
5806 kvm_inject_gp(vcpu, 0);
5807 return 1;
5808 }
Abel Gordon145c28d2013-04-18 14:36:55 +03005809 if (vmx->nested.vmxon) {
5810 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
5811 skip_emulated_instruction(vcpu);
5812 return 1;
5813 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08005814
5815 if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
5816 != VMXON_NEEDED_FEATURES) {
5817 kvm_inject_gp(vcpu, 0);
5818 return 1;
5819 }
5820
Abel Gordon8de48832013-04-18 14:37:25 +03005821 if (enable_shadow_vmcs) {
5822 shadow_vmcs = alloc_vmcs();
5823 if (!shadow_vmcs)
5824 return -ENOMEM;
5825 /* mark vmcs as shadow */
5826 shadow_vmcs->revision_id |= (1u << 31);
5827 /* init shadow vmcs */
5828 vmcs_clear(shadow_vmcs);
5829 vmx->nested.current_shadow_vmcs = shadow_vmcs;
5830 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005831
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005832 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
5833 vmx->nested.vmcs02_num = 0;
5834
Jan Kiszkaf41245002014-03-07 20:03:13 +01005835 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
5836 HRTIMER_MODE_REL);
5837 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
5838
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005839 vmx->nested.vmxon = true;
5840
5841 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08005842 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005843 return 1;
5844}
5845
5846/*
5847 * Intel's VMX Instruction Reference specifies a common set of prerequisites
5848 * for running VMX instructions (except VMXON, whose prerequisites are
5849 * slightly different). It also specifies what exception to inject otherwise.
5850 */
5851static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
5852{
5853 struct kvm_segment cs;
5854 struct vcpu_vmx *vmx = to_vmx(vcpu);
5855
5856 if (!vmx->nested.vmxon) {
5857 kvm_queue_exception(vcpu, UD_VECTOR);
5858 return 0;
5859 }
5860
5861 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5862 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
5863 (is_long_mode(vcpu) && !cs.l)) {
5864 kvm_queue_exception(vcpu, UD_VECTOR);
5865 return 0;
5866 }
5867
5868 if (vmx_get_cpl(vcpu)) {
5869 kvm_inject_gp(vcpu, 0);
5870 return 0;
5871 }
5872
5873 return 1;
5874}
5875
Abel Gordone7953d72013-04-18 14:37:55 +03005876static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
5877{
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03005878 u32 exec_control;
Abel Gordon012f83c2013-04-18 14:39:25 +03005879 if (enable_shadow_vmcs) {
5880 if (vmx->nested.current_vmcs12 != NULL) {
5881 /* copy to memory all shadowed fields in case
5882 they were modified */
5883 copy_shadow_to_vmcs12(vmx);
5884 vmx->nested.sync_shadow_vmcs = false;
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03005885 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5886 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
5887 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
5888 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03005889 }
5890 }
Abel Gordone7953d72013-04-18 14:37:55 +03005891 kunmap(vmx->nested.current_vmcs12_page);
5892 nested_release_page(vmx->nested.current_vmcs12_page);
5893}
5894
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005895/*
5896 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
5897 * just stops using VMX.
5898 */
5899static void free_nested(struct vcpu_vmx *vmx)
5900{
5901 if (!vmx->nested.vmxon)
5902 return;
5903 vmx->nested.vmxon = false;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03005904 if (vmx->nested.current_vmptr != -1ull) {
Abel Gordone7953d72013-04-18 14:37:55 +03005905 nested_release_vmcs12(vmx);
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03005906 vmx->nested.current_vmptr = -1ull;
5907 vmx->nested.current_vmcs12 = NULL;
5908 }
Abel Gordone7953d72013-04-18 14:37:55 +03005909 if (enable_shadow_vmcs)
5910 free_vmcs(vmx->nested.current_shadow_vmcs);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005911 /* Unpin physical memory we referred to in current vmcs02 */
5912 if (vmx->nested.apic_access_page) {
5913 nested_release_page(vmx->nested.apic_access_page);
5914 vmx->nested.apic_access_page = 0;
5915 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005916
5917 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005918}
5919
5920/* Emulate the VMXOFF instruction */
5921static int handle_vmoff(struct kvm_vcpu *vcpu)
5922{
5923 if (!nested_vmx_check_permission(vcpu))
5924 return 1;
5925 free_nested(to_vmx(vcpu));
5926 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08005927 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005928 return 1;
5929}
5930
5931/*
Nadav Har'El064aea72011-05-25 23:04:56 +03005932 * Decode the memory-address operand of a vmx instruction, as recorded on an
5933 * exit caused by such an instruction (run by a guest hypervisor).
5934 * On success, returns 0. When the operand is invalid, returns 1 and throws
5935 * #UD or #GP.
5936 */
5937static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
5938 unsigned long exit_qualification,
5939 u32 vmx_instruction_info, gva_t *ret)
5940{
5941 /*
5942 * According to Vol. 3B, "Information for VM Exits Due to Instruction
5943 * Execution", on an exit, vmx_instruction_info holds most of the
5944 * addressing components of the operand. Only the displacement part
5945 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
5946 * For how an actual address is calculated from all these components,
5947 * refer to Vol. 1, "Operand Addressing".
5948 */
5949 int scaling = vmx_instruction_info & 3;
5950 int addr_size = (vmx_instruction_info >> 7) & 7;
5951 bool is_reg = vmx_instruction_info & (1u << 10);
5952 int seg_reg = (vmx_instruction_info >> 15) & 7;
5953 int index_reg = (vmx_instruction_info >> 18) & 0xf;
5954 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
5955 int base_reg = (vmx_instruction_info >> 23) & 0xf;
5956 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
5957
5958 if (is_reg) {
5959 kvm_queue_exception(vcpu, UD_VECTOR);
5960 return 1;
5961 }
5962
5963 /* Addr = segment_base + offset */
5964 /* offset = base + [index * scale] + displacement */
5965 *ret = vmx_get_segment_base(vcpu, seg_reg);
5966 if (base_is_valid)
5967 *ret += kvm_register_read(vcpu, base_reg);
5968 if (index_is_valid)
5969 *ret += kvm_register_read(vcpu, index_reg)<<scaling;
5970 *ret += exit_qualification; /* holds the displacement */
5971
5972 if (addr_size == 1) /* 32 bit */
5973 *ret &= 0xffffffff;
5974
5975 /*
5976 * TODO: throw #GP (and return 1) in various cases that the VM*
5977 * instructions require it - e.g., offset beyond segment limit,
5978 * unusable or unreadable/unwritable segment, non-canonical 64-bit
5979 * address, and so on. Currently these are not checked.
5980 */
5981 return 0;
5982}
5983
Nadav Har'El27d6c862011-05-25 23:06:59 +03005984/* Emulate the VMCLEAR instruction */
5985static int handle_vmclear(struct kvm_vcpu *vcpu)
5986{
5987 struct vcpu_vmx *vmx = to_vmx(vcpu);
5988 gva_t gva;
5989 gpa_t vmptr;
5990 struct vmcs12 *vmcs12;
5991 struct page *page;
5992 struct x86_exception e;
5993
5994 if (!nested_vmx_check_permission(vcpu))
5995 return 1;
5996
5997 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5998 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5999 return 1;
6000
6001 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6002 sizeof(vmptr), &e)) {
6003 kvm_inject_page_fault(vcpu, &e);
6004 return 1;
6005 }
6006
6007 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
6008 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
6009 skip_emulated_instruction(vcpu);
6010 return 1;
6011 }
6012
6013 if (vmptr == vmx->nested.current_vmptr) {
Abel Gordone7953d72013-04-18 14:37:55 +03006014 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03006015 vmx->nested.current_vmptr = -1ull;
6016 vmx->nested.current_vmcs12 = NULL;
6017 }
6018
6019 page = nested_get_page(vcpu, vmptr);
6020 if (page == NULL) {
6021 /*
6022 * For accurate processor emulation, VMCLEAR beyond available
6023 * physical memory should do nothing at all. However, it is
6024 * possible that a nested vmx bug, not a guest hypervisor bug,
6025 * resulted in this case, so let's shut down before doing any
6026 * more damage:
6027 */
6028 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6029 return 1;
6030 }
6031 vmcs12 = kmap(page);
6032 vmcs12->launch_state = 0;
6033 kunmap(page);
6034 nested_release_page(page);
6035
6036 nested_free_vmcs02(vmx, vmptr);
6037
6038 skip_emulated_instruction(vcpu);
6039 nested_vmx_succeed(vcpu);
6040 return 1;
6041}
6042
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006043static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
6044
6045/* Emulate the VMLAUNCH instruction */
6046static int handle_vmlaunch(struct kvm_vcpu *vcpu)
6047{
6048 return nested_vmx_run(vcpu, true);
6049}
6050
6051/* Emulate the VMRESUME instruction */
6052static int handle_vmresume(struct kvm_vcpu *vcpu)
6053{
6054
6055 return nested_vmx_run(vcpu, false);
6056}
6057
Nadav Har'El49f705c2011-05-25 23:08:30 +03006058enum vmcs_field_type {
6059 VMCS_FIELD_TYPE_U16 = 0,
6060 VMCS_FIELD_TYPE_U64 = 1,
6061 VMCS_FIELD_TYPE_U32 = 2,
6062 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
6063};
6064
6065static inline int vmcs_field_type(unsigned long field)
6066{
6067 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
6068 return VMCS_FIELD_TYPE_U32;
6069 return (field >> 13) & 0x3 ;
6070}
6071
6072static inline int vmcs_field_readonly(unsigned long field)
6073{
6074 return (((field >> 10) & 0x3) == 1);
6075}
6076
6077/*
6078 * Read a vmcs12 field. Since these can have varying lengths and we return
6079 * one type, we chose the biggest type (u64) and zero-extend the return value
6080 * to that size. Note that the caller, handle_vmread, might need to use only
6081 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
6082 * 64-bit fields are to be returned).
6083 */
6084static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
6085 unsigned long field, u64 *ret)
6086{
6087 short offset = vmcs_field_to_offset(field);
6088 char *p;
6089
6090 if (offset < 0)
6091 return 0;
6092
6093 p = ((char *)(get_vmcs12(vcpu))) + offset;
6094
6095 switch (vmcs_field_type(field)) {
6096 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6097 *ret = *((natural_width *)p);
6098 return 1;
6099 case VMCS_FIELD_TYPE_U16:
6100 *ret = *((u16 *)p);
6101 return 1;
6102 case VMCS_FIELD_TYPE_U32:
6103 *ret = *((u32 *)p);
6104 return 1;
6105 case VMCS_FIELD_TYPE_U64:
6106 *ret = *((u64 *)p);
6107 return 1;
6108 default:
6109 return 0; /* can never happen. */
6110 }
6111}
6112
Abel Gordon20b97fe2013-04-18 14:36:25 +03006113
6114static inline bool vmcs12_write_any(struct kvm_vcpu *vcpu,
6115 unsigned long field, u64 field_value){
6116 short offset = vmcs_field_to_offset(field);
6117 char *p = ((char *) get_vmcs12(vcpu)) + offset;
6118 if (offset < 0)
6119 return false;
6120
6121 switch (vmcs_field_type(field)) {
6122 case VMCS_FIELD_TYPE_U16:
6123 *(u16 *)p = field_value;
6124 return true;
6125 case VMCS_FIELD_TYPE_U32:
6126 *(u32 *)p = field_value;
6127 return true;
6128 case VMCS_FIELD_TYPE_U64:
6129 *(u64 *)p = field_value;
6130 return true;
6131 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6132 *(natural_width *)p = field_value;
6133 return true;
6134 default:
6135 return false; /* can never happen. */
6136 }
6137
6138}
6139
Abel Gordon16f5b902013-04-18 14:38:25 +03006140static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
6141{
6142 int i;
6143 unsigned long field;
6144 u64 field_value;
6145 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02006146 const unsigned long *fields = shadow_read_write_fields;
6147 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03006148
6149 vmcs_load(shadow_vmcs);
6150
6151 for (i = 0; i < num_fields; i++) {
6152 field = fields[i];
6153 switch (vmcs_field_type(field)) {
6154 case VMCS_FIELD_TYPE_U16:
6155 field_value = vmcs_read16(field);
6156 break;
6157 case VMCS_FIELD_TYPE_U32:
6158 field_value = vmcs_read32(field);
6159 break;
6160 case VMCS_FIELD_TYPE_U64:
6161 field_value = vmcs_read64(field);
6162 break;
6163 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6164 field_value = vmcs_readl(field);
6165 break;
6166 }
6167 vmcs12_write_any(&vmx->vcpu, field, field_value);
6168 }
6169
6170 vmcs_clear(shadow_vmcs);
6171 vmcs_load(vmx->loaded_vmcs->vmcs);
6172}
6173
Abel Gordonc3114422013-04-18 14:38:55 +03006174static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
6175{
Mathias Krausec2bae892013-06-26 20:36:21 +02006176 const unsigned long *fields[] = {
6177 shadow_read_write_fields,
6178 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03006179 };
Mathias Krausec2bae892013-06-26 20:36:21 +02006180 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03006181 max_shadow_read_write_fields,
6182 max_shadow_read_only_fields
6183 };
6184 int i, q;
6185 unsigned long field;
6186 u64 field_value = 0;
6187 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
6188
6189 vmcs_load(shadow_vmcs);
6190
Mathias Krausec2bae892013-06-26 20:36:21 +02006191 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03006192 for (i = 0; i < max_fields[q]; i++) {
6193 field = fields[q][i];
6194 vmcs12_read_any(&vmx->vcpu, field, &field_value);
6195
6196 switch (vmcs_field_type(field)) {
6197 case VMCS_FIELD_TYPE_U16:
6198 vmcs_write16(field, (u16)field_value);
6199 break;
6200 case VMCS_FIELD_TYPE_U32:
6201 vmcs_write32(field, (u32)field_value);
6202 break;
6203 case VMCS_FIELD_TYPE_U64:
6204 vmcs_write64(field, (u64)field_value);
6205 break;
6206 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6207 vmcs_writel(field, (long)field_value);
6208 break;
6209 }
6210 }
6211 }
6212
6213 vmcs_clear(shadow_vmcs);
6214 vmcs_load(vmx->loaded_vmcs->vmcs);
6215}
6216
Nadav Har'El49f705c2011-05-25 23:08:30 +03006217/*
6218 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
6219 * used before) all generate the same failure when it is missing.
6220 */
6221static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
6222{
6223 struct vcpu_vmx *vmx = to_vmx(vcpu);
6224 if (vmx->nested.current_vmptr == -1ull) {
6225 nested_vmx_failInvalid(vcpu);
6226 skip_emulated_instruction(vcpu);
6227 return 0;
6228 }
6229 return 1;
6230}
6231
6232static int handle_vmread(struct kvm_vcpu *vcpu)
6233{
6234 unsigned long field;
6235 u64 field_value;
6236 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6237 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6238 gva_t gva = 0;
6239
6240 if (!nested_vmx_check_permission(vcpu) ||
6241 !nested_vmx_check_vmcs12(vcpu))
6242 return 1;
6243
6244 /* Decode instruction info and find the field to read */
6245 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
6246 /* Read the field, zero-extended to a u64 field_value */
6247 if (!vmcs12_read_any(vcpu, field, &field_value)) {
6248 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
6249 skip_emulated_instruction(vcpu);
6250 return 1;
6251 }
6252 /*
6253 * Now copy part of this value to register or memory, as requested.
6254 * Note that the number of bits actually copied is 32 or 64 depending
6255 * on the guest's mode (32 or 64 bit), not on the given field's length.
6256 */
6257 if (vmx_instruction_info & (1u << 10)) {
6258 kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
6259 field_value);
6260 } else {
6261 if (get_vmx_mem_address(vcpu, exit_qualification,
6262 vmx_instruction_info, &gva))
6263 return 1;
6264 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
6265 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
6266 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
6267 }
6268
6269 nested_vmx_succeed(vcpu);
6270 skip_emulated_instruction(vcpu);
6271 return 1;
6272}
6273
6274
6275static int handle_vmwrite(struct kvm_vcpu *vcpu)
6276{
6277 unsigned long field;
6278 gva_t gva;
6279 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6280 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03006281 /* The value to write might be 32 or 64 bits, depending on L1's long
6282 * mode, and eventually we need to write that into a field of several
6283 * possible lengths. The code below first zero-extends the value to 64
6284 * bit (field_value), and then copies only the approriate number of
6285 * bits into the vmcs12 field.
6286 */
6287 u64 field_value = 0;
6288 struct x86_exception e;
6289
6290 if (!nested_vmx_check_permission(vcpu) ||
6291 !nested_vmx_check_vmcs12(vcpu))
6292 return 1;
6293
6294 if (vmx_instruction_info & (1u << 10))
6295 field_value = kvm_register_read(vcpu,
6296 (((vmx_instruction_info) >> 3) & 0xf));
6297 else {
6298 if (get_vmx_mem_address(vcpu, exit_qualification,
6299 vmx_instruction_info, &gva))
6300 return 1;
6301 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
6302 &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) {
6303 kvm_inject_page_fault(vcpu, &e);
6304 return 1;
6305 }
6306 }
6307
6308
6309 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
6310 if (vmcs_field_readonly(field)) {
6311 nested_vmx_failValid(vcpu,
6312 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
6313 skip_emulated_instruction(vcpu);
6314 return 1;
6315 }
6316
Abel Gordon20b97fe2013-04-18 14:36:25 +03006317 if (!vmcs12_write_any(vcpu, field, field_value)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03006318 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
6319 skip_emulated_instruction(vcpu);
6320 return 1;
6321 }
6322
6323 nested_vmx_succeed(vcpu);
6324 skip_emulated_instruction(vcpu);
6325 return 1;
6326}
6327
Nadav Har'El63846662011-05-25 23:07:29 +03006328/* Emulate the VMPTRLD instruction */
6329static int handle_vmptrld(struct kvm_vcpu *vcpu)
6330{
6331 struct vcpu_vmx *vmx = to_vmx(vcpu);
6332 gva_t gva;
6333 gpa_t vmptr;
6334 struct x86_exception e;
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006335 u32 exec_control;
Nadav Har'El63846662011-05-25 23:07:29 +03006336
6337 if (!nested_vmx_check_permission(vcpu))
6338 return 1;
6339
6340 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
6341 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
6342 return 1;
6343
6344 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6345 sizeof(vmptr), &e)) {
6346 kvm_inject_page_fault(vcpu, &e);
6347 return 1;
6348 }
6349
6350 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
6351 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
6352 skip_emulated_instruction(vcpu);
6353 return 1;
6354 }
6355
6356 if (vmx->nested.current_vmptr != vmptr) {
6357 struct vmcs12 *new_vmcs12;
6358 struct page *page;
6359 page = nested_get_page(vcpu, vmptr);
6360 if (page == NULL) {
6361 nested_vmx_failInvalid(vcpu);
6362 skip_emulated_instruction(vcpu);
6363 return 1;
6364 }
6365 new_vmcs12 = kmap(page);
6366 if (new_vmcs12->revision_id != VMCS12_REVISION) {
6367 kunmap(page);
6368 nested_release_page_clean(page);
6369 nested_vmx_failValid(vcpu,
6370 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
6371 skip_emulated_instruction(vcpu);
6372 return 1;
6373 }
Abel Gordone7953d72013-04-18 14:37:55 +03006374 if (vmx->nested.current_vmptr != -1ull)
6375 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03006376
6377 vmx->nested.current_vmptr = vmptr;
6378 vmx->nested.current_vmcs12 = new_vmcs12;
6379 vmx->nested.current_vmcs12_page = page;
Abel Gordon012f83c2013-04-18 14:39:25 +03006380 if (enable_shadow_vmcs) {
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006381 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6382 exec_control |= SECONDARY_EXEC_SHADOW_VMCS;
6383 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
6384 vmcs_write64(VMCS_LINK_POINTER,
6385 __pa(vmx->nested.current_shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03006386 vmx->nested.sync_shadow_vmcs = true;
6387 }
Nadav Har'El63846662011-05-25 23:07:29 +03006388 }
6389
6390 nested_vmx_succeed(vcpu);
6391 skip_emulated_instruction(vcpu);
6392 return 1;
6393}
6394
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006395/* Emulate the VMPTRST instruction */
6396static int handle_vmptrst(struct kvm_vcpu *vcpu)
6397{
6398 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6399 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6400 gva_t vmcs_gva;
6401 struct x86_exception e;
6402
6403 if (!nested_vmx_check_permission(vcpu))
6404 return 1;
6405
6406 if (get_vmx_mem_address(vcpu, exit_qualification,
6407 vmx_instruction_info, &vmcs_gva))
6408 return 1;
6409 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
6410 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
6411 (void *)&to_vmx(vcpu)->nested.current_vmptr,
6412 sizeof(u64), &e)) {
6413 kvm_inject_page_fault(vcpu, &e);
6414 return 1;
6415 }
6416 nested_vmx_succeed(vcpu);
6417 skip_emulated_instruction(vcpu);
6418 return 1;
6419}
6420
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006421/* Emulate the INVEPT instruction */
6422static int handle_invept(struct kvm_vcpu *vcpu)
6423{
6424 u32 vmx_instruction_info, types;
6425 unsigned long type;
6426 gva_t gva;
6427 struct x86_exception e;
6428 struct {
6429 u64 eptp, gpa;
6430 } operand;
6431 u64 eptp_mask = ((1ull << 51) - 1) & PAGE_MASK;
6432
6433 if (!(nested_vmx_secondary_ctls_high & SECONDARY_EXEC_ENABLE_EPT) ||
6434 !(nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
6435 kvm_queue_exception(vcpu, UD_VECTOR);
6436 return 1;
6437 }
6438
6439 if (!nested_vmx_check_permission(vcpu))
6440 return 1;
6441
6442 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
6443 kvm_queue_exception(vcpu, UD_VECTOR);
6444 return 1;
6445 }
6446
6447 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6448 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
6449
6450 types = (nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
6451
6452 if (!(types & (1UL << type))) {
6453 nested_vmx_failValid(vcpu,
6454 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
6455 return 1;
6456 }
6457
6458 /* According to the Intel VMX instruction reference, the memory
6459 * operand is read even if it isn't needed (e.g., for type==global)
6460 */
6461 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
6462 vmx_instruction_info, &gva))
6463 return 1;
6464 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
6465 sizeof(operand), &e)) {
6466 kvm_inject_page_fault(vcpu, &e);
6467 return 1;
6468 }
6469
6470 switch (type) {
6471 case VMX_EPT_EXTENT_CONTEXT:
6472 if ((operand.eptp & eptp_mask) !=
6473 (nested_ept_get_cr3(vcpu) & eptp_mask))
6474 break;
6475 case VMX_EPT_EXTENT_GLOBAL:
6476 kvm_mmu_sync_roots(vcpu);
6477 kvm_mmu_flush_tlb(vcpu);
6478 nested_vmx_succeed(vcpu);
6479 break;
6480 default:
6481 BUG_ON(1);
6482 break;
6483 }
6484
6485 skip_emulated_instruction(vcpu);
6486 return 1;
6487}
6488
Nadav Har'El0140cae2011-05-25 23:06:28 +03006489/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006490 * The exit handlers return 1 if the exit was handled fully and guest execution
6491 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
6492 * to be done to userspace and return 0.
6493 */
Mathias Krause772e0312012-08-30 01:30:19 +02006494static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006495 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
6496 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08006497 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08006498 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006499 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006500 [EXIT_REASON_CR_ACCESS] = handle_cr,
6501 [EXIT_REASON_DR_ACCESS] = handle_dr,
6502 [EXIT_REASON_CPUID] = handle_cpuid,
6503 [EXIT_REASON_MSR_READ] = handle_rdmsr,
6504 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
6505 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
6506 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006507 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03006508 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02006509 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02006510 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03006511 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006512 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03006513 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006514 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03006515 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006516 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03006517 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006518 [EXIT_REASON_VMOFF] = handle_vmoff,
6519 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08006520 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
6521 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08006522 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08006523 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02006524 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08006525 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02006526 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08006527 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006528 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
6529 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006530 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08006531 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
6532 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006533 [EXIT_REASON_INVEPT] = handle_invept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006534};
6535
6536static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04006537 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006538
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006539static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
6540 struct vmcs12 *vmcs12)
6541{
6542 unsigned long exit_qualification;
6543 gpa_t bitmap, last_bitmap;
6544 unsigned int port;
6545 int size;
6546 u8 b;
6547
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006548 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05006549 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006550
6551 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6552
6553 port = exit_qualification >> 16;
6554 size = (exit_qualification & 7) + 1;
6555
6556 last_bitmap = (gpa_t)-1;
6557 b = -1;
6558
6559 while (size > 0) {
6560 if (port < 0x8000)
6561 bitmap = vmcs12->io_bitmap_a;
6562 else if (port < 0x10000)
6563 bitmap = vmcs12->io_bitmap_b;
6564 else
6565 return 1;
6566 bitmap += (port & 0x7fff) / 8;
6567
6568 if (last_bitmap != bitmap)
6569 if (kvm_read_guest(vcpu->kvm, bitmap, &b, 1))
6570 return 1;
6571 if (b & (1 << (port & 7)))
6572 return 1;
6573
6574 port++;
6575 size--;
6576 last_bitmap = bitmap;
6577 }
6578
6579 return 0;
6580}
6581
Nadav Har'El644d7112011-05-25 23:12:35 +03006582/*
6583 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
6584 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
6585 * disinterest in the current event (read or write a specific MSR) by using an
6586 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
6587 */
6588static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
6589 struct vmcs12 *vmcs12, u32 exit_reason)
6590{
6591 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
6592 gpa_t bitmap;
6593
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01006594 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Nadav Har'El644d7112011-05-25 23:12:35 +03006595 return 1;
6596
6597 /*
6598 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
6599 * for the four combinations of read/write and low/high MSR numbers.
6600 * First we need to figure out which of the four to use:
6601 */
6602 bitmap = vmcs12->msr_bitmap;
6603 if (exit_reason == EXIT_REASON_MSR_WRITE)
6604 bitmap += 2048;
6605 if (msr_index >= 0xc0000000) {
6606 msr_index -= 0xc0000000;
6607 bitmap += 1024;
6608 }
6609
6610 /* Then read the msr_index'th bit from this bitmap: */
6611 if (msr_index < 1024*8) {
6612 unsigned char b;
Jan Kiszkabd31a7f2013-02-14 19:46:27 +01006613 if (kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1))
6614 return 1;
Nadav Har'El644d7112011-05-25 23:12:35 +03006615 return 1 & (b >> (msr_index & 7));
6616 } else
6617 return 1; /* let L1 handle the wrong parameter */
6618}
6619
6620/*
6621 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
6622 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
6623 * intercept (via guest_host_mask etc.) the current event.
6624 */
6625static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
6626 struct vmcs12 *vmcs12)
6627{
6628 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6629 int cr = exit_qualification & 15;
6630 int reg = (exit_qualification >> 8) & 15;
6631 unsigned long val = kvm_register_read(vcpu, reg);
6632
6633 switch ((exit_qualification >> 4) & 3) {
6634 case 0: /* mov to cr */
6635 switch (cr) {
6636 case 0:
6637 if (vmcs12->cr0_guest_host_mask &
6638 (val ^ vmcs12->cr0_read_shadow))
6639 return 1;
6640 break;
6641 case 3:
6642 if ((vmcs12->cr3_target_count >= 1 &&
6643 vmcs12->cr3_target_value0 == val) ||
6644 (vmcs12->cr3_target_count >= 2 &&
6645 vmcs12->cr3_target_value1 == val) ||
6646 (vmcs12->cr3_target_count >= 3 &&
6647 vmcs12->cr3_target_value2 == val) ||
6648 (vmcs12->cr3_target_count >= 4 &&
6649 vmcs12->cr3_target_value3 == val))
6650 return 0;
6651 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
6652 return 1;
6653 break;
6654 case 4:
6655 if (vmcs12->cr4_guest_host_mask &
6656 (vmcs12->cr4_read_shadow ^ val))
6657 return 1;
6658 break;
6659 case 8:
6660 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
6661 return 1;
6662 break;
6663 }
6664 break;
6665 case 2: /* clts */
6666 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
6667 (vmcs12->cr0_read_shadow & X86_CR0_TS))
6668 return 1;
6669 break;
6670 case 1: /* mov from cr */
6671 switch (cr) {
6672 case 3:
6673 if (vmcs12->cpu_based_vm_exec_control &
6674 CPU_BASED_CR3_STORE_EXITING)
6675 return 1;
6676 break;
6677 case 8:
6678 if (vmcs12->cpu_based_vm_exec_control &
6679 CPU_BASED_CR8_STORE_EXITING)
6680 return 1;
6681 break;
6682 }
6683 break;
6684 case 3: /* lmsw */
6685 /*
6686 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
6687 * cr0. Other attempted changes are ignored, with no exit.
6688 */
6689 if (vmcs12->cr0_guest_host_mask & 0xe &
6690 (val ^ vmcs12->cr0_read_shadow))
6691 return 1;
6692 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
6693 !(vmcs12->cr0_read_shadow & 0x1) &&
6694 (val & 0x1))
6695 return 1;
6696 break;
6697 }
6698 return 0;
6699}
6700
6701/*
6702 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
6703 * should handle it ourselves in L0 (and then continue L2). Only call this
6704 * when in is_guest_mode (L2).
6705 */
6706static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
6707{
Nadav Har'El644d7112011-05-25 23:12:35 +03006708 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6709 struct vcpu_vmx *vmx = to_vmx(vcpu);
6710 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01006711 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03006712
Jan Kiszka542060e2014-01-04 18:47:21 +01006713 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
6714 vmcs_readl(EXIT_QUALIFICATION),
6715 vmx->idt_vectoring_info,
6716 intr_info,
6717 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
6718 KVM_ISA_VMX);
6719
Nadav Har'El644d7112011-05-25 23:12:35 +03006720 if (vmx->nested.nested_run_pending)
6721 return 0;
6722
6723 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02006724 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
6725 vmcs_read32(VM_INSTRUCTION_ERROR));
Nadav Har'El644d7112011-05-25 23:12:35 +03006726 return 1;
6727 }
6728
6729 switch (exit_reason) {
6730 case EXIT_REASON_EXCEPTION_NMI:
6731 if (!is_exception(intr_info))
6732 return 0;
6733 else if (is_page_fault(intr_info))
6734 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01006735 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01006736 !(vmcs12->guest_cr0 & X86_CR0_TS))
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01006737 return 0;
Nadav Har'El644d7112011-05-25 23:12:35 +03006738 return vmcs12->exception_bitmap &
6739 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
6740 case EXIT_REASON_EXTERNAL_INTERRUPT:
6741 return 0;
6742 case EXIT_REASON_TRIPLE_FAULT:
6743 return 1;
6744 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02006745 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03006746 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02006747 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03006748 case EXIT_REASON_TASK_SWITCH:
6749 return 1;
6750 case EXIT_REASON_CPUID:
6751 return 1;
6752 case EXIT_REASON_HLT:
6753 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
6754 case EXIT_REASON_INVD:
6755 return 1;
6756 case EXIT_REASON_INVLPG:
6757 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
6758 case EXIT_REASON_RDPMC:
6759 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
6760 case EXIT_REASON_RDTSC:
6761 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
6762 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
6763 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
6764 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
6765 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
6766 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006767 case EXIT_REASON_INVEPT:
Nadav Har'El644d7112011-05-25 23:12:35 +03006768 /*
6769 * VMX instructions trap unconditionally. This allows L1 to
6770 * emulate them for its L2 guest, i.e., allows 3-level nesting!
6771 */
6772 return 1;
6773 case EXIT_REASON_CR_ACCESS:
6774 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
6775 case EXIT_REASON_DR_ACCESS:
6776 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
6777 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006778 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03006779 case EXIT_REASON_MSR_READ:
6780 case EXIT_REASON_MSR_WRITE:
6781 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
6782 case EXIT_REASON_INVALID_STATE:
6783 return 1;
6784 case EXIT_REASON_MWAIT_INSTRUCTION:
6785 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
6786 case EXIT_REASON_MONITOR_INSTRUCTION:
6787 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
6788 case EXIT_REASON_PAUSE_INSTRUCTION:
6789 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
6790 nested_cpu_has2(vmcs12,
6791 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
6792 case EXIT_REASON_MCE_DURING_VMENTRY:
6793 return 0;
6794 case EXIT_REASON_TPR_BELOW_THRESHOLD:
6795 return 1;
6796 case EXIT_REASON_APIC_ACCESS:
6797 return nested_cpu_has2(vmcs12,
6798 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
6799 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03006800 /*
6801 * L0 always deals with the EPT violation. If nested EPT is
6802 * used, and the nested mmu code discovers that the address is
6803 * missing in the guest EPT table (EPT12), the EPT violation
6804 * will be injected with nested_ept_inject_page_fault()
6805 */
6806 return 0;
Nadav Har'El644d7112011-05-25 23:12:35 +03006807 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03006808 /*
6809 * L2 never uses directly L1's EPT, but rather L0's own EPT
6810 * table (shadow on EPT) or a merged EPT table that L0 built
6811 * (EPT on EPT). So any problems with the structure of the
6812 * table is L0's fault.
6813 */
Nadav Har'El644d7112011-05-25 23:12:35 +03006814 return 0;
6815 case EXIT_REASON_WBINVD:
6816 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
6817 case EXIT_REASON_XSETBV:
6818 return 1;
6819 default:
6820 return 1;
6821 }
6822}
6823
Avi Kivity586f9602010-11-18 13:09:54 +02006824static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
6825{
6826 *info1 = vmcs_readl(EXIT_QUALIFICATION);
6827 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
6828}
6829
Avi Kivity6aa8b732006-12-10 02:21:36 -08006830/*
6831 * The guest has exited. See if we can fix it or if we need userspace
6832 * assistance.
6833 */
Avi Kivity851ba692009-08-24 11:10:17 +03006834static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006835{
Avi Kivity29bd8a72007-09-10 17:27:03 +03006836 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006837 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02006838 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03006839
Mohammed Gamal80ced182009-09-01 12:48:18 +02006840 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02006841 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02006842 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006843
Nadav Har'El644d7112011-05-25 23:12:35 +03006844 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01006845 nested_vmx_vmexit(vcpu, exit_reason,
6846 vmcs_read32(VM_EXIT_INTR_INFO),
6847 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03006848 return 1;
6849 }
6850
Mohammed Gamal51207022010-05-31 22:40:54 +03006851 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
6852 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6853 vcpu->run->fail_entry.hardware_entry_failure_reason
6854 = exit_reason;
6855 return 0;
6856 }
6857
Avi Kivity29bd8a72007-09-10 17:27:03 +03006858 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006859 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6860 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03006861 = vmcs_read32(VM_INSTRUCTION_ERROR);
6862 return 0;
6863 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006864
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006865 /*
6866 * Note:
6867 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6868 * delivery event since it indicates guest is accessing MMIO.
6869 * The vm-exit can be triggered again after return to guest that
6870 * will cause infinite loop.
6871 */
Mike Dayd77c26f2007-10-08 09:02:08 -04006872 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08006873 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02006874 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006875 exit_reason != EXIT_REASON_TASK_SWITCH)) {
6876 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6877 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
6878 vcpu->run->internal.ndata = 2;
6879 vcpu->run->internal.data[0] = vectoring_info;
6880 vcpu->run->internal.data[1] = exit_reason;
6881 return 0;
6882 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006883
Nadav Har'El644d7112011-05-25 23:12:35 +03006884 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
6885 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03006886 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03006887 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006888 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006889 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01006890 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006891 /*
6892 * This CPU don't support us in finding the end of an
6893 * NMI-blocked window if the guest runs with IRQs
6894 * disabled. So we pull the trigger after 1 s of
6895 * futile waiting, but inform the user about this.
6896 */
6897 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6898 "state on VCPU %d after 1 s timeout\n",
6899 __func__, vcpu->vcpu_id);
6900 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006901 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006902 }
6903
Avi Kivity6aa8b732006-12-10 02:21:36 -08006904 if (exit_reason < kvm_vmx_max_exit_handlers
6905 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03006906 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006907 else {
Avi Kivity851ba692009-08-24 11:10:17 +03006908 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6909 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006910 }
6911 return 0;
6912}
6913
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006914static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006915{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006916 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006917 vmcs_write32(TPR_THRESHOLD, 0);
6918 return;
6919 }
6920
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006921 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006922}
6923
Yang Zhang8d146952013-01-25 10:18:50 +08006924static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
6925{
6926 u32 sec_exec_control;
6927
6928 /*
6929 * There is not point to enable virtualize x2apic without enable
6930 * apicv
6931 */
Yang Zhangc7c9c562013-01-25 10:18:51 +08006932 if (!cpu_has_vmx_virtualize_x2apic_mode() ||
6933 !vmx_vm_has_apicv(vcpu->kvm))
Yang Zhang8d146952013-01-25 10:18:50 +08006934 return;
6935
6936 if (!vm_need_tpr_shadow(vcpu->kvm))
6937 return;
6938
6939 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6940
6941 if (set) {
6942 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6943 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6944 } else {
6945 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6946 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6947 }
6948 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
6949
6950 vmx_set_msr_bitmap(vcpu);
6951}
6952
Yang Zhangc7c9c562013-01-25 10:18:51 +08006953static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
6954{
6955 u16 status;
6956 u8 old;
6957
6958 if (!vmx_vm_has_apicv(kvm))
6959 return;
6960
6961 if (isr == -1)
6962 isr = 0;
6963
6964 status = vmcs_read16(GUEST_INTR_STATUS);
6965 old = status >> 8;
6966 if (isr != old) {
6967 status &= 0xff;
6968 status |= isr << 8;
6969 vmcs_write16(GUEST_INTR_STATUS, status);
6970 }
6971}
6972
6973static void vmx_set_rvi(int vector)
6974{
6975 u16 status;
6976 u8 old;
6977
6978 status = vmcs_read16(GUEST_INTR_STATUS);
6979 old = (u8)status & 0xff;
6980 if ((u8)vector != old) {
6981 status &= ~0xff;
6982 status |= (u8)vector;
6983 vmcs_write16(GUEST_INTR_STATUS, status);
6984 }
6985}
6986
6987static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6988{
6989 if (max_irr == -1)
6990 return;
6991
6992 vmx_set_rvi(max_irr);
6993}
6994
6995static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
6996{
Yang Zhang3d81bc72013-04-11 19:25:13 +08006997 if (!vmx_vm_has_apicv(vcpu->kvm))
6998 return;
6999
Yang Zhangc7c9c562013-01-25 10:18:51 +08007000 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
7001 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
7002 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
7003 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
7004}
7005
Avi Kivity51aa01d2010-07-20 14:31:20 +03007006static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03007007{
Avi Kivity00eba012011-03-07 17:24:54 +02007008 u32 exit_intr_info;
7009
7010 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
7011 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
7012 return;
7013
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007014 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02007015 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08007016
7017 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02007018 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08007019 kvm_machine_check();
7020
Gleb Natapov20f65982009-05-11 13:35:55 +03007021 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02007022 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007023 (exit_intr_info & INTR_INFO_VALID_MASK)) {
7024 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03007025 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08007026 kvm_after_handle_nmi(&vmx->vcpu);
7027 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03007028}
Gleb Natapov20f65982009-05-11 13:35:55 +03007029
Yang Zhanga547c6d2013-04-11 19:25:10 +08007030static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
7031{
7032 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7033
7034 /*
7035 * If external interrupt exists, IF bit is set in rflags/eflags on the
7036 * interrupt stack frame, and interrupt will be enabled on a return
7037 * from interrupt handler.
7038 */
7039 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
7040 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
7041 unsigned int vector;
7042 unsigned long entry;
7043 gate_desc *desc;
7044 struct vcpu_vmx *vmx = to_vmx(vcpu);
7045#ifdef CONFIG_X86_64
7046 unsigned long tmp;
7047#endif
7048
7049 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
7050 desc = (gate_desc *)vmx->host_idt_base + vector;
7051 entry = gate_offset(*desc);
7052 asm volatile(
7053#ifdef CONFIG_X86_64
7054 "mov %%" _ASM_SP ", %[sp]\n\t"
7055 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
7056 "push $%c[ss]\n\t"
7057 "push %[sp]\n\t"
7058#endif
7059 "pushf\n\t"
7060 "orl $0x200, (%%" _ASM_SP ")\n\t"
7061 __ASM_SIZE(push) " $%c[cs]\n\t"
7062 "call *%[entry]\n\t"
7063 :
7064#ifdef CONFIG_X86_64
7065 [sp]"=&r"(tmp)
7066#endif
7067 :
7068 [entry]"r"(entry),
7069 [ss]"i"(__KERNEL_DS),
7070 [cs]"i"(__KERNEL_CS)
7071 );
7072 } else
7073 local_irq_enable();
7074}
7075
Liu, Jinsongda8999d2014-02-24 10:55:46 +00007076static bool vmx_mpx_supported(void)
7077{
7078 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
7079 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
7080}
7081
Avi Kivity51aa01d2010-07-20 14:31:20 +03007082static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
7083{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007084 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03007085 bool unblock_nmi;
7086 u8 vector;
7087 bool idtv_info_valid;
7088
7089 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03007090
Avi Kivitycf393f72008-07-01 16:20:21 +03007091 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02007092 if (vmx->nmi_known_unmasked)
7093 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007094 /*
7095 * Can't use vmx->exit_intr_info since we're not sure what
7096 * the exit reason is.
7097 */
7098 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03007099 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
7100 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
7101 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007102 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03007103 * Re-set bit "block by NMI" before VM entry if vmexit caused by
7104 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007105 * SDM 3: 23.2.2 (September 2008)
7106 * Bit 12 is undefined in any of the following cases:
7107 * If the VM exit sets the valid bit in the IDT-vectoring
7108 * information field.
7109 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03007110 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007111 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
7112 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03007113 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7114 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02007115 else
7116 vmx->nmi_known_unmasked =
7117 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
7118 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007119 } else if (unlikely(vmx->soft_vnmi_blocked))
7120 vmx->vnmi_blocked_time +=
7121 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03007122}
7123
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007124static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03007125 u32 idt_vectoring_info,
7126 int instr_len_field,
7127 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03007128{
Avi Kivity51aa01d2010-07-20 14:31:20 +03007129 u8 vector;
7130 int type;
7131 bool idtv_info_valid;
7132
7133 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03007134
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007135 vcpu->arch.nmi_injected = false;
7136 kvm_clear_exception_queue(vcpu);
7137 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007138
7139 if (!idtv_info_valid)
7140 return;
7141
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007142 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03007143
Avi Kivity668f6122008-07-02 09:28:55 +03007144 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
7145 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03007146
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007147 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03007148 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007149 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03007150 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007151 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03007152 * Clear bit "block by NMI" before VM entry if a NMI
7153 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03007154 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007155 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007156 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03007157 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007158 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007159 /* fall through */
7160 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03007161 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03007162 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03007163 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03007164 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03007165 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007166 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007167 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007168 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007169 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03007170 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007171 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007172 break;
7173 default:
7174 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03007175 }
Avi Kivitycf393f72008-07-01 16:20:21 +03007176}
7177
Avi Kivity83422e12010-07-20 14:43:23 +03007178static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
7179{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007180 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03007181 VM_EXIT_INSTRUCTION_LEN,
7182 IDT_VECTORING_ERROR_CODE);
7183}
7184
Avi Kivityb463a6f2010-07-20 15:06:17 +03007185static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
7186{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007187 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007188 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
7189 VM_ENTRY_INSTRUCTION_LEN,
7190 VM_ENTRY_EXCEPTION_ERROR_CODE);
7191
7192 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
7193}
7194
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007195static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
7196{
7197 int i, nr_msrs;
7198 struct perf_guest_switch_msr *msrs;
7199
7200 msrs = perf_guest_get_msrs(&nr_msrs);
7201
7202 if (!msrs)
7203 return;
7204
7205 for (i = 0; i < nr_msrs; i++)
7206 if (msrs[i].host == msrs[i].guest)
7207 clear_atomic_switch_msr(vmx, msrs[i].msr);
7208 else
7209 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
7210 msrs[i].host);
7211}
7212
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08007213static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007214{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007215 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03007216 unsigned long debugctlmsr;
Avi Kivity104f2262010-11-18 13:12:52 +02007217
7218 /* Record the guest's net vcpu time for enforced NMI injections. */
7219 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
7220 vmx->entry_time = ktime_get();
7221
7222 /* Don't enter VMX if guest state is invalid, let the exit handler
7223 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02007224 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02007225 return;
7226
Abel Gordon012f83c2013-04-18 14:39:25 +03007227 if (vmx->nested.sync_shadow_vmcs) {
7228 copy_vmcs12_to_shadow(vmx);
7229 vmx->nested.sync_shadow_vmcs = false;
7230 }
7231
Avi Kivity104f2262010-11-18 13:12:52 +02007232 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
7233 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
7234 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
7235 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
7236
7237 /* When single-stepping over STI and MOV SS, we must clear the
7238 * corresponding interruptibility bits in the guest state. Otherwise
7239 * vmentry fails as it then expects bit 14 (BS) in pending debug
7240 * exceptions being set, but that's not correct for the guest debugging
7241 * case. */
7242 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
7243 vmx_set_interrupt_shadow(vcpu, 0);
7244
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007245 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03007246 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007247
Nadav Har'Eld462b812011-05-24 15:26:10 +03007248 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02007249 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08007250 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007251 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
7252 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
7253 "push %%" _ASM_CX " \n\t"
7254 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03007255 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007256 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03007257 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03007258 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007259 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007260 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
7261 "mov %%cr2, %%" _ASM_DX " \n\t"
7262 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007263 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007264 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007265 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007266 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02007267 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007268 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007269 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
7270 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
7271 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
7272 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
7273 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
7274 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08007275#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02007276 "mov %c[r8](%0), %%r8 \n\t"
7277 "mov %c[r9](%0), %%r9 \n\t"
7278 "mov %c[r10](%0), %%r10 \n\t"
7279 "mov %c[r11](%0), %%r11 \n\t"
7280 "mov %c[r12](%0), %%r12 \n\t"
7281 "mov %c[r13](%0), %%r13 \n\t"
7282 "mov %c[r14](%0), %%r14 \n\t"
7283 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007284#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03007285 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03007286
Avi Kivity6aa8b732006-12-10 02:21:36 -08007287 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03007288 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03007289 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03007290 "jmp 2f \n\t"
7291 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
7292 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08007293 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007294 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02007295 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007296 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
7297 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
7298 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
7299 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
7300 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
7301 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
7302 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08007303#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02007304 "mov %%r8, %c[r8](%0) \n\t"
7305 "mov %%r9, %c[r9](%0) \n\t"
7306 "mov %%r10, %c[r10](%0) \n\t"
7307 "mov %%r11, %c[r11](%0) \n\t"
7308 "mov %%r12, %c[r12](%0) \n\t"
7309 "mov %%r13, %c[r13](%0) \n\t"
7310 "mov %%r14, %c[r14](%0) \n\t"
7311 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007312#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03007313 "mov %%cr2, %%" _ASM_AX " \n\t"
7314 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03007315
Avi Kivityb188c81f2012-09-16 15:10:58 +03007316 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02007317 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03007318 ".pushsection .rodata \n\t"
7319 ".global vmx_return \n\t"
7320 "vmx_return: " _ASM_PTR " 2b \n\t"
7321 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02007322 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03007323 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02007324 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03007325 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007326 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
7327 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
7328 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
7329 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
7330 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
7331 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
7332 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08007333#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007334 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
7335 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
7336 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
7337 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
7338 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
7339 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
7340 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
7341 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08007342#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02007343 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
7344 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02007345 : "cc", "memory"
7346#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03007347 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02007348 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007349#else
7350 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02007351#endif
7352 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08007353
Gleb Natapov2a7921b2012-08-12 16:12:29 +03007354 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
7355 if (debugctlmsr)
7356 update_debugctlmsr(debugctlmsr);
7357
Avi Kivityaa67f602012-08-01 16:48:03 +03007358#ifndef CONFIG_X86_64
7359 /*
7360 * The sysexit path does not restore ds/es, so we must set them to
7361 * a reasonable value ourselves.
7362 *
7363 * We can't defer this to vmx_load_host_state() since that function
7364 * may be executed in interrupt context, which saves and restore segments
7365 * around it, nullifying its effect.
7366 */
7367 loadsegment(ds, __USER_DS);
7368 loadsegment(es, __USER_DS);
7369#endif
7370
Avi Kivity6de4f3a2009-05-31 22:58:47 +03007371 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02007372 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivity69c73022011-03-07 15:26:44 +02007373 | (1 << VCPU_EXREG_CPL)
Avi Kivityaff48ba2010-12-05 18:56:11 +02007374 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03007375 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02007376 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007377 vcpu->arch.regs_dirty = 0;
7378
Avi Kivity1155f762007-11-22 11:30:47 +02007379 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
7380
Nadav Har'Eld462b812011-05-24 15:26:10 +03007381 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02007382
Avi Kivity51aa01d2010-07-20 14:31:20 +03007383 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02007384 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
Avi Kivity51aa01d2010-07-20 14:31:20 +03007385
Gleb Natapove0b890d2013-09-25 12:51:33 +03007386 /*
7387 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
7388 * we did not inject a still-pending event to L1 now because of
7389 * nested_run_pending, we need to re-enable this bit.
7390 */
7391 if (vmx->nested.nested_run_pending)
7392 kvm_make_request(KVM_REQ_EVENT, vcpu);
7393
7394 vmx->nested.nested_run_pending = 0;
7395
Avi Kivity51aa01d2010-07-20 14:31:20 +03007396 vmx_complete_atomic_exit(vmx);
7397 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03007398 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007399}
7400
Avi Kivity6aa8b732006-12-10 02:21:36 -08007401static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
7402{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007403 struct vcpu_vmx *vmx = to_vmx(vcpu);
7404
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08007405 free_vpid(vmx);
Nadav Har'Eld462b812011-05-24 15:26:10 +03007406 free_loaded_vmcs(vmx->loaded_vmcs);
Marcelo Tosatti26a865f2014-01-03 17:00:51 -02007407 free_nested(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007408 kfree(vmx->guest_msrs);
7409 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10007410 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007411}
7412
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007413static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007414{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007415 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10007416 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03007417 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007418
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007419 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007420 return ERR_PTR(-ENOMEM);
7421
Sheng Yang2384d2b2008-01-17 15:14:33 +08007422 allocate_vpid(vmx);
7423
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007424 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
7425 if (err)
7426 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08007427
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007428 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02007429 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007430 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007431 goto uninit_vcpu;
7432 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08007433
Nadav Har'Eld462b812011-05-24 15:26:10 +03007434 vmx->loaded_vmcs = &vmx->vmcs01;
7435 vmx->loaded_vmcs->vmcs = alloc_vmcs();
7436 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007437 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03007438 if (!vmm_exclusive)
7439 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
7440 loaded_vmcs_init(vmx->loaded_vmcs);
7441 if (!vmm_exclusive)
7442 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007443
Avi Kivity15ad7142007-07-11 18:17:21 +03007444 cpu = get_cpu();
7445 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10007446 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10007447 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007448 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03007449 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007450 if (err)
7451 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02007452 if (vm_need_virtualize_apic_accesses(kvm)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02007453 err = alloc_apic_access_page(kvm);
7454 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02007455 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02007456 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08007457
Sheng Yangb927a3c2009-07-21 10:42:48 +08007458 if (enable_ept) {
7459 if (!kvm->arch.ept_identity_map_addr)
7460 kvm->arch.ept_identity_map_addr =
7461 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Gleb Natapov93ea5382011-02-21 12:07:59 +02007462 err = -ENOMEM;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007463 if (alloc_identity_pagetable(kvm) != 0)
7464 goto free_vmcs;
Gleb Natapov93ea5382011-02-21 12:07:59 +02007465 if (!init_rmode_identity_map(kvm))
7466 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08007467 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007468
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03007469 vmx->nested.current_vmptr = -1ull;
7470 vmx->nested.current_vmcs12 = NULL;
7471
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007472 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08007473
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007474free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08007475 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007476free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007477 kfree(vmx->guest_msrs);
7478uninit_vcpu:
7479 kvm_vcpu_uninit(&vmx->vcpu);
7480free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08007481 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10007482 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007483 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007484}
7485
Yang, Sheng002c7f72007-07-31 14:23:01 +03007486static void __init vmx_check_processor_compat(void *rtn)
7487{
7488 struct vmcs_config vmcs_conf;
7489
7490 *(int *)rtn = 0;
7491 if (setup_vmcs_config(&vmcs_conf) < 0)
7492 *(int *)rtn = -EIO;
7493 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
7494 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
7495 smp_processor_id());
7496 *(int *)rtn = -EIO;
7497 }
7498}
7499
Sheng Yang67253af2008-04-25 10:20:22 +08007500static int get_ept_level(void)
7501{
7502 return VMX_EPT_DEFAULT_GAW + 1;
7503}
7504
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007505static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08007506{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007507 u64 ret;
7508
Sheng Yang522c68c2009-04-27 20:35:43 +08007509 /* For VT-d and EPT combination
7510 * 1. MMIO: always map as UC
7511 * 2. EPT with VT-d:
7512 * a. VT-d without snooping control feature: can't guarantee the
7513 * result, try to trust guest.
7514 * b. VT-d with snooping control feature: snooping control feature of
7515 * VT-d engine can guarantee the cache correctness. Just set it
7516 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08007517 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08007518 * consistent with host MTRR
7519 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007520 if (is_mmio)
7521 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06007522 else if (kvm_arch_has_noncoherent_dma(vcpu->kvm))
Sheng Yang522c68c2009-04-27 20:35:43 +08007523 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
7524 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007525 else
Sheng Yang522c68c2009-04-27 20:35:43 +08007526 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08007527 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007528
7529 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08007530}
7531
Sheng Yang17cc3932010-01-05 19:02:27 +08007532static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02007533{
Sheng Yang878403b2010-01-05 19:02:29 +08007534 if (enable_ept && !cpu_has_vmx_ept_1g_page())
7535 return PT_DIRECTORY_LEVEL;
7536 else
7537 /* For shadow and EPT supported 1GB page */
7538 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02007539}
7540
Sheng Yang0e851882009-12-18 16:48:46 +08007541static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
7542{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007543 struct kvm_cpuid_entry2 *best;
7544 struct vcpu_vmx *vmx = to_vmx(vcpu);
7545 u32 exec_control;
7546
7547 vmx->rdtscp_enabled = false;
7548 if (vmx_rdtscp_supported()) {
7549 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7550 if (exec_control & SECONDARY_EXEC_RDTSCP) {
7551 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
7552 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
7553 vmx->rdtscp_enabled = true;
7554 else {
7555 exec_control &= ~SECONDARY_EXEC_RDTSCP;
7556 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7557 exec_control);
7558 }
7559 }
7560 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007561
Mao, Junjiead756a12012-07-02 01:18:48 +00007562 /* Exposing INVPCID only when PCID is exposed */
7563 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
7564 if (vmx_invpcid_supported() &&
Ren, Yongjie4f977042012-09-07 07:36:59 +00007565 best && (best->ebx & bit(X86_FEATURE_INVPCID)) &&
Mao, Junjiead756a12012-07-02 01:18:48 +00007566 guest_cpuid_has_pcid(vcpu)) {
Takashi Iwai29282fd2012-11-09 15:20:17 +01007567 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Mao, Junjiead756a12012-07-02 01:18:48 +00007568 exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
7569 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7570 exec_control);
7571 } else {
Takashi Iwai29282fd2012-11-09 15:20:17 +01007572 if (cpu_has_secondary_exec_ctrls()) {
7573 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7574 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
7575 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7576 exec_control);
7577 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007578 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00007579 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00007580 }
Sheng Yang0e851882009-12-18 16:48:46 +08007581}
7582
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007583static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
7584{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03007585 if (func == 1 && nested)
7586 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007587}
7588
Yang Zhang25d92082013-08-06 12:00:32 +03007589static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
7590 struct x86_exception *fault)
7591{
Jan Kiszka533558b2014-01-04 18:47:20 +01007592 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7593 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03007594
7595 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01007596 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03007597 else
Jan Kiszka533558b2014-01-04 18:47:20 +01007598 exit_reason = EXIT_REASON_EPT_VIOLATION;
7599 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03007600 vmcs12->guest_physical_address = fault->address;
7601}
7602
Nadav Har'El155a97a2013-08-05 11:07:16 +03007603/* Callbacks for nested_ept_init_mmu_context: */
7604
7605static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
7606{
7607 /* return the page table to be shadowed - in our case, EPT12 */
7608 return get_vmcs12(vcpu)->ept_pointer;
7609}
7610
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02007611static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03007612{
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02007613 kvm_init_shadow_ept_mmu(vcpu, &vcpu->arch.mmu,
Nadav Har'El155a97a2013-08-05 11:07:16 +03007614 nested_vmx_ept_caps & VMX_EPT_EXECUTE_ONLY_BIT);
7615
7616 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
7617 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
7618 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
7619
7620 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03007621}
7622
7623static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
7624{
7625 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
7626}
7627
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03007628static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
7629 struct x86_exception *fault)
7630{
7631 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7632
7633 WARN_ON(!is_guest_mode(vcpu));
7634
7635 /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
7636 if (vmcs12->exception_bitmap & (1u << PF_VECTOR))
Jan Kiszka533558b2014-01-04 18:47:20 +01007637 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
7638 vmcs_read32(VM_EXIT_INTR_INFO),
7639 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03007640 else
7641 kvm_inject_page_fault(vcpu, fault);
7642}
7643
Jan Kiszkaf41245002014-03-07 20:03:13 +01007644static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
7645{
7646 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
7647 struct vcpu_vmx *vmx = to_vmx(vcpu);
7648
7649 if (vcpu->arch.virtual_tsc_khz == 0)
7650 return;
7651
7652 /* Make sure short timeouts reliably trigger an immediate vmexit.
7653 * hrtimer_start does not guarantee this. */
7654 if (preemption_timeout <= 1) {
7655 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
7656 return;
7657 }
7658
7659 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
7660 preemption_timeout *= 1000000;
7661 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
7662 hrtimer_start(&vmx->nested.preemption_timer,
7663 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
7664}
7665
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007666/*
7667 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
7668 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
7669 * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2
7670 * guest in a way that will both be appropriate to L1's requests, and our
7671 * needs. In addition to modifying the active vmcs (which is vmcs02), this
7672 * function also has additional necessary side-effects, like setting various
7673 * vcpu->arch fields.
7674 */
7675static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7676{
7677 struct vcpu_vmx *vmx = to_vmx(vcpu);
7678 u32 exec_control;
7679
7680 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
7681 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
7682 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
7683 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
7684 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
7685 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
7686 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
7687 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
7688 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
7689 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
7690 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
7691 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
7692 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
7693 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
7694 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
7695 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
7696 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
7697 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
7698 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
7699 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
7700 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
7701 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
7702 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
7703 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
7704 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
7705 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
7706 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
7707 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
7708 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
7709 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
7710 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
7711 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
7712 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
7713 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
7714 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
7715 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
7716
7717 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
7718 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
7719 vmcs12->vm_entry_intr_info_field);
7720 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
7721 vmcs12->vm_entry_exception_error_code);
7722 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
7723 vmcs12->vm_entry_instruction_len);
7724 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
7725 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007726 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Jan Kiszka503cd0c2013-03-03 13:05:44 +01007727 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
Gleb Natapov63fbf592013-07-28 18:31:06 +03007728 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007729 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
7730 vmcs12->guest_pending_dbg_exceptions);
7731 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
7732 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
7733
7734 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7735
Jan Kiszkaf41245002014-03-07 20:03:13 +01007736 exec_control = vmcs12->pin_based_vm_exec_control;
7737 exec_control |= vmcs_config.pin_based_exec_ctrl;
7738 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
7739 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007740
Jan Kiszkaf41245002014-03-07 20:03:13 +01007741 vmx->nested.preemption_timer_expired = false;
7742 if (nested_cpu_has_preemption_timer(vmcs12))
7743 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +01007744
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007745 /*
7746 * Whether page-faults are trapped is determined by a combination of
7747 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
7748 * If enable_ept, L0 doesn't care about page faults and we should
7749 * set all of these to L1's desires. However, if !enable_ept, L0 does
7750 * care about (at least some) page faults, and because it is not easy
7751 * (if at all possible?) to merge L0 and L1's desires, we simply ask
7752 * to exit on each and every L2 page fault. This is done by setting
7753 * MASK=MATCH=0 and (see below) EB.PF=1.
7754 * Note that below we don't need special code to set EB.PF beyond the
7755 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
7756 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
7757 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
7758 *
7759 * A problem with this approach (when !enable_ept) is that L1 may be
7760 * injected with more page faults than it asked for. This could have
7761 * caused problems, but in practice existing hypervisors don't care.
7762 * To fix this, we will need to emulate the PFEC checking (on the L1
7763 * page tables), using walk_addr(), when injecting PFs to L1.
7764 */
7765 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
7766 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
7767 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
7768 enable_ept ? vmcs12->page_fault_error_code_match : 0);
7769
7770 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf41245002014-03-07 20:03:13 +01007771 exec_control = vmx_secondary_exec_control(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007772 if (!vmx->rdtscp_enabled)
7773 exec_control &= ~SECONDARY_EXEC_RDTSCP;
7774 /* Take the following fields only from vmcs12 */
7775 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7776 if (nested_cpu_has(vmcs12,
7777 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
7778 exec_control |= vmcs12->secondary_vm_exec_control;
7779
7780 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
7781 /*
7782 * Translate L1 physical address to host physical
7783 * address for vmcs02. Keep the page pinned, so this
7784 * physical address remains valid. We keep a reference
7785 * to it so we can release it later.
7786 */
7787 if (vmx->nested.apic_access_page) /* shouldn't happen */
7788 nested_release_page(vmx->nested.apic_access_page);
7789 vmx->nested.apic_access_page =
7790 nested_get_page(vcpu, vmcs12->apic_access_addr);
7791 /*
7792 * If translation failed, no matter: This feature asks
7793 * to exit when accessing the given address, and if it
7794 * can never be accessed, this feature won't do
7795 * anything anyway.
7796 */
7797 if (!vmx->nested.apic_access_page)
7798 exec_control &=
7799 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7800 else
7801 vmcs_write64(APIC_ACCESS_ADDR,
7802 page_to_phys(vmx->nested.apic_access_page));
Jan Kiszkaca3f2572013-12-16 12:55:46 +01007803 } else if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) {
7804 exec_control |=
7805 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7806 vmcs_write64(APIC_ACCESS_ADDR,
7807 page_to_phys(vcpu->kvm->arch.apic_access_page));
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007808 }
7809
7810 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
7811 }
7812
7813
7814 /*
7815 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
7816 * Some constant fields are set here by vmx_set_constant_host_state().
7817 * Other fields are different per CPU, and will be set later when
7818 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
7819 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08007820 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007821
7822 /*
7823 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
7824 * entry, but only if the current (host) sp changed from the value
7825 * we wrote last (vmx->host_rsp). This cache is no longer relevant
7826 * if we switch vmcs, and rather than hold a separate cache per vmcs,
7827 * here we just force the write to happen on entry.
7828 */
7829 vmx->host_rsp = 0;
7830
7831 exec_control = vmx_exec_control(vmx); /* L0's desires */
7832 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
7833 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
7834 exec_control &= ~CPU_BASED_TPR_SHADOW;
7835 exec_control |= vmcs12->cpu_based_vm_exec_control;
7836 /*
7837 * Merging of IO and MSR bitmaps not currently supported.
7838 * Rather, exit every time.
7839 */
7840 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
7841 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
7842 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
7843
7844 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
7845
7846 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
7847 * bitwise-or of what L1 wants to trap for L2, and what we want to
7848 * trap. Note that CR0.TS also needs updating - we do this later.
7849 */
7850 update_exception_bitmap(vcpu);
7851 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
7852 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
7853
Nadav Har'El8049d652013-08-05 11:07:06 +03007854 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
7855 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
7856 * bits are further modified by vmx_set_efer() below.
7857 */
Jan Kiszkaf41245002014-03-07 20:03:13 +01007858 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +03007859
7860 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
7861 * emulated by vmx_set_efer(), below.
7862 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02007863 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +03007864 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
7865 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007866 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
7867
Jan Kiszka44811c02013-08-04 17:17:27 +02007868 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007869 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02007870 vcpu->arch.pat = vmcs12->guest_ia32_pat;
7871 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007872 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
7873
7874
7875 set_cr4_guest_host_mask(vmx);
7876
Paolo Bonzini36be0b92014-02-24 12:30:04 +01007877 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)
7878 vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs);
7879
Nadav Har'El27fc51b2011-08-02 15:54:52 +03007880 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
7881 vmcs_write64(TSC_OFFSET,
7882 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
7883 else
7884 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007885
7886 if (enable_vpid) {
7887 /*
7888 * Trivially support vpid by letting L2s share their parent
7889 * L1's vpid. TODO: move to a more elaborate solution, giving
7890 * each L2 its own vpid and exposing the vpid feature to L1.
7891 */
7892 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
7893 vmx_flush_tlb(vcpu);
7894 }
7895
Nadav Har'El155a97a2013-08-05 11:07:16 +03007896 if (nested_cpu_has_ept(vmcs12)) {
7897 kvm_mmu_unload(vcpu);
7898 nested_ept_init_mmu_context(vcpu);
7899 }
7900
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007901 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
7902 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +02007903 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007904 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
7905 else
7906 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
7907 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
7908 vmx_set_efer(vcpu, vcpu->arch.efer);
7909
7910 /*
7911 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
7912 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
7913 * The CR0_READ_SHADOW is what L2 should have expected to read given
7914 * the specifications by L1; It's not enough to take
7915 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
7916 * have more bits than L1 expected.
7917 */
7918 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
7919 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
7920
7921 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
7922 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
7923
7924 /* shadow page tables on either EPT or shadow page tables */
7925 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
7926 kvm_mmu_reset_context(vcpu);
7927
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03007928 if (!enable_ept)
7929 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
7930
Nadav Har'El3633cfc2013-08-05 11:07:07 +03007931 /*
7932 * L1 may access the L2's PDPTR, so save them to construct vmcs12
7933 */
7934 if (enable_ept) {
7935 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
7936 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
7937 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
7938 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
7939 }
7940
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007941 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
7942 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
7943}
7944
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007945/*
7946 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
7947 * for running an L2 nested guest.
7948 */
7949static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
7950{
7951 struct vmcs12 *vmcs12;
7952 struct vcpu_vmx *vmx = to_vmx(vcpu);
7953 int cpu;
7954 struct loaded_vmcs *vmcs02;
Jan Kiszka384bb782013-04-20 10:52:36 +02007955 bool ia32e;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007956
7957 if (!nested_vmx_check_permission(vcpu) ||
7958 !nested_vmx_check_vmcs12(vcpu))
7959 return 1;
7960
7961 skip_emulated_instruction(vcpu);
7962 vmcs12 = get_vmcs12(vcpu);
7963
Abel Gordon012f83c2013-04-18 14:39:25 +03007964 if (enable_shadow_vmcs)
7965 copy_shadow_to_vmcs12(vmx);
7966
Nadav Har'El7c177932011-05-25 23:12:04 +03007967 /*
7968 * The nested entry process starts with enforcing various prerequisites
7969 * on vmcs12 as required by the Intel SDM, and act appropriately when
7970 * they fail: As the SDM explains, some conditions should cause the
7971 * instruction to fail, while others will cause the instruction to seem
7972 * to succeed, but return an EXIT_REASON_INVALID_STATE.
7973 * To speed up the normal (success) code path, we should avoid checking
7974 * for misconfigurations which will anyway be caught by the processor
7975 * when using the merged vmcs02.
7976 */
7977 if (vmcs12->launch_state == launch) {
7978 nested_vmx_failValid(vcpu,
7979 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
7980 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
7981 return 1;
7982 }
7983
Jan Kiszka6dfacad2013-12-04 08:58:54 +01007984 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
7985 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +02007986 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7987 return 1;
7988 }
7989
Nadav Har'El7c177932011-05-25 23:12:04 +03007990 if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
7991 !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) {
7992 /*TODO: Also verify bits beyond physical address width are 0*/
7993 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7994 return 1;
7995 }
7996
7997 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
7998 !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) {
7999 /*TODO: Also verify bits beyond physical address width are 0*/
8000 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8001 return 1;
8002 }
8003
8004 if (vmcs12->vm_entry_msr_load_count > 0 ||
8005 vmcs12->vm_exit_msr_load_count > 0 ||
8006 vmcs12->vm_exit_msr_store_count > 0) {
Jan Kiszkabd801582011-09-12 11:26:22 +02008007 pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n",
8008 __func__);
Nadav Har'El7c177932011-05-25 23:12:04 +03008009 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8010 return 1;
8011 }
8012
8013 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
8014 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) ||
8015 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
8016 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
8017 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
8018 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
8019 !vmx_control_verify(vmcs12->vm_exit_controls,
8020 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) ||
8021 !vmx_control_verify(vmcs12->vm_entry_controls,
8022 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high))
8023 {
8024 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
8025 return 1;
8026 }
8027
8028 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
8029 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
8030 nested_vmx_failValid(vcpu,
8031 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
8032 return 1;
8033 }
8034
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02008035 if (!nested_cr0_valid(vmcs12, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03008036 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
8037 nested_vmx_entry_failure(vcpu, vmcs12,
8038 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
8039 return 1;
8040 }
8041 if (vmcs12->vmcs_link_pointer != -1ull) {
8042 nested_vmx_entry_failure(vcpu, vmcs12,
8043 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
8044 return 1;
8045 }
8046
8047 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +02008048 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +02008049 * are performed on the field for the IA32_EFER MSR:
8050 * - Bits reserved in the IA32_EFER MSR must be 0.
8051 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
8052 * the IA-32e mode guest VM-exit control. It must also be identical
8053 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
8054 * CR0.PG) is 1.
8055 */
8056 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
8057 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
8058 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
8059 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
8060 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
8061 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
8062 nested_vmx_entry_failure(vcpu, vmcs12,
8063 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
8064 return 1;
8065 }
8066 }
8067
8068 /*
8069 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
8070 * IA32_EFER MSR must be 0 in the field for that register. In addition,
8071 * the values of the LMA and LME bits in the field must each be that of
8072 * the host address-space size VM-exit control.
8073 */
8074 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
8075 ia32e = (vmcs12->vm_exit_controls &
8076 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
8077 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
8078 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
8079 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
8080 nested_vmx_entry_failure(vcpu, vmcs12,
8081 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
8082 return 1;
8083 }
8084 }
8085
8086 /*
Nadav Har'El7c177932011-05-25 23:12:04 +03008087 * We're finally done with prerequisite checking, and can start with
8088 * the nested entry.
8089 */
8090
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008091 vmcs02 = nested_get_current_vmcs02(vmx);
8092 if (!vmcs02)
8093 return -ENOMEM;
8094
8095 enter_guest_mode(vcpu);
8096
8097 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
8098
8099 cpu = get_cpu();
8100 vmx->loaded_vmcs = vmcs02;
8101 vmx_vcpu_put(vcpu);
8102 vmx_vcpu_load(vcpu, cpu);
8103 vcpu->cpu = cpu;
8104 put_cpu();
8105
Jan Kiszka36c3cc42013-02-23 22:35:37 +01008106 vmx_segment_cache_clear(vmx);
8107
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008108 vmcs12->launch_state = 1;
8109
8110 prepare_vmcs02(vcpu, vmcs12);
8111
Jan Kiszka6dfacad2013-12-04 08:58:54 +01008112 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
8113 return kvm_emulate_halt(vcpu);
8114
Jan Kiszka7af40ad32014-01-04 18:47:23 +01008115 vmx->nested.nested_run_pending = 1;
8116
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008117 /*
8118 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
8119 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
8120 * returned as far as L1 is concerned. It will only return (and set
8121 * the success flag) when L2 exits (see nested_vmx_vmexit()).
8122 */
8123 return 1;
8124}
8125
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008126/*
8127 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
8128 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
8129 * This function returns the new value we should put in vmcs12.guest_cr0.
8130 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
8131 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
8132 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
8133 * didn't trap the bit, because if L1 did, so would L0).
8134 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
8135 * been modified by L2, and L1 knows it. So just leave the old value of
8136 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
8137 * isn't relevant, because if L0 traps this bit it can set it to anything.
8138 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
8139 * changed these bits, and therefore they need to be updated, but L0
8140 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
8141 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
8142 */
8143static inline unsigned long
8144vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
8145{
8146 return
8147 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
8148 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
8149 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
8150 vcpu->arch.cr0_guest_owned_bits));
8151}
8152
8153static inline unsigned long
8154vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
8155{
8156 return
8157 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
8158 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
8159 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
8160 vcpu->arch.cr4_guest_owned_bits));
8161}
8162
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008163static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
8164 struct vmcs12 *vmcs12)
8165{
8166 u32 idt_vectoring;
8167 unsigned int nr;
8168
Gleb Natapov851eb6672013-09-25 12:51:34 +03008169 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008170 nr = vcpu->arch.exception.nr;
8171 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
8172
8173 if (kvm_exception_is_soft(nr)) {
8174 vmcs12->vm_exit_instruction_len =
8175 vcpu->arch.event_exit_inst_len;
8176 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
8177 } else
8178 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
8179
8180 if (vcpu->arch.exception.has_error_code) {
8181 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
8182 vmcs12->idt_vectoring_error_code =
8183 vcpu->arch.exception.error_code;
8184 }
8185
8186 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +01008187 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008188 vmcs12->idt_vectoring_info_field =
8189 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
8190 } else if (vcpu->arch.interrupt.pending) {
8191 nr = vcpu->arch.interrupt.nr;
8192 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
8193
8194 if (vcpu->arch.interrupt.soft) {
8195 idt_vectoring |= INTR_TYPE_SOFT_INTR;
8196 vmcs12->vm_entry_instruction_len =
8197 vcpu->arch.event_exit_inst_len;
8198 } else
8199 idt_vectoring |= INTR_TYPE_EXT_INTR;
8200
8201 vmcs12->idt_vectoring_info_field = idt_vectoring;
8202 }
8203}
8204
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008205static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
8206{
8207 struct vcpu_vmx *vmx = to_vmx(vcpu);
8208
Jan Kiszkaf41245002014-03-07 20:03:13 +01008209 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
8210 vmx->nested.preemption_timer_expired) {
8211 if (vmx->nested.nested_run_pending)
8212 return -EBUSY;
8213 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
8214 return 0;
8215 }
8216
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008217 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +01008218 if (vmx->nested.nested_run_pending ||
8219 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008220 return -EBUSY;
8221 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
8222 NMI_VECTOR | INTR_TYPE_NMI_INTR |
8223 INTR_INFO_VALID_MASK, 0);
8224 /*
8225 * The NMI-triggered VM exit counts as injection:
8226 * clear this one and block further NMIs.
8227 */
8228 vcpu->arch.nmi_pending = 0;
8229 vmx_set_nmi_mask(vcpu, true);
8230 return 0;
8231 }
8232
8233 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
8234 nested_exit_on_intr(vcpu)) {
8235 if (vmx->nested.nested_run_pending)
8236 return -EBUSY;
8237 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
8238 }
8239
8240 return 0;
8241}
8242
Jan Kiszkaf41245002014-03-07 20:03:13 +01008243static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
8244{
8245 ktime_t remaining =
8246 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
8247 u64 value;
8248
8249 if (ktime_to_ns(remaining) <= 0)
8250 return 0;
8251
8252 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
8253 do_div(value, 1000000);
8254 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
8255}
8256
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008257/*
8258 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
8259 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
8260 * and this function updates it to reflect the changes to the guest state while
8261 * L2 was running (and perhaps made some exits which were handled directly by L0
8262 * without going back to L1), and to reflect the exit reason.
8263 * Note that we do not have to copy here all VMCS fields, just those that
8264 * could have changed by the L2 guest or the exit - i.e., the guest-state and
8265 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
8266 * which already writes to vmcs12 directly.
8267 */
Jan Kiszka533558b2014-01-04 18:47:20 +01008268static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
8269 u32 exit_reason, u32 exit_intr_info,
8270 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008271{
8272 /* update guest state fields: */
8273 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
8274 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
8275
8276 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
8277 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
8278 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
8279 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
8280
8281 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
8282 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
8283 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
8284 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
8285 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
8286 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
8287 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
8288 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
8289 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
8290 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
8291 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
8292 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
8293 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
8294 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
8295 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
8296 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
8297 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
8298 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
8299 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
8300 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
8301 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
8302 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
8303 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
8304 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
8305 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
8306 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
8307 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
8308 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
8309 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
8310 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
8311 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
8312 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
8313 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
8314 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
8315 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
8316 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
8317
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008318 vmcs12->guest_interruptibility_info =
8319 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
8320 vmcs12->guest_pending_dbg_exceptions =
8321 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +01008322 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
8323 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
8324 else
8325 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008326
Jan Kiszkaf41245002014-03-07 20:03:13 +01008327 if (nested_cpu_has_preemption_timer(vmcs12)) {
8328 if (vmcs12->vm_exit_controls &
8329 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
8330 vmcs12->vmx_preemption_timer_value =
8331 vmx_get_preemption_timer_value(vcpu);
8332 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
8333 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +08008334
Nadav Har'El3633cfc2013-08-05 11:07:07 +03008335 /*
8336 * In some cases (usually, nested EPT), L2 is allowed to change its
8337 * own CR3 without exiting. If it has changed it, we must keep it.
8338 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
8339 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
8340 *
8341 * Additionally, restore L2's PDPTR to vmcs12.
8342 */
8343 if (enable_ept) {
8344 vmcs12->guest_cr3 = vmcs_read64(GUEST_CR3);
8345 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
8346 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
8347 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
8348 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
8349 }
8350
Jan Kiszkac18911a2013-03-13 16:06:41 +01008351 vmcs12->vm_entry_controls =
8352 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +02008353 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +01008354
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008355 /* TODO: These cannot have changed unless we have MSR bitmaps and
8356 * the relevant bit asks not to trap the change */
8357 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Jan Kiszkab8c07d52013-04-06 13:51:21 +02008358 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008359 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +02008360 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
8361 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008362 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
8363 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
8364 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
Paolo Bonzini36be0b92014-02-24 12:30:04 +01008365 if (vmx_mpx_supported())
8366 vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008367
8368 /* update exit information fields: */
8369
Jan Kiszka533558b2014-01-04 18:47:20 +01008370 vmcs12->vm_exit_reason = exit_reason;
8371 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008372
Jan Kiszka533558b2014-01-04 18:47:20 +01008373 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +02008374 if ((vmcs12->vm_exit_intr_info &
8375 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8376 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
8377 vmcs12->vm_exit_intr_error_code =
8378 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008379 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008380 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
8381 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8382
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008383 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
8384 /* vm_entry_intr_info_field is cleared on exit. Emulate this
8385 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008386 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008387
8388 /*
8389 * Transfer the event that L0 or L1 may wanted to inject into
8390 * L2 to IDT_VECTORING_INFO_FIELD.
8391 */
8392 vmcs12_save_pending_event(vcpu, vmcs12);
8393 }
8394
8395 /*
8396 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
8397 * preserved above and would only end up incorrectly in L1.
8398 */
8399 vcpu->arch.nmi_injected = false;
8400 kvm_clear_exception_queue(vcpu);
8401 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008402}
8403
8404/*
8405 * A part of what we need to when the nested L2 guest exits and we want to
8406 * run its L1 parent, is to reset L1's guest state to the host state specified
8407 * in vmcs12.
8408 * This function is to be called not only on normal nested exit, but also on
8409 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
8410 * Failures During or After Loading Guest State").
8411 * This function should be called when the active VMCS is L1's (vmcs01).
8412 */
Jan Kiszka733568f2013-02-23 15:07:47 +01008413static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
8414 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008415{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08008416 struct kvm_segment seg;
8417
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008418 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
8419 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +02008420 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008421 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
8422 else
8423 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
8424 vmx_set_efer(vcpu, vcpu->arch.efer);
8425
8426 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
8427 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -07008428 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008429 /*
8430 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
8431 * actually changed, because it depends on the current state of
8432 * fpu_active (which may have changed).
8433 * Note that vmx_set_cr0 refers to efer set above.
8434 */
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +02008435 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008436 /*
8437 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
8438 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
8439 * but we also need to update cr0_guest_host_mask and exception_bitmap.
8440 */
8441 update_exception_bitmap(vcpu);
8442 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
8443 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
8444
8445 /*
8446 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
8447 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
8448 */
8449 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
8450 kvm_set_cr4(vcpu, vmcs12->host_cr4);
8451
Jan Kiszka29bf08f2013-12-28 16:31:52 +01008452 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +03008453
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008454 kvm_set_cr3(vcpu, vmcs12->host_cr3);
8455 kvm_mmu_reset_context(vcpu);
8456
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03008457 if (!enable_ept)
8458 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
8459
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008460 if (enable_vpid) {
8461 /*
8462 * Trivially support vpid by letting L2s share their parent
8463 * L1's vpid. TODO: move to a more elaborate solution, giving
8464 * each L2 its own vpid and exposing the vpid feature to L1.
8465 */
8466 vmx_flush_tlb(vcpu);
8467 }
8468
8469
8470 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
8471 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
8472 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
8473 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
8474 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008475
Paolo Bonzini36be0b92014-02-24 12:30:04 +01008476 /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */
8477 if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS)
8478 vmcs_write64(GUEST_BNDCFGS, 0);
8479
Jan Kiszka44811c02013-08-04 17:17:27 +02008480 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008481 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02008482 vcpu->arch.pat = vmcs12->host_ia32_pat;
8483 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008484 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
8485 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
8486 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +01008487
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08008488 /* Set L1 segment info according to Intel SDM
8489 27.5.2 Loading Host Segment and Descriptor-Table Registers */
8490 seg = (struct kvm_segment) {
8491 .base = 0,
8492 .limit = 0xFFFFFFFF,
8493 .selector = vmcs12->host_cs_selector,
8494 .type = 11,
8495 .present = 1,
8496 .s = 1,
8497 .g = 1
8498 };
8499 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
8500 seg.l = 1;
8501 else
8502 seg.db = 1;
8503 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
8504 seg = (struct kvm_segment) {
8505 .base = 0,
8506 .limit = 0xFFFFFFFF,
8507 .type = 3,
8508 .present = 1,
8509 .s = 1,
8510 .db = 1,
8511 .g = 1
8512 };
8513 seg.selector = vmcs12->host_ds_selector;
8514 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
8515 seg.selector = vmcs12->host_es_selector;
8516 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
8517 seg.selector = vmcs12->host_ss_selector;
8518 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
8519 seg.selector = vmcs12->host_fs_selector;
8520 seg.base = vmcs12->host_fs_base;
8521 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
8522 seg.selector = vmcs12->host_gs_selector;
8523 seg.base = vmcs12->host_gs_base;
8524 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
8525 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +03008526 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08008527 .limit = 0x67,
8528 .selector = vmcs12->host_tr_selector,
8529 .type = 11,
8530 .present = 1
8531 };
8532 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
8533
Jan Kiszka503cd0c2013-03-03 13:05:44 +01008534 kvm_set_dr(vcpu, 7, 0x400);
8535 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008536}
8537
8538/*
8539 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
8540 * and modify vmcs12 to make it see what it would expect to see there if
8541 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
8542 */
Jan Kiszka533558b2014-01-04 18:47:20 +01008543static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
8544 u32 exit_intr_info,
8545 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008546{
8547 struct vcpu_vmx *vmx = to_vmx(vcpu);
8548 int cpu;
8549 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8550
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008551 /* trying to cancel vmlaunch/vmresume is a bug */
8552 WARN_ON_ONCE(vmx->nested.nested_run_pending);
8553
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008554 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +01008555 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
8556 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008557
Jan Kiszka542060e2014-01-04 18:47:21 +01008558 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
8559 vmcs12->exit_qualification,
8560 vmcs12->idt_vectoring_info_field,
8561 vmcs12->vm_exit_intr_info,
8562 vmcs12->vm_exit_intr_error_code,
8563 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008564
8565 cpu = get_cpu();
8566 vmx->loaded_vmcs = &vmx->vmcs01;
8567 vmx_vcpu_put(vcpu);
8568 vmx_vcpu_load(vcpu, cpu);
8569 vcpu->cpu = cpu;
8570 put_cpu();
8571
Gleb Natapov2961e8762013-11-25 15:37:13 +02008572 vm_entry_controls_init(vmx, vmcs_read32(VM_ENTRY_CONTROLS));
8573 vm_exit_controls_init(vmx, vmcs_read32(VM_EXIT_CONTROLS));
Jan Kiszka36c3cc42013-02-23 22:35:37 +01008574 vmx_segment_cache_clear(vmx);
8575
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008576 /* if no vmcs02 cache requested, remove the one we used */
8577 if (VMCS02_POOL_SIZE == 0)
8578 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
8579
8580 load_vmcs12_host_state(vcpu, vmcs12);
8581
Nadav Har'El27fc51b2011-08-02 15:54:52 +03008582 /* Update TSC_OFFSET if TSC was changed while L2 ran */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008583 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
8584
8585 /* This is needed for same reason as it was needed in prepare_vmcs02 */
8586 vmx->host_rsp = 0;
8587
8588 /* Unpin physical memory we referred to in vmcs02 */
8589 if (vmx->nested.apic_access_page) {
8590 nested_release_page(vmx->nested.apic_access_page);
8591 vmx->nested.apic_access_page = 0;
8592 }
8593
8594 /*
8595 * Exiting from L2 to L1, we're now back to L1 which thinks it just
8596 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
8597 * success or failure flag accordingly.
8598 */
8599 if (unlikely(vmx->fail)) {
8600 vmx->fail = 0;
8601 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
8602 } else
8603 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +03008604 if (enable_shadow_vmcs)
8605 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008606
8607 /* in case we halted in L2 */
8608 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008609}
8610
Nadav Har'El7c177932011-05-25 23:12:04 +03008611/*
Jan Kiszka42124922014-01-04 18:47:19 +01008612 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
8613 */
8614static void vmx_leave_nested(struct kvm_vcpu *vcpu)
8615{
8616 if (is_guest_mode(vcpu))
Jan Kiszka533558b2014-01-04 18:47:20 +01008617 nested_vmx_vmexit(vcpu, -1, 0, 0);
Jan Kiszka42124922014-01-04 18:47:19 +01008618 free_nested(to_vmx(vcpu));
8619}
8620
8621/*
Nadav Har'El7c177932011-05-25 23:12:04 +03008622 * L1's failure to enter L2 is a subset of a normal exit, as explained in
8623 * 23.7 "VM-entry failures during or after loading guest state" (this also
8624 * lists the acceptable exit-reason and exit-qualification parameters).
8625 * It should only be called before L2 actually succeeded to run, and when
8626 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
8627 */
8628static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
8629 struct vmcs12 *vmcs12,
8630 u32 reason, unsigned long qualification)
8631{
8632 load_vmcs12_host_state(vcpu, vmcs12);
8633 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
8634 vmcs12->exit_qualification = qualification;
8635 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +03008636 if (enable_shadow_vmcs)
8637 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +03008638}
8639
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02008640static int vmx_check_intercept(struct kvm_vcpu *vcpu,
8641 struct x86_instruction_info *info,
8642 enum x86_intercept_stage stage)
8643{
8644 return X86EMUL_CONTINUE;
8645}
8646
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03008647static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008648 .cpu_has_kvm_support = cpu_has_kvm_support,
8649 .disabled_by_bios = vmx_disabled_by_bios,
8650 .hardware_setup = hardware_setup,
8651 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03008652 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008653 .hardware_enable = hardware_enable,
8654 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08008655 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008656
8657 .vcpu_create = vmx_create_vcpu,
8658 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03008659 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008660
Avi Kivity04d2cc72007-09-10 18:10:54 +03008661 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008662 .vcpu_load = vmx_vcpu_load,
8663 .vcpu_put = vmx_vcpu_put,
8664
Jan Kiszkac8639012012-09-21 05:42:55 +02008665 .update_db_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008666 .get_msr = vmx_get_msr,
8667 .set_msr = vmx_set_msr,
8668 .get_segment_base = vmx_get_segment_base,
8669 .get_segment = vmx_get_segment,
8670 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02008671 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008672 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02008673 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02008674 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03008675 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008676 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008677 .set_cr3 = vmx_set_cr3,
8678 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008679 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008680 .get_idt = vmx_get_idt,
8681 .set_idt = vmx_set_idt,
8682 .get_gdt = vmx_get_gdt,
8683 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01008684 .get_dr6 = vmx_get_dr6,
8685 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03008686 .set_dr7 = vmx_set_dr7,
Paolo Bonzini81908bf2014-02-21 10:32:27 +01008687 .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008688 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008689 .get_rflags = vmx_get_rflags,
8690 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02008691 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02008692 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008693
8694 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008695
Avi Kivity6aa8b732006-12-10 02:21:36 -08008696 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02008697 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008698 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04008699 .set_interrupt_shadow = vmx_set_interrupt_shadow,
8700 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02008701 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03008702 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008703 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02008704 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008705 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02008706 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008707 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01008708 .get_nmi_mask = vmx_get_nmi_mask,
8709 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008710 .enable_nmi_window = enable_nmi_window,
8711 .enable_irq_window = enable_irq_window,
8712 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08008713 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008714 .vm_has_apicv = vmx_vm_has_apicv,
8715 .load_eoi_exitmap = vmx_load_eoi_exitmap,
8716 .hwapic_irr_update = vmx_hwapic_irr_update,
8717 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +08008718 .sync_pir_to_irr = vmx_sync_pir_to_irr,
8719 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008720
Izik Eiduscbc94022007-10-25 00:29:55 +02008721 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08008722 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08008723 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008724
Avi Kivity586f9602010-11-18 13:09:54 +02008725 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02008726
Sheng Yang17cc3932010-01-05 19:02:27 +08008727 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08008728
8729 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08008730
8731 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00008732 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02008733
8734 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08008735
8736 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10008737
Joerg Roedel4051b182011-03-25 09:44:49 +01008738 .set_tsc_khz = vmx_set_tsc_khz,
Will Auldba904632012-11-29 12:42:50 -08008739 .read_tsc_offset = vmx_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10008740 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10008741 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01008742 .compute_tsc_offset = vmx_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03008743 .read_l1_tsc = vmx_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02008744
8745 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02008746
8747 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08008748 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008749 .mpx_supported = vmx_mpx_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008750
8751 .check_nested_events = vmx_check_nested_events,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008752};
8753
8754static int __init vmx_init(void)
8755{
Yang Zhang8d146952013-01-25 10:18:50 +08008756 int r, i, msr;
Avi Kivity26bb0982009-09-07 11:14:12 +03008757
8758 rdmsrl_safe(MSR_EFER, &host_efer);
8759
8760 for (i = 0; i < NR_VMX_MSR; ++i)
8761 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03008762
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008763 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03008764 if (!vmx_io_bitmap_a)
8765 return -ENOMEM;
8766
Guo Chao2106a542012-06-15 11:31:56 +08008767 r = -ENOMEM;
8768
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008769 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008770 if (!vmx_io_bitmap_b)
He, Qingfdef3ad2007-04-30 09:45:24 +03008771 goto out;
He, Qingfdef3ad2007-04-30 09:45:24 +03008772
Avi Kivity58972972009-02-24 22:26:47 +02008773 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008774 if (!vmx_msr_bitmap_legacy)
Sheng Yang25c5f222008-03-28 13:18:56 +08008775 goto out1;
Guo Chao2106a542012-06-15 11:31:56 +08008776
Yang Zhang8d146952013-01-25 10:18:50 +08008777 vmx_msr_bitmap_legacy_x2apic =
8778 (unsigned long *)__get_free_page(GFP_KERNEL);
8779 if (!vmx_msr_bitmap_legacy_x2apic)
8780 goto out2;
Sheng Yang25c5f222008-03-28 13:18:56 +08008781
Avi Kivity58972972009-02-24 22:26:47 +02008782 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008783 if (!vmx_msr_bitmap_longmode)
Yang Zhang8d146952013-01-25 10:18:50 +08008784 goto out3;
Guo Chao2106a542012-06-15 11:31:56 +08008785
Yang Zhang8d146952013-01-25 10:18:50 +08008786 vmx_msr_bitmap_longmode_x2apic =
8787 (unsigned long *)__get_free_page(GFP_KERNEL);
8788 if (!vmx_msr_bitmap_longmode_x2apic)
8789 goto out4;
Abel Gordon4607c2d2013-04-18 14:35:55 +03008790 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
8791 if (!vmx_vmread_bitmap)
8792 goto out5;
8793
8794 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
8795 if (!vmx_vmwrite_bitmap)
8796 goto out6;
8797
8798 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
8799 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
8800 /* shadowed read/write fields */
8801 for (i = 0; i < max_shadow_read_write_fields; i++) {
8802 clear_bit(shadow_read_write_fields[i], vmx_vmwrite_bitmap);
8803 clear_bit(shadow_read_write_fields[i], vmx_vmread_bitmap);
8804 }
8805 /* shadowed read only fields */
8806 for (i = 0; i < max_shadow_read_only_fields; i++)
8807 clear_bit(shadow_read_only_fields[i], vmx_vmread_bitmap);
Avi Kivity58972972009-02-24 22:26:47 +02008808
He, Qingfdef3ad2007-04-30 09:45:24 +03008809 /*
8810 * Allow direct access to the PC debug port (it is often used for I/O
8811 * delays, but the vmexits simply slow things down).
8812 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008813 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
8814 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03008815
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008816 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03008817
Avi Kivity58972972009-02-24 22:26:47 +02008818 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
8819 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08008820
Sheng Yang2384d2b2008-01-17 15:14:33 +08008821 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
8822
Avi Kivity0ee75be2010-04-28 15:39:01 +03008823 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
8824 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03008825 if (r)
Abel Gordon4607c2d2013-04-18 14:35:55 +03008826 goto out7;
Sheng Yang25c5f222008-03-28 13:18:56 +08008827
Zhang Yanfei8f536b72012-12-06 23:43:34 +08008828#ifdef CONFIG_KEXEC
8829 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
8830 crash_vmclear_local_loaded_vmcss);
8831#endif
8832
Avi Kivity58972972009-02-24 22:26:47 +02008833 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
8834 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
8835 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
8836 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
8837 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
8838 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008839 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
8840
Yang Zhang8d146952013-01-25 10:18:50 +08008841 memcpy(vmx_msr_bitmap_legacy_x2apic,
8842 vmx_msr_bitmap_legacy, PAGE_SIZE);
8843 memcpy(vmx_msr_bitmap_longmode_x2apic,
8844 vmx_msr_bitmap_longmode, PAGE_SIZE);
8845
Yang Zhang01e439b2013-04-11 19:25:12 +08008846 if (enable_apicv) {
Yang Zhang8d146952013-01-25 10:18:50 +08008847 for (msr = 0x800; msr <= 0x8ff; msr++)
8848 vmx_disable_intercept_msr_read_x2apic(msr);
8849
8850 /* According SDM, in x2apic mode, the whole id reg is used.
8851 * But in KVM, it only use the highest eight bits. Need to
8852 * intercept it */
8853 vmx_enable_intercept_msr_read_x2apic(0x802);
8854 /* TMCCT */
8855 vmx_enable_intercept_msr_read_x2apic(0x839);
8856 /* TPR */
8857 vmx_disable_intercept_msr_write_x2apic(0x808);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008858 /* EOI */
8859 vmx_disable_intercept_msr_write_x2apic(0x80b);
8860 /* SELF-IPI */
8861 vmx_disable_intercept_msr_write_x2apic(0x83f);
Yang Zhang8d146952013-01-25 10:18:50 +08008862 }
He, Qingfdef3ad2007-04-30 09:45:24 +03008863
Avi Kivity089d0342009-03-23 18:26:32 +02008864 if (enable_ept) {
Xudong Hao3f6d8c82012-05-22 11:23:15 +08008865 kvm_mmu_set_mask_ptes(0ull,
8866 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
8867 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
8868 0ull, VMX_EPT_EXECUTABLE_MASK);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08008869 ept_set_mmio_spte_mask();
Sheng Yang5fdbcb92008-07-16 09:25:40 +08008870 kvm_enable_tdp();
8871 } else
8872 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08008873
He, Qingfdef3ad2007-04-30 09:45:24 +03008874 return 0;
8875
Abel Gordon4607c2d2013-04-18 14:35:55 +03008876out7:
8877 free_page((unsigned long)vmx_vmwrite_bitmap);
8878out6:
8879 free_page((unsigned long)vmx_vmread_bitmap);
Yang Zhang458f2122013-04-08 15:26:33 +08008880out5:
8881 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Yang Zhang8d146952013-01-25 10:18:50 +08008882out4:
Avi Kivity58972972009-02-24 22:26:47 +02008883 free_page((unsigned long)vmx_msr_bitmap_longmode);
Yang Zhang8d146952013-01-25 10:18:50 +08008884out3:
8885 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
Sheng Yang25c5f222008-03-28 13:18:56 +08008886out2:
Avi Kivity58972972009-02-24 22:26:47 +02008887 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03008888out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008889 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03008890out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008891 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03008892 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008893}
8894
8895static void __exit vmx_exit(void)
8896{
Yang Zhang8d146952013-01-25 10:18:50 +08008897 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
8898 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Avi Kivity58972972009-02-24 22:26:47 +02008899 free_page((unsigned long)vmx_msr_bitmap_legacy);
8900 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008901 free_page((unsigned long)vmx_io_bitmap_b);
8902 free_page((unsigned long)vmx_io_bitmap_a);
Abel Gordon4607c2d2013-04-18 14:35:55 +03008903 free_page((unsigned long)vmx_vmwrite_bitmap);
8904 free_page((unsigned long)vmx_vmread_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03008905
Zhang Yanfei8f536b72012-12-06 23:43:34 +08008906#ifdef CONFIG_KEXEC
8907 rcu_assign_pointer(crash_vmclear_loaded_vmcss, NULL);
8908 synchronize_rcu();
8909#endif
8910
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08008911 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08008912}
8913
8914module_init(vmx_init)
8915module_exit(vmx_exit)