blob: fcc1947db29671758c9b76a0efe5e7a70d617a7d [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>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080046#include <asm/kexec.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080047
Marcelo Tosatti229456f2009-06-17 09:22:14 -030048#include "trace.h"
49
Avi Kivity4ecac3f2008-05-13 13:23:38 +030050#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040051#define __ex_clear(x, reg) \
52 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030053
Avi Kivity6aa8b732006-12-10 02:21:36 -080054MODULE_AUTHOR("Qumranet");
55MODULE_LICENSE("GPL");
56
Josh Triplette9bda3b2012-03-20 23:33:51 -070057static const struct x86_cpu_id vmx_cpu_id[] = {
58 X86_FEATURE_MATCH(X86_FEATURE_VMX),
59 {}
60};
61MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
62
Rusty Russell476bc002012-01-13 09:32:18 +103063static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020064module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080065
Rusty Russell476bc002012-01-13 09:32:18 +103066static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020067module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020068
Rusty Russell476bc002012-01-13 09:32:18 +103069static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020070module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080071
Rusty Russell476bc002012-01-13 09:32:18 +103072static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070073module_param_named(unrestricted_guest,
74 enable_unrestricted_guest, bool, S_IRUGO);
75
Xudong Hao83c3a332012-05-28 19:33:35 +080076static bool __read_mostly enable_ept_ad_bits = 1;
77module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
78
Avi Kivitya27685c2012-06-12 20:30:18 +030079static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020080module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030081
Rusty Russell476bc002012-01-13 09:32:18 +103082static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080083module_param(vmm_exclusive, bool, S_IRUGO);
84
Rusty Russell476bc002012-01-13 09:32:18 +103085static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030086module_param(fasteoi, bool, S_IRUGO);
87
Yang Zhang5a717852013-04-11 19:25:16 +080088static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080089module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080090
Abel Gordonabc4fc52013-04-18 14:35:25 +030091static bool __read_mostly enable_shadow_vmcs = 1;
92module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +030093/*
94 * If nested=1, nested virtualization is supported, i.e., guests may use
95 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
96 * use VMX instructions.
97 */
Rusty Russell476bc002012-01-13 09:32:18 +103098static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +030099module_param(nested, bool, S_IRUGO);
100
Gleb Natapov50378782013-02-04 16:00:28 +0200101#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
102#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200103#define KVM_VM_CR0_ALWAYS_ON \
104 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200105#define KVM_CR4_GUEST_OWNED_BITS \
106 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
107 | X86_CR4_OSXMMEXCPT)
108
Avi Kivitycdc0e242009-12-06 17:21:14 +0200109#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
110#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
111
Avi Kivity78ac8b42010-04-08 18:19:35 +0300112#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
113
Jan Kiszkaf41245002014-03-07 20:03:13 +0100114#define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5
115
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800116/*
117 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
118 * ple_gap: upper bound on the amount of time between two successive
119 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500120 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800121 * ple_window: upper bound on the amount of time a guest is allowed to execute
122 * in a PAUSE loop. Tests indicate that most spinlocks are held for
123 * less than 2^12 cycles
124 * Time is measured based on a counter that runs at the same rate as the TSC,
125 * refer SDM volume 3b section 21.6.13 & 22.1.3.
126 */
Rik van Riel00c25bc2011-01-04 09:51:33 -0500127#define KVM_VMX_DEFAULT_PLE_GAP 128
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800128#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
129static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
130module_param(ple_gap, int, S_IRUGO);
131
132static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
133module_param(ple_window, int, S_IRUGO);
134
Avi Kivity83287ea422012-09-16 15:10:57 +0300135extern const ulong vmx_return;
136
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200137#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300138#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300139
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400140struct vmcs {
141 u32 revision_id;
142 u32 abort;
143 char data[0];
144};
145
Nadav Har'Eld462b812011-05-24 15:26:10 +0300146/*
147 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
148 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
149 * loaded on this CPU (so we can clear them if the CPU goes down).
150 */
151struct loaded_vmcs {
152 struct vmcs *vmcs;
153 int cpu;
154 int launched;
155 struct list_head loaded_vmcss_on_cpu_link;
156};
157
Avi Kivity26bb0982009-09-07 11:14:12 +0300158struct shared_msr_entry {
159 unsigned index;
160 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200161 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300162};
163
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300164/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300165 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
166 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
167 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
168 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
169 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
170 * More than one of these structures may exist, if L1 runs multiple L2 guests.
171 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
172 * underlying hardware which will be used to run L2.
173 * This structure is packed to ensure that its layout is identical across
174 * machines (necessary for live migration).
175 * If there are changes in this struct, VMCS12_REVISION must be changed.
176 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300177typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300178struct __packed vmcs12 {
179 /* According to the Intel spec, a VMCS region must start with the
180 * following two fields. Then follow implementation-specific data.
181 */
182 u32 revision_id;
183 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300184
Nadav Har'El27d6c862011-05-25 23:06:59 +0300185 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
186 u32 padding[7]; /* room for future expansion */
187
Nadav Har'El22bd0352011-05-25 23:05:57 +0300188 u64 io_bitmap_a;
189 u64 io_bitmap_b;
190 u64 msr_bitmap;
191 u64 vm_exit_msr_store_addr;
192 u64 vm_exit_msr_load_addr;
193 u64 vm_entry_msr_load_addr;
194 u64 tsc_offset;
195 u64 virtual_apic_page_addr;
196 u64 apic_access_addr;
197 u64 ept_pointer;
198 u64 guest_physical_address;
199 u64 vmcs_link_pointer;
200 u64 guest_ia32_debugctl;
201 u64 guest_ia32_pat;
202 u64 guest_ia32_efer;
203 u64 guest_ia32_perf_global_ctrl;
204 u64 guest_pdptr0;
205 u64 guest_pdptr1;
206 u64 guest_pdptr2;
207 u64 guest_pdptr3;
208 u64 host_ia32_pat;
209 u64 host_ia32_efer;
210 u64 host_ia32_perf_global_ctrl;
211 u64 padding64[8]; /* room for future expansion */
212 /*
213 * To allow migration of L1 (complete with its L2 guests) between
214 * machines of different natural widths (32 or 64 bit), we cannot have
215 * unsigned long fields with no explict size. We use u64 (aliased
216 * natural_width) instead. Luckily, x86 is little-endian.
217 */
218 natural_width cr0_guest_host_mask;
219 natural_width cr4_guest_host_mask;
220 natural_width cr0_read_shadow;
221 natural_width cr4_read_shadow;
222 natural_width cr3_target_value0;
223 natural_width cr3_target_value1;
224 natural_width cr3_target_value2;
225 natural_width cr3_target_value3;
226 natural_width exit_qualification;
227 natural_width guest_linear_address;
228 natural_width guest_cr0;
229 natural_width guest_cr3;
230 natural_width guest_cr4;
231 natural_width guest_es_base;
232 natural_width guest_cs_base;
233 natural_width guest_ss_base;
234 natural_width guest_ds_base;
235 natural_width guest_fs_base;
236 natural_width guest_gs_base;
237 natural_width guest_ldtr_base;
238 natural_width guest_tr_base;
239 natural_width guest_gdtr_base;
240 natural_width guest_idtr_base;
241 natural_width guest_dr7;
242 natural_width guest_rsp;
243 natural_width guest_rip;
244 natural_width guest_rflags;
245 natural_width guest_pending_dbg_exceptions;
246 natural_width guest_sysenter_esp;
247 natural_width guest_sysenter_eip;
248 natural_width host_cr0;
249 natural_width host_cr3;
250 natural_width host_cr4;
251 natural_width host_fs_base;
252 natural_width host_gs_base;
253 natural_width host_tr_base;
254 natural_width host_gdtr_base;
255 natural_width host_idtr_base;
256 natural_width host_ia32_sysenter_esp;
257 natural_width host_ia32_sysenter_eip;
258 natural_width host_rsp;
259 natural_width host_rip;
260 natural_width paddingl[8]; /* room for future expansion */
261 u32 pin_based_vm_exec_control;
262 u32 cpu_based_vm_exec_control;
263 u32 exception_bitmap;
264 u32 page_fault_error_code_mask;
265 u32 page_fault_error_code_match;
266 u32 cr3_target_count;
267 u32 vm_exit_controls;
268 u32 vm_exit_msr_store_count;
269 u32 vm_exit_msr_load_count;
270 u32 vm_entry_controls;
271 u32 vm_entry_msr_load_count;
272 u32 vm_entry_intr_info_field;
273 u32 vm_entry_exception_error_code;
274 u32 vm_entry_instruction_len;
275 u32 tpr_threshold;
276 u32 secondary_vm_exec_control;
277 u32 vm_instruction_error;
278 u32 vm_exit_reason;
279 u32 vm_exit_intr_info;
280 u32 vm_exit_intr_error_code;
281 u32 idt_vectoring_info_field;
282 u32 idt_vectoring_error_code;
283 u32 vm_exit_instruction_len;
284 u32 vmx_instruction_info;
285 u32 guest_es_limit;
286 u32 guest_cs_limit;
287 u32 guest_ss_limit;
288 u32 guest_ds_limit;
289 u32 guest_fs_limit;
290 u32 guest_gs_limit;
291 u32 guest_ldtr_limit;
292 u32 guest_tr_limit;
293 u32 guest_gdtr_limit;
294 u32 guest_idtr_limit;
295 u32 guest_es_ar_bytes;
296 u32 guest_cs_ar_bytes;
297 u32 guest_ss_ar_bytes;
298 u32 guest_ds_ar_bytes;
299 u32 guest_fs_ar_bytes;
300 u32 guest_gs_ar_bytes;
301 u32 guest_ldtr_ar_bytes;
302 u32 guest_tr_ar_bytes;
303 u32 guest_interruptibility_info;
304 u32 guest_activity_state;
305 u32 guest_sysenter_cs;
306 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100307 u32 vmx_preemption_timer_value;
308 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300309 u16 virtual_processor_id;
310 u16 guest_es_selector;
311 u16 guest_cs_selector;
312 u16 guest_ss_selector;
313 u16 guest_ds_selector;
314 u16 guest_fs_selector;
315 u16 guest_gs_selector;
316 u16 guest_ldtr_selector;
317 u16 guest_tr_selector;
318 u16 host_es_selector;
319 u16 host_cs_selector;
320 u16 host_ss_selector;
321 u16 host_ds_selector;
322 u16 host_fs_selector;
323 u16 host_gs_selector;
324 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300325};
326
327/*
328 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
329 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
330 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
331 */
332#define VMCS12_REVISION 0x11e57ed0
333
334/*
335 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
336 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
337 * current implementation, 4K are reserved to avoid future complications.
338 */
339#define VMCS12_SIZE 0x1000
340
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300341/* Used to remember the last vmcs02 used for some recently used vmcs12s */
342struct vmcs02_list {
343 struct list_head list;
344 gpa_t vmptr;
345 struct loaded_vmcs vmcs02;
346};
347
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300348/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300349 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
350 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
351 */
352struct nested_vmx {
353 /* Has the level1 guest done vmxon? */
354 bool vmxon;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300355
356 /* The guest-physical address of the current VMCS L1 keeps for L2 */
357 gpa_t current_vmptr;
358 /* The host-usable pointer to the above */
359 struct page *current_vmcs12_page;
360 struct vmcs12 *current_vmcs12;
Abel Gordon8de48832013-04-18 14:37:25 +0300361 struct vmcs *current_shadow_vmcs;
Abel Gordon012f83c2013-04-18 14:39:25 +0300362 /*
363 * Indicates if the shadow vmcs must be updated with the
364 * data hold by vmcs12
365 */
366 bool sync_shadow_vmcs;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300367
368 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
369 struct list_head vmcs02_pool;
370 int vmcs02_num;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300371 u64 vmcs01_tsc_offset;
Nadav Har'El644d7112011-05-25 23:12:35 +0300372 /* L2 must run next, and mustn't decide to exit to L1. */
373 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300374 /*
375 * Guest pages referred to in vmcs02 with host-physical pointers, so
376 * we must keep them pinned while L2 runs.
377 */
378 struct page *apic_access_page;
Nadav Har'Elb3897a42013-07-08 19:12:35 +0800379 u64 msr_ia32_feature_control;
Jan Kiszkaf41245002014-03-07 20:03:13 +0100380
381 struct hrtimer preemption_timer;
382 bool preemption_timer_expired;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300383};
384
Yang Zhang01e439b2013-04-11 19:25:12 +0800385#define POSTED_INTR_ON 0
386/* Posted-Interrupt Descriptor */
387struct pi_desc {
388 u32 pir[8]; /* Posted interrupt requested */
389 u32 control; /* bit 0 of control is outstanding notification bit */
390 u32 rsvd[7];
391} __aligned(64);
392
Yang Zhanga20ed542013-04-11 19:25:15 +0800393static bool pi_test_and_set_on(struct pi_desc *pi_desc)
394{
395 return test_and_set_bit(POSTED_INTR_ON,
396 (unsigned long *)&pi_desc->control);
397}
398
399static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
400{
401 return test_and_clear_bit(POSTED_INTR_ON,
402 (unsigned long *)&pi_desc->control);
403}
404
405static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
406{
407 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
408}
409
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400410struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000411 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300412 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300413 u8 fail;
Avi Kivity69c73022011-03-07 15:26:44 +0200414 u8 cpl;
Avi Kivity9d58b932011-03-07 16:52:07 +0200415 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300416 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200417 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200418 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300419 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400420 int nmsrs;
421 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800422 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400423#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300424 u64 msr_host_kernel_gs_base;
425 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400426#endif
Gleb Natapov2961e8762013-11-25 15:37:13 +0200427 u32 vm_entry_controls_shadow;
428 u32 vm_exit_controls_shadow;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300429 /*
430 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
431 * non-nested (L1) guest, it always points to vmcs01. For a nested
432 * guest (L2), it points to a different VMCS.
433 */
434 struct loaded_vmcs vmcs01;
435 struct loaded_vmcs *loaded_vmcs;
436 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300437 struct msr_autoload {
438 unsigned nr;
439 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
440 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
441 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400442 struct {
443 int loaded;
444 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300445#ifdef CONFIG_X86_64
446 u16 ds_sel, es_sel;
447#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200448 int gs_ldt_reload_needed;
449 int fs_reload_needed;
Liu, Jinsongda8999d2014-02-24 10:55:46 +0000450 u64 msr_host_bndcfgs;
Mike Dayd77c26f2007-10-08 09:02:08 -0400451 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200452 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300453 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300454 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300455 struct kvm_segment segs[8];
456 } rmode;
457 struct {
458 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300459 struct kvm_save_segment {
460 u16 selector;
461 unsigned long base;
462 u32 limit;
463 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300464 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300465 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800466 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300467 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200468
469 /* Support for vnmi-less CPUs */
470 int soft_vnmi_blocked;
471 ktime_t entry_time;
472 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800473 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800474
475 bool rdtscp_enabled;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300476
Yang Zhang01e439b2013-04-11 19:25:12 +0800477 /* Posted interrupt descriptor */
478 struct pi_desc pi_desc;
479
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300480 /* Support for a guest hypervisor (nested VMX) */
481 struct nested_vmx nested;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400482};
483
Avi Kivity2fb92db2011-04-27 19:42:18 +0300484enum segment_cache_field {
485 SEG_FIELD_SEL = 0,
486 SEG_FIELD_BASE = 1,
487 SEG_FIELD_LIMIT = 2,
488 SEG_FIELD_AR = 3,
489
490 SEG_FIELD_NR = 4
491};
492
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400493static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
494{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000495 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400496}
497
Nadav Har'El22bd0352011-05-25 23:05:57 +0300498#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
499#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
500#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
501 [number##_HIGH] = VMCS12_OFFSET(name)+4
502
Abel Gordon4607c2d2013-04-18 14:35:55 +0300503
504static const unsigned long shadow_read_only_fields[] = {
505 /*
506 * We do NOT shadow fields that are modified when L0
507 * traps and emulates any vmx instruction (e.g. VMPTRLD,
508 * VMXON...) executed by L1.
509 * For example, VM_INSTRUCTION_ERROR is read
510 * by L1 if a vmx instruction fails (part of the error path).
511 * Note the code assumes this logic. If for some reason
512 * we start shadowing these fields then we need to
513 * force a shadow sync when L0 emulates vmx instructions
514 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
515 * by nested_vmx_failValid)
516 */
517 VM_EXIT_REASON,
518 VM_EXIT_INTR_INFO,
519 VM_EXIT_INSTRUCTION_LEN,
520 IDT_VECTORING_INFO_FIELD,
521 IDT_VECTORING_ERROR_CODE,
522 VM_EXIT_INTR_ERROR_CODE,
523 EXIT_QUALIFICATION,
524 GUEST_LINEAR_ADDRESS,
525 GUEST_PHYSICAL_ADDRESS
526};
527static const int max_shadow_read_only_fields =
528 ARRAY_SIZE(shadow_read_only_fields);
529
530static const unsigned long shadow_read_write_fields[] = {
531 GUEST_RIP,
532 GUEST_RSP,
533 GUEST_CR0,
534 GUEST_CR3,
535 GUEST_CR4,
536 GUEST_INTERRUPTIBILITY_INFO,
537 GUEST_RFLAGS,
538 GUEST_CS_SELECTOR,
539 GUEST_CS_AR_BYTES,
540 GUEST_CS_LIMIT,
541 GUEST_CS_BASE,
542 GUEST_ES_BASE,
543 CR0_GUEST_HOST_MASK,
544 CR0_READ_SHADOW,
545 CR4_READ_SHADOW,
546 TSC_OFFSET,
547 EXCEPTION_BITMAP,
548 CPU_BASED_VM_EXEC_CONTROL,
549 VM_ENTRY_EXCEPTION_ERROR_CODE,
550 VM_ENTRY_INTR_INFO_FIELD,
551 VM_ENTRY_INSTRUCTION_LEN,
552 VM_ENTRY_EXCEPTION_ERROR_CODE,
553 HOST_FS_BASE,
554 HOST_GS_BASE,
555 HOST_FS_SELECTOR,
556 HOST_GS_SELECTOR
557};
558static const int max_shadow_read_write_fields =
559 ARRAY_SIZE(shadow_read_write_fields);
560
Mathias Krause772e0312012-08-30 01:30:19 +0200561static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300562 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
563 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
564 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
565 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
566 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
567 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
568 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
569 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
570 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
571 FIELD(HOST_ES_SELECTOR, host_es_selector),
572 FIELD(HOST_CS_SELECTOR, host_cs_selector),
573 FIELD(HOST_SS_SELECTOR, host_ss_selector),
574 FIELD(HOST_DS_SELECTOR, host_ds_selector),
575 FIELD(HOST_FS_SELECTOR, host_fs_selector),
576 FIELD(HOST_GS_SELECTOR, host_gs_selector),
577 FIELD(HOST_TR_SELECTOR, host_tr_selector),
578 FIELD64(IO_BITMAP_A, io_bitmap_a),
579 FIELD64(IO_BITMAP_B, io_bitmap_b),
580 FIELD64(MSR_BITMAP, msr_bitmap),
581 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
582 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
583 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
584 FIELD64(TSC_OFFSET, tsc_offset),
585 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
586 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
587 FIELD64(EPT_POINTER, ept_pointer),
588 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
589 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
590 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
591 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
592 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
593 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
594 FIELD64(GUEST_PDPTR0, guest_pdptr0),
595 FIELD64(GUEST_PDPTR1, guest_pdptr1),
596 FIELD64(GUEST_PDPTR2, guest_pdptr2),
597 FIELD64(GUEST_PDPTR3, guest_pdptr3),
598 FIELD64(HOST_IA32_PAT, host_ia32_pat),
599 FIELD64(HOST_IA32_EFER, host_ia32_efer),
600 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
601 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
602 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
603 FIELD(EXCEPTION_BITMAP, exception_bitmap),
604 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
605 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
606 FIELD(CR3_TARGET_COUNT, cr3_target_count),
607 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
608 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
609 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
610 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
611 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
612 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
613 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
614 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
615 FIELD(TPR_THRESHOLD, tpr_threshold),
616 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
617 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
618 FIELD(VM_EXIT_REASON, vm_exit_reason),
619 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
620 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
621 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
622 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
623 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
624 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
625 FIELD(GUEST_ES_LIMIT, guest_es_limit),
626 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
627 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
628 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
629 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
630 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
631 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
632 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
633 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
634 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
635 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
636 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
637 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
638 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
639 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
640 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
641 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
642 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
643 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
644 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
645 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
646 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100647 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300648 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
649 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
650 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
651 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
652 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
653 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
654 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
655 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
656 FIELD(EXIT_QUALIFICATION, exit_qualification),
657 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
658 FIELD(GUEST_CR0, guest_cr0),
659 FIELD(GUEST_CR3, guest_cr3),
660 FIELD(GUEST_CR4, guest_cr4),
661 FIELD(GUEST_ES_BASE, guest_es_base),
662 FIELD(GUEST_CS_BASE, guest_cs_base),
663 FIELD(GUEST_SS_BASE, guest_ss_base),
664 FIELD(GUEST_DS_BASE, guest_ds_base),
665 FIELD(GUEST_FS_BASE, guest_fs_base),
666 FIELD(GUEST_GS_BASE, guest_gs_base),
667 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
668 FIELD(GUEST_TR_BASE, guest_tr_base),
669 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
670 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
671 FIELD(GUEST_DR7, guest_dr7),
672 FIELD(GUEST_RSP, guest_rsp),
673 FIELD(GUEST_RIP, guest_rip),
674 FIELD(GUEST_RFLAGS, guest_rflags),
675 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
676 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
677 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
678 FIELD(HOST_CR0, host_cr0),
679 FIELD(HOST_CR3, host_cr3),
680 FIELD(HOST_CR4, host_cr4),
681 FIELD(HOST_FS_BASE, host_fs_base),
682 FIELD(HOST_GS_BASE, host_gs_base),
683 FIELD(HOST_TR_BASE, host_tr_base),
684 FIELD(HOST_GDTR_BASE, host_gdtr_base),
685 FIELD(HOST_IDTR_BASE, host_idtr_base),
686 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
687 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
688 FIELD(HOST_RSP, host_rsp),
689 FIELD(HOST_RIP, host_rip),
690};
691static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table);
692
693static inline short vmcs_field_to_offset(unsigned long field)
694{
695 if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0)
696 return -1;
697 return vmcs_field_to_offset_table[field];
698}
699
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300700static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
701{
702 return to_vmx(vcpu)->nested.current_vmcs12;
703}
704
705static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
706{
707 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800708 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300709 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800710
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300711 return page;
712}
713
714static void nested_release_page(struct page *page)
715{
716 kvm_release_page_dirty(page);
717}
718
719static void nested_release_page_clean(struct page *page)
720{
721 kvm_release_page_clean(page);
722}
723
Nadav Har'Elbfd0a562013-08-05 11:07:17 +0300724static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800725static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800726static void kvm_cpu_vmxon(u64 addr);
727static void kvm_cpu_vmxoff(void);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200728static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300729static void vmx_set_segment(struct kvm_vcpu *vcpu,
730 struct kvm_segment *var, int seg);
731static void vmx_get_segment(struct kvm_vcpu *vcpu,
732 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200733static bool guest_state_valid(struct kvm_vcpu *vcpu);
734static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yang Zhanga20ed542013-04-11 19:25:15 +0800735static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu);
Abel Gordonc3114422013-04-18 14:38:55 +0300736static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx);
Abel Gordon16f5b902013-04-18 14:38:25 +0300737static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx);
Avi Kivity75880a02007-06-20 11:20:04 +0300738
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739static DEFINE_PER_CPU(struct vmcs *, vmxarea);
740static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300741/*
742 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
743 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
744 */
745static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300746static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200748static unsigned long *vmx_io_bitmap_a;
749static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200750static unsigned long *vmx_msr_bitmap_legacy;
751static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800752static unsigned long *vmx_msr_bitmap_legacy_x2apic;
753static unsigned long *vmx_msr_bitmap_longmode_x2apic;
Abel Gordon4607c2d2013-04-18 14:35:55 +0300754static unsigned long *vmx_vmread_bitmap;
755static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300756
Avi Kivity110312c2010-12-21 12:54:20 +0200757static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200758static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200759
Sheng Yang2384d2b2008-01-17 15:14:33 +0800760static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
761static DEFINE_SPINLOCK(vmx_vpid_lock);
762
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300763static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764 int size;
765 int order;
766 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300767 u32 pin_based_exec_ctrl;
768 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800769 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300770 u32 vmexit_ctrl;
771 u32 vmentry_ctrl;
772} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773
Hannes Ederefff9e52008-11-28 17:02:06 +0100774static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800775 u32 ept;
776 u32 vpid;
777} vmx_capability;
778
Avi Kivity6aa8b732006-12-10 02:21:36 -0800779#define VMX_SEGMENT_FIELD(seg) \
780 [VCPU_SREG_##seg] = { \
781 .selector = GUEST_##seg##_SELECTOR, \
782 .base = GUEST_##seg##_BASE, \
783 .limit = GUEST_##seg##_LIMIT, \
784 .ar_bytes = GUEST_##seg##_AR_BYTES, \
785 }
786
Mathias Krause772e0312012-08-30 01:30:19 +0200787static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 unsigned selector;
789 unsigned base;
790 unsigned limit;
791 unsigned ar_bytes;
792} kvm_vmx_segment_fields[] = {
793 VMX_SEGMENT_FIELD(CS),
794 VMX_SEGMENT_FIELD(DS),
795 VMX_SEGMENT_FIELD(ES),
796 VMX_SEGMENT_FIELD(FS),
797 VMX_SEGMENT_FIELD(GS),
798 VMX_SEGMENT_FIELD(SS),
799 VMX_SEGMENT_FIELD(TR),
800 VMX_SEGMENT_FIELD(LDTR),
801};
802
Avi Kivity26bb0982009-09-07 11:14:12 +0300803static u64 host_efer;
804
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300805static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
806
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300807/*
Brian Gerst8c065852010-07-17 09:03:26 -0400808 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300809 * away by decrementing the array size.
810 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800812#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300813 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400815 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800816};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200817#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818
Gui Jianfeng31299942010-03-15 17:29:09 +0800819static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800820{
821 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
822 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100823 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824}
825
Gui Jianfeng31299942010-03-15 17:29:09 +0800826static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300827{
828 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
829 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100830 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300831}
832
Gui Jianfeng31299942010-03-15 17:29:09 +0800833static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500834{
835 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
836 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100837 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500838}
839
Gui Jianfeng31299942010-03-15 17:29:09 +0800840static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841{
842 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
843 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
844}
845
Gui Jianfeng31299942010-03-15 17:29:09 +0800846static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800847{
848 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
849 INTR_INFO_VALID_MASK)) ==
850 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
851}
852
Gui Jianfeng31299942010-03-15 17:29:09 +0800853static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800854{
Sheng Yang04547152009-04-01 15:52:31 +0800855 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800856}
857
Gui Jianfeng31299942010-03-15 17:29:09 +0800858static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800859{
Sheng Yang04547152009-04-01 15:52:31 +0800860 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800861}
862
Gui Jianfeng31299942010-03-15 17:29:09 +0800863static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800864{
Sheng Yang04547152009-04-01 15:52:31 +0800865 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800866}
867
Gui Jianfeng31299942010-03-15 17:29:09 +0800868static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800869{
Sheng Yang04547152009-04-01 15:52:31 +0800870 return vmcs_config.cpu_based_exec_ctrl &
871 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800872}
873
Avi Kivity774ead32007-12-26 13:57:04 +0200874static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800875{
Sheng Yang04547152009-04-01 15:52:31 +0800876 return vmcs_config.cpu_based_2nd_exec_ctrl &
877 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
878}
879
Yang Zhang8d146952013-01-25 10:18:50 +0800880static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
881{
882 return vmcs_config.cpu_based_2nd_exec_ctrl &
883 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
884}
885
Yang Zhang83d4c282013-01-25 10:18:49 +0800886static inline bool cpu_has_vmx_apic_register_virt(void)
887{
888 return vmcs_config.cpu_based_2nd_exec_ctrl &
889 SECONDARY_EXEC_APIC_REGISTER_VIRT;
890}
891
Yang Zhangc7c9c562013-01-25 10:18:51 +0800892static inline bool cpu_has_vmx_virtual_intr_delivery(void)
893{
894 return vmcs_config.cpu_based_2nd_exec_ctrl &
895 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
896}
897
Yang Zhang01e439b2013-04-11 19:25:12 +0800898static inline bool cpu_has_vmx_posted_intr(void)
899{
900 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
901}
902
903static inline bool cpu_has_vmx_apicv(void)
904{
905 return cpu_has_vmx_apic_register_virt() &&
906 cpu_has_vmx_virtual_intr_delivery() &&
907 cpu_has_vmx_posted_intr();
908}
909
Sheng Yang04547152009-04-01 15:52:31 +0800910static inline bool cpu_has_vmx_flexpriority(void)
911{
912 return cpu_has_vmx_tpr_shadow() &&
913 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800914}
915
Marcelo Tosattie7997942009-06-11 12:07:40 -0300916static inline bool cpu_has_vmx_ept_execute_only(void)
917{
Gui Jianfeng31299942010-03-15 17:29:09 +0800918 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300919}
920
921static inline bool cpu_has_vmx_eptp_uncacheable(void)
922{
Gui Jianfeng31299942010-03-15 17:29:09 +0800923 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300924}
925
926static inline bool cpu_has_vmx_eptp_writeback(void)
927{
Gui Jianfeng31299942010-03-15 17:29:09 +0800928 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300929}
930
931static inline bool cpu_has_vmx_ept_2m_page(void)
932{
Gui Jianfeng31299942010-03-15 17:29:09 +0800933 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300934}
935
Sheng Yang878403b2010-01-05 19:02:29 +0800936static inline bool cpu_has_vmx_ept_1g_page(void)
937{
Gui Jianfeng31299942010-03-15 17:29:09 +0800938 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800939}
940
Sheng Yang4bc9b982010-06-02 14:05:24 +0800941static inline bool cpu_has_vmx_ept_4levels(void)
942{
943 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
944}
945
Xudong Hao83c3a332012-05-28 19:33:35 +0800946static inline bool cpu_has_vmx_ept_ad_bits(void)
947{
948 return vmx_capability.ept & VMX_EPT_AD_BIT;
949}
950
Gui Jianfeng31299942010-03-15 17:29:09 +0800951static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800952{
Gui Jianfeng31299942010-03-15 17:29:09 +0800953 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800954}
955
Gui Jianfeng31299942010-03-15 17:29:09 +0800956static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800957{
Gui Jianfeng31299942010-03-15 17:29:09 +0800958 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800959}
960
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800961static inline bool cpu_has_vmx_invvpid_single(void)
962{
963 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
964}
965
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800966static inline bool cpu_has_vmx_invvpid_global(void)
967{
968 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
969}
970
Gui Jianfeng31299942010-03-15 17:29:09 +0800971static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800972{
Sheng Yang04547152009-04-01 15:52:31 +0800973 return vmcs_config.cpu_based_2nd_exec_ctrl &
974 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800975}
976
Gui Jianfeng31299942010-03-15 17:29:09 +0800977static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700978{
979 return vmcs_config.cpu_based_2nd_exec_ctrl &
980 SECONDARY_EXEC_UNRESTRICTED_GUEST;
981}
982
Gui Jianfeng31299942010-03-15 17:29:09 +0800983static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800984{
985 return vmcs_config.cpu_based_2nd_exec_ctrl &
986 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
987}
988
Gui Jianfeng31299942010-03-15 17:29:09 +0800989static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800990{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800991 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800992}
993
Gui Jianfeng31299942010-03-15 17:29:09 +0800994static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800995{
Sheng Yang04547152009-04-01 15:52:31 +0800996 return vmcs_config.cpu_based_2nd_exec_ctrl &
997 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800998}
999
Gui Jianfeng31299942010-03-15 17:29:09 +08001000static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001001{
1002 return vmcs_config.cpu_based_2nd_exec_ctrl &
1003 SECONDARY_EXEC_RDTSCP;
1004}
1005
Mao, Junjiead756a12012-07-02 01:18:48 +00001006static inline bool cpu_has_vmx_invpcid(void)
1007{
1008 return vmcs_config.cpu_based_2nd_exec_ctrl &
1009 SECONDARY_EXEC_ENABLE_INVPCID;
1010}
1011
Gui Jianfeng31299942010-03-15 17:29:09 +08001012static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +08001013{
1014 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
1015}
1016
Sheng Yangf5f48ee2010-06-30 12:25:15 +08001017static inline bool cpu_has_vmx_wbinvd_exit(void)
1018{
1019 return vmcs_config.cpu_based_2nd_exec_ctrl &
1020 SECONDARY_EXEC_WBINVD_EXITING;
1021}
1022
Abel Gordonabc4fc52013-04-18 14:35:25 +03001023static inline bool cpu_has_vmx_shadow_vmcs(void)
1024{
1025 u64 vmx_msr;
1026 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1027 /* check if the cpu supports writing r/o exit information fields */
1028 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1029 return false;
1030
1031 return vmcs_config.cpu_based_2nd_exec_ctrl &
1032 SECONDARY_EXEC_SHADOW_VMCS;
1033}
1034
Sheng Yang04547152009-04-01 15:52:31 +08001035static inline bool report_flexpriority(void)
1036{
1037 return flexpriority_enabled;
1038}
1039
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001040static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1041{
1042 return vmcs12->cpu_based_vm_exec_control & bit;
1043}
1044
1045static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1046{
1047 return (vmcs12->cpu_based_vm_exec_control &
1048 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1049 (vmcs12->secondary_vm_exec_control & bit);
1050}
1051
Nadav Har'Elf5c43682013-08-05 11:07:20 +03001052static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12)
Nadav Har'El644d7112011-05-25 23:12:35 +03001053{
1054 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1055}
1056
Jan Kiszkaf41245002014-03-07 20:03:13 +01001057static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12)
1058{
1059 return vmcs12->pin_based_vm_exec_control &
1060 PIN_BASED_VMX_PREEMPTION_TIMER;
1061}
1062
Nadav Har'El155a97a2013-08-05 11:07:16 +03001063static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12)
1064{
1065 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT);
1066}
1067
Nadav Har'El644d7112011-05-25 23:12:35 +03001068static inline bool is_exception(u32 intr_info)
1069{
1070 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1071 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
1072}
1073
Jan Kiszka533558b2014-01-04 18:47:20 +01001074static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
1075 u32 exit_intr_info,
1076 unsigned long exit_qualification);
Nadav Har'El7c177932011-05-25 23:12:04 +03001077static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1078 struct vmcs12 *vmcs12,
1079 u32 reason, unsigned long qualification);
1080
Rusty Russell8b9cf982007-07-30 16:31:43 +10001081static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001082{
1083 int i;
1084
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001085 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001086 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001087 return i;
1088 return -1;
1089}
1090
Sheng Yang2384d2b2008-01-17 15:14:33 +08001091static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1092{
1093 struct {
1094 u64 vpid : 16;
1095 u64 rsvd : 48;
1096 u64 gva;
1097 } operand = { vpid, 0, gva };
1098
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001099 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001100 /* CF==1 or ZF==1 --> rc = -1 */
1101 "; ja 1f ; ud2 ; 1:"
1102 : : "a"(&operand), "c"(ext) : "cc", "memory");
1103}
1104
Sheng Yang14394422008-04-28 12:24:45 +08001105static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1106{
1107 struct {
1108 u64 eptp, gpa;
1109 } operand = {eptp, gpa};
1110
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001111 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001112 /* CF==1 or ZF==1 --> rc = -1 */
1113 "; ja 1f ; ud2 ; 1:\n"
1114 : : "a" (&operand), "c" (ext) : "cc", "memory");
1115}
1116
Avi Kivity26bb0982009-09-07 11:14:12 +03001117static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001118{
1119 int i;
1120
Rusty Russell8b9cf982007-07-30 16:31:43 +10001121 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001122 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001123 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001124 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001125}
1126
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127static void vmcs_clear(struct vmcs *vmcs)
1128{
1129 u64 phys_addr = __pa(vmcs);
1130 u8 error;
1131
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001132 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001133 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134 : "cc", "memory");
1135 if (error)
1136 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1137 vmcs, phys_addr);
1138}
1139
Nadav Har'Eld462b812011-05-24 15:26:10 +03001140static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1141{
1142 vmcs_clear(loaded_vmcs->vmcs);
1143 loaded_vmcs->cpu = -1;
1144 loaded_vmcs->launched = 0;
1145}
1146
Dongxiao Xu7725b892010-05-11 18:29:38 +08001147static void vmcs_load(struct vmcs *vmcs)
1148{
1149 u64 phys_addr = __pa(vmcs);
1150 u8 error;
1151
1152 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001153 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001154 : "cc", "memory");
1155 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001156 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001157 vmcs, phys_addr);
1158}
1159
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001160#ifdef CONFIG_KEXEC
1161/*
1162 * This bitmap is used to indicate whether the vmclear
1163 * operation is enabled on all cpus. All disabled by
1164 * default.
1165 */
1166static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1167
1168static inline void crash_enable_local_vmclear(int cpu)
1169{
1170 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1171}
1172
1173static inline void crash_disable_local_vmclear(int cpu)
1174{
1175 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1176}
1177
1178static inline int crash_local_vmclear_enabled(int cpu)
1179{
1180 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1181}
1182
1183static void crash_vmclear_local_loaded_vmcss(void)
1184{
1185 int cpu = raw_smp_processor_id();
1186 struct loaded_vmcs *v;
1187
1188 if (!crash_local_vmclear_enabled(cpu))
1189 return;
1190
1191 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1192 loaded_vmcss_on_cpu_link)
1193 vmcs_clear(v->vmcs);
1194}
1195#else
1196static inline void crash_enable_local_vmclear(int cpu) { }
1197static inline void crash_disable_local_vmclear(int cpu) { }
1198#endif /* CONFIG_KEXEC */
1199
Nadav Har'Eld462b812011-05-24 15:26:10 +03001200static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001201{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001202 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001203 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204
Nadav Har'Eld462b812011-05-24 15:26:10 +03001205 if (loaded_vmcs->cpu != cpu)
1206 return; /* vcpu migration can race with cpu offline */
1207 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001209 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001210 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001211
1212 /*
1213 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1214 * is before setting loaded_vmcs->vcpu to -1 which is done in
1215 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1216 * then adds the vmcs into percpu list before it is deleted.
1217 */
1218 smp_wmb();
1219
Nadav Har'Eld462b812011-05-24 15:26:10 +03001220 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001221 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222}
1223
Nadav Har'Eld462b812011-05-24 15:26:10 +03001224static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001225{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001226 int cpu = loaded_vmcs->cpu;
1227
1228 if (cpu != -1)
1229 smp_call_function_single(cpu,
1230 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001231}
1232
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001233static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001234{
1235 if (vmx->vpid == 0)
1236 return;
1237
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001238 if (cpu_has_vmx_invvpid_single())
1239 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001240}
1241
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001242static inline void vpid_sync_vcpu_global(void)
1243{
1244 if (cpu_has_vmx_invvpid_global())
1245 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1246}
1247
1248static inline void vpid_sync_context(struct vcpu_vmx *vmx)
1249{
1250 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001251 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001252 else
1253 vpid_sync_vcpu_global();
1254}
1255
Sheng Yang14394422008-04-28 12:24:45 +08001256static inline void ept_sync_global(void)
1257{
1258 if (cpu_has_vmx_invept_global())
1259 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1260}
1261
1262static inline void ept_sync_context(u64 eptp)
1263{
Avi Kivity089d0342009-03-23 18:26:32 +02001264 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001265 if (cpu_has_vmx_invept_context())
1266 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1267 else
1268 ept_sync_global();
1269 }
1270}
1271
Avi Kivity96304212011-05-15 10:13:13 -04001272static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273{
Avi Kivity5e520e62011-05-15 10:13:12 -04001274 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275
Avi Kivity5e520e62011-05-15 10:13:12 -04001276 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1277 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278 return value;
1279}
1280
Avi Kivity96304212011-05-15 10:13:13 -04001281static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282{
1283 return vmcs_readl(field);
1284}
1285
Avi Kivity96304212011-05-15 10:13:13 -04001286static __always_inline u32 vmcs_read32(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 u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292{
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001293#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294 return vmcs_readl(field);
1295#else
1296 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
1297#endif
1298}
1299
Avi Kivitye52de1b2007-01-05 16:36:56 -08001300static noinline void vmwrite_error(unsigned long field, unsigned long value)
1301{
1302 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1303 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1304 dump_stack();
1305}
1306
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307static void vmcs_writel(unsigned long field, unsigned long value)
1308{
1309 u8 error;
1310
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001311 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001312 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001313 if (unlikely(error))
1314 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315}
1316
1317static void vmcs_write16(unsigned long field, u16 value)
1318{
1319 vmcs_writel(field, value);
1320}
1321
1322static void vmcs_write32(unsigned long field, u32 value)
1323{
1324 vmcs_writel(field, value);
1325}
1326
1327static void vmcs_write64(unsigned long field, u64 value)
1328{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001330#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331 asm volatile ("");
1332 vmcs_writel(field+1, value >> 32);
1333#endif
1334}
1335
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001336static void vmcs_clear_bits(unsigned long field, u32 mask)
1337{
1338 vmcs_writel(field, vmcs_readl(field) & ~mask);
1339}
1340
1341static void vmcs_set_bits(unsigned long field, u32 mask)
1342{
1343 vmcs_writel(field, vmcs_readl(field) | mask);
1344}
1345
Gleb Natapov2961e8762013-11-25 15:37:13 +02001346static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val)
1347{
1348 vmcs_write32(VM_ENTRY_CONTROLS, val);
1349 vmx->vm_entry_controls_shadow = val;
1350}
1351
1352static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val)
1353{
1354 if (vmx->vm_entry_controls_shadow != val)
1355 vm_entry_controls_init(vmx, val);
1356}
1357
1358static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx)
1359{
1360 return vmx->vm_entry_controls_shadow;
1361}
1362
1363
1364static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1365{
1366 vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val);
1367}
1368
1369static inline void vm_entry_controls_clearbit(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_exit_controls_init(struct vcpu_vmx *vmx, u32 val)
1375{
1376 vmcs_write32(VM_EXIT_CONTROLS, val);
1377 vmx->vm_exit_controls_shadow = val;
1378}
1379
1380static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val)
1381{
1382 if (vmx->vm_exit_controls_shadow != val)
1383 vm_exit_controls_init(vmx, val);
1384}
1385
1386static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx)
1387{
1388 return vmx->vm_exit_controls_shadow;
1389}
1390
1391
1392static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val)
1393{
1394 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val);
1395}
1396
1397static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val)
1398{
1399 vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val);
1400}
1401
Avi Kivity2fb92db2011-04-27 19:42:18 +03001402static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1403{
1404 vmx->segment_cache.bitmask = 0;
1405}
1406
1407static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1408 unsigned field)
1409{
1410 bool ret;
1411 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1412
1413 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1414 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1415 vmx->segment_cache.bitmask = 0;
1416 }
1417 ret = vmx->segment_cache.bitmask & mask;
1418 vmx->segment_cache.bitmask |= mask;
1419 return ret;
1420}
1421
1422static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1423{
1424 u16 *p = &vmx->segment_cache.seg[seg].selector;
1425
1426 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1427 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1428 return *p;
1429}
1430
1431static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1432{
1433 ulong *p = &vmx->segment_cache.seg[seg].base;
1434
1435 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1436 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1437 return *p;
1438}
1439
1440static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1441{
1442 u32 *p = &vmx->segment_cache.seg[seg].limit;
1443
1444 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1445 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1446 return *p;
1447}
1448
1449static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1450{
1451 u32 *p = &vmx->segment_cache.seg[seg].ar;
1452
1453 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1454 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1455 return *p;
1456}
1457
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001458static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1459{
1460 u32 eb;
1461
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001462 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1463 (1u << NM_VECTOR) | (1u << DB_VECTOR);
1464 if ((vcpu->guest_debug &
1465 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1466 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1467 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001468 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001469 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001470 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001471 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001472 if (vcpu->fpu_active)
1473 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001474
1475 /* When we are running a nested L2 guest and L1 specified for it a
1476 * certain exception bitmap, we must trap the same exceptions and pass
1477 * them to L1. When running L2, we will only handle the exceptions
1478 * specified above if L1 did not want them.
1479 */
1480 if (is_guest_mode(vcpu))
1481 eb |= get_vmcs12(vcpu)->exception_bitmap;
1482
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001483 vmcs_write32(EXCEPTION_BITMAP, eb);
1484}
1485
Gleb Natapov2961e8762013-11-25 15:37:13 +02001486static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1487 unsigned long entry, unsigned long exit)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001488{
Gleb Natapov2961e8762013-11-25 15:37:13 +02001489 vm_entry_controls_clearbit(vmx, entry);
1490 vm_exit_controls_clearbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001491}
1492
Avi Kivity61d2ef22010-04-28 16:40:38 +03001493static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1494{
1495 unsigned i;
1496 struct msr_autoload *m = &vmx->msr_autoload;
1497
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001498 switch (msr) {
1499 case MSR_EFER:
1500 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001501 clear_atomic_switch_msr_special(vmx,
1502 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001503 VM_EXIT_LOAD_IA32_EFER);
1504 return;
1505 }
1506 break;
1507 case MSR_CORE_PERF_GLOBAL_CTRL:
1508 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001509 clear_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001510 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1511 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1512 return;
1513 }
1514 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001515 }
1516
Avi Kivity61d2ef22010-04-28 16:40:38 +03001517 for (i = 0; i < m->nr; ++i)
1518 if (m->guest[i].index == msr)
1519 break;
1520
1521 if (i == m->nr)
1522 return;
1523 --m->nr;
1524 m->guest[i] = m->guest[m->nr];
1525 m->host[i] = m->host[m->nr];
1526 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1527 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1528}
1529
Gleb Natapov2961e8762013-11-25 15:37:13 +02001530static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1531 unsigned long entry, unsigned long exit,
1532 unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1533 u64 guest_val, u64 host_val)
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001534{
1535 vmcs_write64(guest_val_vmcs, guest_val);
1536 vmcs_write64(host_val_vmcs, host_val);
Gleb Natapov2961e8762013-11-25 15:37:13 +02001537 vm_entry_controls_setbit(vmx, entry);
1538 vm_exit_controls_setbit(vmx, exit);
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001539}
1540
Avi Kivity61d2ef22010-04-28 16:40:38 +03001541static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1542 u64 guest_val, u64 host_val)
1543{
1544 unsigned i;
1545 struct msr_autoload *m = &vmx->msr_autoload;
1546
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001547 switch (msr) {
1548 case MSR_EFER:
1549 if (cpu_has_load_ia32_efer) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001550 add_atomic_switch_msr_special(vmx,
1551 VM_ENTRY_LOAD_IA32_EFER,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001552 VM_EXIT_LOAD_IA32_EFER,
1553 GUEST_IA32_EFER,
1554 HOST_IA32_EFER,
1555 guest_val, host_val);
1556 return;
1557 }
1558 break;
1559 case MSR_CORE_PERF_GLOBAL_CTRL:
1560 if (cpu_has_load_perf_global_ctrl) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02001561 add_atomic_switch_msr_special(vmx,
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001562 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1563 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1564 GUEST_IA32_PERF_GLOBAL_CTRL,
1565 HOST_IA32_PERF_GLOBAL_CTRL,
1566 guest_val, host_val);
1567 return;
1568 }
1569 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001570 }
1571
Avi Kivity61d2ef22010-04-28 16:40:38 +03001572 for (i = 0; i < m->nr; ++i)
1573 if (m->guest[i].index == msr)
1574 break;
1575
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001576 if (i == NR_AUTOLOAD_MSRS) {
Michael S. Tsirkin60266202013-10-31 00:34:56 +02001577 printk_once(KERN_WARNING "Not enough msr switch entries. "
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001578 "Can't add msr %x\n", msr);
1579 return;
1580 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001581 ++m->nr;
1582 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1583 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1584 }
1585
1586 m->guest[i].index = msr;
1587 m->guest[i].value = guest_val;
1588 m->host[i].index = msr;
1589 m->host[i].value = host_val;
1590}
1591
Avi Kivity33ed6322007-05-02 16:54:03 +03001592static void reload_tss(void)
1593{
Avi Kivity33ed6322007-05-02 16:54:03 +03001594 /*
1595 * VT restores TR but not its size. Useless.
1596 */
Avi Kivityd3591922010-07-26 18:32:39 +03001597 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001598 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001599
Avi Kivityd3591922010-07-26 18:32:39 +03001600 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001601 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1602 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001603}
1604
Avi Kivity92c0d902009-10-29 11:00:16 +02001605static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001606{
Roel Kluin3a34a882009-08-04 02:08:45 -07001607 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001608 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +03001609
Avi Kivityf6801df2010-01-21 15:31:50 +02001610 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -07001611
Avi Kivity51c6cf62007-08-29 03:48:05 +03001612 /*
Guo Chao0fa06072012-06-28 15:16:19 +08001613 * NX is emulated; LMA and LME handled by hardware; SCE meaningless
Avi Kivity51c6cf62007-08-29 03:48:05 +03001614 * outside long mode
1615 */
1616 ignore_bits = EFER_NX | EFER_SCE;
1617#ifdef CONFIG_X86_64
1618 ignore_bits |= EFER_LMA | EFER_LME;
1619 /* SCE is meaningful only in long mode on Intel */
1620 if (guest_efer & EFER_LMA)
1621 ignore_bits &= ~(u64)EFER_SCE;
1622#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +03001623 guest_efer &= ~ignore_bits;
1624 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +03001625 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +02001626 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03001627
1628 clear_atomic_switch_msr(vmx, MSR_EFER);
1629 /* On ept, can't emulate nx, and must switch nx atomically */
1630 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
1631 guest_efer = vmx->vcpu.arch.efer;
1632 if (!(guest_efer & EFER_LMA))
1633 guest_efer &= ~EFER_LME;
1634 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
1635 return false;
1636 }
1637
Avi Kivity26bb0982009-09-07 11:14:12 +03001638 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001639}
1640
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001641static unsigned long segment_base(u16 selector)
1642{
Avi Kivityd3591922010-07-26 18:32:39 +03001643 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001644 struct desc_struct *d;
1645 unsigned long table_base;
1646 unsigned long v;
1647
1648 if (!(selector & ~3))
1649 return 0;
1650
Avi Kivityd3591922010-07-26 18:32:39 +03001651 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001652
1653 if (selector & 4) { /* from ldt */
1654 u16 ldt_selector = kvm_read_ldt();
1655
1656 if (!(ldt_selector & ~3))
1657 return 0;
1658
1659 table_base = segment_base(ldt_selector);
1660 }
1661 d = (struct desc_struct *)(table_base + (selector & ~7));
1662 v = get_desc_base(d);
1663#ifdef CONFIG_X86_64
1664 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
1665 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
1666#endif
1667 return v;
1668}
1669
1670static inline unsigned long kvm_read_tr_base(void)
1671{
1672 u16 tr;
1673 asm("str %0" : "=g"(tr));
1674 return segment_base(tr);
1675}
1676
Avi Kivity04d2cc72007-09-10 18:10:54 +03001677static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001678{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001679 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001680 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001681
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001682 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001683 return;
1684
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001685 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001686 /*
1687 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1688 * allow segment selectors with cpl > 0 or ti == 1.
1689 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001690 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02001691 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02001692 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001693 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001694 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001695 vmx->host_state.fs_reload_needed = 0;
1696 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03001697 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001698 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001699 }
Avi Kivity9581d442010-10-19 16:46:55 +02001700 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001701 if (!(vmx->host_state.gs_sel & 7))
1702 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001703 else {
1704 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001705 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001706 }
1707
1708#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03001709 savesegment(ds, vmx->host_state.ds_sel);
1710 savesegment(es, vmx->host_state.es_sel);
1711#endif
1712
1713#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03001714 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1715 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1716#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001717 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1718 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03001719#endif
Avi Kivity707c0872007-05-02 17:33:43 +03001720
1721#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001722 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1723 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03001724 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03001725#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001726 if (boot_cpu_has(X86_FEATURE_MPX))
1727 rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Avi Kivity26bb0982009-09-07 11:14:12 +03001728 for (i = 0; i < vmx->save_nmsrs; ++i)
1729 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02001730 vmx->guest_msrs[i].data,
1731 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03001732}
1733
Avi Kivitya9b21b62008-06-24 11:48:49 +03001734static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001735{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001736 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001737 return;
1738
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001739 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001740 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02001741#ifdef CONFIG_X86_64
1742 if (is_long_mode(&vmx->vcpu))
1743 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1744#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001745 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001746 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001747#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02001748 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001749#else
1750 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001751#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001752 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02001753 if (vmx->host_state.fs_reload_needed)
1754 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001755#ifdef CONFIG_X86_64
1756 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
1757 loadsegment(ds, vmx->host_state.ds_sel);
1758 loadsegment(es, vmx->host_state.es_sel);
1759 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001760#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001761 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001762#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001763 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001764#endif
Liu, Jinsongda8999d2014-02-24 10:55:46 +00001765 if (vmx->host_state.msr_host_bndcfgs)
1766 wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs);
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07001767 /*
1768 * If the FPU is not active (through the host task or
1769 * the guest vcpu), then restore the cr0.TS bit.
1770 */
1771 if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded)
1772 stts();
Avi Kivity3444d7d2010-07-26 18:32:38 +03001773 load_gdt(&__get_cpu_var(host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03001774}
1775
Avi Kivitya9b21b62008-06-24 11:48:49 +03001776static void vmx_load_host_state(struct vcpu_vmx *vmx)
1777{
1778 preempt_disable();
1779 __vmx_load_host_state(vmx);
1780 preempt_enable();
1781}
1782
Avi Kivity6aa8b732006-12-10 02:21:36 -08001783/*
1784 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1785 * vcpu mutex is already taken.
1786 */
Avi Kivity15ad7142007-07-11 18:17:21 +03001787static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001789 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001790 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001791
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001792 if (!vmm_exclusive)
1793 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001794 else if (vmx->loaded_vmcs->cpu != cpu)
1795 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001796
Nadav Har'Eld462b812011-05-24 15:26:10 +03001797 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1798 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1799 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800 }
1801
Nadav Har'Eld462b812011-05-24 15:26:10 +03001802 if (vmx->loaded_vmcs->cpu != cpu) {
Avi Kivityd3591922010-07-26 18:32:39 +03001803 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001804 unsigned long sysenter_esp;
1805
Avi Kivitya8eeb042010-05-10 12:34:53 +03001806 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001807 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001808 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001809
1810 /*
1811 * Read loaded_vmcs->cpu should be before fetching
1812 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1813 * See the comments in __loaded_vmcs_clear().
1814 */
1815 smp_rmb();
1816
Nadav Har'Eld462b812011-05-24 15:26:10 +03001817 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1818 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001819 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001820 local_irq_enable();
1821
Avi Kivity6aa8b732006-12-10 02:21:36 -08001822 /*
1823 * Linux uses per-cpu TSS and GDT, so set these when switching
1824 * processors.
1825 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001826 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03001827 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001828
1829 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1830 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Nadav Har'Eld462b812011-05-24 15:26:10 +03001831 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001833}
1834
1835static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1836{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001837 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001838 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001839 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1840 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001841 kvm_cpu_vmxoff();
1842 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001843}
1844
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001845static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1846{
Avi Kivity81231c62010-01-24 16:26:40 +02001847 ulong cr0;
1848
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001849 if (vcpu->fpu_active)
1850 return;
1851 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001852 cr0 = vmcs_readl(GUEST_CR0);
1853 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1854 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1855 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001856 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001857 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001858 if (is_guest_mode(vcpu))
1859 vcpu->arch.cr0_guest_owned_bits &=
1860 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02001861 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001862}
1863
Avi Kivityedcafe32009-12-30 18:07:40 +02001864static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1865
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001866/*
1867 * Return the cr0 value that a nested guest would read. This is a combination
1868 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1869 * its hypervisor (cr0_read_shadow).
1870 */
1871static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
1872{
1873 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
1874 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
1875}
1876static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
1877{
1878 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
1879 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
1880}
1881
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001882static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1883{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001884 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
1885 * set this *before* calling this function.
1886 */
Avi Kivityedcafe32009-12-30 18:07:40 +02001887 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001888 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001889 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001890 vcpu->arch.cr0_guest_owned_bits = 0;
1891 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001892 if (is_guest_mode(vcpu)) {
1893 /*
1894 * L1's specified read shadow might not contain the TS bit,
1895 * so now that we turned on shadowing of this bit, we need to
1896 * set this bit of the shadow. Like in nested_vmx_run we need
1897 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
1898 * up-to-date here because we just decached cr0.TS (and we'll
1899 * only update vmcs12->guest_cr0 on nested exit).
1900 */
1901 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1902 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
1903 (vcpu->arch.cr0 & X86_CR0_TS);
1904 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
1905 } else
1906 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001907}
1908
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1910{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001911 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001912
Avi Kivity6de12732011-03-07 12:51:22 +02001913 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1914 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1915 rflags = vmcs_readl(GUEST_RFLAGS);
1916 if (to_vmx(vcpu)->rmode.vm86_active) {
1917 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1918 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1919 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1920 }
1921 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001922 }
Avi Kivity6de12732011-03-07 12:51:22 +02001923 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001924}
1925
1926static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1927{
Avi Kivity6de12732011-03-07 12:51:22 +02001928 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1929 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001930 if (to_vmx(vcpu)->rmode.vm86_active) {
1931 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001932 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001933 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001934 vmcs_writel(GUEST_RFLAGS, rflags);
1935}
1936
Glauber Costa2809f5d2009-05-12 16:21:05 -04001937static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1938{
1939 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1940 int ret = 0;
1941
1942 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001943 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001944 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001945 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001946
1947 return ret & mask;
1948}
1949
1950static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1951{
1952 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1953 u32 interruptibility = interruptibility_old;
1954
1955 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1956
Jan Kiszka48005f62010-02-19 19:38:07 +01001957 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001958 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001959 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001960 interruptibility |= GUEST_INTR_STATE_STI;
1961
1962 if ((interruptibility != interruptibility_old))
1963 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1964}
1965
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1967{
1968 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001969
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001970 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001971 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001972 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973
Glauber Costa2809f5d2009-05-12 16:21:05 -04001974 /* skipping an emulated instruction also counts */
1975 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001976}
1977
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001978/*
1979 * KVM wants to inject page-faults which it got to the guest. This function
1980 * checks whether in a nested guest, we need to inject them to L1 or L2.
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001981 */
Gleb Natapove011c662013-09-25 12:51:35 +03001982static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr)
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001983{
1984 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1985
Gleb Natapove011c662013-09-25 12:51:35 +03001986 if (!(vmcs12->exception_bitmap & (1u << nr)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001987 return 0;
1988
Jan Kiszka533558b2014-01-04 18:47:20 +01001989 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
1990 vmcs_read32(VM_EXIT_INTR_INFO),
1991 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001992 return 1;
1993}
1994
Avi Kivity298101d2007-11-25 13:41:11 +02001995static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02001996 bool has_error_code, u32 error_code,
1997 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02001998{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001999 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002000 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002001
Gleb Natapove011c662013-09-25 12:51:35 +03002002 if (!reinject && is_guest_mode(vcpu) &&
2003 nested_vmx_check_exception(vcpu, nr))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03002004 return;
2005
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002006 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002007 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002008 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2009 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002010
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002011 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05002012 int inc_eip = 0;
2013 if (kvm_exception_is_soft(nr))
2014 inc_eip = vcpu->arch.event_exit_inst_len;
2015 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02002016 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002017 return;
2018 }
2019
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002020 if (kvm_exception_is_soft(nr)) {
2021 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2022 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01002023 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2024 } else
2025 intr_info |= INTR_TYPE_HARD_EXCEPTION;
2026
2027 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02002028}
2029
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002030static bool vmx_rdtscp_supported(void)
2031{
2032 return cpu_has_vmx_rdtscp();
2033}
2034
Mao, Junjiead756a12012-07-02 01:18:48 +00002035static bool vmx_invpcid_supported(void)
2036{
2037 return cpu_has_vmx_invpcid() && enable_ept;
2038}
2039
Avi Kivity6aa8b732006-12-10 02:21:36 -08002040/*
Eddie Donga75beee2007-05-17 18:55:15 +03002041 * Swap MSR entry in host/guest MSR entry array.
2042 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002043static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03002044{
Avi Kivity26bb0982009-09-07 11:14:12 +03002045 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002046
2047 tmp = vmx->guest_msrs[to];
2048 vmx->guest_msrs[to] = vmx->guest_msrs[from];
2049 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03002050}
2051
Yang Zhang8d146952013-01-25 10:18:50 +08002052static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
2053{
2054 unsigned long *msr_bitmap;
2055
2056 if (irqchip_in_kernel(vcpu->kvm) && apic_x2apic_mode(vcpu->arch.apic)) {
2057 if (is_long_mode(vcpu))
2058 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
2059 else
2060 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
2061 } else {
2062 if (is_long_mode(vcpu))
2063 msr_bitmap = vmx_msr_bitmap_longmode;
2064 else
2065 msr_bitmap = vmx_msr_bitmap_legacy;
2066 }
2067
2068 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
2069}
2070
Eddie Donga75beee2007-05-17 18:55:15 +03002071/*
Avi Kivitye38aea32007-04-19 13:22:48 +03002072 * Set up the vmcs to automatically save and restore system
2073 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2074 * mode, as fiddling with msrs is very expensive.
2075 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002076static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03002077{
Avi Kivity26bb0982009-09-07 11:14:12 +03002078 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03002079
Eddie Donga75beee2007-05-17 18:55:15 +03002080 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002081#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10002082 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002083 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03002084 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002085 move_msr_up(vmx, index, save_nmsrs++);
2086 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002087 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002088 move_msr_up(vmx, index, save_nmsrs++);
2089 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03002090 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002091 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002092 index = __find_msr_index(vmx, MSR_TSC_AUX);
2093 if (index >= 0 && vmx->rdtscp_enabled)
2094 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002095 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002096 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002097 * if efer.sce is enabled.
2098 */
Brian Gerst8c065852010-07-17 09:03:26 -04002099 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002100 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002101 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002102 }
Eddie Donga75beee2007-05-17 18:55:15 +03002103#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002104 index = __find_msr_index(vmx, MSR_EFER);
2105 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002106 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002107
Avi Kivity26bb0982009-09-07 11:14:12 +03002108 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002109
Yang Zhang8d146952013-01-25 10:18:50 +08002110 if (cpu_has_vmx_msr_bitmap())
2111 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002112}
2113
2114/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115 * reads and returns guest's timestamp counter "register"
2116 * guest_tsc = host_tsc + tsc_offset -- 21.3
2117 */
2118static u64 guest_read_tsc(void)
2119{
2120 u64 host_tsc, tsc_offset;
2121
2122 rdtscll(host_tsc);
2123 tsc_offset = vmcs_read64(TSC_OFFSET);
2124 return host_tsc + tsc_offset;
2125}
2126
2127/*
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002128 * Like guest_read_tsc, but always returns L1's notion of the timestamp
2129 * counter, even if a nested guest (L2) is currently running.
2130 */
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002131u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002132{
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002133 u64 tsc_offset;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002134
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002135 tsc_offset = is_guest_mode(vcpu) ?
2136 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
2137 vmcs_read64(TSC_OFFSET);
2138 return host_tsc + tsc_offset;
2139}
2140
2141/*
Zachary Amsdencc578282012-02-03 15:43:50 -02002142 * Engage any workarounds for mis-matched TSC rates. Currently limited to
2143 * software catchup for faster rates on slower CPUs.
Joerg Roedel4051b182011-03-25 09:44:49 +01002144 */
Zachary Amsdencc578282012-02-03 15:43:50 -02002145static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +01002146{
Zachary Amsdencc578282012-02-03 15:43:50 -02002147 if (!scale)
2148 return;
2149
2150 if (user_tsc_khz > tsc_khz) {
2151 vcpu->arch.tsc_catchup = 1;
2152 vcpu->arch.tsc_always_catchup = 1;
2153 } else
2154 WARN(1, "user requested TSC rate below hardware speed\n");
Joerg Roedel4051b182011-03-25 09:44:49 +01002155}
2156
Will Auldba904632012-11-29 12:42:50 -08002157static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
2158{
2159 return vmcs_read64(TSC_OFFSET);
2160}
2161
Joerg Roedel4051b182011-03-25 09:44:49 +01002162/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002163 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002165static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002166{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002167 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002168 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002169 * We're here if L1 chose not to trap WRMSR to TSC. According
2170 * to the spec, this should set L1's TSC; The offset that L1
2171 * set for L2 remains unchanged, and still needs to be added
2172 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002173 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002174 struct vmcs12 *vmcs12;
2175 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
2176 /* recalculate vmcs02.TSC_OFFSET: */
2177 vmcs12 = get_vmcs12(vcpu);
2178 vmcs_write64(TSC_OFFSET, offset +
2179 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2180 vmcs12->tsc_offset : 0));
2181 } else {
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002182 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2183 vmcs_read64(TSC_OFFSET), offset);
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002184 vmcs_write64(TSC_OFFSET, offset);
2185 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002186}
2187
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002188static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10002189{
2190 u64 offset = vmcs_read64(TSC_OFFSET);
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002191
Zachary Amsdene48672f2010-08-19 22:07:23 -10002192 vmcs_write64(TSC_OFFSET, offset + adjustment);
Nadav Har'El79918252011-05-25 23:15:39 +03002193 if (is_guest_mode(vcpu)) {
2194 /* Even when running L2, the adjustment needs to apply to L1 */
2195 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
Yoshihiro YUNOMAE489223e2013-06-12 16:43:44 +09002196 } else
2197 trace_kvm_write_tsc_offset(vcpu->vcpu_id, offset,
2198 offset + adjustment);
Zachary Amsdene48672f2010-08-19 22:07:23 -10002199}
2200
Joerg Roedel857e4092011-03-25 09:44:50 +01002201static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
2202{
2203 return target_tsc - native_read_tsc();
2204}
2205
Nadav Har'El801d3422011-05-25 23:02:23 +03002206static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2207{
2208 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2209 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2210}
2211
2212/*
2213 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2214 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2215 * all guests if the "nested" module option is off, and can also be disabled
2216 * for a single guest by disabling its VMX cpuid bit.
2217 */
2218static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2219{
2220 return nested && guest_cpuid_has_vmx(vcpu);
2221}
2222
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002224 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2225 * returned for the various VMX controls MSRs when nested VMX is enabled.
2226 * The same values should also be used to verify that vmcs12 control fields are
2227 * valid during nested entry from L1 to L2.
2228 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2229 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2230 * bit in the high half is on if the corresponding bit in the control field
2231 * may be on. See also vmx_control_verify().
2232 * TODO: allow these variables to be modified (downgraded) by module options
2233 * or other means.
2234 */
2235static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
2236static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
2237static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
2238static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
2239static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002240static u32 nested_vmx_misc_low, nested_vmx_misc_high;
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03002241static u32 nested_vmx_ept_caps;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002242static __init void nested_vmx_setup_ctls_msrs(void)
2243{
2244 /*
2245 * Note that as a general rule, the high half of the MSRs (bits in
2246 * the control fields which may be 1) should be initialized by the
2247 * intersection of the underlying hardware's MSR (i.e., features which
2248 * can be supported) and the list of features we want to expose -
2249 * because they are known to be properly supported in our code.
2250 * Also, usually, the low half of the MSRs (bits which must be 1) can
2251 * be set to 0, meaning that L1 may turn off any of these bits. The
2252 * reason is that if one of these bits is necessary, it will appear
2253 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2254 * fields of vmcs01 and vmcs02, will turn these bits off - and
2255 * nested_vmx_exit_handled() will not pass related exits to L1.
2256 * These rules have exceptions below.
2257 */
2258
2259 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002260 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
2261 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002262 /*
2263 * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
2264 * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
2265 */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002266 nested_vmx_pinbased_ctls_low |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2267 nested_vmx_pinbased_ctls_high &= PIN_BASED_EXT_INTR_MASK |
Jan Kiszkaf41245002014-03-07 20:03:13 +01002268 PIN_BASED_NMI_EXITING | PIN_BASED_VIRTUAL_NMIS;
2269 nested_vmx_pinbased_ctls_high |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002270 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002271
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002272 /*
2273 * Exit controls
2274 * If bit 55 of VMX_BASIC is off, bits 0-8 and 10, 11, 13, 14, 16 and
2275 * 17 must be 1.
2276 */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002277 rdmsr(MSR_IA32_VMX_EXIT_CTLS,
2278 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high);
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002279 nested_vmx_exit_ctls_low = VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb6f12502011-05-25 23:13:06 +03002280 /* Note that guest use of VM_EXIT_ACK_INTR_ON_EXIT is not supported. */
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002281 nested_vmx_exit_ctls_high &=
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002282#ifdef CONFIG_X86_64
Arthur Chunqi Lic0dfee52013-08-06 18:41:45 +08002283 VM_EXIT_HOST_ADDR_SPACE_SIZE |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002284#endif
Jan Kiszkaf41245002014-03-07 20:03:13 +01002285 VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT;
2286 nested_vmx_exit_ctls_high |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR |
2287 VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER |
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +08002288 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002289
2290 /* entry controls */
2291 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
2292 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002293 /* If bit 55 of VMX_BASIC is off, bits 0-8 and 12 must be 1. */
2294 nested_vmx_entry_ctls_low = VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002295 nested_vmx_entry_ctls_high &=
Jan Kiszka57435342013-08-06 10:39:56 +02002296#ifdef CONFIG_X86_64
2297 VM_ENTRY_IA32E_MODE |
2298#endif
2299 VM_ENTRY_LOAD_IA32_PAT;
Nadav Har'El8049d652013-08-05 11:07:06 +03002300 nested_vmx_entry_ctls_high |= (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR |
2301 VM_ENTRY_LOAD_IA32_EFER);
Jan Kiszka57435342013-08-06 10:39:56 +02002302
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002303 /* cpu-based controls */
2304 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
2305 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
2306 nested_vmx_procbased_ctls_low = 0;
2307 nested_vmx_procbased_ctls_high &=
Jan Kiszkaa294c9b2013-10-23 17:43:09 +01002308 CPU_BASED_VIRTUAL_INTR_PENDING |
2309 CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002310 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2311 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2312 CPU_BASED_CR3_STORE_EXITING |
2313#ifdef CONFIG_X86_64
2314 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2315#endif
2316 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
2317 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
Avi Kivitydbcb4e72012-08-13 15:38:22 +03002318 CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING |
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002319 CPU_BASED_PAUSE_EXITING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002320 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2321 /*
2322 * We can allow some features even when not supported by the
2323 * hardware. For example, L1 can specify an MSR bitmap - and we
2324 * can use it to avoid exits to L1 - even when L0 runs L2
2325 * without MSR bitmaps.
2326 */
2327 nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
2328
2329 /* secondary cpu-based controls */
2330 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
2331 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
2332 nested_vmx_secondary_ctls_low = 0;
2333 nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002334 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02002335 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002336 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002337
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002338 if (enable_ept) {
2339 /* nested EPT: emulate EPT also to L1 */
2340 nested_vmx_secondary_ctls_high |= SECONDARY_EXEC_ENABLE_EPT;
Jan Kiszkaca72d972013-08-06 10:39:55 +02002341 nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT |
Jan Kiszkad3134db2013-10-23 14:40:31 +01002342 VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT |
2343 VMX_EPT_INVEPT_BIT;
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002344 nested_vmx_ept_caps &= vmx_capability.ept;
2345 /*
2346 * Since invept is completely emulated we support both global
2347 * and context invalidation independent of what host cpu
2348 * supports
2349 */
2350 nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT |
2351 VMX_EPT_EXTENT_CONTEXT_BIT;
2352 } else
2353 nested_vmx_ept_caps = 0;
2354
Jan Kiszkac18911a2013-03-13 16:06:41 +01002355 /* miscellaneous data */
2356 rdmsr(MSR_IA32_VMX_MISC, nested_vmx_misc_low, nested_vmx_misc_high);
Jan Kiszkaf41245002014-03-07 20:03:13 +01002357 nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA;
2358 nested_vmx_misc_low |= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE |
2359 VMX_MISC_ACTIVITY_HLT;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002360 nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002361}
2362
2363static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2364{
2365 /*
2366 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2367 */
2368 return ((control & high) | low) == control;
2369}
2370
2371static inline u64 vmx_control_msr(u32 low, u32 high)
2372{
2373 return low | ((u64)high << 32);
2374}
2375
Jan Kiszkacae50132014-01-04 18:47:22 +01002376/* Returns 0 on success, non-0 otherwise. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002377static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2378{
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002379 switch (msr_index) {
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002380 case MSR_IA32_VMX_BASIC:
2381 /*
2382 * This MSR reports some information about VMX support. We
2383 * should return information about the VMX we emulate for the
2384 * guest, and the VMCS structure we give it - not about the
2385 * VMX support of the underlying hardware.
2386 */
2387 *pdata = VMCS12_REVISION |
2388 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2389 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2390 break;
2391 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2392 case MSR_IA32_VMX_PINBASED_CTLS:
2393 *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
2394 nested_vmx_pinbased_ctls_high);
2395 break;
2396 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2397 case MSR_IA32_VMX_PROCBASED_CTLS:
2398 *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
2399 nested_vmx_procbased_ctls_high);
2400 break;
2401 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2402 case MSR_IA32_VMX_EXIT_CTLS:
2403 *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
2404 nested_vmx_exit_ctls_high);
2405 break;
2406 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2407 case MSR_IA32_VMX_ENTRY_CTLS:
2408 *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
2409 nested_vmx_entry_ctls_high);
2410 break;
2411 case MSR_IA32_VMX_MISC:
Jan Kiszkac18911a2013-03-13 16:06:41 +01002412 *pdata = vmx_control_msr(nested_vmx_misc_low,
2413 nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002414 break;
2415 /*
2416 * These MSRs specify bits which the guest must keep fixed (on or off)
2417 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2418 * We picked the standard core2 setting.
2419 */
2420#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2421#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2422 case MSR_IA32_VMX_CR0_FIXED0:
2423 *pdata = VMXON_CR0_ALWAYSON;
2424 break;
2425 case MSR_IA32_VMX_CR0_FIXED1:
2426 *pdata = -1ULL;
2427 break;
2428 case MSR_IA32_VMX_CR4_FIXED0:
2429 *pdata = VMXON_CR4_ALWAYSON;
2430 break;
2431 case MSR_IA32_VMX_CR4_FIXED1:
2432 *pdata = -1ULL;
2433 break;
2434 case MSR_IA32_VMX_VMCS_ENUM:
2435 *pdata = 0x1f;
2436 break;
2437 case MSR_IA32_VMX_PROCBASED_CTLS2:
2438 *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
2439 nested_vmx_secondary_ctls_high);
2440 break;
2441 case MSR_IA32_VMX_EPT_VPID_CAP:
Nadav Har'Elafa61f7522013-08-07 14:59:22 +02002442 /* Currently, no nested vpid support */
2443 *pdata = nested_vmx_ept_caps;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002444 break;
2445 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002446 return 1;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08002447 }
2448
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002449 return 0;
2450}
2451
2452/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453 * Reads an msr value (of 'msr_index') into 'pdata'.
2454 * Returns 0 on success, non-0 otherwise.
2455 * Assumes vcpu_load() was already called.
2456 */
2457static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2458{
2459 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03002460 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461
2462 if (!pdata) {
2463 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
2464 return -EINVAL;
2465 }
2466
2467 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002468#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469 case MSR_FS_BASE:
2470 data = vmcs_readl(GUEST_FS_BASE);
2471 break;
2472 case MSR_GS_BASE:
2473 data = vmcs_readl(GUEST_GS_BASE);
2474 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002475 case MSR_KERNEL_GS_BASE:
2476 vmx_load_host_state(to_vmx(vcpu));
2477 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
2478 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002479#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002481 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302482 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002483 data = guest_read_tsc();
2484 break;
2485 case MSR_IA32_SYSENTER_CS:
2486 data = vmcs_read32(GUEST_SYSENTER_CS);
2487 break;
2488 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002489 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002490 break;
2491 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002492 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002493 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002494 case MSR_IA32_BNDCFGS:
2495 data = vmcs_read64(GUEST_BNDCFGS);
2496 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002497 case MSR_IA32_FEATURE_CONTROL:
2498 if (!nested_vmx_allowed(vcpu))
2499 return 1;
2500 data = to_vmx(vcpu)->nested.msr_ia32_feature_control;
2501 break;
2502 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2503 if (!nested_vmx_allowed(vcpu))
2504 return 1;
2505 return vmx_get_vmx_msr(vcpu, msr_index, pdata);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002506 case MSR_TSC_AUX:
2507 if (!to_vmx(vcpu)->rdtscp_enabled)
2508 return 1;
2509 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002511 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002512 if (msr) {
2513 data = msr->data;
2514 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002515 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002516 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517 }
2518
2519 *pdata = data;
2520 return 0;
2521}
2522
Jan Kiszkacae50132014-01-04 18:47:22 +01002523static void vmx_leave_nested(struct kvm_vcpu *vcpu);
2524
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525/*
2526 * Writes msr value into into the appropriate "register".
2527 * Returns 0 on success, non-0 otherwise.
2528 * Assumes vcpu_load() was already called.
2529 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002530static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002531{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002532 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002533 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002534 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002535 u32 msr_index = msr_info->index;
2536 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03002537
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002539 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002540 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002541 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002542#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002544 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545 vmcs_writel(GUEST_FS_BASE, data);
2546 break;
2547 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002548 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002549 vmcs_writel(GUEST_GS_BASE, data);
2550 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002551 case MSR_KERNEL_GS_BASE:
2552 vmx_load_host_state(vmx);
2553 vmx->msr_guest_kernel_gs_base = data;
2554 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555#endif
2556 case MSR_IA32_SYSENTER_CS:
2557 vmcs_write32(GUEST_SYSENTER_CS, data);
2558 break;
2559 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002560 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561 break;
2562 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002563 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564 break;
Liu, Jinsong0dd376e2014-02-24 10:56:53 +00002565 case MSR_IA32_BNDCFGS:
2566 vmcs_write64(GUEST_BNDCFGS, data);
2567 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302568 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08002569 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002571 case MSR_IA32_CR_PAT:
2572 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2573 vmcs_write64(GUEST_IA32_PAT, data);
2574 vcpu->arch.pat = data;
2575 break;
2576 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002577 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002578 break;
Will Auldba904632012-11-29 12:42:50 -08002579 case MSR_IA32_TSC_ADJUST:
2580 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002581 break;
Jan Kiszkacae50132014-01-04 18:47:22 +01002582 case MSR_IA32_FEATURE_CONTROL:
2583 if (!nested_vmx_allowed(vcpu) ||
2584 (to_vmx(vcpu)->nested.msr_ia32_feature_control &
2585 FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
2586 return 1;
2587 vmx->nested.msr_ia32_feature_control = data;
2588 if (msr_info->host_initiated && data == 0)
2589 vmx_leave_nested(vcpu);
2590 break;
2591 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2592 return 1; /* they are read-only */
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002593 case MSR_TSC_AUX:
2594 if (!vmx->rdtscp_enabled)
2595 return 1;
2596 /* Check reserved bit, higher 32 bits should be zero */
2597 if ((data >> 32) != 0)
2598 return 1;
2599 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10002601 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002602 if (msr) {
2603 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002604 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2605 preempt_disable();
Avi Kivity9ee73972012-03-06 14:16:33 +02002606 kvm_set_shared_msr(msr->index, msr->data,
2607 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03002608 preempt_enable();
2609 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002610 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002611 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002612 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613 }
2614
Eddie Dong2cc51562007-05-21 07:28:09 +03002615 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002616}
2617
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002618static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002619{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002620 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2621 switch (reg) {
2622 case VCPU_REGS_RSP:
2623 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2624 break;
2625 case VCPU_REGS_RIP:
2626 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2627 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002628 case VCPU_EXREG_PDPTR:
2629 if (enable_ept)
2630 ept_save_pdptrs(vcpu);
2631 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002632 default:
2633 break;
2634 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635}
2636
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637static __init int cpu_has_kvm_support(void)
2638{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002639 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002640}
2641
2642static __init int vmx_disabled_by_bios(void)
2643{
2644 u64 msr;
2645
2646 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04002647 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08002648 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04002649 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2650 && tboot_enabled())
2651 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08002652 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04002653 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08002654 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08002655 && !tboot_enabled()) {
2656 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08002657 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04002658 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08002659 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08002660 /* launched w/o TXT and VMX disabled */
2661 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2662 && !tboot_enabled())
2663 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04002664 }
2665
2666 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002667}
2668
Dongxiao Xu7725b892010-05-11 18:29:38 +08002669static void kvm_cpu_vmxon(u64 addr)
2670{
2671 asm volatile (ASM_VMX_VMXON_RAX
2672 : : "a"(&addr), "m"(addr)
2673 : "memory", "cc");
2674}
2675
Alexander Graf10474ae2009-09-15 11:37:46 +02002676static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677{
2678 int cpu = raw_smp_processor_id();
2679 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04002680 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002681
Alexander Graf10474ae2009-09-15 11:37:46 +02002682 if (read_cr4() & X86_CR4_VMXE)
2683 return -EBUSY;
2684
Nadav Har'Eld462b812011-05-24 15:26:10 +03002685 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002686
2687 /*
2688 * Now we can enable the vmclear operation in kdump
2689 * since the loaded_vmcss_on_cpu list on this cpu
2690 * has been initialized.
2691 *
2692 * Though the cpu is not in VMX operation now, there
2693 * is no problem to enable the vmclear operation
2694 * for the loaded_vmcss_on_cpu list is empty!
2695 */
2696 crash_enable_local_vmclear(cpu);
2697
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04002699
2700 test_bits = FEATURE_CONTROL_LOCKED;
2701 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2702 if (tboot_enabled())
2703 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2704
2705 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04002707 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2708 }
Rusty Russell66aee912007-07-17 23:34:16 +10002709 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02002710
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002711 if (vmm_exclusive) {
2712 kvm_cpu_vmxon(phys_addr);
2713 ept_sync_global();
2714 }
Alexander Graf10474ae2009-09-15 11:37:46 +02002715
Konrad Rzeszutek Wilk357d1222013-04-05 16:42:23 -04002716 native_store_gdt(&__get_cpu_var(host_gdt));
Avi Kivity3444d7d2010-07-26 18:32:38 +03002717
Alexander Graf10474ae2009-09-15 11:37:46 +02002718 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002719}
2720
Nadav Har'Eld462b812011-05-24 15:26:10 +03002721static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002722{
2723 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002724 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002725
Nadav Har'Eld462b812011-05-24 15:26:10 +03002726 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2727 loaded_vmcss_on_cpu_link)
2728 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002729}
2730
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002731
2732/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2733 * tricks.
2734 */
2735static void kvm_cpu_vmxoff(void)
2736{
2737 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002738}
2739
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740static void hardware_disable(void *garbage)
2741{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002742 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002743 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002744 kvm_cpu_vmxoff();
2745 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08002746 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747}
2748
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002749static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002750 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751{
2752 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002753 u32 ctl = ctl_min | ctl_opt;
2754
2755 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2756
2757 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2758 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2759
2760 /* Ensure minimum (required) set of control bits are supported. */
2761 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002762 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002763
2764 *result = ctl;
2765 return 0;
2766}
2767
Avi Kivity110312c2010-12-21 12:54:20 +02002768static __init bool allow_1_setting(u32 msr, u32 ctl)
2769{
2770 u32 vmx_msr_low, vmx_msr_high;
2771
2772 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2773 return vmx_msr_high & ctl;
2774}
2775
Yang, Sheng002c7f72007-07-31 14:23:01 +03002776static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002777{
2778 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002779 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002780 u32 _pin_based_exec_control = 0;
2781 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002782 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002783 u32 _vmexit_control = 0;
2784 u32 _vmentry_control = 0;
2785
Raghavendra K T10166742012-02-07 23:19:20 +05302786 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002787#ifdef CONFIG_X86_64
2788 CPU_BASED_CR8_LOAD_EXITING |
2789 CPU_BASED_CR8_STORE_EXITING |
2790#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002791 CPU_BASED_CR3_LOAD_EXITING |
2792 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002793 CPU_BASED_USE_IO_BITMAPS |
2794 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03002795 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08002796 CPU_BASED_MWAIT_EXITING |
2797 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002798 CPU_BASED_INVLPG_EXITING |
2799 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002800
Sheng Yangf78e0e22007-10-29 09:40:42 +08002801 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002802 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002803 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002804 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2805 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002806 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002807#ifdef CONFIG_X86_64
2808 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2809 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2810 ~CPU_BASED_CR8_STORE_EXITING;
2811#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002812 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002813 min2 = 0;
2814 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002815 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002816 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002817 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002818 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002819 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002820 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00002821 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002822 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002823 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03002824 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2825 SECONDARY_EXEC_SHADOW_VMCS;
Sheng Yangd56f5462008-04-25 10:13:16 +08002826 if (adjust_vmx_controls(min2, opt2,
2827 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002828 &_cpu_based_2nd_exec_control) < 0)
2829 return -EIO;
2830 }
2831#ifndef CONFIG_X86_64
2832 if (!(_cpu_based_2nd_exec_control &
2833 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2834 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2835#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002836
2837 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2838 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002839 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002840 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2841 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002842
Sheng Yangd56f5462008-04-25 10:13:16 +08002843 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002844 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2845 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002846 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2847 CPU_BASED_CR3_STORE_EXITING |
2848 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08002849 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
2850 vmx_capability.ept, vmx_capability.vpid);
2851 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002852
2853 min = 0;
2854#ifdef CONFIG_X86_64
2855 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2856#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08002857 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002858 VM_EXIT_ACK_INTR_ON_EXIT | VM_EXIT_CLEAR_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002859 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2860 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002861 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002862
Yang Zhang01e439b2013-04-11 19:25:12 +08002863 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2864 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR;
2865 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2866 &_pin_based_exec_control) < 0)
2867 return -EIO;
2868
2869 if (!(_cpu_based_2nd_exec_control &
2870 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) ||
2871 !(_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT))
2872 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2873
Sheng Yang468d4722008-10-09 16:01:55 +08002874 min = 0;
Liu, Jinsongda8999d2014-02-24 10:55:46 +00002875 opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002876 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2877 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002878 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002880 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002881
2882 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2883 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002884 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002885
2886#ifdef CONFIG_X86_64
2887 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2888 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002889 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002890#endif
2891
2892 /* Require Write-Back (WB) memory type for VMCS accesses. */
2893 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002894 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002895
Yang, Sheng002c7f72007-07-31 14:23:01 +03002896 vmcs_conf->size = vmx_msr_high & 0x1fff;
2897 vmcs_conf->order = get_order(vmcs_config.size);
2898 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002899
Yang, Sheng002c7f72007-07-31 14:23:01 +03002900 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2901 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002902 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002903 vmcs_conf->vmexit_ctrl = _vmexit_control;
2904 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002905
Avi Kivity110312c2010-12-21 12:54:20 +02002906 cpu_has_load_ia32_efer =
2907 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2908 VM_ENTRY_LOAD_IA32_EFER)
2909 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2910 VM_EXIT_LOAD_IA32_EFER);
2911
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002912 cpu_has_load_perf_global_ctrl =
2913 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2914 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
2915 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2916 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2917
2918 /*
2919 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
2920 * but due to arrata below it can't be used. Workaround is to use
2921 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2922 *
2923 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
2924 *
2925 * AAK155 (model 26)
2926 * AAP115 (model 30)
2927 * AAT100 (model 37)
2928 * BC86,AAY89,BD102 (model 44)
2929 * BA97 (model 46)
2930 *
2931 */
2932 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
2933 switch (boot_cpu_data.x86_model) {
2934 case 26:
2935 case 30:
2936 case 37:
2937 case 44:
2938 case 46:
2939 cpu_has_load_perf_global_ctrl = false;
2940 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2941 "does not work properly. Using workaround\n");
2942 break;
2943 default:
2944 break;
2945 }
2946 }
2947
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002948 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002949}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002950
2951static struct vmcs *alloc_vmcs_cpu(int cpu)
2952{
2953 int node = cpu_to_node(cpu);
2954 struct page *pages;
2955 struct vmcs *vmcs;
2956
Mel Gorman6484eb32009-06-16 15:31:54 -07002957 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958 if (!pages)
2959 return NULL;
2960 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002961 memset(vmcs, 0, vmcs_config.size);
2962 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002963 return vmcs;
2964}
2965
2966static struct vmcs *alloc_vmcs(void)
2967{
Ingo Molnard3b2c332007-01-05 16:36:23 -08002968 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969}
2970
2971static void free_vmcs(struct vmcs *vmcs)
2972{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002973 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002974}
2975
Nadav Har'Eld462b812011-05-24 15:26:10 +03002976/*
2977 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2978 */
2979static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
2980{
2981 if (!loaded_vmcs->vmcs)
2982 return;
2983 loaded_vmcs_clear(loaded_vmcs);
2984 free_vmcs(loaded_vmcs->vmcs);
2985 loaded_vmcs->vmcs = NULL;
2986}
2987
Sam Ravnborg39959582007-06-01 00:47:13 -07002988static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989{
2990 int cpu;
2991
Zachary Amsden3230bb42009-09-29 11:38:37 -10002992 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002993 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002994 per_cpu(vmxarea, cpu) = NULL;
2995 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996}
2997
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998static __init int alloc_kvm_area(void)
2999{
3000 int cpu;
3001
Zachary Amsden3230bb42009-09-29 11:38:37 -10003002 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003003 struct vmcs *vmcs;
3004
3005 vmcs = alloc_vmcs_cpu(cpu);
3006 if (!vmcs) {
3007 free_kvm_area();
3008 return -ENOMEM;
3009 }
3010
3011 per_cpu(vmxarea, cpu) = vmcs;
3012 }
3013 return 0;
3014}
3015
3016static __init int hardware_setup(void)
3017{
Yang, Sheng002c7f72007-07-31 14:23:01 +03003018 if (setup_vmcs_config(&vmcs_config) < 0)
3019 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01003020
3021 if (boot_cpu_has(X86_FEATURE_NX))
3022 kvm_enable_efer_bits(EFER_NX);
3023
Sheng Yang93ba03c2009-04-01 15:52:32 +08003024 if (!cpu_has_vmx_vpid())
3025 enable_vpid = 0;
Abel Gordonabc4fc52013-04-18 14:35:25 +03003026 if (!cpu_has_vmx_shadow_vmcs())
3027 enable_shadow_vmcs = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08003028
Sheng Yang4bc9b982010-06-02 14:05:24 +08003029 if (!cpu_has_vmx_ept() ||
3030 !cpu_has_vmx_ept_4levels()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08003031 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003032 enable_unrestricted_guest = 0;
Xudong Hao83c3a332012-05-28 19:33:35 +08003033 enable_ept_ad_bits = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003034 }
3035
Xudong Hao83c3a332012-05-28 19:33:35 +08003036 if (!cpu_has_vmx_ept_ad_bits())
3037 enable_ept_ad_bits = 0;
3038
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003039 if (!cpu_has_vmx_unrestricted_guest())
3040 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08003041
3042 if (!cpu_has_vmx_flexpriority())
3043 flexpriority_enabled = 0;
3044
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003045 if (!cpu_has_vmx_tpr_shadow())
3046 kvm_x86_ops->update_cr8_intercept = NULL;
3047
Marcelo Tosatti54dee992009-06-11 12:07:44 -03003048 if (enable_ept && !cpu_has_vmx_ept_2m_page())
3049 kvm_disable_largepages();
3050
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003051 if (!cpu_has_vmx_ple())
3052 ple_gap = 0;
3053
Yang Zhang01e439b2013-04-11 19:25:12 +08003054 if (!cpu_has_vmx_apicv())
3055 enable_apicv = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08003056
Yang Zhang01e439b2013-04-11 19:25:12 +08003057 if (enable_apicv)
Yang Zhangc7c9c562013-01-25 10:18:51 +08003058 kvm_x86_ops->update_cr8_intercept = NULL;
Yang Zhanga20ed542013-04-11 19:25:15 +08003059 else {
Yang Zhangc7c9c562013-01-25 10:18:51 +08003060 kvm_x86_ops->hwapic_irr_update = NULL;
Yang Zhanga20ed542013-04-11 19:25:15 +08003061 kvm_x86_ops->deliver_posted_interrupt = NULL;
3062 kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy;
3063 }
Yang Zhang83d4c282013-01-25 10:18:49 +08003064
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03003065 if (nested)
3066 nested_vmx_setup_ctls_msrs();
3067
Avi Kivity6aa8b732006-12-10 02:21:36 -08003068 return alloc_kvm_area();
3069}
3070
3071static __exit void hardware_unsetup(void)
3072{
3073 free_kvm_area();
3074}
3075
Gleb Natapov14168782013-01-21 15:36:49 +02003076static bool emulation_required(struct kvm_vcpu *vcpu)
3077{
3078 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
3079}
3080
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003081static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02003082 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083{
Gleb Natapovd99e4152012-12-20 16:57:45 +02003084 if (!emulate_invalid_guest_state) {
3085 /*
3086 * CS and SS RPL should be equal during guest entry according
3087 * to VMX spec, but in reality it is not always so. Since vcpu
3088 * is in the middle of the transition from real mode to
3089 * protected mode it is safe to assume that RPL 0 is a good
3090 * default value.
3091 */
3092 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
3093 save->selector &= ~SELECTOR_RPL_MASK;
3094 save->dpl = save->selector & SELECTOR_RPL_MASK;
3095 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003096 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02003097 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098}
3099
3100static void enter_pmode(struct kvm_vcpu *vcpu)
3101{
3102 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003103 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104
Gleb Natapovd99e4152012-12-20 16:57:45 +02003105 /*
3106 * Update real mode segment cache. It may be not up-to-date if sement
3107 * register was written while vcpu was in a guest mode.
3108 */
3109 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3110 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3111 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3112 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3113 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3114 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3115
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003116 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003117
Avi Kivity2fb92db2011-04-27 19:42:18 +03003118 vmx_segment_cache_clear(vmx);
3119
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003120 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003121
3122 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003123 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3124 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003125 vmcs_writel(GUEST_RFLAGS, flags);
3126
Rusty Russell66aee912007-07-17 23:34:16 +10003127 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3128 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129
3130 update_exception_bitmap(vcpu);
3131
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003132 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3133 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3134 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3135 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3136 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3137 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Gleb Natapov1f3141e2013-01-21 15:36:41 +02003138
3139 /* CPL is always 0 when CPU enters protected mode */
3140 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
3141 vmx->cpl = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142}
3143
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003144static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145{
Mathias Krause772e0312012-08-30 01:30:19 +02003146 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003147 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148
Gleb Natapovd99e4152012-12-20 16:57:45 +02003149 var.dpl = 0x3;
3150 if (seg == VCPU_SREG_CS)
3151 var.type = 0x3;
3152
3153 if (!emulate_invalid_guest_state) {
3154 var.selector = var.base >> 4;
3155 var.base = var.base & 0xffff0;
3156 var.limit = 0xffff;
3157 var.g = 0;
3158 var.db = 0;
3159 var.present = 1;
3160 var.s = 1;
3161 var.l = 0;
3162 var.unusable = 0;
3163 var.type = 0x3;
3164 var.avl = 0;
3165 if (save->base & 0xf)
3166 printk_once(KERN_WARNING "kvm: segment base is not "
3167 "paragraph aligned when entering "
3168 "protected mode (seg=%d)", seg);
3169 }
3170
3171 vmcs_write16(sf->selector, var.selector);
3172 vmcs_write32(sf->base, var.base);
3173 vmcs_write32(sf->limit, var.limit);
3174 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003175}
3176
3177static void enter_rmode(struct kvm_vcpu *vcpu)
3178{
3179 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003180 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003182 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3183 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3184 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3185 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3186 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003187 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3188 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003189
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003190 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191
Gleb Natapov776e58e2011-03-13 12:34:27 +02003192 /*
3193 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003194 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003195 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003196 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003197 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3198 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003199
Avi Kivity2fb92db2011-04-27 19:42:18 +03003200 vmx_segment_cache_clear(vmx);
3201
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003202 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3205
3206 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003207 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003208
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003209 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003210
3211 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003212 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003213 update_exception_bitmap(vcpu);
3214
Gleb Natapovd99e4152012-12-20 16:57:45 +02003215 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3216 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3217 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3218 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3219 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3220 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003221
Eddie Dong8668a3c2007-10-10 14:26:45 +08003222 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223}
3224
Amit Shah401d10d2009-02-20 22:53:37 +05303225static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3226{
3227 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003228 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3229
3230 if (!msr)
3231 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303232
Avi Kivity44ea2b12009-09-06 15:55:37 +03003233 /*
3234 * Force kernel_gs_base reloading before EFER changes, as control
3235 * of this msr depends on is_long_mode().
3236 */
3237 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003238 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303239 if (efer & EFER_LMA) {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003240 vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303241 msr->data = efer;
3242 } else {
Gleb Natapov2961e8762013-11-25 15:37:13 +02003243 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Amit Shah401d10d2009-02-20 22:53:37 +05303244
3245 msr->data = efer & ~EFER_LME;
3246 }
3247 setup_msrs(vmx);
3248}
3249
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003250#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003251
3252static void enter_lmode(struct kvm_vcpu *vcpu)
3253{
3254 u32 guest_tr_ar;
3255
Avi Kivity2fb92db2011-04-27 19:42:18 +03003256 vmx_segment_cache_clear(to_vmx(vcpu));
3257
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
3259 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003260 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3261 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003262 vmcs_write32(GUEST_TR_AR_BYTES,
3263 (guest_tr_ar & ~AR_TYPE_MASK)
3264 | AR_TYPE_BUSY_64_TSS);
3265 }
Avi Kivityda38f432010-07-06 11:30:49 +03003266 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267}
3268
3269static void exit_lmode(struct kvm_vcpu *vcpu)
3270{
Gleb Natapov2961e8762013-11-25 15:37:13 +02003271 vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003272 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273}
3274
3275#endif
3276
Sheng Yang2384d2b2008-01-17 15:14:33 +08003277static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3278{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08003279 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003280 if (enable_ept) {
3281 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3282 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003283 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003284 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003285}
3286
Avi Kivitye8467fd2009-12-29 18:43:06 +02003287static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3288{
3289 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3290
3291 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3292 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3293}
3294
Avi Kivityaff48ba2010-12-05 18:56:11 +02003295static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3296{
3297 if (enable_ept && is_paging(vcpu))
3298 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3299 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3300}
3301
Anthony Liguori25c4c272007-04-27 09:29:21 +03003302static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003303{
Avi Kivityfc78f512009-12-07 12:16:48 +02003304 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3305
3306 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3307 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003308}
3309
Sheng Yang14394422008-04-28 12:24:45 +08003310static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3311{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003312 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3313
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003314 if (!test_bit(VCPU_EXREG_PDPTR,
3315 (unsigned long *)&vcpu->arch.regs_dirty))
3316 return;
3317
Sheng Yang14394422008-04-28 12:24:45 +08003318 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003319 vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3320 vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3321 vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3322 vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003323 }
3324}
3325
Avi Kivity8f5d5492009-05-31 18:41:29 +03003326static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3327{
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003328 struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3329
Avi Kivity8f5d5492009-05-31 18:41:29 +03003330 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Gleb Natapovd0d538b2013-10-09 19:13:19 +03003331 mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3332 mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3333 mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3334 mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003335 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003336
3337 __set_bit(VCPU_EXREG_PDPTR,
3338 (unsigned long *)&vcpu->arch.regs_avail);
3339 __set_bit(VCPU_EXREG_PDPTR,
3340 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003341}
3342
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003343static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003344
3345static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3346 unsigned long cr0,
3347 struct kvm_vcpu *vcpu)
3348{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003349 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3350 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003351 if (!(cr0 & X86_CR0_PG)) {
3352 /* From paging/starting to nonpaging */
3353 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003354 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003355 (CPU_BASED_CR3_LOAD_EXITING |
3356 CPU_BASED_CR3_STORE_EXITING));
3357 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003358 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003359 } else if (!is_paging(vcpu)) {
3360 /* From nonpaging to paging */
3361 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003362 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003363 ~(CPU_BASED_CR3_LOAD_EXITING |
3364 CPU_BASED_CR3_STORE_EXITING));
3365 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003366 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003367 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003368
3369 if (!(cr0 & X86_CR0_WP))
3370 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003371}
3372
Avi Kivity6aa8b732006-12-10 02:21:36 -08003373static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3374{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003375 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003376 unsigned long hw_cr0;
3377
Gleb Natapov50378782013-02-04 16:00:28 +02003378 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003379 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003380 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003381 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003382 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003383
Gleb Natapov218e7632013-01-21 15:36:45 +02003384 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3385 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003386
Gleb Natapov218e7632013-01-21 15:36:45 +02003387 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3388 enter_rmode(vcpu);
3389 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003391#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003392 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003393 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003394 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003395 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003396 exit_lmode(vcpu);
3397 }
3398#endif
3399
Avi Kivity089d0342009-03-23 18:26:32 +02003400 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003401 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3402
Avi Kivity02daab22009-12-30 12:40:26 +02003403 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003404 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003405
Avi Kivity6aa8b732006-12-10 02:21:36 -08003406 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003407 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003408 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003409
3410 /* depends on vcpu->arch.cr0 to be set to a new value */
3411 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003412}
3413
Sheng Yang14394422008-04-28 12:24:45 +08003414static u64 construct_eptp(unsigned long root_hpa)
3415{
3416 u64 eptp;
3417
3418 /* TODO write the value reading from MSR */
3419 eptp = VMX_EPT_DEFAULT_MT |
3420 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003421 if (enable_ept_ad_bits)
3422 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003423 eptp |= (root_hpa & PAGE_MASK);
3424
3425 return eptp;
3426}
3427
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3429{
Sheng Yang14394422008-04-28 12:24:45 +08003430 unsigned long guest_cr3;
3431 u64 eptp;
3432
3433 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003434 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003435 eptp = construct_eptp(cr3);
3436 vmcs_write64(EPT_POINTER, eptp);
Jan Kiszka59ab5a82013-08-08 16:26:29 +02003437 if (is_paging(vcpu) || is_guest_mode(vcpu))
3438 guest_cr3 = kvm_read_cr3(vcpu);
3439 else
3440 guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02003441 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003442 }
3443
Sheng Yang2384d2b2008-01-17 15:14:33 +08003444 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003445 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003446}
3447
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003448static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003449{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003450 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08003451 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
3452
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003453 if (cr4 & X86_CR4_VMXE) {
3454 /*
3455 * To use VMXON (and later other VMX instructions), a guest
3456 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3457 * So basically the check on whether to allow nested VMX
3458 * is here.
3459 */
3460 if (!nested_vmx_allowed(vcpu))
3461 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003462 }
3463 if (to_vmx(vcpu)->nested.vmxon &&
3464 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003465 return 1;
3466
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003467 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02003468 if (enable_ept) {
3469 if (!is_paging(vcpu)) {
3470 hw_cr4 &= ~X86_CR4_PAE;
3471 hw_cr4 |= X86_CR4_PSE;
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003472 /*
3473 * SMEP is disabled if CPU is in non-paging mode in
3474 * hardware. However KVM always uses paging mode to
3475 * emulate guest non-paging mode with TDP.
3476 * To emulate this behavior, SMEP needs to be manually
3477 * disabled when guest switches to non-paging mode.
3478 */
3479 hw_cr4 &= ~X86_CR4_SMEP;
Avi Kivitybc230082009-12-08 12:14:42 +02003480 } else if (!(cr4 & X86_CR4_PAE)) {
3481 hw_cr4 &= ~X86_CR4_PAE;
3482 }
3483 }
Sheng Yang14394422008-04-28 12:24:45 +08003484
3485 vmcs_writel(CR4_READ_SHADOW, cr4);
3486 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003487 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003488}
3489
Avi Kivity6aa8b732006-12-10 02:21:36 -08003490static void vmx_get_segment(struct kvm_vcpu *vcpu,
3491 struct kvm_segment *var, int seg)
3492{
Avi Kivitya9179492011-01-03 14:28:52 +02003493 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003494 u32 ar;
3495
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003496 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003497 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003498 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003499 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003500 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003501 var->base = vmx_read_guest_seg_base(vmx, seg);
3502 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3503 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003504 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003505 var->base = vmx_read_guest_seg_base(vmx, seg);
3506 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3507 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3508 ar = vmx_read_guest_seg_ar(vmx, seg);
Gleb Natapov03617c12013-06-28 13:17:18 +03003509 var->unusable = (ar >> 16) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510 var->type = ar & 15;
3511 var->s = (ar >> 4) & 1;
3512 var->dpl = (ar >> 5) & 3;
Gleb Natapov03617c12013-06-28 13:17:18 +03003513 /*
3514 * Some userspaces do not preserve unusable property. Since usable
3515 * segment has to be present according to VMX spec we can use present
3516 * property to amend userspace bug by making unusable segment always
3517 * nonpresent. vmx_segment_access_rights() already marks nonpresent
3518 * segment as unusable.
3519 */
3520 var->present = !var->unusable;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521 var->avl = (ar >> 12) & 1;
3522 var->l = (ar >> 13) & 1;
3523 var->db = (ar >> 14) & 1;
3524 var->g = (ar >> 15) & 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003525}
3526
Avi Kivitya9179492011-01-03 14:28:52 +02003527static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3528{
Avi Kivitya9179492011-01-03 14:28:52 +02003529 struct kvm_segment s;
3530
3531 if (to_vmx(vcpu)->rmode.vm86_active) {
3532 vmx_get_segment(vcpu, &s, seg);
3533 return s.base;
3534 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003535 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003536}
3537
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003538static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003539{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003540 struct vcpu_vmx *vmx = to_vmx(vcpu);
3541
Avi Kivity3eeb3282010-01-21 15:31:48 +02003542 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003543 return 0;
3544
Avi Kivityf4c63e52011-03-07 14:54:28 +02003545 if (!is_long_mode(vcpu)
3546 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
Izik Eidus2e4d2652008-03-24 19:38:34 +02003547 return 3;
3548
Avi Kivity69c73022011-03-07 15:26:44 +02003549 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
3550 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003551 vmx->cpl = vmx_read_guest_seg_selector(vmx, VCPU_SREG_CS) & 3;
Avi Kivity69c73022011-03-07 15:26:44 +02003552 }
Avi Kivityd881e6f2012-06-06 18:36:48 +03003553
3554 return vmx->cpl;
Avi Kivity69c73022011-03-07 15:26:44 +02003555}
3556
3557
Avi Kivity653e3102007-05-07 10:55:37 +03003558static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003559{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003560 u32 ar;
3561
Avi Kivityf0495f92012-06-07 17:06:10 +03003562 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003563 ar = 1 << 16;
3564 else {
3565 ar = var->type & 15;
3566 ar |= (var->s & 1) << 4;
3567 ar |= (var->dpl & 3) << 5;
3568 ar |= (var->present & 1) << 7;
3569 ar |= (var->avl & 1) << 12;
3570 ar |= (var->l & 1) << 13;
3571 ar |= (var->db & 1) << 14;
3572 ar |= (var->g & 1) << 15;
3573 }
Avi Kivity653e3102007-05-07 10:55:37 +03003574
3575 return ar;
3576}
3577
3578static void vmx_set_segment(struct kvm_vcpu *vcpu,
3579 struct kvm_segment *var, int seg)
3580{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003581 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003582 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003583
Avi Kivity2fb92db2011-04-27 19:42:18 +03003584 vmx_segment_cache_clear(vmx);
Gleb Natapov2f143242013-01-21 15:36:42 +02003585 if (seg == VCPU_SREG_CS)
3586 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity2fb92db2011-04-27 19:42:18 +03003587
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003588 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3589 vmx->rmode.segs[seg] = *var;
3590 if (seg == VCPU_SREG_TR)
3591 vmcs_write16(sf->selector, var->selector);
3592 else if (var->s)
3593 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003594 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003595 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003596
Avi Kivity653e3102007-05-07 10:55:37 +03003597 vmcs_writel(sf->base, var->base);
3598 vmcs_write32(sf->limit, var->limit);
3599 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003600
3601 /*
3602 * Fix the "Accessed" bit in AR field of segment registers for older
3603 * qemu binaries.
3604 * IA32 arch specifies that at the time of processor reset the
3605 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003606 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003607 * state vmexit when "unrestricted guest" mode is turned on.
3608 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3609 * tree. Newer qemu binaries with that qemu fix would not need this
3610 * kvm hack.
3611 */
3612 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003613 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003614
Gleb Natapovf924d662012-12-12 19:10:55 +02003615 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003616
3617out:
Gleb Natapov14168782013-01-21 15:36:49 +02003618 vmx->emulation_required |= emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003619}
3620
Avi Kivity6aa8b732006-12-10 02:21:36 -08003621static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3622{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003623 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003624
3625 *db = (ar >> 14) & 1;
3626 *l = (ar >> 13) & 1;
3627}
3628
Gleb Natapov89a27f42010-02-16 10:51:48 +02003629static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003630{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003631 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3632 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633}
3634
Gleb Natapov89a27f42010-02-16 10:51:48 +02003635static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003636{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003637 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3638 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003639}
3640
Gleb Natapov89a27f42010-02-16 10:51:48 +02003641static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003642{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003643 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3644 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003645}
3646
Gleb Natapov89a27f42010-02-16 10:51:48 +02003647static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003648{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003649 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3650 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003651}
3652
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003653static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3654{
3655 struct kvm_segment var;
3656 u32 ar;
3657
3658 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003659 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003660 if (seg == VCPU_SREG_CS)
3661 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003662 ar = vmx_segment_access_rights(&var);
3663
3664 if (var.base != (var.selector << 4))
3665 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003666 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003667 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003668 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003669 return false;
3670
3671 return true;
3672}
3673
3674static bool code_segment_valid(struct kvm_vcpu *vcpu)
3675{
3676 struct kvm_segment cs;
3677 unsigned int cs_rpl;
3678
3679 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3680 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
3681
Avi Kivity1872a3f2009-01-04 23:26:52 +02003682 if (cs.unusable)
3683 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003684 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
3685 return false;
3686 if (!cs.s)
3687 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003688 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003689 if (cs.dpl > cs_rpl)
3690 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003691 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003692 if (cs.dpl != cs_rpl)
3693 return false;
3694 }
3695 if (!cs.present)
3696 return false;
3697
3698 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3699 return true;
3700}
3701
3702static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3703{
3704 struct kvm_segment ss;
3705 unsigned int ss_rpl;
3706
3707 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3708 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
3709
Avi Kivity1872a3f2009-01-04 23:26:52 +02003710 if (ss.unusable)
3711 return true;
3712 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003713 return false;
3714 if (!ss.s)
3715 return false;
3716 if (ss.dpl != ss_rpl) /* DPL != RPL */
3717 return false;
3718 if (!ss.present)
3719 return false;
3720
3721 return true;
3722}
3723
3724static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3725{
3726 struct kvm_segment var;
3727 unsigned int rpl;
3728
3729 vmx_get_segment(vcpu, &var, seg);
3730 rpl = var.selector & SELECTOR_RPL_MASK;
3731
Avi Kivity1872a3f2009-01-04 23:26:52 +02003732 if (var.unusable)
3733 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003734 if (!var.s)
3735 return false;
3736 if (!var.present)
3737 return false;
3738 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
3739 if (var.dpl < rpl) /* DPL < RPL */
3740 return false;
3741 }
3742
3743 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3744 * rights flags
3745 */
3746 return true;
3747}
3748
3749static bool tr_valid(struct kvm_vcpu *vcpu)
3750{
3751 struct kvm_segment tr;
3752
3753 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3754
Avi Kivity1872a3f2009-01-04 23:26:52 +02003755 if (tr.unusable)
3756 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003757 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3758 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003759 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003760 return false;
3761 if (!tr.present)
3762 return false;
3763
3764 return true;
3765}
3766
3767static bool ldtr_valid(struct kvm_vcpu *vcpu)
3768{
3769 struct kvm_segment ldtr;
3770
3771 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3772
Avi Kivity1872a3f2009-01-04 23:26:52 +02003773 if (ldtr.unusable)
3774 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003775 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3776 return false;
3777 if (ldtr.type != 2)
3778 return false;
3779 if (!ldtr.present)
3780 return false;
3781
3782 return true;
3783}
3784
3785static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3786{
3787 struct kvm_segment cs, ss;
3788
3789 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3790 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3791
3792 return ((cs.selector & SELECTOR_RPL_MASK) ==
3793 (ss.selector & SELECTOR_RPL_MASK));
3794}
3795
3796/*
3797 * Check if guest state is valid. Returns true if valid, false if
3798 * not.
3799 * We assume that registers are always usable
3800 */
3801static bool guest_state_valid(struct kvm_vcpu *vcpu)
3802{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02003803 if (enable_unrestricted_guest)
3804 return true;
3805
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003806 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003807 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003808 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3809 return false;
3810 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3811 return false;
3812 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3813 return false;
3814 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3815 return false;
3816 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3817 return false;
3818 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3819 return false;
3820 } else {
3821 /* protected mode guest state checks */
3822 if (!cs_ss_rpl_check(vcpu))
3823 return false;
3824 if (!code_segment_valid(vcpu))
3825 return false;
3826 if (!stack_segment_valid(vcpu))
3827 return false;
3828 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3829 return false;
3830 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3831 return false;
3832 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3833 return false;
3834 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3835 return false;
3836 if (!tr_valid(vcpu))
3837 return false;
3838 if (!ldtr_valid(vcpu))
3839 return false;
3840 }
3841 /* TODO:
3842 * - Add checks on RIP
3843 * - Add checks on RFLAGS
3844 */
3845
3846 return true;
3847}
3848
Mike Dayd77c26f2007-10-08 09:02:08 -04003849static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003851 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003852 u16 data = 0;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003853 int r, idx, ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003854
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003855 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003856 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003857 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3858 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003859 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003860 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003861 r = kvm_write_guest_page(kvm, fn++, &data,
3862 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003863 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003864 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003865 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3866 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003867 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003868 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3869 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003870 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003871 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003872 r = kvm_write_guest_page(kvm, fn, &data,
3873 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3874 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02003875 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003876 goto out;
3877
3878 ret = 1;
3879out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003880 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003881 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003882}
3883
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003884static int init_rmode_identity_map(struct kvm *kvm)
3885{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003886 int i, idx, r, ret;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003887 pfn_t identity_map_pfn;
3888 u32 tmp;
3889
Avi Kivity089d0342009-03-23 18:26:32 +02003890 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003891 return 1;
3892 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
3893 printk(KERN_ERR "EPT: identity-mapping pagetable "
3894 "haven't been allocated!\n");
3895 return 0;
3896 }
3897 if (likely(kvm->arch.ept_identity_pagetable_done))
3898 return 1;
3899 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003900 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003901 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003902 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3903 if (r < 0)
3904 goto out;
3905 /* Set up identity-mapping pagetable for EPT in real mode */
3906 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3907 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3908 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3909 r = kvm_write_guest_page(kvm, identity_map_pfn,
3910 &tmp, i * sizeof(tmp), sizeof(tmp));
3911 if (r < 0)
3912 goto out;
3913 }
3914 kvm->arch.ept_identity_pagetable_done = true;
3915 ret = 1;
3916out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003917 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003918 return ret;
3919}
3920
Avi Kivity6aa8b732006-12-10 02:21:36 -08003921static void seg_setup(int seg)
3922{
Mathias Krause772e0312012-08-30 01:30:19 +02003923 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003924 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003925
3926 vmcs_write16(sf->selector, 0);
3927 vmcs_writel(sf->base, 0);
3928 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003929 ar = 0x93;
3930 if (seg == VCPU_SREG_CS)
3931 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003932
3933 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003934}
3935
Sheng Yangf78e0e22007-10-29 09:40:42 +08003936static int alloc_apic_access_page(struct kvm *kvm)
3937{
Xiao Guangrong44841412012-09-07 14:14:20 +08003938 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003939 struct kvm_userspace_memory_region kvm_userspace_mem;
3940 int r = 0;
3941
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003942 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003943 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003944 goto out;
3945 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
3946 kvm_userspace_mem.flags = 0;
3947 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
3948 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09003949 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003950 if (r)
3951 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003952
Xiao Guangrong44841412012-09-07 14:14:20 +08003953 page = gfn_to_page(kvm, 0xfee00);
3954 if (is_error_page(page)) {
3955 r = -EFAULT;
3956 goto out;
3957 }
3958
3959 kvm->arch.apic_access_page = page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003960out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003961 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003962 return r;
3963}
3964
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003965static int alloc_identity_pagetable(struct kvm *kvm)
3966{
Xiao Guangrong44841412012-09-07 14:14:20 +08003967 struct page *page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003968 struct kvm_userspace_memory_region kvm_userspace_mem;
3969 int r = 0;
3970
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003971 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003972 if (kvm->arch.ept_identity_pagetable)
3973 goto out;
3974 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
3975 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003976 kvm_userspace_mem.guest_phys_addr =
3977 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003978 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09003979 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003980 if (r)
3981 goto out;
3982
Xiao Guangrong44841412012-09-07 14:14:20 +08003983 page = gfn_to_page(kvm, kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
3984 if (is_error_page(page)) {
3985 r = -EFAULT;
3986 goto out;
3987 }
3988
3989 kvm->arch.ept_identity_pagetable = page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003990out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003991 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003992 return r;
3993}
3994
Sheng Yang2384d2b2008-01-17 15:14:33 +08003995static void allocate_vpid(struct vcpu_vmx *vmx)
3996{
3997 int vpid;
3998
3999 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02004000 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08004001 return;
4002 spin_lock(&vmx_vpid_lock);
4003 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
4004 if (vpid < VMX_NR_VPIDS) {
4005 vmx->vpid = vpid;
4006 __set_bit(vpid, vmx_vpid_bitmap);
4007 }
4008 spin_unlock(&vmx_vpid_lock);
4009}
4010
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004011static void free_vpid(struct vcpu_vmx *vmx)
4012{
4013 if (!enable_vpid)
4014 return;
4015 spin_lock(&vmx_vpid_lock);
4016 if (vmx->vpid != 0)
4017 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
4018 spin_unlock(&vmx_vpid_lock);
4019}
4020
Yang Zhang8d146952013-01-25 10:18:50 +08004021#define MSR_TYPE_R 1
4022#define MSR_TYPE_W 2
4023static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4024 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08004025{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004026 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08004027
4028 if (!cpu_has_vmx_msr_bitmap())
4029 return;
4030
4031 /*
4032 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4033 * have the write-low and read-high bitmap offsets the wrong way round.
4034 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4035 */
Sheng Yang25c5f222008-03-28 13:18:56 +08004036 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08004037 if (type & MSR_TYPE_R)
4038 /* read-low */
4039 __clear_bit(msr, msr_bitmap + 0x000 / f);
4040
4041 if (type & MSR_TYPE_W)
4042 /* write-low */
4043 __clear_bit(msr, msr_bitmap + 0x800 / f);
4044
Sheng Yang25c5f222008-03-28 13:18:56 +08004045 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4046 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08004047 if (type & MSR_TYPE_R)
4048 /* read-high */
4049 __clear_bit(msr, msr_bitmap + 0x400 / f);
4050
4051 if (type & MSR_TYPE_W)
4052 /* write-high */
4053 __clear_bit(msr, msr_bitmap + 0xc00 / f);
4054
4055 }
4056}
4057
4058static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4059 u32 msr, int type)
4060{
4061 int f = sizeof(unsigned long);
4062
4063 if (!cpu_has_vmx_msr_bitmap())
4064 return;
4065
4066 /*
4067 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4068 * have the write-low and read-high bitmap offsets the wrong way round.
4069 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4070 */
4071 if (msr <= 0x1fff) {
4072 if (type & MSR_TYPE_R)
4073 /* read-low */
4074 __set_bit(msr, msr_bitmap + 0x000 / f);
4075
4076 if (type & MSR_TYPE_W)
4077 /* write-low */
4078 __set_bit(msr, msr_bitmap + 0x800 / f);
4079
4080 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4081 msr &= 0x1fff;
4082 if (type & MSR_TYPE_R)
4083 /* read-high */
4084 __set_bit(msr, msr_bitmap + 0x400 / f);
4085
4086 if (type & MSR_TYPE_W)
4087 /* write-high */
4088 __set_bit(msr, msr_bitmap + 0xc00 / f);
4089
Sheng Yang25c5f222008-03-28 13:18:56 +08004090 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004091}
4092
Avi Kivity58972972009-02-24 22:26:47 +02004093static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
4094{
4095 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08004096 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
4097 msr, MSR_TYPE_R | MSR_TYPE_W);
4098 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
4099 msr, MSR_TYPE_R | MSR_TYPE_W);
4100}
4101
4102static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
4103{
4104 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4105 msr, MSR_TYPE_R);
4106 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4107 msr, MSR_TYPE_R);
4108}
4109
4110static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
4111{
4112 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4113 msr, MSR_TYPE_R);
4114 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4115 msr, MSR_TYPE_R);
4116}
4117
4118static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
4119{
4120 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
4121 msr, MSR_TYPE_W);
4122 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4123 msr, MSR_TYPE_W);
Avi Kivity58972972009-02-24 22:26:47 +02004124}
4125
Yang Zhang01e439b2013-04-11 19:25:12 +08004126static int vmx_vm_has_apicv(struct kvm *kvm)
4127{
4128 return enable_apicv && irqchip_in_kernel(kvm);
4129}
4130
Avi Kivity6aa8b732006-12-10 02:21:36 -08004131/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004132 * Send interrupt to vcpu via posted interrupt way.
4133 * 1. If target vcpu is running(non-root mode), send posted interrupt
4134 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4135 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4136 * interrupt from PIR in next vmentry.
4137 */
4138static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4139{
4140 struct vcpu_vmx *vmx = to_vmx(vcpu);
4141 int r;
4142
4143 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4144 return;
4145
4146 r = pi_test_and_set_on(&vmx->pi_desc);
4147 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang, Yang Z6ffbbbb2013-04-17 23:11:54 -03004148#ifdef CONFIG_SMP
Yang Zhanga20ed542013-04-11 19:25:15 +08004149 if (!r && (vcpu->mode == IN_GUEST_MODE))
4150 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4151 POSTED_INTR_VECTOR);
4152 else
Zhang, Yang Z6ffbbbb2013-04-17 23:11:54 -03004153#endif
Yang Zhanga20ed542013-04-11 19:25:15 +08004154 kvm_vcpu_kick(vcpu);
4155}
4156
4157static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4158{
4159 struct vcpu_vmx *vmx = to_vmx(vcpu);
4160
4161 if (!pi_test_and_clear_on(&vmx->pi_desc))
4162 return;
4163
4164 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4165}
4166
4167static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu)
4168{
4169 return;
4170}
4171
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004173 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4174 * will not change in the lifetime of the guest.
4175 * Note that host-state that does change is set elsewhere. E.g., host-state
4176 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4177 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004178static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004179{
4180 u32 low32, high32;
4181 unsigned long tmpl;
4182 struct desc_ptr dt;
4183
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07004184 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004185 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
4186 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4187
4188 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004189#ifdef CONFIG_X86_64
4190 /*
4191 * Load null selectors, so we can avoid reloading them in
4192 * __vmx_load_host_state(), in case userspace uses the null selectors
4193 * too (the expected case).
4194 */
4195 vmcs_write16(HOST_DS_SELECTOR, 0);
4196 vmcs_write16(HOST_ES_SELECTOR, 0);
4197#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004198 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4199 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004200#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004201 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4202 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4203
4204 native_store_idt(&dt);
4205 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004206 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004207
Avi Kivity83287ea422012-09-16 15:10:57 +03004208 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004209
4210 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4211 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4212 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4213 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4214
4215 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4216 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4217 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4218 }
4219}
4220
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004221static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4222{
4223 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4224 if (enable_ept)
4225 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004226 if (is_guest_mode(&vmx->vcpu))
4227 vmx->vcpu.arch.cr4_guest_owned_bits &=
4228 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004229 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4230}
4231
Yang Zhang01e439b2013-04-11 19:25:12 +08004232static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4233{
4234 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4235
4236 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4237 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
4238 return pin_based_exec_ctrl;
4239}
4240
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004241static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4242{
4243 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
4244 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
4245 exec_control &= ~CPU_BASED_TPR_SHADOW;
4246#ifdef CONFIG_X86_64
4247 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4248 CPU_BASED_CR8_LOAD_EXITING;
4249#endif
4250 }
4251 if (!enable_ept)
4252 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4253 CPU_BASED_CR3_LOAD_EXITING |
4254 CPU_BASED_INVLPG_EXITING;
4255 return exec_control;
4256}
4257
4258static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4259{
4260 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
4261 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4262 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4263 if (vmx->vpid == 0)
4264 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4265 if (!enable_ept) {
4266 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4267 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00004268 /* Enable INVPCID for non-ept guests may cause performance regression. */
4269 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004270 }
4271 if (!enable_unrestricted_guest)
4272 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4273 if (!ple_gap)
4274 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Yang Zhangc7c9c562013-01-25 10:18:51 +08004275 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4276 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4277 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004278 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03004279 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4280 (handle_vmptrld).
4281 We can NOT enable shadow_vmcs here because we don't have yet
4282 a current VMCS12
4283 */
4284 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004285 return exec_control;
4286}
4287
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004288static void ept_set_mmio_spte_mask(void)
4289{
4290 /*
4291 * EPT Misconfigurations can be generated if the value of bits 2:0
4292 * of an EPT paging-structure entry is 110b (write/execute).
Xiao Guangrong885032b2013-06-07 16:51:23 +08004293 * Also, magic bits (0x3ull << 62) is set to quickly identify mmio
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004294 * spte.
4295 */
Xiao Guangrong885032b2013-06-07 16:51:23 +08004296 kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004297}
4298
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004299/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004300 * Sets up the vmcs for emulated real mode.
4301 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004302static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004303{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004304#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004305 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004306#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004307 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004308
Avi Kivity6aa8b732006-12-10 02:21:36 -08004309 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004310 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4311 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004312
Abel Gordon4607c2d2013-04-18 14:35:55 +03004313 if (enable_shadow_vmcs) {
4314 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
4315 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
4316 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004317 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02004318 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08004319
Avi Kivity6aa8b732006-12-10 02:21:36 -08004320 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4321
Avi Kivity6aa8b732006-12-10 02:21:36 -08004322 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08004323 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004324
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004325 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004326
Sheng Yang83ff3b92007-11-21 14:33:25 +08004327 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004328 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4329 vmx_secondary_exec_control(vmx));
Sheng Yang83ff3b92007-11-21 14:33:25 +08004330 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004331
Yang Zhang01e439b2013-04-11 19:25:12 +08004332 if (vmx_vm_has_apicv(vmx->vcpu.kvm)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004333 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4334 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4335 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4336 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4337
4338 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004339
4340 vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
4341 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004342 }
4343
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004344 if (ple_gap) {
4345 vmcs_write32(PLE_GAP, ple_gap);
4346 vmcs_write32(PLE_WINDOW, ple_window);
4347 }
4348
Xiao Guangrongc3707952011-07-12 03:28:04 +08004349 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4350 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004351 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4352
Avi Kivity9581d442010-10-19 16:46:55 +02004353 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4354 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004355 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004356#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004357 rdmsrl(MSR_FS_BASE, a);
4358 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
4359 rdmsrl(MSR_GS_BASE, a);
4360 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
4361#else
4362 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4363 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
4364#endif
4365
Eddie Dong2cc51562007-05-21 07:28:09 +03004366 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4367 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004368 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03004369 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004370 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004371
Sheng Yang468d4722008-10-09 16:01:55 +08004372 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004373 u32 msr_low, msr_high;
4374 u64 host_pat;
Sheng Yang468d4722008-10-09 16:01:55 +08004375 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
4376 host_pat = msr_low | ((u64) msr_high << 32);
4377 /* Write the default value follow host pat */
4378 vmcs_write64(GUEST_IA32_PAT, host_pat);
4379 /* Keep arch.pat sync with GUEST_IA32_PAT */
4380 vmx->vcpu.arch.pat = host_pat;
4381 }
4382
Avi Kivity6aa8b732006-12-10 02:21:36 -08004383 for (i = 0; i < NR_VMX_MSR; ++i) {
4384 u32 index = vmx_msr_index[i];
4385 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004386 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004387
4388 if (rdmsr_safe(index, &data_low, &data_high) < 0)
4389 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08004390 if (wrmsr_safe(index, data_low, data_high) < 0)
4391 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03004392 vmx->guest_msrs[j].index = i;
4393 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02004394 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004395 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004396 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004397
Gleb Natapov2961e8762013-11-25 15:37:13 +02004398
4399 vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004400
4401 /* 22.2.1, 20.8.1 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02004402 vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004403
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004404 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004405 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004406
4407 return 0;
4408}
4409
Jan Kiszka57f252f2013-03-12 10:20:24 +01004410static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004411{
4412 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004413 struct msr_data apic_base_msr;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004414
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004415 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004416
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004417 vmx->soft_vnmi_blocked = 0;
4418
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004419 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004420 kvm_set_cr8(&vmx->vcpu, 0);
Jan Kiszka58cb6282014-01-24 16:48:44 +01004421 apic_base_msr.data = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004422 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Jan Kiszka58cb6282014-01-24 16:48:44 +01004423 apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
4424 apic_base_msr.host_initiated = true;
4425 kvm_set_apic_base(&vmx->vcpu, &apic_base_msr);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004426
Avi Kivity2fb92db2011-04-27 19:42:18 +03004427 vmx_segment_cache_clear(vmx);
4428
Avi Kivity5706be02008-08-20 15:07:31 +03004429 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004430 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzini04b66832013-03-19 16:30:26 +01004431 vmcs_write32(GUEST_CS_BASE, 0xffff0000);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004432
4433 seg_setup(VCPU_SREG_DS);
4434 seg_setup(VCPU_SREG_ES);
4435 seg_setup(VCPU_SREG_FS);
4436 seg_setup(VCPU_SREG_GS);
4437 seg_setup(VCPU_SREG_SS);
4438
4439 vmcs_write16(GUEST_TR_SELECTOR, 0);
4440 vmcs_writel(GUEST_TR_BASE, 0);
4441 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4442 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4443
4444 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4445 vmcs_writel(GUEST_LDTR_BASE, 0);
4446 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4447 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4448
4449 vmcs_write32(GUEST_SYSENTER_CS, 0);
4450 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4451 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4452
4453 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01004454 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004455
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004456 vmcs_writel(GUEST_GDTR_BASE, 0);
4457 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4458
4459 vmcs_writel(GUEST_IDTR_BASE, 0);
4460 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4461
Anthony Liguori443381a2010-12-06 10:53:38 -06004462 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004463 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
4464 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
4465
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004466 /* Special registers */
4467 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4468
4469 setup_msrs(vmx);
4470
Avi Kivity6aa8b732006-12-10 02:21:36 -08004471 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4472
Sheng Yangf78e0e22007-10-29 09:40:42 +08004473 if (cpu_has_vmx_tpr_shadow()) {
4474 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
4475 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
4476 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09004477 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004478 vmcs_write32(TPR_THRESHOLD, 0);
4479 }
4480
4481 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4482 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004483 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004484
Yang Zhang01e439b2013-04-11 19:25:12 +08004485 if (vmx_vm_has_apicv(vcpu->kvm))
4486 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
4487
Sheng Yang2384d2b2008-01-17 15:14:33 +08004488 if (vmx->vpid != 0)
4489 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4490
Eduardo Habkostfa400522009-10-24 02:49:58 -02004491 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004492 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004493 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004494 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004495 vmx_fpu_activate(&vmx->vcpu);
4496 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004497
Gui Jianfengb9d762f2010-06-07 10:32:29 +08004498 vpid_sync_context(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004499}
4500
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004501/*
4502 * In nested virtualization, check if L1 asked to exit on external interrupts.
4503 * For most existing hypervisors, this will always return true.
4504 */
4505static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4506{
4507 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4508 PIN_BASED_EXT_INTR_MASK;
4509}
4510
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004511static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu)
4512{
4513 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4514 PIN_BASED_NMI_EXITING;
4515}
4516
Jan Kiszkac9a79532014-03-07 20:03:15 +01004517static void enable_irq_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004518{
4519 u32 cpu_based_vm_exec_control;
Jan Kiszka730dca42013-04-28 10:50:52 +02004520
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004521 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4522 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
4523 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4524}
4525
Jan Kiszkac9a79532014-03-07 20:03:15 +01004526static void enable_nmi_window(struct kvm_vcpu *vcpu)
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004527{
4528 u32 cpu_based_vm_exec_control;
4529
Jan Kiszkac9a79532014-03-07 20:03:15 +01004530 if (!cpu_has_virtual_nmis() ||
4531 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
4532 enable_irq_window(vcpu);
4533 return;
4534 }
Jan Kiszka03b28f82013-04-29 16:46:42 +02004535
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004536 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4537 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
4538 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4539}
4540
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004541static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004542{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004543 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004544 uint32_t intr;
4545 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004546
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004547 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004548
Avi Kivityfa89a812008-09-01 15:57:51 +03004549 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004550 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004551 int inc_eip = 0;
4552 if (vcpu->arch.interrupt.soft)
4553 inc_eip = vcpu->arch.event_exit_inst_len;
4554 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004555 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004556 return;
4557 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004558 intr = irq | INTR_INFO_VALID_MASK;
4559 if (vcpu->arch.interrupt.soft) {
4560 intr |= INTR_TYPE_SOFT_INTR;
4561 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4562 vmx->vcpu.arch.event_exit_inst_len);
4563 } else
4564 intr |= INTR_TYPE_EXT_INTR;
4565 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03004566}
4567
Sheng Yangf08864b2008-05-15 18:23:25 +08004568static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4569{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004570 struct vcpu_vmx *vmx = to_vmx(vcpu);
4571
Nadav Har'El0b6ac342011-05-25 23:13:36 +03004572 if (is_guest_mode(vcpu))
4573 return;
4574
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004575 if (!cpu_has_virtual_nmis()) {
4576 /*
4577 * Tracking the NMI-blocked state in software is built upon
4578 * finding the next open IRQ window. This, in turn, depends on
4579 * well-behaving guests: They have to keep IRQs disabled at
4580 * least as long as the NMI handler runs. Otherwise we may
4581 * cause NMI nesting, maybe breaking the guest. But as this is
4582 * highly unlikely, we can live with the residual risk.
4583 */
4584 vmx->soft_vnmi_blocked = 1;
4585 vmx->vnmi_blocked_time = 0;
4586 }
4587
Jan Kiszka487b3912008-09-26 09:30:56 +02004588 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02004589 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004590 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004591 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004592 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004593 return;
4594 }
Sheng Yangf08864b2008-05-15 18:23:25 +08004595 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4596 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08004597}
4598
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004599static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
4600{
4601 if (!cpu_has_virtual_nmis())
4602 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02004603 if (to_vmx(vcpu)->nmi_known_unmasked)
4604 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03004605 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004606}
4607
4608static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4609{
4610 struct vcpu_vmx *vmx = to_vmx(vcpu);
4611
4612 if (!cpu_has_virtual_nmis()) {
4613 if (vmx->soft_vnmi_blocked != masked) {
4614 vmx->soft_vnmi_blocked = masked;
4615 vmx->vnmi_blocked_time = 0;
4616 }
4617 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02004618 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004619 if (masked)
4620 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4621 GUEST_INTR_STATE_NMI);
4622 else
4623 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4624 GUEST_INTR_STATE_NMI);
4625 }
4626}
4627
Jan Kiszka2505dc92013-04-14 12:12:47 +02004628static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
4629{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004630 if (to_vmx(vcpu)->nested.nested_run_pending)
4631 return 0;
Jan Kiszkaea8ceb82013-04-14 21:04:26 +02004632
Jan Kiszka2505dc92013-04-14 12:12:47 +02004633 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
4634 return 0;
4635
4636 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4637 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4638 | GUEST_INTR_STATE_NMI));
4639}
4640
Gleb Natapov78646122009-03-23 12:12:11 +02004641static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4642{
Jan Kiszkab6b8a142014-03-07 20:03:12 +01004643 return (!to_vmx(vcpu)->nested.nested_run_pending &&
4644 vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
Gleb Natapovc4282df2009-04-21 17:45:07 +03004645 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4646 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02004647}
4648
Izik Eiduscbc94022007-10-25 00:29:55 +02004649static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4650{
4651 int ret;
4652 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08004653 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02004654 .guest_phys_addr = addr,
4655 .memory_size = PAGE_SIZE * 3,
4656 .flags = 0,
4657 };
4658
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09004659 ret = kvm_set_memory_region(kvm, &tss_mem);
Izik Eiduscbc94022007-10-25 00:29:55 +02004660 if (ret)
4661 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004662 kvm->arch.tss_addr = addr;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004663 if (!init_rmode_tss(kvm))
4664 return -ENOMEM;
4665
Izik Eiduscbc94022007-10-25 00:29:55 +02004666 return 0;
4667}
4668
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004669static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004670{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004671 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004672 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004673 /*
4674 * Update instruction length as we may reinject the exception
4675 * from user space while in guest debugging mode.
4676 */
4677 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4678 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004679 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004680 return false;
4681 /* fall through */
4682 case DB_VECTOR:
4683 if (vcpu->guest_debug &
4684 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4685 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004686 /* fall through */
4687 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004688 case OF_VECTOR:
4689 case BR_VECTOR:
4690 case UD_VECTOR:
4691 case DF_VECTOR:
4692 case SS_VECTOR:
4693 case GP_VECTOR:
4694 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004695 return true;
4696 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004697 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004698 return false;
4699}
4700
4701static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4702 int vec, u32 err_code)
4703{
4704 /*
4705 * Instruction with address size override prefix opcode 0x67
4706 * Cause the #SS fault with 0 error code in VM86 mode.
4707 */
4708 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
4709 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
4710 if (vcpu->arch.halt_request) {
4711 vcpu->arch.halt_request = 0;
4712 return kvm_emulate_halt(vcpu);
4713 }
4714 return 1;
4715 }
4716 return 0;
4717 }
4718
4719 /*
4720 * Forward all other exceptions that are valid in real mode.
4721 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4722 * the required debugging infrastructure rework.
4723 */
4724 kvm_queue_exception(vcpu, vec);
4725 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004726}
4727
Andi Kleena0861c02009-06-08 17:37:09 +08004728/*
4729 * Trigger machine check on the host. We assume all the MSRs are already set up
4730 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4731 * We pass a fake environment to the machine check handler because we want
4732 * the guest to be always treated like user space, no matter what context
4733 * it used internally.
4734 */
4735static void kvm_machine_check(void)
4736{
4737#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4738 struct pt_regs regs = {
4739 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4740 .flags = X86_EFLAGS_IF,
4741 };
4742
4743 do_machine_check(&regs, 0);
4744#endif
4745}
4746
Avi Kivity851ba692009-08-24 11:10:17 +03004747static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004748{
4749 /* already handled by vcpu_run */
4750 return 1;
4751}
4752
Avi Kivity851ba692009-08-24 11:10:17 +03004753static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004754{
Avi Kivity1155f762007-11-22 11:30:47 +02004755 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004756 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004757 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004758 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004759 u32 vect_info;
4760 enum emulation_result er;
4761
Avi Kivity1155f762007-11-22 11:30:47 +02004762 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02004763 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004764
Andi Kleena0861c02009-06-08 17:37:09 +08004765 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03004766 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004767
Jan Kiszkae4a41882008-09-26 09:30:46 +02004768 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02004769 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004770
4771 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03004772 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004773 return 1;
4774 }
4775
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004776 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01004777 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004778 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02004779 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004780 return 1;
4781 }
4782
Avi Kivity6aa8b732006-12-10 02:21:36 -08004783 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004784 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004785 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004786
4787 /*
4788 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4789 * MMIO, it is better to report an internal error.
4790 * See the comments in vmx_handle_exit.
4791 */
4792 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4793 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4794 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4795 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
4796 vcpu->run->internal.ndata = 2;
4797 vcpu->run->internal.data[0] = vect_info;
4798 vcpu->run->internal.data[1] = intr_info;
4799 return 0;
4800 }
4801
Avi Kivity6aa8b732006-12-10 02:21:36 -08004802 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08004803 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02004804 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004805 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004806 trace_kvm_page_fault(cr2, error_code);
4807
Gleb Natapov3298b752009-05-11 13:35:46 +03004808 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03004809 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01004810 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004811 }
4812
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004813 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004814
4815 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4816 return handle_rmode_exception(vcpu, ex_no, error_code);
4817
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004818 switch (ex_no) {
4819 case DB_VECTOR:
4820 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4821 if (!(vcpu->guest_debug &
4822 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
Jan Kiszka8246bf52014-01-04 18:47:17 +01004823 vcpu->arch.dr6 &= ~15;
4824 vcpu->arch.dr6 |= dr6;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004825 kvm_queue_exception(vcpu, DB_VECTOR);
4826 return 1;
4827 }
4828 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4829 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4830 /* fall through */
4831 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004832 /*
4833 * Update instruction length as we may reinject #BP from
4834 * user space while in guest debugging mode. Reading it for
4835 * #DB as well causes no harm, it is not used in that case.
4836 */
4837 vmx->vcpu.arch.event_exit_inst_len =
4838 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004839 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004840 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004841 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4842 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004843 break;
4844 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004845 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4846 kvm_run->ex.exception = ex_no;
4847 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004848 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004849 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004850 return 0;
4851}
4852
Avi Kivity851ba692009-08-24 11:10:17 +03004853static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004854{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004855 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004856 return 1;
4857}
4858
Avi Kivity851ba692009-08-24 11:10:17 +03004859static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004860{
Avi Kivity851ba692009-08-24 11:10:17 +03004861 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08004862 return 0;
4863}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004864
Avi Kivity851ba692009-08-24 11:10:17 +03004865static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866{
He, Qingbfdaab02007-09-12 14:18:28 +08004867 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01004868 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004869 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870
He, Qingbfdaab02007-09-12 14:18:28 +08004871 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02004872 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004873 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004874
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004875 ++vcpu->stat.io_exits;
4876
4877 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01004878 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004879
4880 port = exit_qualification >> 16;
4881 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01004882 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004883
4884 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004885}
4886
Ingo Molnar102d8322007-02-19 14:37:47 +02004887static void
4888vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4889{
4890 /*
4891 * Patch in the VMCALL instruction:
4892 */
4893 hypercall[0] = 0x0f;
4894 hypercall[1] = 0x01;
4895 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004896}
4897
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02004898static bool nested_cr0_valid(struct vmcs12 *vmcs12, unsigned long val)
4899{
4900 unsigned long always_on = VMXON_CR0_ALWAYSON;
4901
4902 if (nested_vmx_secondary_ctls_high &
4903 SECONDARY_EXEC_UNRESTRICTED_GUEST &&
4904 nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST))
4905 always_on &= ~(X86_CR0_PE | X86_CR0_PG);
4906 return (val & always_on) == always_on;
4907}
4908
Guo Chao0fa06072012-06-28 15:16:19 +08004909/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004910static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4911{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004912 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004913 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4914 unsigned long orig_val = val;
4915
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004916 /*
4917 * We get here when L2 changed cr0 in a way that did not change
4918 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004919 * but did change L0 shadowed bits. So we first calculate the
4920 * effective cr0 value that L1 would like to write into the
4921 * hardware. It consists of the L2-owned bits from the new
4922 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004923 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004924 val = (val & ~vmcs12->cr0_guest_host_mask) |
4925 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4926
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02004927 if (!nested_cr0_valid(vmcs12, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004928 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004929
4930 if (kvm_set_cr0(vcpu, val))
4931 return 1;
4932 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004933 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004934 } else {
4935 if (to_vmx(vcpu)->nested.vmxon &&
4936 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
4937 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004938 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004939 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004940}
4941
4942static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4943{
4944 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004945 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4946 unsigned long orig_val = val;
4947
4948 /* analogously to handle_set_cr0 */
4949 val = (val & ~vmcs12->cr4_guest_host_mask) |
4950 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4951 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004952 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004953 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004954 return 0;
4955 } else
4956 return kvm_set_cr4(vcpu, val);
4957}
4958
4959/* called to set cr0 as approriate for clts instruction exit. */
4960static void handle_clts(struct kvm_vcpu *vcpu)
4961{
4962 if (is_guest_mode(vcpu)) {
4963 /*
4964 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
4965 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
4966 * just pretend it's off (also in arch.cr0 for fpu_activate).
4967 */
4968 vmcs_writel(CR0_READ_SHADOW,
4969 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
4970 vcpu->arch.cr0 &= ~X86_CR0_TS;
4971 } else
4972 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
4973}
4974
Avi Kivity851ba692009-08-24 11:10:17 +03004975static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004976{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004977 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004978 int cr;
4979 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004980 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004981
He, Qingbfdaab02007-09-12 14:18:28 +08004982 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004983 cr = exit_qualification & 15;
4984 reg = (exit_qualification >> 8) & 15;
4985 switch ((exit_qualification >> 4) & 3) {
4986 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004987 val = kvm_register_read(vcpu, reg);
4988 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004989 switch (cr) {
4990 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004991 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004992 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004993 return 1;
4994 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03004995 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004996 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004997 return 1;
4998 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004999 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005000 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005001 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005002 case 8: {
5003 u8 cr8_prev = kvm_get_cr8(vcpu);
5004 u8 cr8 = kvm_register_read(vcpu, reg);
Andre Przywaraeea1cff2010-12-21 11:12:00 +01005005 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01005006 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005007 if (irqchip_in_kernel(vcpu->kvm))
5008 return 1;
5009 if (cr8_prev <= cr8)
5010 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03005011 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03005012 return 0;
5013 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02005014 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005015 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03005016 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03005017 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02005018 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03005019 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02005020 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03005021 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005022 case 1: /*mov from cr*/
5023 switch (cr) {
5024 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02005025 val = kvm_read_cr3(vcpu);
5026 kvm_register_write(vcpu, reg, val);
5027 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005028 skip_emulated_instruction(vcpu);
5029 return 1;
5030 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005031 val = kvm_get_cr8(vcpu);
5032 kvm_register_write(vcpu, reg, val);
5033 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005034 skip_emulated_instruction(vcpu);
5035 return 1;
5036 }
5037 break;
5038 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02005039 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02005040 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02005041 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005042
5043 skip_emulated_instruction(vcpu);
5044 return 1;
5045 default:
5046 break;
5047 }
Avi Kivity851ba692009-08-24 11:10:17 +03005048 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03005049 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08005050 (int)(exit_qualification >> 4) & 3, cr);
5051 return 0;
5052}
5053
Avi Kivity851ba692009-08-24 11:10:17 +03005054static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005055{
He, Qingbfdaab02007-09-12 14:18:28 +08005056 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005057 int dr, reg;
5058
Jan Kiszkaf2483412010-01-20 18:20:20 +01005059 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03005060 if (!kvm_require_cpl(vcpu, 0))
5061 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005062 dr = vmcs_readl(GUEST_DR7);
5063 if (dr & DR7_GD) {
5064 /*
5065 * As the vm-exit takes precedence over the debug trap, we
5066 * need to emulate the latter, either for the host or the
5067 * guest debugging itself.
5068 */
5069 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03005070 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
5071 vcpu->run->debug.arch.dr7 = dr;
5072 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005073 vmcs_readl(GUEST_CS_BASE) +
5074 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03005075 vcpu->run->debug.arch.exception = DB_VECTOR;
5076 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005077 return 0;
5078 } else {
5079 vcpu->arch.dr7 &= ~DR7_GD;
5080 vcpu->arch.dr6 |= DR6_BD;
5081 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
5082 kvm_queue_exception(vcpu, DB_VECTOR);
5083 return 1;
5084 }
5085 }
5086
He, Qingbfdaab02007-09-12 14:18:28 +08005087 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005088 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5089 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5090 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03005091 unsigned long val;
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005092
5093 if (kvm_get_dr(vcpu, dr, &val))
5094 return 1;
5095 kvm_register_write(vcpu, reg, val);
Gleb Natapov020df072010-04-13 10:05:23 +03005096 } else
Jan Kiszka4c4d5632013-12-18 19:16:24 +01005097 if (kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]))
5098 return 1;
5099
Avi Kivity6aa8b732006-12-10 02:21:36 -08005100 skip_emulated_instruction(vcpu);
5101 return 1;
5102}
5103
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005104static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5105{
5106 return vcpu->arch.dr6;
5107}
5108
5109static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5110{
5111}
5112
Gleb Natapov020df072010-04-13 10:05:23 +03005113static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5114{
5115 vmcs_writel(GUEST_DR7, val);
5116}
5117
Avi Kivity851ba692009-08-24 11:10:17 +03005118static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005119{
Avi Kivity06465c52007-02-28 20:46:53 +02005120 kvm_emulate_cpuid(vcpu);
5121 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005122}
5123
Avi Kivity851ba692009-08-24 11:10:17 +03005124static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005125{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005126 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08005127 u64 data;
5128
5129 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02005130 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005131 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005132 return 1;
5133 }
5134
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005135 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005136
Avi Kivity6aa8b732006-12-10 02:21:36 -08005137 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005138 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
5139 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005140 skip_emulated_instruction(vcpu);
5141 return 1;
5142}
5143
Avi Kivity851ba692009-08-24 11:10:17 +03005144static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005145{
Will Auld8fe8ab42012-11-29 12:42:12 -08005146 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005147 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5148 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5149 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005150
Will Auld8fe8ab42012-11-29 12:42:12 -08005151 msr.data = data;
5152 msr.index = ecx;
5153 msr.host_initiated = false;
5154 if (vmx_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005155 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005156 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005157 return 1;
5158 }
5159
Avi Kivity59200272010-01-25 19:47:02 +02005160 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005161 skip_emulated_instruction(vcpu);
5162 return 1;
5163}
5164
Avi Kivity851ba692009-08-24 11:10:17 +03005165static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005166{
Avi Kivity3842d132010-07-27 12:30:24 +03005167 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005168 return 1;
5169}
5170
Avi Kivity851ba692009-08-24 11:10:17 +03005171static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005172{
Eddie Dong85f455f2007-07-06 12:20:49 +03005173 u32 cpu_based_vm_exec_control;
5174
5175 /* clear pending irq */
5176 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5177 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5178 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005179
Avi Kivity3842d132010-07-27 12:30:24 +03005180 kvm_make_request(KVM_REQ_EVENT, vcpu);
5181
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005182 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005183
Dor Laorc1150d82007-01-05 16:36:24 -08005184 /*
5185 * If the user space waits to inject interrupts, exit as soon as
5186 * possible
5187 */
Gleb Natapov80618232009-04-21 17:44:56 +03005188 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03005189 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03005190 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03005191 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08005192 return 0;
5193 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005194 return 1;
5195}
5196
Avi Kivity851ba692009-08-24 11:10:17 +03005197static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005198{
5199 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03005200 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005201}
5202
Avi Kivity851ba692009-08-24 11:10:17 +03005203static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005204{
Dor Laor510043d2007-02-19 18:25:43 +02005205 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005206 kvm_emulate_hypercall(vcpu);
5207 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02005208}
5209
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005210static int handle_invd(struct kvm_vcpu *vcpu)
5211{
Andre Przywara51d8b662010-12-21 11:12:02 +01005212 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005213}
5214
Avi Kivity851ba692009-08-24 11:10:17 +03005215static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005216{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005217 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005218
5219 kvm_mmu_invlpg(vcpu, exit_qualification);
5220 skip_emulated_instruction(vcpu);
5221 return 1;
5222}
5223
Avi Kivityfee84b02011-11-10 14:57:25 +02005224static int handle_rdpmc(struct kvm_vcpu *vcpu)
5225{
5226 int err;
5227
5228 err = kvm_rdpmc(vcpu);
5229 kvm_complete_insn_gp(vcpu, err);
5230
5231 return 1;
5232}
5233
Avi Kivity851ba692009-08-24 11:10:17 +03005234static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005235{
5236 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005237 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005238 return 1;
5239}
5240
Dexuan Cui2acf9232010-06-10 11:27:12 +08005241static int handle_xsetbv(struct kvm_vcpu *vcpu)
5242{
5243 u64 new_bv = kvm_read_edx_eax(vcpu);
5244 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5245
5246 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5247 skip_emulated_instruction(vcpu);
5248 return 1;
5249}
5250
Avi Kivity851ba692009-08-24 11:10:17 +03005251static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005252{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005253 if (likely(fasteoi)) {
5254 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5255 int access_type, offset;
5256
5257 access_type = exit_qualification & APIC_ACCESS_TYPE;
5258 offset = exit_qualification & APIC_ACCESS_OFFSET;
5259 /*
5260 * Sane guest uses MOV to write EOI, with written value
5261 * not cared. So make a short-circuit here by avoiding
5262 * heavy instruction emulation.
5263 */
5264 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5265 (offset == APIC_EOI)) {
5266 kvm_lapic_set_eoi(vcpu);
5267 skip_emulated_instruction(vcpu);
5268 return 1;
5269 }
5270 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005271 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005272}
5273
Yang Zhangc7c9c562013-01-25 10:18:51 +08005274static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5275{
5276 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5277 int vector = exit_qualification & 0xff;
5278
5279 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5280 kvm_apic_set_eoi_accelerated(vcpu, vector);
5281 return 1;
5282}
5283
Yang Zhang83d4c282013-01-25 10:18:49 +08005284static int handle_apic_write(struct kvm_vcpu *vcpu)
5285{
5286 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5287 u32 offset = exit_qualification & 0xfff;
5288
5289 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5290 kvm_apic_write_nodecode(vcpu, offset);
5291 return 1;
5292}
5293
Avi Kivity851ba692009-08-24 11:10:17 +03005294static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005295{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005296 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005297 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005298 bool has_error_code = false;
5299 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005300 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005301 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005302
5303 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005304 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005305 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005306
5307 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5308
5309 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005310 if (reason == TASK_SWITCH_GATE && idt_v) {
5311 switch (type) {
5312 case INTR_TYPE_NMI_INTR:
5313 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005314 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005315 break;
5316 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005317 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005318 kvm_clear_interrupt_queue(vcpu);
5319 break;
5320 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005321 if (vmx->idt_vectoring_info &
5322 VECTORING_INFO_DELIVER_CODE_MASK) {
5323 has_error_code = true;
5324 error_code =
5325 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5326 }
5327 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005328 case INTR_TYPE_SOFT_EXCEPTION:
5329 kvm_clear_exception_queue(vcpu);
5330 break;
5331 default:
5332 break;
5333 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005334 }
Izik Eidus37817f22008-03-24 23:14:53 +02005335 tss_selector = exit_qualification;
5336
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005337 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5338 type != INTR_TYPE_EXT_INTR &&
5339 type != INTR_TYPE_NMI_INTR))
5340 skip_emulated_instruction(vcpu);
5341
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005342 if (kvm_task_switch(vcpu, tss_selector,
5343 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
5344 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03005345 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5346 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5347 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005348 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03005349 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005350
5351 /* clear all local breakpoint enable flags */
5352 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
5353
5354 /*
5355 * TODO: What about debug traps on tss switch?
5356 * Are we supposed to inject them and update dr6?
5357 */
5358
5359 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02005360}
5361
Avi Kivity851ba692009-08-24 11:10:17 +03005362static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005363{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005364 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005365 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005366 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005367 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08005368
Sheng Yangf9c617f2009-03-25 10:08:52 +08005369 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08005370
Sheng Yang14394422008-04-28 12:24:45 +08005371 gla_validity = (exit_qualification >> 7) & 0x3;
5372 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
5373 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
5374 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
5375 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08005376 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08005377 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
5378 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03005379 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5380 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03005381 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08005382 }
5383
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005384 /*
5385 * EPT violation happened while executing iret from NMI,
5386 * "blocked by NMI" bit has to be set before next VM entry.
5387 * There are errata that may cause this bit to not be set:
5388 * AAK134, BY25.
5389 */
Gleb Natapovbcd1c292013-09-25 10:58:22 +03005390 if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
5391 cpu_has_virtual_nmis() &&
5392 (exit_qualification & INTR_INFO_UNBLOCK_NMI))
Gleb Natapov0be9c7a2013-09-15 11:07:23 +03005393 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5394
Sheng Yang14394422008-04-28 12:24:45 +08005395 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005396 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005397
5398 /* It is a write fault? */
5399 error_code = exit_qualification & (1U << 1);
Yang Zhang25d92082013-08-06 12:00:32 +03005400 /* It is a fetch fault? */
5401 error_code |= (exit_qualification & (1U << 2)) << 2;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005402 /* ept page table is present? */
5403 error_code |= (exit_qualification >> 3) & 0x1;
5404
Yang Zhang25d92082013-08-06 12:00:32 +03005405 vcpu->arch.exit_qualification = exit_qualification;
5406
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005407 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005408}
5409
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005410static u64 ept_rsvd_mask(u64 spte, int level)
5411{
5412 int i;
5413 u64 mask = 0;
5414
5415 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
5416 mask |= (1ULL << i);
5417
5418 if (level > 2)
5419 /* bits 7:3 reserved */
5420 mask |= 0xf8;
5421 else if (level == 2) {
5422 if (spte & (1ULL << 7))
5423 /* 2MB ref, bits 20:12 reserved */
5424 mask |= 0x1ff000;
5425 else
5426 /* bits 6:3 reserved */
5427 mask |= 0x78;
5428 }
5429
5430 return mask;
5431}
5432
5433static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
5434 int level)
5435{
5436 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
5437
5438 /* 010b (write-only) */
5439 WARN_ON((spte & 0x7) == 0x2);
5440
5441 /* 110b (write/execute) */
5442 WARN_ON((spte & 0x7) == 0x6);
5443
5444 /* 100b (execute-only) and value not supported by logical processor */
5445 if (!cpu_has_vmx_ept_execute_only())
5446 WARN_ON((spte & 0x7) == 0x4);
5447
5448 /* not 000b */
5449 if ((spte & 0x7)) {
5450 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
5451
5452 if (rsvd_bits != 0) {
5453 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
5454 __func__, rsvd_bits);
5455 WARN_ON(1);
5456 }
5457
5458 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
5459 u64 ept_mem_type = (spte & 0x38) >> 3;
5460
5461 if (ept_mem_type == 2 || ept_mem_type == 3 ||
5462 ept_mem_type == 7) {
5463 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
5464 __func__, ept_mem_type);
5465 WARN_ON(1);
5466 }
5467 }
5468 }
5469}
5470
Avi Kivity851ba692009-08-24 11:10:17 +03005471static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005472{
5473 u64 sptes[4];
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005474 int nr_sptes, i, ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005475 gpa_t gpa;
5476
5477 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
5478
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005479 ret = handle_mmio_page_fault_common(vcpu, gpa, true);
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005480 if (likely(ret == RET_MMIO_PF_EMULATE))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005481 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
5482 EMULATE_DONE;
Xiao Guangrongf8f55942013-06-07 16:51:26 +08005483
5484 if (unlikely(ret == RET_MMIO_PF_INVALID))
5485 return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0);
5486
Xiao Guangrongb37fbea2013-06-07 16:51:25 +08005487 if (unlikely(ret == RET_MMIO_PF_RETRY))
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005488 return 1;
5489
5490 /* It is the real ept misconfig */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005491 printk(KERN_ERR "EPT: Misconfiguration.\n");
5492 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
5493
5494 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
5495
5496 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
5497 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
5498
Avi Kivity851ba692009-08-24 11:10:17 +03005499 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5500 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005501
5502 return 0;
5503}
5504
Avi Kivity851ba692009-08-24 11:10:17 +03005505static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005506{
5507 u32 cpu_based_vm_exec_control;
5508
5509 /* clear pending NMI */
5510 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5511 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
5512 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5513 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005514 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005515
5516 return 1;
5517}
5518
Mohammed Gamal80ced182009-09-01 12:48:18 +02005519static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005520{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005521 struct vcpu_vmx *vmx = to_vmx(vcpu);
5522 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005523 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02005524 u32 cpu_exec_ctrl;
5525 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005526 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005527
5528 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5529 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005530
Avi Kivityb8405c12012-06-07 17:08:48 +03005531 while (!guest_state_valid(vcpu) && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03005532 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005533 return handle_interrupt_window(&vmx->vcpu);
5534
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005535 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
5536 return 1;
5537
Gleb Natapov991eebf2013-04-11 12:10:51 +03005538 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005539
Paolo Bonziniac0a48c2013-06-25 18:24:41 +02005540 if (err == EMULATE_USER_EXIT) {
Paolo Bonzini94452b92013-08-27 15:41:42 +02005541 ++vcpu->stat.mmio_exits;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005542 ret = 0;
5543 goto out;
5544 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005545
Avi Kivityde5f70e2012-06-12 20:22:28 +03005546 if (err != EMULATE_DONE) {
5547 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5548 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5549 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005550 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03005551 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005552
Gleb Natapov8d76c492013-05-08 18:38:44 +03005553 if (vcpu->arch.halt_request) {
5554 vcpu->arch.halt_request = 0;
5555 ret = kvm_emulate_halt(vcpu);
5556 goto out;
5557 }
5558
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005559 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02005560 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005561 if (need_resched())
5562 schedule();
5563 }
5564
Gleb Natapov14168782013-01-21 15:36:49 +02005565 vmx->emulation_required = emulation_required(vcpu);
Mohammed Gamal80ced182009-09-01 12:48:18 +02005566out:
5567 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005568}
5569
Avi Kivity6aa8b732006-12-10 02:21:36 -08005570/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005571 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5572 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5573 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005574static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005575{
5576 skip_emulated_instruction(vcpu);
5577 kvm_vcpu_on_spin(vcpu);
5578
5579 return 1;
5580}
5581
Sheng Yang59708672009-12-15 13:29:54 +08005582static int handle_invalid_op(struct kvm_vcpu *vcpu)
5583{
5584 kvm_queue_exception(vcpu, UD_VECTOR);
5585 return 1;
5586}
5587
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005588/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005589 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
5590 * We could reuse a single VMCS for all the L2 guests, but we also want the
5591 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
5592 * allows keeping them loaded on the processor, and in the future will allow
5593 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
5594 * every entry if they never change.
5595 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
5596 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
5597 *
5598 * The following functions allocate and free a vmcs02 in this pool.
5599 */
5600
5601/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
5602static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
5603{
5604 struct vmcs02_list *item;
5605 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5606 if (item->vmptr == vmx->nested.current_vmptr) {
5607 list_move(&item->list, &vmx->nested.vmcs02_pool);
5608 return &item->vmcs02;
5609 }
5610
5611 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
5612 /* Recycle the least recently used VMCS. */
5613 item = list_entry(vmx->nested.vmcs02_pool.prev,
5614 struct vmcs02_list, list);
5615 item->vmptr = vmx->nested.current_vmptr;
5616 list_move(&item->list, &vmx->nested.vmcs02_pool);
5617 return &item->vmcs02;
5618 }
5619
5620 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02005621 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005622 if (!item)
5623 return NULL;
5624 item->vmcs02.vmcs = alloc_vmcs();
5625 if (!item->vmcs02.vmcs) {
5626 kfree(item);
5627 return NULL;
5628 }
5629 loaded_vmcs_init(&item->vmcs02);
5630 item->vmptr = vmx->nested.current_vmptr;
5631 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
5632 vmx->nested.vmcs02_num++;
5633 return &item->vmcs02;
5634}
5635
5636/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
5637static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
5638{
5639 struct vmcs02_list *item;
5640 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5641 if (item->vmptr == vmptr) {
5642 free_loaded_vmcs(&item->vmcs02);
5643 list_del(&item->list);
5644 kfree(item);
5645 vmx->nested.vmcs02_num--;
5646 return;
5647 }
5648}
5649
5650/*
5651 * Free all VMCSs saved for this vcpu, except the one pointed by
5652 * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
5653 * currently used, if running L2), and vmcs01 when running L2.
5654 */
5655static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
5656{
5657 struct vmcs02_list *item, *n;
5658 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
5659 if (vmx->loaded_vmcs != &item->vmcs02)
5660 free_loaded_vmcs(&item->vmcs02);
5661 list_del(&item->list);
5662 kfree(item);
5663 }
5664 vmx->nested.vmcs02_num = 0;
5665
5666 if (vmx->loaded_vmcs != &vmx->vmcs01)
5667 free_loaded_vmcs(&vmx->vmcs01);
5668}
5669
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08005670/*
5671 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
5672 * set the success or error code of an emulated VMX instruction, as specified
5673 * by Vol 2B, VMX Instruction Reference, "Conventions".
5674 */
5675static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
5676{
5677 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
5678 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5679 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
5680}
5681
5682static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
5683{
5684 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5685 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
5686 X86_EFLAGS_SF | X86_EFLAGS_OF))
5687 | X86_EFLAGS_CF);
5688}
5689
Abel Gordon145c28d2013-04-18 14:36:55 +03005690static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
Arthur Chunqi Li0658fba2013-07-04 15:03:32 +08005691 u32 vm_instruction_error)
5692{
5693 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
5694 /*
5695 * failValid writes the error number to the current VMCS, which
5696 * can't be done there isn't a current VMCS.
5697 */
5698 nested_vmx_failInvalid(vcpu);
5699 return;
5700 }
5701 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5702 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5703 X86_EFLAGS_SF | X86_EFLAGS_OF))
5704 | X86_EFLAGS_ZF);
5705 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
5706 /*
5707 * We don't need to force a shadow sync because
5708 * VM_INSTRUCTION_ERROR is not shadowed
5709 */
5710}
Abel Gordon145c28d2013-04-18 14:36:55 +03005711
Jan Kiszkaf41245002014-03-07 20:03:13 +01005712static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer)
5713{
5714 struct vcpu_vmx *vmx =
5715 container_of(timer, struct vcpu_vmx, nested.preemption_timer);
5716
5717 vmx->nested.preemption_timer_expired = true;
5718 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
5719 kvm_vcpu_kick(&vmx->vcpu);
5720
5721 return HRTIMER_NORESTART;
5722}
5723
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005724/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005725 * Emulate the VMXON instruction.
5726 * Currently, we just remember that VMX is active, and do not save or even
5727 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
5728 * do not currently need to store anything in that guest-allocated memory
5729 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
5730 * argument is different from the VMXON pointer (which the spec says they do).
5731 */
5732static int handle_vmon(struct kvm_vcpu *vcpu)
5733{
5734 struct kvm_segment cs;
5735 struct vcpu_vmx *vmx = to_vmx(vcpu);
Abel Gordon8de48832013-04-18 14:37:25 +03005736 struct vmcs *shadow_vmcs;
Nadav Har'Elb3897a42013-07-08 19:12:35 +08005737 const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED
5738 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005739
5740 /* The Intel VMX Instruction Reference lists a bunch of bits that
5741 * are prerequisite to running VMXON, most notably cr4.VMXE must be
5742 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
5743 * Otherwise, we should fail with #UD. We test these now:
5744 */
5745 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
5746 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
5747 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5748 kvm_queue_exception(vcpu, UD_VECTOR);
5749 return 1;
5750 }
5751
5752 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5753 if (is_long_mode(vcpu) && !cs.l) {
5754 kvm_queue_exception(vcpu, UD_VECTOR);
5755 return 1;
5756 }
5757
5758 if (vmx_get_cpl(vcpu)) {
5759 kvm_inject_gp(vcpu, 0);
5760 return 1;
5761 }
Abel Gordon145c28d2013-04-18 14:36:55 +03005762 if (vmx->nested.vmxon) {
5763 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
5764 skip_emulated_instruction(vcpu);
5765 return 1;
5766 }
Nadav Har'Elb3897a42013-07-08 19:12:35 +08005767
5768 if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES)
5769 != VMXON_NEEDED_FEATURES) {
5770 kvm_inject_gp(vcpu, 0);
5771 return 1;
5772 }
5773
Abel Gordon8de48832013-04-18 14:37:25 +03005774 if (enable_shadow_vmcs) {
5775 shadow_vmcs = alloc_vmcs();
5776 if (!shadow_vmcs)
5777 return -ENOMEM;
5778 /* mark vmcs as shadow */
5779 shadow_vmcs->revision_id |= (1u << 31);
5780 /* init shadow vmcs */
5781 vmcs_clear(shadow_vmcs);
5782 vmx->nested.current_shadow_vmcs = shadow_vmcs;
5783 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005784
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005785 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
5786 vmx->nested.vmcs02_num = 0;
5787
Jan Kiszkaf41245002014-03-07 20:03:13 +01005788 hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC,
5789 HRTIMER_MODE_REL);
5790 vmx->nested.preemption_timer.function = vmx_preemption_timer_fn;
5791
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005792 vmx->nested.vmxon = true;
5793
5794 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08005795 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005796 return 1;
5797}
5798
5799/*
5800 * Intel's VMX Instruction Reference specifies a common set of prerequisites
5801 * for running VMX instructions (except VMXON, whose prerequisites are
5802 * slightly different). It also specifies what exception to inject otherwise.
5803 */
5804static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
5805{
5806 struct kvm_segment cs;
5807 struct vcpu_vmx *vmx = to_vmx(vcpu);
5808
5809 if (!vmx->nested.vmxon) {
5810 kvm_queue_exception(vcpu, UD_VECTOR);
5811 return 0;
5812 }
5813
5814 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5815 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
5816 (is_long_mode(vcpu) && !cs.l)) {
5817 kvm_queue_exception(vcpu, UD_VECTOR);
5818 return 0;
5819 }
5820
5821 if (vmx_get_cpl(vcpu)) {
5822 kvm_inject_gp(vcpu, 0);
5823 return 0;
5824 }
5825
5826 return 1;
5827}
5828
Abel Gordone7953d72013-04-18 14:37:55 +03005829static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)
5830{
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03005831 u32 exec_control;
Abel Gordon012f83c2013-04-18 14:39:25 +03005832 if (enable_shadow_vmcs) {
5833 if (vmx->nested.current_vmcs12 != NULL) {
5834 /* copy to memory all shadowed fields in case
5835 they were modified */
5836 copy_shadow_to_vmcs12(vmx);
5837 vmx->nested.sync_shadow_vmcs = false;
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03005838 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
5839 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
5840 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
5841 vmcs_write64(VMCS_LINK_POINTER, -1ull);
Abel Gordon012f83c2013-04-18 14:39:25 +03005842 }
5843 }
Abel Gordone7953d72013-04-18 14:37:55 +03005844 kunmap(vmx->nested.current_vmcs12_page);
5845 nested_release_page(vmx->nested.current_vmcs12_page);
5846}
5847
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005848/*
5849 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
5850 * just stops using VMX.
5851 */
5852static void free_nested(struct vcpu_vmx *vmx)
5853{
5854 if (!vmx->nested.vmxon)
5855 return;
5856 vmx->nested.vmxon = false;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03005857 if (vmx->nested.current_vmptr != -1ull) {
Abel Gordone7953d72013-04-18 14:37:55 +03005858 nested_release_vmcs12(vmx);
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03005859 vmx->nested.current_vmptr = -1ull;
5860 vmx->nested.current_vmcs12 = NULL;
5861 }
Abel Gordone7953d72013-04-18 14:37:55 +03005862 if (enable_shadow_vmcs)
5863 free_vmcs(vmx->nested.current_shadow_vmcs);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005864 /* Unpin physical memory we referred to in current vmcs02 */
5865 if (vmx->nested.apic_access_page) {
5866 nested_release_page(vmx->nested.apic_access_page);
5867 vmx->nested.apic_access_page = 0;
5868 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005869
5870 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005871}
5872
5873/* Emulate the VMXOFF instruction */
5874static int handle_vmoff(struct kvm_vcpu *vcpu)
5875{
5876 if (!nested_vmx_check_permission(vcpu))
5877 return 1;
5878 free_nested(to_vmx(vcpu));
5879 skip_emulated_instruction(vcpu);
Arthur Chunqi Lia25eb112013-07-04 15:03:33 +08005880 nested_vmx_succeed(vcpu);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005881 return 1;
5882}
5883
5884/*
Nadav Har'El064aea72011-05-25 23:04:56 +03005885 * Decode the memory-address operand of a vmx instruction, as recorded on an
5886 * exit caused by such an instruction (run by a guest hypervisor).
5887 * On success, returns 0. When the operand is invalid, returns 1 and throws
5888 * #UD or #GP.
5889 */
5890static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
5891 unsigned long exit_qualification,
5892 u32 vmx_instruction_info, gva_t *ret)
5893{
5894 /*
5895 * According to Vol. 3B, "Information for VM Exits Due to Instruction
5896 * Execution", on an exit, vmx_instruction_info holds most of the
5897 * addressing components of the operand. Only the displacement part
5898 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
5899 * For how an actual address is calculated from all these components,
5900 * refer to Vol. 1, "Operand Addressing".
5901 */
5902 int scaling = vmx_instruction_info & 3;
5903 int addr_size = (vmx_instruction_info >> 7) & 7;
5904 bool is_reg = vmx_instruction_info & (1u << 10);
5905 int seg_reg = (vmx_instruction_info >> 15) & 7;
5906 int index_reg = (vmx_instruction_info >> 18) & 0xf;
5907 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
5908 int base_reg = (vmx_instruction_info >> 23) & 0xf;
5909 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
5910
5911 if (is_reg) {
5912 kvm_queue_exception(vcpu, UD_VECTOR);
5913 return 1;
5914 }
5915
5916 /* Addr = segment_base + offset */
5917 /* offset = base + [index * scale] + displacement */
5918 *ret = vmx_get_segment_base(vcpu, seg_reg);
5919 if (base_is_valid)
5920 *ret += kvm_register_read(vcpu, base_reg);
5921 if (index_is_valid)
5922 *ret += kvm_register_read(vcpu, index_reg)<<scaling;
5923 *ret += exit_qualification; /* holds the displacement */
5924
5925 if (addr_size == 1) /* 32 bit */
5926 *ret &= 0xffffffff;
5927
5928 /*
5929 * TODO: throw #GP (and return 1) in various cases that the VM*
5930 * instructions require it - e.g., offset beyond segment limit,
5931 * unusable or unreadable/unwritable segment, non-canonical 64-bit
5932 * address, and so on. Currently these are not checked.
5933 */
5934 return 0;
5935}
5936
Nadav Har'El27d6c862011-05-25 23:06:59 +03005937/* Emulate the VMCLEAR instruction */
5938static int handle_vmclear(struct kvm_vcpu *vcpu)
5939{
5940 struct vcpu_vmx *vmx = to_vmx(vcpu);
5941 gva_t gva;
5942 gpa_t vmptr;
5943 struct vmcs12 *vmcs12;
5944 struct page *page;
5945 struct x86_exception e;
5946
5947 if (!nested_vmx_check_permission(vcpu))
5948 return 1;
5949
5950 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5951 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5952 return 1;
5953
5954 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5955 sizeof(vmptr), &e)) {
5956 kvm_inject_page_fault(vcpu, &e);
5957 return 1;
5958 }
5959
5960 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
5961 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
5962 skip_emulated_instruction(vcpu);
5963 return 1;
5964 }
5965
5966 if (vmptr == vmx->nested.current_vmptr) {
Abel Gordone7953d72013-04-18 14:37:55 +03005967 nested_release_vmcs12(vmx);
Nadav Har'El27d6c862011-05-25 23:06:59 +03005968 vmx->nested.current_vmptr = -1ull;
5969 vmx->nested.current_vmcs12 = NULL;
5970 }
5971
5972 page = nested_get_page(vcpu, vmptr);
5973 if (page == NULL) {
5974 /*
5975 * For accurate processor emulation, VMCLEAR beyond available
5976 * physical memory should do nothing at all. However, it is
5977 * possible that a nested vmx bug, not a guest hypervisor bug,
5978 * resulted in this case, so let's shut down before doing any
5979 * more damage:
5980 */
5981 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5982 return 1;
5983 }
5984 vmcs12 = kmap(page);
5985 vmcs12->launch_state = 0;
5986 kunmap(page);
5987 nested_release_page(page);
5988
5989 nested_free_vmcs02(vmx, vmptr);
5990
5991 skip_emulated_instruction(vcpu);
5992 nested_vmx_succeed(vcpu);
5993 return 1;
5994}
5995
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03005996static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
5997
5998/* Emulate the VMLAUNCH instruction */
5999static int handle_vmlaunch(struct kvm_vcpu *vcpu)
6000{
6001 return nested_vmx_run(vcpu, true);
6002}
6003
6004/* Emulate the VMRESUME instruction */
6005static int handle_vmresume(struct kvm_vcpu *vcpu)
6006{
6007
6008 return nested_vmx_run(vcpu, false);
6009}
6010
Nadav Har'El49f705c2011-05-25 23:08:30 +03006011enum vmcs_field_type {
6012 VMCS_FIELD_TYPE_U16 = 0,
6013 VMCS_FIELD_TYPE_U64 = 1,
6014 VMCS_FIELD_TYPE_U32 = 2,
6015 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
6016};
6017
6018static inline int vmcs_field_type(unsigned long field)
6019{
6020 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
6021 return VMCS_FIELD_TYPE_U32;
6022 return (field >> 13) & 0x3 ;
6023}
6024
6025static inline int vmcs_field_readonly(unsigned long field)
6026{
6027 return (((field >> 10) & 0x3) == 1);
6028}
6029
6030/*
6031 * Read a vmcs12 field. Since these can have varying lengths and we return
6032 * one type, we chose the biggest type (u64) and zero-extend the return value
6033 * to that size. Note that the caller, handle_vmread, might need to use only
6034 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
6035 * 64-bit fields are to be returned).
6036 */
6037static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
6038 unsigned long field, u64 *ret)
6039{
6040 short offset = vmcs_field_to_offset(field);
6041 char *p;
6042
6043 if (offset < 0)
6044 return 0;
6045
6046 p = ((char *)(get_vmcs12(vcpu))) + offset;
6047
6048 switch (vmcs_field_type(field)) {
6049 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6050 *ret = *((natural_width *)p);
6051 return 1;
6052 case VMCS_FIELD_TYPE_U16:
6053 *ret = *((u16 *)p);
6054 return 1;
6055 case VMCS_FIELD_TYPE_U32:
6056 *ret = *((u32 *)p);
6057 return 1;
6058 case VMCS_FIELD_TYPE_U64:
6059 *ret = *((u64 *)p);
6060 return 1;
6061 default:
6062 return 0; /* can never happen. */
6063 }
6064}
6065
Abel Gordon20b97fe2013-04-18 14:36:25 +03006066
6067static inline bool vmcs12_write_any(struct kvm_vcpu *vcpu,
6068 unsigned long field, u64 field_value){
6069 short offset = vmcs_field_to_offset(field);
6070 char *p = ((char *) get_vmcs12(vcpu)) + offset;
6071 if (offset < 0)
6072 return false;
6073
6074 switch (vmcs_field_type(field)) {
6075 case VMCS_FIELD_TYPE_U16:
6076 *(u16 *)p = field_value;
6077 return true;
6078 case VMCS_FIELD_TYPE_U32:
6079 *(u32 *)p = field_value;
6080 return true;
6081 case VMCS_FIELD_TYPE_U64:
6082 *(u64 *)p = field_value;
6083 return true;
6084 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6085 *(natural_width *)p = field_value;
6086 return true;
6087 default:
6088 return false; /* can never happen. */
6089 }
6090
6091}
6092
Abel Gordon16f5b902013-04-18 14:38:25 +03006093static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx)
6094{
6095 int i;
6096 unsigned long field;
6097 u64 field_value;
6098 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
Mathias Krausec2bae892013-06-26 20:36:21 +02006099 const unsigned long *fields = shadow_read_write_fields;
6100 const int num_fields = max_shadow_read_write_fields;
Abel Gordon16f5b902013-04-18 14:38:25 +03006101
6102 vmcs_load(shadow_vmcs);
6103
6104 for (i = 0; i < num_fields; i++) {
6105 field = fields[i];
6106 switch (vmcs_field_type(field)) {
6107 case VMCS_FIELD_TYPE_U16:
6108 field_value = vmcs_read16(field);
6109 break;
6110 case VMCS_FIELD_TYPE_U32:
6111 field_value = vmcs_read32(field);
6112 break;
6113 case VMCS_FIELD_TYPE_U64:
6114 field_value = vmcs_read64(field);
6115 break;
6116 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6117 field_value = vmcs_readl(field);
6118 break;
6119 }
6120 vmcs12_write_any(&vmx->vcpu, field, field_value);
6121 }
6122
6123 vmcs_clear(shadow_vmcs);
6124 vmcs_load(vmx->loaded_vmcs->vmcs);
6125}
6126
Abel Gordonc3114422013-04-18 14:38:55 +03006127static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx)
6128{
Mathias Krausec2bae892013-06-26 20:36:21 +02006129 const unsigned long *fields[] = {
6130 shadow_read_write_fields,
6131 shadow_read_only_fields
Abel Gordonc3114422013-04-18 14:38:55 +03006132 };
Mathias Krausec2bae892013-06-26 20:36:21 +02006133 const int max_fields[] = {
Abel Gordonc3114422013-04-18 14:38:55 +03006134 max_shadow_read_write_fields,
6135 max_shadow_read_only_fields
6136 };
6137 int i, q;
6138 unsigned long field;
6139 u64 field_value = 0;
6140 struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs;
6141
6142 vmcs_load(shadow_vmcs);
6143
Mathias Krausec2bae892013-06-26 20:36:21 +02006144 for (q = 0; q < ARRAY_SIZE(fields); q++) {
Abel Gordonc3114422013-04-18 14:38:55 +03006145 for (i = 0; i < max_fields[q]; i++) {
6146 field = fields[q][i];
6147 vmcs12_read_any(&vmx->vcpu, field, &field_value);
6148
6149 switch (vmcs_field_type(field)) {
6150 case VMCS_FIELD_TYPE_U16:
6151 vmcs_write16(field, (u16)field_value);
6152 break;
6153 case VMCS_FIELD_TYPE_U32:
6154 vmcs_write32(field, (u32)field_value);
6155 break;
6156 case VMCS_FIELD_TYPE_U64:
6157 vmcs_write64(field, (u64)field_value);
6158 break;
6159 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
6160 vmcs_writel(field, (long)field_value);
6161 break;
6162 }
6163 }
6164 }
6165
6166 vmcs_clear(shadow_vmcs);
6167 vmcs_load(vmx->loaded_vmcs->vmcs);
6168}
6169
Nadav Har'El49f705c2011-05-25 23:08:30 +03006170/*
6171 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
6172 * used before) all generate the same failure when it is missing.
6173 */
6174static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
6175{
6176 struct vcpu_vmx *vmx = to_vmx(vcpu);
6177 if (vmx->nested.current_vmptr == -1ull) {
6178 nested_vmx_failInvalid(vcpu);
6179 skip_emulated_instruction(vcpu);
6180 return 0;
6181 }
6182 return 1;
6183}
6184
6185static int handle_vmread(struct kvm_vcpu *vcpu)
6186{
6187 unsigned long field;
6188 u64 field_value;
6189 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6190 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6191 gva_t gva = 0;
6192
6193 if (!nested_vmx_check_permission(vcpu) ||
6194 !nested_vmx_check_vmcs12(vcpu))
6195 return 1;
6196
6197 /* Decode instruction info and find the field to read */
6198 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
6199 /* Read the field, zero-extended to a u64 field_value */
6200 if (!vmcs12_read_any(vcpu, field, &field_value)) {
6201 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
6202 skip_emulated_instruction(vcpu);
6203 return 1;
6204 }
6205 /*
6206 * Now copy part of this value to register or memory, as requested.
6207 * Note that the number of bits actually copied is 32 or 64 depending
6208 * on the guest's mode (32 or 64 bit), not on the given field's length.
6209 */
6210 if (vmx_instruction_info & (1u << 10)) {
6211 kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
6212 field_value);
6213 } else {
6214 if (get_vmx_mem_address(vcpu, exit_qualification,
6215 vmx_instruction_info, &gva))
6216 return 1;
6217 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
6218 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
6219 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
6220 }
6221
6222 nested_vmx_succeed(vcpu);
6223 skip_emulated_instruction(vcpu);
6224 return 1;
6225}
6226
6227
6228static int handle_vmwrite(struct kvm_vcpu *vcpu)
6229{
6230 unsigned long field;
6231 gva_t gva;
6232 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6233 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03006234 /* The value to write might be 32 or 64 bits, depending on L1's long
6235 * mode, and eventually we need to write that into a field of several
6236 * possible lengths. The code below first zero-extends the value to 64
6237 * bit (field_value), and then copies only the approriate number of
6238 * bits into the vmcs12 field.
6239 */
6240 u64 field_value = 0;
6241 struct x86_exception e;
6242
6243 if (!nested_vmx_check_permission(vcpu) ||
6244 !nested_vmx_check_vmcs12(vcpu))
6245 return 1;
6246
6247 if (vmx_instruction_info & (1u << 10))
6248 field_value = kvm_register_read(vcpu,
6249 (((vmx_instruction_info) >> 3) & 0xf));
6250 else {
6251 if (get_vmx_mem_address(vcpu, exit_qualification,
6252 vmx_instruction_info, &gva))
6253 return 1;
6254 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
6255 &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) {
6256 kvm_inject_page_fault(vcpu, &e);
6257 return 1;
6258 }
6259 }
6260
6261
6262 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
6263 if (vmcs_field_readonly(field)) {
6264 nested_vmx_failValid(vcpu,
6265 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
6266 skip_emulated_instruction(vcpu);
6267 return 1;
6268 }
6269
Abel Gordon20b97fe2013-04-18 14:36:25 +03006270 if (!vmcs12_write_any(vcpu, field, field_value)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03006271 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
6272 skip_emulated_instruction(vcpu);
6273 return 1;
6274 }
6275
6276 nested_vmx_succeed(vcpu);
6277 skip_emulated_instruction(vcpu);
6278 return 1;
6279}
6280
Nadav Har'El63846662011-05-25 23:07:29 +03006281/* Emulate the VMPTRLD instruction */
6282static int handle_vmptrld(struct kvm_vcpu *vcpu)
6283{
6284 struct vcpu_vmx *vmx = to_vmx(vcpu);
6285 gva_t gva;
6286 gpa_t vmptr;
6287 struct x86_exception e;
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006288 u32 exec_control;
Nadav Har'El63846662011-05-25 23:07:29 +03006289
6290 if (!nested_vmx_check_permission(vcpu))
6291 return 1;
6292
6293 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
6294 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
6295 return 1;
6296
6297 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6298 sizeof(vmptr), &e)) {
6299 kvm_inject_page_fault(vcpu, &e);
6300 return 1;
6301 }
6302
6303 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
6304 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
6305 skip_emulated_instruction(vcpu);
6306 return 1;
6307 }
6308
6309 if (vmx->nested.current_vmptr != vmptr) {
6310 struct vmcs12 *new_vmcs12;
6311 struct page *page;
6312 page = nested_get_page(vcpu, vmptr);
6313 if (page == NULL) {
6314 nested_vmx_failInvalid(vcpu);
6315 skip_emulated_instruction(vcpu);
6316 return 1;
6317 }
6318 new_vmcs12 = kmap(page);
6319 if (new_vmcs12->revision_id != VMCS12_REVISION) {
6320 kunmap(page);
6321 nested_release_page_clean(page);
6322 nested_vmx_failValid(vcpu,
6323 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
6324 skip_emulated_instruction(vcpu);
6325 return 1;
6326 }
Abel Gordone7953d72013-04-18 14:37:55 +03006327 if (vmx->nested.current_vmptr != -1ull)
6328 nested_release_vmcs12(vmx);
Nadav Har'El63846662011-05-25 23:07:29 +03006329
6330 vmx->nested.current_vmptr = vmptr;
6331 vmx->nested.current_vmcs12 = new_vmcs12;
6332 vmx->nested.current_vmcs12_page = page;
Abel Gordon012f83c2013-04-18 14:39:25 +03006333 if (enable_shadow_vmcs) {
Abel Gordon8a1b9dd2013-04-18 14:39:55 +03006334 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6335 exec_control |= SECONDARY_EXEC_SHADOW_VMCS;
6336 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
6337 vmcs_write64(VMCS_LINK_POINTER,
6338 __pa(vmx->nested.current_shadow_vmcs));
Abel Gordon012f83c2013-04-18 14:39:25 +03006339 vmx->nested.sync_shadow_vmcs = true;
6340 }
Nadav Har'El63846662011-05-25 23:07:29 +03006341 }
6342
6343 nested_vmx_succeed(vcpu);
6344 skip_emulated_instruction(vcpu);
6345 return 1;
6346}
6347
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006348/* Emulate the VMPTRST instruction */
6349static int handle_vmptrst(struct kvm_vcpu *vcpu)
6350{
6351 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6352 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6353 gva_t vmcs_gva;
6354 struct x86_exception e;
6355
6356 if (!nested_vmx_check_permission(vcpu))
6357 return 1;
6358
6359 if (get_vmx_mem_address(vcpu, exit_qualification,
6360 vmx_instruction_info, &vmcs_gva))
6361 return 1;
6362 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
6363 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
6364 (void *)&to_vmx(vcpu)->nested.current_vmptr,
6365 sizeof(u64), &e)) {
6366 kvm_inject_page_fault(vcpu, &e);
6367 return 1;
6368 }
6369 nested_vmx_succeed(vcpu);
6370 skip_emulated_instruction(vcpu);
6371 return 1;
6372}
6373
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006374/* Emulate the INVEPT instruction */
6375static int handle_invept(struct kvm_vcpu *vcpu)
6376{
6377 u32 vmx_instruction_info, types;
6378 unsigned long type;
6379 gva_t gva;
6380 struct x86_exception e;
6381 struct {
6382 u64 eptp, gpa;
6383 } operand;
6384 u64 eptp_mask = ((1ull << 51) - 1) & PAGE_MASK;
6385
6386 if (!(nested_vmx_secondary_ctls_high & SECONDARY_EXEC_ENABLE_EPT) ||
6387 !(nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) {
6388 kvm_queue_exception(vcpu, UD_VECTOR);
6389 return 1;
6390 }
6391
6392 if (!nested_vmx_check_permission(vcpu))
6393 return 1;
6394
6395 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
6396 kvm_queue_exception(vcpu, UD_VECTOR);
6397 return 1;
6398 }
6399
6400 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6401 type = kvm_register_read(vcpu, (vmx_instruction_info >> 28) & 0xf);
6402
6403 types = (nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6;
6404
6405 if (!(types & (1UL << type))) {
6406 nested_vmx_failValid(vcpu,
6407 VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID);
6408 return 1;
6409 }
6410
6411 /* According to the Intel VMX instruction reference, the memory
6412 * operand is read even if it isn't needed (e.g., for type==global)
6413 */
6414 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
6415 vmx_instruction_info, &gva))
6416 return 1;
6417 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand,
6418 sizeof(operand), &e)) {
6419 kvm_inject_page_fault(vcpu, &e);
6420 return 1;
6421 }
6422
6423 switch (type) {
6424 case VMX_EPT_EXTENT_CONTEXT:
6425 if ((operand.eptp & eptp_mask) !=
6426 (nested_ept_get_cr3(vcpu) & eptp_mask))
6427 break;
6428 case VMX_EPT_EXTENT_GLOBAL:
6429 kvm_mmu_sync_roots(vcpu);
6430 kvm_mmu_flush_tlb(vcpu);
6431 nested_vmx_succeed(vcpu);
6432 break;
6433 default:
6434 BUG_ON(1);
6435 break;
6436 }
6437
6438 skip_emulated_instruction(vcpu);
6439 return 1;
6440}
6441
Nadav Har'El0140cae2011-05-25 23:06:28 +03006442/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006443 * The exit handlers return 1 if the exit was handled fully and guest execution
6444 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
6445 * to be done to userspace and return 0.
6446 */
Mathias Krause772e0312012-08-30 01:30:19 +02006447static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006448 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
6449 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08006450 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08006451 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006452 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006453 [EXIT_REASON_CR_ACCESS] = handle_cr,
6454 [EXIT_REASON_DR_ACCESS] = handle_dr,
6455 [EXIT_REASON_CPUID] = handle_cpuid,
6456 [EXIT_REASON_MSR_READ] = handle_rdmsr,
6457 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
6458 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
6459 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006460 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03006461 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02006462 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02006463 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03006464 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006465 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03006466 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006467 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03006468 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006469 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03006470 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006471 [EXIT_REASON_VMOFF] = handle_vmoff,
6472 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08006473 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
6474 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08006475 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08006476 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02006477 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08006478 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02006479 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08006480 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006481 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
6482 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006483 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08006484 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
6485 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006486 [EXIT_REASON_INVEPT] = handle_invept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006487};
6488
6489static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04006490 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006491
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006492static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
6493 struct vmcs12 *vmcs12)
6494{
6495 unsigned long exit_qualification;
6496 gpa_t bitmap, last_bitmap;
6497 unsigned int port;
6498 int size;
6499 u8 b;
6500
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006501 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
Zhihui Zhang2f0a6392013-12-30 15:56:29 -05006502 return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING);
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006503
6504 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6505
6506 port = exit_qualification >> 16;
6507 size = (exit_qualification & 7) + 1;
6508
6509 last_bitmap = (gpa_t)-1;
6510 b = -1;
6511
6512 while (size > 0) {
6513 if (port < 0x8000)
6514 bitmap = vmcs12->io_bitmap_a;
6515 else if (port < 0x10000)
6516 bitmap = vmcs12->io_bitmap_b;
6517 else
6518 return 1;
6519 bitmap += (port & 0x7fff) / 8;
6520
6521 if (last_bitmap != bitmap)
6522 if (kvm_read_guest(vcpu->kvm, bitmap, &b, 1))
6523 return 1;
6524 if (b & (1 << (port & 7)))
6525 return 1;
6526
6527 port++;
6528 size--;
6529 last_bitmap = bitmap;
6530 }
6531
6532 return 0;
6533}
6534
Nadav Har'El644d7112011-05-25 23:12:35 +03006535/*
6536 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
6537 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
6538 * disinterest in the current event (read or write a specific MSR) by using an
6539 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
6540 */
6541static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
6542 struct vmcs12 *vmcs12, u32 exit_reason)
6543{
6544 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
6545 gpa_t bitmap;
6546
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01006547 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Nadav Har'El644d7112011-05-25 23:12:35 +03006548 return 1;
6549
6550 /*
6551 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
6552 * for the four combinations of read/write and low/high MSR numbers.
6553 * First we need to figure out which of the four to use:
6554 */
6555 bitmap = vmcs12->msr_bitmap;
6556 if (exit_reason == EXIT_REASON_MSR_WRITE)
6557 bitmap += 2048;
6558 if (msr_index >= 0xc0000000) {
6559 msr_index -= 0xc0000000;
6560 bitmap += 1024;
6561 }
6562
6563 /* Then read the msr_index'th bit from this bitmap: */
6564 if (msr_index < 1024*8) {
6565 unsigned char b;
Jan Kiszkabd31a7f2013-02-14 19:46:27 +01006566 if (kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1))
6567 return 1;
Nadav Har'El644d7112011-05-25 23:12:35 +03006568 return 1 & (b >> (msr_index & 7));
6569 } else
6570 return 1; /* let L1 handle the wrong parameter */
6571}
6572
6573/*
6574 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
6575 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
6576 * intercept (via guest_host_mask etc.) the current event.
6577 */
6578static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
6579 struct vmcs12 *vmcs12)
6580{
6581 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6582 int cr = exit_qualification & 15;
6583 int reg = (exit_qualification >> 8) & 15;
6584 unsigned long val = kvm_register_read(vcpu, reg);
6585
6586 switch ((exit_qualification >> 4) & 3) {
6587 case 0: /* mov to cr */
6588 switch (cr) {
6589 case 0:
6590 if (vmcs12->cr0_guest_host_mask &
6591 (val ^ vmcs12->cr0_read_shadow))
6592 return 1;
6593 break;
6594 case 3:
6595 if ((vmcs12->cr3_target_count >= 1 &&
6596 vmcs12->cr3_target_value0 == val) ||
6597 (vmcs12->cr3_target_count >= 2 &&
6598 vmcs12->cr3_target_value1 == val) ||
6599 (vmcs12->cr3_target_count >= 3 &&
6600 vmcs12->cr3_target_value2 == val) ||
6601 (vmcs12->cr3_target_count >= 4 &&
6602 vmcs12->cr3_target_value3 == val))
6603 return 0;
6604 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
6605 return 1;
6606 break;
6607 case 4:
6608 if (vmcs12->cr4_guest_host_mask &
6609 (vmcs12->cr4_read_shadow ^ val))
6610 return 1;
6611 break;
6612 case 8:
6613 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
6614 return 1;
6615 break;
6616 }
6617 break;
6618 case 2: /* clts */
6619 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
6620 (vmcs12->cr0_read_shadow & X86_CR0_TS))
6621 return 1;
6622 break;
6623 case 1: /* mov from cr */
6624 switch (cr) {
6625 case 3:
6626 if (vmcs12->cpu_based_vm_exec_control &
6627 CPU_BASED_CR3_STORE_EXITING)
6628 return 1;
6629 break;
6630 case 8:
6631 if (vmcs12->cpu_based_vm_exec_control &
6632 CPU_BASED_CR8_STORE_EXITING)
6633 return 1;
6634 break;
6635 }
6636 break;
6637 case 3: /* lmsw */
6638 /*
6639 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
6640 * cr0. Other attempted changes are ignored, with no exit.
6641 */
6642 if (vmcs12->cr0_guest_host_mask & 0xe &
6643 (val ^ vmcs12->cr0_read_shadow))
6644 return 1;
6645 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
6646 !(vmcs12->cr0_read_shadow & 0x1) &&
6647 (val & 0x1))
6648 return 1;
6649 break;
6650 }
6651 return 0;
6652}
6653
6654/*
6655 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
6656 * should handle it ourselves in L0 (and then continue L2). Only call this
6657 * when in is_guest_mode (L2).
6658 */
6659static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
6660{
Nadav Har'El644d7112011-05-25 23:12:35 +03006661 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6662 struct vcpu_vmx *vmx = to_vmx(vcpu);
6663 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01006664 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03006665
Jan Kiszka542060e2014-01-04 18:47:21 +01006666 trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason,
6667 vmcs_readl(EXIT_QUALIFICATION),
6668 vmx->idt_vectoring_info,
6669 intr_info,
6670 vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
6671 KVM_ISA_VMX);
6672
Nadav Har'El644d7112011-05-25 23:12:35 +03006673 if (vmx->nested.nested_run_pending)
6674 return 0;
6675
6676 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02006677 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
6678 vmcs_read32(VM_INSTRUCTION_ERROR));
Nadav Har'El644d7112011-05-25 23:12:35 +03006679 return 1;
6680 }
6681
6682 switch (exit_reason) {
6683 case EXIT_REASON_EXCEPTION_NMI:
6684 if (!is_exception(intr_info))
6685 return 0;
6686 else if (is_page_fault(intr_info))
6687 return enable_ept;
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01006688 else if (is_no_device(intr_info) &&
Paolo Bonziniccf98442014-02-27 22:54:11 +01006689 !(vmcs12->guest_cr0 & X86_CR0_TS))
Anthoine Bourgeoise504c902013-11-13 11:45:37 +01006690 return 0;
Nadav Har'El644d7112011-05-25 23:12:35 +03006691 return vmcs12->exception_bitmap &
6692 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
6693 case EXIT_REASON_EXTERNAL_INTERRUPT:
6694 return 0;
6695 case EXIT_REASON_TRIPLE_FAULT:
6696 return 1;
6697 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02006698 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03006699 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02006700 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03006701 case EXIT_REASON_TASK_SWITCH:
6702 return 1;
6703 case EXIT_REASON_CPUID:
6704 return 1;
6705 case EXIT_REASON_HLT:
6706 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
6707 case EXIT_REASON_INVD:
6708 return 1;
6709 case EXIT_REASON_INVLPG:
6710 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
6711 case EXIT_REASON_RDPMC:
6712 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
6713 case EXIT_REASON_RDTSC:
6714 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
6715 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
6716 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
6717 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
6718 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
6719 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
Nadav Har'Elbfd0a562013-08-05 11:07:17 +03006720 case EXIT_REASON_INVEPT:
Nadav Har'El644d7112011-05-25 23:12:35 +03006721 /*
6722 * VMX instructions trap unconditionally. This allows L1 to
6723 * emulate them for its L2 guest, i.e., allows 3-level nesting!
6724 */
6725 return 1;
6726 case EXIT_REASON_CR_ACCESS:
6727 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
6728 case EXIT_REASON_DR_ACCESS:
6729 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
6730 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006731 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03006732 case EXIT_REASON_MSR_READ:
6733 case EXIT_REASON_MSR_WRITE:
6734 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
6735 case EXIT_REASON_INVALID_STATE:
6736 return 1;
6737 case EXIT_REASON_MWAIT_INSTRUCTION:
6738 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
6739 case EXIT_REASON_MONITOR_INSTRUCTION:
6740 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
6741 case EXIT_REASON_PAUSE_INSTRUCTION:
6742 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
6743 nested_cpu_has2(vmcs12,
6744 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
6745 case EXIT_REASON_MCE_DURING_VMENTRY:
6746 return 0;
6747 case EXIT_REASON_TPR_BELOW_THRESHOLD:
6748 return 1;
6749 case EXIT_REASON_APIC_ACCESS:
6750 return nested_cpu_has2(vmcs12,
6751 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
6752 case EXIT_REASON_EPT_VIOLATION:
Nadav Har'El2b1be672013-08-05 11:07:19 +03006753 /*
6754 * L0 always deals with the EPT violation. If nested EPT is
6755 * used, and the nested mmu code discovers that the address is
6756 * missing in the guest EPT table (EPT12), the EPT violation
6757 * will be injected with nested_ept_inject_page_fault()
6758 */
6759 return 0;
Nadav Har'El644d7112011-05-25 23:12:35 +03006760 case EXIT_REASON_EPT_MISCONFIG:
Nadav Har'El2b1be672013-08-05 11:07:19 +03006761 /*
6762 * L2 never uses directly L1's EPT, but rather L0's own EPT
6763 * table (shadow on EPT) or a merged EPT table that L0 built
6764 * (EPT on EPT). So any problems with the structure of the
6765 * table is L0's fault.
6766 */
Nadav Har'El644d7112011-05-25 23:12:35 +03006767 return 0;
6768 case EXIT_REASON_WBINVD:
6769 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
6770 case EXIT_REASON_XSETBV:
6771 return 1;
6772 default:
6773 return 1;
6774 }
6775}
6776
Avi Kivity586f9602010-11-18 13:09:54 +02006777static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
6778{
6779 *info1 = vmcs_readl(EXIT_QUALIFICATION);
6780 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
6781}
6782
Avi Kivity6aa8b732006-12-10 02:21:36 -08006783/*
6784 * The guest has exited. See if we can fix it or if we need userspace
6785 * assistance.
6786 */
Avi Kivity851ba692009-08-24 11:10:17 +03006787static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006788{
Avi Kivity29bd8a72007-09-10 17:27:03 +03006789 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006790 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02006791 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03006792
Mohammed Gamal80ced182009-09-01 12:48:18 +02006793 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02006794 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02006795 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006796
Nadav Har'El644d7112011-05-25 23:12:35 +03006797 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
Jan Kiszka533558b2014-01-04 18:47:20 +01006798 nested_vmx_vmexit(vcpu, exit_reason,
6799 vmcs_read32(VM_EXIT_INTR_INFO),
6800 vmcs_readl(EXIT_QUALIFICATION));
Nadav Har'El644d7112011-05-25 23:12:35 +03006801 return 1;
6802 }
6803
Mohammed Gamal51207022010-05-31 22:40:54 +03006804 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
6805 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6806 vcpu->run->fail_entry.hardware_entry_failure_reason
6807 = exit_reason;
6808 return 0;
6809 }
6810
Avi Kivity29bd8a72007-09-10 17:27:03 +03006811 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006812 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6813 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03006814 = vmcs_read32(VM_INSTRUCTION_ERROR);
6815 return 0;
6816 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006817
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006818 /*
6819 * Note:
6820 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6821 * delivery event since it indicates guest is accessing MMIO.
6822 * The vm-exit can be triggered again after return to guest that
6823 * will cause infinite loop.
6824 */
Mike Dayd77c26f2007-10-08 09:02:08 -04006825 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08006826 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02006827 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006828 exit_reason != EXIT_REASON_TASK_SWITCH)) {
6829 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6830 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
6831 vcpu->run->internal.ndata = 2;
6832 vcpu->run->internal.data[0] = vectoring_info;
6833 vcpu->run->internal.data[1] = exit_reason;
6834 return 0;
6835 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006836
Nadav Har'El644d7112011-05-25 23:12:35 +03006837 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
6838 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
Nadav Har'Elf5c43682013-08-05 11:07:20 +03006839 get_vmcs12(vcpu))))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03006840 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006841 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006842 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01006843 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006844 /*
6845 * This CPU don't support us in finding the end of an
6846 * NMI-blocked window if the guest runs with IRQs
6847 * disabled. So we pull the trigger after 1 s of
6848 * futile waiting, but inform the user about this.
6849 */
6850 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6851 "state on VCPU %d after 1 s timeout\n",
6852 __func__, vcpu->vcpu_id);
6853 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006854 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006855 }
6856
Avi Kivity6aa8b732006-12-10 02:21:36 -08006857 if (exit_reason < kvm_vmx_max_exit_handlers
6858 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03006859 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006860 else {
Avi Kivity851ba692009-08-24 11:10:17 +03006861 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6862 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006863 }
6864 return 0;
6865}
6866
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006867static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006868{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006869 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006870 vmcs_write32(TPR_THRESHOLD, 0);
6871 return;
6872 }
6873
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006874 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006875}
6876
Yang Zhang8d146952013-01-25 10:18:50 +08006877static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
6878{
6879 u32 sec_exec_control;
6880
6881 /*
6882 * There is not point to enable virtualize x2apic without enable
6883 * apicv
6884 */
Yang Zhangc7c9c562013-01-25 10:18:51 +08006885 if (!cpu_has_vmx_virtualize_x2apic_mode() ||
6886 !vmx_vm_has_apicv(vcpu->kvm))
Yang Zhang8d146952013-01-25 10:18:50 +08006887 return;
6888
6889 if (!vm_need_tpr_shadow(vcpu->kvm))
6890 return;
6891
6892 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6893
6894 if (set) {
6895 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6896 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6897 } else {
6898 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6899 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6900 }
6901 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
6902
6903 vmx_set_msr_bitmap(vcpu);
6904}
6905
Yang Zhangc7c9c562013-01-25 10:18:51 +08006906static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
6907{
6908 u16 status;
6909 u8 old;
6910
6911 if (!vmx_vm_has_apicv(kvm))
6912 return;
6913
6914 if (isr == -1)
6915 isr = 0;
6916
6917 status = vmcs_read16(GUEST_INTR_STATUS);
6918 old = status >> 8;
6919 if (isr != old) {
6920 status &= 0xff;
6921 status |= isr << 8;
6922 vmcs_write16(GUEST_INTR_STATUS, status);
6923 }
6924}
6925
6926static void vmx_set_rvi(int vector)
6927{
6928 u16 status;
6929 u8 old;
6930
6931 status = vmcs_read16(GUEST_INTR_STATUS);
6932 old = (u8)status & 0xff;
6933 if ((u8)vector != old) {
6934 status &= ~0xff;
6935 status |= (u8)vector;
6936 vmcs_write16(GUEST_INTR_STATUS, status);
6937 }
6938}
6939
6940static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6941{
6942 if (max_irr == -1)
6943 return;
6944
6945 vmx_set_rvi(max_irr);
6946}
6947
6948static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
6949{
Yang Zhang3d81bc72013-04-11 19:25:13 +08006950 if (!vmx_vm_has_apicv(vcpu->kvm))
6951 return;
6952
Yang Zhangc7c9c562013-01-25 10:18:51 +08006953 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
6954 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
6955 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
6956 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
6957}
6958
Avi Kivity51aa01d2010-07-20 14:31:20 +03006959static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03006960{
Avi Kivity00eba012011-03-07 17:24:54 +02006961 u32 exit_intr_info;
6962
6963 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
6964 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
6965 return;
6966
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006967 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02006968 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08006969
6970 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02006971 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08006972 kvm_machine_check();
6973
Gleb Natapov20f65982009-05-11 13:35:55 +03006974 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02006975 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006976 (exit_intr_info & INTR_INFO_VALID_MASK)) {
6977 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03006978 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006979 kvm_after_handle_nmi(&vmx->vcpu);
6980 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03006981}
Gleb Natapov20f65982009-05-11 13:35:55 +03006982
Yang Zhanga547c6d2013-04-11 19:25:10 +08006983static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
6984{
6985 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6986
6987 /*
6988 * If external interrupt exists, IF bit is set in rflags/eflags on the
6989 * interrupt stack frame, and interrupt will be enabled on a return
6990 * from interrupt handler.
6991 */
6992 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
6993 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
6994 unsigned int vector;
6995 unsigned long entry;
6996 gate_desc *desc;
6997 struct vcpu_vmx *vmx = to_vmx(vcpu);
6998#ifdef CONFIG_X86_64
6999 unsigned long tmp;
7000#endif
7001
7002 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
7003 desc = (gate_desc *)vmx->host_idt_base + vector;
7004 entry = gate_offset(*desc);
7005 asm volatile(
7006#ifdef CONFIG_X86_64
7007 "mov %%" _ASM_SP ", %[sp]\n\t"
7008 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
7009 "push $%c[ss]\n\t"
7010 "push %[sp]\n\t"
7011#endif
7012 "pushf\n\t"
7013 "orl $0x200, (%%" _ASM_SP ")\n\t"
7014 __ASM_SIZE(push) " $%c[cs]\n\t"
7015 "call *%[entry]\n\t"
7016 :
7017#ifdef CONFIG_X86_64
7018 [sp]"=&r"(tmp)
7019#endif
7020 :
7021 [entry]"r"(entry),
7022 [ss]"i"(__KERNEL_DS),
7023 [cs]"i"(__KERNEL_CS)
7024 );
7025 } else
7026 local_irq_enable();
7027}
7028
Liu, Jinsongda8999d2014-02-24 10:55:46 +00007029static bool vmx_mpx_supported(void)
7030{
7031 return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) &&
7032 (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS);
7033}
7034
Avi Kivity51aa01d2010-07-20 14:31:20 +03007035static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
7036{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007037 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03007038 bool unblock_nmi;
7039 u8 vector;
7040 bool idtv_info_valid;
7041
7042 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03007043
Avi Kivitycf393f72008-07-01 16:20:21 +03007044 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02007045 if (vmx->nmi_known_unmasked)
7046 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02007047 /*
7048 * Can't use vmx->exit_intr_info since we're not sure what
7049 * the exit reason is.
7050 */
7051 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03007052 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
7053 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
7054 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007055 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03007056 * Re-set bit "block by NMI" before VM entry if vmexit caused by
7057 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007058 * SDM 3: 23.2.2 (September 2008)
7059 * Bit 12 is undefined in any of the following cases:
7060 * If the VM exit sets the valid bit in the IDT-vectoring
7061 * information field.
7062 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03007063 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007064 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
7065 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03007066 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7067 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02007068 else
7069 vmx->nmi_known_unmasked =
7070 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
7071 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02007072 } else if (unlikely(vmx->soft_vnmi_blocked))
7073 vmx->vnmi_blocked_time +=
7074 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03007075}
7076
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007077static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03007078 u32 idt_vectoring_info,
7079 int instr_len_field,
7080 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03007081{
Avi Kivity51aa01d2010-07-20 14:31:20 +03007082 u8 vector;
7083 int type;
7084 bool idtv_info_valid;
7085
7086 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03007087
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007088 vcpu->arch.nmi_injected = false;
7089 kvm_clear_exception_queue(vcpu);
7090 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007091
7092 if (!idtv_info_valid)
7093 return;
7094
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007095 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03007096
Avi Kivity668f6122008-07-02 09:28:55 +03007097 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
7098 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03007099
Gleb Natapov64a7ec02009-03-30 16:03:29 +03007100 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03007101 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007102 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03007103 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03007104 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03007105 * Clear bit "block by NMI" before VM entry if a NMI
7106 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03007107 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007108 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007109 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03007110 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007111 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007112 /* fall through */
7113 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03007114 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03007115 u32 err = vmcs_read32(error_code_field);
Gleb Natapov851eb6672013-09-25 12:51:34 +03007116 kvm_requeue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03007117 } else
Gleb Natapov851eb6672013-09-25 12:51:34 +03007118 kvm_requeue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007119 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007120 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007121 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03007122 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03007123 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007124 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03007125 break;
7126 default:
7127 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03007128 }
Avi Kivitycf393f72008-07-01 16:20:21 +03007129}
7130
Avi Kivity83422e12010-07-20 14:43:23 +03007131static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
7132{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007133 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03007134 VM_EXIT_INSTRUCTION_LEN,
7135 IDT_VECTORING_ERROR_CODE);
7136}
7137
Avi Kivityb463a6f2010-07-20 15:06:17 +03007138static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
7139{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01007140 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007141 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
7142 VM_ENTRY_INSTRUCTION_LEN,
7143 VM_ENTRY_EXCEPTION_ERROR_CODE);
7144
7145 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
7146}
7147
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007148static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
7149{
7150 int i, nr_msrs;
7151 struct perf_guest_switch_msr *msrs;
7152
7153 msrs = perf_guest_get_msrs(&nr_msrs);
7154
7155 if (!msrs)
7156 return;
7157
7158 for (i = 0; i < nr_msrs; i++)
7159 if (msrs[i].host == msrs[i].guest)
7160 clear_atomic_switch_msr(vmx, msrs[i].msr);
7161 else
7162 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
7163 msrs[i].host);
7164}
7165
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08007166static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007167{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007168 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03007169 unsigned long debugctlmsr;
Avi Kivity104f2262010-11-18 13:12:52 +02007170
7171 /* Record the guest's net vcpu time for enforced NMI injections. */
7172 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
7173 vmx->entry_time = ktime_get();
7174
7175 /* Don't enter VMX if guest state is invalid, let the exit handler
7176 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02007177 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02007178 return;
7179
Abel Gordon012f83c2013-04-18 14:39:25 +03007180 if (vmx->nested.sync_shadow_vmcs) {
7181 copy_vmcs12_to_shadow(vmx);
7182 vmx->nested.sync_shadow_vmcs = false;
7183 }
7184
Avi Kivity104f2262010-11-18 13:12:52 +02007185 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
7186 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
7187 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
7188 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
7189
7190 /* When single-stepping over STI and MOV SS, we must clear the
7191 * corresponding interruptibility bits in the guest state. Otherwise
7192 * vmentry fails as it then expects bit 14 (BS) in pending debug
7193 * exceptions being set, but that's not correct for the guest debugging
7194 * case. */
7195 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
7196 vmx_set_interrupt_shadow(vcpu, 0);
7197
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007198 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03007199 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02007200
Nadav Har'Eld462b812011-05-24 15:26:10 +03007201 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02007202 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08007203 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007204 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
7205 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
7206 "push %%" _ASM_CX " \n\t"
7207 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03007208 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007209 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03007210 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03007211 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007212 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007213 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
7214 "mov %%cr2, %%" _ASM_DX " \n\t"
7215 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007216 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007217 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03007218 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007219 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02007220 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007221 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007222 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
7223 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
7224 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
7225 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
7226 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
7227 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08007228#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02007229 "mov %c[r8](%0), %%r8 \n\t"
7230 "mov %c[r9](%0), %%r9 \n\t"
7231 "mov %c[r10](%0), %%r10 \n\t"
7232 "mov %c[r11](%0), %%r11 \n\t"
7233 "mov %c[r12](%0), %%r12 \n\t"
7234 "mov %c[r13](%0), %%r13 \n\t"
7235 "mov %c[r14](%0), %%r14 \n\t"
7236 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007237#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03007238 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03007239
Avi Kivity6aa8b732006-12-10 02:21:36 -08007240 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03007241 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03007242 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03007243 "jmp 2f \n\t"
7244 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
7245 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08007246 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03007247 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02007248 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007249 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
7250 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
7251 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
7252 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
7253 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
7254 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
7255 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08007256#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02007257 "mov %%r8, %c[r8](%0) \n\t"
7258 "mov %%r9, %c[r9](%0) \n\t"
7259 "mov %%r10, %c[r10](%0) \n\t"
7260 "mov %%r11, %c[r11](%0) \n\t"
7261 "mov %%r12, %c[r12](%0) \n\t"
7262 "mov %%r13, %c[r13](%0) \n\t"
7263 "mov %%r14, %c[r14](%0) \n\t"
7264 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08007265#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03007266 "mov %%cr2, %%" _ASM_AX " \n\t"
7267 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03007268
Avi Kivityb188c81f2012-09-16 15:10:58 +03007269 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02007270 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03007271 ".pushsection .rodata \n\t"
7272 ".global vmx_return \n\t"
7273 "vmx_return: " _ASM_PTR " 2b \n\t"
7274 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02007275 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03007276 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02007277 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03007278 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007279 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
7280 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
7281 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
7282 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
7283 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
7284 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
7285 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08007286#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08007287 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
7288 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
7289 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
7290 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
7291 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
7292 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
7293 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
7294 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08007295#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02007296 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
7297 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02007298 : "cc", "memory"
7299#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03007300 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02007301 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03007302#else
7303 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02007304#endif
7305 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08007306
Gleb Natapov2a7921b2012-08-12 16:12:29 +03007307 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
7308 if (debugctlmsr)
7309 update_debugctlmsr(debugctlmsr);
7310
Avi Kivityaa67f602012-08-01 16:48:03 +03007311#ifndef CONFIG_X86_64
7312 /*
7313 * The sysexit path does not restore ds/es, so we must set them to
7314 * a reasonable value ourselves.
7315 *
7316 * We can't defer this to vmx_load_host_state() since that function
7317 * may be executed in interrupt context, which saves and restore segments
7318 * around it, nullifying its effect.
7319 */
7320 loadsegment(ds, __USER_DS);
7321 loadsegment(es, __USER_DS);
7322#endif
7323
Avi Kivity6de4f3a2009-05-31 22:58:47 +03007324 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02007325 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivity69c73022011-03-07 15:26:44 +02007326 | (1 << VCPU_EXREG_CPL)
Avi Kivityaff48ba2010-12-05 18:56:11 +02007327 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03007328 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02007329 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007330 vcpu->arch.regs_dirty = 0;
7331
Avi Kivity1155f762007-11-22 11:30:47 +02007332 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
7333
Nadav Har'Eld462b812011-05-24 15:26:10 +03007334 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02007335
Avi Kivity51aa01d2010-07-20 14:31:20 +03007336 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02007337 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
Avi Kivity51aa01d2010-07-20 14:31:20 +03007338
Gleb Natapove0b890d2013-09-25 12:51:33 +03007339 /*
7340 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
7341 * we did not inject a still-pending event to L1 now because of
7342 * nested_run_pending, we need to re-enable this bit.
7343 */
7344 if (vmx->nested.nested_run_pending)
7345 kvm_make_request(KVM_REQ_EVENT, vcpu);
7346
7347 vmx->nested.nested_run_pending = 0;
7348
Avi Kivity51aa01d2010-07-20 14:31:20 +03007349 vmx_complete_atomic_exit(vmx);
7350 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03007351 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007352}
7353
Avi Kivity6aa8b732006-12-10 02:21:36 -08007354static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
7355{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007356 struct vcpu_vmx *vmx = to_vmx(vcpu);
7357
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08007358 free_vpid(vmx);
Nadav Har'Eld462b812011-05-24 15:26:10 +03007359 free_loaded_vmcs(vmx->loaded_vmcs);
Marcelo Tosatti26a865f2014-01-03 17:00:51 -02007360 free_nested(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007361 kfree(vmx->guest_msrs);
7362 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10007363 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007364}
7365
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007366static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08007367{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007368 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10007369 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03007370 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007371
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007372 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007373 return ERR_PTR(-ENOMEM);
7374
Sheng Yang2384d2b2008-01-17 15:14:33 +08007375 allocate_vpid(vmx);
7376
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007377 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
7378 if (err)
7379 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08007380
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007381 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02007382 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007383 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007384 goto uninit_vcpu;
7385 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08007386
Nadav Har'Eld462b812011-05-24 15:26:10 +03007387 vmx->loaded_vmcs = &vmx->vmcs01;
7388 vmx->loaded_vmcs->vmcs = alloc_vmcs();
7389 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007390 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03007391 if (!vmm_exclusive)
7392 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
7393 loaded_vmcs_init(vmx->loaded_vmcs);
7394 if (!vmm_exclusive)
7395 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007396
Avi Kivity15ad7142007-07-11 18:17:21 +03007397 cpu = get_cpu();
7398 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10007399 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10007400 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007401 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03007402 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007403 if (err)
7404 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02007405 if (vm_need_virtualize_apic_accesses(kvm)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02007406 err = alloc_apic_access_page(kvm);
7407 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02007408 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02007409 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08007410
Sheng Yangb927a3c2009-07-21 10:42:48 +08007411 if (enable_ept) {
7412 if (!kvm->arch.ept_identity_map_addr)
7413 kvm->arch.ept_identity_map_addr =
7414 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Gleb Natapov93ea5382011-02-21 12:07:59 +02007415 err = -ENOMEM;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007416 if (alloc_identity_pagetable(kvm) != 0)
7417 goto free_vmcs;
Gleb Natapov93ea5382011-02-21 12:07:59 +02007418 if (!init_rmode_identity_map(kvm))
7419 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08007420 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007421
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03007422 vmx->nested.current_vmptr = -1ull;
7423 vmx->nested.current_vmcs12 = NULL;
7424
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007425 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08007426
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007427free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08007428 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007429free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007430 kfree(vmx->guest_msrs);
7431uninit_vcpu:
7432 kvm_vcpu_uninit(&vmx->vcpu);
7433free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08007434 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10007435 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007436 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007437}
7438
Yang, Sheng002c7f72007-07-31 14:23:01 +03007439static void __init vmx_check_processor_compat(void *rtn)
7440{
7441 struct vmcs_config vmcs_conf;
7442
7443 *(int *)rtn = 0;
7444 if (setup_vmcs_config(&vmcs_conf) < 0)
7445 *(int *)rtn = -EIO;
7446 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
7447 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
7448 smp_processor_id());
7449 *(int *)rtn = -EIO;
7450 }
7451}
7452
Sheng Yang67253af2008-04-25 10:20:22 +08007453static int get_ept_level(void)
7454{
7455 return VMX_EPT_DEFAULT_GAW + 1;
7456}
7457
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007458static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08007459{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007460 u64 ret;
7461
Sheng Yang522c68c2009-04-27 20:35:43 +08007462 /* For VT-d and EPT combination
7463 * 1. MMIO: always map as UC
7464 * 2. EPT with VT-d:
7465 * a. VT-d without snooping control feature: can't guarantee the
7466 * result, try to trust guest.
7467 * b. VT-d with snooping control feature: snooping control feature of
7468 * VT-d engine can guarantee the cache correctness. Just set it
7469 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08007470 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08007471 * consistent with host MTRR
7472 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007473 if (is_mmio)
7474 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Alex Williamsone0f0bbc2013-10-30 11:02:30 -06007475 else if (kvm_arch_has_noncoherent_dma(vcpu->kvm))
Sheng Yang522c68c2009-04-27 20:35:43 +08007476 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
7477 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007478 else
Sheng Yang522c68c2009-04-27 20:35:43 +08007479 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08007480 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007481
7482 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08007483}
7484
Sheng Yang17cc3932010-01-05 19:02:27 +08007485static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02007486{
Sheng Yang878403b2010-01-05 19:02:29 +08007487 if (enable_ept && !cpu_has_vmx_ept_1g_page())
7488 return PT_DIRECTORY_LEVEL;
7489 else
7490 /* For shadow and EPT supported 1GB page */
7491 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02007492}
7493
Sheng Yang0e851882009-12-18 16:48:46 +08007494static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
7495{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007496 struct kvm_cpuid_entry2 *best;
7497 struct vcpu_vmx *vmx = to_vmx(vcpu);
7498 u32 exec_control;
7499
7500 vmx->rdtscp_enabled = false;
7501 if (vmx_rdtscp_supported()) {
7502 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7503 if (exec_control & SECONDARY_EXEC_RDTSCP) {
7504 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
7505 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
7506 vmx->rdtscp_enabled = true;
7507 else {
7508 exec_control &= ~SECONDARY_EXEC_RDTSCP;
7509 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7510 exec_control);
7511 }
7512 }
7513 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007514
Mao, Junjiead756a12012-07-02 01:18:48 +00007515 /* Exposing INVPCID only when PCID is exposed */
7516 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
7517 if (vmx_invpcid_supported() &&
Ren, Yongjie4f977042012-09-07 07:36:59 +00007518 best && (best->ebx & bit(X86_FEATURE_INVPCID)) &&
Mao, Junjiead756a12012-07-02 01:18:48 +00007519 guest_cpuid_has_pcid(vcpu)) {
Takashi Iwai29282fd2012-11-09 15:20:17 +01007520 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Mao, Junjiead756a12012-07-02 01:18:48 +00007521 exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
7522 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7523 exec_control);
7524 } else {
Takashi Iwai29282fd2012-11-09 15:20:17 +01007525 if (cpu_has_secondary_exec_ctrls()) {
7526 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7527 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
7528 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7529 exec_control);
7530 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007531 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00007532 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00007533 }
Sheng Yang0e851882009-12-18 16:48:46 +08007534}
7535
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007536static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
7537{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03007538 if (func == 1 && nested)
7539 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007540}
7541
Yang Zhang25d92082013-08-06 12:00:32 +03007542static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
7543 struct x86_exception *fault)
7544{
Jan Kiszka533558b2014-01-04 18:47:20 +01007545 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7546 u32 exit_reason;
Yang Zhang25d92082013-08-06 12:00:32 +03007547
7548 if (fault->error_code & PFERR_RSVD_MASK)
Jan Kiszka533558b2014-01-04 18:47:20 +01007549 exit_reason = EXIT_REASON_EPT_MISCONFIG;
Yang Zhang25d92082013-08-06 12:00:32 +03007550 else
Jan Kiszka533558b2014-01-04 18:47:20 +01007551 exit_reason = EXIT_REASON_EPT_VIOLATION;
7552 nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification);
Yang Zhang25d92082013-08-06 12:00:32 +03007553 vmcs12->guest_physical_address = fault->address;
7554}
7555
Nadav Har'El155a97a2013-08-05 11:07:16 +03007556/* Callbacks for nested_ept_init_mmu_context: */
7557
7558static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu)
7559{
7560 /* return the page table to be shadowed - in our case, EPT12 */
7561 return get_vmcs12(vcpu)->ept_pointer;
7562}
7563
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02007564static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
Nadav Har'El155a97a2013-08-05 11:07:16 +03007565{
Paolo Bonzini8a3c1a332013-10-02 16:56:13 +02007566 kvm_init_shadow_ept_mmu(vcpu, &vcpu->arch.mmu,
Nadav Har'El155a97a2013-08-05 11:07:16 +03007567 nested_vmx_ept_caps & VMX_EPT_EXECUTE_ONLY_BIT);
7568
7569 vcpu->arch.mmu.set_cr3 = vmx_set_cr3;
7570 vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3;
7571 vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault;
7572
7573 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
Nadav Har'El155a97a2013-08-05 11:07:16 +03007574}
7575
7576static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
7577{
7578 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
7579}
7580
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03007581static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu,
7582 struct x86_exception *fault)
7583{
7584 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7585
7586 WARN_ON(!is_guest_mode(vcpu));
7587
7588 /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
7589 if (vmcs12->exception_bitmap & (1u << PF_VECTOR))
Jan Kiszka533558b2014-01-04 18:47:20 +01007590 nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason,
7591 vmcs_read32(VM_EXIT_INTR_INFO),
7592 vmcs_readl(EXIT_QUALIFICATION));
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03007593 else
7594 kvm_inject_page_fault(vcpu, fault);
7595}
7596
Jan Kiszkaf41245002014-03-07 20:03:13 +01007597static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu)
7598{
7599 u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value;
7600 struct vcpu_vmx *vmx = to_vmx(vcpu);
7601
7602 if (vcpu->arch.virtual_tsc_khz == 0)
7603 return;
7604
7605 /* Make sure short timeouts reliably trigger an immediate vmexit.
7606 * hrtimer_start does not guarantee this. */
7607 if (preemption_timeout <= 1) {
7608 vmx_preemption_timer_fn(&vmx->nested.preemption_timer);
7609 return;
7610 }
7611
7612 preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
7613 preemption_timeout *= 1000000;
7614 do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz);
7615 hrtimer_start(&vmx->nested.preemption_timer,
7616 ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL);
7617}
7618
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007619/*
7620 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
7621 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
7622 * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2
7623 * guest in a way that will both be appropriate to L1's requests, and our
7624 * needs. In addition to modifying the active vmcs (which is vmcs02), this
7625 * function also has additional necessary side-effects, like setting various
7626 * vcpu->arch fields.
7627 */
7628static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7629{
7630 struct vcpu_vmx *vmx = to_vmx(vcpu);
7631 u32 exec_control;
7632
7633 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
7634 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
7635 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
7636 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
7637 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
7638 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
7639 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
7640 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
7641 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
7642 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
7643 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
7644 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
7645 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
7646 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
7647 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
7648 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
7649 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
7650 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
7651 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
7652 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
7653 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
7654 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
7655 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
7656 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
7657 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
7658 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
7659 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
7660 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
7661 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
7662 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
7663 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
7664 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
7665 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
7666 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
7667 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
7668 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
7669
7670 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
7671 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
7672 vmcs12->vm_entry_intr_info_field);
7673 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
7674 vmcs12->vm_entry_exception_error_code);
7675 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
7676 vmcs12->vm_entry_instruction_len);
7677 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
7678 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007679 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Jan Kiszka503cd0c2013-03-03 13:05:44 +01007680 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
Gleb Natapov63fbf592013-07-28 18:31:06 +03007681 vmx_set_rflags(vcpu, vmcs12->guest_rflags);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007682 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
7683 vmcs12->guest_pending_dbg_exceptions);
7684 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
7685 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
7686
7687 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7688
Jan Kiszkaf41245002014-03-07 20:03:13 +01007689 exec_control = vmcs12->pin_based_vm_exec_control;
7690 exec_control |= vmcs_config.pin_based_exec_ctrl;
7691 exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
7692 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007693
Jan Kiszkaf41245002014-03-07 20:03:13 +01007694 vmx->nested.preemption_timer_expired = false;
7695 if (nested_cpu_has_preemption_timer(vmcs12))
7696 vmx_start_preemption_timer(vcpu);
Jan Kiszka0238ea92013-03-13 11:31:24 +01007697
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007698 /*
7699 * Whether page-faults are trapped is determined by a combination of
7700 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
7701 * If enable_ept, L0 doesn't care about page faults and we should
7702 * set all of these to L1's desires. However, if !enable_ept, L0 does
7703 * care about (at least some) page faults, and because it is not easy
7704 * (if at all possible?) to merge L0 and L1's desires, we simply ask
7705 * to exit on each and every L2 page fault. This is done by setting
7706 * MASK=MATCH=0 and (see below) EB.PF=1.
7707 * Note that below we don't need special code to set EB.PF beyond the
7708 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
7709 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
7710 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
7711 *
7712 * A problem with this approach (when !enable_ept) is that L1 may be
7713 * injected with more page faults than it asked for. This could have
7714 * caused problems, but in practice existing hypervisors don't care.
7715 * To fix this, we will need to emulate the PFEC checking (on the L1
7716 * page tables), using walk_addr(), when injecting PFs to L1.
7717 */
7718 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
7719 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
7720 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
7721 enable_ept ? vmcs12->page_fault_error_code_match : 0);
7722
7723 if (cpu_has_secondary_exec_ctrls()) {
Jan Kiszkaf41245002014-03-07 20:03:13 +01007724 exec_control = vmx_secondary_exec_control(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007725 if (!vmx->rdtscp_enabled)
7726 exec_control &= ~SECONDARY_EXEC_RDTSCP;
7727 /* Take the following fields only from vmcs12 */
7728 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7729 if (nested_cpu_has(vmcs12,
7730 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
7731 exec_control |= vmcs12->secondary_vm_exec_control;
7732
7733 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
7734 /*
7735 * Translate L1 physical address to host physical
7736 * address for vmcs02. Keep the page pinned, so this
7737 * physical address remains valid. We keep a reference
7738 * to it so we can release it later.
7739 */
7740 if (vmx->nested.apic_access_page) /* shouldn't happen */
7741 nested_release_page(vmx->nested.apic_access_page);
7742 vmx->nested.apic_access_page =
7743 nested_get_page(vcpu, vmcs12->apic_access_addr);
7744 /*
7745 * If translation failed, no matter: This feature asks
7746 * to exit when accessing the given address, and if it
7747 * can never be accessed, this feature won't do
7748 * anything anyway.
7749 */
7750 if (!vmx->nested.apic_access_page)
7751 exec_control &=
7752 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7753 else
7754 vmcs_write64(APIC_ACCESS_ADDR,
7755 page_to_phys(vmx->nested.apic_access_page));
Jan Kiszkaca3f2572013-12-16 12:55:46 +01007756 } else if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) {
7757 exec_control |=
7758 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7759 vmcs_write64(APIC_ACCESS_ADDR,
7760 page_to_phys(vcpu->kvm->arch.apic_access_page));
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007761 }
7762
7763 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
7764 }
7765
7766
7767 /*
7768 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
7769 * Some constant fields are set here by vmx_set_constant_host_state().
7770 * Other fields are different per CPU, and will be set later when
7771 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
7772 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08007773 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007774
7775 /*
7776 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
7777 * entry, but only if the current (host) sp changed from the value
7778 * we wrote last (vmx->host_rsp). This cache is no longer relevant
7779 * if we switch vmcs, and rather than hold a separate cache per vmcs,
7780 * here we just force the write to happen on entry.
7781 */
7782 vmx->host_rsp = 0;
7783
7784 exec_control = vmx_exec_control(vmx); /* L0's desires */
7785 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
7786 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
7787 exec_control &= ~CPU_BASED_TPR_SHADOW;
7788 exec_control |= vmcs12->cpu_based_vm_exec_control;
7789 /*
7790 * Merging of IO and MSR bitmaps not currently supported.
7791 * Rather, exit every time.
7792 */
7793 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
7794 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
7795 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
7796
7797 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
7798
7799 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
7800 * bitwise-or of what L1 wants to trap for L2, and what we want to
7801 * trap. Note that CR0.TS also needs updating - we do this later.
7802 */
7803 update_exception_bitmap(vcpu);
7804 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
7805 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
7806
Nadav Har'El8049d652013-08-05 11:07:06 +03007807 /* L2->L1 exit controls are emulated - the hardware exit is to L0 so
7808 * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER
7809 * bits are further modified by vmx_set_efer() below.
7810 */
Jan Kiszkaf41245002014-03-07 20:03:13 +01007811 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Nadav Har'El8049d652013-08-05 11:07:06 +03007812
7813 /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are
7814 * emulated by vmx_set_efer(), below.
7815 */
Gleb Natapov2961e8762013-11-25 15:37:13 +02007816 vm_entry_controls_init(vmx,
Nadav Har'El8049d652013-08-05 11:07:06 +03007817 (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER &
7818 ~VM_ENTRY_IA32E_MODE) |
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007819 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
7820
Jan Kiszka44811c02013-08-04 17:17:27 +02007821 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007822 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02007823 vcpu->arch.pat = vmcs12->guest_ia32_pat;
7824 } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007825 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
7826
7827
7828 set_cr4_guest_host_mask(vmx);
7829
Nadav Har'El27fc51b2011-08-02 15:54:52 +03007830 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
7831 vmcs_write64(TSC_OFFSET,
7832 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
7833 else
7834 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007835
7836 if (enable_vpid) {
7837 /*
7838 * Trivially support vpid by letting L2s share their parent
7839 * L1's vpid. TODO: move to a more elaborate solution, giving
7840 * each L2 its own vpid and exposing the vpid feature to L1.
7841 */
7842 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
7843 vmx_flush_tlb(vcpu);
7844 }
7845
Nadav Har'El155a97a2013-08-05 11:07:16 +03007846 if (nested_cpu_has_ept(vmcs12)) {
7847 kvm_mmu_unload(vcpu);
7848 nested_ept_init_mmu_context(vcpu);
7849 }
7850
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007851 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
7852 vcpu->arch.efer = vmcs12->guest_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +02007853 else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007854 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
7855 else
7856 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
7857 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
7858 vmx_set_efer(vcpu, vcpu->arch.efer);
7859
7860 /*
7861 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
7862 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
7863 * The CR0_READ_SHADOW is what L2 should have expected to read given
7864 * the specifications by L1; It's not enough to take
7865 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
7866 * have more bits than L1 expected.
7867 */
7868 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
7869 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
7870
7871 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
7872 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
7873
7874 /* shadow page tables on either EPT or shadow page tables */
7875 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
7876 kvm_mmu_reset_context(vcpu);
7877
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03007878 if (!enable_ept)
7879 vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested;
7880
Nadav Har'El3633cfc2013-08-05 11:07:07 +03007881 /*
7882 * L1 may access the L2's PDPTR, so save them to construct vmcs12
7883 */
7884 if (enable_ept) {
7885 vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0);
7886 vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1);
7887 vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2);
7888 vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3);
7889 }
7890
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007891 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
7892 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
7893}
7894
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007895/*
7896 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
7897 * for running an L2 nested guest.
7898 */
7899static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
7900{
7901 struct vmcs12 *vmcs12;
7902 struct vcpu_vmx *vmx = to_vmx(vcpu);
7903 int cpu;
7904 struct loaded_vmcs *vmcs02;
Jan Kiszka384bb782013-04-20 10:52:36 +02007905 bool ia32e;
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007906
7907 if (!nested_vmx_check_permission(vcpu) ||
7908 !nested_vmx_check_vmcs12(vcpu))
7909 return 1;
7910
7911 skip_emulated_instruction(vcpu);
7912 vmcs12 = get_vmcs12(vcpu);
7913
Abel Gordon012f83c2013-04-18 14:39:25 +03007914 if (enable_shadow_vmcs)
7915 copy_shadow_to_vmcs12(vmx);
7916
Nadav Har'El7c177932011-05-25 23:12:04 +03007917 /*
7918 * The nested entry process starts with enforcing various prerequisites
7919 * on vmcs12 as required by the Intel SDM, and act appropriately when
7920 * they fail: As the SDM explains, some conditions should cause the
7921 * instruction to fail, while others will cause the instruction to seem
7922 * to succeed, but return an EXIT_REASON_INVALID_STATE.
7923 * To speed up the normal (success) code path, we should avoid checking
7924 * for misconfigurations which will anyway be caught by the processor
7925 * when using the merged vmcs02.
7926 */
7927 if (vmcs12->launch_state == launch) {
7928 nested_vmx_failValid(vcpu,
7929 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
7930 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
7931 return 1;
7932 }
7933
Jan Kiszka6dfacad2013-12-04 08:58:54 +01007934 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE &&
7935 vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) {
Paolo Bonzini26539bd2013-04-15 15:00:27 +02007936 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7937 return 1;
7938 }
7939
Nadav Har'El7c177932011-05-25 23:12:04 +03007940 if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
7941 !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) {
7942 /*TODO: Also verify bits beyond physical address width are 0*/
7943 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7944 return 1;
7945 }
7946
7947 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
7948 !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) {
7949 /*TODO: Also verify bits beyond physical address width are 0*/
7950 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7951 return 1;
7952 }
7953
7954 if (vmcs12->vm_entry_msr_load_count > 0 ||
7955 vmcs12->vm_exit_msr_load_count > 0 ||
7956 vmcs12->vm_exit_msr_store_count > 0) {
Jan Kiszkabd801582011-09-12 11:26:22 +02007957 pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n",
7958 __func__);
Nadav Har'El7c177932011-05-25 23:12:04 +03007959 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7960 return 1;
7961 }
7962
7963 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
7964 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) ||
7965 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
7966 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
7967 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
7968 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
7969 !vmx_control_verify(vmcs12->vm_exit_controls,
7970 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) ||
7971 !vmx_control_verify(vmcs12->vm_entry_controls,
7972 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high))
7973 {
7974 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7975 return 1;
7976 }
7977
7978 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
7979 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
7980 nested_vmx_failValid(vcpu,
7981 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
7982 return 1;
7983 }
7984
Jan Kiszka92fbc7b2013-08-08 16:26:33 +02007985 if (!nested_cr0_valid(vmcs12, vmcs12->guest_cr0) ||
Nadav Har'El7c177932011-05-25 23:12:04 +03007986 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
7987 nested_vmx_entry_failure(vcpu, vmcs12,
7988 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
7989 return 1;
7990 }
7991 if (vmcs12->vmcs_link_pointer != -1ull) {
7992 nested_vmx_entry_failure(vcpu, vmcs12,
7993 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
7994 return 1;
7995 }
7996
7997 /*
Jan Kiszkacb0c8cda2013-04-27 12:58:00 +02007998 * If the load IA32_EFER VM-entry control is 1, the following checks
Jan Kiszka384bb782013-04-20 10:52:36 +02007999 * are performed on the field for the IA32_EFER MSR:
8000 * - Bits reserved in the IA32_EFER MSR must be 0.
8001 * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of
8002 * the IA-32e mode guest VM-exit control. It must also be identical
8003 * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to
8004 * CR0.PG) is 1.
8005 */
8006 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) {
8007 ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0;
8008 if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) ||
8009 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) ||
8010 ((vmcs12->guest_cr0 & X86_CR0_PG) &&
8011 ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) {
8012 nested_vmx_entry_failure(vcpu, vmcs12,
8013 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
8014 return 1;
8015 }
8016 }
8017
8018 /*
8019 * If the load IA32_EFER VM-exit control is 1, bits reserved in the
8020 * IA32_EFER MSR must be 0 in the field for that register. In addition,
8021 * the values of the LMA and LME bits in the field must each be that of
8022 * the host address-space size VM-exit control.
8023 */
8024 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) {
8025 ia32e = (vmcs12->vm_exit_controls &
8026 VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0;
8027 if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) ||
8028 ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) ||
8029 ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) {
8030 nested_vmx_entry_failure(vcpu, vmcs12,
8031 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
8032 return 1;
8033 }
8034 }
8035
8036 /*
Nadav Har'El7c177932011-05-25 23:12:04 +03008037 * We're finally done with prerequisite checking, and can start with
8038 * the nested entry.
8039 */
8040
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008041 vmcs02 = nested_get_current_vmcs02(vmx);
8042 if (!vmcs02)
8043 return -ENOMEM;
8044
8045 enter_guest_mode(vcpu);
8046
8047 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
8048
8049 cpu = get_cpu();
8050 vmx->loaded_vmcs = vmcs02;
8051 vmx_vcpu_put(vcpu);
8052 vmx_vcpu_load(vcpu, cpu);
8053 vcpu->cpu = cpu;
8054 put_cpu();
8055
Jan Kiszka36c3cc42013-02-23 22:35:37 +01008056 vmx_segment_cache_clear(vmx);
8057
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008058 vmcs12->launch_state = 1;
8059
8060 prepare_vmcs02(vcpu, vmcs12);
8061
Jan Kiszka6dfacad2013-12-04 08:58:54 +01008062 if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT)
8063 return kvm_emulate_halt(vcpu);
8064
Jan Kiszka7af40ad32014-01-04 18:47:23 +01008065 vmx->nested.nested_run_pending = 1;
8066
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03008067 /*
8068 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
8069 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
8070 * returned as far as L1 is concerned. It will only return (and set
8071 * the success flag) when L2 exits (see nested_vmx_vmexit()).
8072 */
8073 return 1;
8074}
8075
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008076/*
8077 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
8078 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
8079 * This function returns the new value we should put in vmcs12.guest_cr0.
8080 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
8081 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
8082 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
8083 * didn't trap the bit, because if L1 did, so would L0).
8084 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
8085 * been modified by L2, and L1 knows it. So just leave the old value of
8086 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
8087 * isn't relevant, because if L0 traps this bit it can set it to anything.
8088 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
8089 * changed these bits, and therefore they need to be updated, but L0
8090 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
8091 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
8092 */
8093static inline unsigned long
8094vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
8095{
8096 return
8097 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
8098 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
8099 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
8100 vcpu->arch.cr0_guest_owned_bits));
8101}
8102
8103static inline unsigned long
8104vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
8105{
8106 return
8107 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
8108 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
8109 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
8110 vcpu->arch.cr4_guest_owned_bits));
8111}
8112
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008113static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
8114 struct vmcs12 *vmcs12)
8115{
8116 u32 idt_vectoring;
8117 unsigned int nr;
8118
Gleb Natapov851eb6672013-09-25 12:51:34 +03008119 if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008120 nr = vcpu->arch.exception.nr;
8121 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
8122
8123 if (kvm_exception_is_soft(nr)) {
8124 vmcs12->vm_exit_instruction_len =
8125 vcpu->arch.event_exit_inst_len;
8126 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
8127 } else
8128 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
8129
8130 if (vcpu->arch.exception.has_error_code) {
8131 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
8132 vmcs12->idt_vectoring_error_code =
8133 vcpu->arch.exception.error_code;
8134 }
8135
8136 vmcs12->idt_vectoring_info_field = idt_vectoring;
Jan Kiszkacd2633c2013-10-23 17:42:15 +01008137 } else if (vcpu->arch.nmi_injected) {
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008138 vmcs12->idt_vectoring_info_field =
8139 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
8140 } else if (vcpu->arch.interrupt.pending) {
8141 nr = vcpu->arch.interrupt.nr;
8142 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
8143
8144 if (vcpu->arch.interrupt.soft) {
8145 idt_vectoring |= INTR_TYPE_SOFT_INTR;
8146 vmcs12->vm_entry_instruction_len =
8147 vcpu->arch.event_exit_inst_len;
8148 } else
8149 idt_vectoring |= INTR_TYPE_EXT_INTR;
8150
8151 vmcs12->idt_vectoring_info_field = idt_vectoring;
8152 }
8153}
8154
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008155static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr)
8156{
8157 struct vcpu_vmx *vmx = to_vmx(vcpu);
8158
Jan Kiszkaf41245002014-03-07 20:03:13 +01008159 if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) &&
8160 vmx->nested.preemption_timer_expired) {
8161 if (vmx->nested.nested_run_pending)
8162 return -EBUSY;
8163 nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0);
8164 return 0;
8165 }
8166
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008167 if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) {
Jan Kiszka220c5672014-03-07 20:03:14 +01008168 if (vmx->nested.nested_run_pending ||
8169 vcpu->arch.interrupt.pending)
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008170 return -EBUSY;
8171 nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI,
8172 NMI_VECTOR | INTR_TYPE_NMI_INTR |
8173 INTR_INFO_VALID_MASK, 0);
8174 /*
8175 * The NMI-triggered VM exit counts as injection:
8176 * clear this one and block further NMIs.
8177 */
8178 vcpu->arch.nmi_pending = 0;
8179 vmx_set_nmi_mask(vcpu, true);
8180 return 0;
8181 }
8182
8183 if ((kvm_cpu_has_interrupt(vcpu) || external_intr) &&
8184 nested_exit_on_intr(vcpu)) {
8185 if (vmx->nested.nested_run_pending)
8186 return -EBUSY;
8187 nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0);
8188 }
8189
8190 return 0;
8191}
8192
Jan Kiszkaf41245002014-03-07 20:03:13 +01008193static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu)
8194{
8195 ktime_t remaining =
8196 hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer);
8197 u64 value;
8198
8199 if (ktime_to_ns(remaining) <= 0)
8200 return 0;
8201
8202 value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz;
8203 do_div(value, 1000000);
8204 return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE;
8205}
8206
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008207/*
8208 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
8209 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
8210 * and this function updates it to reflect the changes to the guest state while
8211 * L2 was running (and perhaps made some exits which were handled directly by L0
8212 * without going back to L1), and to reflect the exit reason.
8213 * Note that we do not have to copy here all VMCS fields, just those that
8214 * could have changed by the L2 guest or the exit - i.e., the guest-state and
8215 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
8216 * which already writes to vmcs12 directly.
8217 */
Jan Kiszka533558b2014-01-04 18:47:20 +01008218static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12,
8219 u32 exit_reason, u32 exit_intr_info,
8220 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008221{
8222 /* update guest state fields: */
8223 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
8224 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
8225
8226 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
8227 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
8228 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
8229 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
8230
8231 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
8232 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
8233 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
8234 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
8235 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
8236 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
8237 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
8238 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
8239 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
8240 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
8241 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
8242 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
8243 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
8244 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
8245 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
8246 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
8247 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
8248 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
8249 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
8250 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
8251 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
8252 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
8253 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
8254 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
8255 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
8256 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
8257 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
8258 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
8259 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
8260 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
8261 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
8262 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
8263 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
8264 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
8265 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
8266 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
8267
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008268 vmcs12->guest_interruptibility_info =
8269 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
8270 vmcs12->guest_pending_dbg_exceptions =
8271 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
Jan Kiszka3edf1e62014-01-04 18:47:24 +01008272 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
8273 vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT;
8274 else
8275 vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008276
Jan Kiszkaf41245002014-03-07 20:03:13 +01008277 if (nested_cpu_has_preemption_timer(vmcs12)) {
8278 if (vmcs12->vm_exit_controls &
8279 VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)
8280 vmcs12->vmx_preemption_timer_value =
8281 vmx_get_preemption_timer_value(vcpu);
8282 hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer);
8283 }
Arthur Chunqi Li7854cbc2013-09-16 16:11:44 +08008284
Nadav Har'El3633cfc2013-08-05 11:07:07 +03008285 /*
8286 * In some cases (usually, nested EPT), L2 is allowed to change its
8287 * own CR3 without exiting. If it has changed it, we must keep it.
8288 * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined
8289 * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12.
8290 *
8291 * Additionally, restore L2's PDPTR to vmcs12.
8292 */
8293 if (enable_ept) {
8294 vmcs12->guest_cr3 = vmcs_read64(GUEST_CR3);
8295 vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0);
8296 vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1);
8297 vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2);
8298 vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3);
8299 }
8300
Jan Kiszkac18911a2013-03-13 16:06:41 +01008301 vmcs12->vm_entry_controls =
8302 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
Gleb Natapov2961e8762013-11-25 15:37:13 +02008303 (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE);
Jan Kiszkac18911a2013-03-13 16:06:41 +01008304
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008305 /* TODO: These cannot have changed unless we have MSR bitmaps and
8306 * the relevant bit asks not to trap the change */
8307 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Jan Kiszkab8c07d52013-04-06 13:51:21 +02008308 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008309 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
Jan Kiszka10ba54a2013-08-08 16:26:31 +02008310 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER)
8311 vmcs12->guest_ia32_efer = vcpu->arch.efer;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008312 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
8313 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
8314 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
8315
8316 /* update exit information fields: */
8317
Jan Kiszka533558b2014-01-04 18:47:20 +01008318 vmcs12->vm_exit_reason = exit_reason;
8319 vmcs12->exit_qualification = exit_qualification;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008320
Jan Kiszka533558b2014-01-04 18:47:20 +01008321 vmcs12->vm_exit_intr_info = exit_intr_info;
Jan Kiszkac0d1c772013-04-14 12:12:50 +02008322 if ((vmcs12->vm_exit_intr_info &
8323 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
8324 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
8325 vmcs12->vm_exit_intr_error_code =
8326 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008327 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008328 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
8329 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
8330
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008331 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
8332 /* vm_entry_intr_info_field is cleared on exit. Emulate this
8333 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008334 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008335
8336 /*
8337 * Transfer the event that L0 or L1 may wanted to inject into
8338 * L2 to IDT_VECTORING_INFO_FIELD.
8339 */
8340 vmcs12_save_pending_event(vcpu, vmcs12);
8341 }
8342
8343 /*
8344 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
8345 * preserved above and would only end up incorrectly in L1.
8346 */
8347 vcpu->arch.nmi_injected = false;
8348 kvm_clear_exception_queue(vcpu);
8349 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008350}
8351
8352/*
8353 * A part of what we need to when the nested L2 guest exits and we want to
8354 * run its L1 parent, is to reset L1's guest state to the host state specified
8355 * in vmcs12.
8356 * This function is to be called not only on normal nested exit, but also on
8357 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
8358 * Failures During or After Loading Guest State").
8359 * This function should be called when the active VMCS is L1's (vmcs01).
8360 */
Jan Kiszka733568f2013-02-23 15:07:47 +01008361static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
8362 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008363{
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08008364 struct kvm_segment seg;
8365
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008366 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
8367 vcpu->arch.efer = vmcs12->host_ia32_efer;
Jan Kiszkad1fa0352013-04-14 12:44:54 +02008368 else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008369 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
8370 else
8371 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
8372 vmx_set_efer(vcpu, vcpu->arch.efer);
8373
8374 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
8375 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
H. Peter Anvin1adfa762013-04-27 16:10:11 -07008376 vmx_set_rflags(vcpu, X86_EFLAGS_FIXED);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008377 /*
8378 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
8379 * actually changed, because it depends on the current state of
8380 * fpu_active (which may have changed).
8381 * Note that vmx_set_cr0 refers to efer set above.
8382 */
Jan Kiszka9e3e4dbf2013-09-03 21:11:45 +02008383 vmx_set_cr0(vcpu, vmcs12->host_cr0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008384 /*
8385 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
8386 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
8387 * but we also need to update cr0_guest_host_mask and exception_bitmap.
8388 */
8389 update_exception_bitmap(vcpu);
8390 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
8391 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
8392
8393 /*
8394 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
8395 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
8396 */
8397 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
8398 kvm_set_cr4(vcpu, vmcs12->host_cr4);
8399
Jan Kiszka29bf08f2013-12-28 16:31:52 +01008400 nested_ept_uninit_mmu_context(vcpu);
Nadav Har'El155a97a2013-08-05 11:07:16 +03008401
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008402 kvm_set_cr3(vcpu, vmcs12->host_cr3);
8403 kvm_mmu_reset_context(vcpu);
8404
Gleb Natapovfeaf0c7d2013-09-25 12:51:36 +03008405 if (!enable_ept)
8406 vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault;
8407
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008408 if (enable_vpid) {
8409 /*
8410 * Trivially support vpid by letting L2s share their parent
8411 * L1's vpid. TODO: move to a more elaborate solution, giving
8412 * each L2 its own vpid and exposing the vpid feature to L1.
8413 */
8414 vmx_flush_tlb(vcpu);
8415 }
8416
8417
8418 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
8419 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
8420 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
8421 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
8422 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008423
Jan Kiszka44811c02013-08-04 17:17:27 +02008424 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) {
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008425 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
Jan Kiszka44811c02013-08-04 17:17:27 +02008426 vcpu->arch.pat = vmcs12->host_ia32_pat;
8427 }
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008428 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
8429 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
8430 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +01008431
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08008432 /* Set L1 segment info according to Intel SDM
8433 27.5.2 Loading Host Segment and Descriptor-Table Registers */
8434 seg = (struct kvm_segment) {
8435 .base = 0,
8436 .limit = 0xFFFFFFFF,
8437 .selector = vmcs12->host_cs_selector,
8438 .type = 11,
8439 .present = 1,
8440 .s = 1,
8441 .g = 1
8442 };
8443 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
8444 seg.l = 1;
8445 else
8446 seg.db = 1;
8447 vmx_set_segment(vcpu, &seg, VCPU_SREG_CS);
8448 seg = (struct kvm_segment) {
8449 .base = 0,
8450 .limit = 0xFFFFFFFF,
8451 .type = 3,
8452 .present = 1,
8453 .s = 1,
8454 .db = 1,
8455 .g = 1
8456 };
8457 seg.selector = vmcs12->host_ds_selector;
8458 vmx_set_segment(vcpu, &seg, VCPU_SREG_DS);
8459 seg.selector = vmcs12->host_es_selector;
8460 vmx_set_segment(vcpu, &seg, VCPU_SREG_ES);
8461 seg.selector = vmcs12->host_ss_selector;
8462 vmx_set_segment(vcpu, &seg, VCPU_SREG_SS);
8463 seg.selector = vmcs12->host_fs_selector;
8464 seg.base = vmcs12->host_fs_base;
8465 vmx_set_segment(vcpu, &seg, VCPU_SREG_FS);
8466 seg.selector = vmcs12->host_gs_selector;
8467 seg.base = vmcs12->host_gs_base;
8468 vmx_set_segment(vcpu, &seg, VCPU_SREG_GS);
8469 seg = (struct kvm_segment) {
Gleb Natapov205befd2013-08-04 15:08:06 +03008470 .base = vmcs12->host_tr_base,
Arthur Chunqi Li21feb4e2013-07-15 16:04:08 +08008471 .limit = 0x67,
8472 .selector = vmcs12->host_tr_selector,
8473 .type = 11,
8474 .present = 1
8475 };
8476 vmx_set_segment(vcpu, &seg, VCPU_SREG_TR);
8477
Jan Kiszka503cd0c2013-03-03 13:05:44 +01008478 kvm_set_dr(vcpu, 7, 0x400);
8479 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008480}
8481
8482/*
8483 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
8484 * and modify vmcs12 to make it see what it would expect to see there if
8485 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
8486 */
Jan Kiszka533558b2014-01-04 18:47:20 +01008487static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason,
8488 u32 exit_intr_info,
8489 unsigned long exit_qualification)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008490{
8491 struct vcpu_vmx *vmx = to_vmx(vcpu);
8492 int cpu;
8493 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8494
Jan Kiszka5f3d5792013-04-14 12:12:46 +02008495 /* trying to cancel vmlaunch/vmresume is a bug */
8496 WARN_ON_ONCE(vmx->nested.nested_run_pending);
8497
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008498 leave_guest_mode(vcpu);
Jan Kiszka533558b2014-01-04 18:47:20 +01008499 prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info,
8500 exit_qualification);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008501
Jan Kiszka542060e2014-01-04 18:47:21 +01008502 trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason,
8503 vmcs12->exit_qualification,
8504 vmcs12->idt_vectoring_info_field,
8505 vmcs12->vm_exit_intr_info,
8506 vmcs12->vm_exit_intr_error_code,
8507 KVM_ISA_VMX);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008508
8509 cpu = get_cpu();
8510 vmx->loaded_vmcs = &vmx->vmcs01;
8511 vmx_vcpu_put(vcpu);
8512 vmx_vcpu_load(vcpu, cpu);
8513 vcpu->cpu = cpu;
8514 put_cpu();
8515
Gleb Natapov2961e8762013-11-25 15:37:13 +02008516 vm_entry_controls_init(vmx, vmcs_read32(VM_ENTRY_CONTROLS));
8517 vm_exit_controls_init(vmx, vmcs_read32(VM_EXIT_CONTROLS));
Jan Kiszka36c3cc42013-02-23 22:35:37 +01008518 vmx_segment_cache_clear(vmx);
8519
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008520 /* if no vmcs02 cache requested, remove the one we used */
8521 if (VMCS02_POOL_SIZE == 0)
8522 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
8523
8524 load_vmcs12_host_state(vcpu, vmcs12);
8525
Nadav Har'El27fc51b2011-08-02 15:54:52 +03008526 /* Update TSC_OFFSET if TSC was changed while L2 ran */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008527 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
8528
8529 /* This is needed for same reason as it was needed in prepare_vmcs02 */
8530 vmx->host_rsp = 0;
8531
8532 /* Unpin physical memory we referred to in vmcs02 */
8533 if (vmx->nested.apic_access_page) {
8534 nested_release_page(vmx->nested.apic_access_page);
8535 vmx->nested.apic_access_page = 0;
8536 }
8537
8538 /*
8539 * Exiting from L2 to L1, we're now back to L1 which thinks it just
8540 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
8541 * success or failure flag accordingly.
8542 */
8543 if (unlikely(vmx->fail)) {
8544 vmx->fail = 0;
8545 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
8546 } else
8547 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +03008548 if (enable_shadow_vmcs)
8549 vmx->nested.sync_shadow_vmcs = true;
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008550
8551 /* in case we halted in L2 */
8552 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03008553}
8554
Nadav Har'El7c177932011-05-25 23:12:04 +03008555/*
Jan Kiszka42124922014-01-04 18:47:19 +01008556 * Forcibly leave nested mode in order to be able to reset the VCPU later on.
8557 */
8558static void vmx_leave_nested(struct kvm_vcpu *vcpu)
8559{
8560 if (is_guest_mode(vcpu))
Jan Kiszka533558b2014-01-04 18:47:20 +01008561 nested_vmx_vmexit(vcpu, -1, 0, 0);
Jan Kiszka42124922014-01-04 18:47:19 +01008562 free_nested(to_vmx(vcpu));
8563}
8564
8565/*
Nadav Har'El7c177932011-05-25 23:12:04 +03008566 * L1's failure to enter L2 is a subset of a normal exit, as explained in
8567 * 23.7 "VM-entry failures during or after loading guest state" (this also
8568 * lists the acceptable exit-reason and exit-qualification parameters).
8569 * It should only be called before L2 actually succeeded to run, and when
8570 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
8571 */
8572static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
8573 struct vmcs12 *vmcs12,
8574 u32 reason, unsigned long qualification)
8575{
8576 load_vmcs12_host_state(vcpu, vmcs12);
8577 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
8578 vmcs12->exit_qualification = qualification;
8579 nested_vmx_succeed(vcpu);
Abel Gordon012f83c2013-04-18 14:39:25 +03008580 if (enable_shadow_vmcs)
8581 to_vmx(vcpu)->nested.sync_shadow_vmcs = true;
Nadav Har'El7c177932011-05-25 23:12:04 +03008582}
8583
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02008584static int vmx_check_intercept(struct kvm_vcpu *vcpu,
8585 struct x86_instruction_info *info,
8586 enum x86_intercept_stage stage)
8587{
8588 return X86EMUL_CONTINUE;
8589}
8590
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03008591static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08008592 .cpu_has_kvm_support = cpu_has_kvm_support,
8593 .disabled_by_bios = vmx_disabled_by_bios,
8594 .hardware_setup = hardware_setup,
8595 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03008596 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008597 .hardware_enable = hardware_enable,
8598 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08008599 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008600
8601 .vcpu_create = vmx_create_vcpu,
8602 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03008603 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008604
Avi Kivity04d2cc72007-09-10 18:10:54 +03008605 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008606 .vcpu_load = vmx_vcpu_load,
8607 .vcpu_put = vmx_vcpu_put,
8608
Jan Kiszkac8639012012-09-21 05:42:55 +02008609 .update_db_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008610 .get_msr = vmx_get_msr,
8611 .set_msr = vmx_set_msr,
8612 .get_segment_base = vmx_get_segment_base,
8613 .get_segment = vmx_get_segment,
8614 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02008615 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008616 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02008617 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02008618 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03008619 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008620 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008621 .set_cr3 = vmx_set_cr3,
8622 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008623 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008624 .get_idt = vmx_get_idt,
8625 .set_idt = vmx_set_idt,
8626 .get_gdt = vmx_get_gdt,
8627 .set_gdt = vmx_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01008628 .get_dr6 = vmx_get_dr6,
8629 .set_dr6 = vmx_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03008630 .set_dr7 = vmx_set_dr7,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03008631 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008632 .get_rflags = vmx_get_rflags,
8633 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02008634 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02008635 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008636
8637 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008638
Avi Kivity6aa8b732006-12-10 02:21:36 -08008639 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02008640 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008641 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04008642 .set_interrupt_shadow = vmx_set_interrupt_shadow,
8643 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02008644 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03008645 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008646 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02008647 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03008648 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02008649 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008650 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01008651 .get_nmi_mask = vmx_get_nmi_mask,
8652 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008653 .enable_nmi_window = enable_nmi_window,
8654 .enable_irq_window = enable_irq_window,
8655 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08008656 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Yang Zhangc7c9c562013-01-25 10:18:51 +08008657 .vm_has_apicv = vmx_vm_has_apicv,
8658 .load_eoi_exitmap = vmx_load_eoi_exitmap,
8659 .hwapic_irr_update = vmx_hwapic_irr_update,
8660 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +08008661 .sync_pir_to_irr = vmx_sync_pir_to_irr,
8662 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03008663
Izik Eiduscbc94022007-10-25 00:29:55 +02008664 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08008665 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08008666 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03008667
Avi Kivity586f9602010-11-18 13:09:54 +02008668 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02008669
Sheng Yang17cc3932010-01-05 19:02:27 +08008670 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08008671
8672 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08008673
8674 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00008675 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02008676
8677 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08008678
8679 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10008680
Joerg Roedel4051b182011-03-25 09:44:49 +01008681 .set_tsc_khz = vmx_set_tsc_khz,
Will Auldba904632012-11-29 12:42:50 -08008682 .read_tsc_offset = vmx_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10008683 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10008684 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01008685 .compute_tsc_offset = vmx_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03008686 .read_l1_tsc = vmx_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02008687
8688 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02008689
8690 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08008691 .handle_external_intr = vmx_handle_external_intr,
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008692 .mpx_supported = vmx_mpx_supported,
Jan Kiszkab6b8a142014-03-07 20:03:12 +01008693
8694 .check_nested_events = vmx_check_nested_events,
Avi Kivity6aa8b732006-12-10 02:21:36 -08008695};
8696
8697static int __init vmx_init(void)
8698{
Yang Zhang8d146952013-01-25 10:18:50 +08008699 int r, i, msr;
Avi Kivity26bb0982009-09-07 11:14:12 +03008700
8701 rdmsrl_safe(MSR_EFER, &host_efer);
8702
8703 for (i = 0; i < NR_VMX_MSR; ++i)
8704 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03008705
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008706 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03008707 if (!vmx_io_bitmap_a)
8708 return -ENOMEM;
8709
Guo Chao2106a542012-06-15 11:31:56 +08008710 r = -ENOMEM;
8711
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008712 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008713 if (!vmx_io_bitmap_b)
He, Qingfdef3ad2007-04-30 09:45:24 +03008714 goto out;
He, Qingfdef3ad2007-04-30 09:45:24 +03008715
Avi Kivity58972972009-02-24 22:26:47 +02008716 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008717 if (!vmx_msr_bitmap_legacy)
Sheng Yang25c5f222008-03-28 13:18:56 +08008718 goto out1;
Guo Chao2106a542012-06-15 11:31:56 +08008719
Yang Zhang8d146952013-01-25 10:18:50 +08008720 vmx_msr_bitmap_legacy_x2apic =
8721 (unsigned long *)__get_free_page(GFP_KERNEL);
8722 if (!vmx_msr_bitmap_legacy_x2apic)
8723 goto out2;
Sheng Yang25c5f222008-03-28 13:18:56 +08008724
Avi Kivity58972972009-02-24 22:26:47 +02008725 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008726 if (!vmx_msr_bitmap_longmode)
Yang Zhang8d146952013-01-25 10:18:50 +08008727 goto out3;
Guo Chao2106a542012-06-15 11:31:56 +08008728
Yang Zhang8d146952013-01-25 10:18:50 +08008729 vmx_msr_bitmap_longmode_x2apic =
8730 (unsigned long *)__get_free_page(GFP_KERNEL);
8731 if (!vmx_msr_bitmap_longmode_x2apic)
8732 goto out4;
Abel Gordon4607c2d2013-04-18 14:35:55 +03008733 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
8734 if (!vmx_vmread_bitmap)
8735 goto out5;
8736
8737 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
8738 if (!vmx_vmwrite_bitmap)
8739 goto out6;
8740
8741 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
8742 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
8743 /* shadowed read/write fields */
8744 for (i = 0; i < max_shadow_read_write_fields; i++) {
8745 clear_bit(shadow_read_write_fields[i], vmx_vmwrite_bitmap);
8746 clear_bit(shadow_read_write_fields[i], vmx_vmread_bitmap);
8747 }
8748 /* shadowed read only fields */
8749 for (i = 0; i < max_shadow_read_only_fields; i++)
8750 clear_bit(shadow_read_only_fields[i], vmx_vmread_bitmap);
Avi Kivity58972972009-02-24 22:26:47 +02008751
He, Qingfdef3ad2007-04-30 09:45:24 +03008752 /*
8753 * Allow direct access to the PC debug port (it is often used for I/O
8754 * delays, but the vmexits simply slow things down).
8755 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008756 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
8757 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03008758
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008759 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03008760
Avi Kivity58972972009-02-24 22:26:47 +02008761 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
8762 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08008763
Sheng Yang2384d2b2008-01-17 15:14:33 +08008764 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
8765
Avi Kivity0ee75be2010-04-28 15:39:01 +03008766 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
8767 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03008768 if (r)
Abel Gordon4607c2d2013-04-18 14:35:55 +03008769 goto out7;
Sheng Yang25c5f222008-03-28 13:18:56 +08008770
Zhang Yanfei8f536b72012-12-06 23:43:34 +08008771#ifdef CONFIG_KEXEC
8772 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
8773 crash_vmclear_local_loaded_vmcss);
8774#endif
8775
Avi Kivity58972972009-02-24 22:26:47 +02008776 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
8777 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
8778 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
8779 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
8780 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
8781 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
Liu, Jinsongda8999d2014-02-24 10:55:46 +00008782 vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true);
8783
Yang Zhang8d146952013-01-25 10:18:50 +08008784 memcpy(vmx_msr_bitmap_legacy_x2apic,
8785 vmx_msr_bitmap_legacy, PAGE_SIZE);
8786 memcpy(vmx_msr_bitmap_longmode_x2apic,
8787 vmx_msr_bitmap_longmode, PAGE_SIZE);
8788
Yang Zhang01e439b2013-04-11 19:25:12 +08008789 if (enable_apicv) {
Yang Zhang8d146952013-01-25 10:18:50 +08008790 for (msr = 0x800; msr <= 0x8ff; msr++)
8791 vmx_disable_intercept_msr_read_x2apic(msr);
8792
8793 /* According SDM, in x2apic mode, the whole id reg is used.
8794 * But in KVM, it only use the highest eight bits. Need to
8795 * intercept it */
8796 vmx_enable_intercept_msr_read_x2apic(0x802);
8797 /* TMCCT */
8798 vmx_enable_intercept_msr_read_x2apic(0x839);
8799 /* TPR */
8800 vmx_disable_intercept_msr_write_x2apic(0x808);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008801 /* EOI */
8802 vmx_disable_intercept_msr_write_x2apic(0x80b);
8803 /* SELF-IPI */
8804 vmx_disable_intercept_msr_write_x2apic(0x83f);
Yang Zhang8d146952013-01-25 10:18:50 +08008805 }
He, Qingfdef3ad2007-04-30 09:45:24 +03008806
Avi Kivity089d0342009-03-23 18:26:32 +02008807 if (enable_ept) {
Xudong Hao3f6d8c82012-05-22 11:23:15 +08008808 kvm_mmu_set_mask_ptes(0ull,
8809 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
8810 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
8811 0ull, VMX_EPT_EXECUTABLE_MASK);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08008812 ept_set_mmio_spte_mask();
Sheng Yang5fdbcb92008-07-16 09:25:40 +08008813 kvm_enable_tdp();
8814 } else
8815 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08008816
He, Qingfdef3ad2007-04-30 09:45:24 +03008817 return 0;
8818
Abel Gordon4607c2d2013-04-18 14:35:55 +03008819out7:
8820 free_page((unsigned long)vmx_vmwrite_bitmap);
8821out6:
8822 free_page((unsigned long)vmx_vmread_bitmap);
Yang Zhang458f2122013-04-08 15:26:33 +08008823out5:
8824 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Yang Zhang8d146952013-01-25 10:18:50 +08008825out4:
Avi Kivity58972972009-02-24 22:26:47 +02008826 free_page((unsigned long)vmx_msr_bitmap_longmode);
Yang Zhang8d146952013-01-25 10:18:50 +08008827out3:
8828 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
Sheng Yang25c5f222008-03-28 13:18:56 +08008829out2:
Avi Kivity58972972009-02-24 22:26:47 +02008830 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03008831out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008832 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03008833out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008834 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03008835 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008836}
8837
8838static void __exit vmx_exit(void)
8839{
Yang Zhang8d146952013-01-25 10:18:50 +08008840 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
8841 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Avi Kivity58972972009-02-24 22:26:47 +02008842 free_page((unsigned long)vmx_msr_bitmap_legacy);
8843 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008844 free_page((unsigned long)vmx_io_bitmap_b);
8845 free_page((unsigned long)vmx_io_bitmap_a);
Abel Gordon4607c2d2013-04-18 14:35:55 +03008846 free_page((unsigned long)vmx_vmwrite_bitmap);
8847 free_page((unsigned long)vmx_vmread_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03008848
Zhang Yanfei8f536b72012-12-06 23:43:34 +08008849#ifdef CONFIG_KEXEC
8850 rcu_assign_pointer(crash_vmclear_loaded_vmcss, NULL);
8851 synchronize_rcu();
8852#endif
8853
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08008854 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08008855}
8856
8857module_init(vmx_init)
8858module_exit(vmx_exit)