blob: 1cdfb5d7580a9f6d6ffafe9f36d9676f4ba2d703 [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>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030034#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030035#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040036
Avi Kivity6aa8b732006-12-10 02:21:36 -080037#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080038#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020039#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020040#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080041#include <asm/mce.h>
Dexuan Cui2acf9232010-06-10 11:27:12 +080042#include <asm/i387.h>
43#include <asm/xcr.h>
Gleb Natapovd7cd9792011-10-05 14:01:23 +020044#include <asm/perf_event.h>
Zhang Yanfei8f536b72012-12-06 23:43:34 +080045#include <asm/kexec.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080046
Marcelo Tosatti229456f2009-06-17 09:22:14 -030047#include "trace.h"
48
Avi Kivity4ecac3f2008-05-13 13:23:38 +030049#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040050#define __ex_clear(x, reg) \
51 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030052
Avi Kivity6aa8b732006-12-10 02:21:36 -080053MODULE_AUTHOR("Qumranet");
54MODULE_LICENSE("GPL");
55
Josh Triplette9bda3b2012-03-20 23:33:51 -070056static const struct x86_cpu_id vmx_cpu_id[] = {
57 X86_FEATURE_MATCH(X86_FEATURE_VMX),
58 {}
59};
60MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
61
Rusty Russell476bc002012-01-13 09:32:18 +103062static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020063module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080064
Rusty Russell476bc002012-01-13 09:32:18 +103065static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020066module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020067
Rusty Russell476bc002012-01-13 09:32:18 +103068static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020069module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080070
Rusty Russell476bc002012-01-13 09:32:18 +103071static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070072module_param_named(unrestricted_guest,
73 enable_unrestricted_guest, bool, S_IRUGO);
74
Xudong Hao83c3a332012-05-28 19:33:35 +080075static bool __read_mostly enable_ept_ad_bits = 1;
76module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
77
Avi Kivitya27685c2012-06-12 20:30:18 +030078static bool __read_mostly emulate_invalid_guest_state = true;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020079module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030080
Rusty Russell476bc002012-01-13 09:32:18 +103081static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080082module_param(vmm_exclusive, bool, S_IRUGO);
83
Rusty Russell476bc002012-01-13 09:32:18 +103084static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030085module_param(fasteoi, bool, S_IRUGO);
86
Yang Zhang5a717852013-04-11 19:25:16 +080087static bool __read_mostly enable_apicv = 1;
Yang Zhang01e439b2013-04-11 19:25:12 +080088module_param(enable_apicv, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080089
Abel Gordonabc4fc52013-04-18 14:35:25 +030090static bool __read_mostly enable_shadow_vmcs = 1;
91module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO);
Nadav Har'El801d3422011-05-25 23:02:23 +030092/*
93 * If nested=1, nested virtualization is supported, i.e., guests may use
94 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
95 * use VMX instructions.
96 */
Rusty Russell476bc002012-01-13 09:32:18 +103097static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +030098module_param(nested, bool, S_IRUGO);
99
Gleb Natapov50378782013-02-04 16:00:28 +0200100#define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD)
101#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200102#define KVM_VM_CR0_ALWAYS_ON \
103 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200104#define KVM_CR4_GUEST_OWNED_BITS \
105 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
106 | X86_CR4_OSXMMEXCPT)
107
Avi Kivitycdc0e242009-12-06 17:21:14 +0200108#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
109#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
110
Avi Kivity78ac8b42010-04-08 18:19:35 +0300111#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
112
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800113/*
114 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
115 * ple_gap: upper bound on the amount of time between two successive
116 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500117 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800118 * ple_window: upper bound on the amount of time a guest is allowed to execute
119 * in a PAUSE loop. Tests indicate that most spinlocks are held for
120 * less than 2^12 cycles
121 * Time is measured based on a counter that runs at the same rate as the TSC,
122 * refer SDM volume 3b section 21.6.13 & 22.1.3.
123 */
Rik van Riel00c25bc2011-01-04 09:51:33 -0500124#define KVM_VMX_DEFAULT_PLE_GAP 128
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800125#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
126static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
127module_param(ple_gap, int, S_IRUGO);
128
129static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
130module_param(ple_window, int, S_IRUGO);
131
Avi Kivity83287ea422012-09-16 15:10:57 +0300132extern const ulong vmx_return;
133
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200134#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300135#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300136
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400137struct vmcs {
138 u32 revision_id;
139 u32 abort;
140 char data[0];
141};
142
Nadav Har'Eld462b812011-05-24 15:26:10 +0300143/*
144 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
145 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
146 * loaded on this CPU (so we can clear them if the CPU goes down).
147 */
148struct loaded_vmcs {
149 struct vmcs *vmcs;
150 int cpu;
151 int launched;
152 struct list_head loaded_vmcss_on_cpu_link;
153};
154
Avi Kivity26bb0982009-09-07 11:14:12 +0300155struct shared_msr_entry {
156 unsigned index;
157 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200158 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300159};
160
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300161/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300162 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
163 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
164 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
165 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
166 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
167 * More than one of these structures may exist, if L1 runs multiple L2 guests.
168 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
169 * underlying hardware which will be used to run L2.
170 * This structure is packed to ensure that its layout is identical across
171 * machines (necessary for live migration).
172 * If there are changes in this struct, VMCS12_REVISION must be changed.
173 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300174typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300175struct __packed vmcs12 {
176 /* According to the Intel spec, a VMCS region must start with the
177 * following two fields. Then follow implementation-specific data.
178 */
179 u32 revision_id;
180 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300181
Nadav Har'El27d6c862011-05-25 23:06:59 +0300182 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
183 u32 padding[7]; /* room for future expansion */
184
Nadav Har'El22bd0352011-05-25 23:05:57 +0300185 u64 io_bitmap_a;
186 u64 io_bitmap_b;
187 u64 msr_bitmap;
188 u64 vm_exit_msr_store_addr;
189 u64 vm_exit_msr_load_addr;
190 u64 vm_entry_msr_load_addr;
191 u64 tsc_offset;
192 u64 virtual_apic_page_addr;
193 u64 apic_access_addr;
194 u64 ept_pointer;
195 u64 guest_physical_address;
196 u64 vmcs_link_pointer;
197 u64 guest_ia32_debugctl;
198 u64 guest_ia32_pat;
199 u64 guest_ia32_efer;
200 u64 guest_ia32_perf_global_ctrl;
201 u64 guest_pdptr0;
202 u64 guest_pdptr1;
203 u64 guest_pdptr2;
204 u64 guest_pdptr3;
205 u64 host_ia32_pat;
206 u64 host_ia32_efer;
207 u64 host_ia32_perf_global_ctrl;
208 u64 padding64[8]; /* room for future expansion */
209 /*
210 * To allow migration of L1 (complete with its L2 guests) between
211 * machines of different natural widths (32 or 64 bit), we cannot have
212 * unsigned long fields with no explict size. We use u64 (aliased
213 * natural_width) instead. Luckily, x86 is little-endian.
214 */
215 natural_width cr0_guest_host_mask;
216 natural_width cr4_guest_host_mask;
217 natural_width cr0_read_shadow;
218 natural_width cr4_read_shadow;
219 natural_width cr3_target_value0;
220 natural_width cr3_target_value1;
221 natural_width cr3_target_value2;
222 natural_width cr3_target_value3;
223 natural_width exit_qualification;
224 natural_width guest_linear_address;
225 natural_width guest_cr0;
226 natural_width guest_cr3;
227 natural_width guest_cr4;
228 natural_width guest_es_base;
229 natural_width guest_cs_base;
230 natural_width guest_ss_base;
231 natural_width guest_ds_base;
232 natural_width guest_fs_base;
233 natural_width guest_gs_base;
234 natural_width guest_ldtr_base;
235 natural_width guest_tr_base;
236 natural_width guest_gdtr_base;
237 natural_width guest_idtr_base;
238 natural_width guest_dr7;
239 natural_width guest_rsp;
240 natural_width guest_rip;
241 natural_width guest_rflags;
242 natural_width guest_pending_dbg_exceptions;
243 natural_width guest_sysenter_esp;
244 natural_width guest_sysenter_eip;
245 natural_width host_cr0;
246 natural_width host_cr3;
247 natural_width host_cr4;
248 natural_width host_fs_base;
249 natural_width host_gs_base;
250 natural_width host_tr_base;
251 natural_width host_gdtr_base;
252 natural_width host_idtr_base;
253 natural_width host_ia32_sysenter_esp;
254 natural_width host_ia32_sysenter_eip;
255 natural_width host_rsp;
256 natural_width host_rip;
257 natural_width paddingl[8]; /* room for future expansion */
258 u32 pin_based_vm_exec_control;
259 u32 cpu_based_vm_exec_control;
260 u32 exception_bitmap;
261 u32 page_fault_error_code_mask;
262 u32 page_fault_error_code_match;
263 u32 cr3_target_count;
264 u32 vm_exit_controls;
265 u32 vm_exit_msr_store_count;
266 u32 vm_exit_msr_load_count;
267 u32 vm_entry_controls;
268 u32 vm_entry_msr_load_count;
269 u32 vm_entry_intr_info_field;
270 u32 vm_entry_exception_error_code;
271 u32 vm_entry_instruction_len;
272 u32 tpr_threshold;
273 u32 secondary_vm_exec_control;
274 u32 vm_instruction_error;
275 u32 vm_exit_reason;
276 u32 vm_exit_intr_info;
277 u32 vm_exit_intr_error_code;
278 u32 idt_vectoring_info_field;
279 u32 idt_vectoring_error_code;
280 u32 vm_exit_instruction_len;
281 u32 vmx_instruction_info;
282 u32 guest_es_limit;
283 u32 guest_cs_limit;
284 u32 guest_ss_limit;
285 u32 guest_ds_limit;
286 u32 guest_fs_limit;
287 u32 guest_gs_limit;
288 u32 guest_ldtr_limit;
289 u32 guest_tr_limit;
290 u32 guest_gdtr_limit;
291 u32 guest_idtr_limit;
292 u32 guest_es_ar_bytes;
293 u32 guest_cs_ar_bytes;
294 u32 guest_ss_ar_bytes;
295 u32 guest_ds_ar_bytes;
296 u32 guest_fs_ar_bytes;
297 u32 guest_gs_ar_bytes;
298 u32 guest_ldtr_ar_bytes;
299 u32 guest_tr_ar_bytes;
300 u32 guest_interruptibility_info;
301 u32 guest_activity_state;
302 u32 guest_sysenter_cs;
303 u32 host_ia32_sysenter_cs;
Jan Kiszka0238ea92013-03-13 11:31:24 +0100304 u32 vmx_preemption_timer_value;
305 u32 padding32[7]; /* room for future expansion */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300306 u16 virtual_processor_id;
307 u16 guest_es_selector;
308 u16 guest_cs_selector;
309 u16 guest_ss_selector;
310 u16 guest_ds_selector;
311 u16 guest_fs_selector;
312 u16 guest_gs_selector;
313 u16 guest_ldtr_selector;
314 u16 guest_tr_selector;
315 u16 host_es_selector;
316 u16 host_cs_selector;
317 u16 host_ss_selector;
318 u16 host_ds_selector;
319 u16 host_fs_selector;
320 u16 host_gs_selector;
321 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300322};
323
324/*
325 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
326 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
327 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
328 */
329#define VMCS12_REVISION 0x11e57ed0
330
331/*
332 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
333 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
334 * current implementation, 4K are reserved to avoid future complications.
335 */
336#define VMCS12_SIZE 0x1000
337
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300338/* Used to remember the last vmcs02 used for some recently used vmcs12s */
339struct vmcs02_list {
340 struct list_head list;
341 gpa_t vmptr;
342 struct loaded_vmcs vmcs02;
343};
344
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300345/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300346 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
347 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
348 */
349struct nested_vmx {
350 /* Has the level1 guest done vmxon? */
351 bool vmxon;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300352
353 /* The guest-physical address of the current VMCS L1 keeps for L2 */
354 gpa_t current_vmptr;
355 /* The host-usable pointer to the above */
356 struct page *current_vmcs12_page;
357 struct vmcs12 *current_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300358
359 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
360 struct list_head vmcs02_pool;
361 int vmcs02_num;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300362 u64 vmcs01_tsc_offset;
Nadav Har'El644d7112011-05-25 23:12:35 +0300363 /* L2 must run next, and mustn't decide to exit to L1. */
364 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300365 /*
366 * Guest pages referred to in vmcs02 with host-physical pointers, so
367 * we must keep them pinned while L2 runs.
368 */
369 struct page *apic_access_page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300370};
371
Yang Zhang01e439b2013-04-11 19:25:12 +0800372#define POSTED_INTR_ON 0
373/* Posted-Interrupt Descriptor */
374struct pi_desc {
375 u32 pir[8]; /* Posted interrupt requested */
376 u32 control; /* bit 0 of control is outstanding notification bit */
377 u32 rsvd[7];
378} __aligned(64);
379
Yang Zhanga20ed542013-04-11 19:25:15 +0800380static bool pi_test_and_set_on(struct pi_desc *pi_desc)
381{
382 return test_and_set_bit(POSTED_INTR_ON,
383 (unsigned long *)&pi_desc->control);
384}
385
386static bool pi_test_and_clear_on(struct pi_desc *pi_desc)
387{
388 return test_and_clear_bit(POSTED_INTR_ON,
389 (unsigned long *)&pi_desc->control);
390}
391
392static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
393{
394 return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
395}
396
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400397struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000398 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300399 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300400 u8 fail;
Avi Kivity69c73022011-03-07 15:26:44 +0200401 u8 cpl;
Avi Kivity9d58b932011-03-07 16:52:07 +0200402 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300403 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200404 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200405 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300406 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400407 int nmsrs;
408 int save_nmsrs;
Yang Zhanga547c6d2013-04-11 19:25:10 +0800409 unsigned long host_idt_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400410#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300411 u64 msr_host_kernel_gs_base;
412 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400413#endif
Nadav Har'Eld462b812011-05-24 15:26:10 +0300414 /*
415 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
416 * non-nested (L1) guest, it always points to vmcs01. For a nested
417 * guest (L2), it points to a different VMCS.
418 */
419 struct loaded_vmcs vmcs01;
420 struct loaded_vmcs *loaded_vmcs;
421 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300422 struct msr_autoload {
423 unsigned nr;
424 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
425 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
426 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400427 struct {
428 int loaded;
429 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300430#ifdef CONFIG_X86_64
431 u16 ds_sel, es_sel;
432#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200433 int gs_ldt_reload_needed;
434 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400435 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200436 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300437 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300438 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300439 struct kvm_segment segs[8];
440 } rmode;
441 struct {
442 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300443 struct kvm_save_segment {
444 u16 selector;
445 unsigned long base;
446 u32 limit;
447 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300448 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300449 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800450 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300451 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200452
453 /* Support for vnmi-less CPUs */
454 int soft_vnmi_blocked;
455 ktime_t entry_time;
456 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800457 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800458
459 bool rdtscp_enabled;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300460
Yang Zhang01e439b2013-04-11 19:25:12 +0800461 /* Posted interrupt descriptor */
462 struct pi_desc pi_desc;
463
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300464 /* Support for a guest hypervisor (nested VMX) */
465 struct nested_vmx nested;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400466};
467
Avi Kivity2fb92db2011-04-27 19:42:18 +0300468enum segment_cache_field {
469 SEG_FIELD_SEL = 0,
470 SEG_FIELD_BASE = 1,
471 SEG_FIELD_LIMIT = 2,
472 SEG_FIELD_AR = 3,
473
474 SEG_FIELD_NR = 4
475};
476
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400477static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
478{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000479 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400480}
481
Nadav Har'El22bd0352011-05-25 23:05:57 +0300482#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
483#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
484#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
485 [number##_HIGH] = VMCS12_OFFSET(name)+4
486
Abel Gordon4607c2d2013-04-18 14:35:55 +0300487
488static const unsigned long shadow_read_only_fields[] = {
489 /*
490 * We do NOT shadow fields that are modified when L0
491 * traps and emulates any vmx instruction (e.g. VMPTRLD,
492 * VMXON...) executed by L1.
493 * For example, VM_INSTRUCTION_ERROR is read
494 * by L1 if a vmx instruction fails (part of the error path).
495 * Note the code assumes this logic. If for some reason
496 * we start shadowing these fields then we need to
497 * force a shadow sync when L0 emulates vmx instructions
498 * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified
499 * by nested_vmx_failValid)
500 */
501 VM_EXIT_REASON,
502 VM_EXIT_INTR_INFO,
503 VM_EXIT_INSTRUCTION_LEN,
504 IDT_VECTORING_INFO_FIELD,
505 IDT_VECTORING_ERROR_CODE,
506 VM_EXIT_INTR_ERROR_CODE,
507 EXIT_QUALIFICATION,
508 GUEST_LINEAR_ADDRESS,
509 GUEST_PHYSICAL_ADDRESS
510};
511static const int max_shadow_read_only_fields =
512 ARRAY_SIZE(shadow_read_only_fields);
513
514static const unsigned long shadow_read_write_fields[] = {
515 GUEST_RIP,
516 GUEST_RSP,
517 GUEST_CR0,
518 GUEST_CR3,
519 GUEST_CR4,
520 GUEST_INTERRUPTIBILITY_INFO,
521 GUEST_RFLAGS,
522 GUEST_CS_SELECTOR,
523 GUEST_CS_AR_BYTES,
524 GUEST_CS_LIMIT,
525 GUEST_CS_BASE,
526 GUEST_ES_BASE,
527 CR0_GUEST_HOST_MASK,
528 CR0_READ_SHADOW,
529 CR4_READ_SHADOW,
530 TSC_OFFSET,
531 EXCEPTION_BITMAP,
532 CPU_BASED_VM_EXEC_CONTROL,
533 VM_ENTRY_EXCEPTION_ERROR_CODE,
534 VM_ENTRY_INTR_INFO_FIELD,
535 VM_ENTRY_INSTRUCTION_LEN,
536 VM_ENTRY_EXCEPTION_ERROR_CODE,
537 HOST_FS_BASE,
538 HOST_GS_BASE,
539 HOST_FS_SELECTOR,
540 HOST_GS_SELECTOR
541};
542static const int max_shadow_read_write_fields =
543 ARRAY_SIZE(shadow_read_write_fields);
544
Mathias Krause772e0312012-08-30 01:30:19 +0200545static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300546 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
547 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
548 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
549 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
550 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
551 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
552 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
553 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
554 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
555 FIELD(HOST_ES_SELECTOR, host_es_selector),
556 FIELD(HOST_CS_SELECTOR, host_cs_selector),
557 FIELD(HOST_SS_SELECTOR, host_ss_selector),
558 FIELD(HOST_DS_SELECTOR, host_ds_selector),
559 FIELD(HOST_FS_SELECTOR, host_fs_selector),
560 FIELD(HOST_GS_SELECTOR, host_gs_selector),
561 FIELD(HOST_TR_SELECTOR, host_tr_selector),
562 FIELD64(IO_BITMAP_A, io_bitmap_a),
563 FIELD64(IO_BITMAP_B, io_bitmap_b),
564 FIELD64(MSR_BITMAP, msr_bitmap),
565 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
566 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
567 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
568 FIELD64(TSC_OFFSET, tsc_offset),
569 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
570 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
571 FIELD64(EPT_POINTER, ept_pointer),
572 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
573 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
574 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
575 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
576 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
577 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
578 FIELD64(GUEST_PDPTR0, guest_pdptr0),
579 FIELD64(GUEST_PDPTR1, guest_pdptr1),
580 FIELD64(GUEST_PDPTR2, guest_pdptr2),
581 FIELD64(GUEST_PDPTR3, guest_pdptr3),
582 FIELD64(HOST_IA32_PAT, host_ia32_pat),
583 FIELD64(HOST_IA32_EFER, host_ia32_efer),
584 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
585 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
586 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
587 FIELD(EXCEPTION_BITMAP, exception_bitmap),
588 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
589 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
590 FIELD(CR3_TARGET_COUNT, cr3_target_count),
591 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
592 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
593 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
594 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
595 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
596 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
597 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
598 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
599 FIELD(TPR_THRESHOLD, tpr_threshold),
600 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
601 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
602 FIELD(VM_EXIT_REASON, vm_exit_reason),
603 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
604 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
605 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
606 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
607 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
608 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
609 FIELD(GUEST_ES_LIMIT, guest_es_limit),
610 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
611 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
612 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
613 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
614 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
615 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
616 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
617 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
618 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
619 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
620 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
621 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
622 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
623 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
624 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
625 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
626 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
627 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
628 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
629 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
630 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
Jan Kiszka0238ea92013-03-13 11:31:24 +0100631 FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value),
Nadav Har'El22bd0352011-05-25 23:05:57 +0300632 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
633 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
634 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
635 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
636 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
637 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
638 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
639 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
640 FIELD(EXIT_QUALIFICATION, exit_qualification),
641 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
642 FIELD(GUEST_CR0, guest_cr0),
643 FIELD(GUEST_CR3, guest_cr3),
644 FIELD(GUEST_CR4, guest_cr4),
645 FIELD(GUEST_ES_BASE, guest_es_base),
646 FIELD(GUEST_CS_BASE, guest_cs_base),
647 FIELD(GUEST_SS_BASE, guest_ss_base),
648 FIELD(GUEST_DS_BASE, guest_ds_base),
649 FIELD(GUEST_FS_BASE, guest_fs_base),
650 FIELD(GUEST_GS_BASE, guest_gs_base),
651 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
652 FIELD(GUEST_TR_BASE, guest_tr_base),
653 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
654 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
655 FIELD(GUEST_DR7, guest_dr7),
656 FIELD(GUEST_RSP, guest_rsp),
657 FIELD(GUEST_RIP, guest_rip),
658 FIELD(GUEST_RFLAGS, guest_rflags),
659 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
660 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
661 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
662 FIELD(HOST_CR0, host_cr0),
663 FIELD(HOST_CR3, host_cr3),
664 FIELD(HOST_CR4, host_cr4),
665 FIELD(HOST_FS_BASE, host_fs_base),
666 FIELD(HOST_GS_BASE, host_gs_base),
667 FIELD(HOST_TR_BASE, host_tr_base),
668 FIELD(HOST_GDTR_BASE, host_gdtr_base),
669 FIELD(HOST_IDTR_BASE, host_idtr_base),
670 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
671 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
672 FIELD(HOST_RSP, host_rsp),
673 FIELD(HOST_RIP, host_rip),
674};
675static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table);
676
677static inline short vmcs_field_to_offset(unsigned long field)
678{
679 if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0)
680 return -1;
681 return vmcs_field_to_offset_table[field];
682}
683
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300684static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
685{
686 return to_vmx(vcpu)->nested.current_vmcs12;
687}
688
689static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
690{
691 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800692 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300693 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800694
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300695 return page;
696}
697
698static void nested_release_page(struct page *page)
699{
700 kvm_release_page_dirty(page);
701}
702
703static void nested_release_page_clean(struct page *page)
704{
705 kvm_release_page_clean(page);
706}
707
Sheng Yang4e1096d2008-07-06 19:16:51 +0800708static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800709static void kvm_cpu_vmxon(u64 addr);
710static void kvm_cpu_vmxoff(void);
Avi Kivityaff48ba2010-12-05 18:56:11 +0200711static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200712static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300713static void vmx_set_segment(struct kvm_vcpu *vcpu,
714 struct kvm_segment *var, int seg);
715static void vmx_get_segment(struct kvm_vcpu *vcpu,
716 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200717static bool guest_state_valid(struct kvm_vcpu *vcpu);
718static u32 vmx_segment_access_rights(struct kvm_segment *var);
Yang Zhanga20ed542013-04-11 19:25:15 +0800719static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu);
Avi Kivity75880a02007-06-20 11:20:04 +0300720
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721static DEFINE_PER_CPU(struct vmcs *, vmxarea);
722static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300723/*
724 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
725 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
726 */
727static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300728static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200730static unsigned long *vmx_io_bitmap_a;
731static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200732static unsigned long *vmx_msr_bitmap_legacy;
733static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800734static unsigned long *vmx_msr_bitmap_legacy_x2apic;
735static unsigned long *vmx_msr_bitmap_longmode_x2apic;
Abel Gordon4607c2d2013-04-18 14:35:55 +0300736static unsigned long *vmx_vmread_bitmap;
737static unsigned long *vmx_vmwrite_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +0300738
Avi Kivity110312c2010-12-21 12:54:20 +0200739static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200740static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200741
Sheng Yang2384d2b2008-01-17 15:14:33 +0800742static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
743static DEFINE_SPINLOCK(vmx_vpid_lock);
744
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300745static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746 int size;
747 int order;
748 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300749 u32 pin_based_exec_ctrl;
750 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800751 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300752 u32 vmexit_ctrl;
753 u32 vmentry_ctrl;
754} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755
Hannes Ederefff9e52008-11-28 17:02:06 +0100756static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800757 u32 ept;
758 u32 vpid;
759} vmx_capability;
760
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761#define VMX_SEGMENT_FIELD(seg) \
762 [VCPU_SREG_##seg] = { \
763 .selector = GUEST_##seg##_SELECTOR, \
764 .base = GUEST_##seg##_BASE, \
765 .limit = GUEST_##seg##_LIMIT, \
766 .ar_bytes = GUEST_##seg##_AR_BYTES, \
767 }
768
Mathias Krause772e0312012-08-30 01:30:19 +0200769static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770 unsigned selector;
771 unsigned base;
772 unsigned limit;
773 unsigned ar_bytes;
774} kvm_vmx_segment_fields[] = {
775 VMX_SEGMENT_FIELD(CS),
776 VMX_SEGMENT_FIELD(DS),
777 VMX_SEGMENT_FIELD(ES),
778 VMX_SEGMENT_FIELD(FS),
779 VMX_SEGMENT_FIELD(GS),
780 VMX_SEGMENT_FIELD(SS),
781 VMX_SEGMENT_FIELD(TR),
782 VMX_SEGMENT_FIELD(LDTR),
783};
784
Avi Kivity26bb0982009-09-07 11:14:12 +0300785static u64 host_efer;
786
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300787static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
788
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300789/*
Brian Gerst8c065852010-07-17 09:03:26 -0400790 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300791 * away by decrementing the array size.
792 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800794#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300795 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400797 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200799#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800800
Gui Jianfeng31299942010-03-15 17:29:09 +0800801static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802{
803 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
804 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100805 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806}
807
Gui Jianfeng31299942010-03-15 17:29:09 +0800808static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300809{
810 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
811 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100812 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300813}
814
Gui Jianfeng31299942010-03-15 17:29:09 +0800815static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500816{
817 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
818 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100819 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500820}
821
Gui Jianfeng31299942010-03-15 17:29:09 +0800822static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823{
824 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
825 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
826}
827
Gui Jianfeng31299942010-03-15 17:29:09 +0800828static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800829{
830 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
831 INTR_INFO_VALID_MASK)) ==
832 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
833}
834
Gui Jianfeng31299942010-03-15 17:29:09 +0800835static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800836{
Sheng Yang04547152009-04-01 15:52:31 +0800837 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800838}
839
Gui Jianfeng31299942010-03-15 17:29:09 +0800840static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800841{
Sheng Yang04547152009-04-01 15:52:31 +0800842 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800843}
844
Gui Jianfeng31299942010-03-15 17:29:09 +0800845static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800846{
Sheng Yang04547152009-04-01 15:52:31 +0800847 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800848}
849
Gui Jianfeng31299942010-03-15 17:29:09 +0800850static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800851{
Sheng Yang04547152009-04-01 15:52:31 +0800852 return vmcs_config.cpu_based_exec_ctrl &
853 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800854}
855
Avi Kivity774ead32007-12-26 13:57:04 +0200856static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800857{
Sheng Yang04547152009-04-01 15:52:31 +0800858 return vmcs_config.cpu_based_2nd_exec_ctrl &
859 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
860}
861
Yang Zhang8d146952013-01-25 10:18:50 +0800862static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
863{
864 return vmcs_config.cpu_based_2nd_exec_ctrl &
865 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
866}
867
Yang Zhang83d4c282013-01-25 10:18:49 +0800868static inline bool cpu_has_vmx_apic_register_virt(void)
869{
870 return vmcs_config.cpu_based_2nd_exec_ctrl &
871 SECONDARY_EXEC_APIC_REGISTER_VIRT;
872}
873
Yang Zhangc7c9c562013-01-25 10:18:51 +0800874static inline bool cpu_has_vmx_virtual_intr_delivery(void)
875{
876 return vmcs_config.cpu_based_2nd_exec_ctrl &
877 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
878}
879
Yang Zhang01e439b2013-04-11 19:25:12 +0800880static inline bool cpu_has_vmx_posted_intr(void)
881{
882 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR;
883}
884
885static inline bool cpu_has_vmx_apicv(void)
886{
887 return cpu_has_vmx_apic_register_virt() &&
888 cpu_has_vmx_virtual_intr_delivery() &&
889 cpu_has_vmx_posted_intr();
890}
891
Sheng Yang04547152009-04-01 15:52:31 +0800892static inline bool cpu_has_vmx_flexpriority(void)
893{
894 return cpu_has_vmx_tpr_shadow() &&
895 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800896}
897
Marcelo Tosattie7997942009-06-11 12:07:40 -0300898static inline bool cpu_has_vmx_ept_execute_only(void)
899{
Gui Jianfeng31299942010-03-15 17:29:09 +0800900 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300901}
902
903static inline bool cpu_has_vmx_eptp_uncacheable(void)
904{
Gui Jianfeng31299942010-03-15 17:29:09 +0800905 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300906}
907
908static inline bool cpu_has_vmx_eptp_writeback(void)
909{
Gui Jianfeng31299942010-03-15 17:29:09 +0800910 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300911}
912
913static inline bool cpu_has_vmx_ept_2m_page(void)
914{
Gui Jianfeng31299942010-03-15 17:29:09 +0800915 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300916}
917
Sheng Yang878403b2010-01-05 19:02:29 +0800918static inline bool cpu_has_vmx_ept_1g_page(void)
919{
Gui Jianfeng31299942010-03-15 17:29:09 +0800920 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800921}
922
Sheng Yang4bc9b982010-06-02 14:05:24 +0800923static inline bool cpu_has_vmx_ept_4levels(void)
924{
925 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
926}
927
Xudong Hao83c3a332012-05-28 19:33:35 +0800928static inline bool cpu_has_vmx_ept_ad_bits(void)
929{
930 return vmx_capability.ept & VMX_EPT_AD_BIT;
931}
932
Gui Jianfeng31299942010-03-15 17:29:09 +0800933static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800934{
Gui Jianfeng31299942010-03-15 17:29:09 +0800935 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800936}
937
Gui Jianfeng31299942010-03-15 17:29:09 +0800938static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800939{
Gui Jianfeng31299942010-03-15 17:29:09 +0800940 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800941}
942
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800943static inline bool cpu_has_vmx_invvpid_single(void)
944{
945 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
946}
947
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800948static inline bool cpu_has_vmx_invvpid_global(void)
949{
950 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
951}
952
Gui Jianfeng31299942010-03-15 17:29:09 +0800953static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800954{
Sheng Yang04547152009-04-01 15:52:31 +0800955 return vmcs_config.cpu_based_2nd_exec_ctrl &
956 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800957}
958
Gui Jianfeng31299942010-03-15 17:29:09 +0800959static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700960{
961 return vmcs_config.cpu_based_2nd_exec_ctrl &
962 SECONDARY_EXEC_UNRESTRICTED_GUEST;
963}
964
Gui Jianfeng31299942010-03-15 17:29:09 +0800965static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800966{
967 return vmcs_config.cpu_based_2nd_exec_ctrl &
968 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
969}
970
Gui Jianfeng31299942010-03-15 17:29:09 +0800971static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800972{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800973 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800974}
975
Gui Jianfeng31299942010-03-15 17:29:09 +0800976static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800977{
Sheng Yang04547152009-04-01 15:52:31 +0800978 return vmcs_config.cpu_based_2nd_exec_ctrl &
979 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800980}
981
Gui Jianfeng31299942010-03-15 17:29:09 +0800982static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800983{
984 return vmcs_config.cpu_based_2nd_exec_ctrl &
985 SECONDARY_EXEC_RDTSCP;
986}
987
Mao, Junjiead756a12012-07-02 01:18:48 +0000988static inline bool cpu_has_vmx_invpcid(void)
989{
990 return vmcs_config.cpu_based_2nd_exec_ctrl &
991 SECONDARY_EXEC_ENABLE_INVPCID;
992}
993
Gui Jianfeng31299942010-03-15 17:29:09 +0800994static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +0800995{
996 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
997}
998
Sheng Yangf5f48ee2010-06-30 12:25:15 +0800999static inline bool cpu_has_vmx_wbinvd_exit(void)
1000{
1001 return vmcs_config.cpu_based_2nd_exec_ctrl &
1002 SECONDARY_EXEC_WBINVD_EXITING;
1003}
1004
Abel Gordonabc4fc52013-04-18 14:35:25 +03001005static inline bool cpu_has_vmx_shadow_vmcs(void)
1006{
1007 u64 vmx_msr;
1008 rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
1009 /* check if the cpu supports writing r/o exit information fields */
1010 if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS))
1011 return false;
1012
1013 return vmcs_config.cpu_based_2nd_exec_ctrl &
1014 SECONDARY_EXEC_SHADOW_VMCS;
1015}
1016
Sheng Yang04547152009-04-01 15:52:31 +08001017static inline bool report_flexpriority(void)
1018{
1019 return flexpriority_enabled;
1020}
1021
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001022static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
1023{
1024 return vmcs12->cpu_based_vm_exec_control & bit;
1025}
1026
1027static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
1028{
1029 return (vmcs12->cpu_based_vm_exec_control &
1030 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
1031 (vmcs12->secondary_vm_exec_control & bit);
1032}
1033
Nadav Har'El644d7112011-05-25 23:12:35 +03001034static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12,
1035 struct kvm_vcpu *vcpu)
1036{
1037 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
1038}
1039
1040static inline bool is_exception(u32 intr_info)
1041{
1042 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
1043 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
1044}
1045
1046static void nested_vmx_vmexit(struct kvm_vcpu *vcpu);
Nadav Har'El7c177932011-05-25 23:12:04 +03001047static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
1048 struct vmcs12 *vmcs12,
1049 u32 reason, unsigned long qualification);
1050
Rusty Russell8b9cf982007-07-30 16:31:43 +10001051static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -08001052{
1053 int i;
1054
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001055 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +03001056 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001057 return i;
1058 return -1;
1059}
1060
Sheng Yang2384d2b2008-01-17 15:14:33 +08001061static inline void __invvpid(int ext, u16 vpid, gva_t gva)
1062{
1063 struct {
1064 u64 vpid : 16;
1065 u64 rsvd : 48;
1066 u64 gva;
1067 } operand = { vpid, 0, gva };
1068
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001069 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001070 /* CF==1 or ZF==1 --> rc = -1 */
1071 "; ja 1f ; ud2 ; 1:"
1072 : : "a"(&operand), "c"(ext) : "cc", "memory");
1073}
1074
Sheng Yang14394422008-04-28 12:24:45 +08001075static inline void __invept(int ext, u64 eptp, gpa_t gpa)
1076{
1077 struct {
1078 u64 eptp, gpa;
1079 } operand = {eptp, gpa};
1080
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001081 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +08001082 /* CF==1 or ZF==1 --> rc = -1 */
1083 "; ja 1f ; ud2 ; 1:\n"
1084 : : "a" (&operand), "c" (ext) : "cc", "memory");
1085}
1086
Avi Kivity26bb0982009-09-07 11:14:12 +03001087static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +03001088{
1089 int i;
1090
Rusty Russell8b9cf982007-07-30 16:31:43 +10001091 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +03001092 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001093 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +00001094 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -08001095}
1096
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097static void vmcs_clear(struct vmcs *vmcs)
1098{
1099 u64 phys_addr = __pa(vmcs);
1100 u8 error;
1101
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001102 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001103 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 : "cc", "memory");
1105 if (error)
1106 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
1107 vmcs, phys_addr);
1108}
1109
Nadav Har'Eld462b812011-05-24 15:26:10 +03001110static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1111{
1112 vmcs_clear(loaded_vmcs->vmcs);
1113 loaded_vmcs->cpu = -1;
1114 loaded_vmcs->launched = 0;
1115}
1116
Dongxiao Xu7725b892010-05-11 18:29:38 +08001117static void vmcs_load(struct vmcs *vmcs)
1118{
1119 u64 phys_addr = __pa(vmcs);
1120 u8 error;
1121
1122 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001123 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001124 : "cc", "memory");
1125 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001126 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001127 vmcs, phys_addr);
1128}
1129
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001130#ifdef CONFIG_KEXEC
1131/*
1132 * This bitmap is used to indicate whether the vmclear
1133 * operation is enabled on all cpus. All disabled by
1134 * default.
1135 */
1136static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1137
1138static inline void crash_enable_local_vmclear(int cpu)
1139{
1140 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1141}
1142
1143static inline void crash_disable_local_vmclear(int cpu)
1144{
1145 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1146}
1147
1148static inline int crash_local_vmclear_enabled(int cpu)
1149{
1150 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1151}
1152
1153static void crash_vmclear_local_loaded_vmcss(void)
1154{
1155 int cpu = raw_smp_processor_id();
1156 struct loaded_vmcs *v;
1157
1158 if (!crash_local_vmclear_enabled(cpu))
1159 return;
1160
1161 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1162 loaded_vmcss_on_cpu_link)
1163 vmcs_clear(v->vmcs);
1164}
1165#else
1166static inline void crash_enable_local_vmclear(int cpu) { }
1167static inline void crash_disable_local_vmclear(int cpu) { }
1168#endif /* CONFIG_KEXEC */
1169
Nadav Har'Eld462b812011-05-24 15:26:10 +03001170static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001172 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001173 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001174
Nadav Har'Eld462b812011-05-24 15:26:10 +03001175 if (loaded_vmcs->cpu != cpu)
1176 return; /* vcpu migration can race with cpu offline */
1177 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001179 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001180 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001181
1182 /*
1183 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1184 * is before setting loaded_vmcs->vcpu to -1 which is done in
1185 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1186 * then adds the vmcs into percpu list before it is deleted.
1187 */
1188 smp_wmb();
1189
Nadav Har'Eld462b812011-05-24 15:26:10 +03001190 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001191 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192}
1193
Nadav Har'Eld462b812011-05-24 15:26:10 +03001194static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001195{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001196 int cpu = loaded_vmcs->cpu;
1197
1198 if (cpu != -1)
1199 smp_call_function_single(cpu,
1200 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001201}
1202
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001203static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001204{
1205 if (vmx->vpid == 0)
1206 return;
1207
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001208 if (cpu_has_vmx_invvpid_single())
1209 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001210}
1211
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001212static inline void vpid_sync_vcpu_global(void)
1213{
1214 if (cpu_has_vmx_invvpid_global())
1215 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1216}
1217
1218static inline void vpid_sync_context(struct vcpu_vmx *vmx)
1219{
1220 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001221 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001222 else
1223 vpid_sync_vcpu_global();
1224}
1225
Sheng Yang14394422008-04-28 12:24:45 +08001226static inline void ept_sync_global(void)
1227{
1228 if (cpu_has_vmx_invept_global())
1229 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1230}
1231
1232static inline void ept_sync_context(u64 eptp)
1233{
Avi Kivity089d0342009-03-23 18:26:32 +02001234 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001235 if (cpu_has_vmx_invept_context())
1236 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1237 else
1238 ept_sync_global();
1239 }
1240}
1241
Avi Kivity96304212011-05-15 10:13:13 -04001242static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243{
Avi Kivity5e520e62011-05-15 10:13:12 -04001244 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245
Avi Kivity5e520e62011-05-15 10:13:12 -04001246 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1247 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001248 return value;
1249}
1250
Avi Kivity96304212011-05-15 10:13:13 -04001251static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252{
1253 return vmcs_readl(field);
1254}
1255
Avi Kivity96304212011-05-15 10:13:13 -04001256static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001257{
1258 return vmcs_readl(field);
1259}
1260
Avi Kivity96304212011-05-15 10:13:13 -04001261static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262{
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001263#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264 return vmcs_readl(field);
1265#else
1266 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
1267#endif
1268}
1269
Avi Kivitye52de1b2007-01-05 16:36:56 -08001270static noinline void vmwrite_error(unsigned long field, unsigned long value)
1271{
1272 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1273 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1274 dump_stack();
1275}
1276
Avi Kivity6aa8b732006-12-10 02:21:36 -08001277static void vmcs_writel(unsigned long field, unsigned long value)
1278{
1279 u8 error;
1280
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001281 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001282 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001283 if (unlikely(error))
1284 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001285}
1286
1287static void vmcs_write16(unsigned long field, u16 value)
1288{
1289 vmcs_writel(field, value);
1290}
1291
1292static void vmcs_write32(unsigned long field, u32 value)
1293{
1294 vmcs_writel(field, value);
1295}
1296
1297static void vmcs_write64(unsigned long field, u64 value)
1298{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001300#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001301 asm volatile ("");
1302 vmcs_writel(field+1, value >> 32);
1303#endif
1304}
1305
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001306static void vmcs_clear_bits(unsigned long field, u32 mask)
1307{
1308 vmcs_writel(field, vmcs_readl(field) & ~mask);
1309}
1310
1311static void vmcs_set_bits(unsigned long field, u32 mask)
1312{
1313 vmcs_writel(field, vmcs_readl(field) | mask);
1314}
1315
Avi Kivity2fb92db2011-04-27 19:42:18 +03001316static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1317{
1318 vmx->segment_cache.bitmask = 0;
1319}
1320
1321static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1322 unsigned field)
1323{
1324 bool ret;
1325 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1326
1327 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1328 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1329 vmx->segment_cache.bitmask = 0;
1330 }
1331 ret = vmx->segment_cache.bitmask & mask;
1332 vmx->segment_cache.bitmask |= mask;
1333 return ret;
1334}
1335
1336static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1337{
1338 u16 *p = &vmx->segment_cache.seg[seg].selector;
1339
1340 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1341 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1342 return *p;
1343}
1344
1345static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1346{
1347 ulong *p = &vmx->segment_cache.seg[seg].base;
1348
1349 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1350 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1351 return *p;
1352}
1353
1354static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1355{
1356 u32 *p = &vmx->segment_cache.seg[seg].limit;
1357
1358 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1359 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1360 return *p;
1361}
1362
1363static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1364{
1365 u32 *p = &vmx->segment_cache.seg[seg].ar;
1366
1367 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1368 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1369 return *p;
1370}
1371
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001372static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1373{
1374 u32 eb;
1375
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001376 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1377 (1u << NM_VECTOR) | (1u << DB_VECTOR);
1378 if ((vcpu->guest_debug &
1379 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1380 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1381 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001382 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001383 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001384 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001385 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001386 if (vcpu->fpu_active)
1387 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001388
1389 /* When we are running a nested L2 guest and L1 specified for it a
1390 * certain exception bitmap, we must trap the same exceptions and pass
1391 * them to L1. When running L2, we will only handle the exceptions
1392 * specified above if L1 did not want them.
1393 */
1394 if (is_guest_mode(vcpu))
1395 eb |= get_vmcs12(vcpu)->exception_bitmap;
1396
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001397 vmcs_write32(EXCEPTION_BITMAP, eb);
1398}
1399
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001400static void clear_atomic_switch_msr_special(unsigned long entry,
1401 unsigned long exit)
1402{
1403 vmcs_clear_bits(VM_ENTRY_CONTROLS, entry);
1404 vmcs_clear_bits(VM_EXIT_CONTROLS, exit);
1405}
1406
Avi Kivity61d2ef22010-04-28 16:40:38 +03001407static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1408{
1409 unsigned i;
1410 struct msr_autoload *m = &vmx->msr_autoload;
1411
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001412 switch (msr) {
1413 case MSR_EFER:
1414 if (cpu_has_load_ia32_efer) {
1415 clear_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
1416 VM_EXIT_LOAD_IA32_EFER);
1417 return;
1418 }
1419 break;
1420 case MSR_CORE_PERF_GLOBAL_CTRL:
1421 if (cpu_has_load_perf_global_ctrl) {
1422 clear_atomic_switch_msr_special(
1423 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1424 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1425 return;
1426 }
1427 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001428 }
1429
Avi Kivity61d2ef22010-04-28 16:40:38 +03001430 for (i = 0; i < m->nr; ++i)
1431 if (m->guest[i].index == msr)
1432 break;
1433
1434 if (i == m->nr)
1435 return;
1436 --m->nr;
1437 m->guest[i] = m->guest[m->nr];
1438 m->host[i] = m->host[m->nr];
1439 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1440 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1441}
1442
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001443static void add_atomic_switch_msr_special(unsigned long entry,
1444 unsigned long exit, unsigned long guest_val_vmcs,
1445 unsigned long host_val_vmcs, u64 guest_val, u64 host_val)
1446{
1447 vmcs_write64(guest_val_vmcs, guest_val);
1448 vmcs_write64(host_val_vmcs, host_val);
1449 vmcs_set_bits(VM_ENTRY_CONTROLS, entry);
1450 vmcs_set_bits(VM_EXIT_CONTROLS, exit);
1451}
1452
Avi Kivity61d2ef22010-04-28 16:40:38 +03001453static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1454 u64 guest_val, u64 host_val)
1455{
1456 unsigned i;
1457 struct msr_autoload *m = &vmx->msr_autoload;
1458
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001459 switch (msr) {
1460 case MSR_EFER:
1461 if (cpu_has_load_ia32_efer) {
1462 add_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
1463 VM_EXIT_LOAD_IA32_EFER,
1464 GUEST_IA32_EFER,
1465 HOST_IA32_EFER,
1466 guest_val, host_val);
1467 return;
1468 }
1469 break;
1470 case MSR_CORE_PERF_GLOBAL_CTRL:
1471 if (cpu_has_load_perf_global_ctrl) {
1472 add_atomic_switch_msr_special(
1473 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1474 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1475 GUEST_IA32_PERF_GLOBAL_CTRL,
1476 HOST_IA32_PERF_GLOBAL_CTRL,
1477 guest_val, host_val);
1478 return;
1479 }
1480 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001481 }
1482
Avi Kivity61d2ef22010-04-28 16:40:38 +03001483 for (i = 0; i < m->nr; ++i)
1484 if (m->guest[i].index == msr)
1485 break;
1486
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001487 if (i == NR_AUTOLOAD_MSRS) {
1488 printk_once(KERN_WARNING"Not enough mst switch entries. "
1489 "Can't add msr %x\n", msr);
1490 return;
1491 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001492 ++m->nr;
1493 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1494 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1495 }
1496
1497 m->guest[i].index = msr;
1498 m->guest[i].value = guest_val;
1499 m->host[i].index = msr;
1500 m->host[i].value = host_val;
1501}
1502
Avi Kivity33ed6322007-05-02 16:54:03 +03001503static void reload_tss(void)
1504{
Avi Kivity33ed6322007-05-02 16:54:03 +03001505 /*
1506 * VT restores TR but not its size. Useless.
1507 */
Avi Kivityd3591922010-07-26 18:32:39 +03001508 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001509 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001510
Avi Kivityd3591922010-07-26 18:32:39 +03001511 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001512 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1513 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001514}
1515
Avi Kivity92c0d902009-10-29 11:00:16 +02001516static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001517{
Roel Kluin3a34a882009-08-04 02:08:45 -07001518 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001519 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +03001520
Avi Kivityf6801df2010-01-21 15:31:50 +02001521 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -07001522
Avi Kivity51c6cf62007-08-29 03:48:05 +03001523 /*
Guo Chao0fa06072012-06-28 15:16:19 +08001524 * NX is emulated; LMA and LME handled by hardware; SCE meaningless
Avi Kivity51c6cf62007-08-29 03:48:05 +03001525 * outside long mode
1526 */
1527 ignore_bits = EFER_NX | EFER_SCE;
1528#ifdef CONFIG_X86_64
1529 ignore_bits |= EFER_LMA | EFER_LME;
1530 /* SCE is meaningful only in long mode on Intel */
1531 if (guest_efer & EFER_LMA)
1532 ignore_bits &= ~(u64)EFER_SCE;
1533#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +03001534 guest_efer &= ~ignore_bits;
1535 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +03001536 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +02001537 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03001538
1539 clear_atomic_switch_msr(vmx, MSR_EFER);
1540 /* On ept, can't emulate nx, and must switch nx atomically */
1541 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
1542 guest_efer = vmx->vcpu.arch.efer;
1543 if (!(guest_efer & EFER_LMA))
1544 guest_efer &= ~EFER_LME;
1545 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
1546 return false;
1547 }
1548
Avi Kivity26bb0982009-09-07 11:14:12 +03001549 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001550}
1551
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001552static unsigned long segment_base(u16 selector)
1553{
Avi Kivityd3591922010-07-26 18:32:39 +03001554 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001555 struct desc_struct *d;
1556 unsigned long table_base;
1557 unsigned long v;
1558
1559 if (!(selector & ~3))
1560 return 0;
1561
Avi Kivityd3591922010-07-26 18:32:39 +03001562 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001563
1564 if (selector & 4) { /* from ldt */
1565 u16 ldt_selector = kvm_read_ldt();
1566
1567 if (!(ldt_selector & ~3))
1568 return 0;
1569
1570 table_base = segment_base(ldt_selector);
1571 }
1572 d = (struct desc_struct *)(table_base + (selector & ~7));
1573 v = get_desc_base(d);
1574#ifdef CONFIG_X86_64
1575 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
1576 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
1577#endif
1578 return v;
1579}
1580
1581static inline unsigned long kvm_read_tr_base(void)
1582{
1583 u16 tr;
1584 asm("str %0" : "=g"(tr));
1585 return segment_base(tr);
1586}
1587
Avi Kivity04d2cc72007-09-10 18:10:54 +03001588static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001589{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001590 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001591 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001592
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001593 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001594 return;
1595
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001596 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001597 /*
1598 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1599 * allow segment selectors with cpl > 0 or ti == 1.
1600 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001601 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02001602 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02001603 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001604 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001605 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001606 vmx->host_state.fs_reload_needed = 0;
1607 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03001608 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001609 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001610 }
Avi Kivity9581d442010-10-19 16:46:55 +02001611 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001612 if (!(vmx->host_state.gs_sel & 7))
1613 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001614 else {
1615 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001616 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001617 }
1618
1619#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03001620 savesegment(ds, vmx->host_state.ds_sel);
1621 savesegment(es, vmx->host_state.es_sel);
1622#endif
1623
1624#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03001625 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1626 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1627#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001628 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1629 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03001630#endif
Avi Kivity707c0872007-05-02 17:33:43 +03001631
1632#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001633 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1634 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03001635 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03001636#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03001637 for (i = 0; i < vmx->save_nmsrs; ++i)
1638 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02001639 vmx->guest_msrs[i].data,
1640 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03001641}
1642
Avi Kivitya9b21b62008-06-24 11:48:49 +03001643static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001644{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001645 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001646 return;
1647
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001648 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001649 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02001650#ifdef CONFIG_X86_64
1651 if (is_long_mode(&vmx->vcpu))
1652 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1653#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001654 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001655 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001656#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02001657 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001658#else
1659 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001660#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001661 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02001662 if (vmx->host_state.fs_reload_needed)
1663 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001664#ifdef CONFIG_X86_64
1665 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
1666 loadsegment(ds, vmx->host_state.ds_sel);
1667 loadsegment(es, vmx->host_state.es_sel);
1668 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001669#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001670 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001671#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001672 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001673#endif
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07001674 /*
1675 * If the FPU is not active (through the host task or
1676 * the guest vcpu), then restore the cr0.TS bit.
1677 */
1678 if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded)
1679 stts();
Avi Kivity3444d7d2010-07-26 18:32:38 +03001680 load_gdt(&__get_cpu_var(host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03001681}
1682
Avi Kivitya9b21b62008-06-24 11:48:49 +03001683static void vmx_load_host_state(struct vcpu_vmx *vmx)
1684{
1685 preempt_disable();
1686 __vmx_load_host_state(vmx);
1687 preempt_enable();
1688}
1689
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690/*
1691 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1692 * vcpu mutex is already taken.
1693 */
Avi Kivity15ad7142007-07-11 18:17:21 +03001694static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001696 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001697 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001699 if (!vmm_exclusive)
1700 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001701 else if (vmx->loaded_vmcs->cpu != cpu)
1702 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703
Nadav Har'Eld462b812011-05-24 15:26:10 +03001704 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1705 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1706 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001707 }
1708
Nadav Har'Eld462b812011-05-24 15:26:10 +03001709 if (vmx->loaded_vmcs->cpu != cpu) {
Avi Kivityd3591922010-07-26 18:32:39 +03001710 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711 unsigned long sysenter_esp;
1712
Avi Kivitya8eeb042010-05-10 12:34:53 +03001713 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001714 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001715 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001716
1717 /*
1718 * Read loaded_vmcs->cpu should be before fetching
1719 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1720 * See the comments in __loaded_vmcs_clear().
1721 */
1722 smp_rmb();
1723
Nadav Har'Eld462b812011-05-24 15:26:10 +03001724 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1725 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001726 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001727 local_irq_enable();
1728
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729 /*
1730 * Linux uses per-cpu TSS and GDT, so set these when switching
1731 * processors.
1732 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001733 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03001734 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735
1736 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1737 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Nadav Har'Eld462b812011-05-24 15:26:10 +03001738 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740}
1741
1742static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1743{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001744 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001745 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001746 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1747 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001748 kvm_cpu_vmxoff();
1749 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750}
1751
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001752static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1753{
Avi Kivity81231c62010-01-24 16:26:40 +02001754 ulong cr0;
1755
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001756 if (vcpu->fpu_active)
1757 return;
1758 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001759 cr0 = vmcs_readl(GUEST_CR0);
1760 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1761 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1762 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001763 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001764 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001765 if (is_guest_mode(vcpu))
1766 vcpu->arch.cr0_guest_owned_bits &=
1767 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02001768 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001769}
1770
Avi Kivityedcafe32009-12-30 18:07:40 +02001771static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1772
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001773/*
1774 * Return the cr0 value that a nested guest would read. This is a combination
1775 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1776 * its hypervisor (cr0_read_shadow).
1777 */
1778static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
1779{
1780 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
1781 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
1782}
1783static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
1784{
1785 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
1786 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
1787}
1788
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001789static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1790{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001791 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
1792 * set this *before* calling this function.
1793 */
Avi Kivityedcafe32009-12-30 18:07:40 +02001794 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001795 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001796 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001797 vcpu->arch.cr0_guest_owned_bits = 0;
1798 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001799 if (is_guest_mode(vcpu)) {
1800 /*
1801 * L1's specified read shadow might not contain the TS bit,
1802 * so now that we turned on shadowing of this bit, we need to
1803 * set this bit of the shadow. Like in nested_vmx_run we need
1804 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
1805 * up-to-date here because we just decached cr0.TS (and we'll
1806 * only update vmcs12->guest_cr0 on nested exit).
1807 */
1808 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1809 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
1810 (vcpu->arch.cr0 & X86_CR0_TS);
1811 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
1812 } else
1813 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001814}
1815
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1817{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001818 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001819
Avi Kivity6de12732011-03-07 12:51:22 +02001820 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1821 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1822 rflags = vmcs_readl(GUEST_RFLAGS);
1823 if (to_vmx(vcpu)->rmode.vm86_active) {
1824 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1825 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1826 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1827 }
1828 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001829 }
Avi Kivity6de12732011-03-07 12:51:22 +02001830 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831}
1832
1833static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1834{
Avi Kivity6de12732011-03-07 12:51:22 +02001835 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1836 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001837 if (to_vmx(vcpu)->rmode.vm86_active) {
1838 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001839 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001840 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001841 vmcs_writel(GUEST_RFLAGS, rflags);
1842}
1843
Glauber Costa2809f5d2009-05-12 16:21:05 -04001844static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1845{
1846 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1847 int ret = 0;
1848
1849 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001850 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001851 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001852 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001853
1854 return ret & mask;
1855}
1856
1857static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1858{
1859 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1860 u32 interruptibility = interruptibility_old;
1861
1862 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1863
Jan Kiszka48005f62010-02-19 19:38:07 +01001864 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001865 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001866 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001867 interruptibility |= GUEST_INTR_STATE_STI;
1868
1869 if ((interruptibility != interruptibility_old))
1870 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1871}
1872
Avi Kivity6aa8b732006-12-10 02:21:36 -08001873static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1874{
1875 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001876
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001877 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001879 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880
Glauber Costa2809f5d2009-05-12 16:21:05 -04001881 /* skipping an emulated instruction also counts */
1882 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883}
1884
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001885/*
1886 * KVM wants to inject page-faults which it got to the guest. This function
1887 * checks whether in a nested guest, we need to inject them to L1 or L2.
1888 * This function assumes it is called with the exit reason in vmcs02 being
1889 * a #PF exception (this is the only case in which KVM injects a #PF when L2
1890 * is running).
1891 */
1892static int nested_pf_handled(struct kvm_vcpu *vcpu)
1893{
1894 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1895
1896 /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
Nadav Har'El95871902012-03-06 16:39:22 +02001897 if (!(vmcs12->exception_bitmap & (1u << PF_VECTOR)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001898 return 0;
1899
1900 nested_vmx_vmexit(vcpu);
1901 return 1;
1902}
1903
Avi Kivity298101d2007-11-25 13:41:11 +02001904static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02001905 bool has_error_code, u32 error_code,
1906 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02001907{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001908 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001909 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001910
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001911 if (nr == PF_VECTOR && is_guest_mode(vcpu) &&
1912 nested_pf_handled(vcpu))
1913 return;
1914
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001915 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001916 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001917 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1918 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001919
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001920 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001921 int inc_eip = 0;
1922 if (kvm_exception_is_soft(nr))
1923 inc_eip = vcpu->arch.event_exit_inst_len;
1924 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02001925 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001926 return;
1927 }
1928
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001929 if (kvm_exception_is_soft(nr)) {
1930 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1931 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001932 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1933 } else
1934 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1935
1936 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02001937}
1938
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001939static bool vmx_rdtscp_supported(void)
1940{
1941 return cpu_has_vmx_rdtscp();
1942}
1943
Mao, Junjiead756a12012-07-02 01:18:48 +00001944static bool vmx_invpcid_supported(void)
1945{
1946 return cpu_has_vmx_invpcid() && enable_ept;
1947}
1948
Avi Kivity6aa8b732006-12-10 02:21:36 -08001949/*
Eddie Donga75beee2007-05-17 18:55:15 +03001950 * Swap MSR entry in host/guest MSR entry array.
1951 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001952static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001953{
Avi Kivity26bb0982009-09-07 11:14:12 +03001954 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001955
1956 tmp = vmx->guest_msrs[to];
1957 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1958 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001959}
1960
Yang Zhang8d146952013-01-25 10:18:50 +08001961static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
1962{
1963 unsigned long *msr_bitmap;
1964
1965 if (irqchip_in_kernel(vcpu->kvm) && apic_x2apic_mode(vcpu->arch.apic)) {
1966 if (is_long_mode(vcpu))
1967 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
1968 else
1969 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
1970 } else {
1971 if (is_long_mode(vcpu))
1972 msr_bitmap = vmx_msr_bitmap_longmode;
1973 else
1974 msr_bitmap = vmx_msr_bitmap_legacy;
1975 }
1976
1977 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1978}
1979
Eddie Donga75beee2007-05-17 18:55:15 +03001980/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001981 * Set up the vmcs to automatically save and restore system
1982 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1983 * mode, as fiddling with msrs is very expensive.
1984 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001985static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001986{
Avi Kivity26bb0982009-09-07 11:14:12 +03001987 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03001988
Eddie Donga75beee2007-05-17 18:55:15 +03001989 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001990#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001991 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10001992 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03001993 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001994 move_msr_up(vmx, index, save_nmsrs++);
1995 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001996 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001997 move_msr_up(vmx, index, save_nmsrs++);
1998 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001999 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10002000 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002001 index = __find_msr_index(vmx, MSR_TSC_AUX);
2002 if (index >= 0 && vmx->rdtscp_enabled)
2003 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03002004 /*
Brian Gerst8c065852010-07-17 09:03:26 -04002005 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03002006 * if efer.sce is enabled.
2007 */
Brian Gerst8c065852010-07-17 09:03:26 -04002008 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02002009 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10002010 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002011 }
Eddie Donga75beee2007-05-17 18:55:15 +03002012#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02002013 index = __find_msr_index(vmx, MSR_EFER);
2014 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03002015 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03002016
Avi Kivity26bb0982009-09-07 11:14:12 +03002017 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02002018
Yang Zhang8d146952013-01-25 10:18:50 +08002019 if (cpu_has_vmx_msr_bitmap())
2020 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03002021}
2022
2023/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002024 * reads and returns guest's timestamp counter "register"
2025 * guest_tsc = host_tsc + tsc_offset -- 21.3
2026 */
2027static u64 guest_read_tsc(void)
2028{
2029 u64 host_tsc, tsc_offset;
2030
2031 rdtscll(host_tsc);
2032 tsc_offset = vmcs_read64(TSC_OFFSET);
2033 return host_tsc + tsc_offset;
2034}
2035
2036/*
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002037 * Like guest_read_tsc, but always returns L1's notion of the timestamp
2038 * counter, even if a nested guest (L2) is currently running.
2039 */
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002040u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002041{
Marcelo Tosatti886b4702012-11-27 23:28:58 -02002042 u64 tsc_offset;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002043
Nadav Har'Eld5c17852011-08-02 15:54:20 +03002044 tsc_offset = is_guest_mode(vcpu) ?
2045 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
2046 vmcs_read64(TSC_OFFSET);
2047 return host_tsc + tsc_offset;
2048}
2049
2050/*
Zachary Amsdencc578282012-02-03 15:43:50 -02002051 * Engage any workarounds for mis-matched TSC rates. Currently limited to
2052 * software catchup for faster rates on slower CPUs.
Joerg Roedel4051b182011-03-25 09:44:49 +01002053 */
Zachary Amsdencc578282012-02-03 15:43:50 -02002054static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +01002055{
Zachary Amsdencc578282012-02-03 15:43:50 -02002056 if (!scale)
2057 return;
2058
2059 if (user_tsc_khz > tsc_khz) {
2060 vcpu->arch.tsc_catchup = 1;
2061 vcpu->arch.tsc_always_catchup = 1;
2062 } else
2063 WARN(1, "user requested TSC rate below hardware speed\n");
Joerg Roedel4051b182011-03-25 09:44:49 +01002064}
2065
Will Auldba904632012-11-29 12:42:50 -08002066static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
2067{
2068 return vmcs_read64(TSC_OFFSET);
2069}
2070
Joerg Roedel4051b182011-03-25 09:44:49 +01002071/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10002072 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08002073 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10002074static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002075{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002076 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03002077 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002078 * We're here if L1 chose not to trap WRMSR to TSC. According
2079 * to the spec, this should set L1's TSC; The offset that L1
2080 * set for L2 remains unchanged, and still needs to be added
2081 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03002082 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03002083 struct vmcs12 *vmcs12;
2084 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
2085 /* recalculate vmcs02.TSC_OFFSET: */
2086 vmcs12 = get_vmcs12(vcpu);
2087 vmcs_write64(TSC_OFFSET, offset +
2088 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
2089 vmcs12->tsc_offset : 0));
2090 } else {
2091 vmcs_write64(TSC_OFFSET, offset);
2092 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093}
2094
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02002095static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10002096{
2097 u64 offset = vmcs_read64(TSC_OFFSET);
2098 vmcs_write64(TSC_OFFSET, offset + adjustment);
Nadav Har'El79918252011-05-25 23:15:39 +03002099 if (is_guest_mode(vcpu)) {
2100 /* Even when running L2, the adjustment needs to apply to L1 */
2101 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
2102 }
Zachary Amsdene48672f2010-08-19 22:07:23 -10002103}
2104
Joerg Roedel857e4092011-03-25 09:44:50 +01002105static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
2106{
2107 return target_tsc - native_read_tsc();
2108}
2109
Nadav Har'El801d3422011-05-25 23:02:23 +03002110static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
2111{
2112 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
2113 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2114}
2115
2116/*
2117 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2118 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2119 * all guests if the "nested" module option is off, and can also be disabled
2120 * for a single guest by disabling its VMX cpuid bit.
2121 */
2122static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2123{
2124 return nested && guest_cpuid_has_vmx(vcpu);
2125}
2126
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002128 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2129 * returned for the various VMX controls MSRs when nested VMX is enabled.
2130 * The same values should also be used to verify that vmcs12 control fields are
2131 * valid during nested entry from L1 to L2.
2132 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2133 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2134 * bit in the high half is on if the corresponding bit in the control field
2135 * may be on. See also vmx_control_verify().
2136 * TODO: allow these variables to be modified (downgraded) by module options
2137 * or other means.
2138 */
2139static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
2140static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
2141static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
2142static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
2143static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002144static u32 nested_vmx_misc_low, nested_vmx_misc_high;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002145static __init void nested_vmx_setup_ctls_msrs(void)
2146{
2147 /*
2148 * Note that as a general rule, the high half of the MSRs (bits in
2149 * the control fields which may be 1) should be initialized by the
2150 * intersection of the underlying hardware's MSR (i.e., features which
2151 * can be supported) and the list of features we want to expose -
2152 * because they are known to be properly supported in our code.
2153 * Also, usually, the low half of the MSRs (bits which must be 1) can
2154 * be set to 0, meaning that L1 may turn off any of these bits. The
2155 * reason is that if one of these bits is necessary, it will appear
2156 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2157 * fields of vmcs01 and vmcs02, will turn these bits off - and
2158 * nested_vmx_exit_handled() will not pass related exits to L1.
2159 * These rules have exceptions below.
2160 */
2161
2162 /* pin-based controls */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002163 rdmsr(MSR_IA32_VMX_PINBASED_CTLS,
2164 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002165 /*
2166 * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
2167 * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
2168 */
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002169 nested_vmx_pinbased_ctls_low |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
2170 nested_vmx_pinbased_ctls_high &= PIN_BASED_EXT_INTR_MASK |
Jan Kiszka0238ea92013-03-13 11:31:24 +01002171 PIN_BASED_NMI_EXITING | PIN_BASED_VIRTUAL_NMIS |
2172 PIN_BASED_VMX_PREEMPTION_TIMER;
Jan Kiszkaeabeaac2013-03-13 11:30:50 +01002173 nested_vmx_pinbased_ctls_high |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002174
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002175 /*
2176 * Exit controls
2177 * If bit 55 of VMX_BASIC is off, bits 0-8 and 10, 11, 13, 14, 16 and
2178 * 17 must be 1.
2179 */
2180 nested_vmx_exit_ctls_low = VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb6f12502011-05-25 23:13:06 +03002181 /* Note that guest use of VM_EXIT_ACK_INTR_ON_EXIT is not supported. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002182#ifdef CONFIG_X86_64
2183 nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE;
2184#else
2185 nested_vmx_exit_ctls_high = 0;
2186#endif
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002187 nested_vmx_exit_ctls_high |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002188
2189 /* entry controls */
2190 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
2191 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002192 /* If bit 55 of VMX_BASIC is off, bits 0-8 and 12 must be 1. */
2193 nested_vmx_entry_ctls_low = VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002194 nested_vmx_entry_ctls_high &=
2195 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE;
Jan Kiszka33fb20c2013-03-06 15:44:03 +01002196 nested_vmx_entry_ctls_high |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002197
2198 /* cpu-based controls */
2199 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
2200 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
2201 nested_vmx_procbased_ctls_low = 0;
2202 nested_vmx_procbased_ctls_high &=
2203 CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING |
2204 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2205 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2206 CPU_BASED_CR3_STORE_EXITING |
2207#ifdef CONFIG_X86_64
2208 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2209#endif
2210 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
2211 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
Avi Kivitydbcb4e72012-08-13 15:38:22 +03002212 CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING |
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002213 CPU_BASED_PAUSE_EXITING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002214 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2215 /*
2216 * We can allow some features even when not supported by the
2217 * hardware. For example, L1 can specify an MSR bitmap - and we
2218 * can use it to avoid exits to L1 - even when L0 runs L2
2219 * without MSR bitmaps.
2220 */
2221 nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
2222
2223 /* secondary cpu-based controls */
2224 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
2225 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
2226 nested_vmx_secondary_ctls_low = 0;
2227 nested_vmx_secondary_ctls_high &=
Jan Kiszkad6851fb2013-02-23 22:34:39 +01002228 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
2229 SECONDARY_EXEC_WBINVD_EXITING;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002230
2231 /* miscellaneous data */
2232 rdmsr(MSR_IA32_VMX_MISC, nested_vmx_misc_low, nested_vmx_misc_high);
Jan Kiszka0238ea92013-03-13 11:31:24 +01002233 nested_vmx_misc_low &= VMX_MISC_PREEMPTION_TIMER_RATE_MASK |
2234 VMX_MISC_SAVE_EFER_LMA;
Jan Kiszkac18911a2013-03-13 16:06:41 +01002235 nested_vmx_misc_high = 0;
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002236}
2237
2238static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2239{
2240 /*
2241 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2242 */
2243 return ((control & high) | low) == control;
2244}
2245
2246static inline u64 vmx_control_msr(u32 low, u32 high)
2247{
2248 return low | ((u64)high << 32);
2249}
2250
2251/*
2252 * If we allow our guest to use VMX instructions (i.e., nested VMX), we should
2253 * also let it use VMX-specific MSRs.
2254 * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a
2255 * VMX-specific MSR, or 0 when we haven't (and the caller should handle it
2256 * like all other MSRs).
2257 */
2258static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2259{
2260 if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC &&
2261 msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) {
2262 /*
2263 * According to the spec, processors which do not support VMX
2264 * should throw a #GP(0) when VMX capability MSRs are read.
2265 */
2266 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
2267 return 1;
2268 }
2269
2270 switch (msr_index) {
2271 case MSR_IA32_FEATURE_CONTROL:
2272 *pdata = 0;
2273 break;
2274 case MSR_IA32_VMX_BASIC:
2275 /*
2276 * This MSR reports some information about VMX support. We
2277 * should return information about the VMX we emulate for the
2278 * guest, and the VMCS structure we give it - not about the
2279 * VMX support of the underlying hardware.
2280 */
2281 *pdata = VMCS12_REVISION |
2282 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2283 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2284 break;
2285 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2286 case MSR_IA32_VMX_PINBASED_CTLS:
2287 *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
2288 nested_vmx_pinbased_ctls_high);
2289 break;
2290 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2291 case MSR_IA32_VMX_PROCBASED_CTLS:
2292 *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
2293 nested_vmx_procbased_ctls_high);
2294 break;
2295 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2296 case MSR_IA32_VMX_EXIT_CTLS:
2297 *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
2298 nested_vmx_exit_ctls_high);
2299 break;
2300 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2301 case MSR_IA32_VMX_ENTRY_CTLS:
2302 *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
2303 nested_vmx_entry_ctls_high);
2304 break;
2305 case MSR_IA32_VMX_MISC:
Jan Kiszkac18911a2013-03-13 16:06:41 +01002306 *pdata = vmx_control_msr(nested_vmx_misc_low,
2307 nested_vmx_misc_high);
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002308 break;
2309 /*
2310 * These MSRs specify bits which the guest must keep fixed (on or off)
2311 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2312 * We picked the standard core2 setting.
2313 */
2314#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2315#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2316 case MSR_IA32_VMX_CR0_FIXED0:
2317 *pdata = VMXON_CR0_ALWAYSON;
2318 break;
2319 case MSR_IA32_VMX_CR0_FIXED1:
2320 *pdata = -1ULL;
2321 break;
2322 case MSR_IA32_VMX_CR4_FIXED0:
2323 *pdata = VMXON_CR4_ALWAYSON;
2324 break;
2325 case MSR_IA32_VMX_CR4_FIXED1:
2326 *pdata = -1ULL;
2327 break;
2328 case MSR_IA32_VMX_VMCS_ENUM:
2329 *pdata = 0x1f;
2330 break;
2331 case MSR_IA32_VMX_PROCBASED_CTLS2:
2332 *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
2333 nested_vmx_secondary_ctls_high);
2334 break;
2335 case MSR_IA32_VMX_EPT_VPID_CAP:
2336 /* Currently, no nested ept or nested vpid */
2337 *pdata = 0;
2338 break;
2339 default:
2340 return 0;
2341 }
2342
2343 return 1;
2344}
2345
2346static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
2347{
2348 if (!nested_vmx_allowed(vcpu))
2349 return 0;
2350
2351 if (msr_index == MSR_IA32_FEATURE_CONTROL)
2352 /* TODO: the right thing. */
2353 return 1;
2354 /*
2355 * No need to treat VMX capability MSRs specially: If we don't handle
2356 * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
2357 */
2358 return 0;
2359}
2360
2361/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362 * Reads an msr value (of 'msr_index') into 'pdata'.
2363 * Returns 0 on success, non-0 otherwise.
2364 * Assumes vcpu_load() was already called.
2365 */
2366static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2367{
2368 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03002369 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370
2371 if (!pdata) {
2372 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
2373 return -EINVAL;
2374 }
2375
2376 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002377#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378 case MSR_FS_BASE:
2379 data = vmcs_readl(GUEST_FS_BASE);
2380 break;
2381 case MSR_GS_BASE:
2382 data = vmcs_readl(GUEST_GS_BASE);
2383 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002384 case MSR_KERNEL_GS_BASE:
2385 vmx_load_host_state(to_vmx(vcpu));
2386 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
2387 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002388#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002390 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302391 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002392 data = guest_read_tsc();
2393 break;
2394 case MSR_IA32_SYSENTER_CS:
2395 data = vmcs_read32(GUEST_SYSENTER_CS);
2396 break;
2397 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002398 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399 break;
2400 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002401 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002403 case MSR_TSC_AUX:
2404 if (!to_vmx(vcpu)->rdtscp_enabled)
2405 return 1;
2406 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002407 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002408 if (vmx_get_vmx_msr(vcpu, msr_index, pdata))
2409 return 0;
Rusty Russell8b9cf982007-07-30 16:31:43 +10002410 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002411 if (msr) {
2412 data = msr->data;
2413 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002414 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002415 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002416 }
2417
2418 *pdata = data;
2419 return 0;
2420}
2421
2422/*
2423 * Writes msr value into into the appropriate "register".
2424 * Returns 0 on success, non-0 otherwise.
2425 * Assumes vcpu_load() was already called.
2426 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002427static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002429 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002430 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002431 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002432 u32 msr_index = msr_info->index;
2433 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03002434
Avi Kivity6aa8b732006-12-10 02:21:36 -08002435 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002436 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002437 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002438 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002439#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002440 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002441 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442 vmcs_writel(GUEST_FS_BASE, data);
2443 break;
2444 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002445 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002446 vmcs_writel(GUEST_GS_BASE, data);
2447 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002448 case MSR_KERNEL_GS_BASE:
2449 vmx_load_host_state(vmx);
2450 vmx->msr_guest_kernel_gs_base = data;
2451 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002452#endif
2453 case MSR_IA32_SYSENTER_CS:
2454 vmcs_write32(GUEST_SYSENTER_CS, data);
2455 break;
2456 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002457 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002458 break;
2459 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002460 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002461 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302462 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08002463 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002464 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002465 case MSR_IA32_CR_PAT:
2466 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2467 vmcs_write64(GUEST_IA32_PAT, data);
2468 vcpu->arch.pat = data;
2469 break;
2470 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002471 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002472 break;
Will Auldba904632012-11-29 12:42:50 -08002473 case MSR_IA32_TSC_ADJUST:
2474 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002475 break;
2476 case MSR_TSC_AUX:
2477 if (!vmx->rdtscp_enabled)
2478 return 1;
2479 /* Check reserved bit, higher 32 bits should be zero */
2480 if ((data >> 32) != 0)
2481 return 1;
2482 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002483 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002484 if (vmx_set_vmx_msr(vcpu, msr_index, data))
2485 break;
Rusty Russell8b9cf982007-07-30 16:31:43 +10002486 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002487 if (msr) {
2488 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002489 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2490 preempt_disable();
Avi Kivity9ee73972012-03-06 14:16:33 +02002491 kvm_set_shared_msr(msr->index, msr->data,
2492 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03002493 preempt_enable();
2494 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002495 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002497 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498 }
2499
Eddie Dong2cc51562007-05-21 07:28:09 +03002500 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002501}
2502
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002503static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002505 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2506 switch (reg) {
2507 case VCPU_REGS_RSP:
2508 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2509 break;
2510 case VCPU_REGS_RIP:
2511 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2512 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002513 case VCPU_EXREG_PDPTR:
2514 if (enable_ept)
2515 ept_save_pdptrs(vcpu);
2516 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002517 default:
2518 break;
2519 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520}
2521
Avi Kivity6aa8b732006-12-10 02:21:36 -08002522static __init int cpu_has_kvm_support(void)
2523{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002524 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525}
2526
2527static __init int vmx_disabled_by_bios(void)
2528{
2529 u64 msr;
2530
2531 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04002532 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08002533 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04002534 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2535 && tboot_enabled())
2536 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08002537 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04002538 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08002539 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08002540 && !tboot_enabled()) {
2541 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08002542 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04002543 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08002544 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08002545 /* launched w/o TXT and VMX disabled */
2546 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2547 && !tboot_enabled())
2548 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04002549 }
2550
2551 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552}
2553
Dongxiao Xu7725b892010-05-11 18:29:38 +08002554static void kvm_cpu_vmxon(u64 addr)
2555{
2556 asm volatile (ASM_VMX_VMXON_RAX
2557 : : "a"(&addr), "m"(addr)
2558 : "memory", "cc");
2559}
2560
Alexander Graf10474ae2009-09-15 11:37:46 +02002561static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002562{
2563 int cpu = raw_smp_processor_id();
2564 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04002565 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566
Alexander Graf10474ae2009-09-15 11:37:46 +02002567 if (read_cr4() & X86_CR4_VMXE)
2568 return -EBUSY;
2569
Nadav Har'Eld462b812011-05-24 15:26:10 +03002570 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002571
2572 /*
2573 * Now we can enable the vmclear operation in kdump
2574 * since the loaded_vmcss_on_cpu list on this cpu
2575 * has been initialized.
2576 *
2577 * Though the cpu is not in VMX operation now, there
2578 * is no problem to enable the vmclear operation
2579 * for the loaded_vmcss_on_cpu list is empty!
2580 */
2581 crash_enable_local_vmclear(cpu);
2582
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04002584
2585 test_bits = FEATURE_CONTROL_LOCKED;
2586 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2587 if (tboot_enabled())
2588 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2589
2590 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002591 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04002592 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2593 }
Rusty Russell66aee912007-07-17 23:34:16 +10002594 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02002595
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002596 if (vmm_exclusive) {
2597 kvm_cpu_vmxon(phys_addr);
2598 ept_sync_global();
2599 }
Alexander Graf10474ae2009-09-15 11:37:46 +02002600
Avi Kivity3444d7d2010-07-26 18:32:38 +03002601 store_gdt(&__get_cpu_var(host_gdt));
2602
Alexander Graf10474ae2009-09-15 11:37:46 +02002603 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604}
2605
Nadav Har'Eld462b812011-05-24 15:26:10 +03002606static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002607{
2608 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002609 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002610
Nadav Har'Eld462b812011-05-24 15:26:10 +03002611 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2612 loaded_vmcss_on_cpu_link)
2613 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002614}
2615
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002616
2617/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2618 * tricks.
2619 */
2620static void kvm_cpu_vmxoff(void)
2621{
2622 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002623}
2624
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625static void hardware_disable(void *garbage)
2626{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002627 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002628 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002629 kvm_cpu_vmxoff();
2630 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08002631 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632}
2633
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002634static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002635 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002636{
2637 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002638 u32 ctl = ctl_min | ctl_opt;
2639
2640 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2641
2642 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2643 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2644
2645 /* Ensure minimum (required) set of control bits are supported. */
2646 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002647 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002648
2649 *result = ctl;
2650 return 0;
2651}
2652
Avi Kivity110312c2010-12-21 12:54:20 +02002653static __init bool allow_1_setting(u32 msr, u32 ctl)
2654{
2655 u32 vmx_msr_low, vmx_msr_high;
2656
2657 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2658 return vmx_msr_high & ctl;
2659}
2660
Yang, Sheng002c7f72007-07-31 14:23:01 +03002661static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002662{
2663 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002664 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002665 u32 _pin_based_exec_control = 0;
2666 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002667 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002668 u32 _vmexit_control = 0;
2669 u32 _vmentry_control = 0;
2670
Raghavendra K T10166742012-02-07 23:19:20 +05302671 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002672#ifdef CONFIG_X86_64
2673 CPU_BASED_CR8_LOAD_EXITING |
2674 CPU_BASED_CR8_STORE_EXITING |
2675#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002676 CPU_BASED_CR3_LOAD_EXITING |
2677 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002678 CPU_BASED_USE_IO_BITMAPS |
2679 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03002680 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08002681 CPU_BASED_MWAIT_EXITING |
2682 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002683 CPU_BASED_INVLPG_EXITING |
2684 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002685
Sheng Yangf78e0e22007-10-29 09:40:42 +08002686 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002687 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002688 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002689 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2690 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002691 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002692#ifdef CONFIG_X86_64
2693 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2694 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2695 ~CPU_BASED_CR8_STORE_EXITING;
2696#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002697 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002698 min2 = 0;
2699 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002700 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002701 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002702 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002703 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002704 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002705 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00002706 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002707 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002708 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Abel Gordonabc4fc52013-04-18 14:35:25 +03002709 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
2710 SECONDARY_EXEC_SHADOW_VMCS;
Sheng Yangd56f5462008-04-25 10:13:16 +08002711 if (adjust_vmx_controls(min2, opt2,
2712 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002713 &_cpu_based_2nd_exec_control) < 0)
2714 return -EIO;
2715 }
2716#ifndef CONFIG_X86_64
2717 if (!(_cpu_based_2nd_exec_control &
2718 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2719 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2720#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002721
2722 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2723 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002724 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002725 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2726 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002727
Sheng Yangd56f5462008-04-25 10:13:16 +08002728 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002729 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2730 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002731 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2732 CPU_BASED_CR3_STORE_EXITING |
2733 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08002734 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
2735 vmx_capability.ept, vmx_capability.vpid);
2736 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002737
2738 min = 0;
2739#ifdef CONFIG_X86_64
2740 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2741#endif
Yang Zhanga547c6d2013-04-11 19:25:10 +08002742 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT |
2743 VM_EXIT_ACK_INTR_ON_EXIT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002744 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2745 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002746 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002747
Yang Zhang01e439b2013-04-11 19:25:12 +08002748 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
2749 opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR;
2750 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2751 &_pin_based_exec_control) < 0)
2752 return -EIO;
2753
2754 if (!(_cpu_based_2nd_exec_control &
2755 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) ||
2756 !(_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT))
2757 _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
2758
Sheng Yang468d4722008-10-09 16:01:55 +08002759 min = 0;
2760 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002761 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2762 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002763 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002765 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002766
2767 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2768 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002769 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002770
2771#ifdef CONFIG_X86_64
2772 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2773 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002774 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002775#endif
2776
2777 /* Require Write-Back (WB) memory type for VMCS accesses. */
2778 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002779 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002780
Yang, Sheng002c7f72007-07-31 14:23:01 +03002781 vmcs_conf->size = vmx_msr_high & 0x1fff;
2782 vmcs_conf->order = get_order(vmcs_config.size);
2783 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002784
Yang, Sheng002c7f72007-07-31 14:23:01 +03002785 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2786 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002787 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002788 vmcs_conf->vmexit_ctrl = _vmexit_control;
2789 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002790
Avi Kivity110312c2010-12-21 12:54:20 +02002791 cpu_has_load_ia32_efer =
2792 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2793 VM_ENTRY_LOAD_IA32_EFER)
2794 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2795 VM_EXIT_LOAD_IA32_EFER);
2796
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002797 cpu_has_load_perf_global_ctrl =
2798 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2799 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
2800 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2801 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2802
2803 /*
2804 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
2805 * but due to arrata below it can't be used. Workaround is to use
2806 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2807 *
2808 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
2809 *
2810 * AAK155 (model 26)
2811 * AAP115 (model 30)
2812 * AAT100 (model 37)
2813 * BC86,AAY89,BD102 (model 44)
2814 * BA97 (model 46)
2815 *
2816 */
2817 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
2818 switch (boot_cpu_data.x86_model) {
2819 case 26:
2820 case 30:
2821 case 37:
2822 case 44:
2823 case 46:
2824 cpu_has_load_perf_global_ctrl = false;
2825 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2826 "does not work properly. Using workaround\n");
2827 break;
2828 default:
2829 break;
2830 }
2831 }
2832
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002833 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002834}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835
2836static struct vmcs *alloc_vmcs_cpu(int cpu)
2837{
2838 int node = cpu_to_node(cpu);
2839 struct page *pages;
2840 struct vmcs *vmcs;
2841
Mel Gorman6484eb32009-06-16 15:31:54 -07002842 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 if (!pages)
2844 return NULL;
2845 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002846 memset(vmcs, 0, vmcs_config.size);
2847 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848 return vmcs;
2849}
2850
2851static struct vmcs *alloc_vmcs(void)
2852{
Ingo Molnard3b2c332007-01-05 16:36:23 -08002853 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854}
2855
2856static void free_vmcs(struct vmcs *vmcs)
2857{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002858 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859}
2860
Nadav Har'Eld462b812011-05-24 15:26:10 +03002861/*
2862 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2863 */
2864static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
2865{
2866 if (!loaded_vmcs->vmcs)
2867 return;
2868 loaded_vmcs_clear(loaded_vmcs);
2869 free_vmcs(loaded_vmcs->vmcs);
2870 loaded_vmcs->vmcs = NULL;
2871}
2872
Sam Ravnborg39959582007-06-01 00:47:13 -07002873static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874{
2875 int cpu;
2876
Zachary Amsden3230bb42009-09-29 11:38:37 -10002877 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002879 per_cpu(vmxarea, cpu) = NULL;
2880 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881}
2882
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883static __init int alloc_kvm_area(void)
2884{
2885 int cpu;
2886
Zachary Amsden3230bb42009-09-29 11:38:37 -10002887 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888 struct vmcs *vmcs;
2889
2890 vmcs = alloc_vmcs_cpu(cpu);
2891 if (!vmcs) {
2892 free_kvm_area();
2893 return -ENOMEM;
2894 }
2895
2896 per_cpu(vmxarea, cpu) = vmcs;
2897 }
2898 return 0;
2899}
2900
2901static __init int hardware_setup(void)
2902{
Yang, Sheng002c7f72007-07-31 14:23:01 +03002903 if (setup_vmcs_config(&vmcs_config) < 0)
2904 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01002905
2906 if (boot_cpu_has(X86_FEATURE_NX))
2907 kvm_enable_efer_bits(EFER_NX);
2908
Sheng Yang93ba03c2009-04-01 15:52:32 +08002909 if (!cpu_has_vmx_vpid())
2910 enable_vpid = 0;
Abel Gordonabc4fc52013-04-18 14:35:25 +03002911 if (!cpu_has_vmx_shadow_vmcs())
2912 enable_shadow_vmcs = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08002913
Sheng Yang4bc9b982010-06-02 14:05:24 +08002914 if (!cpu_has_vmx_ept() ||
2915 !cpu_has_vmx_ept_4levels()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08002916 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002917 enable_unrestricted_guest = 0;
Xudong Hao83c3a332012-05-28 19:33:35 +08002918 enable_ept_ad_bits = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002919 }
2920
Xudong Hao83c3a332012-05-28 19:33:35 +08002921 if (!cpu_has_vmx_ept_ad_bits())
2922 enable_ept_ad_bits = 0;
2923
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002924 if (!cpu_has_vmx_unrestricted_guest())
2925 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08002926
2927 if (!cpu_has_vmx_flexpriority())
2928 flexpriority_enabled = 0;
2929
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002930 if (!cpu_has_vmx_tpr_shadow())
2931 kvm_x86_ops->update_cr8_intercept = NULL;
2932
Marcelo Tosatti54dee992009-06-11 12:07:44 -03002933 if (enable_ept && !cpu_has_vmx_ept_2m_page())
2934 kvm_disable_largepages();
2935
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002936 if (!cpu_has_vmx_ple())
2937 ple_gap = 0;
2938
Yang Zhang01e439b2013-04-11 19:25:12 +08002939 if (!cpu_has_vmx_apicv())
2940 enable_apicv = 0;
Yang Zhangc7c9c562013-01-25 10:18:51 +08002941
Yang Zhang01e439b2013-04-11 19:25:12 +08002942 if (enable_apicv)
Yang Zhangc7c9c562013-01-25 10:18:51 +08002943 kvm_x86_ops->update_cr8_intercept = NULL;
Yang Zhanga20ed542013-04-11 19:25:15 +08002944 else {
Yang Zhangc7c9c562013-01-25 10:18:51 +08002945 kvm_x86_ops->hwapic_irr_update = NULL;
Yang Zhanga20ed542013-04-11 19:25:15 +08002946 kvm_x86_ops->deliver_posted_interrupt = NULL;
2947 kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy;
2948 }
Yang Zhang83d4c282013-01-25 10:18:49 +08002949
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002950 if (nested)
2951 nested_vmx_setup_ctls_msrs();
2952
Avi Kivity6aa8b732006-12-10 02:21:36 -08002953 return alloc_kvm_area();
2954}
2955
2956static __exit void hardware_unsetup(void)
2957{
2958 free_kvm_area();
2959}
2960
Gleb Natapov14168782013-01-21 15:36:49 +02002961static bool emulation_required(struct kvm_vcpu *vcpu)
2962{
2963 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2964}
2965
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002966static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02002967 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968{
Gleb Natapovd99e4152012-12-20 16:57:45 +02002969 if (!emulate_invalid_guest_state) {
2970 /*
2971 * CS and SS RPL should be equal during guest entry according
2972 * to VMX spec, but in reality it is not always so. Since vcpu
2973 * is in the middle of the transition from real mode to
2974 * protected mode it is safe to assume that RPL 0 is a good
2975 * default value.
2976 */
2977 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
2978 save->selector &= ~SELECTOR_RPL_MASK;
2979 save->dpl = save->selector & SELECTOR_RPL_MASK;
2980 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02002982 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983}
2984
2985static void enter_pmode(struct kvm_vcpu *vcpu)
2986{
2987 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002988 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989
Gleb Natapovd99e4152012-12-20 16:57:45 +02002990 /*
2991 * Update real mode segment cache. It may be not up-to-date if sement
2992 * register was written while vcpu was in a guest mode.
2993 */
2994 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2995 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2996 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2997 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2998 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2999 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3000
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003001 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003002
Avi Kivity2fb92db2011-04-27 19:42:18 +03003003 vmx_segment_cache_clear(vmx);
3004
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003005 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003006
3007 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003008 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3009 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003010 vmcs_writel(GUEST_RFLAGS, flags);
3011
Rusty Russell66aee912007-07-17 23:34:16 +10003012 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3013 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003014
3015 update_exception_bitmap(vcpu);
3016
Gleb Natapov91b0aa22013-01-21 15:36:47 +02003017 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3018 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3019 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3020 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3021 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3022 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Gleb Natapov1f3141e2013-01-21 15:36:41 +02003023
3024 /* CPL is always 0 when CPU enters protected mode */
3025 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
3026 vmx->cpl = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003027}
3028
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003029static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030{
Mathias Krause772e0312012-08-30 01:30:19 +02003031 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02003032 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033
Gleb Natapovd99e4152012-12-20 16:57:45 +02003034 var.dpl = 0x3;
3035 if (seg == VCPU_SREG_CS)
3036 var.type = 0x3;
3037
3038 if (!emulate_invalid_guest_state) {
3039 var.selector = var.base >> 4;
3040 var.base = var.base & 0xffff0;
3041 var.limit = 0xffff;
3042 var.g = 0;
3043 var.db = 0;
3044 var.present = 1;
3045 var.s = 1;
3046 var.l = 0;
3047 var.unusable = 0;
3048 var.type = 0x3;
3049 var.avl = 0;
3050 if (save->base & 0xf)
3051 printk_once(KERN_WARNING "kvm: segment base is not "
3052 "paragraph aligned when entering "
3053 "protected mode (seg=%d)", seg);
3054 }
3055
3056 vmcs_write16(sf->selector, var.selector);
3057 vmcs_write32(sf->base, var.base);
3058 vmcs_write32(sf->limit, var.limit);
3059 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060}
3061
3062static void enter_rmode(struct kvm_vcpu *vcpu)
3063{
3064 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003065 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003067 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3068 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3069 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3070 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3071 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003072 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3073 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003074
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003075 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076
Gleb Natapov776e58e2011-03-13 12:34:27 +02003077 /*
3078 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003079 * vcpu. Warn the user that an update is overdue.
Gleb Natapov776e58e2011-03-13 12:34:27 +02003080 */
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003081 if (!vcpu->kvm->arch.tss_addr)
Gleb Natapov776e58e2011-03-13 12:34:27 +02003082 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3083 "called before entering vcpu\n");
Gleb Natapov776e58e2011-03-13 12:34:27 +02003084
Avi Kivity2fb92db2011-04-27 19:42:18 +03003085 vmx_segment_cache_clear(vmx);
3086
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003087 vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003088 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3090
3091 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03003092 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003093
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01003094 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095
3096 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10003097 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098 update_exception_bitmap(vcpu);
3099
Gleb Natapovd99e4152012-12-20 16:57:45 +02003100 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3101 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3102 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3103 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3104 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3105 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003106
Eddie Dong8668a3c2007-10-10 14:26:45 +08003107 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003108}
3109
Amit Shah401d10d2009-02-20 22:53:37 +05303110static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3111{
3112 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03003113 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3114
3115 if (!msr)
3116 return;
Amit Shah401d10d2009-02-20 22:53:37 +05303117
Avi Kivity44ea2b12009-09-06 15:55:37 +03003118 /*
3119 * Force kernel_gs_base reloading before EFER changes, as control
3120 * of this msr depends on is_long_mode().
3121 */
3122 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02003123 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05303124 if (efer & EFER_LMA) {
3125 vmcs_write32(VM_ENTRY_CONTROLS,
3126 vmcs_read32(VM_ENTRY_CONTROLS) |
3127 VM_ENTRY_IA32E_MODE);
3128 msr->data = efer;
3129 } else {
3130 vmcs_write32(VM_ENTRY_CONTROLS,
3131 vmcs_read32(VM_ENTRY_CONTROLS) &
3132 ~VM_ENTRY_IA32E_MODE);
3133
3134 msr->data = efer & ~EFER_LME;
3135 }
3136 setup_msrs(vmx);
3137}
3138
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003139#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140
3141static void enter_lmode(struct kvm_vcpu *vcpu)
3142{
3143 u32 guest_tr_ar;
3144
Avi Kivity2fb92db2011-04-27 19:42:18 +03003145 vmx_segment_cache_clear(to_vmx(vcpu));
3146
Avi Kivity6aa8b732006-12-10 02:21:36 -08003147 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
3148 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003149 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3150 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151 vmcs_write32(GUEST_TR_AR_BYTES,
3152 (guest_tr_ar & ~AR_TYPE_MASK)
3153 | AR_TYPE_BUSY_64_TSS);
3154 }
Avi Kivityda38f432010-07-06 11:30:49 +03003155 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156}
3157
3158static void exit_lmode(struct kvm_vcpu *vcpu)
3159{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160 vmcs_write32(VM_ENTRY_CONTROLS,
3161 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03003162 & ~VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003163 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003164}
3165
3166#endif
3167
Sheng Yang2384d2b2008-01-17 15:14:33 +08003168static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3169{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08003170 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003171 if (enable_ept) {
3172 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3173 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003174 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003175 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003176}
3177
Avi Kivitye8467fd2009-12-29 18:43:06 +02003178static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3179{
3180 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3181
3182 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3183 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3184}
3185
Avi Kivityaff48ba2010-12-05 18:56:11 +02003186static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3187{
3188 if (enable_ept && is_paging(vcpu))
3189 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3190 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3191}
3192
Anthony Liguori25c4c272007-04-27 09:29:21 +03003193static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003194{
Avi Kivityfc78f512009-12-07 12:16:48 +02003195 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3196
3197 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3198 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003199}
3200
Sheng Yang14394422008-04-28 12:24:45 +08003201static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3202{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003203 if (!test_bit(VCPU_EXREG_PDPTR,
3204 (unsigned long *)&vcpu->arch.regs_dirty))
3205 return;
3206
Sheng Yang14394422008-04-28 12:24:45 +08003207 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02003208 vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
3209 vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
3210 vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
3211 vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003212 }
3213}
3214
Avi Kivity8f5d5492009-05-31 18:41:29 +03003215static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3216{
3217 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02003218 vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3219 vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3220 vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3221 vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003222 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003223
3224 __set_bit(VCPU_EXREG_PDPTR,
3225 (unsigned long *)&vcpu->arch.regs_avail);
3226 __set_bit(VCPU_EXREG_PDPTR,
3227 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003228}
3229
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003230static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003231
3232static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3233 unsigned long cr0,
3234 struct kvm_vcpu *vcpu)
3235{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003236 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3237 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003238 if (!(cr0 & X86_CR0_PG)) {
3239 /* From paging/starting to nonpaging */
3240 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003241 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003242 (CPU_BASED_CR3_LOAD_EXITING |
3243 CPU_BASED_CR3_STORE_EXITING));
3244 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003245 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003246 } else if (!is_paging(vcpu)) {
3247 /* From nonpaging to paging */
3248 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003249 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003250 ~(CPU_BASED_CR3_LOAD_EXITING |
3251 CPU_BASED_CR3_STORE_EXITING));
3252 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003253 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003254 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003255
3256 if (!(cr0 & X86_CR0_WP))
3257 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003258}
3259
Avi Kivity6aa8b732006-12-10 02:21:36 -08003260static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3261{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003262 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003263 unsigned long hw_cr0;
3264
Gleb Natapov50378782013-02-04 16:00:28 +02003265 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003266 if (enable_unrestricted_guest)
Gleb Natapov50378782013-02-04 16:00:28 +02003267 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003268 else {
Gleb Natapov50378782013-02-04 16:00:28 +02003269 hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003270
Gleb Natapov218e7632013-01-21 15:36:45 +02003271 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3272 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273
Gleb Natapov218e7632013-01-21 15:36:45 +02003274 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3275 enter_rmode(vcpu);
3276 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003278#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003279 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003280 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003282 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003283 exit_lmode(vcpu);
3284 }
3285#endif
3286
Avi Kivity089d0342009-03-23 18:26:32 +02003287 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003288 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3289
Avi Kivity02daab22009-12-30 12:40:26 +02003290 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003291 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003292
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003294 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003295 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003296
3297 /* depends on vcpu->arch.cr0 to be set to a new value */
3298 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003299}
3300
Sheng Yang14394422008-04-28 12:24:45 +08003301static u64 construct_eptp(unsigned long root_hpa)
3302{
3303 u64 eptp;
3304
3305 /* TODO write the value reading from MSR */
3306 eptp = VMX_EPT_DEFAULT_MT |
3307 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003308 if (enable_ept_ad_bits)
3309 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003310 eptp |= (root_hpa & PAGE_MASK);
3311
3312 return eptp;
3313}
3314
Avi Kivity6aa8b732006-12-10 02:21:36 -08003315static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3316{
Sheng Yang14394422008-04-28 12:24:45 +08003317 unsigned long guest_cr3;
3318 u64 eptp;
3319
3320 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003321 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003322 eptp = construct_eptp(cr3);
3323 vmcs_write64(EPT_POINTER, eptp);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003324 guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
Sheng Yangb927a3c2009-07-21 10:42:48 +08003325 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02003326 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003327 }
3328
Sheng Yang2384d2b2008-01-17 15:14:33 +08003329 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003330 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003331}
3332
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003333static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003334{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003335 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08003336 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
3337
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003338 if (cr4 & X86_CR4_VMXE) {
3339 /*
3340 * To use VMXON (and later other VMX instructions), a guest
3341 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3342 * So basically the check on whether to allow nested VMX
3343 * is here.
3344 */
3345 if (!nested_vmx_allowed(vcpu))
3346 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01003347 }
3348 if (to_vmx(vcpu)->nested.vmxon &&
3349 ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON))
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003350 return 1;
3351
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003352 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02003353 if (enable_ept) {
3354 if (!is_paging(vcpu)) {
3355 hw_cr4 &= ~X86_CR4_PAE;
3356 hw_cr4 |= X86_CR4_PSE;
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003357 /*
3358 * SMEP is disabled if CPU is in non-paging mode in
3359 * hardware. However KVM always uses paging mode to
3360 * emulate guest non-paging mode with TDP.
3361 * To emulate this behavior, SMEP needs to be manually
3362 * disabled when guest switches to non-paging mode.
3363 */
3364 hw_cr4 &= ~X86_CR4_SMEP;
Avi Kivitybc230082009-12-08 12:14:42 +02003365 } else if (!(cr4 & X86_CR4_PAE)) {
3366 hw_cr4 &= ~X86_CR4_PAE;
3367 }
3368 }
Sheng Yang14394422008-04-28 12:24:45 +08003369
3370 vmcs_writel(CR4_READ_SHADOW, cr4);
3371 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003372 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003373}
3374
Avi Kivity6aa8b732006-12-10 02:21:36 -08003375static void vmx_get_segment(struct kvm_vcpu *vcpu,
3376 struct kvm_segment *var, int seg)
3377{
Avi Kivitya9179492011-01-03 14:28:52 +02003378 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003379 u32 ar;
3380
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003381 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003382 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003383 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003384 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003385 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003386 var->base = vmx_read_guest_seg_base(vmx, seg);
3387 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3388 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003389 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003390 var->base = vmx_read_guest_seg_base(vmx, seg);
3391 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3392 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3393 ar = vmx_read_guest_seg_ar(vmx, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003394 var->type = ar & 15;
3395 var->s = (ar >> 4) & 1;
3396 var->dpl = (ar >> 5) & 3;
3397 var->present = (ar >> 7) & 1;
3398 var->avl = (ar >> 12) & 1;
3399 var->l = (ar >> 13) & 1;
3400 var->db = (ar >> 14) & 1;
3401 var->g = (ar >> 15) & 1;
3402 var->unusable = (ar >> 16) & 1;
3403}
3404
Avi Kivitya9179492011-01-03 14:28:52 +02003405static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3406{
Avi Kivitya9179492011-01-03 14:28:52 +02003407 struct kvm_segment s;
3408
3409 if (to_vmx(vcpu)->rmode.vm86_active) {
3410 vmx_get_segment(vcpu, &s, seg);
3411 return s.base;
3412 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003413 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003414}
3415
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003416static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003417{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003418 struct vcpu_vmx *vmx = to_vmx(vcpu);
3419
Avi Kivity3eeb3282010-01-21 15:31:48 +02003420 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003421 return 0;
3422
Avi Kivityf4c63e52011-03-07 14:54:28 +02003423 if (!is_long_mode(vcpu)
3424 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
Izik Eidus2e4d2652008-03-24 19:38:34 +02003425 return 3;
3426
Avi Kivity69c73022011-03-07 15:26:44 +02003427 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
3428 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003429 vmx->cpl = vmx_read_guest_seg_selector(vmx, VCPU_SREG_CS) & 3;
Avi Kivity69c73022011-03-07 15:26:44 +02003430 }
Avi Kivityd881e6f2012-06-06 18:36:48 +03003431
3432 return vmx->cpl;
Avi Kivity69c73022011-03-07 15:26:44 +02003433}
3434
3435
Avi Kivity653e3102007-05-07 10:55:37 +03003436static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003437{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438 u32 ar;
3439
Avi Kivityf0495f92012-06-07 17:06:10 +03003440 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441 ar = 1 << 16;
3442 else {
3443 ar = var->type & 15;
3444 ar |= (var->s & 1) << 4;
3445 ar |= (var->dpl & 3) << 5;
3446 ar |= (var->present & 1) << 7;
3447 ar |= (var->avl & 1) << 12;
3448 ar |= (var->l & 1) << 13;
3449 ar |= (var->db & 1) << 14;
3450 ar |= (var->g & 1) << 15;
3451 }
Avi Kivity653e3102007-05-07 10:55:37 +03003452
3453 return ar;
3454}
3455
3456static void vmx_set_segment(struct kvm_vcpu *vcpu,
3457 struct kvm_segment *var, int seg)
3458{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003459 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003460 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003461
Avi Kivity2fb92db2011-04-27 19:42:18 +03003462 vmx_segment_cache_clear(vmx);
Gleb Natapov2f143242013-01-21 15:36:42 +02003463 if (seg == VCPU_SREG_CS)
3464 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity2fb92db2011-04-27 19:42:18 +03003465
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003466 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3467 vmx->rmode.segs[seg] = *var;
3468 if (seg == VCPU_SREG_TR)
3469 vmcs_write16(sf->selector, var->selector);
3470 else if (var->s)
3471 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003472 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003473 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003474
Avi Kivity653e3102007-05-07 10:55:37 +03003475 vmcs_writel(sf->base, var->base);
3476 vmcs_write32(sf->limit, var->limit);
3477 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003478
3479 /*
3480 * Fix the "Accessed" bit in AR field of segment registers for older
3481 * qemu binaries.
3482 * IA32 arch specifies that at the time of processor reset the
3483 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003484 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003485 * state vmexit when "unrestricted guest" mode is turned on.
3486 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3487 * tree. Newer qemu binaries with that qemu fix would not need this
3488 * kvm hack.
3489 */
3490 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003491 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003492
Gleb Natapovf924d662012-12-12 19:10:55 +02003493 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003494
3495out:
Gleb Natapov14168782013-01-21 15:36:49 +02003496 vmx->emulation_required |= emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003497}
3498
Avi Kivity6aa8b732006-12-10 02:21:36 -08003499static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3500{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003501 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502
3503 *db = (ar >> 14) & 1;
3504 *l = (ar >> 13) & 1;
3505}
3506
Gleb Natapov89a27f42010-02-16 10:51:48 +02003507static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003508{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003509 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3510 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003511}
3512
Gleb Natapov89a27f42010-02-16 10:51:48 +02003513static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003514{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003515 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3516 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003517}
3518
Gleb Natapov89a27f42010-02-16 10:51:48 +02003519static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003520{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003521 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3522 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003523}
3524
Gleb Natapov89a27f42010-02-16 10:51:48 +02003525static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003527 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3528 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003529}
3530
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003531static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3532{
3533 struct kvm_segment var;
3534 u32 ar;
3535
3536 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003537 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003538 if (seg == VCPU_SREG_CS)
3539 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003540 ar = vmx_segment_access_rights(&var);
3541
3542 if (var.base != (var.selector << 4))
3543 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003544 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003545 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003546 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003547 return false;
3548
3549 return true;
3550}
3551
3552static bool code_segment_valid(struct kvm_vcpu *vcpu)
3553{
3554 struct kvm_segment cs;
3555 unsigned int cs_rpl;
3556
3557 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3558 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
3559
Avi Kivity1872a3f2009-01-04 23:26:52 +02003560 if (cs.unusable)
3561 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003562 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
3563 return false;
3564 if (!cs.s)
3565 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003566 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003567 if (cs.dpl > cs_rpl)
3568 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003569 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003570 if (cs.dpl != cs_rpl)
3571 return false;
3572 }
3573 if (!cs.present)
3574 return false;
3575
3576 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3577 return true;
3578}
3579
3580static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3581{
3582 struct kvm_segment ss;
3583 unsigned int ss_rpl;
3584
3585 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3586 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
3587
Avi Kivity1872a3f2009-01-04 23:26:52 +02003588 if (ss.unusable)
3589 return true;
3590 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003591 return false;
3592 if (!ss.s)
3593 return false;
3594 if (ss.dpl != ss_rpl) /* DPL != RPL */
3595 return false;
3596 if (!ss.present)
3597 return false;
3598
3599 return true;
3600}
3601
3602static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3603{
3604 struct kvm_segment var;
3605 unsigned int rpl;
3606
3607 vmx_get_segment(vcpu, &var, seg);
3608 rpl = var.selector & SELECTOR_RPL_MASK;
3609
Avi Kivity1872a3f2009-01-04 23:26:52 +02003610 if (var.unusable)
3611 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003612 if (!var.s)
3613 return false;
3614 if (!var.present)
3615 return false;
3616 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
3617 if (var.dpl < rpl) /* DPL < RPL */
3618 return false;
3619 }
3620
3621 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3622 * rights flags
3623 */
3624 return true;
3625}
3626
3627static bool tr_valid(struct kvm_vcpu *vcpu)
3628{
3629 struct kvm_segment tr;
3630
3631 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3632
Avi Kivity1872a3f2009-01-04 23:26:52 +02003633 if (tr.unusable)
3634 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003635 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3636 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003637 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003638 return false;
3639 if (!tr.present)
3640 return false;
3641
3642 return true;
3643}
3644
3645static bool ldtr_valid(struct kvm_vcpu *vcpu)
3646{
3647 struct kvm_segment ldtr;
3648
3649 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3650
Avi Kivity1872a3f2009-01-04 23:26:52 +02003651 if (ldtr.unusable)
3652 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003653 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3654 return false;
3655 if (ldtr.type != 2)
3656 return false;
3657 if (!ldtr.present)
3658 return false;
3659
3660 return true;
3661}
3662
3663static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3664{
3665 struct kvm_segment cs, ss;
3666
3667 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3668 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3669
3670 return ((cs.selector & SELECTOR_RPL_MASK) ==
3671 (ss.selector & SELECTOR_RPL_MASK));
3672}
3673
3674/*
3675 * Check if guest state is valid. Returns true if valid, false if
3676 * not.
3677 * We assume that registers are always usable
3678 */
3679static bool guest_state_valid(struct kvm_vcpu *vcpu)
3680{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02003681 if (enable_unrestricted_guest)
3682 return true;
3683
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003684 /* real mode guest state checks */
Gleb Natapovf13882d2013-04-14 16:07:37 +03003685 if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003686 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3687 return false;
3688 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3689 return false;
3690 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3691 return false;
3692 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3693 return false;
3694 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3695 return false;
3696 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3697 return false;
3698 } else {
3699 /* protected mode guest state checks */
3700 if (!cs_ss_rpl_check(vcpu))
3701 return false;
3702 if (!code_segment_valid(vcpu))
3703 return false;
3704 if (!stack_segment_valid(vcpu))
3705 return false;
3706 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3707 return false;
3708 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3709 return false;
3710 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3711 return false;
3712 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3713 return false;
3714 if (!tr_valid(vcpu))
3715 return false;
3716 if (!ldtr_valid(vcpu))
3717 return false;
3718 }
3719 /* TODO:
3720 * - Add checks on RIP
3721 * - Add checks on RFLAGS
3722 */
3723
3724 return true;
3725}
3726
Mike Dayd77c26f2007-10-08 09:02:08 -04003727static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003728{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003729 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003730 u16 data = 0;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003731 int r, idx, ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003732
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003733 idx = srcu_read_lock(&kvm->srcu);
Jan Kiszka4918c6c2013-03-15 08:38:56 +01003734 fn = kvm->arch.tss_addr >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003735 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3736 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003737 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003738 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003739 r = kvm_write_guest_page(kvm, fn++, &data,
3740 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003741 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003742 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003743 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3744 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003745 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003746 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3747 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003748 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003749 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003750 r = kvm_write_guest_page(kvm, fn, &data,
3751 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3752 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02003753 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003754 goto out;
3755
3756 ret = 1;
3757out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003758 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003759 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003760}
3761
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003762static int init_rmode_identity_map(struct kvm *kvm)
3763{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003764 int i, idx, r, ret;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003765 pfn_t identity_map_pfn;
3766 u32 tmp;
3767
Avi Kivity089d0342009-03-23 18:26:32 +02003768 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003769 return 1;
3770 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
3771 printk(KERN_ERR "EPT: identity-mapping pagetable "
3772 "haven't been allocated!\n");
3773 return 0;
3774 }
3775 if (likely(kvm->arch.ept_identity_pagetable_done))
3776 return 1;
3777 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003778 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003779 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003780 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3781 if (r < 0)
3782 goto out;
3783 /* Set up identity-mapping pagetable for EPT in real mode */
3784 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3785 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3786 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3787 r = kvm_write_guest_page(kvm, identity_map_pfn,
3788 &tmp, i * sizeof(tmp), sizeof(tmp));
3789 if (r < 0)
3790 goto out;
3791 }
3792 kvm->arch.ept_identity_pagetable_done = true;
3793 ret = 1;
3794out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003795 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003796 return ret;
3797}
3798
Avi Kivity6aa8b732006-12-10 02:21:36 -08003799static void seg_setup(int seg)
3800{
Mathias Krause772e0312012-08-30 01:30:19 +02003801 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003802 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003803
3804 vmcs_write16(sf->selector, 0);
3805 vmcs_writel(sf->base, 0);
3806 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003807 ar = 0x93;
3808 if (seg == VCPU_SREG_CS)
3809 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003810
3811 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003812}
3813
Sheng Yangf78e0e22007-10-29 09:40:42 +08003814static int alloc_apic_access_page(struct kvm *kvm)
3815{
Xiao Guangrong44841412012-09-07 14:14:20 +08003816 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003817 struct kvm_userspace_memory_region kvm_userspace_mem;
3818 int r = 0;
3819
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003820 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003821 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003822 goto out;
3823 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
3824 kvm_userspace_mem.flags = 0;
3825 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
3826 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09003827 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003828 if (r)
3829 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003830
Xiao Guangrong44841412012-09-07 14:14:20 +08003831 page = gfn_to_page(kvm, 0xfee00);
3832 if (is_error_page(page)) {
3833 r = -EFAULT;
3834 goto out;
3835 }
3836
3837 kvm->arch.apic_access_page = page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003838out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003839 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003840 return r;
3841}
3842
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003843static int alloc_identity_pagetable(struct kvm *kvm)
3844{
Xiao Guangrong44841412012-09-07 14:14:20 +08003845 struct page *page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003846 struct kvm_userspace_memory_region kvm_userspace_mem;
3847 int r = 0;
3848
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003849 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003850 if (kvm->arch.ept_identity_pagetable)
3851 goto out;
3852 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
3853 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003854 kvm_userspace_mem.guest_phys_addr =
3855 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003856 kvm_userspace_mem.memory_size = PAGE_SIZE;
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09003857 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003858 if (r)
3859 goto out;
3860
Xiao Guangrong44841412012-09-07 14:14:20 +08003861 page = gfn_to_page(kvm, kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
3862 if (is_error_page(page)) {
3863 r = -EFAULT;
3864 goto out;
3865 }
3866
3867 kvm->arch.ept_identity_pagetable = page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003868out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003869 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003870 return r;
3871}
3872
Sheng Yang2384d2b2008-01-17 15:14:33 +08003873static void allocate_vpid(struct vcpu_vmx *vmx)
3874{
3875 int vpid;
3876
3877 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02003878 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003879 return;
3880 spin_lock(&vmx_vpid_lock);
3881 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
3882 if (vpid < VMX_NR_VPIDS) {
3883 vmx->vpid = vpid;
3884 __set_bit(vpid, vmx_vpid_bitmap);
3885 }
3886 spin_unlock(&vmx_vpid_lock);
3887}
3888
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003889static void free_vpid(struct vcpu_vmx *vmx)
3890{
3891 if (!enable_vpid)
3892 return;
3893 spin_lock(&vmx_vpid_lock);
3894 if (vmx->vpid != 0)
3895 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3896 spin_unlock(&vmx_vpid_lock);
3897}
3898
Yang Zhang8d146952013-01-25 10:18:50 +08003899#define MSR_TYPE_R 1
3900#define MSR_TYPE_W 2
3901static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
3902 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08003903{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003904 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08003905
3906 if (!cpu_has_vmx_msr_bitmap())
3907 return;
3908
3909 /*
3910 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3911 * have the write-low and read-high bitmap offsets the wrong way round.
3912 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3913 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003914 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08003915 if (type & MSR_TYPE_R)
3916 /* read-low */
3917 __clear_bit(msr, msr_bitmap + 0x000 / f);
3918
3919 if (type & MSR_TYPE_W)
3920 /* write-low */
3921 __clear_bit(msr, msr_bitmap + 0x800 / f);
3922
Sheng Yang25c5f222008-03-28 13:18:56 +08003923 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3924 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08003925 if (type & MSR_TYPE_R)
3926 /* read-high */
3927 __clear_bit(msr, msr_bitmap + 0x400 / f);
3928
3929 if (type & MSR_TYPE_W)
3930 /* write-high */
3931 __clear_bit(msr, msr_bitmap + 0xc00 / f);
3932
3933 }
3934}
3935
3936static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
3937 u32 msr, int type)
3938{
3939 int f = sizeof(unsigned long);
3940
3941 if (!cpu_has_vmx_msr_bitmap())
3942 return;
3943
3944 /*
3945 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3946 * have the write-low and read-high bitmap offsets the wrong way round.
3947 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3948 */
3949 if (msr <= 0x1fff) {
3950 if (type & MSR_TYPE_R)
3951 /* read-low */
3952 __set_bit(msr, msr_bitmap + 0x000 / f);
3953
3954 if (type & MSR_TYPE_W)
3955 /* write-low */
3956 __set_bit(msr, msr_bitmap + 0x800 / f);
3957
3958 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3959 msr &= 0x1fff;
3960 if (type & MSR_TYPE_R)
3961 /* read-high */
3962 __set_bit(msr, msr_bitmap + 0x400 / f);
3963
3964 if (type & MSR_TYPE_W)
3965 /* write-high */
3966 __set_bit(msr, msr_bitmap + 0xc00 / f);
3967
Sheng Yang25c5f222008-03-28 13:18:56 +08003968 }
Sheng Yang25c5f222008-03-28 13:18:56 +08003969}
3970
Avi Kivity58972972009-02-24 22:26:47 +02003971static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
3972{
3973 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08003974 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
3975 msr, MSR_TYPE_R | MSR_TYPE_W);
3976 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
3977 msr, MSR_TYPE_R | MSR_TYPE_W);
3978}
3979
3980static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
3981{
3982 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
3983 msr, MSR_TYPE_R);
3984 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
3985 msr, MSR_TYPE_R);
3986}
3987
3988static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
3989{
3990 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
3991 msr, MSR_TYPE_R);
3992 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
3993 msr, MSR_TYPE_R);
3994}
3995
3996static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
3997{
3998 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
3999 msr, MSR_TYPE_W);
4000 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
4001 msr, MSR_TYPE_W);
Avi Kivity58972972009-02-24 22:26:47 +02004002}
4003
Yang Zhang01e439b2013-04-11 19:25:12 +08004004static int vmx_vm_has_apicv(struct kvm *kvm)
4005{
4006 return enable_apicv && irqchip_in_kernel(kvm);
4007}
4008
Avi Kivity6aa8b732006-12-10 02:21:36 -08004009/*
Yang Zhanga20ed542013-04-11 19:25:15 +08004010 * Send interrupt to vcpu via posted interrupt way.
4011 * 1. If target vcpu is running(non-root mode), send posted interrupt
4012 * notification to vcpu and hardware will sync PIR to vIRR atomically.
4013 * 2. If target vcpu isn't running(root mode), kick it to pick up the
4014 * interrupt from PIR in next vmentry.
4015 */
4016static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4017{
4018 struct vcpu_vmx *vmx = to_vmx(vcpu);
4019 int r;
4020
4021 if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4022 return;
4023
4024 r = pi_test_and_set_on(&vmx->pi_desc);
4025 kvm_make_request(KVM_REQ_EVENT, vcpu);
Zhang, Yang Z6ffbbbb2013-04-17 23:11:54 -03004026#ifdef CONFIG_SMP
Yang Zhanga20ed542013-04-11 19:25:15 +08004027 if (!r && (vcpu->mode == IN_GUEST_MODE))
4028 apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
4029 POSTED_INTR_VECTOR);
4030 else
Zhang, Yang Z6ffbbbb2013-04-17 23:11:54 -03004031#endif
Yang Zhanga20ed542013-04-11 19:25:15 +08004032 kvm_vcpu_kick(vcpu);
4033}
4034
4035static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
4036{
4037 struct vcpu_vmx *vmx = to_vmx(vcpu);
4038
4039 if (!pi_test_and_clear_on(&vmx->pi_desc))
4040 return;
4041
4042 kvm_apic_update_irr(vcpu, vmx->pi_desc.pir);
4043}
4044
4045static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu)
4046{
4047 return;
4048}
4049
4050/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004051 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4052 * will not change in the lifetime of the guest.
4053 * Note that host-state that does change is set elsewhere. E.g., host-state
4054 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4055 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004056static void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004057{
4058 u32 low32, high32;
4059 unsigned long tmpl;
4060 struct desc_ptr dt;
4061
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07004062 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004063 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
4064 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
4065
4066 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004067#ifdef CONFIG_X86_64
4068 /*
4069 * Load null selectors, so we can avoid reloading them in
4070 * __vmx_load_host_state(), in case userspace uses the null selectors
4071 * too (the expected case).
4072 */
4073 vmcs_write16(HOST_DS_SELECTOR, 0);
4074 vmcs_write16(HOST_ES_SELECTOR, 0);
4075#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004076 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4077 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03004078#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004079 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4080 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4081
4082 native_store_idt(&dt);
4083 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004084 vmx->host_idt_base = dt.address;
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004085
Avi Kivity83287ea422012-09-16 15:10:57 +03004086 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004087
4088 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4089 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4090 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4091 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4092
4093 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4094 rdmsr(MSR_IA32_CR_PAT, low32, high32);
4095 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4096 }
4097}
4098
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004099static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4100{
4101 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4102 if (enable_ept)
4103 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03004104 if (is_guest_mode(&vmx->vcpu))
4105 vmx->vcpu.arch.cr4_guest_owned_bits &=
4106 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004107 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4108}
4109
Yang Zhang01e439b2013-04-11 19:25:12 +08004110static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4111{
4112 u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4113
4114 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4115 pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
4116 return pin_based_exec_ctrl;
4117}
4118
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004119static u32 vmx_exec_control(struct vcpu_vmx *vmx)
4120{
4121 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
4122 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
4123 exec_control &= ~CPU_BASED_TPR_SHADOW;
4124#ifdef CONFIG_X86_64
4125 exec_control |= CPU_BASED_CR8_STORE_EXITING |
4126 CPU_BASED_CR8_LOAD_EXITING;
4127#endif
4128 }
4129 if (!enable_ept)
4130 exec_control |= CPU_BASED_CR3_STORE_EXITING |
4131 CPU_BASED_CR3_LOAD_EXITING |
4132 CPU_BASED_INVLPG_EXITING;
4133 return exec_control;
4134}
4135
4136static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
4137{
4138 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
4139 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4140 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4141 if (vmx->vpid == 0)
4142 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4143 if (!enable_ept) {
4144 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4145 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00004146 /* Enable INVPCID for non-ept guests may cause performance regression. */
4147 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004148 }
4149 if (!enable_unrestricted_guest)
4150 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4151 if (!ple_gap)
4152 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Yang Zhangc7c9c562013-01-25 10:18:51 +08004153 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
4154 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4155 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08004156 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Abel Gordonabc4fc52013-04-18 14:35:25 +03004157 /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4158 (handle_vmptrld).
4159 We can NOT enable shadow_vmcs here because we don't have yet
4160 a current VMCS12
4161 */
4162 exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004163 return exec_control;
4164}
4165
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004166static void ept_set_mmio_spte_mask(void)
4167{
4168 /*
4169 * EPT Misconfigurations can be generated if the value of bits 2:0
4170 * of an EPT paging-structure entry is 110b (write/execute).
4171 * Also, magic bits (0xffull << 49) is set to quickly identify mmio
4172 * spte.
4173 */
4174 kvm_mmu_set_mmio_spte_mask(0xffull << 49 | 0x6ull);
4175}
4176
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004177/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178 * Sets up the vmcs for emulated real mode.
4179 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004180static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004181{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004182#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004183 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004184#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186
Avi Kivity6aa8b732006-12-10 02:21:36 -08004187 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004188 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4189 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004190
Abel Gordon4607c2d2013-04-18 14:35:55 +03004191 if (enable_shadow_vmcs) {
4192 vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap));
4193 vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap));
4194 }
Sheng Yang25c5f222008-03-28 13:18:56 +08004195 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02004196 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08004197
Avi Kivity6aa8b732006-12-10 02:21:36 -08004198 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4199
Avi Kivity6aa8b732006-12-10 02:21:36 -08004200 /* Control */
Yang Zhang01e439b2013-04-11 19:25:12 +08004201 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx));
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004202
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004203 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004204
Sheng Yang83ff3b92007-11-21 14:33:25 +08004205 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004206 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4207 vmx_secondary_exec_control(vmx));
Sheng Yang83ff3b92007-11-21 14:33:25 +08004208 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004209
Yang Zhang01e439b2013-04-11 19:25:12 +08004210 if (vmx_vm_has_apicv(vmx->vcpu.kvm)) {
Yang Zhangc7c9c562013-01-25 10:18:51 +08004211 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4212 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4213 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4214 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4215
4216 vmcs_write16(GUEST_INTR_STATUS, 0);
Yang Zhang01e439b2013-04-11 19:25:12 +08004217
4218 vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
4219 vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
Yang Zhangc7c9c562013-01-25 10:18:51 +08004220 }
4221
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004222 if (ple_gap) {
4223 vmcs_write32(PLE_GAP, ple_gap);
4224 vmcs_write32(PLE_WINDOW, ple_window);
4225 }
4226
Xiao Guangrongc3707952011-07-12 03:28:04 +08004227 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4228 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004229 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4230
Avi Kivity9581d442010-10-19 16:46:55 +02004231 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4232 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08004233 vmx_set_constant_host_state(vmx);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004234#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004235 rdmsrl(MSR_FS_BASE, a);
4236 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
4237 rdmsrl(MSR_GS_BASE, a);
4238 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
4239#else
4240 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4241 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
4242#endif
4243
Eddie Dong2cc51562007-05-21 07:28:09 +03004244 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4245 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004246 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03004247 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004248 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004249
Sheng Yang468d4722008-10-09 16:01:55 +08004250 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004251 u32 msr_low, msr_high;
4252 u64 host_pat;
Sheng Yang468d4722008-10-09 16:01:55 +08004253 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
4254 host_pat = msr_low | ((u64) msr_high << 32);
4255 /* Write the default value follow host pat */
4256 vmcs_write64(GUEST_IA32_PAT, host_pat);
4257 /* Keep arch.pat sync with GUEST_IA32_PAT */
4258 vmx->vcpu.arch.pat = host_pat;
4259 }
4260
Avi Kivity6aa8b732006-12-10 02:21:36 -08004261 for (i = 0; i < NR_VMX_MSR; ++i) {
4262 u32 index = vmx_msr_index[i];
4263 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004264 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004265
4266 if (rdmsr_safe(index, &data_low, &data_high) < 0)
4267 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08004268 if (wrmsr_safe(index, data_low, data_high) < 0)
4269 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03004270 vmx->guest_msrs[j].index = i;
4271 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02004272 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004273 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004274 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004275
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004276 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004277
4278 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004279 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
4280
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004281 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004282 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004283
4284 return 0;
4285}
4286
Jan Kiszka57f252f2013-03-12 10:20:24 +01004287static void vmx_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004288{
4289 struct vcpu_vmx *vmx = to_vmx(vcpu);
4290 u64 msr;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004291
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004292 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004293
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004294 vmx->soft_vnmi_blocked = 0;
4295
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004296 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004297 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004298 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004299 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004300 msr |= MSR_IA32_APICBASE_BSP;
4301 kvm_set_apic_base(&vmx->vcpu, msr);
4302
Avi Kivity2fb92db2011-04-27 19:42:18 +03004303 vmx_segment_cache_clear(vmx);
4304
Avi Kivity5706be02008-08-20 15:07:31 +03004305 seg_setup(VCPU_SREG_CS);
Jan Kiszka66450a22013-03-13 12:42:34 +01004306 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Paolo Bonzini04b66832013-03-19 16:30:26 +01004307 vmcs_write32(GUEST_CS_BASE, 0xffff0000);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004308
4309 seg_setup(VCPU_SREG_DS);
4310 seg_setup(VCPU_SREG_ES);
4311 seg_setup(VCPU_SREG_FS);
4312 seg_setup(VCPU_SREG_GS);
4313 seg_setup(VCPU_SREG_SS);
4314
4315 vmcs_write16(GUEST_TR_SELECTOR, 0);
4316 vmcs_writel(GUEST_TR_BASE, 0);
4317 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4318 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4319
4320 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4321 vmcs_writel(GUEST_LDTR_BASE, 0);
4322 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4323 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4324
4325 vmcs_write32(GUEST_SYSENTER_CS, 0);
4326 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4327 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4328
4329 vmcs_writel(GUEST_RFLAGS, 0x02);
Jan Kiszka66450a22013-03-13 12:42:34 +01004330 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004331
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004332 vmcs_writel(GUEST_GDTR_BASE, 0);
4333 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4334
4335 vmcs_writel(GUEST_IDTR_BASE, 0);
4336 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4337
Anthony Liguori443381a2010-12-06 10:53:38 -06004338 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004339 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
4340 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
4341
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004342 /* Special registers */
4343 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4344
4345 setup_msrs(vmx);
4346
Avi Kivity6aa8b732006-12-10 02:21:36 -08004347 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4348
Sheng Yangf78e0e22007-10-29 09:40:42 +08004349 if (cpu_has_vmx_tpr_shadow()) {
4350 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
4351 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
4352 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09004353 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004354 vmcs_write32(TPR_THRESHOLD, 0);
4355 }
4356
4357 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4358 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004359 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004360
Yang Zhang01e439b2013-04-11 19:25:12 +08004361 if (vmx_vm_has_apicv(vcpu->kvm))
4362 memset(&vmx->pi_desc, 0, sizeof(struct pi_desc));
4363
Sheng Yang2384d2b2008-01-17 15:14:33 +08004364 if (vmx->vpid != 0)
4365 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4366
Eduardo Habkostfa400522009-10-24 02:49:58 -02004367 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004368 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10004369 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004370 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004371 vmx_fpu_activate(&vmx->vcpu);
4372 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004373
Gui Jianfengb9d762f2010-06-07 10:32:29 +08004374 vpid_sync_context(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004375}
4376
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004377/*
4378 * In nested virtualization, check if L1 asked to exit on external interrupts.
4379 * For most existing hypervisors, this will always return true.
4380 */
4381static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4382{
4383 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4384 PIN_BASED_EXT_INTR_MASK;
4385}
4386
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004387static void enable_irq_window(struct kvm_vcpu *vcpu)
4388{
4389 u32 cpu_based_vm_exec_control;
Nadav Har'Eld6185f22011-09-22 13:52:56 +03004390 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
4391 /*
4392 * We get here if vmx_interrupt_allowed() said we can't
4393 * inject to L1 now because L2 must run. Ask L2 to exit
4394 * right after entry, so we can inject to L1 more promptly.
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004395 */
Nadav Har'Eld6185f22011-09-22 13:52:56 +03004396 kvm_make_request(KVM_REQ_IMMEDIATE_EXIT, vcpu);
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004397 return;
Nadav Har'Eld6185f22011-09-22 13:52:56 +03004398 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004399
4400 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4401 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
4402 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4403}
4404
4405static void enable_nmi_window(struct kvm_vcpu *vcpu)
4406{
4407 u32 cpu_based_vm_exec_control;
4408
4409 if (!cpu_has_virtual_nmis()) {
4410 enable_irq_window(vcpu);
4411 return;
4412 }
4413
Avi Kivity30bd0c42010-11-01 23:20:48 +02004414 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
4415 enable_irq_window(vcpu);
4416 return;
4417 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004418 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4419 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
4420 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4421}
4422
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004423static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004424{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004425 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004426 uint32_t intr;
4427 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004428
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004429 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004430
Avi Kivityfa89a812008-09-01 15:57:51 +03004431 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004432 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004433 int inc_eip = 0;
4434 if (vcpu->arch.interrupt.soft)
4435 inc_eip = vcpu->arch.event_exit_inst_len;
4436 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004437 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004438 return;
4439 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004440 intr = irq | INTR_INFO_VALID_MASK;
4441 if (vcpu->arch.interrupt.soft) {
4442 intr |= INTR_TYPE_SOFT_INTR;
4443 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4444 vmx->vcpu.arch.event_exit_inst_len);
4445 } else
4446 intr |= INTR_TYPE_EXT_INTR;
4447 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03004448}
4449
Sheng Yangf08864b2008-05-15 18:23:25 +08004450static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4451{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004452 struct vcpu_vmx *vmx = to_vmx(vcpu);
4453
Nadav Har'El0b6ac342011-05-25 23:13:36 +03004454 if (is_guest_mode(vcpu))
4455 return;
4456
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004457 if (!cpu_has_virtual_nmis()) {
4458 /*
4459 * Tracking the NMI-blocked state in software is built upon
4460 * finding the next open IRQ window. This, in turn, depends on
4461 * well-behaving guests: They have to keep IRQs disabled at
4462 * least as long as the NMI handler runs. Otherwise we may
4463 * cause NMI nesting, maybe breaking the guest. But as this is
4464 * highly unlikely, we can live with the residual risk.
4465 */
4466 vmx->soft_vnmi_blocked = 1;
4467 vmx->vnmi_blocked_time = 0;
4468 }
4469
Jan Kiszka487b3912008-09-26 09:30:56 +02004470 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02004471 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004472 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004473 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004474 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004475 return;
4476 }
Sheng Yangf08864b2008-05-15 18:23:25 +08004477 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4478 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08004479}
4480
Gleb Natapovc4282df2009-04-21 17:45:07 +03004481static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02004482{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004483 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03004484 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02004485
Gleb Natapovc4282df2009-04-21 17:45:07 +03004486 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
Avi Kivity30bd0c42010-11-01 23:20:48 +02004487 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4488 | GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02004489}
4490
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004491static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
4492{
4493 if (!cpu_has_virtual_nmis())
4494 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02004495 if (to_vmx(vcpu)->nmi_known_unmasked)
4496 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03004497 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004498}
4499
4500static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4501{
4502 struct vcpu_vmx *vmx = to_vmx(vcpu);
4503
4504 if (!cpu_has_virtual_nmis()) {
4505 if (vmx->soft_vnmi_blocked != masked) {
4506 vmx->soft_vnmi_blocked = masked;
4507 vmx->vnmi_blocked_time = 0;
4508 }
4509 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02004510 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004511 if (masked)
4512 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4513 GUEST_INTR_STATE_NMI);
4514 else
4515 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4516 GUEST_INTR_STATE_NMI);
4517 }
4518}
4519
Gleb Natapov78646122009-03-23 12:12:11 +02004520static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4521{
Jan Kiszkae8457c62013-04-14 12:12:48 +02004522 if (is_guest_mode(vcpu)) {
Nadav Har'El51cfe382011-09-22 13:53:26 +03004523 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszkae8457c62013-04-14 12:12:48 +02004524
4525 if (to_vmx(vcpu)->nested.nested_run_pending)
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004526 return 0;
Jan Kiszkae8457c62013-04-14 12:12:48 +02004527 if (nested_exit_on_intr(vcpu)) {
4528 nested_vmx_vmexit(vcpu);
4529 vmcs12->vm_exit_reason =
4530 EXIT_REASON_EXTERNAL_INTERRUPT;
4531 vmcs12->vm_exit_intr_info = 0;
4532 /*
4533 * fall through to normal code, but now in L1, not L2
4534 */
4535 }
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004536 }
4537
Gleb Natapovc4282df2009-04-21 17:45:07 +03004538 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
4539 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4540 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02004541}
4542
Izik Eiduscbc94022007-10-25 00:29:55 +02004543static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4544{
4545 int ret;
4546 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08004547 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02004548 .guest_phys_addr = addr,
4549 .memory_size = PAGE_SIZE * 3,
4550 .flags = 0,
4551 };
4552
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09004553 ret = kvm_set_memory_region(kvm, &tss_mem);
Izik Eiduscbc94022007-10-25 00:29:55 +02004554 if (ret)
4555 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004556 kvm->arch.tss_addr = addr;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004557 if (!init_rmode_tss(kvm))
4558 return -ENOMEM;
4559
Izik Eiduscbc94022007-10-25 00:29:55 +02004560 return 0;
4561}
4562
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004563static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004564{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004565 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004566 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004567 /*
4568 * Update instruction length as we may reinject the exception
4569 * from user space while in guest debugging mode.
4570 */
4571 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4572 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004573 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004574 return false;
4575 /* fall through */
4576 case DB_VECTOR:
4577 if (vcpu->guest_debug &
4578 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4579 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004580 /* fall through */
4581 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004582 case OF_VECTOR:
4583 case BR_VECTOR:
4584 case UD_VECTOR:
4585 case DF_VECTOR:
4586 case SS_VECTOR:
4587 case GP_VECTOR:
4588 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004589 return true;
4590 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004591 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004592 return false;
4593}
4594
4595static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4596 int vec, u32 err_code)
4597{
4598 /*
4599 * Instruction with address size override prefix opcode 0x67
4600 * Cause the #SS fault with 0 error code in VM86 mode.
4601 */
4602 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
4603 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
4604 if (vcpu->arch.halt_request) {
4605 vcpu->arch.halt_request = 0;
4606 return kvm_emulate_halt(vcpu);
4607 }
4608 return 1;
4609 }
4610 return 0;
4611 }
4612
4613 /*
4614 * Forward all other exceptions that are valid in real mode.
4615 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4616 * the required debugging infrastructure rework.
4617 */
4618 kvm_queue_exception(vcpu, vec);
4619 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004620}
4621
Andi Kleena0861c02009-06-08 17:37:09 +08004622/*
4623 * Trigger machine check on the host. We assume all the MSRs are already set up
4624 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4625 * We pass a fake environment to the machine check handler because we want
4626 * the guest to be always treated like user space, no matter what context
4627 * it used internally.
4628 */
4629static void kvm_machine_check(void)
4630{
4631#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4632 struct pt_regs regs = {
4633 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4634 .flags = X86_EFLAGS_IF,
4635 };
4636
4637 do_machine_check(&regs, 0);
4638#endif
4639}
4640
Avi Kivity851ba692009-08-24 11:10:17 +03004641static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004642{
4643 /* already handled by vcpu_run */
4644 return 1;
4645}
4646
Avi Kivity851ba692009-08-24 11:10:17 +03004647static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004648{
Avi Kivity1155f762007-11-22 11:30:47 +02004649 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004650 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004651 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004652 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004653 u32 vect_info;
4654 enum emulation_result er;
4655
Avi Kivity1155f762007-11-22 11:30:47 +02004656 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02004657 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004658
Andi Kleena0861c02009-06-08 17:37:09 +08004659 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03004660 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004661
Jan Kiszkae4a41882008-09-26 09:30:46 +02004662 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02004663 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004664
4665 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03004666 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004667 return 1;
4668 }
4669
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004670 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01004671 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004672 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02004673 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004674 return 1;
4675 }
4676
Avi Kivity6aa8b732006-12-10 02:21:36 -08004677 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004678 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004679 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004680
4681 /*
4682 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4683 * MMIO, it is better to report an internal error.
4684 * See the comments in vmx_handle_exit.
4685 */
4686 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4687 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4688 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4689 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
4690 vcpu->run->internal.ndata = 2;
4691 vcpu->run->internal.data[0] = vect_info;
4692 vcpu->run->internal.data[1] = intr_info;
4693 return 0;
4694 }
4695
Avi Kivity6aa8b732006-12-10 02:21:36 -08004696 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08004697 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02004698 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004699 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004700 trace_kvm_page_fault(cr2, error_code);
4701
Gleb Natapov3298b752009-05-11 13:35:46 +03004702 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03004703 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01004704 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004705 }
4706
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004707 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004708
4709 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4710 return handle_rmode_exception(vcpu, ex_no, error_code);
4711
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004712 switch (ex_no) {
4713 case DB_VECTOR:
4714 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4715 if (!(vcpu->guest_debug &
4716 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
4717 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
4718 kvm_queue_exception(vcpu, DB_VECTOR);
4719 return 1;
4720 }
4721 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4722 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4723 /* fall through */
4724 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01004725 /*
4726 * Update instruction length as we may reinject #BP from
4727 * user space while in guest debugging mode. Reading it for
4728 * #DB as well causes no harm, it is not used in that case.
4729 */
4730 vmx->vcpu.arch.event_exit_inst_len =
4731 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004732 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004733 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004734 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4735 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004736 break;
4737 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004738 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4739 kvm_run->ex.exception = ex_no;
4740 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004741 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004742 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004743 return 0;
4744}
4745
Avi Kivity851ba692009-08-24 11:10:17 +03004746static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004747{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004748 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004749 return 1;
4750}
4751
Avi Kivity851ba692009-08-24 11:10:17 +03004752static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004753{
Avi Kivity851ba692009-08-24 11:10:17 +03004754 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08004755 return 0;
4756}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004757
Avi Kivity851ba692009-08-24 11:10:17 +03004758static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004759{
He, Qingbfdaab02007-09-12 14:18:28 +08004760 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01004761 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004762 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004763
He, Qingbfdaab02007-09-12 14:18:28 +08004764 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02004765 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004766 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004767
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004768 ++vcpu->stat.io_exits;
4769
4770 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01004771 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004772
4773 port = exit_qualification >> 16;
4774 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01004775 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004776
4777 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004778}
4779
Ingo Molnar102d8322007-02-19 14:37:47 +02004780static void
4781vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4782{
4783 /*
4784 * Patch in the VMCALL instruction:
4785 */
4786 hypercall[0] = 0x0f;
4787 hypercall[1] = 0x01;
4788 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004789}
4790
Guo Chao0fa06072012-06-28 15:16:19 +08004791/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004792static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4793{
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004794 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004795 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4796 unsigned long orig_val = val;
4797
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004798 /*
4799 * We get here when L2 changed cr0 in a way that did not change
4800 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004801 * but did change L0 shadowed bits. So we first calculate the
4802 * effective cr0 value that L1 would like to write into the
4803 * hardware. It consists of the L2-owned bits from the new
4804 * value combined with the L1-owned bits from L1's guest_cr0.
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004805 */
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004806 val = (val & ~vmcs12->cr0_guest_host_mask) |
4807 (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
4808
4809 /* TODO: will have to take unrestricted guest mode into
4810 * account */
4811 if ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON)
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004812 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004813
4814 if (kvm_set_cr0(vcpu, val))
4815 return 1;
4816 vmcs_writel(CR0_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004817 return 0;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004818 } else {
4819 if (to_vmx(vcpu)->nested.vmxon &&
4820 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
4821 return 1;
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004822 return kvm_set_cr0(vcpu, val);
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004823 }
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004824}
4825
4826static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4827{
4828 if (is_guest_mode(vcpu)) {
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004829 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4830 unsigned long orig_val = val;
4831
4832 /* analogously to handle_set_cr0 */
4833 val = (val & ~vmcs12->cr4_guest_host_mask) |
4834 (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
4835 if (kvm_set_cr4(vcpu, val))
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004836 return 1;
Jan Kiszka1a0d74e2013-03-07 14:08:07 +01004837 vmcs_writel(CR4_READ_SHADOW, orig_val);
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004838 return 0;
4839 } else
4840 return kvm_set_cr4(vcpu, val);
4841}
4842
4843/* called to set cr0 as approriate for clts instruction exit. */
4844static void handle_clts(struct kvm_vcpu *vcpu)
4845{
4846 if (is_guest_mode(vcpu)) {
4847 /*
4848 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
4849 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
4850 * just pretend it's off (also in arch.cr0 for fpu_activate).
4851 */
4852 vmcs_writel(CR0_READ_SHADOW,
4853 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
4854 vcpu->arch.cr0 &= ~X86_CR0_TS;
4855 } else
4856 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
4857}
4858
Avi Kivity851ba692009-08-24 11:10:17 +03004859static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004860{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004861 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004862 int cr;
4863 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004864 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004865
He, Qingbfdaab02007-09-12 14:18:28 +08004866 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004867 cr = exit_qualification & 15;
4868 reg = (exit_qualification >> 8) & 15;
4869 switch ((exit_qualification >> 4) & 3) {
4870 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004871 val = kvm_register_read(vcpu, reg);
4872 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004873 switch (cr) {
4874 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004875 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004876 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004877 return 1;
4878 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03004879 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004880 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004881 return 1;
4882 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004883 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004884 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004885 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004886 case 8: {
4887 u8 cr8_prev = kvm_get_cr8(vcpu);
4888 u8 cr8 = kvm_register_read(vcpu, reg);
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004889 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004890 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004891 if (irqchip_in_kernel(vcpu->kvm))
4892 return 1;
4893 if (cr8_prev <= cr8)
4894 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03004895 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004896 return 0;
4897 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02004898 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004899 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03004900 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004901 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02004902 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03004903 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02004904 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03004905 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004906 case 1: /*mov from cr*/
4907 switch (cr) {
4908 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02004909 val = kvm_read_cr3(vcpu);
4910 kvm_register_write(vcpu, reg, val);
4911 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004912 skip_emulated_instruction(vcpu);
4913 return 1;
4914 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004915 val = kvm_get_cr8(vcpu);
4916 kvm_register_write(vcpu, reg, val);
4917 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004918 skip_emulated_instruction(vcpu);
4919 return 1;
4920 }
4921 break;
4922 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02004923 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004924 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02004925 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004926
4927 skip_emulated_instruction(vcpu);
4928 return 1;
4929 default:
4930 break;
4931 }
Avi Kivity851ba692009-08-24 11:10:17 +03004932 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03004933 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08004934 (int)(exit_qualification >> 4) & 3, cr);
4935 return 0;
4936}
4937
Avi Kivity851ba692009-08-24 11:10:17 +03004938static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004939{
He, Qingbfdaab02007-09-12 14:18:28 +08004940 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004941 int dr, reg;
4942
Jan Kiszkaf2483412010-01-20 18:20:20 +01004943 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03004944 if (!kvm_require_cpl(vcpu, 0))
4945 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004946 dr = vmcs_readl(GUEST_DR7);
4947 if (dr & DR7_GD) {
4948 /*
4949 * As the vm-exit takes precedence over the debug trap, we
4950 * need to emulate the latter, either for the host or the
4951 * guest debugging itself.
4952 */
4953 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03004954 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
4955 vcpu->run->debug.arch.dr7 = dr;
4956 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004957 vmcs_readl(GUEST_CS_BASE) +
4958 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03004959 vcpu->run->debug.arch.exception = DB_VECTOR;
4960 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004961 return 0;
4962 } else {
4963 vcpu->arch.dr7 &= ~DR7_GD;
4964 vcpu->arch.dr6 |= DR6_BD;
4965 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
4966 kvm_queue_exception(vcpu, DB_VECTOR);
4967 return 1;
4968 }
4969 }
4970
He, Qingbfdaab02007-09-12 14:18:28 +08004971 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004972 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
4973 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
4974 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03004975 unsigned long val;
4976 if (!kvm_get_dr(vcpu, dr, &val))
4977 kvm_register_write(vcpu, reg, val);
4978 } else
4979 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004980 skip_emulated_instruction(vcpu);
4981 return 1;
4982}
4983
Gleb Natapov020df072010-04-13 10:05:23 +03004984static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
4985{
4986 vmcs_writel(GUEST_DR7, val);
4987}
4988
Avi Kivity851ba692009-08-24 11:10:17 +03004989static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004990{
Avi Kivity06465c52007-02-28 20:46:53 +02004991 kvm_emulate_cpuid(vcpu);
4992 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004993}
4994
Avi Kivity851ba692009-08-24 11:10:17 +03004995static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004996{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004997 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004998 u64 data;
4999
5000 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02005001 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005002 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005003 return 1;
5004 }
5005
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005006 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005007
Avi Kivity6aa8b732006-12-10 02:21:36 -08005008 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005009 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
5010 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005011 skip_emulated_instruction(vcpu);
5012 return 1;
5013}
5014
Avi Kivity851ba692009-08-24 11:10:17 +03005015static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005016{
Will Auld8fe8ab42012-11-29 12:42:12 -08005017 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08005018 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
5019 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
5020 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005021
Will Auld8fe8ab42012-11-29 12:42:12 -08005022 msr.data = data;
5023 msr.index = ecx;
5024 msr.host_initiated = false;
5025 if (vmx_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02005026 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02005027 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005028 return 1;
5029 }
5030
Avi Kivity59200272010-01-25 19:47:02 +02005031 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005032 skip_emulated_instruction(vcpu);
5033 return 1;
5034}
5035
Avi Kivity851ba692009-08-24 11:10:17 +03005036static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005037{
Avi Kivity3842d132010-07-27 12:30:24 +03005038 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005039 return 1;
5040}
5041
Avi Kivity851ba692009-08-24 11:10:17 +03005042static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005043{
Eddie Dong85f455f2007-07-06 12:20:49 +03005044 u32 cpu_based_vm_exec_control;
5045
5046 /* clear pending irq */
5047 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5048 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
5049 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005050
Avi Kivity3842d132010-07-27 12:30:24 +03005051 kvm_make_request(KVM_REQ_EVENT, vcpu);
5052
Jan Kiszkaa26bf122008-09-26 09:30:45 +02005053 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04005054
Dor Laorc1150d82007-01-05 16:36:24 -08005055 /*
5056 * If the user space waits to inject interrupts, exit as soon as
5057 * possible
5058 */
Gleb Natapov80618232009-04-21 17:44:56 +03005059 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03005060 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03005061 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03005062 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08005063 return 0;
5064 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005065 return 1;
5066}
5067
Avi Kivity851ba692009-08-24 11:10:17 +03005068static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005069{
5070 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03005071 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005072}
5073
Avi Kivity851ba692009-08-24 11:10:17 +03005074static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02005075{
Dor Laor510043d2007-02-19 18:25:43 +02005076 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05005077 kvm_emulate_hypercall(vcpu);
5078 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02005079}
5080
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005081static int handle_invd(struct kvm_vcpu *vcpu)
5082{
Andre Przywara51d8b662010-12-21 11:12:02 +01005083 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005084}
5085
Avi Kivity851ba692009-08-24 11:10:17 +03005086static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03005087{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005088 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03005089
5090 kvm_mmu_invlpg(vcpu, exit_qualification);
5091 skip_emulated_instruction(vcpu);
5092 return 1;
5093}
5094
Avi Kivityfee84b02011-11-10 14:57:25 +02005095static int handle_rdpmc(struct kvm_vcpu *vcpu)
5096{
5097 int err;
5098
5099 err = kvm_rdpmc(vcpu);
5100 kvm_complete_insn_gp(vcpu, err);
5101
5102 return 1;
5103}
5104
Avi Kivity851ba692009-08-24 11:10:17 +03005105static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02005106{
5107 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005108 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02005109 return 1;
5110}
5111
Dexuan Cui2acf9232010-06-10 11:27:12 +08005112static int handle_xsetbv(struct kvm_vcpu *vcpu)
5113{
5114 u64 new_bv = kvm_read_edx_eax(vcpu);
5115 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
5116
5117 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5118 skip_emulated_instruction(vcpu);
5119 return 1;
5120}
5121
Avi Kivity851ba692009-08-24 11:10:17 +03005122static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08005123{
Kevin Tian58fbbf22011-08-30 13:56:17 +03005124 if (likely(fasteoi)) {
5125 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5126 int access_type, offset;
5127
5128 access_type = exit_qualification & APIC_ACCESS_TYPE;
5129 offset = exit_qualification & APIC_ACCESS_OFFSET;
5130 /*
5131 * Sane guest uses MOV to write EOI, with written value
5132 * not cared. So make a short-circuit here by avoiding
5133 * heavy instruction emulation.
5134 */
5135 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5136 (offset == APIC_EOI)) {
5137 kvm_lapic_set_eoi(vcpu);
5138 skip_emulated_instruction(vcpu);
5139 return 1;
5140 }
5141 }
Andre Przywara51d8b662010-12-21 11:12:02 +01005142 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08005143}
5144
Yang Zhangc7c9c562013-01-25 10:18:51 +08005145static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5146{
5147 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5148 int vector = exit_qualification & 0xff;
5149
5150 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5151 kvm_apic_set_eoi_accelerated(vcpu, vector);
5152 return 1;
5153}
5154
Yang Zhang83d4c282013-01-25 10:18:49 +08005155static int handle_apic_write(struct kvm_vcpu *vcpu)
5156{
5157 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5158 u32 offset = exit_qualification & 0xfff;
5159
5160 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5161 kvm_apic_write_nodecode(vcpu, offset);
5162 return 1;
5163}
5164
Avi Kivity851ba692009-08-24 11:10:17 +03005165static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02005166{
Jan Kiszka60637aa2008-09-26 09:30:47 +02005167 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02005168 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02005169 bool has_error_code = false;
5170 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02005171 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005172 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005173
5174 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005175 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005176 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02005177
5178 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5179
5180 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005181 if (reason == TASK_SWITCH_GATE && idt_v) {
5182 switch (type) {
5183 case INTR_TYPE_NMI_INTR:
5184 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02005185 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005186 break;
5187 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03005188 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005189 kvm_clear_interrupt_queue(vcpu);
5190 break;
5191 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02005192 if (vmx->idt_vectoring_info &
5193 VECTORING_INFO_DELIVER_CODE_MASK) {
5194 has_error_code = true;
5195 error_code =
5196 vmcs_read32(IDT_VECTORING_ERROR_CODE);
5197 }
5198 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005199 case INTR_TYPE_SOFT_EXCEPTION:
5200 kvm_clear_exception_queue(vcpu);
5201 break;
5202 default:
5203 break;
5204 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005205 }
Izik Eidus37817f22008-03-24 23:14:53 +02005206 tss_selector = exit_qualification;
5207
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005208 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5209 type != INTR_TYPE_EXT_INTR &&
5210 type != INTR_TYPE_NMI_INTR))
5211 skip_emulated_instruction(vcpu);
5212
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005213 if (kvm_task_switch(vcpu, tss_selector,
5214 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
5215 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03005216 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5217 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5218 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005219 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03005220 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005221
5222 /* clear all local breakpoint enable flags */
5223 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
5224
5225 /*
5226 * TODO: What about debug traps on tss switch?
5227 * Are we supposed to inject them and update dr6?
5228 */
5229
5230 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02005231}
5232
Avi Kivity851ba692009-08-24 11:10:17 +03005233static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005234{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005235 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005236 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005237 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005238 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08005239
Sheng Yangf9c617f2009-03-25 10:08:52 +08005240 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08005241
Sheng Yang14394422008-04-28 12:24:45 +08005242 gla_validity = (exit_qualification >> 7) & 0x3;
5243 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
5244 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
5245 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
5246 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08005247 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08005248 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
5249 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03005250 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5251 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03005252 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08005253 }
5254
5255 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005256 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005257
5258 /* It is a write fault? */
5259 error_code = exit_qualification & (1U << 1);
5260 /* ept page table is present? */
5261 error_code |= (exit_qualification >> 3) & 0x1;
5262
5263 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005264}
5265
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005266static u64 ept_rsvd_mask(u64 spte, int level)
5267{
5268 int i;
5269 u64 mask = 0;
5270
5271 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
5272 mask |= (1ULL << i);
5273
5274 if (level > 2)
5275 /* bits 7:3 reserved */
5276 mask |= 0xf8;
5277 else if (level == 2) {
5278 if (spte & (1ULL << 7))
5279 /* 2MB ref, bits 20:12 reserved */
5280 mask |= 0x1ff000;
5281 else
5282 /* bits 6:3 reserved */
5283 mask |= 0x78;
5284 }
5285
5286 return mask;
5287}
5288
5289static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
5290 int level)
5291{
5292 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
5293
5294 /* 010b (write-only) */
5295 WARN_ON((spte & 0x7) == 0x2);
5296
5297 /* 110b (write/execute) */
5298 WARN_ON((spte & 0x7) == 0x6);
5299
5300 /* 100b (execute-only) and value not supported by logical processor */
5301 if (!cpu_has_vmx_ept_execute_only())
5302 WARN_ON((spte & 0x7) == 0x4);
5303
5304 /* not 000b */
5305 if ((spte & 0x7)) {
5306 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
5307
5308 if (rsvd_bits != 0) {
5309 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
5310 __func__, rsvd_bits);
5311 WARN_ON(1);
5312 }
5313
5314 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
5315 u64 ept_mem_type = (spte & 0x38) >> 3;
5316
5317 if (ept_mem_type == 2 || ept_mem_type == 3 ||
5318 ept_mem_type == 7) {
5319 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
5320 __func__, ept_mem_type);
5321 WARN_ON(1);
5322 }
5323 }
5324 }
5325}
5326
Avi Kivity851ba692009-08-24 11:10:17 +03005327static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005328{
5329 u64 sptes[4];
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005330 int nr_sptes, i, ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005331 gpa_t gpa;
5332
5333 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
5334
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005335 ret = handle_mmio_page_fault_common(vcpu, gpa, true);
5336 if (likely(ret == 1))
5337 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
5338 EMULATE_DONE;
5339 if (unlikely(!ret))
5340 return 1;
5341
5342 /* It is the real ept misconfig */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005343 printk(KERN_ERR "EPT: Misconfiguration.\n");
5344 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
5345
5346 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
5347
5348 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
5349 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
5350
Avi Kivity851ba692009-08-24 11:10:17 +03005351 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5352 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005353
5354 return 0;
5355}
5356
Avi Kivity851ba692009-08-24 11:10:17 +03005357static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005358{
5359 u32 cpu_based_vm_exec_control;
5360
5361 /* clear pending NMI */
5362 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5363 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
5364 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5365 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005366 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005367
5368 return 1;
5369}
5370
Mohammed Gamal80ced182009-09-01 12:48:18 +02005371static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005372{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005373 struct vcpu_vmx *vmx = to_vmx(vcpu);
5374 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005375 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02005376 u32 cpu_exec_ctrl;
5377 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005378 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005379
5380 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5381 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005382
Avi Kivityb8405c12012-06-07 17:08:48 +03005383 while (!guest_state_valid(vcpu) && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03005384 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005385 return handle_interrupt_window(&vmx->vcpu);
5386
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005387 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
5388 return 1;
5389
Gleb Natapov991eebf2013-04-11 12:10:51 +03005390 err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005391
Mohammed Gamal80ced182009-09-01 12:48:18 +02005392 if (err == EMULATE_DO_MMIO) {
5393 ret = 0;
5394 goto out;
5395 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005396
Avi Kivityde5f70e2012-06-12 20:22:28 +03005397 if (err != EMULATE_DONE) {
5398 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5399 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5400 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005401 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03005402 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005403
5404 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02005405 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005406 if (need_resched())
5407 schedule();
5408 }
5409
Gleb Natapov14168782013-01-21 15:36:49 +02005410 vmx->emulation_required = emulation_required(vcpu);
Mohammed Gamal80ced182009-09-01 12:48:18 +02005411out:
5412 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005413}
5414
Avi Kivity6aa8b732006-12-10 02:21:36 -08005415/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005416 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5417 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5418 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005419static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005420{
5421 skip_emulated_instruction(vcpu);
5422 kvm_vcpu_on_spin(vcpu);
5423
5424 return 1;
5425}
5426
Sheng Yang59708672009-12-15 13:29:54 +08005427static int handle_invalid_op(struct kvm_vcpu *vcpu)
5428{
5429 kvm_queue_exception(vcpu, UD_VECTOR);
5430 return 1;
5431}
5432
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005433/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005434 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
5435 * We could reuse a single VMCS for all the L2 guests, but we also want the
5436 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
5437 * allows keeping them loaded on the processor, and in the future will allow
5438 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
5439 * every entry if they never change.
5440 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
5441 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
5442 *
5443 * The following functions allocate and free a vmcs02 in this pool.
5444 */
5445
5446/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
5447static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
5448{
5449 struct vmcs02_list *item;
5450 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5451 if (item->vmptr == vmx->nested.current_vmptr) {
5452 list_move(&item->list, &vmx->nested.vmcs02_pool);
5453 return &item->vmcs02;
5454 }
5455
5456 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
5457 /* Recycle the least recently used VMCS. */
5458 item = list_entry(vmx->nested.vmcs02_pool.prev,
5459 struct vmcs02_list, list);
5460 item->vmptr = vmx->nested.current_vmptr;
5461 list_move(&item->list, &vmx->nested.vmcs02_pool);
5462 return &item->vmcs02;
5463 }
5464
5465 /* Create a new VMCS */
Ioan Orghici0fa24ce2013-03-10 15:46:00 +02005466 item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005467 if (!item)
5468 return NULL;
5469 item->vmcs02.vmcs = alloc_vmcs();
5470 if (!item->vmcs02.vmcs) {
5471 kfree(item);
5472 return NULL;
5473 }
5474 loaded_vmcs_init(&item->vmcs02);
5475 item->vmptr = vmx->nested.current_vmptr;
5476 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
5477 vmx->nested.vmcs02_num++;
5478 return &item->vmcs02;
5479}
5480
5481/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
5482static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
5483{
5484 struct vmcs02_list *item;
5485 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5486 if (item->vmptr == vmptr) {
5487 free_loaded_vmcs(&item->vmcs02);
5488 list_del(&item->list);
5489 kfree(item);
5490 vmx->nested.vmcs02_num--;
5491 return;
5492 }
5493}
5494
5495/*
5496 * Free all VMCSs saved for this vcpu, except the one pointed by
5497 * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
5498 * currently used, if running L2), and vmcs01 when running L2.
5499 */
5500static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
5501{
5502 struct vmcs02_list *item, *n;
5503 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
5504 if (vmx->loaded_vmcs != &item->vmcs02)
5505 free_loaded_vmcs(&item->vmcs02);
5506 list_del(&item->list);
5507 kfree(item);
5508 }
5509 vmx->nested.vmcs02_num = 0;
5510
5511 if (vmx->loaded_vmcs != &vmx->vmcs01)
5512 free_loaded_vmcs(&vmx->vmcs01);
5513}
5514
Abel Gordon145c28d2013-04-18 14:36:55 +03005515static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
5516 u32 vm_instruction_error);
5517
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005518/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005519 * Emulate the VMXON instruction.
5520 * Currently, we just remember that VMX is active, and do not save or even
5521 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
5522 * do not currently need to store anything in that guest-allocated memory
5523 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
5524 * argument is different from the VMXON pointer (which the spec says they do).
5525 */
5526static int handle_vmon(struct kvm_vcpu *vcpu)
5527{
5528 struct kvm_segment cs;
5529 struct vcpu_vmx *vmx = to_vmx(vcpu);
5530
5531 /* The Intel VMX Instruction Reference lists a bunch of bits that
5532 * are prerequisite to running VMXON, most notably cr4.VMXE must be
5533 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
5534 * Otherwise, we should fail with #UD. We test these now:
5535 */
5536 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
5537 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
5538 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5539 kvm_queue_exception(vcpu, UD_VECTOR);
5540 return 1;
5541 }
5542
5543 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5544 if (is_long_mode(vcpu) && !cs.l) {
5545 kvm_queue_exception(vcpu, UD_VECTOR);
5546 return 1;
5547 }
5548
5549 if (vmx_get_cpl(vcpu)) {
5550 kvm_inject_gp(vcpu, 0);
5551 return 1;
5552 }
Abel Gordon145c28d2013-04-18 14:36:55 +03005553 if (vmx->nested.vmxon) {
5554 nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION);
5555 skip_emulated_instruction(vcpu);
5556 return 1;
5557 }
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005558
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005559 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
5560 vmx->nested.vmcs02_num = 0;
5561
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005562 vmx->nested.vmxon = true;
5563
5564 skip_emulated_instruction(vcpu);
5565 return 1;
5566}
5567
5568/*
5569 * Intel's VMX Instruction Reference specifies a common set of prerequisites
5570 * for running VMX instructions (except VMXON, whose prerequisites are
5571 * slightly different). It also specifies what exception to inject otherwise.
5572 */
5573static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
5574{
5575 struct kvm_segment cs;
5576 struct vcpu_vmx *vmx = to_vmx(vcpu);
5577
5578 if (!vmx->nested.vmxon) {
5579 kvm_queue_exception(vcpu, UD_VECTOR);
5580 return 0;
5581 }
5582
5583 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5584 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
5585 (is_long_mode(vcpu) && !cs.l)) {
5586 kvm_queue_exception(vcpu, UD_VECTOR);
5587 return 0;
5588 }
5589
5590 if (vmx_get_cpl(vcpu)) {
5591 kvm_inject_gp(vcpu, 0);
5592 return 0;
5593 }
5594
5595 return 1;
5596}
5597
5598/*
5599 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
5600 * just stops using VMX.
5601 */
5602static void free_nested(struct vcpu_vmx *vmx)
5603{
5604 if (!vmx->nested.vmxon)
5605 return;
5606 vmx->nested.vmxon = false;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03005607 if (vmx->nested.current_vmptr != -1ull) {
5608 kunmap(vmx->nested.current_vmcs12_page);
5609 nested_release_page(vmx->nested.current_vmcs12_page);
5610 vmx->nested.current_vmptr = -1ull;
5611 vmx->nested.current_vmcs12 = NULL;
5612 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005613 /* Unpin physical memory we referred to in current vmcs02 */
5614 if (vmx->nested.apic_access_page) {
5615 nested_release_page(vmx->nested.apic_access_page);
5616 vmx->nested.apic_access_page = 0;
5617 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005618
5619 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005620}
5621
5622/* Emulate the VMXOFF instruction */
5623static int handle_vmoff(struct kvm_vcpu *vcpu)
5624{
5625 if (!nested_vmx_check_permission(vcpu))
5626 return 1;
5627 free_nested(to_vmx(vcpu));
5628 skip_emulated_instruction(vcpu);
5629 return 1;
5630}
5631
5632/*
Nadav Har'El064aea72011-05-25 23:04:56 +03005633 * Decode the memory-address operand of a vmx instruction, as recorded on an
5634 * exit caused by such an instruction (run by a guest hypervisor).
5635 * On success, returns 0. When the operand is invalid, returns 1 and throws
5636 * #UD or #GP.
5637 */
5638static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
5639 unsigned long exit_qualification,
5640 u32 vmx_instruction_info, gva_t *ret)
5641{
5642 /*
5643 * According to Vol. 3B, "Information for VM Exits Due to Instruction
5644 * Execution", on an exit, vmx_instruction_info holds most of the
5645 * addressing components of the operand. Only the displacement part
5646 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
5647 * For how an actual address is calculated from all these components,
5648 * refer to Vol. 1, "Operand Addressing".
5649 */
5650 int scaling = vmx_instruction_info & 3;
5651 int addr_size = (vmx_instruction_info >> 7) & 7;
5652 bool is_reg = vmx_instruction_info & (1u << 10);
5653 int seg_reg = (vmx_instruction_info >> 15) & 7;
5654 int index_reg = (vmx_instruction_info >> 18) & 0xf;
5655 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
5656 int base_reg = (vmx_instruction_info >> 23) & 0xf;
5657 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
5658
5659 if (is_reg) {
5660 kvm_queue_exception(vcpu, UD_VECTOR);
5661 return 1;
5662 }
5663
5664 /* Addr = segment_base + offset */
5665 /* offset = base + [index * scale] + displacement */
5666 *ret = vmx_get_segment_base(vcpu, seg_reg);
5667 if (base_is_valid)
5668 *ret += kvm_register_read(vcpu, base_reg);
5669 if (index_is_valid)
5670 *ret += kvm_register_read(vcpu, index_reg)<<scaling;
5671 *ret += exit_qualification; /* holds the displacement */
5672
5673 if (addr_size == 1) /* 32 bit */
5674 *ret &= 0xffffffff;
5675
5676 /*
5677 * TODO: throw #GP (and return 1) in various cases that the VM*
5678 * instructions require it - e.g., offset beyond segment limit,
5679 * unusable or unreadable/unwritable segment, non-canonical 64-bit
5680 * address, and so on. Currently these are not checked.
5681 */
5682 return 0;
5683}
5684
5685/*
Nadav Har'El0140cae2011-05-25 23:06:28 +03005686 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
5687 * set the success or error code of an emulated VMX instruction, as specified
5688 * by Vol 2B, VMX Instruction Reference, "Conventions".
5689 */
5690static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
5691{
5692 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
5693 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5694 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
5695}
5696
5697static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
5698{
5699 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5700 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
5701 X86_EFLAGS_SF | X86_EFLAGS_OF))
5702 | X86_EFLAGS_CF);
5703}
5704
5705static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
5706 u32 vm_instruction_error)
5707{
5708 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
5709 /*
5710 * failValid writes the error number to the current VMCS, which
5711 * can't be done there isn't a current VMCS.
5712 */
5713 nested_vmx_failInvalid(vcpu);
5714 return;
5715 }
5716 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5717 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5718 X86_EFLAGS_SF | X86_EFLAGS_OF))
5719 | X86_EFLAGS_ZF);
5720 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
5721}
5722
Nadav Har'El27d6c862011-05-25 23:06:59 +03005723/* Emulate the VMCLEAR instruction */
5724static int handle_vmclear(struct kvm_vcpu *vcpu)
5725{
5726 struct vcpu_vmx *vmx = to_vmx(vcpu);
5727 gva_t gva;
5728 gpa_t vmptr;
5729 struct vmcs12 *vmcs12;
5730 struct page *page;
5731 struct x86_exception e;
5732
5733 if (!nested_vmx_check_permission(vcpu))
5734 return 1;
5735
5736 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5737 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5738 return 1;
5739
5740 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5741 sizeof(vmptr), &e)) {
5742 kvm_inject_page_fault(vcpu, &e);
5743 return 1;
5744 }
5745
5746 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
5747 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
5748 skip_emulated_instruction(vcpu);
5749 return 1;
5750 }
5751
5752 if (vmptr == vmx->nested.current_vmptr) {
5753 kunmap(vmx->nested.current_vmcs12_page);
5754 nested_release_page(vmx->nested.current_vmcs12_page);
5755 vmx->nested.current_vmptr = -1ull;
5756 vmx->nested.current_vmcs12 = NULL;
5757 }
5758
5759 page = nested_get_page(vcpu, vmptr);
5760 if (page == NULL) {
5761 /*
5762 * For accurate processor emulation, VMCLEAR beyond available
5763 * physical memory should do nothing at all. However, it is
5764 * possible that a nested vmx bug, not a guest hypervisor bug,
5765 * resulted in this case, so let's shut down before doing any
5766 * more damage:
5767 */
5768 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5769 return 1;
5770 }
5771 vmcs12 = kmap(page);
5772 vmcs12->launch_state = 0;
5773 kunmap(page);
5774 nested_release_page(page);
5775
5776 nested_free_vmcs02(vmx, vmptr);
5777
5778 skip_emulated_instruction(vcpu);
5779 nested_vmx_succeed(vcpu);
5780 return 1;
5781}
5782
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03005783static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
5784
5785/* Emulate the VMLAUNCH instruction */
5786static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5787{
5788 return nested_vmx_run(vcpu, true);
5789}
5790
5791/* Emulate the VMRESUME instruction */
5792static int handle_vmresume(struct kvm_vcpu *vcpu)
5793{
5794
5795 return nested_vmx_run(vcpu, false);
5796}
5797
Nadav Har'El49f705c2011-05-25 23:08:30 +03005798enum vmcs_field_type {
5799 VMCS_FIELD_TYPE_U16 = 0,
5800 VMCS_FIELD_TYPE_U64 = 1,
5801 VMCS_FIELD_TYPE_U32 = 2,
5802 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
5803};
5804
5805static inline int vmcs_field_type(unsigned long field)
5806{
5807 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
5808 return VMCS_FIELD_TYPE_U32;
5809 return (field >> 13) & 0x3 ;
5810}
5811
5812static inline int vmcs_field_readonly(unsigned long field)
5813{
5814 return (((field >> 10) & 0x3) == 1);
5815}
5816
5817/*
5818 * Read a vmcs12 field. Since these can have varying lengths and we return
5819 * one type, we chose the biggest type (u64) and zero-extend the return value
5820 * to that size. Note that the caller, handle_vmread, might need to use only
5821 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
5822 * 64-bit fields are to be returned).
5823 */
5824static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
5825 unsigned long field, u64 *ret)
5826{
5827 short offset = vmcs_field_to_offset(field);
5828 char *p;
5829
5830 if (offset < 0)
5831 return 0;
5832
5833 p = ((char *)(get_vmcs12(vcpu))) + offset;
5834
5835 switch (vmcs_field_type(field)) {
5836 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
5837 *ret = *((natural_width *)p);
5838 return 1;
5839 case VMCS_FIELD_TYPE_U16:
5840 *ret = *((u16 *)p);
5841 return 1;
5842 case VMCS_FIELD_TYPE_U32:
5843 *ret = *((u32 *)p);
5844 return 1;
5845 case VMCS_FIELD_TYPE_U64:
5846 *ret = *((u64 *)p);
5847 return 1;
5848 default:
5849 return 0; /* can never happen. */
5850 }
5851}
5852
Abel Gordon20b97fe2013-04-18 14:36:25 +03005853
5854static inline bool vmcs12_write_any(struct kvm_vcpu *vcpu,
5855 unsigned long field, u64 field_value){
5856 short offset = vmcs_field_to_offset(field);
5857 char *p = ((char *) get_vmcs12(vcpu)) + offset;
5858 if (offset < 0)
5859 return false;
5860
5861 switch (vmcs_field_type(field)) {
5862 case VMCS_FIELD_TYPE_U16:
5863 *(u16 *)p = field_value;
5864 return true;
5865 case VMCS_FIELD_TYPE_U32:
5866 *(u32 *)p = field_value;
5867 return true;
5868 case VMCS_FIELD_TYPE_U64:
5869 *(u64 *)p = field_value;
5870 return true;
5871 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
5872 *(natural_width *)p = field_value;
5873 return true;
5874 default:
5875 return false; /* can never happen. */
5876 }
5877
5878}
5879
Nadav Har'El49f705c2011-05-25 23:08:30 +03005880/*
5881 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
5882 * used before) all generate the same failure when it is missing.
5883 */
5884static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
5885{
5886 struct vcpu_vmx *vmx = to_vmx(vcpu);
5887 if (vmx->nested.current_vmptr == -1ull) {
5888 nested_vmx_failInvalid(vcpu);
5889 skip_emulated_instruction(vcpu);
5890 return 0;
5891 }
5892 return 1;
5893}
5894
5895static int handle_vmread(struct kvm_vcpu *vcpu)
5896{
5897 unsigned long field;
5898 u64 field_value;
5899 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5900 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5901 gva_t gva = 0;
5902
5903 if (!nested_vmx_check_permission(vcpu) ||
5904 !nested_vmx_check_vmcs12(vcpu))
5905 return 1;
5906
5907 /* Decode instruction info and find the field to read */
5908 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5909 /* Read the field, zero-extended to a u64 field_value */
5910 if (!vmcs12_read_any(vcpu, field, &field_value)) {
5911 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5912 skip_emulated_instruction(vcpu);
5913 return 1;
5914 }
5915 /*
5916 * Now copy part of this value to register or memory, as requested.
5917 * Note that the number of bits actually copied is 32 or 64 depending
5918 * on the guest's mode (32 or 64 bit), not on the given field's length.
5919 */
5920 if (vmx_instruction_info & (1u << 10)) {
5921 kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
5922 field_value);
5923 } else {
5924 if (get_vmx_mem_address(vcpu, exit_qualification,
5925 vmx_instruction_info, &gva))
5926 return 1;
5927 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
5928 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
5929 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
5930 }
5931
5932 nested_vmx_succeed(vcpu);
5933 skip_emulated_instruction(vcpu);
5934 return 1;
5935}
5936
5937
5938static int handle_vmwrite(struct kvm_vcpu *vcpu)
5939{
5940 unsigned long field;
5941 gva_t gva;
5942 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5943 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
Nadav Har'El49f705c2011-05-25 23:08:30 +03005944 /* The value to write might be 32 or 64 bits, depending on L1's long
5945 * mode, and eventually we need to write that into a field of several
5946 * possible lengths. The code below first zero-extends the value to 64
5947 * bit (field_value), and then copies only the approriate number of
5948 * bits into the vmcs12 field.
5949 */
5950 u64 field_value = 0;
5951 struct x86_exception e;
5952
5953 if (!nested_vmx_check_permission(vcpu) ||
5954 !nested_vmx_check_vmcs12(vcpu))
5955 return 1;
5956
5957 if (vmx_instruction_info & (1u << 10))
5958 field_value = kvm_register_read(vcpu,
5959 (((vmx_instruction_info) >> 3) & 0xf));
5960 else {
5961 if (get_vmx_mem_address(vcpu, exit_qualification,
5962 vmx_instruction_info, &gva))
5963 return 1;
5964 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
5965 &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) {
5966 kvm_inject_page_fault(vcpu, &e);
5967 return 1;
5968 }
5969 }
5970
5971
5972 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5973 if (vmcs_field_readonly(field)) {
5974 nested_vmx_failValid(vcpu,
5975 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
5976 skip_emulated_instruction(vcpu);
5977 return 1;
5978 }
5979
Abel Gordon20b97fe2013-04-18 14:36:25 +03005980 if (!vmcs12_write_any(vcpu, field, field_value)) {
Nadav Har'El49f705c2011-05-25 23:08:30 +03005981 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5982 skip_emulated_instruction(vcpu);
5983 return 1;
5984 }
5985
5986 nested_vmx_succeed(vcpu);
5987 skip_emulated_instruction(vcpu);
5988 return 1;
5989}
5990
Nadav Har'El63846662011-05-25 23:07:29 +03005991/* Emulate the VMPTRLD instruction */
5992static int handle_vmptrld(struct kvm_vcpu *vcpu)
5993{
5994 struct vcpu_vmx *vmx = to_vmx(vcpu);
5995 gva_t gva;
5996 gpa_t vmptr;
5997 struct x86_exception e;
5998
5999 if (!nested_vmx_check_permission(vcpu))
6000 return 1;
6001
6002 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
6003 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
6004 return 1;
6005
6006 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
6007 sizeof(vmptr), &e)) {
6008 kvm_inject_page_fault(vcpu, &e);
6009 return 1;
6010 }
6011
6012 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
6013 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
6014 skip_emulated_instruction(vcpu);
6015 return 1;
6016 }
6017
6018 if (vmx->nested.current_vmptr != vmptr) {
6019 struct vmcs12 *new_vmcs12;
6020 struct page *page;
6021 page = nested_get_page(vcpu, vmptr);
6022 if (page == NULL) {
6023 nested_vmx_failInvalid(vcpu);
6024 skip_emulated_instruction(vcpu);
6025 return 1;
6026 }
6027 new_vmcs12 = kmap(page);
6028 if (new_vmcs12->revision_id != VMCS12_REVISION) {
6029 kunmap(page);
6030 nested_release_page_clean(page);
6031 nested_vmx_failValid(vcpu,
6032 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
6033 skip_emulated_instruction(vcpu);
6034 return 1;
6035 }
6036 if (vmx->nested.current_vmptr != -1ull) {
6037 kunmap(vmx->nested.current_vmcs12_page);
6038 nested_release_page(vmx->nested.current_vmcs12_page);
6039 }
6040
6041 vmx->nested.current_vmptr = vmptr;
6042 vmx->nested.current_vmcs12 = new_vmcs12;
6043 vmx->nested.current_vmcs12_page = page;
6044 }
6045
6046 nested_vmx_succeed(vcpu);
6047 skip_emulated_instruction(vcpu);
6048 return 1;
6049}
6050
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006051/* Emulate the VMPTRST instruction */
6052static int handle_vmptrst(struct kvm_vcpu *vcpu)
6053{
6054 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6055 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6056 gva_t vmcs_gva;
6057 struct x86_exception e;
6058
6059 if (!nested_vmx_check_permission(vcpu))
6060 return 1;
6061
6062 if (get_vmx_mem_address(vcpu, exit_qualification,
6063 vmx_instruction_info, &vmcs_gva))
6064 return 1;
6065 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
6066 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
6067 (void *)&to_vmx(vcpu)->nested.current_vmptr,
6068 sizeof(u64), &e)) {
6069 kvm_inject_page_fault(vcpu, &e);
6070 return 1;
6071 }
6072 nested_vmx_succeed(vcpu);
6073 skip_emulated_instruction(vcpu);
6074 return 1;
6075}
6076
Nadav Har'El0140cae2011-05-25 23:06:28 +03006077/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08006078 * The exit handlers return 1 if the exit was handled fully and guest execution
6079 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
6080 * to be done to userspace and return 0.
6081 */
Mathias Krause772e0312012-08-30 01:30:19 +02006082static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08006083 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
6084 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08006085 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08006086 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006087 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006088 [EXIT_REASON_CR_ACCESS] = handle_cr,
6089 [EXIT_REASON_DR_ACCESS] = handle_dr,
6090 [EXIT_REASON_CPUID] = handle_cpuid,
6091 [EXIT_REASON_MSR_READ] = handle_rdmsr,
6092 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
6093 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
6094 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02006095 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03006096 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02006097 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02006098 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03006099 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006100 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03006101 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03006102 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03006103 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006104 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03006105 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006106 [EXIT_REASON_VMOFF] = handle_vmoff,
6107 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08006108 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
6109 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08006110 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08006111 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02006112 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08006113 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02006114 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08006115 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03006116 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
6117 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08006118 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08006119 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
6120 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08006121};
6122
6123static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04006124 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006125
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006126static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu,
6127 struct vmcs12 *vmcs12)
6128{
6129 unsigned long exit_qualification;
6130 gpa_t bitmap, last_bitmap;
6131 unsigned int port;
6132 int size;
6133 u8 b;
6134
6135 if (nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING))
6136 return 1;
6137
6138 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS))
6139 return 0;
6140
6141 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6142
6143 port = exit_qualification >> 16;
6144 size = (exit_qualification & 7) + 1;
6145
6146 last_bitmap = (gpa_t)-1;
6147 b = -1;
6148
6149 while (size > 0) {
6150 if (port < 0x8000)
6151 bitmap = vmcs12->io_bitmap_a;
6152 else if (port < 0x10000)
6153 bitmap = vmcs12->io_bitmap_b;
6154 else
6155 return 1;
6156 bitmap += (port & 0x7fff) / 8;
6157
6158 if (last_bitmap != bitmap)
6159 if (kvm_read_guest(vcpu->kvm, bitmap, &b, 1))
6160 return 1;
6161 if (b & (1 << (port & 7)))
6162 return 1;
6163
6164 port++;
6165 size--;
6166 last_bitmap = bitmap;
6167 }
6168
6169 return 0;
6170}
6171
Nadav Har'El644d7112011-05-25 23:12:35 +03006172/*
6173 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
6174 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
6175 * disinterest in the current event (read or write a specific MSR) by using an
6176 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
6177 */
6178static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
6179 struct vmcs12 *vmcs12, u32 exit_reason)
6180{
6181 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
6182 gpa_t bitmap;
6183
Jan Kiszkacbd29cb2013-02-11 12:19:28 +01006184 if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS))
Nadav Har'El644d7112011-05-25 23:12:35 +03006185 return 1;
6186
6187 /*
6188 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
6189 * for the four combinations of read/write and low/high MSR numbers.
6190 * First we need to figure out which of the four to use:
6191 */
6192 bitmap = vmcs12->msr_bitmap;
6193 if (exit_reason == EXIT_REASON_MSR_WRITE)
6194 bitmap += 2048;
6195 if (msr_index >= 0xc0000000) {
6196 msr_index -= 0xc0000000;
6197 bitmap += 1024;
6198 }
6199
6200 /* Then read the msr_index'th bit from this bitmap: */
6201 if (msr_index < 1024*8) {
6202 unsigned char b;
Jan Kiszkabd31a7f2013-02-14 19:46:27 +01006203 if (kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1))
6204 return 1;
Nadav Har'El644d7112011-05-25 23:12:35 +03006205 return 1 & (b >> (msr_index & 7));
6206 } else
6207 return 1; /* let L1 handle the wrong parameter */
6208}
6209
6210/*
6211 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
6212 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
6213 * intercept (via guest_host_mask etc.) the current event.
6214 */
6215static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
6216 struct vmcs12 *vmcs12)
6217{
6218 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6219 int cr = exit_qualification & 15;
6220 int reg = (exit_qualification >> 8) & 15;
6221 unsigned long val = kvm_register_read(vcpu, reg);
6222
6223 switch ((exit_qualification >> 4) & 3) {
6224 case 0: /* mov to cr */
6225 switch (cr) {
6226 case 0:
6227 if (vmcs12->cr0_guest_host_mask &
6228 (val ^ vmcs12->cr0_read_shadow))
6229 return 1;
6230 break;
6231 case 3:
6232 if ((vmcs12->cr3_target_count >= 1 &&
6233 vmcs12->cr3_target_value0 == val) ||
6234 (vmcs12->cr3_target_count >= 2 &&
6235 vmcs12->cr3_target_value1 == val) ||
6236 (vmcs12->cr3_target_count >= 3 &&
6237 vmcs12->cr3_target_value2 == val) ||
6238 (vmcs12->cr3_target_count >= 4 &&
6239 vmcs12->cr3_target_value3 == val))
6240 return 0;
6241 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
6242 return 1;
6243 break;
6244 case 4:
6245 if (vmcs12->cr4_guest_host_mask &
6246 (vmcs12->cr4_read_shadow ^ val))
6247 return 1;
6248 break;
6249 case 8:
6250 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
6251 return 1;
6252 break;
6253 }
6254 break;
6255 case 2: /* clts */
6256 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
6257 (vmcs12->cr0_read_shadow & X86_CR0_TS))
6258 return 1;
6259 break;
6260 case 1: /* mov from cr */
6261 switch (cr) {
6262 case 3:
6263 if (vmcs12->cpu_based_vm_exec_control &
6264 CPU_BASED_CR3_STORE_EXITING)
6265 return 1;
6266 break;
6267 case 8:
6268 if (vmcs12->cpu_based_vm_exec_control &
6269 CPU_BASED_CR8_STORE_EXITING)
6270 return 1;
6271 break;
6272 }
6273 break;
6274 case 3: /* lmsw */
6275 /*
6276 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
6277 * cr0. Other attempted changes are ignored, with no exit.
6278 */
6279 if (vmcs12->cr0_guest_host_mask & 0xe &
6280 (val ^ vmcs12->cr0_read_shadow))
6281 return 1;
6282 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
6283 !(vmcs12->cr0_read_shadow & 0x1) &&
6284 (val & 0x1))
6285 return 1;
6286 break;
6287 }
6288 return 0;
6289}
6290
6291/*
6292 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
6293 * should handle it ourselves in L0 (and then continue L2). Only call this
6294 * when in is_guest_mode (L2).
6295 */
6296static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
6297{
Nadav Har'El644d7112011-05-25 23:12:35 +03006298 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6299 struct vcpu_vmx *vmx = to_vmx(vcpu);
6300 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
Jan Kiszka957c8972013-02-24 14:11:34 +01006301 u32 exit_reason = vmx->exit_reason;
Nadav Har'El644d7112011-05-25 23:12:35 +03006302
6303 if (vmx->nested.nested_run_pending)
6304 return 0;
6305
6306 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02006307 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
6308 vmcs_read32(VM_INSTRUCTION_ERROR));
Nadav Har'El644d7112011-05-25 23:12:35 +03006309 return 1;
6310 }
6311
6312 switch (exit_reason) {
6313 case EXIT_REASON_EXCEPTION_NMI:
6314 if (!is_exception(intr_info))
6315 return 0;
6316 else if (is_page_fault(intr_info))
6317 return enable_ept;
6318 return vmcs12->exception_bitmap &
6319 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
6320 case EXIT_REASON_EXTERNAL_INTERRUPT:
6321 return 0;
6322 case EXIT_REASON_TRIPLE_FAULT:
6323 return 1;
6324 case EXIT_REASON_PENDING_INTERRUPT:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02006325 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03006326 case EXIT_REASON_NMI_WINDOW:
Jan Kiszka3b656cf2013-04-14 12:12:45 +02006327 return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING);
Nadav Har'El644d7112011-05-25 23:12:35 +03006328 case EXIT_REASON_TASK_SWITCH:
6329 return 1;
6330 case EXIT_REASON_CPUID:
6331 return 1;
6332 case EXIT_REASON_HLT:
6333 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
6334 case EXIT_REASON_INVD:
6335 return 1;
6336 case EXIT_REASON_INVLPG:
6337 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
6338 case EXIT_REASON_RDPMC:
6339 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
6340 case EXIT_REASON_RDTSC:
6341 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
6342 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
6343 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
6344 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
6345 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
6346 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
6347 /*
6348 * VMX instructions trap unconditionally. This allows L1 to
6349 * emulate them for its L2 guest, i.e., allows 3-level nesting!
6350 */
6351 return 1;
6352 case EXIT_REASON_CR_ACCESS:
6353 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
6354 case EXIT_REASON_DR_ACCESS:
6355 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
6356 case EXIT_REASON_IO_INSTRUCTION:
Jan Kiszka908a7bd2013-02-18 11:21:16 +01006357 return nested_vmx_exit_handled_io(vcpu, vmcs12);
Nadav Har'El644d7112011-05-25 23:12:35 +03006358 case EXIT_REASON_MSR_READ:
6359 case EXIT_REASON_MSR_WRITE:
6360 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
6361 case EXIT_REASON_INVALID_STATE:
6362 return 1;
6363 case EXIT_REASON_MWAIT_INSTRUCTION:
6364 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
6365 case EXIT_REASON_MONITOR_INSTRUCTION:
6366 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
6367 case EXIT_REASON_PAUSE_INSTRUCTION:
6368 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
6369 nested_cpu_has2(vmcs12,
6370 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
6371 case EXIT_REASON_MCE_DURING_VMENTRY:
6372 return 0;
6373 case EXIT_REASON_TPR_BELOW_THRESHOLD:
6374 return 1;
6375 case EXIT_REASON_APIC_ACCESS:
6376 return nested_cpu_has2(vmcs12,
6377 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
6378 case EXIT_REASON_EPT_VIOLATION:
6379 case EXIT_REASON_EPT_MISCONFIG:
6380 return 0;
Jan Kiszka0238ea92013-03-13 11:31:24 +01006381 case EXIT_REASON_PREEMPTION_TIMER:
6382 return vmcs12->pin_based_vm_exec_control &
6383 PIN_BASED_VMX_PREEMPTION_TIMER;
Nadav Har'El644d7112011-05-25 23:12:35 +03006384 case EXIT_REASON_WBINVD:
6385 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
6386 case EXIT_REASON_XSETBV:
6387 return 1;
6388 default:
6389 return 1;
6390 }
6391}
6392
Avi Kivity586f9602010-11-18 13:09:54 +02006393static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
6394{
6395 *info1 = vmcs_readl(EXIT_QUALIFICATION);
6396 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
6397}
6398
Avi Kivity6aa8b732006-12-10 02:21:36 -08006399/*
6400 * The guest has exited. See if we can fix it or if we need userspace
6401 * assistance.
6402 */
Avi Kivity851ba692009-08-24 11:10:17 +03006403static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006404{
Avi Kivity29bd8a72007-09-10 17:27:03 +03006405 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006406 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02006407 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03006408
Mohammed Gamal80ced182009-09-01 12:48:18 +02006409 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02006410 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02006411 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006412
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006413 /*
6414 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
6415 * we did not inject a still-pending event to L1 now because of
6416 * nested_run_pending, we need to re-enable this bit.
6417 */
6418 if (vmx->nested.nested_run_pending)
6419 kvm_make_request(KVM_REQ_EVENT, vcpu);
6420
Nadav Har'El509c75e2011-06-02 11:54:52 +03006421 if (!is_guest_mode(vcpu) && (exit_reason == EXIT_REASON_VMLAUNCH ||
6422 exit_reason == EXIT_REASON_VMRESUME))
Nadav Har'El644d7112011-05-25 23:12:35 +03006423 vmx->nested.nested_run_pending = 1;
6424 else
6425 vmx->nested.nested_run_pending = 0;
6426
6427 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
6428 nested_vmx_vmexit(vcpu);
6429 return 1;
6430 }
6431
Mohammed Gamal51207022010-05-31 22:40:54 +03006432 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
6433 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6434 vcpu->run->fail_entry.hardware_entry_failure_reason
6435 = exit_reason;
6436 return 0;
6437 }
6438
Avi Kivity29bd8a72007-09-10 17:27:03 +03006439 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006440 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6441 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03006442 = vmcs_read32(VM_INSTRUCTION_ERROR);
6443 return 0;
6444 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006445
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006446 /*
6447 * Note:
6448 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6449 * delivery event since it indicates guest is accessing MMIO.
6450 * The vm-exit can be triggered again after return to guest that
6451 * will cause infinite loop.
6452 */
Mike Dayd77c26f2007-10-08 09:02:08 -04006453 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08006454 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02006455 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006456 exit_reason != EXIT_REASON_TASK_SWITCH)) {
6457 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6458 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
6459 vcpu->run->internal.ndata = 2;
6460 vcpu->run->internal.data[0] = vectoring_info;
6461 vcpu->run->internal.data[1] = exit_reason;
6462 return 0;
6463 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006464
Nadav Har'El644d7112011-05-25 23:12:35 +03006465 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
6466 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
6467 get_vmcs12(vcpu), vcpu)))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03006468 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006469 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006470 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01006471 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006472 /*
6473 * This CPU don't support us in finding the end of an
6474 * NMI-blocked window if the guest runs with IRQs
6475 * disabled. So we pull the trigger after 1 s of
6476 * futile waiting, but inform the user about this.
6477 */
6478 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6479 "state on VCPU %d after 1 s timeout\n",
6480 __func__, vcpu->vcpu_id);
6481 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006482 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006483 }
6484
Avi Kivity6aa8b732006-12-10 02:21:36 -08006485 if (exit_reason < kvm_vmx_max_exit_handlers
6486 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03006487 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006488 else {
Avi Kivity851ba692009-08-24 11:10:17 +03006489 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6490 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006491 }
6492 return 0;
6493}
6494
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006495static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006496{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006497 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006498 vmcs_write32(TPR_THRESHOLD, 0);
6499 return;
6500 }
6501
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006502 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006503}
6504
Yang Zhang8d146952013-01-25 10:18:50 +08006505static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
6506{
6507 u32 sec_exec_control;
6508
6509 /*
6510 * There is not point to enable virtualize x2apic without enable
6511 * apicv
6512 */
Yang Zhangc7c9c562013-01-25 10:18:51 +08006513 if (!cpu_has_vmx_virtualize_x2apic_mode() ||
6514 !vmx_vm_has_apicv(vcpu->kvm))
Yang Zhang8d146952013-01-25 10:18:50 +08006515 return;
6516
6517 if (!vm_need_tpr_shadow(vcpu->kvm))
6518 return;
6519
6520 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6521
6522 if (set) {
6523 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6524 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6525 } else {
6526 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6527 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6528 }
6529 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
6530
6531 vmx_set_msr_bitmap(vcpu);
6532}
6533
Yang Zhangc7c9c562013-01-25 10:18:51 +08006534static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
6535{
6536 u16 status;
6537 u8 old;
6538
6539 if (!vmx_vm_has_apicv(kvm))
6540 return;
6541
6542 if (isr == -1)
6543 isr = 0;
6544
6545 status = vmcs_read16(GUEST_INTR_STATUS);
6546 old = status >> 8;
6547 if (isr != old) {
6548 status &= 0xff;
6549 status |= isr << 8;
6550 vmcs_write16(GUEST_INTR_STATUS, status);
6551 }
6552}
6553
6554static void vmx_set_rvi(int vector)
6555{
6556 u16 status;
6557 u8 old;
6558
6559 status = vmcs_read16(GUEST_INTR_STATUS);
6560 old = (u8)status & 0xff;
6561 if ((u8)vector != old) {
6562 status &= ~0xff;
6563 status |= (u8)vector;
6564 vmcs_write16(GUEST_INTR_STATUS, status);
6565 }
6566}
6567
6568static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6569{
6570 if (max_irr == -1)
6571 return;
6572
6573 vmx_set_rvi(max_irr);
6574}
6575
6576static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
6577{
Yang Zhang3d81bc72013-04-11 19:25:13 +08006578 if (!vmx_vm_has_apicv(vcpu->kvm))
6579 return;
6580
Yang Zhangc7c9c562013-01-25 10:18:51 +08006581 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
6582 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
6583 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
6584 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
6585}
6586
Avi Kivity51aa01d2010-07-20 14:31:20 +03006587static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03006588{
Avi Kivity00eba012011-03-07 17:24:54 +02006589 u32 exit_intr_info;
6590
6591 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
6592 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
6593 return;
6594
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006595 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02006596 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08006597
6598 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02006599 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08006600 kvm_machine_check();
6601
Gleb Natapov20f65982009-05-11 13:35:55 +03006602 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02006603 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006604 (exit_intr_info & INTR_INFO_VALID_MASK)) {
6605 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03006606 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006607 kvm_after_handle_nmi(&vmx->vcpu);
6608 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03006609}
Gleb Natapov20f65982009-05-11 13:35:55 +03006610
Yang Zhanga547c6d2013-04-11 19:25:10 +08006611static void vmx_handle_external_intr(struct kvm_vcpu *vcpu)
6612{
6613 u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6614
6615 /*
6616 * If external interrupt exists, IF bit is set in rflags/eflags on the
6617 * interrupt stack frame, and interrupt will be enabled on a return
6618 * from interrupt handler.
6619 */
6620 if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK))
6621 == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) {
6622 unsigned int vector;
6623 unsigned long entry;
6624 gate_desc *desc;
6625 struct vcpu_vmx *vmx = to_vmx(vcpu);
6626#ifdef CONFIG_X86_64
6627 unsigned long tmp;
6628#endif
6629
6630 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6631 desc = (gate_desc *)vmx->host_idt_base + vector;
6632 entry = gate_offset(*desc);
6633 asm volatile(
6634#ifdef CONFIG_X86_64
6635 "mov %%" _ASM_SP ", %[sp]\n\t"
6636 "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
6637 "push $%c[ss]\n\t"
6638 "push %[sp]\n\t"
6639#endif
6640 "pushf\n\t"
6641 "orl $0x200, (%%" _ASM_SP ")\n\t"
6642 __ASM_SIZE(push) " $%c[cs]\n\t"
6643 "call *%[entry]\n\t"
6644 :
6645#ifdef CONFIG_X86_64
6646 [sp]"=&r"(tmp)
6647#endif
6648 :
6649 [entry]"r"(entry),
6650 [ss]"i"(__KERNEL_DS),
6651 [cs]"i"(__KERNEL_CS)
6652 );
6653 } else
6654 local_irq_enable();
6655}
6656
Avi Kivity51aa01d2010-07-20 14:31:20 +03006657static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6658{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006659 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03006660 bool unblock_nmi;
6661 u8 vector;
6662 bool idtv_info_valid;
6663
6664 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03006665
Avi Kivitycf393f72008-07-01 16:20:21 +03006666 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02006667 if (vmx->nmi_known_unmasked)
6668 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006669 /*
6670 * Can't use vmx->exit_intr_info since we're not sure what
6671 * the exit reason is.
6672 */
6673 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03006674 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6675 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6676 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006677 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03006678 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6679 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006680 * SDM 3: 23.2.2 (September 2008)
6681 * Bit 12 is undefined in any of the following cases:
6682 * If the VM exit sets the valid bit in the IDT-vectoring
6683 * information field.
6684 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03006685 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006686 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6687 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03006688 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6689 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02006690 else
6691 vmx->nmi_known_unmasked =
6692 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6693 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006694 } else if (unlikely(vmx->soft_vnmi_blocked))
6695 vmx->vnmi_blocked_time +=
6696 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03006697}
6698
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006699static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
Avi Kivity83422e12010-07-20 14:43:23 +03006700 u32 idt_vectoring_info,
6701 int instr_len_field,
6702 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03006703{
Avi Kivity51aa01d2010-07-20 14:31:20 +03006704 u8 vector;
6705 int type;
6706 bool idtv_info_valid;
6707
6708 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03006709
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006710 vcpu->arch.nmi_injected = false;
6711 kvm_clear_exception_queue(vcpu);
6712 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006713
6714 if (!idtv_info_valid)
6715 return;
6716
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006717 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03006718
Avi Kivity668f6122008-07-02 09:28:55 +03006719 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6720 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006721
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006722 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03006723 case INTR_TYPE_NMI_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006724 vcpu->arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03006725 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006726 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03006727 * Clear bit "block by NMI" before VM entry if a NMI
6728 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03006729 */
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006730 vmx_set_nmi_mask(vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006731 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006732 case INTR_TYPE_SOFT_EXCEPTION:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006733 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006734 /* fall through */
6735 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03006736 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03006737 u32 err = vmcs_read32(error_code_field);
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006738 kvm_queue_exception_e(vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03006739 } else
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006740 kvm_queue_exception(vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006741 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006742 case INTR_TYPE_SOFT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006743 vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006744 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03006745 case INTR_TYPE_EXT_INTR:
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006746 kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006747 break;
6748 default:
6749 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03006750 }
Avi Kivitycf393f72008-07-01 16:20:21 +03006751}
6752
Avi Kivity83422e12010-07-20 14:43:23 +03006753static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6754{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006755 __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
Avi Kivity83422e12010-07-20 14:43:23 +03006756 VM_EXIT_INSTRUCTION_LEN,
6757 IDT_VECTORING_ERROR_CODE);
6758}
6759
Avi Kivityb463a6f2010-07-20 15:06:17 +03006760static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6761{
Jan Kiszka3ab66e82013-02-20 14:03:24 +01006762 __vmx_complete_interrupts(vcpu,
Avi Kivityb463a6f2010-07-20 15:06:17 +03006763 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6764 VM_ENTRY_INSTRUCTION_LEN,
6765 VM_ENTRY_EXCEPTION_ERROR_CODE);
6766
6767 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6768}
6769
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006770static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6771{
6772 int i, nr_msrs;
6773 struct perf_guest_switch_msr *msrs;
6774
6775 msrs = perf_guest_get_msrs(&nr_msrs);
6776
6777 if (!msrs)
6778 return;
6779
6780 for (i = 0; i < nr_msrs; i++)
6781 if (msrs[i].host == msrs[i].guest)
6782 clear_atomic_switch_msr(vmx, msrs[i].msr);
6783 else
6784 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
6785 msrs[i].host);
6786}
6787
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08006788static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006789{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006790 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006791 unsigned long debugctlmsr;
Avi Kivity104f2262010-11-18 13:12:52 +02006792
6793 /* Record the guest's net vcpu time for enforced NMI injections. */
6794 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
6795 vmx->entry_time = ktime_get();
6796
6797 /* Don't enter VMX if guest state is invalid, let the exit handler
6798 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02006799 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02006800 return;
6801
6802 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
6803 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
6804 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
6805 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6806
6807 /* When single-stepping over STI and MOV SS, we must clear the
6808 * corresponding interruptibility bits in the guest state. Otherwise
6809 * vmentry fails as it then expects bit 14 (BS) in pending debug
6810 * exceptions being set, but that's not correct for the guest debugging
6811 * case. */
6812 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6813 vmx_set_interrupt_shadow(vcpu, 0);
6814
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006815 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006816 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006817
Nadav Har'Eld462b812011-05-24 15:26:10 +03006818 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02006819 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08006820 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006821 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
6822 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
6823 "push %%" _ASM_CX " \n\t"
6824 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03006825 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006826 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03006827 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03006828 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03006829 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006830 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
6831 "mov %%cr2, %%" _ASM_DX " \n\t"
6832 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03006833 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006834 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03006835 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006836 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02006837 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006838 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006839 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
6840 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
6841 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
6842 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
6843 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
6844 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006845#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006846 "mov %c[r8](%0), %%r8 \n\t"
6847 "mov %c[r9](%0), %%r9 \n\t"
6848 "mov %c[r10](%0), %%r10 \n\t"
6849 "mov %c[r11](%0), %%r11 \n\t"
6850 "mov %c[r12](%0), %%r12 \n\t"
6851 "mov %c[r13](%0), %%r13 \n\t"
6852 "mov %c[r14](%0), %%r14 \n\t"
6853 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006854#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03006855 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03006856
Avi Kivity6aa8b732006-12-10 02:21:36 -08006857 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03006858 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03006859 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006860 "jmp 2f \n\t"
6861 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
6862 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08006863 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006864 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02006865 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006866 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
6867 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
6868 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
6869 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
6870 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
6871 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
6872 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006873#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006874 "mov %%r8, %c[r8](%0) \n\t"
6875 "mov %%r9, %c[r9](%0) \n\t"
6876 "mov %%r10, %c[r10](%0) \n\t"
6877 "mov %%r11, %c[r11](%0) \n\t"
6878 "mov %%r12, %c[r12](%0) \n\t"
6879 "mov %%r13, %c[r13](%0) \n\t"
6880 "mov %%r14, %c[r14](%0) \n\t"
6881 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006882#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03006883 "mov %%cr2, %%" _ASM_AX " \n\t"
6884 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03006885
Avi Kivityb188c81f2012-09-16 15:10:58 +03006886 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02006887 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006888 ".pushsection .rodata \n\t"
6889 ".global vmx_return \n\t"
6890 "vmx_return: " _ASM_PTR " 2b \n\t"
6891 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02006892 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03006893 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02006894 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03006895 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006896 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
6897 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
6898 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
6899 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
6900 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
6901 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
6902 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006903#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006904 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
6905 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
6906 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
6907 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
6908 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
6909 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
6910 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
6911 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08006912#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02006913 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
6914 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02006915 : "cc", "memory"
6916#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03006917 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02006918 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006919#else
6920 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02006921#endif
6922 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08006923
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006924 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
6925 if (debugctlmsr)
6926 update_debugctlmsr(debugctlmsr);
6927
Avi Kivityaa67f602012-08-01 16:48:03 +03006928#ifndef CONFIG_X86_64
6929 /*
6930 * The sysexit path does not restore ds/es, so we must set them to
6931 * a reasonable value ourselves.
6932 *
6933 * We can't defer this to vmx_load_host_state() since that function
6934 * may be executed in interrupt context, which saves and restore segments
6935 * around it, nullifying its effect.
6936 */
6937 loadsegment(ds, __USER_DS);
6938 loadsegment(es, __USER_DS);
6939#endif
6940
Avi Kivity6de4f3a2009-05-31 22:58:47 +03006941 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02006942 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivity69c73022011-03-07 15:26:44 +02006943 | (1 << VCPU_EXREG_CPL)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006944 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03006945 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006946 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006947 vcpu->arch.regs_dirty = 0;
6948
Avi Kivity1155f762007-11-22 11:30:47 +02006949 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
6950
Nadav Har'Eld462b812011-05-24 15:26:10 +03006951 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02006952
Avi Kivity51aa01d2010-07-20 14:31:20 +03006953 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02006954 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
Avi Kivity51aa01d2010-07-20 14:31:20 +03006955
6956 vmx_complete_atomic_exit(vmx);
6957 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006958 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006959}
6960
Avi Kivity6aa8b732006-12-10 02:21:36 -08006961static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6962{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006963 struct vcpu_vmx *vmx = to_vmx(vcpu);
6964
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08006965 free_vpid(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006966 free_nested(vmx);
Nadav Har'Eld462b812011-05-24 15:26:10 +03006967 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006968 kfree(vmx->guest_msrs);
6969 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10006970 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006971}
6972
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006973static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006974{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006975 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10006976 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03006977 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006978
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006979 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006980 return ERR_PTR(-ENOMEM);
6981
Sheng Yang2384d2b2008-01-17 15:14:33 +08006982 allocate_vpid(vmx);
6983
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006984 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
6985 if (err)
6986 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006987
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006988 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006989 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006990 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006991 goto uninit_vcpu;
6992 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08006993
Nadav Har'Eld462b812011-05-24 15:26:10 +03006994 vmx->loaded_vmcs = &vmx->vmcs01;
6995 vmx->loaded_vmcs->vmcs = alloc_vmcs();
6996 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006997 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03006998 if (!vmm_exclusive)
6999 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
7000 loaded_vmcs_init(vmx->loaded_vmcs);
7001 if (!vmm_exclusive)
7002 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04007003
Avi Kivity15ad7142007-07-11 18:17:21 +03007004 cpu = get_cpu();
7005 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10007006 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10007007 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007008 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03007009 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007010 if (err)
7011 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02007012 if (vm_need_virtualize_apic_accesses(kvm)) {
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02007013 err = alloc_apic_access_page(kvm);
7014 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02007015 goto free_vmcs;
Jan Kiszkaa63cb562013-04-08 11:07:46 +02007016 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08007017
Sheng Yangb927a3c2009-07-21 10:42:48 +08007018 if (enable_ept) {
7019 if (!kvm->arch.ept_identity_map_addr)
7020 kvm->arch.ept_identity_map_addr =
7021 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Gleb Natapov93ea5382011-02-21 12:07:59 +02007022 err = -ENOMEM;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007023 if (alloc_identity_pagetable(kvm) != 0)
7024 goto free_vmcs;
Gleb Natapov93ea5382011-02-21 12:07:59 +02007025 if (!init_rmode_identity_map(kvm))
7026 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08007027 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08007028
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03007029 vmx->nested.current_vmptr = -1ull;
7030 vmx->nested.current_vmcs12 = NULL;
7031
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007032 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08007033
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007034free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08007035 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007036free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007037 kfree(vmx->guest_msrs);
7038uninit_vcpu:
7039 kvm_vcpu_uninit(&vmx->vcpu);
7040free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08007041 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10007042 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10007043 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08007044}
7045
Yang, Sheng002c7f72007-07-31 14:23:01 +03007046static void __init vmx_check_processor_compat(void *rtn)
7047{
7048 struct vmcs_config vmcs_conf;
7049
7050 *(int *)rtn = 0;
7051 if (setup_vmcs_config(&vmcs_conf) < 0)
7052 *(int *)rtn = -EIO;
7053 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
7054 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
7055 smp_processor_id());
7056 *(int *)rtn = -EIO;
7057 }
7058}
7059
Sheng Yang67253af2008-04-25 10:20:22 +08007060static int get_ept_level(void)
7061{
7062 return VMX_EPT_DEFAULT_GAW + 1;
7063}
7064
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007065static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08007066{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007067 u64 ret;
7068
Sheng Yang522c68c2009-04-27 20:35:43 +08007069 /* For VT-d and EPT combination
7070 * 1. MMIO: always map as UC
7071 * 2. EPT with VT-d:
7072 * a. VT-d without snooping control feature: can't guarantee the
7073 * result, try to trust guest.
7074 * b. VT-d with snooping control feature: snooping control feature of
7075 * VT-d engine can guarantee the cache correctness. Just set it
7076 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08007077 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08007078 * consistent with host MTRR
7079 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007080 if (is_mmio)
7081 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08007082 else if (vcpu->kvm->arch.iommu_domain &&
7083 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
7084 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
7085 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007086 else
Sheng Yang522c68c2009-04-27 20:35:43 +08007087 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08007088 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007089
7090 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08007091}
7092
Sheng Yang17cc3932010-01-05 19:02:27 +08007093static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02007094{
Sheng Yang878403b2010-01-05 19:02:29 +08007095 if (enable_ept && !cpu_has_vmx_ept_1g_page())
7096 return PT_DIRECTORY_LEVEL;
7097 else
7098 /* For shadow and EPT supported 1GB page */
7099 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02007100}
7101
Sheng Yang0e851882009-12-18 16:48:46 +08007102static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
7103{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007104 struct kvm_cpuid_entry2 *best;
7105 struct vcpu_vmx *vmx = to_vmx(vcpu);
7106 u32 exec_control;
7107
7108 vmx->rdtscp_enabled = false;
7109 if (vmx_rdtscp_supported()) {
7110 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7111 if (exec_control & SECONDARY_EXEC_RDTSCP) {
7112 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
7113 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
7114 vmx->rdtscp_enabled = true;
7115 else {
7116 exec_control &= ~SECONDARY_EXEC_RDTSCP;
7117 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7118 exec_control);
7119 }
7120 }
7121 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007122
Mao, Junjiead756a12012-07-02 01:18:48 +00007123 /* Exposing INVPCID only when PCID is exposed */
7124 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
7125 if (vmx_invpcid_supported() &&
Ren, Yongjie4f977042012-09-07 07:36:59 +00007126 best && (best->ebx & bit(X86_FEATURE_INVPCID)) &&
Mao, Junjiead756a12012-07-02 01:18:48 +00007127 guest_cpuid_has_pcid(vcpu)) {
Takashi Iwai29282fd2012-11-09 15:20:17 +01007128 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Mao, Junjiead756a12012-07-02 01:18:48 +00007129 exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
7130 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7131 exec_control);
7132 } else {
Takashi Iwai29282fd2012-11-09 15:20:17 +01007133 if (cpu_has_secondary_exec_ctrls()) {
7134 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
7135 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
7136 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
7137 exec_control);
7138 }
Mao, Junjiead756a12012-07-02 01:18:48 +00007139 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00007140 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00007141 }
Sheng Yang0e851882009-12-18 16:48:46 +08007142}
7143
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007144static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
7145{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03007146 if (func == 1 && nested)
7147 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007148}
7149
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007150/*
7151 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
7152 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
7153 * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2
7154 * guest in a way that will both be appropriate to L1's requests, and our
7155 * needs. In addition to modifying the active vmcs (which is vmcs02), this
7156 * function also has additional necessary side-effects, like setting various
7157 * vcpu->arch fields.
7158 */
7159static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7160{
7161 struct vcpu_vmx *vmx = to_vmx(vcpu);
7162 u32 exec_control;
7163
7164 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
7165 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
7166 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
7167 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
7168 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
7169 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
7170 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
7171 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
7172 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
7173 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
7174 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
7175 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
7176 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
7177 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
7178 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
7179 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
7180 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
7181 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
7182 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
7183 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
7184 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
7185 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
7186 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
7187 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
7188 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
7189 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
7190 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
7191 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
7192 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
7193 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
7194 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
7195 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
7196 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
7197 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
7198 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
7199 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
7200
7201 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
7202 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
7203 vmcs12->vm_entry_intr_info_field);
7204 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
7205 vmcs12->vm_entry_exception_error_code);
7206 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
7207 vmcs12->vm_entry_instruction_len);
7208 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
7209 vmcs12->guest_interruptibility_info);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007210 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
Jan Kiszka503cd0c2013-03-03 13:05:44 +01007211 kvm_set_dr(vcpu, 7, vmcs12->guest_dr7);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007212 vmcs_writel(GUEST_RFLAGS, vmcs12->guest_rflags);
7213 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
7214 vmcs12->guest_pending_dbg_exceptions);
7215 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
7216 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
7217
7218 vmcs_write64(VMCS_LINK_POINTER, -1ull);
7219
7220 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
7221 (vmcs_config.pin_based_exec_ctrl |
7222 vmcs12->pin_based_vm_exec_control));
7223
Jan Kiszka0238ea92013-03-13 11:31:24 +01007224 if (vmcs12->pin_based_vm_exec_control & PIN_BASED_VMX_PREEMPTION_TIMER)
7225 vmcs_write32(VMX_PREEMPTION_TIMER_VALUE,
7226 vmcs12->vmx_preemption_timer_value);
7227
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007228 /*
7229 * Whether page-faults are trapped is determined by a combination of
7230 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
7231 * If enable_ept, L0 doesn't care about page faults and we should
7232 * set all of these to L1's desires. However, if !enable_ept, L0 does
7233 * care about (at least some) page faults, and because it is not easy
7234 * (if at all possible?) to merge L0 and L1's desires, we simply ask
7235 * to exit on each and every L2 page fault. This is done by setting
7236 * MASK=MATCH=0 and (see below) EB.PF=1.
7237 * Note that below we don't need special code to set EB.PF beyond the
7238 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
7239 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
7240 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
7241 *
7242 * A problem with this approach (when !enable_ept) is that L1 may be
7243 * injected with more page faults than it asked for. This could have
7244 * caused problems, but in practice existing hypervisors don't care.
7245 * To fix this, we will need to emulate the PFEC checking (on the L1
7246 * page tables), using walk_addr(), when injecting PFs to L1.
7247 */
7248 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
7249 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
7250 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
7251 enable_ept ? vmcs12->page_fault_error_code_match : 0);
7252
7253 if (cpu_has_secondary_exec_ctrls()) {
7254 u32 exec_control = vmx_secondary_exec_control(vmx);
7255 if (!vmx->rdtscp_enabled)
7256 exec_control &= ~SECONDARY_EXEC_RDTSCP;
7257 /* Take the following fields only from vmcs12 */
7258 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7259 if (nested_cpu_has(vmcs12,
7260 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
7261 exec_control |= vmcs12->secondary_vm_exec_control;
7262
7263 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
7264 /*
7265 * Translate L1 physical address to host physical
7266 * address for vmcs02. Keep the page pinned, so this
7267 * physical address remains valid. We keep a reference
7268 * to it so we can release it later.
7269 */
7270 if (vmx->nested.apic_access_page) /* shouldn't happen */
7271 nested_release_page(vmx->nested.apic_access_page);
7272 vmx->nested.apic_access_page =
7273 nested_get_page(vcpu, vmcs12->apic_access_addr);
7274 /*
7275 * If translation failed, no matter: This feature asks
7276 * to exit when accessing the given address, and if it
7277 * can never be accessed, this feature won't do
7278 * anything anyway.
7279 */
7280 if (!vmx->nested.apic_access_page)
7281 exec_control &=
7282 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7283 else
7284 vmcs_write64(APIC_ACCESS_ADDR,
7285 page_to_phys(vmx->nested.apic_access_page));
7286 }
7287
7288 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
7289 }
7290
7291
7292 /*
7293 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
7294 * Some constant fields are set here by vmx_set_constant_host_state().
7295 * Other fields are different per CPU, and will be set later when
7296 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
7297 */
Yang Zhanga547c6d2013-04-11 19:25:10 +08007298 vmx_set_constant_host_state(vmx);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007299
7300 /*
7301 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
7302 * entry, but only if the current (host) sp changed from the value
7303 * we wrote last (vmx->host_rsp). This cache is no longer relevant
7304 * if we switch vmcs, and rather than hold a separate cache per vmcs,
7305 * here we just force the write to happen on entry.
7306 */
7307 vmx->host_rsp = 0;
7308
7309 exec_control = vmx_exec_control(vmx); /* L0's desires */
7310 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
7311 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
7312 exec_control &= ~CPU_BASED_TPR_SHADOW;
7313 exec_control |= vmcs12->cpu_based_vm_exec_control;
7314 /*
7315 * Merging of IO and MSR bitmaps not currently supported.
7316 * Rather, exit every time.
7317 */
7318 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
7319 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
7320 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
7321
7322 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
7323
7324 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
7325 * bitwise-or of what L1 wants to trap for L2, and what we want to
7326 * trap. Note that CR0.TS also needs updating - we do this later.
7327 */
7328 update_exception_bitmap(vcpu);
7329 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
7330 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
7331
7332 /* Note: IA32_MODE, LOAD_IA32_EFER are modified by vmx_set_efer below */
7333 vmcs_write32(VM_EXIT_CONTROLS,
7334 vmcs12->vm_exit_controls | vmcs_config.vmexit_ctrl);
7335 vmcs_write32(VM_ENTRY_CONTROLS, vmcs12->vm_entry_controls |
7336 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
7337
7338 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)
7339 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
7340 else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
7341 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
7342
7343
7344 set_cr4_guest_host_mask(vmx);
7345
Nadav Har'El27fc51b2011-08-02 15:54:52 +03007346 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
7347 vmcs_write64(TSC_OFFSET,
7348 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
7349 else
7350 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007351
7352 if (enable_vpid) {
7353 /*
7354 * Trivially support vpid by letting L2s share their parent
7355 * L1's vpid. TODO: move to a more elaborate solution, giving
7356 * each L2 its own vpid and exposing the vpid feature to L1.
7357 */
7358 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
7359 vmx_flush_tlb(vcpu);
7360 }
7361
7362 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
7363 vcpu->arch.efer = vmcs12->guest_ia32_efer;
7364 if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
7365 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
7366 else
7367 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
7368 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
7369 vmx_set_efer(vcpu, vcpu->arch.efer);
7370
7371 /*
7372 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
7373 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
7374 * The CR0_READ_SHADOW is what L2 should have expected to read given
7375 * the specifications by L1; It's not enough to take
7376 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
7377 * have more bits than L1 expected.
7378 */
7379 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
7380 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
7381
7382 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
7383 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
7384
7385 /* shadow page tables on either EPT or shadow page tables */
7386 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
7387 kvm_mmu_reset_context(vcpu);
7388
7389 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
7390 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
7391}
7392
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007393/*
7394 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
7395 * for running an L2 nested guest.
7396 */
7397static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
7398{
7399 struct vmcs12 *vmcs12;
7400 struct vcpu_vmx *vmx = to_vmx(vcpu);
7401 int cpu;
7402 struct loaded_vmcs *vmcs02;
7403
7404 if (!nested_vmx_check_permission(vcpu) ||
7405 !nested_vmx_check_vmcs12(vcpu))
7406 return 1;
7407
7408 skip_emulated_instruction(vcpu);
7409 vmcs12 = get_vmcs12(vcpu);
7410
Nadav Har'El7c177932011-05-25 23:12:04 +03007411 /*
7412 * The nested entry process starts with enforcing various prerequisites
7413 * on vmcs12 as required by the Intel SDM, and act appropriately when
7414 * they fail: As the SDM explains, some conditions should cause the
7415 * instruction to fail, while others will cause the instruction to seem
7416 * to succeed, but return an EXIT_REASON_INVALID_STATE.
7417 * To speed up the normal (success) code path, we should avoid checking
7418 * for misconfigurations which will anyway be caught by the processor
7419 * when using the merged vmcs02.
7420 */
7421 if (vmcs12->launch_state == launch) {
7422 nested_vmx_failValid(vcpu,
7423 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
7424 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
7425 return 1;
7426 }
7427
Paolo Bonzini26539bd2013-04-15 15:00:27 +02007428 if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE) {
7429 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7430 return 1;
7431 }
7432
Nadav Har'El7c177932011-05-25 23:12:04 +03007433 if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
7434 !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) {
7435 /*TODO: Also verify bits beyond physical address width are 0*/
7436 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7437 return 1;
7438 }
7439
7440 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
7441 !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) {
7442 /*TODO: Also verify bits beyond physical address width are 0*/
7443 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7444 return 1;
7445 }
7446
7447 if (vmcs12->vm_entry_msr_load_count > 0 ||
7448 vmcs12->vm_exit_msr_load_count > 0 ||
7449 vmcs12->vm_exit_msr_store_count > 0) {
Jan Kiszkabd801582011-09-12 11:26:22 +02007450 pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n",
7451 __func__);
Nadav Har'El7c177932011-05-25 23:12:04 +03007452 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7453 return 1;
7454 }
7455
7456 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
7457 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) ||
7458 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
7459 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
7460 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
7461 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
7462 !vmx_control_verify(vmcs12->vm_exit_controls,
7463 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) ||
7464 !vmx_control_verify(vmcs12->vm_entry_controls,
7465 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high))
7466 {
7467 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7468 return 1;
7469 }
7470
7471 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
7472 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
7473 nested_vmx_failValid(vcpu,
7474 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
7475 return 1;
7476 }
7477
7478 if (((vmcs12->guest_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
7479 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
7480 nested_vmx_entry_failure(vcpu, vmcs12,
7481 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
7482 return 1;
7483 }
7484 if (vmcs12->vmcs_link_pointer != -1ull) {
7485 nested_vmx_entry_failure(vcpu, vmcs12,
7486 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
7487 return 1;
7488 }
7489
7490 /*
7491 * We're finally done with prerequisite checking, and can start with
7492 * the nested entry.
7493 */
7494
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007495 vmcs02 = nested_get_current_vmcs02(vmx);
7496 if (!vmcs02)
7497 return -ENOMEM;
7498
7499 enter_guest_mode(vcpu);
7500
7501 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
7502
7503 cpu = get_cpu();
7504 vmx->loaded_vmcs = vmcs02;
7505 vmx_vcpu_put(vcpu);
7506 vmx_vcpu_load(vcpu, cpu);
7507 vcpu->cpu = cpu;
7508 put_cpu();
7509
Jan Kiszka36c3cc42013-02-23 22:35:37 +01007510 vmx_segment_cache_clear(vmx);
7511
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007512 vmcs12->launch_state = 1;
7513
7514 prepare_vmcs02(vcpu, vmcs12);
7515
7516 /*
7517 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
7518 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
7519 * returned as far as L1 is concerned. It will only return (and set
7520 * the success flag) when L2 exits (see nested_vmx_vmexit()).
7521 */
7522 return 1;
7523}
7524
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007525/*
7526 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
7527 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
7528 * This function returns the new value we should put in vmcs12.guest_cr0.
7529 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
7530 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
7531 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
7532 * didn't trap the bit, because if L1 did, so would L0).
7533 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
7534 * been modified by L2, and L1 knows it. So just leave the old value of
7535 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
7536 * isn't relevant, because if L0 traps this bit it can set it to anything.
7537 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
7538 * changed these bits, and therefore they need to be updated, but L0
7539 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
7540 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
7541 */
7542static inline unsigned long
7543vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7544{
7545 return
7546 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
7547 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
7548 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
7549 vcpu->arch.cr0_guest_owned_bits));
7550}
7551
7552static inline unsigned long
7553vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7554{
7555 return
7556 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
7557 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
7558 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
7559 vcpu->arch.cr4_guest_owned_bits));
7560}
7561
Jan Kiszka5f3d5792013-04-14 12:12:46 +02007562static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,
7563 struct vmcs12 *vmcs12)
7564{
7565 u32 idt_vectoring;
7566 unsigned int nr;
7567
7568 if (vcpu->arch.exception.pending) {
7569 nr = vcpu->arch.exception.nr;
7570 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
7571
7572 if (kvm_exception_is_soft(nr)) {
7573 vmcs12->vm_exit_instruction_len =
7574 vcpu->arch.event_exit_inst_len;
7575 idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
7576 } else
7577 idt_vectoring |= INTR_TYPE_HARD_EXCEPTION;
7578
7579 if (vcpu->arch.exception.has_error_code) {
7580 idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK;
7581 vmcs12->idt_vectoring_error_code =
7582 vcpu->arch.exception.error_code;
7583 }
7584
7585 vmcs12->idt_vectoring_info_field = idt_vectoring;
7586 } else if (vcpu->arch.nmi_pending) {
7587 vmcs12->idt_vectoring_info_field =
7588 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR;
7589 } else if (vcpu->arch.interrupt.pending) {
7590 nr = vcpu->arch.interrupt.nr;
7591 idt_vectoring = nr | VECTORING_INFO_VALID_MASK;
7592
7593 if (vcpu->arch.interrupt.soft) {
7594 idt_vectoring |= INTR_TYPE_SOFT_INTR;
7595 vmcs12->vm_entry_instruction_len =
7596 vcpu->arch.event_exit_inst_len;
7597 } else
7598 idt_vectoring |= INTR_TYPE_EXT_INTR;
7599
7600 vmcs12->idt_vectoring_info_field = idt_vectoring;
7601 }
7602}
7603
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007604/*
7605 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
7606 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
7607 * and this function updates it to reflect the changes to the guest state while
7608 * L2 was running (and perhaps made some exits which were handled directly by L0
7609 * without going back to L1), and to reflect the exit reason.
7610 * Note that we do not have to copy here all VMCS fields, just those that
7611 * could have changed by the L2 guest or the exit - i.e., the guest-state and
7612 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
7613 * which already writes to vmcs12 directly.
7614 */
Jan Kiszka733568f2013-02-23 15:07:47 +01007615static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007616{
7617 /* update guest state fields: */
7618 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
7619 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
7620
7621 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
7622 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
7623 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
7624 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
7625
7626 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
7627 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
7628 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
7629 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
7630 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
7631 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
7632 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
7633 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
7634 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
7635 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
7636 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
7637 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
7638 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
7639 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
7640 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
7641 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
7642 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
7643 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
7644 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
7645 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
7646 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
7647 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
7648 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
7649 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
7650 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
7651 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
7652 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
7653 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
7654 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
7655 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
7656 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
7657 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
7658 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
7659 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
7660 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
7661 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
7662
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007663 vmcs12->guest_interruptibility_info =
7664 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
7665 vmcs12->guest_pending_dbg_exceptions =
7666 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
7667
Jan Kiszkac18911a2013-03-13 16:06:41 +01007668 vmcs12->vm_entry_controls =
7669 (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) |
7670 (vmcs_read32(VM_ENTRY_CONTROLS) & VM_ENTRY_IA32E_MODE);
7671
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007672 /* TODO: These cannot have changed unless we have MSR bitmaps and
7673 * the relevant bit asks not to trap the change */
7674 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
Jan Kiszkab8c07d52013-04-06 13:51:21 +02007675 if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007676 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
7677 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
7678 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
7679 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
7680
7681 /* update exit information fields: */
7682
Jan Kiszka957c8972013-02-24 14:11:34 +01007683 vmcs12->vm_exit_reason = to_vmx(vcpu)->exit_reason;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007684 vmcs12->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7685
7686 vmcs12->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Jan Kiszkac0d1c772013-04-14 12:12:50 +02007687 if ((vmcs12->vm_exit_intr_info &
7688 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) ==
7689 (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK))
7690 vmcs12->vm_exit_intr_error_code =
7691 vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Jan Kiszka5f3d5792013-04-14 12:12:46 +02007692 vmcs12->idt_vectoring_info_field = 0;
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007693 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
7694 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7695
Jan Kiszka5f3d5792013-04-14 12:12:46 +02007696 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) {
7697 /* vm_entry_intr_info_field is cleared on exit. Emulate this
7698 * instead of reading the real value. */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007699 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
Jan Kiszka5f3d5792013-04-14 12:12:46 +02007700
7701 /*
7702 * Transfer the event that L0 or L1 may wanted to inject into
7703 * L2 to IDT_VECTORING_INFO_FIELD.
7704 */
7705 vmcs12_save_pending_event(vcpu, vmcs12);
7706 }
7707
7708 /*
7709 * Drop what we picked up for L2 via vmx_complete_interrupts. It is
7710 * preserved above and would only end up incorrectly in L1.
7711 */
7712 vcpu->arch.nmi_injected = false;
7713 kvm_clear_exception_queue(vcpu);
7714 kvm_clear_interrupt_queue(vcpu);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007715}
7716
7717/*
7718 * A part of what we need to when the nested L2 guest exits and we want to
7719 * run its L1 parent, is to reset L1's guest state to the host state specified
7720 * in vmcs12.
7721 * This function is to be called not only on normal nested exit, but also on
7722 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
7723 * Failures During or After Loading Guest State").
7724 * This function should be called when the active VMCS is L1's (vmcs01).
7725 */
Jan Kiszka733568f2013-02-23 15:07:47 +01007726static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
7727 struct vmcs12 *vmcs12)
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007728{
7729 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
7730 vcpu->arch.efer = vmcs12->host_ia32_efer;
7731 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
7732 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
7733 else
7734 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
7735 vmx_set_efer(vcpu, vcpu->arch.efer);
7736
7737 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
7738 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
Jan Kiszkac4627c72013-03-03 20:47:11 +01007739 vmx_set_rflags(vcpu, X86_EFLAGS_BIT1);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007740 /*
7741 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
7742 * actually changed, because it depends on the current state of
7743 * fpu_active (which may have changed).
7744 * Note that vmx_set_cr0 refers to efer set above.
7745 */
7746 kvm_set_cr0(vcpu, vmcs12->host_cr0);
7747 /*
7748 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
7749 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
7750 * but we also need to update cr0_guest_host_mask and exception_bitmap.
7751 */
7752 update_exception_bitmap(vcpu);
7753 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
7754 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
7755
7756 /*
7757 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
7758 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
7759 */
7760 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
7761 kvm_set_cr4(vcpu, vmcs12->host_cr4);
7762
7763 /* shadow page tables on either EPT or shadow page tables */
7764 kvm_set_cr3(vcpu, vmcs12->host_cr3);
7765 kvm_mmu_reset_context(vcpu);
7766
7767 if (enable_vpid) {
7768 /*
7769 * Trivially support vpid by letting L2s share their parent
7770 * L1's vpid. TODO: move to a more elaborate solution, giving
7771 * each L2 its own vpid and exposing the vpid feature to L1.
7772 */
7773 vmx_flush_tlb(vcpu);
7774 }
7775
7776
7777 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
7778 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
7779 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
7780 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
7781 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
7782 vmcs_writel(GUEST_TR_BASE, vmcs12->host_tr_base);
7783 vmcs_writel(GUEST_GS_BASE, vmcs12->host_gs_base);
7784 vmcs_writel(GUEST_FS_BASE, vmcs12->host_fs_base);
7785 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->host_es_selector);
7786 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->host_cs_selector);
7787 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->host_ss_selector);
7788 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->host_ds_selector);
7789 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->host_fs_selector);
7790 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->host_gs_selector);
7791 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->host_tr_selector);
7792
7793 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT)
7794 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
7795 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
7796 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
7797 vmcs12->host_ia32_perf_global_ctrl);
Jan Kiszka503cd0c2013-03-03 13:05:44 +01007798
7799 kvm_set_dr(vcpu, 7, 0x400);
7800 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007801}
7802
7803/*
7804 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
7805 * and modify vmcs12 to make it see what it would expect to see there if
7806 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
7807 */
7808static void nested_vmx_vmexit(struct kvm_vcpu *vcpu)
7809{
7810 struct vcpu_vmx *vmx = to_vmx(vcpu);
7811 int cpu;
7812 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7813
Jan Kiszka5f3d5792013-04-14 12:12:46 +02007814 /* trying to cancel vmlaunch/vmresume is a bug */
7815 WARN_ON_ONCE(vmx->nested.nested_run_pending);
7816
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007817 leave_guest_mode(vcpu);
7818 prepare_vmcs12(vcpu, vmcs12);
7819
7820 cpu = get_cpu();
7821 vmx->loaded_vmcs = &vmx->vmcs01;
7822 vmx_vcpu_put(vcpu);
7823 vmx_vcpu_load(vcpu, cpu);
7824 vcpu->cpu = cpu;
7825 put_cpu();
7826
Jan Kiszka36c3cc42013-02-23 22:35:37 +01007827 vmx_segment_cache_clear(vmx);
7828
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007829 /* if no vmcs02 cache requested, remove the one we used */
7830 if (VMCS02_POOL_SIZE == 0)
7831 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
7832
7833 load_vmcs12_host_state(vcpu, vmcs12);
7834
Nadav Har'El27fc51b2011-08-02 15:54:52 +03007835 /* Update TSC_OFFSET if TSC was changed while L2 ran */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007836 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
7837
7838 /* This is needed for same reason as it was needed in prepare_vmcs02 */
7839 vmx->host_rsp = 0;
7840
7841 /* Unpin physical memory we referred to in vmcs02 */
7842 if (vmx->nested.apic_access_page) {
7843 nested_release_page(vmx->nested.apic_access_page);
7844 vmx->nested.apic_access_page = 0;
7845 }
7846
7847 /*
7848 * Exiting from L2 to L1, we're now back to L1 which thinks it just
7849 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
7850 * success or failure flag accordingly.
7851 */
7852 if (unlikely(vmx->fail)) {
7853 vmx->fail = 0;
7854 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
7855 } else
7856 nested_vmx_succeed(vcpu);
7857}
7858
Nadav Har'El7c177932011-05-25 23:12:04 +03007859/*
7860 * L1's failure to enter L2 is a subset of a normal exit, as explained in
7861 * 23.7 "VM-entry failures during or after loading guest state" (this also
7862 * lists the acceptable exit-reason and exit-qualification parameters).
7863 * It should only be called before L2 actually succeeded to run, and when
7864 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
7865 */
7866static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
7867 struct vmcs12 *vmcs12,
7868 u32 reason, unsigned long qualification)
7869{
7870 load_vmcs12_host_state(vcpu, vmcs12);
7871 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
7872 vmcs12->exit_qualification = qualification;
7873 nested_vmx_succeed(vcpu);
7874}
7875
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007876static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7877 struct x86_instruction_info *info,
7878 enum x86_intercept_stage stage)
7879{
7880 return X86EMUL_CONTINUE;
7881}
7882
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03007883static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007884 .cpu_has_kvm_support = cpu_has_kvm_support,
7885 .disabled_by_bios = vmx_disabled_by_bios,
7886 .hardware_setup = hardware_setup,
7887 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03007888 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007889 .hardware_enable = hardware_enable,
7890 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007891 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007892
7893 .vcpu_create = vmx_create_vcpu,
7894 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007895 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007896
Avi Kivity04d2cc72007-09-10 18:10:54 +03007897 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007898 .vcpu_load = vmx_vcpu_load,
7899 .vcpu_put = vmx_vcpu_put,
7900
Jan Kiszkac8639012012-09-21 05:42:55 +02007901 .update_db_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007902 .get_msr = vmx_get_msr,
7903 .set_msr = vmx_set_msr,
7904 .get_segment_base = vmx_get_segment_base,
7905 .get_segment = vmx_get_segment,
7906 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007907 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007908 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02007909 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02007910 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03007911 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007912 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007913 .set_cr3 = vmx_set_cr3,
7914 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007915 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007916 .get_idt = vmx_get_idt,
7917 .set_idt = vmx_set_idt,
7918 .get_gdt = vmx_get_gdt,
7919 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03007920 .set_dr7 = vmx_set_dr7,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007921 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007922 .get_rflags = vmx_get_rflags,
7923 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02007924 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02007925 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007926
7927 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007928
Avi Kivity6aa8b732006-12-10 02:21:36 -08007929 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007930 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007931 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007932 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7933 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007934 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007935 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007936 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007937 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007938 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007939 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007940 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007941 .get_nmi_mask = vmx_get_nmi_mask,
7942 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007943 .enable_nmi_window = enable_nmi_window,
7944 .enable_irq_window = enable_irq_window,
7945 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08007946 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007947 .vm_has_apicv = vmx_vm_has_apicv,
7948 .load_eoi_exitmap = vmx_load_eoi_exitmap,
7949 .hwapic_irr_update = vmx_hwapic_irr_update,
7950 .hwapic_isr_update = vmx_hwapic_isr_update,
Yang Zhanga20ed542013-04-11 19:25:15 +08007951 .sync_pir_to_irr = vmx_sync_pir_to_irr,
7952 .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007953
Izik Eiduscbc94022007-10-25 00:29:55 +02007954 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08007955 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007956 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007957
Avi Kivity586f9602010-11-18 13:09:54 +02007958 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007959
Sheng Yang17cc3932010-01-05 19:02:27 +08007960 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08007961
7962 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007963
7964 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00007965 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007966
7967 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007968
7969 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007970
Joerg Roedel4051b182011-03-25 09:44:49 +01007971 .set_tsc_khz = vmx_set_tsc_khz,
Will Auldba904632012-11-29 12:42:50 -08007972 .read_tsc_offset = vmx_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007973 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10007974 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01007975 .compute_tsc_offset = vmx_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03007976 .read_l1_tsc = vmx_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007977
7978 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007979
7980 .check_intercept = vmx_check_intercept,
Yang Zhanga547c6d2013-04-11 19:25:10 +08007981 .handle_external_intr = vmx_handle_external_intr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007982};
7983
7984static int __init vmx_init(void)
7985{
Yang Zhang8d146952013-01-25 10:18:50 +08007986 int r, i, msr;
Avi Kivity26bb0982009-09-07 11:14:12 +03007987
7988 rdmsrl_safe(MSR_EFER, &host_efer);
7989
7990 for (i = 0; i < NR_VMX_MSR; ++i)
7991 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03007992
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007993 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03007994 if (!vmx_io_bitmap_a)
7995 return -ENOMEM;
7996
Guo Chao2106a542012-06-15 11:31:56 +08007997 r = -ENOMEM;
7998
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007999 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008000 if (!vmx_io_bitmap_b)
He, Qingfdef3ad2007-04-30 09:45:24 +03008001 goto out;
He, Qingfdef3ad2007-04-30 09:45:24 +03008002
Avi Kivity58972972009-02-24 22:26:47 +02008003 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008004 if (!vmx_msr_bitmap_legacy)
Sheng Yang25c5f222008-03-28 13:18:56 +08008005 goto out1;
Guo Chao2106a542012-06-15 11:31:56 +08008006
Yang Zhang8d146952013-01-25 10:18:50 +08008007 vmx_msr_bitmap_legacy_x2apic =
8008 (unsigned long *)__get_free_page(GFP_KERNEL);
8009 if (!vmx_msr_bitmap_legacy_x2apic)
8010 goto out2;
Sheng Yang25c5f222008-03-28 13:18:56 +08008011
Avi Kivity58972972009-02-24 22:26:47 +02008012 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08008013 if (!vmx_msr_bitmap_longmode)
Yang Zhang8d146952013-01-25 10:18:50 +08008014 goto out3;
Guo Chao2106a542012-06-15 11:31:56 +08008015
Yang Zhang8d146952013-01-25 10:18:50 +08008016 vmx_msr_bitmap_longmode_x2apic =
8017 (unsigned long *)__get_free_page(GFP_KERNEL);
8018 if (!vmx_msr_bitmap_longmode_x2apic)
8019 goto out4;
Abel Gordon4607c2d2013-04-18 14:35:55 +03008020 vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
8021 if (!vmx_vmread_bitmap)
8022 goto out5;
8023
8024 vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL);
8025 if (!vmx_vmwrite_bitmap)
8026 goto out6;
8027
8028 memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE);
8029 memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE);
8030 /* shadowed read/write fields */
8031 for (i = 0; i < max_shadow_read_write_fields; i++) {
8032 clear_bit(shadow_read_write_fields[i], vmx_vmwrite_bitmap);
8033 clear_bit(shadow_read_write_fields[i], vmx_vmread_bitmap);
8034 }
8035 /* shadowed read only fields */
8036 for (i = 0; i < max_shadow_read_only_fields; i++)
8037 clear_bit(shadow_read_only_fields[i], vmx_vmread_bitmap);
Avi Kivity58972972009-02-24 22:26:47 +02008038
He, Qingfdef3ad2007-04-30 09:45:24 +03008039 /*
8040 * Allow direct access to the PC debug port (it is often used for I/O
8041 * delays, but the vmexits simply slow things down).
8042 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008043 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
8044 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03008045
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008046 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03008047
Avi Kivity58972972009-02-24 22:26:47 +02008048 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
8049 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08008050
Sheng Yang2384d2b2008-01-17 15:14:33 +08008051 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
8052
Avi Kivity0ee75be2010-04-28 15:39:01 +03008053 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
8054 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03008055 if (r)
Abel Gordon4607c2d2013-04-18 14:35:55 +03008056 goto out7;
Sheng Yang25c5f222008-03-28 13:18:56 +08008057
Zhang Yanfei8f536b72012-12-06 23:43:34 +08008058#ifdef CONFIG_KEXEC
8059 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
8060 crash_vmclear_local_loaded_vmcss);
8061#endif
8062
Avi Kivity58972972009-02-24 22:26:47 +02008063 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
8064 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
8065 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
8066 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
8067 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
8068 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
Yang Zhang8d146952013-01-25 10:18:50 +08008069 memcpy(vmx_msr_bitmap_legacy_x2apic,
8070 vmx_msr_bitmap_legacy, PAGE_SIZE);
8071 memcpy(vmx_msr_bitmap_longmode_x2apic,
8072 vmx_msr_bitmap_longmode, PAGE_SIZE);
8073
Yang Zhang01e439b2013-04-11 19:25:12 +08008074 if (enable_apicv) {
Yang Zhang8d146952013-01-25 10:18:50 +08008075 for (msr = 0x800; msr <= 0x8ff; msr++)
8076 vmx_disable_intercept_msr_read_x2apic(msr);
8077
8078 /* According SDM, in x2apic mode, the whole id reg is used.
8079 * But in KVM, it only use the highest eight bits. Need to
8080 * intercept it */
8081 vmx_enable_intercept_msr_read_x2apic(0x802);
8082 /* TMCCT */
8083 vmx_enable_intercept_msr_read_x2apic(0x839);
8084 /* TPR */
8085 vmx_disable_intercept_msr_write_x2apic(0x808);
Yang Zhangc7c9c562013-01-25 10:18:51 +08008086 /* EOI */
8087 vmx_disable_intercept_msr_write_x2apic(0x80b);
8088 /* SELF-IPI */
8089 vmx_disable_intercept_msr_write_x2apic(0x83f);
Yang Zhang8d146952013-01-25 10:18:50 +08008090 }
He, Qingfdef3ad2007-04-30 09:45:24 +03008091
Avi Kivity089d0342009-03-23 18:26:32 +02008092 if (enable_ept) {
Xudong Hao3f6d8c82012-05-22 11:23:15 +08008093 kvm_mmu_set_mask_ptes(0ull,
8094 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
8095 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
8096 0ull, VMX_EPT_EXECUTABLE_MASK);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08008097 ept_set_mmio_spte_mask();
Sheng Yang5fdbcb92008-07-16 09:25:40 +08008098 kvm_enable_tdp();
8099 } else
8100 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08008101
He, Qingfdef3ad2007-04-30 09:45:24 +03008102 return 0;
8103
Abel Gordon4607c2d2013-04-18 14:35:55 +03008104out7:
8105 free_page((unsigned long)vmx_vmwrite_bitmap);
8106out6:
8107 free_page((unsigned long)vmx_vmread_bitmap);
Yang Zhang458f2122013-04-08 15:26:33 +08008108out5:
8109 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Yang Zhang8d146952013-01-25 10:18:50 +08008110out4:
Avi Kivity58972972009-02-24 22:26:47 +02008111 free_page((unsigned long)vmx_msr_bitmap_longmode);
Yang Zhang8d146952013-01-25 10:18:50 +08008112out3:
8113 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
Sheng Yang25c5f222008-03-28 13:18:56 +08008114out2:
Avi Kivity58972972009-02-24 22:26:47 +02008115 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03008116out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008117 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03008118out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008119 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03008120 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08008121}
8122
8123static void __exit vmx_exit(void)
8124{
Yang Zhang8d146952013-01-25 10:18:50 +08008125 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
8126 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Avi Kivity58972972009-02-24 22:26:47 +02008127 free_page((unsigned long)vmx_msr_bitmap_legacy);
8128 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02008129 free_page((unsigned long)vmx_io_bitmap_b);
8130 free_page((unsigned long)vmx_io_bitmap_a);
Abel Gordon4607c2d2013-04-18 14:35:55 +03008131 free_page((unsigned long)vmx_vmwrite_bitmap);
8132 free_page((unsigned long)vmx_vmread_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03008133
Zhang Yanfei8f536b72012-12-06 23:43:34 +08008134#ifdef CONFIG_KEXEC
8135 rcu_assign_pointer(crash_vmclear_loaded_vmcss, NULL);
8136 synchronize_rcu();
8137#endif
8138
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08008139 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08008140}
8141
8142module_init(vmx_init)
8143module_exit(vmx_exit)