blob: fe9a9cfadbd69162f708a4938fcb83f6dc73240f [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 Zhangc7c9c562013-01-25 10:18:51 +080087static bool __read_mostly enable_apicv_reg_vid = 1;
88module_param(enable_apicv_reg_vid, bool, S_IRUGO);
Yang Zhang83d4c282013-01-25 10:18:49 +080089
Nadav Har'El801d3422011-05-25 23:02:23 +030090/*
91 * If nested=1, nested virtualization is supported, i.e., guests may use
92 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
93 * use VMX instructions.
94 */
Rusty Russell476bc002012-01-13 09:32:18 +103095static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +030096module_param(nested, bool, S_IRUGO);
97
Avi Kivitycdc0e242009-12-06 17:21:14 +020098#define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
99 (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
100#define KVM_GUEST_CR0_MASK \
101 (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
102#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
Avi Kivity81231c62010-01-24 16:26:40 +0200103 (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200104#define KVM_VM_CR0_ALWAYS_ON \
105 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200106#define KVM_CR4_GUEST_OWNED_BITS \
107 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
108 | X86_CR4_OSXMMEXCPT)
109
Avi Kivitycdc0e242009-12-06 17:21:14 +0200110#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
111#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
112
Avi Kivity78ac8b42010-04-08 18:19:35 +0300113#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
114
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800115/*
116 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
117 * ple_gap: upper bound on the amount of time between two successive
118 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500119 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800120 * ple_window: upper bound on the amount of time a guest is allowed to execute
121 * in a PAUSE loop. Tests indicate that most spinlocks are held for
122 * less than 2^12 cycles
123 * Time is measured based on a counter that runs at the same rate as the TSC,
124 * refer SDM volume 3b section 21.6.13 & 22.1.3.
125 */
Rik van Riel00c25bc2011-01-04 09:51:33 -0500126#define KVM_VMX_DEFAULT_PLE_GAP 128
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800127#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
128static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
129module_param(ple_gap, int, S_IRUGO);
130
131static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
132module_param(ple_window, int, S_IRUGO);
133
Avi Kivity83287ea422012-09-16 15:10:57 +0300134extern const ulong vmx_return;
135
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200136#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300137#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300138
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400139struct vmcs {
140 u32 revision_id;
141 u32 abort;
142 char data[0];
143};
144
Nadav Har'Eld462b812011-05-24 15:26:10 +0300145/*
146 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
147 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
148 * loaded on this CPU (so we can clear them if the CPU goes down).
149 */
150struct loaded_vmcs {
151 struct vmcs *vmcs;
152 int cpu;
153 int launched;
154 struct list_head loaded_vmcss_on_cpu_link;
155};
156
Avi Kivity26bb0982009-09-07 11:14:12 +0300157struct shared_msr_entry {
158 unsigned index;
159 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200160 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300161};
162
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300163/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300164 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
165 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
166 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
167 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
168 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
169 * More than one of these structures may exist, if L1 runs multiple L2 guests.
170 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
171 * underlying hardware which will be used to run L2.
172 * This structure is packed to ensure that its layout is identical across
173 * machines (necessary for live migration).
174 * If there are changes in this struct, VMCS12_REVISION must be changed.
175 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300176typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300177struct __packed vmcs12 {
178 /* According to the Intel spec, a VMCS region must start with the
179 * following two fields. Then follow implementation-specific data.
180 */
181 u32 revision_id;
182 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300183
Nadav Har'El27d6c862011-05-25 23:06:59 +0300184 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
185 u32 padding[7]; /* room for future expansion */
186
Nadav Har'El22bd0352011-05-25 23:05:57 +0300187 u64 io_bitmap_a;
188 u64 io_bitmap_b;
189 u64 msr_bitmap;
190 u64 vm_exit_msr_store_addr;
191 u64 vm_exit_msr_load_addr;
192 u64 vm_entry_msr_load_addr;
193 u64 tsc_offset;
194 u64 virtual_apic_page_addr;
195 u64 apic_access_addr;
196 u64 ept_pointer;
197 u64 guest_physical_address;
198 u64 vmcs_link_pointer;
199 u64 guest_ia32_debugctl;
200 u64 guest_ia32_pat;
201 u64 guest_ia32_efer;
202 u64 guest_ia32_perf_global_ctrl;
203 u64 guest_pdptr0;
204 u64 guest_pdptr1;
205 u64 guest_pdptr2;
206 u64 guest_pdptr3;
207 u64 host_ia32_pat;
208 u64 host_ia32_efer;
209 u64 host_ia32_perf_global_ctrl;
210 u64 padding64[8]; /* room for future expansion */
211 /*
212 * To allow migration of L1 (complete with its L2 guests) between
213 * machines of different natural widths (32 or 64 bit), we cannot have
214 * unsigned long fields with no explict size. We use u64 (aliased
215 * natural_width) instead. Luckily, x86 is little-endian.
216 */
217 natural_width cr0_guest_host_mask;
218 natural_width cr4_guest_host_mask;
219 natural_width cr0_read_shadow;
220 natural_width cr4_read_shadow;
221 natural_width cr3_target_value0;
222 natural_width cr3_target_value1;
223 natural_width cr3_target_value2;
224 natural_width cr3_target_value3;
225 natural_width exit_qualification;
226 natural_width guest_linear_address;
227 natural_width guest_cr0;
228 natural_width guest_cr3;
229 natural_width guest_cr4;
230 natural_width guest_es_base;
231 natural_width guest_cs_base;
232 natural_width guest_ss_base;
233 natural_width guest_ds_base;
234 natural_width guest_fs_base;
235 natural_width guest_gs_base;
236 natural_width guest_ldtr_base;
237 natural_width guest_tr_base;
238 natural_width guest_gdtr_base;
239 natural_width guest_idtr_base;
240 natural_width guest_dr7;
241 natural_width guest_rsp;
242 natural_width guest_rip;
243 natural_width guest_rflags;
244 natural_width guest_pending_dbg_exceptions;
245 natural_width guest_sysenter_esp;
246 natural_width guest_sysenter_eip;
247 natural_width host_cr0;
248 natural_width host_cr3;
249 natural_width host_cr4;
250 natural_width host_fs_base;
251 natural_width host_gs_base;
252 natural_width host_tr_base;
253 natural_width host_gdtr_base;
254 natural_width host_idtr_base;
255 natural_width host_ia32_sysenter_esp;
256 natural_width host_ia32_sysenter_eip;
257 natural_width host_rsp;
258 natural_width host_rip;
259 natural_width paddingl[8]; /* room for future expansion */
260 u32 pin_based_vm_exec_control;
261 u32 cpu_based_vm_exec_control;
262 u32 exception_bitmap;
263 u32 page_fault_error_code_mask;
264 u32 page_fault_error_code_match;
265 u32 cr3_target_count;
266 u32 vm_exit_controls;
267 u32 vm_exit_msr_store_count;
268 u32 vm_exit_msr_load_count;
269 u32 vm_entry_controls;
270 u32 vm_entry_msr_load_count;
271 u32 vm_entry_intr_info_field;
272 u32 vm_entry_exception_error_code;
273 u32 vm_entry_instruction_len;
274 u32 tpr_threshold;
275 u32 secondary_vm_exec_control;
276 u32 vm_instruction_error;
277 u32 vm_exit_reason;
278 u32 vm_exit_intr_info;
279 u32 vm_exit_intr_error_code;
280 u32 idt_vectoring_info_field;
281 u32 idt_vectoring_error_code;
282 u32 vm_exit_instruction_len;
283 u32 vmx_instruction_info;
284 u32 guest_es_limit;
285 u32 guest_cs_limit;
286 u32 guest_ss_limit;
287 u32 guest_ds_limit;
288 u32 guest_fs_limit;
289 u32 guest_gs_limit;
290 u32 guest_ldtr_limit;
291 u32 guest_tr_limit;
292 u32 guest_gdtr_limit;
293 u32 guest_idtr_limit;
294 u32 guest_es_ar_bytes;
295 u32 guest_cs_ar_bytes;
296 u32 guest_ss_ar_bytes;
297 u32 guest_ds_ar_bytes;
298 u32 guest_fs_ar_bytes;
299 u32 guest_gs_ar_bytes;
300 u32 guest_ldtr_ar_bytes;
301 u32 guest_tr_ar_bytes;
302 u32 guest_interruptibility_info;
303 u32 guest_activity_state;
304 u32 guest_sysenter_cs;
305 u32 host_ia32_sysenter_cs;
306 u32 padding32[8]; /* room for future expansion */
307 u16 virtual_processor_id;
308 u16 guest_es_selector;
309 u16 guest_cs_selector;
310 u16 guest_ss_selector;
311 u16 guest_ds_selector;
312 u16 guest_fs_selector;
313 u16 guest_gs_selector;
314 u16 guest_ldtr_selector;
315 u16 guest_tr_selector;
316 u16 host_es_selector;
317 u16 host_cs_selector;
318 u16 host_ss_selector;
319 u16 host_ds_selector;
320 u16 host_fs_selector;
321 u16 host_gs_selector;
322 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300323};
324
325/*
326 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
327 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
328 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
329 */
330#define VMCS12_REVISION 0x11e57ed0
331
332/*
333 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
334 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
335 * current implementation, 4K are reserved to avoid future complications.
336 */
337#define VMCS12_SIZE 0x1000
338
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300339/* Used to remember the last vmcs02 used for some recently used vmcs12s */
340struct vmcs02_list {
341 struct list_head list;
342 gpa_t vmptr;
343 struct loaded_vmcs vmcs02;
344};
345
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300346/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300347 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
348 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
349 */
350struct nested_vmx {
351 /* Has the level1 guest done vmxon? */
352 bool vmxon;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300353
354 /* The guest-physical address of the current VMCS L1 keeps for L2 */
355 gpa_t current_vmptr;
356 /* The host-usable pointer to the above */
357 struct page *current_vmcs12_page;
358 struct vmcs12 *current_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300359
360 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
361 struct list_head vmcs02_pool;
362 int vmcs02_num;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300363 u64 vmcs01_tsc_offset;
Nadav Har'El644d7112011-05-25 23:12:35 +0300364 /* L2 must run next, and mustn't decide to exit to L1. */
365 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300366 /*
367 * Guest pages referred to in vmcs02 with host-physical pointers, so
368 * we must keep them pinned while L2 runs.
369 */
370 struct page *apic_access_page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300371};
372
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400373struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000374 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300375 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300376 u8 fail;
Avi Kivity69c73022011-03-07 15:26:44 +0200377 u8 cpl;
Avi Kivity9d58b932011-03-07 16:52:07 +0200378 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300379 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200380 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200381 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300382 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400383 int nmsrs;
384 int save_nmsrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400385#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300386 u64 msr_host_kernel_gs_base;
387 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400388#endif
Nadav Har'Eld462b812011-05-24 15:26:10 +0300389 /*
390 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
391 * non-nested (L1) guest, it always points to vmcs01. For a nested
392 * guest (L2), it points to a different VMCS.
393 */
394 struct loaded_vmcs vmcs01;
395 struct loaded_vmcs *loaded_vmcs;
396 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300397 struct msr_autoload {
398 unsigned nr;
399 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
400 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
401 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400402 struct {
403 int loaded;
404 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300405#ifdef CONFIG_X86_64
406 u16 ds_sel, es_sel;
407#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200408 int gs_ldt_reload_needed;
409 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400410 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200411 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300412 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300413 ulong save_rflags;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300414 struct kvm_segment segs[8];
415 } rmode;
416 struct {
417 u32 bitmask; /* 4 bits per segment (1 bit per field) */
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300418 struct kvm_save_segment {
419 u16 selector;
420 unsigned long base;
421 u32 limit;
422 u32 ar;
Avi Kivityf5f7b2f2012-08-21 17:07:00 +0300423 } seg[8];
Avi Kivity2fb92db2011-04-27 19:42:18 +0300424 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800425 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300426 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200427
428 /* Support for vnmi-less CPUs */
429 int soft_vnmi_blocked;
430 ktime_t entry_time;
431 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800432 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800433
434 bool rdtscp_enabled;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300435
436 /* Support for a guest hypervisor (nested VMX) */
437 struct nested_vmx nested;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400438};
439
Avi Kivity2fb92db2011-04-27 19:42:18 +0300440enum segment_cache_field {
441 SEG_FIELD_SEL = 0,
442 SEG_FIELD_BASE = 1,
443 SEG_FIELD_LIMIT = 2,
444 SEG_FIELD_AR = 3,
445
446 SEG_FIELD_NR = 4
447};
448
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400449static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
450{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000451 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400452}
453
Nadav Har'El22bd0352011-05-25 23:05:57 +0300454#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
455#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
456#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
457 [number##_HIGH] = VMCS12_OFFSET(name)+4
458
Mathias Krause772e0312012-08-30 01:30:19 +0200459static const unsigned short vmcs_field_to_offset_table[] = {
Nadav Har'El22bd0352011-05-25 23:05:57 +0300460 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
461 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
462 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
463 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
464 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
465 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
466 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
467 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
468 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
469 FIELD(HOST_ES_SELECTOR, host_es_selector),
470 FIELD(HOST_CS_SELECTOR, host_cs_selector),
471 FIELD(HOST_SS_SELECTOR, host_ss_selector),
472 FIELD(HOST_DS_SELECTOR, host_ds_selector),
473 FIELD(HOST_FS_SELECTOR, host_fs_selector),
474 FIELD(HOST_GS_SELECTOR, host_gs_selector),
475 FIELD(HOST_TR_SELECTOR, host_tr_selector),
476 FIELD64(IO_BITMAP_A, io_bitmap_a),
477 FIELD64(IO_BITMAP_B, io_bitmap_b),
478 FIELD64(MSR_BITMAP, msr_bitmap),
479 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
480 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
481 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
482 FIELD64(TSC_OFFSET, tsc_offset),
483 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
484 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
485 FIELD64(EPT_POINTER, ept_pointer),
486 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
487 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
488 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
489 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
490 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
491 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
492 FIELD64(GUEST_PDPTR0, guest_pdptr0),
493 FIELD64(GUEST_PDPTR1, guest_pdptr1),
494 FIELD64(GUEST_PDPTR2, guest_pdptr2),
495 FIELD64(GUEST_PDPTR3, guest_pdptr3),
496 FIELD64(HOST_IA32_PAT, host_ia32_pat),
497 FIELD64(HOST_IA32_EFER, host_ia32_efer),
498 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
499 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
500 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
501 FIELD(EXCEPTION_BITMAP, exception_bitmap),
502 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
503 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
504 FIELD(CR3_TARGET_COUNT, cr3_target_count),
505 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
506 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
507 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
508 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
509 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
510 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
511 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
512 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
513 FIELD(TPR_THRESHOLD, tpr_threshold),
514 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
515 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
516 FIELD(VM_EXIT_REASON, vm_exit_reason),
517 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
518 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
519 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
520 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
521 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
522 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
523 FIELD(GUEST_ES_LIMIT, guest_es_limit),
524 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
525 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
526 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
527 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
528 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
529 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
530 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
531 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
532 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
533 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
534 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
535 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
536 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
537 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
538 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
539 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
540 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
541 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
542 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
543 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
544 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
545 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
546 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
547 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
548 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
549 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
550 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
551 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
552 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
553 FIELD(EXIT_QUALIFICATION, exit_qualification),
554 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
555 FIELD(GUEST_CR0, guest_cr0),
556 FIELD(GUEST_CR3, guest_cr3),
557 FIELD(GUEST_CR4, guest_cr4),
558 FIELD(GUEST_ES_BASE, guest_es_base),
559 FIELD(GUEST_CS_BASE, guest_cs_base),
560 FIELD(GUEST_SS_BASE, guest_ss_base),
561 FIELD(GUEST_DS_BASE, guest_ds_base),
562 FIELD(GUEST_FS_BASE, guest_fs_base),
563 FIELD(GUEST_GS_BASE, guest_gs_base),
564 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
565 FIELD(GUEST_TR_BASE, guest_tr_base),
566 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
567 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
568 FIELD(GUEST_DR7, guest_dr7),
569 FIELD(GUEST_RSP, guest_rsp),
570 FIELD(GUEST_RIP, guest_rip),
571 FIELD(GUEST_RFLAGS, guest_rflags),
572 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
573 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
574 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
575 FIELD(HOST_CR0, host_cr0),
576 FIELD(HOST_CR3, host_cr3),
577 FIELD(HOST_CR4, host_cr4),
578 FIELD(HOST_FS_BASE, host_fs_base),
579 FIELD(HOST_GS_BASE, host_gs_base),
580 FIELD(HOST_TR_BASE, host_tr_base),
581 FIELD(HOST_GDTR_BASE, host_gdtr_base),
582 FIELD(HOST_IDTR_BASE, host_idtr_base),
583 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
584 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
585 FIELD(HOST_RSP, host_rsp),
586 FIELD(HOST_RIP, host_rip),
587};
588static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table);
589
590static inline short vmcs_field_to_offset(unsigned long field)
591{
592 if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0)
593 return -1;
594 return vmcs_field_to_offset_table[field];
595}
596
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300597static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
598{
599 return to_vmx(vcpu)->nested.current_vmcs12;
600}
601
602static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
603{
604 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
Xiao Guangrong32cad842012-08-03 15:42:52 +0800605 if (is_error_page(page))
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300606 return NULL;
Xiao Guangrong32cad842012-08-03 15:42:52 +0800607
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300608 return page;
609}
610
611static void nested_release_page(struct page *page)
612{
613 kvm_release_page_dirty(page);
614}
615
616static void nested_release_page_clean(struct page *page)
617{
618 kvm_release_page_clean(page);
619}
620
Sheng Yang4e1096d2008-07-06 19:16:51 +0800621static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800622static void kvm_cpu_vmxon(u64 addr);
623static void kvm_cpu_vmxoff(void);
Avi Kivityaff48ba2010-12-05 18:56:11 +0200624static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200625static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Orit Wassermanb246dd52012-05-31 14:49:22 +0300626static void vmx_set_segment(struct kvm_vcpu *vcpu,
627 struct kvm_segment *var, int seg);
628static void vmx_get_segment(struct kvm_vcpu *vcpu,
629 struct kvm_segment *var, int seg);
Gleb Natapovd99e4152012-12-20 16:57:45 +0200630static bool guest_state_valid(struct kvm_vcpu *vcpu);
631static u32 vmx_segment_access_rights(struct kvm_segment *var);
Avi Kivity75880a02007-06-20 11:20:04 +0300632
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633static DEFINE_PER_CPU(struct vmcs *, vmxarea);
634static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300635/*
636 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
637 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
638 */
639static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300640static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800641
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200642static unsigned long *vmx_io_bitmap_a;
643static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200644static unsigned long *vmx_msr_bitmap_legacy;
645static unsigned long *vmx_msr_bitmap_longmode;
Yang Zhang8d146952013-01-25 10:18:50 +0800646static unsigned long *vmx_msr_bitmap_legacy_x2apic;
647static unsigned long *vmx_msr_bitmap_longmode_x2apic;
He, Qingfdef3ad2007-04-30 09:45:24 +0300648
Avi Kivity110312c2010-12-21 12:54:20 +0200649static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200650static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200651
Sheng Yang2384d2b2008-01-17 15:14:33 +0800652static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
653static DEFINE_SPINLOCK(vmx_vpid_lock);
654
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300655static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656 int size;
657 int order;
658 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300659 u32 pin_based_exec_ctrl;
660 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800661 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300662 u32 vmexit_ctrl;
663 u32 vmentry_ctrl;
664} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665
Hannes Ederefff9e52008-11-28 17:02:06 +0100666static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800667 u32 ept;
668 u32 vpid;
669} vmx_capability;
670
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671#define VMX_SEGMENT_FIELD(seg) \
672 [VCPU_SREG_##seg] = { \
673 .selector = GUEST_##seg##_SELECTOR, \
674 .base = GUEST_##seg##_BASE, \
675 .limit = GUEST_##seg##_LIMIT, \
676 .ar_bytes = GUEST_##seg##_AR_BYTES, \
677 }
678
Mathias Krause772e0312012-08-30 01:30:19 +0200679static const struct kvm_vmx_segment_field {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800680 unsigned selector;
681 unsigned base;
682 unsigned limit;
683 unsigned ar_bytes;
684} kvm_vmx_segment_fields[] = {
685 VMX_SEGMENT_FIELD(CS),
686 VMX_SEGMENT_FIELD(DS),
687 VMX_SEGMENT_FIELD(ES),
688 VMX_SEGMENT_FIELD(FS),
689 VMX_SEGMENT_FIELD(GS),
690 VMX_SEGMENT_FIELD(SS),
691 VMX_SEGMENT_FIELD(TR),
692 VMX_SEGMENT_FIELD(LDTR),
693};
694
Avi Kivity26bb0982009-09-07 11:14:12 +0300695static u64 host_efer;
696
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300697static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
698
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300699/*
Brian Gerst8c065852010-07-17 09:03:26 -0400700 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300701 * away by decrementing the array size.
702 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800704#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300705 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400707 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200709#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800710
Gui Jianfeng31299942010-03-15 17:29:09 +0800711static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712{
713 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
714 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100715 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716}
717
Gui Jianfeng31299942010-03-15 17:29:09 +0800718static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300719{
720 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
721 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100722 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300723}
724
Gui Jianfeng31299942010-03-15 17:29:09 +0800725static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500726{
727 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
728 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100729 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500730}
731
Gui Jianfeng31299942010-03-15 17:29:09 +0800732static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733{
734 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
735 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
736}
737
Gui Jianfeng31299942010-03-15 17:29:09 +0800738static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800739{
740 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
741 INTR_INFO_VALID_MASK)) ==
742 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
743}
744
Gui Jianfeng31299942010-03-15 17:29:09 +0800745static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800746{
Sheng Yang04547152009-04-01 15:52:31 +0800747 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800748}
749
Gui Jianfeng31299942010-03-15 17:29:09 +0800750static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800751{
Sheng Yang04547152009-04-01 15:52:31 +0800752 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800753}
754
Gui Jianfeng31299942010-03-15 17:29:09 +0800755static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800756{
Sheng Yang04547152009-04-01 15:52:31 +0800757 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800758}
759
Gui Jianfeng31299942010-03-15 17:29:09 +0800760static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800761{
Sheng Yang04547152009-04-01 15:52:31 +0800762 return vmcs_config.cpu_based_exec_ctrl &
763 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800764}
765
Avi Kivity774ead32007-12-26 13:57:04 +0200766static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800767{
Sheng Yang04547152009-04-01 15:52:31 +0800768 return vmcs_config.cpu_based_2nd_exec_ctrl &
769 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
770}
771
Yang Zhang8d146952013-01-25 10:18:50 +0800772static inline bool cpu_has_vmx_virtualize_x2apic_mode(void)
773{
774 return vmcs_config.cpu_based_2nd_exec_ctrl &
775 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
776}
777
Yang Zhang83d4c282013-01-25 10:18:49 +0800778static inline bool cpu_has_vmx_apic_register_virt(void)
779{
780 return vmcs_config.cpu_based_2nd_exec_ctrl &
781 SECONDARY_EXEC_APIC_REGISTER_VIRT;
782}
783
Yang Zhangc7c9c562013-01-25 10:18:51 +0800784static inline bool cpu_has_vmx_virtual_intr_delivery(void)
785{
786 return vmcs_config.cpu_based_2nd_exec_ctrl &
787 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
788}
789
Sheng Yang04547152009-04-01 15:52:31 +0800790static inline bool cpu_has_vmx_flexpriority(void)
791{
792 return cpu_has_vmx_tpr_shadow() &&
793 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800794}
795
Marcelo Tosattie7997942009-06-11 12:07:40 -0300796static inline bool cpu_has_vmx_ept_execute_only(void)
797{
Gui Jianfeng31299942010-03-15 17:29:09 +0800798 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300799}
800
801static inline bool cpu_has_vmx_eptp_uncacheable(void)
802{
Gui Jianfeng31299942010-03-15 17:29:09 +0800803 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300804}
805
806static inline bool cpu_has_vmx_eptp_writeback(void)
807{
Gui Jianfeng31299942010-03-15 17:29:09 +0800808 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300809}
810
811static inline bool cpu_has_vmx_ept_2m_page(void)
812{
Gui Jianfeng31299942010-03-15 17:29:09 +0800813 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300814}
815
Sheng Yang878403b2010-01-05 19:02:29 +0800816static inline bool cpu_has_vmx_ept_1g_page(void)
817{
Gui Jianfeng31299942010-03-15 17:29:09 +0800818 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800819}
820
Sheng Yang4bc9b982010-06-02 14:05:24 +0800821static inline bool cpu_has_vmx_ept_4levels(void)
822{
823 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
824}
825
Xudong Hao83c3a332012-05-28 19:33:35 +0800826static inline bool cpu_has_vmx_ept_ad_bits(void)
827{
828 return vmx_capability.ept & VMX_EPT_AD_BIT;
829}
830
Gui Jianfeng31299942010-03-15 17:29:09 +0800831static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800832{
Gui Jianfeng31299942010-03-15 17:29:09 +0800833 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800834}
835
Gui Jianfeng31299942010-03-15 17:29:09 +0800836static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800837{
Gui Jianfeng31299942010-03-15 17:29:09 +0800838 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800839}
840
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800841static inline bool cpu_has_vmx_invvpid_single(void)
842{
843 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
844}
845
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800846static inline bool cpu_has_vmx_invvpid_global(void)
847{
848 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
849}
850
Gui Jianfeng31299942010-03-15 17:29:09 +0800851static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800852{
Sheng Yang04547152009-04-01 15:52:31 +0800853 return vmcs_config.cpu_based_2nd_exec_ctrl &
854 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800855}
856
Gui Jianfeng31299942010-03-15 17:29:09 +0800857static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700858{
859 return vmcs_config.cpu_based_2nd_exec_ctrl &
860 SECONDARY_EXEC_UNRESTRICTED_GUEST;
861}
862
Gui Jianfeng31299942010-03-15 17:29:09 +0800863static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800864{
865 return vmcs_config.cpu_based_2nd_exec_ctrl &
866 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
867}
868
Gui Jianfeng31299942010-03-15 17:29:09 +0800869static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800870{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800871 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800872}
873
Gui Jianfeng31299942010-03-15 17:29:09 +0800874static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800875{
Sheng Yang04547152009-04-01 15:52:31 +0800876 return vmcs_config.cpu_based_2nd_exec_ctrl &
877 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800878}
879
Gui Jianfeng31299942010-03-15 17:29:09 +0800880static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800881{
882 return vmcs_config.cpu_based_2nd_exec_ctrl &
883 SECONDARY_EXEC_RDTSCP;
884}
885
Mao, Junjiead756a12012-07-02 01:18:48 +0000886static inline bool cpu_has_vmx_invpcid(void)
887{
888 return vmcs_config.cpu_based_2nd_exec_ctrl &
889 SECONDARY_EXEC_ENABLE_INVPCID;
890}
891
Gui Jianfeng31299942010-03-15 17:29:09 +0800892static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +0800893{
894 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
895}
896
Sheng Yangf5f48ee2010-06-30 12:25:15 +0800897static inline bool cpu_has_vmx_wbinvd_exit(void)
898{
899 return vmcs_config.cpu_based_2nd_exec_ctrl &
900 SECONDARY_EXEC_WBINVD_EXITING;
901}
902
Sheng Yang04547152009-04-01 15:52:31 +0800903static inline bool report_flexpriority(void)
904{
905 return flexpriority_enabled;
906}
907
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300908static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
909{
910 return vmcs12->cpu_based_vm_exec_control & bit;
911}
912
913static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
914{
915 return (vmcs12->cpu_based_vm_exec_control &
916 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
917 (vmcs12->secondary_vm_exec_control & bit);
918}
919
Nadav Har'El644d7112011-05-25 23:12:35 +0300920static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12,
921 struct kvm_vcpu *vcpu)
922{
923 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
924}
925
926static inline bool is_exception(u32 intr_info)
927{
928 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
929 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
930}
931
932static void nested_vmx_vmexit(struct kvm_vcpu *vcpu);
Nadav Har'El7c177932011-05-25 23:12:04 +0300933static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
934 struct vmcs12 *vmcs12,
935 u32 reason, unsigned long qualification);
936
Rusty Russell8b9cf982007-07-30 16:31:43 +1000937static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800938{
939 int i;
940
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400941 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300942 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300943 return i;
944 return -1;
945}
946
Sheng Yang2384d2b2008-01-17 15:14:33 +0800947static inline void __invvpid(int ext, u16 vpid, gva_t gva)
948{
949 struct {
950 u64 vpid : 16;
951 u64 rsvd : 48;
952 u64 gva;
953 } operand = { vpid, 0, gva };
954
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300955 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800956 /* CF==1 or ZF==1 --> rc = -1 */
957 "; ja 1f ; ud2 ; 1:"
958 : : "a"(&operand), "c"(ext) : "cc", "memory");
959}
960
Sheng Yang14394422008-04-28 12:24:45 +0800961static inline void __invept(int ext, u64 eptp, gpa_t gpa)
962{
963 struct {
964 u64 eptp, gpa;
965 } operand = {eptp, gpa};
966
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300967 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800968 /* CF==1 or ZF==1 --> rc = -1 */
969 "; ja 1f ; ud2 ; 1:\n"
970 : : "a" (&operand), "c" (ext) : "cc", "memory");
971}
972
Avi Kivity26bb0982009-09-07 11:14:12 +0300973static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300974{
975 int i;
976
Rusty Russell8b9cf982007-07-30 16:31:43 +1000977 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300978 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400979 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000980 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800981}
982
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983static void vmcs_clear(struct vmcs *vmcs)
984{
985 u64 phys_addr = __pa(vmcs);
986 u8 error;
987
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300988 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200989 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990 : "cc", "memory");
991 if (error)
992 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
993 vmcs, phys_addr);
994}
995
Nadav Har'Eld462b812011-05-24 15:26:10 +0300996static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
997{
998 vmcs_clear(loaded_vmcs->vmcs);
999 loaded_vmcs->cpu = -1;
1000 loaded_vmcs->launched = 0;
1001}
1002
Dongxiao Xu7725b892010-05-11 18:29:38 +08001003static void vmcs_load(struct vmcs *vmcs)
1004{
1005 u64 phys_addr = __pa(vmcs);
1006 u8 error;
1007
1008 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +02001009 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +08001010 : "cc", "memory");
1011 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +03001012 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +08001013 vmcs, phys_addr);
1014}
1015
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001016#ifdef CONFIG_KEXEC
1017/*
1018 * This bitmap is used to indicate whether the vmclear
1019 * operation is enabled on all cpus. All disabled by
1020 * default.
1021 */
1022static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1023
1024static inline void crash_enable_local_vmclear(int cpu)
1025{
1026 cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1027}
1028
1029static inline void crash_disable_local_vmclear(int cpu)
1030{
1031 cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1032}
1033
1034static inline int crash_local_vmclear_enabled(int cpu)
1035{
1036 return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1037}
1038
1039static void crash_vmclear_local_loaded_vmcss(void)
1040{
1041 int cpu = raw_smp_processor_id();
1042 struct loaded_vmcs *v;
1043
1044 if (!crash_local_vmclear_enabled(cpu))
1045 return;
1046
1047 list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1048 loaded_vmcss_on_cpu_link)
1049 vmcs_clear(v->vmcs);
1050}
1051#else
1052static inline void crash_enable_local_vmclear(int cpu) { }
1053static inline void crash_disable_local_vmclear(int cpu) { }
1054#endif /* CONFIG_KEXEC */
1055
Nadav Har'Eld462b812011-05-24 15:26:10 +03001056static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057{
Nadav Har'Eld462b812011-05-24 15:26:10 +03001058 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -08001059 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060
Nadav Har'Eld462b812011-05-24 15:26:10 +03001061 if (loaded_vmcs->cpu != cpu)
1062 return; /* vcpu migration can race with cpu offline */
1063 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 per_cpu(current_vmcs, cpu) = NULL;
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001065 crash_disable_local_vmclear(cpu);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001066 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001067
1068 /*
1069 * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1070 * is before setting loaded_vmcs->vcpu to -1 which is done in
1071 * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1072 * then adds the vmcs into percpu list before it is deleted.
1073 */
1074 smp_wmb();
1075
Nadav Har'Eld462b812011-05-24 15:26:10 +03001076 loaded_vmcs_init(loaded_vmcs);
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001077 crash_enable_local_vmclear(cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078}
1079
Nadav Har'Eld462b812011-05-24 15:26:10 +03001080static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001081{
Xiao Guangronge6c7d322012-11-28 20:53:15 +08001082 int cpu = loaded_vmcs->cpu;
1083
1084 if (cpu != -1)
1085 smp_call_function_single(cpu,
1086 __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001087}
1088
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001089static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001090{
1091 if (vmx->vpid == 0)
1092 return;
1093
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001094 if (cpu_has_vmx_invvpid_single())
1095 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001096}
1097
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001098static inline void vpid_sync_vcpu_global(void)
1099{
1100 if (cpu_has_vmx_invvpid_global())
1101 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1102}
1103
1104static inline void vpid_sync_context(struct vcpu_vmx *vmx)
1105{
1106 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001107 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001108 else
1109 vpid_sync_vcpu_global();
1110}
1111
Sheng Yang14394422008-04-28 12:24:45 +08001112static inline void ept_sync_global(void)
1113{
1114 if (cpu_has_vmx_invept_global())
1115 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1116}
1117
1118static inline void ept_sync_context(u64 eptp)
1119{
Avi Kivity089d0342009-03-23 18:26:32 +02001120 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001121 if (cpu_has_vmx_invept_context())
1122 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1123 else
1124 ept_sync_global();
1125 }
1126}
1127
Avi Kivity96304212011-05-15 10:13:13 -04001128static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129{
Avi Kivity5e520e62011-05-15 10:13:12 -04001130 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131
Avi Kivity5e520e62011-05-15 10:13:12 -04001132 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1133 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134 return value;
1135}
1136
Avi Kivity96304212011-05-15 10:13:13 -04001137static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138{
1139 return vmcs_readl(field);
1140}
1141
Avi Kivity96304212011-05-15 10:13:13 -04001142static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001143{
1144 return vmcs_readl(field);
1145}
1146
Avi Kivity96304212011-05-15 10:13:13 -04001147static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148{
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001149#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150 return vmcs_readl(field);
1151#else
1152 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
1153#endif
1154}
1155
Avi Kivitye52de1b2007-01-05 16:36:56 -08001156static noinline void vmwrite_error(unsigned long field, unsigned long value)
1157{
1158 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1159 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1160 dump_stack();
1161}
1162
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163static void vmcs_writel(unsigned long field, unsigned long value)
1164{
1165 u8 error;
1166
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001167 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001168 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001169 if (unlikely(error))
1170 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171}
1172
1173static void vmcs_write16(unsigned long field, u16 value)
1174{
1175 vmcs_writel(field, value);
1176}
1177
1178static void vmcs_write32(unsigned long field, u32 value)
1179{
1180 vmcs_writel(field, value);
1181}
1182
1183static void vmcs_write64(unsigned long field, u64 value)
1184{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001186#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187 asm volatile ("");
1188 vmcs_writel(field+1, value >> 32);
1189#endif
1190}
1191
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001192static void vmcs_clear_bits(unsigned long field, u32 mask)
1193{
1194 vmcs_writel(field, vmcs_readl(field) & ~mask);
1195}
1196
1197static void vmcs_set_bits(unsigned long field, u32 mask)
1198{
1199 vmcs_writel(field, vmcs_readl(field) | mask);
1200}
1201
Avi Kivity2fb92db2011-04-27 19:42:18 +03001202static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1203{
1204 vmx->segment_cache.bitmask = 0;
1205}
1206
1207static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1208 unsigned field)
1209{
1210 bool ret;
1211 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1212
1213 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1214 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1215 vmx->segment_cache.bitmask = 0;
1216 }
1217 ret = vmx->segment_cache.bitmask & mask;
1218 vmx->segment_cache.bitmask |= mask;
1219 return ret;
1220}
1221
1222static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1223{
1224 u16 *p = &vmx->segment_cache.seg[seg].selector;
1225
1226 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1227 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1228 return *p;
1229}
1230
1231static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1232{
1233 ulong *p = &vmx->segment_cache.seg[seg].base;
1234
1235 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1236 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1237 return *p;
1238}
1239
1240static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1241{
1242 u32 *p = &vmx->segment_cache.seg[seg].limit;
1243
1244 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1245 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1246 return *p;
1247}
1248
1249static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1250{
1251 u32 *p = &vmx->segment_cache.seg[seg].ar;
1252
1253 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1254 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1255 return *p;
1256}
1257
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001258static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1259{
1260 u32 eb;
1261
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001262 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1263 (1u << NM_VECTOR) | (1u << DB_VECTOR);
1264 if ((vcpu->guest_debug &
1265 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1266 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1267 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001268 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001269 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001270 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001271 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001272 if (vcpu->fpu_active)
1273 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001274
1275 /* When we are running a nested L2 guest and L1 specified for it a
1276 * certain exception bitmap, we must trap the same exceptions and pass
1277 * them to L1. When running L2, we will only handle the exceptions
1278 * specified above if L1 did not want them.
1279 */
1280 if (is_guest_mode(vcpu))
1281 eb |= get_vmcs12(vcpu)->exception_bitmap;
1282
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001283 vmcs_write32(EXCEPTION_BITMAP, eb);
1284}
1285
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001286static void clear_atomic_switch_msr_special(unsigned long entry,
1287 unsigned long exit)
1288{
1289 vmcs_clear_bits(VM_ENTRY_CONTROLS, entry);
1290 vmcs_clear_bits(VM_EXIT_CONTROLS, exit);
1291}
1292
Avi Kivity61d2ef22010-04-28 16:40:38 +03001293static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1294{
1295 unsigned i;
1296 struct msr_autoload *m = &vmx->msr_autoload;
1297
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001298 switch (msr) {
1299 case MSR_EFER:
1300 if (cpu_has_load_ia32_efer) {
1301 clear_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
1302 VM_EXIT_LOAD_IA32_EFER);
1303 return;
1304 }
1305 break;
1306 case MSR_CORE_PERF_GLOBAL_CTRL:
1307 if (cpu_has_load_perf_global_ctrl) {
1308 clear_atomic_switch_msr_special(
1309 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1310 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1311 return;
1312 }
1313 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001314 }
1315
Avi Kivity61d2ef22010-04-28 16:40:38 +03001316 for (i = 0; i < m->nr; ++i)
1317 if (m->guest[i].index == msr)
1318 break;
1319
1320 if (i == m->nr)
1321 return;
1322 --m->nr;
1323 m->guest[i] = m->guest[m->nr];
1324 m->host[i] = m->host[m->nr];
1325 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1326 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1327}
1328
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001329static void add_atomic_switch_msr_special(unsigned long entry,
1330 unsigned long exit, unsigned long guest_val_vmcs,
1331 unsigned long host_val_vmcs, u64 guest_val, u64 host_val)
1332{
1333 vmcs_write64(guest_val_vmcs, guest_val);
1334 vmcs_write64(host_val_vmcs, host_val);
1335 vmcs_set_bits(VM_ENTRY_CONTROLS, entry);
1336 vmcs_set_bits(VM_EXIT_CONTROLS, exit);
1337}
1338
Avi Kivity61d2ef22010-04-28 16:40:38 +03001339static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1340 u64 guest_val, u64 host_val)
1341{
1342 unsigned i;
1343 struct msr_autoload *m = &vmx->msr_autoload;
1344
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001345 switch (msr) {
1346 case MSR_EFER:
1347 if (cpu_has_load_ia32_efer) {
1348 add_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
1349 VM_EXIT_LOAD_IA32_EFER,
1350 GUEST_IA32_EFER,
1351 HOST_IA32_EFER,
1352 guest_val, host_val);
1353 return;
1354 }
1355 break;
1356 case MSR_CORE_PERF_GLOBAL_CTRL:
1357 if (cpu_has_load_perf_global_ctrl) {
1358 add_atomic_switch_msr_special(
1359 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1360 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1361 GUEST_IA32_PERF_GLOBAL_CTRL,
1362 HOST_IA32_PERF_GLOBAL_CTRL,
1363 guest_val, host_val);
1364 return;
1365 }
1366 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001367 }
1368
Avi Kivity61d2ef22010-04-28 16:40:38 +03001369 for (i = 0; i < m->nr; ++i)
1370 if (m->guest[i].index == msr)
1371 break;
1372
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001373 if (i == NR_AUTOLOAD_MSRS) {
1374 printk_once(KERN_WARNING"Not enough mst switch entries. "
1375 "Can't add msr %x\n", msr);
1376 return;
1377 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001378 ++m->nr;
1379 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1380 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1381 }
1382
1383 m->guest[i].index = msr;
1384 m->guest[i].value = guest_val;
1385 m->host[i].index = msr;
1386 m->host[i].value = host_val;
1387}
1388
Avi Kivity33ed6322007-05-02 16:54:03 +03001389static void reload_tss(void)
1390{
Avi Kivity33ed6322007-05-02 16:54:03 +03001391 /*
1392 * VT restores TR but not its size. Useless.
1393 */
Avi Kivityd3591922010-07-26 18:32:39 +03001394 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001395 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001396
Avi Kivityd3591922010-07-26 18:32:39 +03001397 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001398 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1399 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001400}
1401
Avi Kivity92c0d902009-10-29 11:00:16 +02001402static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001403{
Roel Kluin3a34a882009-08-04 02:08:45 -07001404 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001405 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +03001406
Avi Kivityf6801df2010-01-21 15:31:50 +02001407 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -07001408
Avi Kivity51c6cf62007-08-29 03:48:05 +03001409 /*
Guo Chao0fa06072012-06-28 15:16:19 +08001410 * NX is emulated; LMA and LME handled by hardware; SCE meaningless
Avi Kivity51c6cf62007-08-29 03:48:05 +03001411 * outside long mode
1412 */
1413 ignore_bits = EFER_NX | EFER_SCE;
1414#ifdef CONFIG_X86_64
1415 ignore_bits |= EFER_LMA | EFER_LME;
1416 /* SCE is meaningful only in long mode on Intel */
1417 if (guest_efer & EFER_LMA)
1418 ignore_bits &= ~(u64)EFER_SCE;
1419#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +03001420 guest_efer &= ~ignore_bits;
1421 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +03001422 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +02001423 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03001424
1425 clear_atomic_switch_msr(vmx, MSR_EFER);
1426 /* On ept, can't emulate nx, and must switch nx atomically */
1427 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
1428 guest_efer = vmx->vcpu.arch.efer;
1429 if (!(guest_efer & EFER_LMA))
1430 guest_efer &= ~EFER_LME;
1431 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
1432 return false;
1433 }
1434
Avi Kivity26bb0982009-09-07 11:14:12 +03001435 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001436}
1437
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001438static unsigned long segment_base(u16 selector)
1439{
Avi Kivityd3591922010-07-26 18:32:39 +03001440 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001441 struct desc_struct *d;
1442 unsigned long table_base;
1443 unsigned long v;
1444
1445 if (!(selector & ~3))
1446 return 0;
1447
Avi Kivityd3591922010-07-26 18:32:39 +03001448 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001449
1450 if (selector & 4) { /* from ldt */
1451 u16 ldt_selector = kvm_read_ldt();
1452
1453 if (!(ldt_selector & ~3))
1454 return 0;
1455
1456 table_base = segment_base(ldt_selector);
1457 }
1458 d = (struct desc_struct *)(table_base + (selector & ~7));
1459 v = get_desc_base(d);
1460#ifdef CONFIG_X86_64
1461 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
1462 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
1463#endif
1464 return v;
1465}
1466
1467static inline unsigned long kvm_read_tr_base(void)
1468{
1469 u16 tr;
1470 asm("str %0" : "=g"(tr));
1471 return segment_base(tr);
1472}
1473
Avi Kivity04d2cc72007-09-10 18:10:54 +03001474static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001475{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001476 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001477 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001478
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001479 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001480 return;
1481
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001482 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001483 /*
1484 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1485 * allow segment selectors with cpl > 0 or ti == 1.
1486 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001487 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02001488 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02001489 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001490 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001491 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001492 vmx->host_state.fs_reload_needed = 0;
1493 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03001494 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001495 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001496 }
Avi Kivity9581d442010-10-19 16:46:55 +02001497 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001498 if (!(vmx->host_state.gs_sel & 7))
1499 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001500 else {
1501 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001502 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001503 }
1504
1505#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03001506 savesegment(ds, vmx->host_state.ds_sel);
1507 savesegment(es, vmx->host_state.es_sel);
1508#endif
1509
1510#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03001511 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1512 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1513#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001514 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1515 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03001516#endif
Avi Kivity707c0872007-05-02 17:33:43 +03001517
1518#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001519 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1520 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03001521 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03001522#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03001523 for (i = 0; i < vmx->save_nmsrs; ++i)
1524 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02001525 vmx->guest_msrs[i].data,
1526 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03001527}
1528
Avi Kivitya9b21b62008-06-24 11:48:49 +03001529static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001530{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001531 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001532 return;
1533
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001534 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001535 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02001536#ifdef CONFIG_X86_64
1537 if (is_long_mode(&vmx->vcpu))
1538 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1539#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001540 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001541 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001542#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02001543 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001544#else
1545 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001546#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001547 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02001548 if (vmx->host_state.fs_reload_needed)
1549 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001550#ifdef CONFIG_X86_64
1551 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
1552 loadsegment(ds, vmx->host_state.ds_sel);
1553 loadsegment(es, vmx->host_state.es_sel);
1554 }
Avi Kivityb2da15a2012-05-13 19:53:24 +03001555#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001556 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001557#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001558 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001559#endif
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07001560 /*
1561 * If the FPU is not active (through the host task or
1562 * the guest vcpu), then restore the cr0.TS bit.
1563 */
1564 if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded)
1565 stts();
Avi Kivity3444d7d2010-07-26 18:32:38 +03001566 load_gdt(&__get_cpu_var(host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03001567}
1568
Avi Kivitya9b21b62008-06-24 11:48:49 +03001569static void vmx_load_host_state(struct vcpu_vmx *vmx)
1570{
1571 preempt_disable();
1572 __vmx_load_host_state(vmx);
1573 preempt_enable();
1574}
1575
Avi Kivity6aa8b732006-12-10 02:21:36 -08001576/*
1577 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1578 * vcpu mutex is already taken.
1579 */
Avi Kivity15ad7142007-07-11 18:17:21 +03001580static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001582 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001583 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001585 if (!vmm_exclusive)
1586 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001587 else if (vmx->loaded_vmcs->cpu != cpu)
1588 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589
Nadav Har'Eld462b812011-05-24 15:26:10 +03001590 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1591 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1592 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593 }
1594
Nadav Har'Eld462b812011-05-24 15:26:10 +03001595 if (vmx->loaded_vmcs->cpu != cpu) {
Avi Kivityd3591922010-07-26 18:32:39 +03001596 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001597 unsigned long sysenter_esp;
1598
Avi Kivitya8eeb042010-05-10 12:34:53 +03001599 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001600 local_irq_disable();
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001601 crash_disable_local_vmclear(cpu);
Xiao Guangrong5a560f82012-11-28 20:54:14 +08001602
1603 /*
1604 * Read loaded_vmcs->cpu should be before fetching
1605 * loaded_vmcs->loaded_vmcss_on_cpu_link.
1606 * See the comments in __loaded_vmcs_clear().
1607 */
1608 smp_rmb();
1609
Nadav Har'Eld462b812011-05-24 15:26:10 +03001610 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1611 &per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08001612 crash_enable_local_vmclear(cpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001613 local_irq_enable();
1614
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615 /*
1616 * Linux uses per-cpu TSS and GDT, so set these when switching
1617 * processors.
1618 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001619 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03001620 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621
1622 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1623 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Nadav Har'Eld462b812011-05-24 15:26:10 +03001624 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001625 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626}
1627
1628static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1629{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001630 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001631 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001632 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1633 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001634 kvm_cpu_vmxoff();
1635 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636}
1637
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001638static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1639{
Avi Kivity81231c62010-01-24 16:26:40 +02001640 ulong cr0;
1641
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001642 if (vcpu->fpu_active)
1643 return;
1644 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001645 cr0 = vmcs_readl(GUEST_CR0);
1646 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1647 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1648 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001649 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001650 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001651 if (is_guest_mode(vcpu))
1652 vcpu->arch.cr0_guest_owned_bits &=
1653 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02001654 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001655}
1656
Avi Kivityedcafe32009-12-30 18:07:40 +02001657static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1658
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001659/*
1660 * Return the cr0 value that a nested guest would read. This is a combination
1661 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1662 * its hypervisor (cr0_read_shadow).
1663 */
1664static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
1665{
1666 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
1667 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
1668}
1669static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
1670{
1671 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
1672 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
1673}
1674
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001675static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1676{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001677 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
1678 * set this *before* calling this function.
1679 */
Avi Kivityedcafe32009-12-30 18:07:40 +02001680 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001681 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001682 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001683 vcpu->arch.cr0_guest_owned_bits = 0;
1684 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001685 if (is_guest_mode(vcpu)) {
1686 /*
1687 * L1's specified read shadow might not contain the TS bit,
1688 * so now that we turned on shadowing of this bit, we need to
1689 * set this bit of the shadow. Like in nested_vmx_run we need
1690 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
1691 * up-to-date here because we just decached cr0.TS (and we'll
1692 * only update vmcs12->guest_cr0 on nested exit).
1693 */
1694 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1695 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
1696 (vcpu->arch.cr0 & X86_CR0_TS);
1697 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
1698 } else
1699 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001700}
1701
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1703{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001704 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001705
Avi Kivity6de12732011-03-07 12:51:22 +02001706 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1707 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1708 rflags = vmcs_readl(GUEST_RFLAGS);
1709 if (to_vmx(vcpu)->rmode.vm86_active) {
1710 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1711 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1712 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1713 }
1714 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001715 }
Avi Kivity6de12732011-03-07 12:51:22 +02001716 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001717}
1718
1719static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1720{
Avi Kivity6de12732011-03-07 12:51:22 +02001721 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1722 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001723 if (to_vmx(vcpu)->rmode.vm86_active) {
1724 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001725 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001726 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001727 vmcs_writel(GUEST_RFLAGS, rflags);
1728}
1729
Glauber Costa2809f5d2009-05-12 16:21:05 -04001730static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1731{
1732 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1733 int ret = 0;
1734
1735 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001736 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001737 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001738 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001739
1740 return ret & mask;
1741}
1742
1743static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1744{
1745 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1746 u32 interruptibility = interruptibility_old;
1747
1748 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1749
Jan Kiszka48005f62010-02-19 19:38:07 +01001750 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001751 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001752 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001753 interruptibility |= GUEST_INTR_STATE_STI;
1754
1755 if ((interruptibility != interruptibility_old))
1756 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1757}
1758
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1760{
1761 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001762
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001763 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001764 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001765 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001766
Glauber Costa2809f5d2009-05-12 16:21:05 -04001767 /* skipping an emulated instruction also counts */
1768 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001769}
1770
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001771/*
1772 * KVM wants to inject page-faults which it got to the guest. This function
1773 * checks whether in a nested guest, we need to inject them to L1 or L2.
1774 * This function assumes it is called with the exit reason in vmcs02 being
1775 * a #PF exception (this is the only case in which KVM injects a #PF when L2
1776 * is running).
1777 */
1778static int nested_pf_handled(struct kvm_vcpu *vcpu)
1779{
1780 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1781
1782 /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
Nadav Har'El95871902012-03-06 16:39:22 +02001783 if (!(vmcs12->exception_bitmap & (1u << PF_VECTOR)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001784 return 0;
1785
1786 nested_vmx_vmexit(vcpu);
1787 return 1;
1788}
1789
Avi Kivity298101d2007-11-25 13:41:11 +02001790static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02001791 bool has_error_code, u32 error_code,
1792 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02001793{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001794 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001795 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001796
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001797 if (nr == PF_VECTOR && is_guest_mode(vcpu) &&
1798 nested_pf_handled(vcpu))
1799 return;
1800
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001801 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001802 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001803 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1804 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001805
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001806 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001807 int inc_eip = 0;
1808 if (kvm_exception_is_soft(nr))
1809 inc_eip = vcpu->arch.event_exit_inst_len;
1810 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02001811 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001812 return;
1813 }
1814
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001815 if (kvm_exception_is_soft(nr)) {
1816 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1817 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001818 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1819 } else
1820 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1821
1822 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02001823}
1824
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001825static bool vmx_rdtscp_supported(void)
1826{
1827 return cpu_has_vmx_rdtscp();
1828}
1829
Mao, Junjiead756a12012-07-02 01:18:48 +00001830static bool vmx_invpcid_supported(void)
1831{
1832 return cpu_has_vmx_invpcid() && enable_ept;
1833}
1834
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835/*
Eddie Donga75beee2007-05-17 18:55:15 +03001836 * Swap MSR entry in host/guest MSR entry array.
1837 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001838static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001839{
Avi Kivity26bb0982009-09-07 11:14:12 +03001840 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001841
1842 tmp = vmx->guest_msrs[to];
1843 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1844 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001845}
1846
Yang Zhang8d146952013-01-25 10:18:50 +08001847static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu)
1848{
1849 unsigned long *msr_bitmap;
1850
1851 if (irqchip_in_kernel(vcpu->kvm) && apic_x2apic_mode(vcpu->arch.apic)) {
1852 if (is_long_mode(vcpu))
1853 msr_bitmap = vmx_msr_bitmap_longmode_x2apic;
1854 else
1855 msr_bitmap = vmx_msr_bitmap_legacy_x2apic;
1856 } else {
1857 if (is_long_mode(vcpu))
1858 msr_bitmap = vmx_msr_bitmap_longmode;
1859 else
1860 msr_bitmap = vmx_msr_bitmap_legacy;
1861 }
1862
1863 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1864}
1865
Eddie Donga75beee2007-05-17 18:55:15 +03001866/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001867 * Set up the vmcs to automatically save and restore system
1868 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1869 * mode, as fiddling with msrs is very expensive.
1870 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001871static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001872{
Avi Kivity26bb0982009-09-07 11:14:12 +03001873 int save_nmsrs, index;
Avi Kivitye38aea32007-04-19 13:22:48 +03001874
Eddie Donga75beee2007-05-17 18:55:15 +03001875 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001876#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001877 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10001878 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03001879 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001880 move_msr_up(vmx, index, save_nmsrs++);
1881 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001882 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001883 move_msr_up(vmx, index, save_nmsrs++);
1884 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001885 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001886 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001887 index = __find_msr_index(vmx, MSR_TSC_AUX);
1888 if (index >= 0 && vmx->rdtscp_enabled)
1889 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03001890 /*
Brian Gerst8c065852010-07-17 09:03:26 -04001891 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03001892 * if efer.sce is enabled.
1893 */
Brian Gerst8c065852010-07-17 09:03:26 -04001894 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02001895 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10001896 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001897 }
Eddie Donga75beee2007-05-17 18:55:15 +03001898#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001899 index = __find_msr_index(vmx, MSR_EFER);
1900 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001901 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001902
Avi Kivity26bb0982009-09-07 11:14:12 +03001903 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02001904
Yang Zhang8d146952013-01-25 10:18:50 +08001905 if (cpu_has_vmx_msr_bitmap())
1906 vmx_set_msr_bitmap(&vmx->vcpu);
Avi Kivitye38aea32007-04-19 13:22:48 +03001907}
1908
1909/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001910 * reads and returns guest's timestamp counter "register"
1911 * guest_tsc = host_tsc + tsc_offset -- 21.3
1912 */
1913static u64 guest_read_tsc(void)
1914{
1915 u64 host_tsc, tsc_offset;
1916
1917 rdtscll(host_tsc);
1918 tsc_offset = vmcs_read64(TSC_OFFSET);
1919 return host_tsc + tsc_offset;
1920}
1921
1922/*
Nadav Har'Eld5c17852011-08-02 15:54:20 +03001923 * Like guest_read_tsc, but always returns L1's notion of the timestamp
1924 * counter, even if a nested guest (L2) is currently running.
1925 */
Marcelo Tosatti886b4702012-11-27 23:28:58 -02001926u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Nadav Har'Eld5c17852011-08-02 15:54:20 +03001927{
Marcelo Tosatti886b4702012-11-27 23:28:58 -02001928 u64 tsc_offset;
Nadav Har'Eld5c17852011-08-02 15:54:20 +03001929
Nadav Har'Eld5c17852011-08-02 15:54:20 +03001930 tsc_offset = is_guest_mode(vcpu) ?
1931 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
1932 vmcs_read64(TSC_OFFSET);
1933 return host_tsc + tsc_offset;
1934}
1935
1936/*
Zachary Amsdencc578282012-02-03 15:43:50 -02001937 * Engage any workarounds for mis-matched TSC rates. Currently limited to
1938 * software catchup for faster rates on slower CPUs.
Joerg Roedel4051b182011-03-25 09:44:49 +01001939 */
Zachary Amsdencc578282012-02-03 15:43:50 -02001940static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +01001941{
Zachary Amsdencc578282012-02-03 15:43:50 -02001942 if (!scale)
1943 return;
1944
1945 if (user_tsc_khz > tsc_khz) {
1946 vcpu->arch.tsc_catchup = 1;
1947 vcpu->arch.tsc_always_catchup = 1;
1948 } else
1949 WARN(1, "user requested TSC rate below hardware speed\n");
Joerg Roedel4051b182011-03-25 09:44:49 +01001950}
1951
Will Auldba904632012-11-29 12:42:50 -08001952static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu)
1953{
1954 return vmcs_read64(TSC_OFFSET);
1955}
1956
Joerg Roedel4051b182011-03-25 09:44:49 +01001957/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10001958 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08001959 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10001960static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03001962 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03001963 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03001964 * We're here if L1 chose not to trap WRMSR to TSC. According
1965 * to the spec, this should set L1's TSC; The offset that L1
1966 * set for L2 remains unchanged, and still needs to be added
1967 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03001968 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03001969 struct vmcs12 *vmcs12;
1970 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
1971 /* recalculate vmcs02.TSC_OFFSET: */
1972 vmcs12 = get_vmcs12(vcpu);
1973 vmcs_write64(TSC_OFFSET, offset +
1974 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
1975 vmcs12->tsc_offset : 0));
1976 } else {
1977 vmcs_write64(TSC_OFFSET, offset);
1978 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001979}
1980
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001981static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10001982{
1983 u64 offset = vmcs_read64(TSC_OFFSET);
1984 vmcs_write64(TSC_OFFSET, offset + adjustment);
Nadav Har'El79918252011-05-25 23:15:39 +03001985 if (is_guest_mode(vcpu)) {
1986 /* Even when running L2, the adjustment needs to apply to L1 */
1987 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
1988 }
Zachary Amsdene48672f2010-08-19 22:07:23 -10001989}
1990
Joerg Roedel857e4092011-03-25 09:44:50 +01001991static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1992{
1993 return target_tsc - native_read_tsc();
1994}
1995
Nadav Har'El801d3422011-05-25 23:02:23 +03001996static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
1997{
1998 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
1999 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
2000}
2001
2002/*
2003 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2004 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2005 * all guests if the "nested" module option is off, and can also be disabled
2006 * for a single guest by disabling its VMX cpuid bit.
2007 */
2008static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2009{
2010 return nested && guest_cpuid_has_vmx(vcpu);
2011}
2012
Avi Kivity6aa8b732006-12-10 02:21:36 -08002013/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002014 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
2015 * returned for the various VMX controls MSRs when nested VMX is enabled.
2016 * The same values should also be used to verify that vmcs12 control fields are
2017 * valid during nested entry from L1 to L2.
2018 * Each of these control msrs has a low and high 32-bit half: A low bit is on
2019 * if the corresponding bit in the (32-bit) control field *must* be on, and a
2020 * bit in the high half is on if the corresponding bit in the control field
2021 * may be on. See also vmx_control_verify().
2022 * TODO: allow these variables to be modified (downgraded) by module options
2023 * or other means.
2024 */
2025static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
2026static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
2027static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
2028static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
2029static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
2030static __init void nested_vmx_setup_ctls_msrs(void)
2031{
2032 /*
2033 * Note that as a general rule, the high half of the MSRs (bits in
2034 * the control fields which may be 1) should be initialized by the
2035 * intersection of the underlying hardware's MSR (i.e., features which
2036 * can be supported) and the list of features we want to expose -
2037 * because they are known to be properly supported in our code.
2038 * Also, usually, the low half of the MSRs (bits which must be 1) can
2039 * be set to 0, meaning that L1 may turn off any of these bits. The
2040 * reason is that if one of these bits is necessary, it will appear
2041 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
2042 * fields of vmcs01 and vmcs02, will turn these bits off - and
2043 * nested_vmx_exit_handled() will not pass related exits to L1.
2044 * These rules have exceptions below.
2045 */
2046
2047 /* pin-based controls */
2048 /*
2049 * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
2050 * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
2051 */
2052 nested_vmx_pinbased_ctls_low = 0x16 ;
2053 nested_vmx_pinbased_ctls_high = 0x16 |
2054 PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING |
2055 PIN_BASED_VIRTUAL_NMIS;
2056
2057 /* exit controls */
2058 nested_vmx_exit_ctls_low = 0;
Nadav Har'Elb6f12502011-05-25 23:13:06 +03002059 /* Note that guest use of VM_EXIT_ACK_INTR_ON_EXIT is not supported. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002060#ifdef CONFIG_X86_64
2061 nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE;
2062#else
2063 nested_vmx_exit_ctls_high = 0;
2064#endif
2065
2066 /* entry controls */
2067 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
2068 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
2069 nested_vmx_entry_ctls_low = 0;
2070 nested_vmx_entry_ctls_high &=
2071 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE;
2072
2073 /* cpu-based controls */
2074 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
2075 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
2076 nested_vmx_procbased_ctls_low = 0;
2077 nested_vmx_procbased_ctls_high &=
2078 CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING |
2079 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
2080 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
2081 CPU_BASED_CR3_STORE_EXITING |
2082#ifdef CONFIG_X86_64
2083 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
2084#endif
2085 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
2086 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
Avi Kivitydbcb4e72012-08-13 15:38:22 +03002087 CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002088 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
2089 /*
2090 * We can allow some features even when not supported by the
2091 * hardware. For example, L1 can specify an MSR bitmap - and we
2092 * can use it to avoid exits to L1 - even when L0 runs L2
2093 * without MSR bitmaps.
2094 */
2095 nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
2096
2097 /* secondary cpu-based controls */
2098 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
2099 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
2100 nested_vmx_secondary_ctls_low = 0;
2101 nested_vmx_secondary_ctls_high &=
2102 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
2103}
2104
2105static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2106{
2107 /*
2108 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2109 */
2110 return ((control & high) | low) == control;
2111}
2112
2113static inline u64 vmx_control_msr(u32 low, u32 high)
2114{
2115 return low | ((u64)high << 32);
2116}
2117
2118/*
2119 * If we allow our guest to use VMX instructions (i.e., nested VMX), we should
2120 * also let it use VMX-specific MSRs.
2121 * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a
2122 * VMX-specific MSR, or 0 when we haven't (and the caller should handle it
2123 * like all other MSRs).
2124 */
2125static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2126{
2127 if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC &&
2128 msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) {
2129 /*
2130 * According to the spec, processors which do not support VMX
2131 * should throw a #GP(0) when VMX capability MSRs are read.
2132 */
2133 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
2134 return 1;
2135 }
2136
2137 switch (msr_index) {
2138 case MSR_IA32_FEATURE_CONTROL:
2139 *pdata = 0;
2140 break;
2141 case MSR_IA32_VMX_BASIC:
2142 /*
2143 * This MSR reports some information about VMX support. We
2144 * should return information about the VMX we emulate for the
2145 * guest, and the VMCS structure we give it - not about the
2146 * VMX support of the underlying hardware.
2147 */
2148 *pdata = VMCS12_REVISION |
2149 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2150 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2151 break;
2152 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2153 case MSR_IA32_VMX_PINBASED_CTLS:
2154 *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
2155 nested_vmx_pinbased_ctls_high);
2156 break;
2157 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2158 case MSR_IA32_VMX_PROCBASED_CTLS:
2159 *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
2160 nested_vmx_procbased_ctls_high);
2161 break;
2162 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2163 case MSR_IA32_VMX_EXIT_CTLS:
2164 *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
2165 nested_vmx_exit_ctls_high);
2166 break;
2167 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2168 case MSR_IA32_VMX_ENTRY_CTLS:
2169 *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
2170 nested_vmx_entry_ctls_high);
2171 break;
2172 case MSR_IA32_VMX_MISC:
2173 *pdata = 0;
2174 break;
2175 /*
2176 * These MSRs specify bits which the guest must keep fixed (on or off)
2177 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2178 * We picked the standard core2 setting.
2179 */
2180#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2181#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2182 case MSR_IA32_VMX_CR0_FIXED0:
2183 *pdata = VMXON_CR0_ALWAYSON;
2184 break;
2185 case MSR_IA32_VMX_CR0_FIXED1:
2186 *pdata = -1ULL;
2187 break;
2188 case MSR_IA32_VMX_CR4_FIXED0:
2189 *pdata = VMXON_CR4_ALWAYSON;
2190 break;
2191 case MSR_IA32_VMX_CR4_FIXED1:
2192 *pdata = -1ULL;
2193 break;
2194 case MSR_IA32_VMX_VMCS_ENUM:
2195 *pdata = 0x1f;
2196 break;
2197 case MSR_IA32_VMX_PROCBASED_CTLS2:
2198 *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
2199 nested_vmx_secondary_ctls_high);
2200 break;
2201 case MSR_IA32_VMX_EPT_VPID_CAP:
2202 /* Currently, no nested ept or nested vpid */
2203 *pdata = 0;
2204 break;
2205 default:
2206 return 0;
2207 }
2208
2209 return 1;
2210}
2211
2212static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
2213{
2214 if (!nested_vmx_allowed(vcpu))
2215 return 0;
2216
2217 if (msr_index == MSR_IA32_FEATURE_CONTROL)
2218 /* TODO: the right thing. */
2219 return 1;
2220 /*
2221 * No need to treat VMX capability MSRs specially: If we don't handle
2222 * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
2223 */
2224 return 0;
2225}
2226
2227/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002228 * Reads an msr value (of 'msr_index') into 'pdata'.
2229 * Returns 0 on success, non-0 otherwise.
2230 * Assumes vcpu_load() was already called.
2231 */
2232static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2233{
2234 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03002235 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002236
2237 if (!pdata) {
2238 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
2239 return -EINVAL;
2240 }
2241
2242 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002243#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 case MSR_FS_BASE:
2245 data = vmcs_readl(GUEST_FS_BASE);
2246 break;
2247 case MSR_GS_BASE:
2248 data = vmcs_readl(GUEST_GS_BASE);
2249 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002250 case MSR_KERNEL_GS_BASE:
2251 vmx_load_host_state(to_vmx(vcpu));
2252 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
2253 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002254#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002255 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002256 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302257 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002258 data = guest_read_tsc();
2259 break;
2260 case MSR_IA32_SYSENTER_CS:
2261 data = vmcs_read32(GUEST_SYSENTER_CS);
2262 break;
2263 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002264 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265 break;
2266 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002267 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002268 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002269 case MSR_TSC_AUX:
2270 if (!to_vmx(vcpu)->rdtscp_enabled)
2271 return 1;
2272 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002274 if (vmx_get_vmx_msr(vcpu, msr_index, pdata))
2275 return 0;
Rusty Russell8b9cf982007-07-30 16:31:43 +10002276 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002277 if (msr) {
2278 data = msr->data;
2279 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002281 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282 }
2283
2284 *pdata = data;
2285 return 0;
2286}
2287
2288/*
2289 * Writes msr value into into the appropriate "register".
2290 * Returns 0 on success, non-0 otherwise.
2291 * Assumes vcpu_load() was already called.
2292 */
Will Auld8fe8ab42012-11-29 12:42:12 -08002293static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002295 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002296 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002297 int ret = 0;
Will Auld8fe8ab42012-11-29 12:42:12 -08002298 u32 msr_index = msr_info->index;
2299 u64 data = msr_info->data;
Eddie Dong2cc51562007-05-21 07:28:09 +03002300
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002302 case MSR_EFER:
Will Auld8fe8ab42012-11-29 12:42:12 -08002303 ret = kvm_set_msr_common(vcpu, msr_info);
Eddie Dong2cc51562007-05-21 07:28:09 +03002304 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002305#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002307 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308 vmcs_writel(GUEST_FS_BASE, data);
2309 break;
2310 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002311 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312 vmcs_writel(GUEST_GS_BASE, data);
2313 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002314 case MSR_KERNEL_GS_BASE:
2315 vmx_load_host_state(vmx);
2316 vmx->msr_guest_kernel_gs_base = data;
2317 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318#endif
2319 case MSR_IA32_SYSENTER_CS:
2320 vmcs_write32(GUEST_SYSENTER_CS, data);
2321 break;
2322 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002323 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324 break;
2325 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002326 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302328 case MSR_IA32_TSC:
Will Auld8fe8ab42012-11-29 12:42:12 -08002329 kvm_write_tsc(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002331 case MSR_IA32_CR_PAT:
2332 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2333 vmcs_write64(GUEST_IA32_PAT, data);
2334 vcpu->arch.pat = data;
2335 break;
2336 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002337 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002338 break;
Will Auldba904632012-11-29 12:42:50 -08002339 case MSR_IA32_TSC_ADJUST:
2340 ret = kvm_set_msr_common(vcpu, msr_info);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002341 break;
2342 case MSR_TSC_AUX:
2343 if (!vmx->rdtscp_enabled)
2344 return 1;
2345 /* Check reserved bit, higher 32 bits should be zero */
2346 if ((data >> 32) != 0)
2347 return 1;
2348 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002350 if (vmx_set_vmx_msr(vcpu, msr_index, data))
2351 break;
Rusty Russell8b9cf982007-07-30 16:31:43 +10002352 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002353 if (msr) {
2354 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002355 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2356 preempt_disable();
Avi Kivity9ee73972012-03-06 14:16:33 +02002357 kvm_set_shared_msr(msr->index, msr->data,
2358 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03002359 preempt_enable();
2360 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002361 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362 }
Will Auld8fe8ab42012-11-29 12:42:12 -08002363 ret = kvm_set_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364 }
2365
Eddie Dong2cc51562007-05-21 07:28:09 +03002366 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002367}
2368
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002369static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002371 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2372 switch (reg) {
2373 case VCPU_REGS_RSP:
2374 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2375 break;
2376 case VCPU_REGS_RIP:
2377 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2378 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002379 case VCPU_EXREG_PDPTR:
2380 if (enable_ept)
2381 ept_save_pdptrs(vcpu);
2382 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002383 default:
2384 break;
2385 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386}
2387
Avi Kivity6aa8b732006-12-10 02:21:36 -08002388static __init int cpu_has_kvm_support(void)
2389{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002390 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391}
2392
2393static __init int vmx_disabled_by_bios(void)
2394{
2395 u64 msr;
2396
2397 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04002398 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08002399 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04002400 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2401 && tboot_enabled())
2402 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08002403 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04002404 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08002405 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08002406 && !tboot_enabled()) {
2407 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08002408 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04002409 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08002410 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08002411 /* launched w/o TXT and VMX disabled */
2412 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2413 && !tboot_enabled())
2414 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04002415 }
2416
2417 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002418}
2419
Dongxiao Xu7725b892010-05-11 18:29:38 +08002420static void kvm_cpu_vmxon(u64 addr)
2421{
2422 asm volatile (ASM_VMX_VMXON_RAX
2423 : : "a"(&addr), "m"(addr)
2424 : "memory", "cc");
2425}
2426
Alexander Graf10474ae2009-09-15 11:37:46 +02002427static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428{
2429 int cpu = raw_smp_processor_id();
2430 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04002431 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432
Alexander Graf10474ae2009-09-15 11:37:46 +02002433 if (read_cr4() & X86_CR4_VMXE)
2434 return -EBUSY;
2435
Nadav Har'Eld462b812011-05-24 15:26:10 +03002436 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Zhang Yanfei8f536b72012-12-06 23:43:34 +08002437
2438 /*
2439 * Now we can enable the vmclear operation in kdump
2440 * since the loaded_vmcss_on_cpu list on this cpu
2441 * has been initialized.
2442 *
2443 * Though the cpu is not in VMX operation now, there
2444 * is no problem to enable the vmclear operation
2445 * for the loaded_vmcss_on_cpu list is empty!
2446 */
2447 crash_enable_local_vmclear(cpu);
2448
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04002450
2451 test_bits = FEATURE_CONTROL_LOCKED;
2452 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2453 if (tboot_enabled())
2454 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2455
2456 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002457 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04002458 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2459 }
Rusty Russell66aee912007-07-17 23:34:16 +10002460 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02002461
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002462 if (vmm_exclusive) {
2463 kvm_cpu_vmxon(phys_addr);
2464 ept_sync_global();
2465 }
Alexander Graf10474ae2009-09-15 11:37:46 +02002466
Avi Kivity3444d7d2010-07-26 18:32:38 +03002467 store_gdt(&__get_cpu_var(host_gdt));
2468
Alexander Graf10474ae2009-09-15 11:37:46 +02002469 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002470}
2471
Nadav Har'Eld462b812011-05-24 15:26:10 +03002472static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002473{
2474 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002475 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002476
Nadav Har'Eld462b812011-05-24 15:26:10 +03002477 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2478 loaded_vmcss_on_cpu_link)
2479 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002480}
2481
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002482
2483/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2484 * tricks.
2485 */
2486static void kvm_cpu_vmxoff(void)
2487{
2488 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002489}
2490
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491static void hardware_disable(void *garbage)
2492{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002493 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002494 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002495 kvm_cpu_vmxoff();
2496 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08002497 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002498}
2499
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002500static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002501 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002502{
2503 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002504 u32 ctl = ctl_min | ctl_opt;
2505
2506 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2507
2508 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2509 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2510
2511 /* Ensure minimum (required) set of control bits are supported. */
2512 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002513 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002514
2515 *result = ctl;
2516 return 0;
2517}
2518
Avi Kivity110312c2010-12-21 12:54:20 +02002519static __init bool allow_1_setting(u32 msr, u32 ctl)
2520{
2521 u32 vmx_msr_low, vmx_msr_high;
2522
2523 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2524 return vmx_msr_high & ctl;
2525}
2526
Yang, Sheng002c7f72007-07-31 14:23:01 +03002527static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002528{
2529 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002530 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002531 u32 _pin_based_exec_control = 0;
2532 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002533 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002534 u32 _vmexit_control = 0;
2535 u32 _vmentry_control = 0;
2536
2537 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08002538 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002539 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2540 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002541 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002542
Raghavendra K T10166742012-02-07 23:19:20 +05302543 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002544#ifdef CONFIG_X86_64
2545 CPU_BASED_CR8_LOAD_EXITING |
2546 CPU_BASED_CR8_STORE_EXITING |
2547#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002548 CPU_BASED_CR3_LOAD_EXITING |
2549 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002550 CPU_BASED_USE_IO_BITMAPS |
2551 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03002552 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08002553 CPU_BASED_MWAIT_EXITING |
2554 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002555 CPU_BASED_INVLPG_EXITING |
2556 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002557
Sheng Yangf78e0e22007-10-29 09:40:42 +08002558 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002559 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002560 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002561 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2562 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002563 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002564#ifdef CONFIG_X86_64
2565 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2566 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2567 ~CPU_BASED_CR8_STORE_EXITING;
2568#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002569 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002570 min2 = 0;
2571 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Yang Zhang8d146952013-01-25 10:18:50 +08002572 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002573 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002574 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002575 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002576 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002577 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
Mao, Junjiead756a12012-07-02 01:18:48 +00002578 SECONDARY_EXEC_RDTSCP |
Yang Zhang83d4c282013-01-25 10:18:49 +08002579 SECONDARY_EXEC_ENABLE_INVPCID |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002580 SECONDARY_EXEC_APIC_REGISTER_VIRT |
2581 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
Sheng Yangd56f5462008-04-25 10:13:16 +08002582 if (adjust_vmx_controls(min2, opt2,
2583 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002584 &_cpu_based_2nd_exec_control) < 0)
2585 return -EIO;
2586 }
2587#ifndef CONFIG_X86_64
2588 if (!(_cpu_based_2nd_exec_control &
2589 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2590 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2591#endif
Yang Zhang83d4c282013-01-25 10:18:49 +08002592
2593 if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2594 _cpu_based_2nd_exec_control &= ~(
Yang Zhang8d146952013-01-25 10:18:50 +08002595 SECONDARY_EXEC_APIC_REGISTER_VIRT |
Yang Zhangc7c9c562013-01-25 10:18:51 +08002596 SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
2597 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang83d4c282013-01-25 10:18:49 +08002598
Sheng Yangd56f5462008-04-25 10:13:16 +08002599 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002600 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2601 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002602 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2603 CPU_BASED_CR3_STORE_EXITING |
2604 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08002605 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
2606 vmx_capability.ept, vmx_capability.vpid);
2607 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002608
2609 min = 0;
2610#ifdef CONFIG_X86_64
2611 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2612#endif
Sheng Yang468d4722008-10-09 16:01:55 +08002613 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002614 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2615 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002616 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002617
Sheng Yang468d4722008-10-09 16:01:55 +08002618 min = 0;
2619 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002620 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2621 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002622 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002624 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002625
2626 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2627 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002628 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002629
2630#ifdef CONFIG_X86_64
2631 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2632 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002633 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002634#endif
2635
2636 /* Require Write-Back (WB) memory type for VMCS accesses. */
2637 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002638 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002639
Yang, Sheng002c7f72007-07-31 14:23:01 +03002640 vmcs_conf->size = vmx_msr_high & 0x1fff;
2641 vmcs_conf->order = get_order(vmcs_config.size);
2642 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002643
Yang, Sheng002c7f72007-07-31 14:23:01 +03002644 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2645 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002646 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002647 vmcs_conf->vmexit_ctrl = _vmexit_control;
2648 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002649
Avi Kivity110312c2010-12-21 12:54:20 +02002650 cpu_has_load_ia32_efer =
2651 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2652 VM_ENTRY_LOAD_IA32_EFER)
2653 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2654 VM_EXIT_LOAD_IA32_EFER);
2655
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002656 cpu_has_load_perf_global_ctrl =
2657 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2658 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
2659 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2660 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2661
2662 /*
2663 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
2664 * but due to arrata below it can't be used. Workaround is to use
2665 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2666 *
2667 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
2668 *
2669 * AAK155 (model 26)
2670 * AAP115 (model 30)
2671 * AAT100 (model 37)
2672 * BC86,AAY89,BD102 (model 44)
2673 * BA97 (model 46)
2674 *
2675 */
2676 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
2677 switch (boot_cpu_data.x86_model) {
2678 case 26:
2679 case 30:
2680 case 37:
2681 case 44:
2682 case 46:
2683 cpu_has_load_perf_global_ctrl = false;
2684 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2685 "does not work properly. Using workaround\n");
2686 break;
2687 default:
2688 break;
2689 }
2690 }
2691
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002692 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002693}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002694
2695static struct vmcs *alloc_vmcs_cpu(int cpu)
2696{
2697 int node = cpu_to_node(cpu);
2698 struct page *pages;
2699 struct vmcs *vmcs;
2700
Mel Gorman6484eb32009-06-16 15:31:54 -07002701 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 if (!pages)
2703 return NULL;
2704 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002705 memset(vmcs, 0, vmcs_config.size);
2706 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 return vmcs;
2708}
2709
2710static struct vmcs *alloc_vmcs(void)
2711{
Ingo Molnard3b2c332007-01-05 16:36:23 -08002712 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713}
2714
2715static void free_vmcs(struct vmcs *vmcs)
2716{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002717 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002718}
2719
Nadav Har'Eld462b812011-05-24 15:26:10 +03002720/*
2721 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2722 */
2723static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
2724{
2725 if (!loaded_vmcs->vmcs)
2726 return;
2727 loaded_vmcs_clear(loaded_vmcs);
2728 free_vmcs(loaded_vmcs->vmcs);
2729 loaded_vmcs->vmcs = NULL;
2730}
2731
Sam Ravnborg39959582007-06-01 00:47:13 -07002732static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733{
2734 int cpu;
2735
Zachary Amsden3230bb42009-09-29 11:38:37 -10002736 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002738 per_cpu(vmxarea, cpu) = NULL;
2739 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740}
2741
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742static __init int alloc_kvm_area(void)
2743{
2744 int cpu;
2745
Zachary Amsden3230bb42009-09-29 11:38:37 -10002746 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747 struct vmcs *vmcs;
2748
2749 vmcs = alloc_vmcs_cpu(cpu);
2750 if (!vmcs) {
2751 free_kvm_area();
2752 return -ENOMEM;
2753 }
2754
2755 per_cpu(vmxarea, cpu) = vmcs;
2756 }
2757 return 0;
2758}
2759
2760static __init int hardware_setup(void)
2761{
Yang, Sheng002c7f72007-07-31 14:23:01 +03002762 if (setup_vmcs_config(&vmcs_config) < 0)
2763 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01002764
2765 if (boot_cpu_has(X86_FEATURE_NX))
2766 kvm_enable_efer_bits(EFER_NX);
2767
Sheng Yang93ba03c2009-04-01 15:52:32 +08002768 if (!cpu_has_vmx_vpid())
2769 enable_vpid = 0;
2770
Sheng Yang4bc9b982010-06-02 14:05:24 +08002771 if (!cpu_has_vmx_ept() ||
2772 !cpu_has_vmx_ept_4levels()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08002773 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002774 enable_unrestricted_guest = 0;
Xudong Hao83c3a332012-05-28 19:33:35 +08002775 enable_ept_ad_bits = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002776 }
2777
Xudong Hao83c3a332012-05-28 19:33:35 +08002778 if (!cpu_has_vmx_ept_ad_bits())
2779 enable_ept_ad_bits = 0;
2780
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002781 if (!cpu_has_vmx_unrestricted_guest())
2782 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08002783
2784 if (!cpu_has_vmx_flexpriority())
2785 flexpriority_enabled = 0;
2786
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002787 if (!cpu_has_vmx_tpr_shadow())
2788 kvm_x86_ops->update_cr8_intercept = NULL;
2789
Marcelo Tosatti54dee992009-06-11 12:07:44 -03002790 if (enable_ept && !cpu_has_vmx_ept_2m_page())
2791 kvm_disable_largepages();
2792
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002793 if (!cpu_has_vmx_ple())
2794 ple_gap = 0;
2795
Yang Zhangc7c9c562013-01-25 10:18:51 +08002796 if (!cpu_has_vmx_apic_register_virt() ||
2797 !cpu_has_vmx_virtual_intr_delivery())
2798 enable_apicv_reg_vid = 0;
2799
2800 if (enable_apicv_reg_vid)
2801 kvm_x86_ops->update_cr8_intercept = NULL;
2802 else
2803 kvm_x86_ops->hwapic_irr_update = NULL;
Yang Zhang83d4c282013-01-25 10:18:49 +08002804
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002805 if (nested)
2806 nested_vmx_setup_ctls_msrs();
2807
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 return alloc_kvm_area();
2809}
2810
2811static __exit void hardware_unsetup(void)
2812{
2813 free_kvm_area();
2814}
2815
Gleb Natapov14168782013-01-21 15:36:49 +02002816static bool emulation_required(struct kvm_vcpu *vcpu)
2817{
2818 return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2819}
2820
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002821static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
Gleb Natapovd99e4152012-12-20 16:57:45 +02002822 struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823{
Gleb Natapovd99e4152012-12-20 16:57:45 +02002824 if (!emulate_invalid_guest_state) {
2825 /*
2826 * CS and SS RPL should be equal during guest entry according
2827 * to VMX spec, but in reality it is not always so. Since vcpu
2828 * is in the middle of the transition from real mode to
2829 * protected mode it is safe to assume that RPL 0 is a good
2830 * default value.
2831 */
2832 if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
2833 save->selector &= ~SELECTOR_RPL_MASK;
2834 save->dpl = save->selector & SELECTOR_RPL_MASK;
2835 save->s = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 }
Gleb Natapovd99e4152012-12-20 16:57:45 +02002837 vmx_set_segment(vcpu, save, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002838}
2839
2840static void enter_pmode(struct kvm_vcpu *vcpu)
2841{
2842 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002843 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844
Gleb Natapovd99e4152012-12-20 16:57:45 +02002845 /*
2846 * Update real mode segment cache. It may be not up-to-date if sement
2847 * register was written while vcpu was in a guest mode.
2848 */
2849 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2850 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2851 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2852 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
2853 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2854 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
2855
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002856 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857
Avi Kivity2fb92db2011-04-27 19:42:18 +03002858 vmx_segment_cache_clear(vmx);
2859
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002860 vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861
2862 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002863 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2864 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865 vmcs_writel(GUEST_RFLAGS, flags);
2866
Rusty Russell66aee912007-07-17 23:34:16 +10002867 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2868 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869
2870 update_exception_bitmap(vcpu);
2871
Gleb Natapov91b0aa22013-01-21 15:36:47 +02002872 fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2873 fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2874 fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2875 fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2876 fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
2877 fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
Gleb Natapov1f3141e2013-01-21 15:36:41 +02002878
2879 /* CPL is always 0 when CPU enters protected mode */
2880 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
2881 vmx->cpl = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882}
2883
Mike Dayd77c26f2007-10-08 09:02:08 -04002884static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002885{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002886 if (!kvm->arch.tss_addr) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02002887 struct kvm_memslots *slots;
Xiao Guangrong28a37542011-11-24 19:04:35 +08002888 struct kvm_memory_slot *slot;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02002889 gfn_t base_gfn;
2890
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002891 slots = kvm_memslots(kvm);
Xiao Guangrong28a37542011-11-24 19:04:35 +08002892 slot = id_to_memslot(slots, 0);
2893 base_gfn = slot->base_gfn + slot->npages - 3;
2894
Izik Eiduscbc94022007-10-25 00:29:55 +02002895 return base_gfn << PAGE_SHIFT;
2896 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002897 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898}
2899
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002900static void fix_rmode_seg(int seg, struct kvm_segment *save)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901{
Mathias Krause772e0312012-08-30 01:30:19 +02002902 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Gleb Natapovd99e4152012-12-20 16:57:45 +02002903 struct kvm_segment var = *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904
Gleb Natapovd99e4152012-12-20 16:57:45 +02002905 var.dpl = 0x3;
2906 if (seg == VCPU_SREG_CS)
2907 var.type = 0x3;
2908
2909 if (!emulate_invalid_guest_state) {
2910 var.selector = var.base >> 4;
2911 var.base = var.base & 0xffff0;
2912 var.limit = 0xffff;
2913 var.g = 0;
2914 var.db = 0;
2915 var.present = 1;
2916 var.s = 1;
2917 var.l = 0;
2918 var.unusable = 0;
2919 var.type = 0x3;
2920 var.avl = 0;
2921 if (save->base & 0xf)
2922 printk_once(KERN_WARNING "kvm: segment base is not "
2923 "paragraph aligned when entering "
2924 "protected mode (seg=%d)", seg);
2925 }
2926
2927 vmcs_write16(sf->selector, var.selector);
2928 vmcs_write32(sf->base, var.base);
2929 vmcs_write32(sf->limit, var.limit);
2930 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002931}
2932
2933static void enter_rmode(struct kvm_vcpu *vcpu)
2934{
2935 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002936 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002937
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002938 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
2939 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
2940 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
2941 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
2942 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
Gleb Natapovc6ad11532012-12-12 19:10:51 +02002943 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
2944 vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03002945
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002946 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002947
Gleb Natapov776e58e2011-03-13 12:34:27 +02002948 /*
2949 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
2950 * vcpu. Call it here with phys address pointing 16M below 4G.
2951 */
2952 if (!vcpu->kvm->arch.tss_addr) {
2953 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2954 "called before entering vcpu\n");
2955 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
2956 vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
2957 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2958 }
2959
Avi Kivity2fb92db2011-04-27 19:42:18 +03002960 vmx_segment_cache_clear(vmx);
2961
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002963 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2965
2966 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002967 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002969 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970
2971 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002972 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973 update_exception_bitmap(vcpu);
2974
Gleb Natapovd99e4152012-12-20 16:57:45 +02002975 fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
2976 fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
2977 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
2978 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
2979 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
2980 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002981
Eddie Dong8668a3c2007-10-10 14:26:45 +08002982 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983}
2984
Amit Shah401d10d2009-02-20 22:53:37 +05302985static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
2986{
2987 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002988 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
2989
2990 if (!msr)
2991 return;
Amit Shah401d10d2009-02-20 22:53:37 +05302992
Avi Kivity44ea2b12009-09-06 15:55:37 +03002993 /*
2994 * Force kernel_gs_base reloading before EFER changes, as control
2995 * of this msr depends on is_long_mode().
2996 */
2997 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02002998 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302999 if (efer & EFER_LMA) {
3000 vmcs_write32(VM_ENTRY_CONTROLS,
3001 vmcs_read32(VM_ENTRY_CONTROLS) |
3002 VM_ENTRY_IA32E_MODE);
3003 msr->data = efer;
3004 } else {
3005 vmcs_write32(VM_ENTRY_CONTROLS,
3006 vmcs_read32(VM_ENTRY_CONTROLS) &
3007 ~VM_ENTRY_IA32E_MODE);
3008
3009 msr->data = efer & ~EFER_LME;
3010 }
3011 setup_msrs(vmx);
3012}
3013
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003014#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003015
3016static void enter_lmode(struct kvm_vcpu *vcpu)
3017{
3018 u32 guest_tr_ar;
3019
Avi Kivity2fb92db2011-04-27 19:42:18 +03003020 vmx_segment_cache_clear(to_vmx(vcpu));
3021
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
3023 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02003024 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3025 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026 vmcs_write32(GUEST_TR_AR_BYTES,
3027 (guest_tr_ar & ~AR_TYPE_MASK)
3028 | AR_TYPE_BUSY_64_TSS);
3029 }
Avi Kivityda38f432010-07-06 11:30:49 +03003030 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031}
3032
3033static void exit_lmode(struct kvm_vcpu *vcpu)
3034{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035 vmcs_write32(VM_ENTRY_CONTROLS,
3036 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03003037 & ~VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03003038 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003039}
3040
3041#endif
3042
Sheng Yang2384d2b2008-01-17 15:14:33 +08003043static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
3044{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08003045 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003046 if (enable_ept) {
3047 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
3048 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08003049 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08003050 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08003051}
3052
Avi Kivitye8467fd2009-12-29 18:43:06 +02003053static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3054{
3055 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3056
3057 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3058 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3059}
3060
Avi Kivityaff48ba2010-12-05 18:56:11 +02003061static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
3062{
3063 if (enable_ept && is_paging(vcpu))
3064 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3065 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
3066}
3067
Anthony Liguori25c4c272007-04-27 09:29:21 +03003068static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08003069{
Avi Kivityfc78f512009-12-07 12:16:48 +02003070 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3071
3072 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3073 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08003074}
3075
Sheng Yang14394422008-04-28 12:24:45 +08003076static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3077{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003078 if (!test_bit(VCPU_EXREG_PDPTR,
3079 (unsigned long *)&vcpu->arch.regs_dirty))
3080 return;
3081
Sheng Yang14394422008-04-28 12:24:45 +08003082 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02003083 vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
3084 vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
3085 vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
3086 vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08003087 }
3088}
3089
Avi Kivity8f5d5492009-05-31 18:41:29 +03003090static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3091{
3092 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02003093 vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3094 vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3095 vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3096 vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003097 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003098
3099 __set_bit(VCPU_EXREG_PDPTR,
3100 (unsigned long *)&vcpu->arch.regs_avail);
3101 __set_bit(VCPU_EXREG_PDPTR,
3102 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03003103}
3104
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003105static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08003106
3107static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3108 unsigned long cr0,
3109 struct kvm_vcpu *vcpu)
3110{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03003111 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3112 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003113 if (!(cr0 & X86_CR0_PG)) {
3114 /* From paging/starting to nonpaging */
3115 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003116 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08003117 (CPU_BASED_CR3_LOAD_EXITING |
3118 CPU_BASED_CR3_STORE_EXITING));
3119 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003120 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003121 } else if (!is_paging(vcpu)) {
3122 /* From nonpaging to paging */
3123 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08003124 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08003125 ~(CPU_BASED_CR3_LOAD_EXITING |
3126 CPU_BASED_CR3_STORE_EXITING));
3127 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02003128 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08003129 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08003130
3131 if (!(cr0 & X86_CR0_WP))
3132 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08003133}
3134
Avi Kivity6aa8b732006-12-10 02:21:36 -08003135static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3136{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003137 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003138 unsigned long hw_cr0;
3139
3140 if (enable_unrestricted_guest)
3141 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
3142 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
Gleb Natapov218e7632013-01-21 15:36:45 +02003143 else {
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003144 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003145
Gleb Natapov218e7632013-01-21 15:36:45 +02003146 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3147 enter_pmode(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003148
Gleb Natapov218e7632013-01-21 15:36:45 +02003149 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3150 enter_rmode(vcpu);
3151 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003153#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003154 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003155 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003157 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158 exit_lmode(vcpu);
3159 }
3160#endif
3161
Avi Kivity089d0342009-03-23 18:26:32 +02003162 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003163 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3164
Avi Kivity02daab22009-12-30 12:40:26 +02003165 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003166 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003167
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003169 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003170 vcpu->arch.cr0 = cr0;
Gleb Natapov14168782013-01-21 15:36:49 +02003171
3172 /* depends on vcpu->arch.cr0 to be set to a new value */
3173 vmx->emulation_required = emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174}
3175
Sheng Yang14394422008-04-28 12:24:45 +08003176static u64 construct_eptp(unsigned long root_hpa)
3177{
3178 u64 eptp;
3179
3180 /* TODO write the value reading from MSR */
3181 eptp = VMX_EPT_DEFAULT_MT |
3182 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
Xudong Haob38f9932012-05-28 19:33:36 +08003183 if (enable_ept_ad_bits)
3184 eptp |= VMX_EPT_AD_ENABLE_BIT;
Sheng Yang14394422008-04-28 12:24:45 +08003185 eptp |= (root_hpa & PAGE_MASK);
3186
3187 return eptp;
3188}
3189
Avi Kivity6aa8b732006-12-10 02:21:36 -08003190static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3191{
Sheng Yang14394422008-04-28 12:24:45 +08003192 unsigned long guest_cr3;
3193 u64 eptp;
3194
3195 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003196 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003197 eptp = construct_eptp(cr3);
3198 vmcs_write64(EPT_POINTER, eptp);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003199 guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
Sheng Yangb927a3c2009-07-21 10:42:48 +08003200 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02003201 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003202 }
3203
Sheng Yang2384d2b2008-01-17 15:14:33 +08003204 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003205 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003206}
3207
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003208static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003209{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003210 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08003211 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
3212
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003213 if (cr4 & X86_CR4_VMXE) {
3214 /*
3215 * To use VMXON (and later other VMX instructions), a guest
3216 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3217 * So basically the check on whether to allow nested VMX
3218 * is here.
3219 */
3220 if (!nested_vmx_allowed(vcpu))
3221 return 1;
3222 } else if (to_vmx(vcpu)->nested.vmxon)
3223 return 1;
3224
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003225 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02003226 if (enable_ept) {
3227 if (!is_paging(vcpu)) {
3228 hw_cr4 &= ~X86_CR4_PAE;
3229 hw_cr4 |= X86_CR4_PSE;
Dongxiao Xuc08800a2013-02-04 11:50:43 +08003230 /*
3231 * SMEP is disabled if CPU is in non-paging mode in
3232 * hardware. However KVM always uses paging mode to
3233 * emulate guest non-paging mode with TDP.
3234 * To emulate this behavior, SMEP needs to be manually
3235 * disabled when guest switches to non-paging mode.
3236 */
3237 hw_cr4 &= ~X86_CR4_SMEP;
Avi Kivitybc230082009-12-08 12:14:42 +02003238 } else if (!(cr4 & X86_CR4_PAE)) {
3239 hw_cr4 &= ~X86_CR4_PAE;
3240 }
3241 }
Sheng Yang14394422008-04-28 12:24:45 +08003242
3243 vmcs_writel(CR4_READ_SHADOW, cr4);
3244 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003245 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246}
3247
Avi Kivity6aa8b732006-12-10 02:21:36 -08003248static void vmx_get_segment(struct kvm_vcpu *vcpu,
3249 struct kvm_segment *var, int seg)
3250{
Avi Kivitya9179492011-01-03 14:28:52 +02003251 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003252 u32 ar;
3253
Gleb Natapovc6ad11532012-12-12 19:10:51 +02003254 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003255 *var = vmx->rmode.segs[seg];
Avi Kivitya9179492011-01-03 14:28:52 +02003256 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003257 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivityf5f7b2f2012-08-21 17:07:00 +03003258 return;
Avi Kivity1390a282012-08-21 17:07:08 +03003259 var->base = vmx_read_guest_seg_base(vmx, seg);
3260 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3261 return;
Avi Kivitya9179492011-01-03 14:28:52 +02003262 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003263 var->base = vmx_read_guest_seg_base(vmx, seg);
3264 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3265 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3266 ar = vmx_read_guest_seg_ar(vmx, seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267 var->type = ar & 15;
3268 var->s = (ar >> 4) & 1;
3269 var->dpl = (ar >> 5) & 3;
3270 var->present = (ar >> 7) & 1;
3271 var->avl = (ar >> 12) & 1;
3272 var->l = (ar >> 13) & 1;
3273 var->db = (ar >> 14) & 1;
3274 var->g = (ar >> 15) & 1;
3275 var->unusable = (ar >> 16) & 1;
3276}
3277
Avi Kivitya9179492011-01-03 14:28:52 +02003278static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3279{
Avi Kivitya9179492011-01-03 14:28:52 +02003280 struct kvm_segment s;
3281
3282 if (to_vmx(vcpu)->rmode.vm86_active) {
3283 vmx_get_segment(vcpu, &s, seg);
3284 return s.base;
3285 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003286 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003287}
3288
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003289static int vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003290{
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003291 struct vcpu_vmx *vmx = to_vmx(vcpu);
3292
Avi Kivity3eeb3282010-01-21 15:31:48 +02003293 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003294 return 0;
3295
Avi Kivityf4c63e52011-03-07 14:54:28 +02003296 if (!is_long_mode(vcpu)
3297 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
Izik Eidus2e4d2652008-03-24 19:38:34 +02003298 return 3;
3299
Avi Kivity69c73022011-03-07 15:26:44 +02003300 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
3301 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Marcelo Tosattib09408d2013-01-07 19:27:06 -02003302 vmx->cpl = vmx_read_guest_seg_selector(vmx, VCPU_SREG_CS) & 3;
Avi Kivity69c73022011-03-07 15:26:44 +02003303 }
Avi Kivityd881e6f2012-06-06 18:36:48 +03003304
3305 return vmx->cpl;
Avi Kivity69c73022011-03-07 15:26:44 +02003306}
3307
3308
Avi Kivity653e3102007-05-07 10:55:37 +03003309static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003311 u32 ar;
3312
Avi Kivityf0495f92012-06-07 17:06:10 +03003313 if (var->unusable || !var->present)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003314 ar = 1 << 16;
3315 else {
3316 ar = var->type & 15;
3317 ar |= (var->s & 1) << 4;
3318 ar |= (var->dpl & 3) << 5;
3319 ar |= (var->present & 1) << 7;
3320 ar |= (var->avl & 1) << 12;
3321 ar |= (var->l & 1) << 13;
3322 ar |= (var->db & 1) << 14;
3323 ar |= (var->g & 1) << 15;
3324 }
Avi Kivity653e3102007-05-07 10:55:37 +03003325
3326 return ar;
3327}
3328
3329static void vmx_set_segment(struct kvm_vcpu *vcpu,
3330 struct kvm_segment *var, int seg)
3331{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003332 struct vcpu_vmx *vmx = to_vmx(vcpu);
Mathias Krause772e0312012-08-30 01:30:19 +02003333 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Avi Kivity653e3102007-05-07 10:55:37 +03003334
Avi Kivity2fb92db2011-04-27 19:42:18 +03003335 vmx_segment_cache_clear(vmx);
Gleb Natapov2f143242013-01-21 15:36:42 +02003336 if (seg == VCPU_SREG_CS)
3337 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity2fb92db2011-04-27 19:42:18 +03003338
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003339 if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3340 vmx->rmode.segs[seg] = *var;
3341 if (seg == VCPU_SREG_TR)
3342 vmcs_write16(sf->selector, var->selector);
3343 else if (var->s)
3344 fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
Gleb Natapovd99e4152012-12-20 16:57:45 +02003345 goto out;
Avi Kivity653e3102007-05-07 10:55:37 +03003346 }
Gleb Natapov1ecd50a2012-12-12 19:10:54 +02003347
Avi Kivity653e3102007-05-07 10:55:37 +03003348 vmcs_writel(sf->base, var->base);
3349 vmcs_write32(sf->limit, var->limit);
3350 vmcs_write16(sf->selector, var->selector);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003351
3352 /*
3353 * Fix the "Accessed" bit in AR field of segment registers for older
3354 * qemu binaries.
3355 * IA32 arch specifies that at the time of processor reset the
3356 * "Accessed" bit in the AR field of segment registers is 1. And qemu
Guo Chao0fa06072012-06-28 15:16:19 +08003357 * is setting it to 0 in the userland code. This causes invalid guest
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003358 * state vmexit when "unrestricted guest" mode is turned on.
3359 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3360 * tree. Newer qemu binaries with that qemu fix would not need this
3361 * kvm hack.
3362 */
3363 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
Gleb Natapovf924d662012-12-12 19:10:55 +02003364 var->type |= 0x1; /* Accessed */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003365
Gleb Natapovf924d662012-12-12 19:10:55 +02003366 vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
Gleb Natapovd99e4152012-12-20 16:57:45 +02003367
3368out:
Gleb Natapov14168782013-01-21 15:36:49 +02003369 vmx->emulation_required |= emulation_required(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003370}
3371
Avi Kivity6aa8b732006-12-10 02:21:36 -08003372static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3373{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003374 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003375
3376 *db = (ar >> 14) & 1;
3377 *l = (ar >> 13) & 1;
3378}
3379
Gleb Natapov89a27f42010-02-16 10:51:48 +02003380static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003381{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003382 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3383 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003384}
3385
Gleb Natapov89a27f42010-02-16 10:51:48 +02003386static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003387{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003388 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3389 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390}
3391
Gleb Natapov89a27f42010-02-16 10:51:48 +02003392static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003393{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003394 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3395 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003396}
3397
Gleb Natapov89a27f42010-02-16 10:51:48 +02003398static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003399{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003400 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3401 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003402}
3403
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003404static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3405{
3406 struct kvm_segment var;
3407 u32 ar;
3408
3409 vmx_get_segment(vcpu, &var, seg);
Gleb Natapov07f42f52012-12-12 19:10:49 +02003410 var.dpl = 0x3;
Gleb Natapov0647f4a2012-12-12 19:10:50 +02003411 if (seg == VCPU_SREG_CS)
3412 var.type = 0x3;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003413 ar = vmx_segment_access_rights(&var);
3414
3415 if (var.base != (var.selector << 4))
3416 return false;
Gleb Natapov89efbed2012-12-20 16:57:44 +02003417 if (var.limit != 0xffff)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003418 return false;
Gleb Natapov07f42f52012-12-12 19:10:49 +02003419 if (ar != 0xf3)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003420 return false;
3421
3422 return true;
3423}
3424
3425static bool code_segment_valid(struct kvm_vcpu *vcpu)
3426{
3427 struct kvm_segment cs;
3428 unsigned int cs_rpl;
3429
3430 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3431 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
3432
Avi Kivity1872a3f2009-01-04 23:26:52 +02003433 if (cs.unusable)
3434 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003435 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
3436 return false;
3437 if (!cs.s)
3438 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003439 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003440 if (cs.dpl > cs_rpl)
3441 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003442 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003443 if (cs.dpl != cs_rpl)
3444 return false;
3445 }
3446 if (!cs.present)
3447 return false;
3448
3449 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3450 return true;
3451}
3452
3453static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3454{
3455 struct kvm_segment ss;
3456 unsigned int ss_rpl;
3457
3458 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3459 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
3460
Avi Kivity1872a3f2009-01-04 23:26:52 +02003461 if (ss.unusable)
3462 return true;
3463 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003464 return false;
3465 if (!ss.s)
3466 return false;
3467 if (ss.dpl != ss_rpl) /* DPL != RPL */
3468 return false;
3469 if (!ss.present)
3470 return false;
3471
3472 return true;
3473}
3474
3475static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3476{
3477 struct kvm_segment var;
3478 unsigned int rpl;
3479
3480 vmx_get_segment(vcpu, &var, seg);
3481 rpl = var.selector & SELECTOR_RPL_MASK;
3482
Avi Kivity1872a3f2009-01-04 23:26:52 +02003483 if (var.unusable)
3484 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003485 if (!var.s)
3486 return false;
3487 if (!var.present)
3488 return false;
3489 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
3490 if (var.dpl < rpl) /* DPL < RPL */
3491 return false;
3492 }
3493
3494 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3495 * rights flags
3496 */
3497 return true;
3498}
3499
3500static bool tr_valid(struct kvm_vcpu *vcpu)
3501{
3502 struct kvm_segment tr;
3503
3504 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3505
Avi Kivity1872a3f2009-01-04 23:26:52 +02003506 if (tr.unusable)
3507 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003508 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3509 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003510 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003511 return false;
3512 if (!tr.present)
3513 return false;
3514
3515 return true;
3516}
3517
3518static bool ldtr_valid(struct kvm_vcpu *vcpu)
3519{
3520 struct kvm_segment ldtr;
3521
3522 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3523
Avi Kivity1872a3f2009-01-04 23:26:52 +02003524 if (ldtr.unusable)
3525 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003526 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3527 return false;
3528 if (ldtr.type != 2)
3529 return false;
3530 if (!ldtr.present)
3531 return false;
3532
3533 return true;
3534}
3535
3536static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3537{
3538 struct kvm_segment cs, ss;
3539
3540 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3541 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3542
3543 return ((cs.selector & SELECTOR_RPL_MASK) ==
3544 (ss.selector & SELECTOR_RPL_MASK));
3545}
3546
3547/*
3548 * Check if guest state is valid. Returns true if valid, false if
3549 * not.
3550 * We assume that registers are always usable
3551 */
3552static bool guest_state_valid(struct kvm_vcpu *vcpu)
3553{
Gleb Natapovc5e97c82013-01-21 15:36:43 +02003554 if (enable_unrestricted_guest)
3555 return true;
3556
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003557 /* real mode guest state checks */
Avi Kivity3eeb3282010-01-21 15:31:48 +02003558 if (!is_protmode(vcpu)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003559 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3560 return false;
3561 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3562 return false;
3563 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3564 return false;
3565 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3566 return false;
3567 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3568 return false;
3569 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3570 return false;
3571 } else {
3572 /* protected mode guest state checks */
3573 if (!cs_ss_rpl_check(vcpu))
3574 return false;
3575 if (!code_segment_valid(vcpu))
3576 return false;
3577 if (!stack_segment_valid(vcpu))
3578 return false;
3579 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3580 return false;
3581 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3582 return false;
3583 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3584 return false;
3585 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3586 return false;
3587 if (!tr_valid(vcpu))
3588 return false;
3589 if (!ldtr_valid(vcpu))
3590 return false;
3591 }
3592 /* TODO:
3593 * - Add checks on RIP
3594 * - Add checks on RFLAGS
3595 */
3596
3597 return true;
3598}
3599
Mike Dayd77c26f2007-10-08 09:02:08 -04003600static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003601{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003602 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003603 u16 data = 0;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003604 int r, idx, ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003605
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003606 idx = srcu_read_lock(&kvm->srcu);
3607 fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003608 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3609 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003610 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003611 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003612 r = kvm_write_guest_page(kvm, fn++, &data,
3613 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003614 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003615 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003616 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3617 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003618 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003619 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3620 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003621 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003622 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003623 r = kvm_write_guest_page(kvm, fn, &data,
3624 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3625 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02003626 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003627 goto out;
3628
3629 ret = 1;
3630out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003631 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003632 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633}
3634
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003635static int init_rmode_identity_map(struct kvm *kvm)
3636{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003637 int i, idx, r, ret;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003638 pfn_t identity_map_pfn;
3639 u32 tmp;
3640
Avi Kivity089d0342009-03-23 18:26:32 +02003641 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003642 return 1;
3643 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
3644 printk(KERN_ERR "EPT: identity-mapping pagetable "
3645 "haven't been allocated!\n");
3646 return 0;
3647 }
3648 if (likely(kvm->arch.ept_identity_pagetable_done))
3649 return 1;
3650 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003651 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003652 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003653 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3654 if (r < 0)
3655 goto out;
3656 /* Set up identity-mapping pagetable for EPT in real mode */
3657 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3658 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3659 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3660 r = kvm_write_guest_page(kvm, identity_map_pfn,
3661 &tmp, i * sizeof(tmp), sizeof(tmp));
3662 if (r < 0)
3663 goto out;
3664 }
3665 kvm->arch.ept_identity_pagetable_done = true;
3666 ret = 1;
3667out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003668 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003669 return ret;
3670}
3671
Avi Kivity6aa8b732006-12-10 02:21:36 -08003672static void seg_setup(int seg)
3673{
Mathias Krause772e0312012-08-30 01:30:19 +02003674 const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003675 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003676
3677 vmcs_write16(sf->selector, 0);
3678 vmcs_writel(sf->base, 0);
3679 vmcs_write32(sf->limit, 0xffff);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02003680 ar = 0x93;
3681 if (seg == VCPU_SREG_CS)
3682 ar |= 0x08; /* code segment */
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003683
3684 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003685}
3686
Sheng Yangf78e0e22007-10-29 09:40:42 +08003687static int alloc_apic_access_page(struct kvm *kvm)
3688{
Xiao Guangrong44841412012-09-07 14:14:20 +08003689 struct page *page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003690 struct kvm_userspace_memory_region kvm_userspace_mem;
3691 int r = 0;
3692
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003693 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003694 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003695 goto out;
3696 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
3697 kvm_userspace_mem.flags = 0;
3698 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
3699 kvm_userspace_mem.memory_size = PAGE_SIZE;
Alex Williamsonf82a8cf2012-12-10 10:33:21 -07003700 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, false);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003701 if (r)
3702 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003703
Xiao Guangrong44841412012-09-07 14:14:20 +08003704 page = gfn_to_page(kvm, 0xfee00);
3705 if (is_error_page(page)) {
3706 r = -EFAULT;
3707 goto out;
3708 }
3709
3710 kvm->arch.apic_access_page = page;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003711out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003712 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003713 return r;
3714}
3715
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003716static int alloc_identity_pagetable(struct kvm *kvm)
3717{
Xiao Guangrong44841412012-09-07 14:14:20 +08003718 struct page *page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003719 struct kvm_userspace_memory_region kvm_userspace_mem;
3720 int r = 0;
3721
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003722 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003723 if (kvm->arch.ept_identity_pagetable)
3724 goto out;
3725 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
3726 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003727 kvm_userspace_mem.guest_phys_addr =
3728 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003729 kvm_userspace_mem.memory_size = PAGE_SIZE;
Alex Williamsonf82a8cf2012-12-10 10:33:21 -07003730 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, false);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003731 if (r)
3732 goto out;
3733
Xiao Guangrong44841412012-09-07 14:14:20 +08003734 page = gfn_to_page(kvm, kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
3735 if (is_error_page(page)) {
3736 r = -EFAULT;
3737 goto out;
3738 }
3739
3740 kvm->arch.ept_identity_pagetable = page;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003741out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003742 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003743 return r;
3744}
3745
Sheng Yang2384d2b2008-01-17 15:14:33 +08003746static void allocate_vpid(struct vcpu_vmx *vmx)
3747{
3748 int vpid;
3749
3750 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02003751 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003752 return;
3753 spin_lock(&vmx_vpid_lock);
3754 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
3755 if (vpid < VMX_NR_VPIDS) {
3756 vmx->vpid = vpid;
3757 __set_bit(vpid, vmx_vpid_bitmap);
3758 }
3759 spin_unlock(&vmx_vpid_lock);
3760}
3761
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003762static void free_vpid(struct vcpu_vmx *vmx)
3763{
3764 if (!enable_vpid)
3765 return;
3766 spin_lock(&vmx_vpid_lock);
3767 if (vmx->vpid != 0)
3768 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3769 spin_unlock(&vmx_vpid_lock);
3770}
3771
Yang Zhang8d146952013-01-25 10:18:50 +08003772#define MSR_TYPE_R 1
3773#define MSR_TYPE_W 2
3774static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
3775 u32 msr, int type)
Sheng Yang25c5f222008-03-28 13:18:56 +08003776{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003777 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08003778
3779 if (!cpu_has_vmx_msr_bitmap())
3780 return;
3781
3782 /*
3783 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3784 * have the write-low and read-high bitmap offsets the wrong way round.
3785 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3786 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003787 if (msr <= 0x1fff) {
Yang Zhang8d146952013-01-25 10:18:50 +08003788 if (type & MSR_TYPE_R)
3789 /* read-low */
3790 __clear_bit(msr, msr_bitmap + 0x000 / f);
3791
3792 if (type & MSR_TYPE_W)
3793 /* write-low */
3794 __clear_bit(msr, msr_bitmap + 0x800 / f);
3795
Sheng Yang25c5f222008-03-28 13:18:56 +08003796 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3797 msr &= 0x1fff;
Yang Zhang8d146952013-01-25 10:18:50 +08003798 if (type & MSR_TYPE_R)
3799 /* read-high */
3800 __clear_bit(msr, msr_bitmap + 0x400 / f);
3801
3802 if (type & MSR_TYPE_W)
3803 /* write-high */
3804 __clear_bit(msr, msr_bitmap + 0xc00 / f);
3805
3806 }
3807}
3808
3809static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
3810 u32 msr, int type)
3811{
3812 int f = sizeof(unsigned long);
3813
3814 if (!cpu_has_vmx_msr_bitmap())
3815 return;
3816
3817 /*
3818 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3819 * have the write-low and read-high bitmap offsets the wrong way round.
3820 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3821 */
3822 if (msr <= 0x1fff) {
3823 if (type & MSR_TYPE_R)
3824 /* read-low */
3825 __set_bit(msr, msr_bitmap + 0x000 / f);
3826
3827 if (type & MSR_TYPE_W)
3828 /* write-low */
3829 __set_bit(msr, msr_bitmap + 0x800 / f);
3830
3831 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3832 msr &= 0x1fff;
3833 if (type & MSR_TYPE_R)
3834 /* read-high */
3835 __set_bit(msr, msr_bitmap + 0x400 / f);
3836
3837 if (type & MSR_TYPE_W)
3838 /* write-high */
3839 __set_bit(msr, msr_bitmap + 0xc00 / f);
3840
Sheng Yang25c5f222008-03-28 13:18:56 +08003841 }
Sheng Yang25c5f222008-03-28 13:18:56 +08003842}
3843
Avi Kivity58972972009-02-24 22:26:47 +02003844static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
3845{
3846 if (!longmode_only)
Yang Zhang8d146952013-01-25 10:18:50 +08003847 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy,
3848 msr, MSR_TYPE_R | MSR_TYPE_W);
3849 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode,
3850 msr, MSR_TYPE_R | MSR_TYPE_W);
3851}
3852
3853static void vmx_enable_intercept_msr_read_x2apic(u32 msr)
3854{
3855 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
3856 msr, MSR_TYPE_R);
3857 __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
3858 msr, MSR_TYPE_R);
3859}
3860
3861static void vmx_disable_intercept_msr_read_x2apic(u32 msr)
3862{
3863 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
3864 msr, MSR_TYPE_R);
3865 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
3866 msr, MSR_TYPE_R);
3867}
3868
3869static void vmx_disable_intercept_msr_write_x2apic(u32 msr)
3870{
3871 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic,
3872 msr, MSR_TYPE_W);
3873 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic,
3874 msr, MSR_TYPE_W);
Avi Kivity58972972009-02-24 22:26:47 +02003875}
3876
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003878 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
3879 * will not change in the lifetime of the guest.
3880 * Note that host-state that does change is set elsewhere. E.g., host-state
3881 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
3882 */
3883static void vmx_set_constant_host_state(void)
3884{
3885 u32 low32, high32;
3886 unsigned long tmpl;
3887 struct desc_ptr dt;
3888
Suresh Siddhab1a74bf2012-09-20 11:01:49 -07003889 vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003890 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
3891 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
3892
3893 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003894#ifdef CONFIG_X86_64
3895 /*
3896 * Load null selectors, so we can avoid reloading them in
3897 * __vmx_load_host_state(), in case userspace uses the null selectors
3898 * too (the expected case).
3899 */
3900 vmcs_write16(HOST_DS_SELECTOR, 0);
3901 vmcs_write16(HOST_ES_SELECTOR, 0);
3902#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003903 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3904 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003905#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003906 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3907 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
3908
3909 native_store_idt(&dt);
3910 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
3911
Avi Kivity83287ea422012-09-16 15:10:57 +03003912 vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003913
3914 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
3915 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
3916 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
3917 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
3918
3919 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
3920 rdmsr(MSR_IA32_CR_PAT, low32, high32);
3921 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
3922 }
3923}
3924
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003925static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
3926{
3927 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
3928 if (enable_ept)
3929 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003930 if (is_guest_mode(&vmx->vcpu))
3931 vmx->vcpu.arch.cr4_guest_owned_bits &=
3932 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003933 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
3934}
3935
3936static u32 vmx_exec_control(struct vcpu_vmx *vmx)
3937{
3938 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
3939 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
3940 exec_control &= ~CPU_BASED_TPR_SHADOW;
3941#ifdef CONFIG_X86_64
3942 exec_control |= CPU_BASED_CR8_STORE_EXITING |
3943 CPU_BASED_CR8_LOAD_EXITING;
3944#endif
3945 }
3946 if (!enable_ept)
3947 exec_control |= CPU_BASED_CR3_STORE_EXITING |
3948 CPU_BASED_CR3_LOAD_EXITING |
3949 CPU_BASED_INVLPG_EXITING;
3950 return exec_control;
3951}
3952
Yang Zhangc7c9c562013-01-25 10:18:51 +08003953static int vmx_vm_has_apicv(struct kvm *kvm)
3954{
3955 return enable_apicv_reg_vid && irqchip_in_kernel(kvm);
3956}
3957
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003958static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
3959{
3960 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
3961 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
3962 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3963 if (vmx->vpid == 0)
3964 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
3965 if (!enable_ept) {
3966 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
3967 enable_unrestricted_guest = 0;
Mao, Junjiead756a12012-07-02 01:18:48 +00003968 /* Enable INVPCID for non-ept guests may cause performance regression. */
3969 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003970 }
3971 if (!enable_unrestricted_guest)
3972 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
3973 if (!ple_gap)
3974 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Yang Zhangc7c9c562013-01-25 10:18:51 +08003975 if (!vmx_vm_has_apicv(vmx->vcpu.kvm))
3976 exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
3977 SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
Yang Zhang8d146952013-01-25 10:18:50 +08003978 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003979 return exec_control;
3980}
3981
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003982static void ept_set_mmio_spte_mask(void)
3983{
3984 /*
3985 * EPT Misconfigurations can be generated if the value of bits 2:0
3986 * of an EPT paging-structure entry is 110b (write/execute).
3987 * Also, magic bits (0xffull << 49) is set to quickly identify mmio
3988 * spte.
3989 */
3990 kvm_mmu_set_mmio_spte_mask(0xffull << 49 | 0x6ull);
3991}
3992
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003993/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003994 * Sets up the vmcs for emulated real mode.
3995 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003996static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003997{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02003998#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003999 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02004000#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08004001 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004002
Avi Kivity6aa8b732006-12-10 02:21:36 -08004003 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004004 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
4005 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004006
Sheng Yang25c5f222008-03-28 13:18:56 +08004007 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02004008 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08004009
Avi Kivity6aa8b732006-12-10 02:21:36 -08004010 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
4011
Avi Kivity6aa8b732006-12-10 02:21:36 -08004012 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004013 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
4014 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004015
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004016 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004017
Sheng Yang83ff3b92007-11-21 14:33:25 +08004018 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004019 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4020 vmx_secondary_exec_control(vmx));
Sheng Yang83ff3b92007-11-21 14:33:25 +08004021 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08004022
Yang Zhangc7c9c562013-01-25 10:18:51 +08004023 if (enable_apicv_reg_vid) {
4024 vmcs_write64(EOI_EXIT_BITMAP0, 0);
4025 vmcs_write64(EOI_EXIT_BITMAP1, 0);
4026 vmcs_write64(EOI_EXIT_BITMAP2, 0);
4027 vmcs_write64(EOI_EXIT_BITMAP3, 0);
4028
4029 vmcs_write16(GUEST_INTR_STATUS, 0);
4030 }
4031
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004032 if (ple_gap) {
4033 vmcs_write32(PLE_GAP, ple_gap);
4034 vmcs_write32(PLE_WINDOW, ple_window);
4035 }
4036
Xiao Guangrongc3707952011-07-12 03:28:04 +08004037 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
4038 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004039 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
4040
Avi Kivity9581d442010-10-19 16:46:55 +02004041 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
4042 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004043 vmx_set_constant_host_state();
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004044#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08004045 rdmsrl(MSR_FS_BASE, a);
4046 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
4047 rdmsrl(MSR_GS_BASE, a);
4048 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
4049#else
4050 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
4051 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
4052#endif
4053
Eddie Dong2cc51562007-05-21 07:28:09 +03004054 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
4055 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004056 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03004057 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03004058 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004059
Sheng Yang468d4722008-10-09 16:01:55 +08004060 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03004061 u32 msr_low, msr_high;
4062 u64 host_pat;
Sheng Yang468d4722008-10-09 16:01:55 +08004063 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
4064 host_pat = msr_low | ((u64) msr_high << 32);
4065 /* Write the default value follow host pat */
4066 vmcs_write64(GUEST_IA32_PAT, host_pat);
4067 /* Keep arch.pat sync with GUEST_IA32_PAT */
4068 vmx->vcpu.arch.pat = host_pat;
4069 }
4070
Avi Kivity6aa8b732006-12-10 02:21:36 -08004071 for (i = 0; i < NR_VMX_MSR; ++i) {
4072 u32 index = vmx_msr_index[i];
4073 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004074 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004075
4076 if (rdmsr_safe(index, &data_low, &data_high) < 0)
4077 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08004078 if (wrmsr_safe(index, data_low, data_high) < 0)
4079 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03004080 vmx->guest_msrs[j].index = i;
4081 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02004082 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004083 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004085
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004086 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004087
4088 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03004089 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
4090
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004091 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03004092 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004093
4094 return 0;
4095}
4096
4097static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
4098{
4099 struct vcpu_vmx *vmx = to_vmx(vcpu);
4100 u64 msr;
Xiao Guangrong4b9d3a02010-06-08 10:15:51 +08004101 int ret;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004102
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004103 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004104
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004105 vmx->soft_vnmi_blocked = 0;
4106
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004107 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02004108 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004109 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004110 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004111 msr |= MSR_IA32_APICBASE_BSP;
4112 kvm_set_apic_base(&vmx->vcpu, msr);
4113
Avi Kivity2fb92db2011-04-27 19:42:18 +03004114 vmx_segment_cache_clear(vmx);
4115
Avi Kivity5706be02008-08-20 15:07:31 +03004116 seg_setup(VCPU_SREG_CS);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004117 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004118 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
Gleb Natapovd54d07b2012-12-20 16:57:46 +02004119 else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004120 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
4121 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004122 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004123
4124 seg_setup(VCPU_SREG_DS);
4125 seg_setup(VCPU_SREG_ES);
4126 seg_setup(VCPU_SREG_FS);
4127 seg_setup(VCPU_SREG_GS);
4128 seg_setup(VCPU_SREG_SS);
4129
4130 vmcs_write16(GUEST_TR_SELECTOR, 0);
4131 vmcs_writel(GUEST_TR_BASE, 0);
4132 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
4133 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
4134
4135 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
4136 vmcs_writel(GUEST_LDTR_BASE, 0);
4137 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
4138 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
4139
4140 vmcs_write32(GUEST_SYSENTER_CS, 0);
4141 vmcs_writel(GUEST_SYSENTER_ESP, 0);
4142 vmcs_writel(GUEST_SYSENTER_EIP, 0);
4143
4144 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03004145 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004146 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004147 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004148 kvm_rip_write(vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004149
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004150 vmcs_writel(GUEST_GDTR_BASE, 0);
4151 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
4152
4153 vmcs_writel(GUEST_IDTR_BASE, 0);
4154 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
4155
Anthony Liguori443381a2010-12-06 10:53:38 -06004156 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004157 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
4158 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
4159
Avi Kivitye00c8cf2007-10-21 11:00:39 +02004160 /* Special registers */
4161 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
4162
4163 setup_msrs(vmx);
4164
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
4166
Sheng Yangf78e0e22007-10-29 09:40:42 +08004167 if (cpu_has_vmx_tpr_shadow()) {
4168 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
4169 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
4170 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09004171 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08004172 vmcs_write32(TPR_THRESHOLD, 0);
4173 }
4174
4175 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
4176 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004177 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08004178
Sheng Yang2384d2b2008-01-17 15:14:33 +08004179 if (vmx->vpid != 0)
4180 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
4181
Eduardo Habkostfa400522009-10-24 02:49:58 -02004182 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Marcelo Tosatti7a4f5ad2012-03-27 19:47:26 -03004183 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02004184 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Marcelo Tosatti7a4f5ad2012-03-27 19:47:26 -03004185 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004186 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004187 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004188 vmx_fpu_activate(&vmx->vcpu);
4189 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004190
Gui Jianfengb9d762f2010-06-07 10:32:29 +08004191 vpid_sync_context(vmx);
Sheng Yang2384d2b2008-01-17 15:14:33 +08004192
Marcelo Tosatti3200f402008-03-29 20:17:59 -03004193 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004194
Avi Kivity6aa8b732006-12-10 02:21:36 -08004195 return ret;
4196}
4197
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004198/*
4199 * In nested virtualization, check if L1 asked to exit on external interrupts.
4200 * For most existing hypervisors, this will always return true.
4201 */
4202static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
4203{
4204 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
4205 PIN_BASED_EXT_INTR_MASK;
4206}
4207
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004208static void enable_irq_window(struct kvm_vcpu *vcpu)
4209{
4210 u32 cpu_based_vm_exec_control;
Nadav Har'Eld6185f22011-09-22 13:52:56 +03004211 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
4212 /*
4213 * We get here if vmx_interrupt_allowed() said we can't
4214 * inject to L1 now because L2 must run. Ask L2 to exit
4215 * right after entry, so we can inject to L1 more promptly.
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004216 */
Nadav Har'Eld6185f22011-09-22 13:52:56 +03004217 kvm_make_request(KVM_REQ_IMMEDIATE_EXIT, vcpu);
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004218 return;
Nadav Har'Eld6185f22011-09-22 13:52:56 +03004219 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004220
4221 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4222 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
4223 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4224}
4225
4226static void enable_nmi_window(struct kvm_vcpu *vcpu)
4227{
4228 u32 cpu_based_vm_exec_control;
4229
4230 if (!cpu_has_virtual_nmis()) {
4231 enable_irq_window(vcpu);
4232 return;
4233 }
4234
Avi Kivity30bd0c42010-11-01 23:20:48 +02004235 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
4236 enable_irq_window(vcpu);
4237 return;
4238 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004239 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4240 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
4241 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4242}
4243
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004244static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004245{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004246 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004247 uint32_t intr;
4248 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004249
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004250 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004251
Avi Kivityfa89a812008-09-01 15:57:51 +03004252 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004253 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004254 int inc_eip = 0;
4255 if (vcpu->arch.interrupt.soft)
4256 inc_eip = vcpu->arch.event_exit_inst_len;
4257 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004258 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004259 return;
4260 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004261 intr = irq | INTR_INFO_VALID_MASK;
4262 if (vcpu->arch.interrupt.soft) {
4263 intr |= INTR_TYPE_SOFT_INTR;
4264 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4265 vmx->vcpu.arch.event_exit_inst_len);
4266 } else
4267 intr |= INTR_TYPE_EXT_INTR;
4268 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03004269}
4270
Sheng Yangf08864b2008-05-15 18:23:25 +08004271static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4272{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004273 struct vcpu_vmx *vmx = to_vmx(vcpu);
4274
Nadav Har'El0b6ac342011-05-25 23:13:36 +03004275 if (is_guest_mode(vcpu))
4276 return;
4277
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004278 if (!cpu_has_virtual_nmis()) {
4279 /*
4280 * Tracking the NMI-blocked state in software is built upon
4281 * finding the next open IRQ window. This, in turn, depends on
4282 * well-behaving guests: They have to keep IRQs disabled at
4283 * least as long as the NMI handler runs. Otherwise we may
4284 * cause NMI nesting, maybe breaking the guest. But as this is
4285 * highly unlikely, we can live with the residual risk.
4286 */
4287 vmx->soft_vnmi_blocked = 1;
4288 vmx->vnmi_blocked_time = 0;
4289 }
4290
Jan Kiszka487b3912008-09-26 09:30:56 +02004291 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02004292 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004293 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004294 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004295 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004296 return;
4297 }
Sheng Yangf08864b2008-05-15 18:23:25 +08004298 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4299 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08004300}
4301
Gleb Natapovc4282df2009-04-21 17:45:07 +03004302static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02004303{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004304 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03004305 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02004306
Gleb Natapovc4282df2009-04-21 17:45:07 +03004307 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
Avi Kivity30bd0c42010-11-01 23:20:48 +02004308 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4309 | GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02004310}
4311
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004312static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
4313{
4314 if (!cpu_has_virtual_nmis())
4315 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02004316 if (to_vmx(vcpu)->nmi_known_unmasked)
4317 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03004318 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004319}
4320
4321static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4322{
4323 struct vcpu_vmx *vmx = to_vmx(vcpu);
4324
4325 if (!cpu_has_virtual_nmis()) {
4326 if (vmx->soft_vnmi_blocked != masked) {
4327 vmx->soft_vnmi_blocked = masked;
4328 vmx->vnmi_blocked_time = 0;
4329 }
4330 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02004331 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004332 if (masked)
4333 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4334 GUEST_INTR_STATE_NMI);
4335 else
4336 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4337 GUEST_INTR_STATE_NMI);
4338 }
4339}
4340
Gleb Natapov78646122009-03-23 12:12:11 +02004341static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4342{
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004343 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
Nadav Har'El51cfe382011-09-22 13:53:26 +03004344 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4345 if (to_vmx(vcpu)->nested.nested_run_pending ||
4346 (vmcs12->idt_vectoring_info_field &
4347 VECTORING_INFO_VALID_MASK))
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004348 return 0;
4349 nested_vmx_vmexit(vcpu);
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004350 vmcs12->vm_exit_reason = EXIT_REASON_EXTERNAL_INTERRUPT;
4351 vmcs12->vm_exit_intr_info = 0;
4352 /* fall through to normal code, but now in L1, not L2 */
4353 }
4354
Gleb Natapovc4282df2009-04-21 17:45:07 +03004355 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
4356 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4357 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02004358}
4359
Izik Eiduscbc94022007-10-25 00:29:55 +02004360static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4361{
4362 int ret;
4363 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08004364 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02004365 .guest_phys_addr = addr,
4366 .memory_size = PAGE_SIZE * 3,
4367 .flags = 0,
4368 };
4369
Alex Williamsonf82a8cf2012-12-10 10:33:21 -07004370 ret = kvm_set_memory_region(kvm, &tss_mem, false);
Izik Eiduscbc94022007-10-25 00:29:55 +02004371 if (ret)
4372 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004373 kvm->arch.tss_addr = addr;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004374 if (!init_rmode_tss(kvm))
4375 return -ENOMEM;
4376
Izik Eiduscbc94022007-10-25 00:29:55 +02004377 return 0;
4378}
4379
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004380static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004381{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004382 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004383 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01004384 /*
4385 * Update instruction length as we may reinject the exception
4386 * from user space while in guest debugging mode.
4387 */
4388 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4389 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004390 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004391 return false;
4392 /* fall through */
4393 case DB_VECTOR:
4394 if (vcpu->guest_debug &
4395 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4396 return false;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004397 /* fall through */
4398 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004399 case OF_VECTOR:
4400 case BR_VECTOR:
4401 case UD_VECTOR:
4402 case DF_VECTOR:
4403 case SS_VECTOR:
4404 case GP_VECTOR:
4405 case MF_VECTOR:
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004406 return true;
4407 break;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004408 }
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004409 return false;
4410}
4411
4412static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4413 int vec, u32 err_code)
4414{
4415 /*
4416 * Instruction with address size override prefix opcode 0x67
4417 * Cause the #SS fault with 0 error code in VM86 mode.
4418 */
4419 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
4420 if (emulate_instruction(vcpu, 0) == EMULATE_DONE) {
4421 if (vcpu->arch.halt_request) {
4422 vcpu->arch.halt_request = 0;
4423 return kvm_emulate_halt(vcpu);
4424 }
4425 return 1;
4426 }
4427 return 0;
4428 }
4429
4430 /*
4431 * Forward all other exceptions that are valid in real mode.
4432 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4433 * the required debugging infrastructure rework.
4434 */
4435 kvm_queue_exception(vcpu, vec);
4436 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004437}
4438
Andi Kleena0861c02009-06-08 17:37:09 +08004439/*
4440 * Trigger machine check on the host. We assume all the MSRs are already set up
4441 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4442 * We pass a fake environment to the machine check handler because we want
4443 * the guest to be always treated like user space, no matter what context
4444 * it used internally.
4445 */
4446static void kvm_machine_check(void)
4447{
4448#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4449 struct pt_regs regs = {
4450 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4451 .flags = X86_EFLAGS_IF,
4452 };
4453
4454 do_machine_check(&regs, 0);
4455#endif
4456}
4457
Avi Kivity851ba692009-08-24 11:10:17 +03004458static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004459{
4460 /* already handled by vcpu_run */
4461 return 1;
4462}
4463
Avi Kivity851ba692009-08-24 11:10:17 +03004464static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004465{
Avi Kivity1155f762007-11-22 11:30:47 +02004466 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004467 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004468 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004469 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004470 u32 vect_info;
4471 enum emulation_result er;
4472
Avi Kivity1155f762007-11-22 11:30:47 +02004473 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02004474 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004475
Andi Kleena0861c02009-06-08 17:37:09 +08004476 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03004477 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004478
Jan Kiszkae4a41882008-09-26 09:30:46 +02004479 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02004480 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004481
4482 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03004483 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004484 return 1;
4485 }
4486
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004487 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01004488 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004489 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02004490 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004491 return 1;
4492 }
4493
Avi Kivity6aa8b732006-12-10 02:21:36 -08004494 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004495 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004496 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
Xiao Guangrongbf4ca232012-10-17 13:48:06 +08004497
4498 /*
4499 * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
4500 * MMIO, it is better to report an internal error.
4501 * See the comments in vmx_handle_exit.
4502 */
4503 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
4504 !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
4505 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4506 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
4507 vcpu->run->internal.ndata = 2;
4508 vcpu->run->internal.data[0] = vect_info;
4509 vcpu->run->internal.data[1] = intr_info;
4510 return 0;
4511 }
4512
Avi Kivity6aa8b732006-12-10 02:21:36 -08004513 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08004514 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02004515 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004516 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004517 trace_kvm_page_fault(cr2, error_code);
4518
Gleb Natapov3298b752009-05-11 13:35:46 +03004519 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03004520 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01004521 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004522 }
4523
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004524 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Gleb Natapov0ca1b4f2012-12-20 16:57:47 +02004525
4526 if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
4527 return handle_rmode_exception(vcpu, ex_no, error_code);
4528
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004529 switch (ex_no) {
4530 case DB_VECTOR:
4531 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4532 if (!(vcpu->guest_debug &
4533 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
4534 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
4535 kvm_queue_exception(vcpu, DB_VECTOR);
4536 return 1;
4537 }
4538 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4539 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4540 /* fall through */
4541 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01004542 /*
4543 * Update instruction length as we may reinject #BP from
4544 * user space while in guest debugging mode. Reading it for
4545 * #DB as well causes no harm, it is not used in that case.
4546 */
4547 vmx->vcpu.arch.event_exit_inst_len =
4548 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004549 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004550 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004551 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4552 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004553 break;
4554 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004555 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4556 kvm_run->ex.exception = ex_no;
4557 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004558 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004559 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004560 return 0;
4561}
4562
Avi Kivity851ba692009-08-24 11:10:17 +03004563static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004564{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004565 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004566 return 1;
4567}
4568
Avi Kivity851ba692009-08-24 11:10:17 +03004569static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004570{
Avi Kivity851ba692009-08-24 11:10:17 +03004571 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08004572 return 0;
4573}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004574
Avi Kivity851ba692009-08-24 11:10:17 +03004575static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004576{
He, Qingbfdaab02007-09-12 14:18:28 +08004577 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01004578 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004579 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004580
He, Qingbfdaab02007-09-12 14:18:28 +08004581 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02004582 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004583 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004584
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004585 ++vcpu->stat.io_exits;
4586
4587 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01004588 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004589
4590 port = exit_qualification >> 16;
4591 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01004592 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004593
4594 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004595}
4596
Ingo Molnar102d8322007-02-19 14:37:47 +02004597static void
4598vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4599{
4600 /*
4601 * Patch in the VMCALL instruction:
4602 */
4603 hypercall[0] = 0x0f;
4604 hypercall[1] = 0x01;
4605 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004606}
4607
Guo Chao0fa06072012-06-28 15:16:19 +08004608/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004609static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4610{
4611 if (to_vmx(vcpu)->nested.vmxon &&
4612 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
4613 return 1;
4614
4615 if (is_guest_mode(vcpu)) {
4616 /*
4617 * We get here when L2 changed cr0 in a way that did not change
4618 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
4619 * but did change L0 shadowed bits. This can currently happen
4620 * with the TS bit: L0 may want to leave TS on (for lazy fpu
4621 * loading) while pretending to allow the guest to change it.
4622 */
4623 if (kvm_set_cr0(vcpu, (val & vcpu->arch.cr0_guest_owned_bits) |
4624 (vcpu->arch.cr0 & ~vcpu->arch.cr0_guest_owned_bits)))
4625 return 1;
4626 vmcs_writel(CR0_READ_SHADOW, val);
4627 return 0;
4628 } else
4629 return kvm_set_cr0(vcpu, val);
4630}
4631
4632static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4633{
4634 if (is_guest_mode(vcpu)) {
4635 if (kvm_set_cr4(vcpu, (val & vcpu->arch.cr4_guest_owned_bits) |
4636 (vcpu->arch.cr4 & ~vcpu->arch.cr4_guest_owned_bits)))
4637 return 1;
4638 vmcs_writel(CR4_READ_SHADOW, val);
4639 return 0;
4640 } else
4641 return kvm_set_cr4(vcpu, val);
4642}
4643
4644/* called to set cr0 as approriate for clts instruction exit. */
4645static void handle_clts(struct kvm_vcpu *vcpu)
4646{
4647 if (is_guest_mode(vcpu)) {
4648 /*
4649 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
4650 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
4651 * just pretend it's off (also in arch.cr0 for fpu_activate).
4652 */
4653 vmcs_writel(CR0_READ_SHADOW,
4654 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
4655 vcpu->arch.cr0 &= ~X86_CR0_TS;
4656 } else
4657 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
4658}
4659
Avi Kivity851ba692009-08-24 11:10:17 +03004660static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004661{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004662 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004663 int cr;
4664 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004665 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004666
He, Qingbfdaab02007-09-12 14:18:28 +08004667 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004668 cr = exit_qualification & 15;
4669 reg = (exit_qualification >> 8) & 15;
4670 switch ((exit_qualification >> 4) & 3) {
4671 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004672 val = kvm_register_read(vcpu, reg);
4673 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004674 switch (cr) {
4675 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004676 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004677 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004678 return 1;
4679 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03004680 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004681 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004682 return 1;
4683 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004684 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004685 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004686 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004687 case 8: {
4688 u8 cr8_prev = kvm_get_cr8(vcpu);
4689 u8 cr8 = kvm_register_read(vcpu, reg);
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004690 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004691 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004692 if (irqchip_in_kernel(vcpu->kvm))
4693 return 1;
4694 if (cr8_prev <= cr8)
4695 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03004696 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004697 return 0;
4698 }
Peter Senna Tschudin4b8073e2012-09-18 18:36:14 +02004699 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004700 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03004701 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004702 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02004703 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03004704 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02004705 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03004706 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004707 case 1: /*mov from cr*/
4708 switch (cr) {
4709 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02004710 val = kvm_read_cr3(vcpu);
4711 kvm_register_write(vcpu, reg, val);
4712 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004713 skip_emulated_instruction(vcpu);
4714 return 1;
4715 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004716 val = kvm_get_cr8(vcpu);
4717 kvm_register_write(vcpu, reg, val);
4718 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004719 skip_emulated_instruction(vcpu);
4720 return 1;
4721 }
4722 break;
4723 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02004724 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004725 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02004726 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004727
4728 skip_emulated_instruction(vcpu);
4729 return 1;
4730 default:
4731 break;
4732 }
Avi Kivity851ba692009-08-24 11:10:17 +03004733 vcpu->run->exit_reason = 0;
Christoffer Dalla737f252012-06-03 21:17:48 +03004734 vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08004735 (int)(exit_qualification >> 4) & 3, cr);
4736 return 0;
4737}
4738
Avi Kivity851ba692009-08-24 11:10:17 +03004739static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004740{
He, Qingbfdaab02007-09-12 14:18:28 +08004741 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004742 int dr, reg;
4743
Jan Kiszkaf2483412010-01-20 18:20:20 +01004744 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03004745 if (!kvm_require_cpl(vcpu, 0))
4746 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004747 dr = vmcs_readl(GUEST_DR7);
4748 if (dr & DR7_GD) {
4749 /*
4750 * As the vm-exit takes precedence over the debug trap, we
4751 * need to emulate the latter, either for the host or the
4752 * guest debugging itself.
4753 */
4754 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03004755 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
4756 vcpu->run->debug.arch.dr7 = dr;
4757 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004758 vmcs_readl(GUEST_CS_BASE) +
4759 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03004760 vcpu->run->debug.arch.exception = DB_VECTOR;
4761 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004762 return 0;
4763 } else {
4764 vcpu->arch.dr7 &= ~DR7_GD;
4765 vcpu->arch.dr6 |= DR6_BD;
4766 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
4767 kvm_queue_exception(vcpu, DB_VECTOR);
4768 return 1;
4769 }
4770 }
4771
He, Qingbfdaab02007-09-12 14:18:28 +08004772 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004773 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
4774 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
4775 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03004776 unsigned long val;
4777 if (!kvm_get_dr(vcpu, dr, &val))
4778 kvm_register_write(vcpu, reg, val);
4779 } else
4780 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004781 skip_emulated_instruction(vcpu);
4782 return 1;
4783}
4784
Gleb Natapov020df072010-04-13 10:05:23 +03004785static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
4786{
4787 vmcs_writel(GUEST_DR7, val);
4788}
4789
Avi Kivity851ba692009-08-24 11:10:17 +03004790static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004791{
Avi Kivity06465c52007-02-28 20:46:53 +02004792 kvm_emulate_cpuid(vcpu);
4793 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004794}
4795
Avi Kivity851ba692009-08-24 11:10:17 +03004796static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004797{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004798 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004799 u64 data;
4800
4801 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02004802 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004803 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004804 return 1;
4805 }
4806
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004807 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004808
Avi Kivity6aa8b732006-12-10 02:21:36 -08004809 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004810 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
4811 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004812 skip_emulated_instruction(vcpu);
4813 return 1;
4814}
4815
Avi Kivity851ba692009-08-24 11:10:17 +03004816static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004817{
Will Auld8fe8ab42012-11-29 12:42:12 -08004818 struct msr_data msr;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004819 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
4820 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
4821 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004822
Will Auld8fe8ab42012-11-29 12:42:12 -08004823 msr.data = data;
4824 msr.index = ecx;
4825 msr.host_initiated = false;
4826 if (vmx_set_msr(vcpu, &msr) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02004827 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004828 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004829 return 1;
4830 }
4831
Avi Kivity59200272010-01-25 19:47:02 +02004832 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004833 skip_emulated_instruction(vcpu);
4834 return 1;
4835}
4836
Avi Kivity851ba692009-08-24 11:10:17 +03004837static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004838{
Avi Kivity3842d132010-07-27 12:30:24 +03004839 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004840 return 1;
4841}
4842
Avi Kivity851ba692009-08-24 11:10:17 +03004843static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004844{
Eddie Dong85f455f2007-07-06 12:20:49 +03004845 u32 cpu_based_vm_exec_control;
4846
4847 /* clear pending irq */
4848 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4849 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
4850 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004851
Avi Kivity3842d132010-07-27 12:30:24 +03004852 kvm_make_request(KVM_REQ_EVENT, vcpu);
4853
Jan Kiszkaa26bf122008-09-26 09:30:45 +02004854 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004855
Dor Laorc1150d82007-01-05 16:36:24 -08004856 /*
4857 * If the user space waits to inject interrupts, exit as soon as
4858 * possible
4859 */
Gleb Natapov80618232009-04-21 17:44:56 +03004860 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004861 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03004862 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004863 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08004864 return 0;
4865 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004866 return 1;
4867}
4868
Avi Kivity851ba692009-08-24 11:10:17 +03004869static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004870{
4871 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03004872 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004873}
4874
Avi Kivity851ba692009-08-24 11:10:17 +03004875static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02004876{
Dor Laor510043d2007-02-19 18:25:43 +02004877 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004878 kvm_emulate_hypercall(vcpu);
4879 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02004880}
4881
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004882static int handle_invd(struct kvm_vcpu *vcpu)
4883{
Andre Przywara51d8b662010-12-21 11:12:02 +01004884 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004885}
4886
Avi Kivity851ba692009-08-24 11:10:17 +03004887static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03004888{
Sheng Yangf9c617f2009-03-25 10:08:52 +08004889 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03004890
4891 kvm_mmu_invlpg(vcpu, exit_qualification);
4892 skip_emulated_instruction(vcpu);
4893 return 1;
4894}
4895
Avi Kivityfee84b02011-11-10 14:57:25 +02004896static int handle_rdpmc(struct kvm_vcpu *vcpu)
4897{
4898 int err;
4899
4900 err = kvm_rdpmc(vcpu);
4901 kvm_complete_insn_gp(vcpu, err);
4902
4903 return 1;
4904}
4905
Avi Kivity851ba692009-08-24 11:10:17 +03004906static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02004907{
4908 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004909 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02004910 return 1;
4911}
4912
Dexuan Cui2acf9232010-06-10 11:27:12 +08004913static int handle_xsetbv(struct kvm_vcpu *vcpu)
4914{
4915 u64 new_bv = kvm_read_edx_eax(vcpu);
4916 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4917
4918 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
4919 skip_emulated_instruction(vcpu);
4920 return 1;
4921}
4922
Avi Kivity851ba692009-08-24 11:10:17 +03004923static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004924{
Kevin Tian58fbbf22011-08-30 13:56:17 +03004925 if (likely(fasteoi)) {
4926 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4927 int access_type, offset;
4928
4929 access_type = exit_qualification & APIC_ACCESS_TYPE;
4930 offset = exit_qualification & APIC_ACCESS_OFFSET;
4931 /*
4932 * Sane guest uses MOV to write EOI, with written value
4933 * not cared. So make a short-circuit here by avoiding
4934 * heavy instruction emulation.
4935 */
4936 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
4937 (offset == APIC_EOI)) {
4938 kvm_lapic_set_eoi(vcpu);
4939 skip_emulated_instruction(vcpu);
4940 return 1;
4941 }
4942 }
Andre Przywara51d8b662010-12-21 11:12:02 +01004943 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004944}
4945
Yang Zhangc7c9c562013-01-25 10:18:51 +08004946static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
4947{
4948 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4949 int vector = exit_qualification & 0xff;
4950
4951 /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
4952 kvm_apic_set_eoi_accelerated(vcpu, vector);
4953 return 1;
4954}
4955
Yang Zhang83d4c282013-01-25 10:18:49 +08004956static int handle_apic_write(struct kvm_vcpu *vcpu)
4957{
4958 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4959 u32 offset = exit_qualification & 0xfff;
4960
4961 /* APIC-write VM exit is trap-like and thus no need to adjust IP */
4962 kvm_apic_write_nodecode(vcpu, offset);
4963 return 1;
4964}
4965
Avi Kivity851ba692009-08-24 11:10:17 +03004966static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02004967{
Jan Kiszka60637aa2008-09-26 09:30:47 +02004968 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004969 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02004970 bool has_error_code = false;
4971 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02004972 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004973 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004974
4975 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004976 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004977 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02004978
4979 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4980
4981 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004982 if (reason == TASK_SWITCH_GATE && idt_v) {
4983 switch (type) {
4984 case INTR_TYPE_NMI_INTR:
4985 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02004986 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004987 break;
4988 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004989 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004990 kvm_clear_interrupt_queue(vcpu);
4991 break;
4992 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02004993 if (vmx->idt_vectoring_info &
4994 VECTORING_INFO_DELIVER_CODE_MASK) {
4995 has_error_code = true;
4996 error_code =
4997 vmcs_read32(IDT_VECTORING_ERROR_CODE);
4998 }
4999 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005000 case INTR_TYPE_SOFT_EXCEPTION:
5001 kvm_clear_exception_queue(vcpu);
5002 break;
5003 default:
5004 break;
5005 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02005006 }
Izik Eidus37817f22008-03-24 23:14:53 +02005007 tss_selector = exit_qualification;
5008
Gleb Natapov64a7ec02009-03-30 16:03:29 +03005009 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5010 type != INTR_TYPE_EXT_INTR &&
5011 type != INTR_TYPE_NMI_INTR))
5012 skip_emulated_instruction(vcpu);
5013
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01005014 if (kvm_task_switch(vcpu, tss_selector,
5015 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
5016 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03005017 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5018 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5019 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005020 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03005021 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01005022
5023 /* clear all local breakpoint enable flags */
5024 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
5025
5026 /*
5027 * TODO: What about debug traps on tss switch?
5028 * Are we supposed to inject them and update dr6?
5029 */
5030
5031 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02005032}
5033
Avi Kivity851ba692009-08-24 11:10:17 +03005034static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08005035{
Sheng Yangf9c617f2009-03-25 10:08:52 +08005036 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08005037 gpa_t gpa;
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005038 u32 error_code;
Sheng Yang14394422008-04-28 12:24:45 +08005039 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08005040
Sheng Yangf9c617f2009-03-25 10:08:52 +08005041 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08005042
Sheng Yang14394422008-04-28 12:24:45 +08005043 gla_validity = (exit_qualification >> 7) & 0x3;
5044 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
5045 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
5046 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
5047 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08005048 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08005049 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
5050 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03005051 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5052 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03005053 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08005054 }
5055
5056 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005057 trace_kvm_page_fault(gpa, exit_qualification);
Xiao Guangrong4f5982a2012-06-20 15:58:04 +08005058
5059 /* It is a write fault? */
5060 error_code = exit_qualification & (1U << 1);
5061 /* ept page table is present? */
5062 error_code |= (exit_qualification >> 3) & 0x1;
5063
5064 return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08005065}
5066
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005067static u64 ept_rsvd_mask(u64 spte, int level)
5068{
5069 int i;
5070 u64 mask = 0;
5071
5072 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
5073 mask |= (1ULL << i);
5074
5075 if (level > 2)
5076 /* bits 7:3 reserved */
5077 mask |= 0xf8;
5078 else if (level == 2) {
5079 if (spte & (1ULL << 7))
5080 /* 2MB ref, bits 20:12 reserved */
5081 mask |= 0x1ff000;
5082 else
5083 /* bits 6:3 reserved */
5084 mask |= 0x78;
5085 }
5086
5087 return mask;
5088}
5089
5090static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
5091 int level)
5092{
5093 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
5094
5095 /* 010b (write-only) */
5096 WARN_ON((spte & 0x7) == 0x2);
5097
5098 /* 110b (write/execute) */
5099 WARN_ON((spte & 0x7) == 0x6);
5100
5101 /* 100b (execute-only) and value not supported by logical processor */
5102 if (!cpu_has_vmx_ept_execute_only())
5103 WARN_ON((spte & 0x7) == 0x4);
5104
5105 /* not 000b */
5106 if ((spte & 0x7)) {
5107 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
5108
5109 if (rsvd_bits != 0) {
5110 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
5111 __func__, rsvd_bits);
5112 WARN_ON(1);
5113 }
5114
5115 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
5116 u64 ept_mem_type = (spte & 0x38) >> 3;
5117
5118 if (ept_mem_type == 2 || ept_mem_type == 3 ||
5119 ept_mem_type == 7) {
5120 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
5121 __func__, ept_mem_type);
5122 WARN_ON(1);
5123 }
5124 }
5125 }
5126}
5127
Avi Kivity851ba692009-08-24 11:10:17 +03005128static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005129{
5130 u64 sptes[4];
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005131 int nr_sptes, i, ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005132 gpa_t gpa;
5133
5134 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
5135
Xiao Guangrongce88dec2011-07-12 03:33:44 +08005136 ret = handle_mmio_page_fault_common(vcpu, gpa, true);
5137 if (likely(ret == 1))
5138 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
5139 EMULATE_DONE;
5140 if (unlikely(!ret))
5141 return 1;
5142
5143 /* It is the real ept misconfig */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005144 printk(KERN_ERR "EPT: Misconfiguration.\n");
5145 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
5146
5147 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
5148
5149 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
5150 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
5151
Avi Kivity851ba692009-08-24 11:10:17 +03005152 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5153 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005154
5155 return 0;
5156}
5157
Avi Kivity851ba692009-08-24 11:10:17 +03005158static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08005159{
5160 u32 cpu_based_vm_exec_control;
5161
5162 /* clear pending NMI */
5163 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5164 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
5165 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
5166 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03005167 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08005168
5169 return 1;
5170}
5171
Mohammed Gamal80ced182009-09-01 12:48:18 +02005172static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005173{
Avi Kivity8b3079a2009-01-05 12:10:54 +02005174 struct vcpu_vmx *vmx = to_vmx(vcpu);
5175 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02005176 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02005177 u32 cpu_exec_ctrl;
5178 bool intr_window_requested;
Avi Kivityb8405c12012-06-07 17:08:48 +03005179 unsigned count = 130;
Avi Kivity49e9d552010-09-19 14:34:08 +02005180
5181 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
5182 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005183
Avi Kivityb8405c12012-06-07 17:08:48 +03005184 while (!guest_state_valid(vcpu) && count-- != 0) {
Avi Kivitybdea48e2012-06-10 18:07:57 +03005185 if (intr_window_requested && vmx_interrupt_allowed(vcpu))
Avi Kivity49e9d552010-09-19 14:34:08 +02005186 return handle_interrupt_window(&vmx->vcpu);
5187
Avi Kivityde87dcdd2012-06-12 20:21:38 +03005188 if (test_bit(KVM_REQ_EVENT, &vcpu->requests))
5189 return 1;
5190
Andre Przywara51d8b662010-12-21 11:12:02 +01005191 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005192
Mohammed Gamal80ced182009-09-01 12:48:18 +02005193 if (err == EMULATE_DO_MMIO) {
5194 ret = 0;
5195 goto out;
5196 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005197
Avi Kivityde5f70e2012-06-12 20:22:28 +03005198 if (err != EMULATE_DONE) {
5199 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5200 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
5201 vcpu->run->internal.ndata = 0;
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03005202 return 0;
Avi Kivityde5f70e2012-06-12 20:22:28 +03005203 }
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005204
5205 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02005206 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005207 if (need_resched())
5208 schedule();
5209 }
5210
Gleb Natapov14168782013-01-21 15:36:49 +02005211 vmx->emulation_required = emulation_required(vcpu);
Mohammed Gamal80ced182009-09-01 12:48:18 +02005212out:
5213 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03005214}
5215
Avi Kivity6aa8b732006-12-10 02:21:36 -08005216/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005217 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
5218 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
5219 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03005220static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005221{
5222 skip_emulated_instruction(vcpu);
5223 kvm_vcpu_on_spin(vcpu);
5224
5225 return 1;
5226}
5227
Sheng Yang59708672009-12-15 13:29:54 +08005228static int handle_invalid_op(struct kvm_vcpu *vcpu)
5229{
5230 kvm_queue_exception(vcpu, UD_VECTOR);
5231 return 1;
5232}
5233
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005234/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005235 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
5236 * We could reuse a single VMCS for all the L2 guests, but we also want the
5237 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
5238 * allows keeping them loaded on the processor, and in the future will allow
5239 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
5240 * every entry if they never change.
5241 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
5242 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
5243 *
5244 * The following functions allocate and free a vmcs02 in this pool.
5245 */
5246
5247/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
5248static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
5249{
5250 struct vmcs02_list *item;
5251 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5252 if (item->vmptr == vmx->nested.current_vmptr) {
5253 list_move(&item->list, &vmx->nested.vmcs02_pool);
5254 return &item->vmcs02;
5255 }
5256
5257 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
5258 /* Recycle the least recently used VMCS. */
5259 item = list_entry(vmx->nested.vmcs02_pool.prev,
5260 struct vmcs02_list, list);
5261 item->vmptr = vmx->nested.current_vmptr;
5262 list_move(&item->list, &vmx->nested.vmcs02_pool);
5263 return &item->vmcs02;
5264 }
5265
5266 /* Create a new VMCS */
5267 item = (struct vmcs02_list *)
5268 kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
5269 if (!item)
5270 return NULL;
5271 item->vmcs02.vmcs = alloc_vmcs();
5272 if (!item->vmcs02.vmcs) {
5273 kfree(item);
5274 return NULL;
5275 }
5276 loaded_vmcs_init(&item->vmcs02);
5277 item->vmptr = vmx->nested.current_vmptr;
5278 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
5279 vmx->nested.vmcs02_num++;
5280 return &item->vmcs02;
5281}
5282
5283/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
5284static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
5285{
5286 struct vmcs02_list *item;
5287 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5288 if (item->vmptr == vmptr) {
5289 free_loaded_vmcs(&item->vmcs02);
5290 list_del(&item->list);
5291 kfree(item);
5292 vmx->nested.vmcs02_num--;
5293 return;
5294 }
5295}
5296
5297/*
5298 * Free all VMCSs saved for this vcpu, except the one pointed by
5299 * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
5300 * currently used, if running L2), and vmcs01 when running L2.
5301 */
5302static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
5303{
5304 struct vmcs02_list *item, *n;
5305 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
5306 if (vmx->loaded_vmcs != &item->vmcs02)
5307 free_loaded_vmcs(&item->vmcs02);
5308 list_del(&item->list);
5309 kfree(item);
5310 }
5311 vmx->nested.vmcs02_num = 0;
5312
5313 if (vmx->loaded_vmcs != &vmx->vmcs01)
5314 free_loaded_vmcs(&vmx->vmcs01);
5315}
5316
5317/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005318 * Emulate the VMXON instruction.
5319 * Currently, we just remember that VMX is active, and do not save or even
5320 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
5321 * do not currently need to store anything in that guest-allocated memory
5322 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
5323 * argument is different from the VMXON pointer (which the spec says they do).
5324 */
5325static int handle_vmon(struct kvm_vcpu *vcpu)
5326{
5327 struct kvm_segment cs;
5328 struct vcpu_vmx *vmx = to_vmx(vcpu);
5329
5330 /* The Intel VMX Instruction Reference lists a bunch of bits that
5331 * are prerequisite to running VMXON, most notably cr4.VMXE must be
5332 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
5333 * Otherwise, we should fail with #UD. We test these now:
5334 */
5335 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
5336 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
5337 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5338 kvm_queue_exception(vcpu, UD_VECTOR);
5339 return 1;
5340 }
5341
5342 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5343 if (is_long_mode(vcpu) && !cs.l) {
5344 kvm_queue_exception(vcpu, UD_VECTOR);
5345 return 1;
5346 }
5347
5348 if (vmx_get_cpl(vcpu)) {
5349 kvm_inject_gp(vcpu, 0);
5350 return 1;
5351 }
5352
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005353 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
5354 vmx->nested.vmcs02_num = 0;
5355
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005356 vmx->nested.vmxon = true;
5357
5358 skip_emulated_instruction(vcpu);
5359 return 1;
5360}
5361
5362/*
5363 * Intel's VMX Instruction Reference specifies a common set of prerequisites
5364 * for running VMX instructions (except VMXON, whose prerequisites are
5365 * slightly different). It also specifies what exception to inject otherwise.
5366 */
5367static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
5368{
5369 struct kvm_segment cs;
5370 struct vcpu_vmx *vmx = to_vmx(vcpu);
5371
5372 if (!vmx->nested.vmxon) {
5373 kvm_queue_exception(vcpu, UD_VECTOR);
5374 return 0;
5375 }
5376
5377 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5378 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
5379 (is_long_mode(vcpu) && !cs.l)) {
5380 kvm_queue_exception(vcpu, UD_VECTOR);
5381 return 0;
5382 }
5383
5384 if (vmx_get_cpl(vcpu)) {
5385 kvm_inject_gp(vcpu, 0);
5386 return 0;
5387 }
5388
5389 return 1;
5390}
5391
5392/*
5393 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
5394 * just stops using VMX.
5395 */
5396static void free_nested(struct vcpu_vmx *vmx)
5397{
5398 if (!vmx->nested.vmxon)
5399 return;
5400 vmx->nested.vmxon = false;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03005401 if (vmx->nested.current_vmptr != -1ull) {
5402 kunmap(vmx->nested.current_vmcs12_page);
5403 nested_release_page(vmx->nested.current_vmcs12_page);
5404 vmx->nested.current_vmptr = -1ull;
5405 vmx->nested.current_vmcs12 = NULL;
5406 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005407 /* Unpin physical memory we referred to in current vmcs02 */
5408 if (vmx->nested.apic_access_page) {
5409 nested_release_page(vmx->nested.apic_access_page);
5410 vmx->nested.apic_access_page = 0;
5411 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005412
5413 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005414}
5415
5416/* Emulate the VMXOFF instruction */
5417static int handle_vmoff(struct kvm_vcpu *vcpu)
5418{
5419 if (!nested_vmx_check_permission(vcpu))
5420 return 1;
5421 free_nested(to_vmx(vcpu));
5422 skip_emulated_instruction(vcpu);
5423 return 1;
5424}
5425
5426/*
Nadav Har'El064aea72011-05-25 23:04:56 +03005427 * Decode the memory-address operand of a vmx instruction, as recorded on an
5428 * exit caused by such an instruction (run by a guest hypervisor).
5429 * On success, returns 0. When the operand is invalid, returns 1 and throws
5430 * #UD or #GP.
5431 */
5432static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
5433 unsigned long exit_qualification,
5434 u32 vmx_instruction_info, gva_t *ret)
5435{
5436 /*
5437 * According to Vol. 3B, "Information for VM Exits Due to Instruction
5438 * Execution", on an exit, vmx_instruction_info holds most of the
5439 * addressing components of the operand. Only the displacement part
5440 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
5441 * For how an actual address is calculated from all these components,
5442 * refer to Vol. 1, "Operand Addressing".
5443 */
5444 int scaling = vmx_instruction_info & 3;
5445 int addr_size = (vmx_instruction_info >> 7) & 7;
5446 bool is_reg = vmx_instruction_info & (1u << 10);
5447 int seg_reg = (vmx_instruction_info >> 15) & 7;
5448 int index_reg = (vmx_instruction_info >> 18) & 0xf;
5449 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
5450 int base_reg = (vmx_instruction_info >> 23) & 0xf;
5451 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
5452
5453 if (is_reg) {
5454 kvm_queue_exception(vcpu, UD_VECTOR);
5455 return 1;
5456 }
5457
5458 /* Addr = segment_base + offset */
5459 /* offset = base + [index * scale] + displacement */
5460 *ret = vmx_get_segment_base(vcpu, seg_reg);
5461 if (base_is_valid)
5462 *ret += kvm_register_read(vcpu, base_reg);
5463 if (index_is_valid)
5464 *ret += kvm_register_read(vcpu, index_reg)<<scaling;
5465 *ret += exit_qualification; /* holds the displacement */
5466
5467 if (addr_size == 1) /* 32 bit */
5468 *ret &= 0xffffffff;
5469
5470 /*
5471 * TODO: throw #GP (and return 1) in various cases that the VM*
5472 * instructions require it - e.g., offset beyond segment limit,
5473 * unusable or unreadable/unwritable segment, non-canonical 64-bit
5474 * address, and so on. Currently these are not checked.
5475 */
5476 return 0;
5477}
5478
5479/*
Nadav Har'El0140cae2011-05-25 23:06:28 +03005480 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
5481 * set the success or error code of an emulated VMX instruction, as specified
5482 * by Vol 2B, VMX Instruction Reference, "Conventions".
5483 */
5484static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
5485{
5486 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
5487 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5488 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
5489}
5490
5491static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
5492{
5493 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5494 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
5495 X86_EFLAGS_SF | X86_EFLAGS_OF))
5496 | X86_EFLAGS_CF);
5497}
5498
5499static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
5500 u32 vm_instruction_error)
5501{
5502 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
5503 /*
5504 * failValid writes the error number to the current VMCS, which
5505 * can't be done there isn't a current VMCS.
5506 */
5507 nested_vmx_failInvalid(vcpu);
5508 return;
5509 }
5510 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5511 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5512 X86_EFLAGS_SF | X86_EFLAGS_OF))
5513 | X86_EFLAGS_ZF);
5514 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
5515}
5516
Nadav Har'El27d6c862011-05-25 23:06:59 +03005517/* Emulate the VMCLEAR instruction */
5518static int handle_vmclear(struct kvm_vcpu *vcpu)
5519{
5520 struct vcpu_vmx *vmx = to_vmx(vcpu);
5521 gva_t gva;
5522 gpa_t vmptr;
5523 struct vmcs12 *vmcs12;
5524 struct page *page;
5525 struct x86_exception e;
5526
5527 if (!nested_vmx_check_permission(vcpu))
5528 return 1;
5529
5530 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5531 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5532 return 1;
5533
5534 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5535 sizeof(vmptr), &e)) {
5536 kvm_inject_page_fault(vcpu, &e);
5537 return 1;
5538 }
5539
5540 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
5541 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
5542 skip_emulated_instruction(vcpu);
5543 return 1;
5544 }
5545
5546 if (vmptr == vmx->nested.current_vmptr) {
5547 kunmap(vmx->nested.current_vmcs12_page);
5548 nested_release_page(vmx->nested.current_vmcs12_page);
5549 vmx->nested.current_vmptr = -1ull;
5550 vmx->nested.current_vmcs12 = NULL;
5551 }
5552
5553 page = nested_get_page(vcpu, vmptr);
5554 if (page == NULL) {
5555 /*
5556 * For accurate processor emulation, VMCLEAR beyond available
5557 * physical memory should do nothing at all. However, it is
5558 * possible that a nested vmx bug, not a guest hypervisor bug,
5559 * resulted in this case, so let's shut down before doing any
5560 * more damage:
5561 */
5562 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5563 return 1;
5564 }
5565 vmcs12 = kmap(page);
5566 vmcs12->launch_state = 0;
5567 kunmap(page);
5568 nested_release_page(page);
5569
5570 nested_free_vmcs02(vmx, vmptr);
5571
5572 skip_emulated_instruction(vcpu);
5573 nested_vmx_succeed(vcpu);
5574 return 1;
5575}
5576
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03005577static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
5578
5579/* Emulate the VMLAUNCH instruction */
5580static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5581{
5582 return nested_vmx_run(vcpu, true);
5583}
5584
5585/* Emulate the VMRESUME instruction */
5586static int handle_vmresume(struct kvm_vcpu *vcpu)
5587{
5588
5589 return nested_vmx_run(vcpu, false);
5590}
5591
Nadav Har'El49f705c2011-05-25 23:08:30 +03005592enum vmcs_field_type {
5593 VMCS_FIELD_TYPE_U16 = 0,
5594 VMCS_FIELD_TYPE_U64 = 1,
5595 VMCS_FIELD_TYPE_U32 = 2,
5596 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
5597};
5598
5599static inline int vmcs_field_type(unsigned long field)
5600{
5601 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
5602 return VMCS_FIELD_TYPE_U32;
5603 return (field >> 13) & 0x3 ;
5604}
5605
5606static inline int vmcs_field_readonly(unsigned long field)
5607{
5608 return (((field >> 10) & 0x3) == 1);
5609}
5610
5611/*
5612 * Read a vmcs12 field. Since these can have varying lengths and we return
5613 * one type, we chose the biggest type (u64) and zero-extend the return value
5614 * to that size. Note that the caller, handle_vmread, might need to use only
5615 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
5616 * 64-bit fields are to be returned).
5617 */
5618static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
5619 unsigned long field, u64 *ret)
5620{
5621 short offset = vmcs_field_to_offset(field);
5622 char *p;
5623
5624 if (offset < 0)
5625 return 0;
5626
5627 p = ((char *)(get_vmcs12(vcpu))) + offset;
5628
5629 switch (vmcs_field_type(field)) {
5630 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
5631 *ret = *((natural_width *)p);
5632 return 1;
5633 case VMCS_FIELD_TYPE_U16:
5634 *ret = *((u16 *)p);
5635 return 1;
5636 case VMCS_FIELD_TYPE_U32:
5637 *ret = *((u32 *)p);
5638 return 1;
5639 case VMCS_FIELD_TYPE_U64:
5640 *ret = *((u64 *)p);
5641 return 1;
5642 default:
5643 return 0; /* can never happen. */
5644 }
5645}
5646
5647/*
5648 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
5649 * used before) all generate the same failure when it is missing.
5650 */
5651static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
5652{
5653 struct vcpu_vmx *vmx = to_vmx(vcpu);
5654 if (vmx->nested.current_vmptr == -1ull) {
5655 nested_vmx_failInvalid(vcpu);
5656 skip_emulated_instruction(vcpu);
5657 return 0;
5658 }
5659 return 1;
5660}
5661
5662static int handle_vmread(struct kvm_vcpu *vcpu)
5663{
5664 unsigned long field;
5665 u64 field_value;
5666 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5667 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5668 gva_t gva = 0;
5669
5670 if (!nested_vmx_check_permission(vcpu) ||
5671 !nested_vmx_check_vmcs12(vcpu))
5672 return 1;
5673
5674 /* Decode instruction info and find the field to read */
5675 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5676 /* Read the field, zero-extended to a u64 field_value */
5677 if (!vmcs12_read_any(vcpu, field, &field_value)) {
5678 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5679 skip_emulated_instruction(vcpu);
5680 return 1;
5681 }
5682 /*
5683 * Now copy part of this value to register or memory, as requested.
5684 * Note that the number of bits actually copied is 32 or 64 depending
5685 * on the guest's mode (32 or 64 bit), not on the given field's length.
5686 */
5687 if (vmx_instruction_info & (1u << 10)) {
5688 kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
5689 field_value);
5690 } else {
5691 if (get_vmx_mem_address(vcpu, exit_qualification,
5692 vmx_instruction_info, &gva))
5693 return 1;
5694 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
5695 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
5696 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
5697 }
5698
5699 nested_vmx_succeed(vcpu);
5700 skip_emulated_instruction(vcpu);
5701 return 1;
5702}
5703
5704
5705static int handle_vmwrite(struct kvm_vcpu *vcpu)
5706{
5707 unsigned long field;
5708 gva_t gva;
5709 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5710 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5711 char *p;
5712 short offset;
5713 /* The value to write might be 32 or 64 bits, depending on L1's long
5714 * mode, and eventually we need to write that into a field of several
5715 * possible lengths. The code below first zero-extends the value to 64
5716 * bit (field_value), and then copies only the approriate number of
5717 * bits into the vmcs12 field.
5718 */
5719 u64 field_value = 0;
5720 struct x86_exception e;
5721
5722 if (!nested_vmx_check_permission(vcpu) ||
5723 !nested_vmx_check_vmcs12(vcpu))
5724 return 1;
5725
5726 if (vmx_instruction_info & (1u << 10))
5727 field_value = kvm_register_read(vcpu,
5728 (((vmx_instruction_info) >> 3) & 0xf));
5729 else {
5730 if (get_vmx_mem_address(vcpu, exit_qualification,
5731 vmx_instruction_info, &gva))
5732 return 1;
5733 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
5734 &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) {
5735 kvm_inject_page_fault(vcpu, &e);
5736 return 1;
5737 }
5738 }
5739
5740
5741 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5742 if (vmcs_field_readonly(field)) {
5743 nested_vmx_failValid(vcpu,
5744 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
5745 skip_emulated_instruction(vcpu);
5746 return 1;
5747 }
5748
5749 offset = vmcs_field_to_offset(field);
5750 if (offset < 0) {
5751 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5752 skip_emulated_instruction(vcpu);
5753 return 1;
5754 }
5755 p = ((char *) get_vmcs12(vcpu)) + offset;
5756
5757 switch (vmcs_field_type(field)) {
5758 case VMCS_FIELD_TYPE_U16:
5759 *(u16 *)p = field_value;
5760 break;
5761 case VMCS_FIELD_TYPE_U32:
5762 *(u32 *)p = field_value;
5763 break;
5764 case VMCS_FIELD_TYPE_U64:
5765 *(u64 *)p = field_value;
5766 break;
5767 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
5768 *(natural_width *)p = field_value;
5769 break;
5770 default:
5771 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5772 skip_emulated_instruction(vcpu);
5773 return 1;
5774 }
5775
5776 nested_vmx_succeed(vcpu);
5777 skip_emulated_instruction(vcpu);
5778 return 1;
5779}
5780
Nadav Har'El63846662011-05-25 23:07:29 +03005781/* Emulate the VMPTRLD instruction */
5782static int handle_vmptrld(struct kvm_vcpu *vcpu)
5783{
5784 struct vcpu_vmx *vmx = to_vmx(vcpu);
5785 gva_t gva;
5786 gpa_t vmptr;
5787 struct x86_exception e;
5788
5789 if (!nested_vmx_check_permission(vcpu))
5790 return 1;
5791
5792 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5793 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5794 return 1;
5795
5796 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5797 sizeof(vmptr), &e)) {
5798 kvm_inject_page_fault(vcpu, &e);
5799 return 1;
5800 }
5801
5802 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
5803 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
5804 skip_emulated_instruction(vcpu);
5805 return 1;
5806 }
5807
5808 if (vmx->nested.current_vmptr != vmptr) {
5809 struct vmcs12 *new_vmcs12;
5810 struct page *page;
5811 page = nested_get_page(vcpu, vmptr);
5812 if (page == NULL) {
5813 nested_vmx_failInvalid(vcpu);
5814 skip_emulated_instruction(vcpu);
5815 return 1;
5816 }
5817 new_vmcs12 = kmap(page);
5818 if (new_vmcs12->revision_id != VMCS12_REVISION) {
5819 kunmap(page);
5820 nested_release_page_clean(page);
5821 nested_vmx_failValid(vcpu,
5822 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5823 skip_emulated_instruction(vcpu);
5824 return 1;
5825 }
5826 if (vmx->nested.current_vmptr != -1ull) {
5827 kunmap(vmx->nested.current_vmcs12_page);
5828 nested_release_page(vmx->nested.current_vmcs12_page);
5829 }
5830
5831 vmx->nested.current_vmptr = vmptr;
5832 vmx->nested.current_vmcs12 = new_vmcs12;
5833 vmx->nested.current_vmcs12_page = page;
5834 }
5835
5836 nested_vmx_succeed(vcpu);
5837 skip_emulated_instruction(vcpu);
5838 return 1;
5839}
5840
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005841/* Emulate the VMPTRST instruction */
5842static int handle_vmptrst(struct kvm_vcpu *vcpu)
5843{
5844 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5845 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5846 gva_t vmcs_gva;
5847 struct x86_exception e;
5848
5849 if (!nested_vmx_check_permission(vcpu))
5850 return 1;
5851
5852 if (get_vmx_mem_address(vcpu, exit_qualification,
5853 vmx_instruction_info, &vmcs_gva))
5854 return 1;
5855 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
5856 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
5857 (void *)&to_vmx(vcpu)->nested.current_vmptr,
5858 sizeof(u64), &e)) {
5859 kvm_inject_page_fault(vcpu, &e);
5860 return 1;
5861 }
5862 nested_vmx_succeed(vcpu);
5863 skip_emulated_instruction(vcpu);
5864 return 1;
5865}
5866
Nadav Har'El0140cae2011-05-25 23:06:28 +03005867/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005868 * The exit handlers return 1 if the exit was handled fully and guest execution
5869 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5870 * to be done to userspace and return 0.
5871 */
Mathias Krause772e0312012-08-30 01:30:19 +02005872static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005873 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
5874 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005875 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005876 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005877 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005878 [EXIT_REASON_CR_ACCESS] = handle_cr,
5879 [EXIT_REASON_DR_ACCESS] = handle_dr,
5880 [EXIT_REASON_CPUID] = handle_cpuid,
5881 [EXIT_REASON_MSR_READ] = handle_rdmsr,
5882 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
5883 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
5884 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005885 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005886 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02005887 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02005888 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03005889 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03005890 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03005891 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005892 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03005893 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03005894 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03005895 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005896 [EXIT_REASON_VMOFF] = handle_vmoff,
5897 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005898 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5899 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Yang Zhang83d4c282013-01-25 10:18:49 +08005900 [EXIT_REASON_APIC_WRITE] = handle_apic_write,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005901 [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
Eddie Donge5edaa02007-11-11 12:28:35 +02005902 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08005903 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005904 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005905 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005906 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5907 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005908 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08005909 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
5910 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005911};
5912
5913static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005914 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005915
Nadav Har'El644d7112011-05-25 23:12:35 +03005916/*
5917 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
5918 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5919 * disinterest in the current event (read or write a specific MSR) by using an
5920 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5921 */
5922static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
5923 struct vmcs12 *vmcs12, u32 exit_reason)
5924{
5925 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
5926 gpa_t bitmap;
5927
5928 if (!nested_cpu_has(get_vmcs12(vcpu), CPU_BASED_USE_MSR_BITMAPS))
5929 return 1;
5930
5931 /*
5932 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5933 * for the four combinations of read/write and low/high MSR numbers.
5934 * First we need to figure out which of the four to use:
5935 */
5936 bitmap = vmcs12->msr_bitmap;
5937 if (exit_reason == EXIT_REASON_MSR_WRITE)
5938 bitmap += 2048;
5939 if (msr_index >= 0xc0000000) {
5940 msr_index -= 0xc0000000;
5941 bitmap += 1024;
5942 }
5943
5944 /* Then read the msr_index'th bit from this bitmap: */
5945 if (msr_index < 1024*8) {
5946 unsigned char b;
5947 kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1);
5948 return 1 & (b >> (msr_index & 7));
5949 } else
5950 return 1; /* let L1 handle the wrong parameter */
5951}
5952
5953/*
5954 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5955 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5956 * intercept (via guest_host_mask etc.) the current event.
5957 */
5958static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5959 struct vmcs12 *vmcs12)
5960{
5961 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5962 int cr = exit_qualification & 15;
5963 int reg = (exit_qualification >> 8) & 15;
5964 unsigned long val = kvm_register_read(vcpu, reg);
5965
5966 switch ((exit_qualification >> 4) & 3) {
5967 case 0: /* mov to cr */
5968 switch (cr) {
5969 case 0:
5970 if (vmcs12->cr0_guest_host_mask &
5971 (val ^ vmcs12->cr0_read_shadow))
5972 return 1;
5973 break;
5974 case 3:
5975 if ((vmcs12->cr3_target_count >= 1 &&
5976 vmcs12->cr3_target_value0 == val) ||
5977 (vmcs12->cr3_target_count >= 2 &&
5978 vmcs12->cr3_target_value1 == val) ||
5979 (vmcs12->cr3_target_count >= 3 &&
5980 vmcs12->cr3_target_value2 == val) ||
5981 (vmcs12->cr3_target_count >= 4 &&
5982 vmcs12->cr3_target_value3 == val))
5983 return 0;
5984 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5985 return 1;
5986 break;
5987 case 4:
5988 if (vmcs12->cr4_guest_host_mask &
5989 (vmcs12->cr4_read_shadow ^ val))
5990 return 1;
5991 break;
5992 case 8:
5993 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5994 return 1;
5995 break;
5996 }
5997 break;
5998 case 2: /* clts */
5999 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
6000 (vmcs12->cr0_read_shadow & X86_CR0_TS))
6001 return 1;
6002 break;
6003 case 1: /* mov from cr */
6004 switch (cr) {
6005 case 3:
6006 if (vmcs12->cpu_based_vm_exec_control &
6007 CPU_BASED_CR3_STORE_EXITING)
6008 return 1;
6009 break;
6010 case 8:
6011 if (vmcs12->cpu_based_vm_exec_control &
6012 CPU_BASED_CR8_STORE_EXITING)
6013 return 1;
6014 break;
6015 }
6016 break;
6017 case 3: /* lmsw */
6018 /*
6019 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
6020 * cr0. Other attempted changes are ignored, with no exit.
6021 */
6022 if (vmcs12->cr0_guest_host_mask & 0xe &
6023 (val ^ vmcs12->cr0_read_shadow))
6024 return 1;
6025 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
6026 !(vmcs12->cr0_read_shadow & 0x1) &&
6027 (val & 0x1))
6028 return 1;
6029 break;
6030 }
6031 return 0;
6032}
6033
6034/*
6035 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
6036 * should handle it ourselves in L0 (and then continue L2). Only call this
6037 * when in is_guest_mode (L2).
6038 */
6039static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
6040{
6041 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
6042 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6043 struct vcpu_vmx *vmx = to_vmx(vcpu);
6044 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6045
6046 if (vmx->nested.nested_run_pending)
6047 return 0;
6048
6049 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02006050 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
6051 vmcs_read32(VM_INSTRUCTION_ERROR));
Nadav Har'El644d7112011-05-25 23:12:35 +03006052 return 1;
6053 }
6054
6055 switch (exit_reason) {
6056 case EXIT_REASON_EXCEPTION_NMI:
6057 if (!is_exception(intr_info))
6058 return 0;
6059 else if (is_page_fault(intr_info))
6060 return enable_ept;
6061 return vmcs12->exception_bitmap &
6062 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
6063 case EXIT_REASON_EXTERNAL_INTERRUPT:
6064 return 0;
6065 case EXIT_REASON_TRIPLE_FAULT:
6066 return 1;
6067 case EXIT_REASON_PENDING_INTERRUPT:
6068 case EXIT_REASON_NMI_WINDOW:
6069 /*
6070 * prepare_vmcs02() set the CPU_BASED_VIRTUAL_INTR_PENDING bit
6071 * (aka Interrupt Window Exiting) only when L1 turned it on,
6072 * so if we got a PENDING_INTERRUPT exit, this must be for L1.
6073 * Same for NMI Window Exiting.
6074 */
6075 return 1;
6076 case EXIT_REASON_TASK_SWITCH:
6077 return 1;
6078 case EXIT_REASON_CPUID:
6079 return 1;
6080 case EXIT_REASON_HLT:
6081 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
6082 case EXIT_REASON_INVD:
6083 return 1;
6084 case EXIT_REASON_INVLPG:
6085 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
6086 case EXIT_REASON_RDPMC:
6087 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
6088 case EXIT_REASON_RDTSC:
6089 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
6090 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
6091 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
6092 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
6093 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
6094 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
6095 /*
6096 * VMX instructions trap unconditionally. This allows L1 to
6097 * emulate them for its L2 guest, i.e., allows 3-level nesting!
6098 */
6099 return 1;
6100 case EXIT_REASON_CR_ACCESS:
6101 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
6102 case EXIT_REASON_DR_ACCESS:
6103 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
6104 case EXIT_REASON_IO_INSTRUCTION:
6105 /* TODO: support IO bitmaps */
6106 return 1;
6107 case EXIT_REASON_MSR_READ:
6108 case EXIT_REASON_MSR_WRITE:
6109 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
6110 case EXIT_REASON_INVALID_STATE:
6111 return 1;
6112 case EXIT_REASON_MWAIT_INSTRUCTION:
6113 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
6114 case EXIT_REASON_MONITOR_INSTRUCTION:
6115 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
6116 case EXIT_REASON_PAUSE_INSTRUCTION:
6117 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
6118 nested_cpu_has2(vmcs12,
6119 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
6120 case EXIT_REASON_MCE_DURING_VMENTRY:
6121 return 0;
6122 case EXIT_REASON_TPR_BELOW_THRESHOLD:
6123 return 1;
6124 case EXIT_REASON_APIC_ACCESS:
6125 return nested_cpu_has2(vmcs12,
6126 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
6127 case EXIT_REASON_EPT_VIOLATION:
6128 case EXIT_REASON_EPT_MISCONFIG:
6129 return 0;
6130 case EXIT_REASON_WBINVD:
6131 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
6132 case EXIT_REASON_XSETBV:
6133 return 1;
6134 default:
6135 return 1;
6136 }
6137}
6138
Avi Kivity586f9602010-11-18 13:09:54 +02006139static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
6140{
6141 *info1 = vmcs_readl(EXIT_QUALIFICATION);
6142 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
6143}
6144
Avi Kivity6aa8b732006-12-10 02:21:36 -08006145/*
6146 * The guest has exited. See if we can fix it or if we need userspace
6147 * assistance.
6148 */
Avi Kivity851ba692009-08-24 11:10:17 +03006149static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006150{
Avi Kivity29bd8a72007-09-10 17:27:03 +03006151 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08006152 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02006153 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03006154
Mohammed Gamal80ced182009-09-01 12:48:18 +02006155 /* If guest state is invalid, start emulating */
Gleb Natapov14168782013-01-21 15:36:49 +02006156 if (vmx->emulation_required)
Mohammed Gamal80ced182009-09-01 12:48:18 +02006157 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01006158
Nadav Har'Elb6f12502011-05-25 23:13:06 +03006159 /*
6160 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
6161 * we did not inject a still-pending event to L1 now because of
6162 * nested_run_pending, we need to re-enable this bit.
6163 */
6164 if (vmx->nested.nested_run_pending)
6165 kvm_make_request(KVM_REQ_EVENT, vcpu);
6166
Nadav Har'El509c75e2011-06-02 11:54:52 +03006167 if (!is_guest_mode(vcpu) && (exit_reason == EXIT_REASON_VMLAUNCH ||
6168 exit_reason == EXIT_REASON_VMRESUME))
Nadav Har'El644d7112011-05-25 23:12:35 +03006169 vmx->nested.nested_run_pending = 1;
6170 else
6171 vmx->nested.nested_run_pending = 0;
6172
6173 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
6174 nested_vmx_vmexit(vcpu);
6175 return 1;
6176 }
6177
Mohammed Gamal51207022010-05-31 22:40:54 +03006178 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
6179 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6180 vcpu->run->fail_entry.hardware_entry_failure_reason
6181 = exit_reason;
6182 return 0;
6183 }
6184
Avi Kivity29bd8a72007-09-10 17:27:03 +03006185 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03006186 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6187 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03006188 = vmcs_read32(VM_INSTRUCTION_ERROR);
6189 return 0;
6190 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08006191
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006192 /*
6193 * Note:
6194 * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6195 * delivery event since it indicates guest is accessing MMIO.
6196 * The vm-exit can be triggered again after return to guest that
6197 * will cause infinite loop.
6198 */
Mike Dayd77c26f2007-10-08 09:02:08 -04006199 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08006200 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02006201 exit_reason != EXIT_REASON_EPT_VIOLATION &&
Xiao Guangrongb9bf6882012-10-17 13:46:52 +08006202 exit_reason != EXIT_REASON_TASK_SWITCH)) {
6203 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6204 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
6205 vcpu->run->internal.ndata = 2;
6206 vcpu->run->internal.data[0] = vectoring_info;
6207 vcpu->run->internal.data[1] = exit_reason;
6208 return 0;
6209 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006210
Nadav Har'El644d7112011-05-25 23:12:35 +03006211 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
6212 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
6213 get_vmcs12(vcpu), vcpu)))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03006214 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006215 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006216 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01006217 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006218 /*
6219 * This CPU don't support us in finding the end of an
6220 * NMI-blocked window if the guest runs with IRQs
6221 * disabled. So we pull the trigger after 1 s of
6222 * futile waiting, but inform the user about this.
6223 */
6224 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6225 "state on VCPU %d after 1 s timeout\n",
6226 __func__, vcpu->vcpu_id);
6227 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006228 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006229 }
6230
Avi Kivity6aa8b732006-12-10 02:21:36 -08006231 if (exit_reason < kvm_vmx_max_exit_handlers
6232 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03006233 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006234 else {
Avi Kivity851ba692009-08-24 11:10:17 +03006235 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
6236 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006237 }
6238 return 0;
6239}
6240
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006241static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006242{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006243 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006244 vmcs_write32(TPR_THRESHOLD, 0);
6245 return;
6246 }
6247
Gleb Natapov95ba8273132009-04-21 17:45:08 +03006248 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08006249}
6250
Yang Zhang8d146952013-01-25 10:18:50 +08006251static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set)
6252{
6253 u32 sec_exec_control;
6254
6255 /*
6256 * There is not point to enable virtualize x2apic without enable
6257 * apicv
6258 */
Yang Zhangc7c9c562013-01-25 10:18:51 +08006259 if (!cpu_has_vmx_virtualize_x2apic_mode() ||
6260 !vmx_vm_has_apicv(vcpu->kvm))
Yang Zhang8d146952013-01-25 10:18:50 +08006261 return;
6262
6263 if (!vm_need_tpr_shadow(vcpu->kvm))
6264 return;
6265
6266 sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6267
6268 if (set) {
6269 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6270 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6271 } else {
6272 sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6273 sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6274 }
6275 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control);
6276
6277 vmx_set_msr_bitmap(vcpu);
6278}
6279
Yang Zhangc7c9c562013-01-25 10:18:51 +08006280static void vmx_hwapic_isr_update(struct kvm *kvm, int isr)
6281{
6282 u16 status;
6283 u8 old;
6284
6285 if (!vmx_vm_has_apicv(kvm))
6286 return;
6287
6288 if (isr == -1)
6289 isr = 0;
6290
6291 status = vmcs_read16(GUEST_INTR_STATUS);
6292 old = status >> 8;
6293 if (isr != old) {
6294 status &= 0xff;
6295 status |= isr << 8;
6296 vmcs_write16(GUEST_INTR_STATUS, status);
6297 }
6298}
6299
6300static void vmx_set_rvi(int vector)
6301{
6302 u16 status;
6303 u8 old;
6304
6305 status = vmcs_read16(GUEST_INTR_STATUS);
6306 old = (u8)status & 0xff;
6307 if ((u8)vector != old) {
6308 status &= ~0xff;
6309 status |= (u8)vector;
6310 vmcs_write16(GUEST_INTR_STATUS, status);
6311 }
6312}
6313
6314static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6315{
6316 if (max_irr == -1)
6317 return;
6318
6319 vmx_set_rvi(max_irr);
6320}
6321
6322static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
6323{
6324 vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
6325 vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
6326 vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
6327 vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
6328}
6329
Avi Kivity51aa01d2010-07-20 14:31:20 +03006330static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03006331{
Avi Kivity00eba012011-03-07 17:24:54 +02006332 u32 exit_intr_info;
6333
6334 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
6335 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
6336 return;
6337
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006338 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02006339 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08006340
6341 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02006342 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08006343 kvm_machine_check();
6344
Gleb Natapov20f65982009-05-11 13:35:55 +03006345 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02006346 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006347 (exit_intr_info & INTR_INFO_VALID_MASK)) {
6348 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03006349 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08006350 kvm_after_handle_nmi(&vmx->vcpu);
6351 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03006352}
Gleb Natapov20f65982009-05-11 13:35:55 +03006353
Avi Kivity51aa01d2010-07-20 14:31:20 +03006354static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6355{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006356 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03006357 bool unblock_nmi;
6358 u8 vector;
6359 bool idtv_info_valid;
6360
6361 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03006362
Avi Kivitycf393f72008-07-01 16:20:21 +03006363 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02006364 if (vmx->nmi_known_unmasked)
6365 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006366 /*
6367 * Can't use vmx->exit_intr_info since we're not sure what
6368 * the exit reason is.
6369 */
6370 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03006371 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6372 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6373 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006374 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03006375 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6376 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006377 * SDM 3: 23.2.2 (September 2008)
6378 * Bit 12 is undefined in any of the following cases:
6379 * If the VM exit sets the valid bit in the IDT-vectoring
6380 * information field.
6381 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03006382 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006383 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6384 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03006385 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6386 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02006387 else
6388 vmx->nmi_known_unmasked =
6389 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6390 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006391 } else if (unlikely(vmx->soft_vnmi_blocked))
6392 vmx->vnmi_blocked_time +=
6393 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03006394}
6395
Avi Kivity83422e12010-07-20 14:43:23 +03006396static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
6397 u32 idt_vectoring_info,
6398 int instr_len_field,
6399 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03006400{
Avi Kivity51aa01d2010-07-20 14:31:20 +03006401 u8 vector;
6402 int type;
6403 bool idtv_info_valid;
6404
6405 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03006406
Gleb Natapov37b96e92009-03-30 16:03:13 +03006407 vmx->vcpu.arch.nmi_injected = false;
6408 kvm_clear_exception_queue(&vmx->vcpu);
6409 kvm_clear_interrupt_queue(&vmx->vcpu);
6410
6411 if (!idtv_info_valid)
6412 return;
6413
Avi Kivity3842d132010-07-27 12:30:24 +03006414 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6415
Avi Kivity668f6122008-07-02 09:28:55 +03006416 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6417 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006418
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006419 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03006420 case INTR_TYPE_NMI_INTR:
6421 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03006422 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006423 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03006424 * Clear bit "block by NMI" before VM entry if a NMI
6425 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03006426 */
Avi Kivity654f06f2011-03-23 15:02:47 +02006427 vmx_set_nmi_mask(&vmx->vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006428 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006429 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006430 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03006431 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006432 /* fall through */
6433 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03006434 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03006435 u32 err = vmcs_read32(error_code_field);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006436 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03006437 } else
6438 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006439 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006440 case INTR_TYPE_SOFT_INTR:
6441 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03006442 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006443 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03006444 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006445 kvm_queue_interrupt(&vmx->vcpu, vector,
6446 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006447 break;
6448 default:
6449 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03006450 }
Avi Kivitycf393f72008-07-01 16:20:21 +03006451}
6452
Avi Kivity83422e12010-07-20 14:43:23 +03006453static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6454{
Nadav Har'El66c78ae2011-05-25 23:14:07 +03006455 if (is_guest_mode(&vmx->vcpu))
6456 return;
Avi Kivity83422e12010-07-20 14:43:23 +03006457 __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
6458 VM_EXIT_INSTRUCTION_LEN,
6459 IDT_VECTORING_ERROR_CODE);
6460}
6461
Avi Kivityb463a6f2010-07-20 15:06:17 +03006462static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6463{
Nadav Har'El66c78ae2011-05-25 23:14:07 +03006464 if (is_guest_mode(vcpu))
6465 return;
Avi Kivityb463a6f2010-07-20 15:06:17 +03006466 __vmx_complete_interrupts(to_vmx(vcpu),
6467 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6468 VM_ENTRY_INSTRUCTION_LEN,
6469 VM_ENTRY_EXCEPTION_ERROR_CODE);
6470
6471 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6472}
6473
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006474static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6475{
6476 int i, nr_msrs;
6477 struct perf_guest_switch_msr *msrs;
6478
6479 msrs = perf_guest_get_msrs(&nr_msrs);
6480
6481 if (!msrs)
6482 return;
6483
6484 for (i = 0; i < nr_msrs; i++)
6485 if (msrs[i].host == msrs[i].guest)
6486 clear_atomic_switch_msr(vmx, msrs[i].msr);
6487 else
6488 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
6489 msrs[i].host);
6490}
6491
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08006492static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006493{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006494 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006495 unsigned long debugctlmsr;
Avi Kivity104f2262010-11-18 13:12:52 +02006496
Nadav Har'El66c78ae2011-05-25 23:14:07 +03006497 if (is_guest_mode(vcpu) && !vmx->nested.nested_run_pending) {
6498 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6499 if (vmcs12->idt_vectoring_info_field &
6500 VECTORING_INFO_VALID_MASK) {
6501 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6502 vmcs12->idt_vectoring_info_field);
6503 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6504 vmcs12->vm_exit_instruction_len);
6505 if (vmcs12->idt_vectoring_info_field &
6506 VECTORING_INFO_DELIVER_CODE_MASK)
6507 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
6508 vmcs12->idt_vectoring_error_code);
6509 }
6510 }
6511
Avi Kivity104f2262010-11-18 13:12:52 +02006512 /* Record the guest's net vcpu time for enforced NMI injections. */
6513 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
6514 vmx->entry_time = ktime_get();
6515
6516 /* Don't enter VMX if guest state is invalid, let the exit handler
6517 start emulation until we arrive back to a valid state */
Gleb Natapov14168782013-01-21 15:36:49 +02006518 if (vmx->emulation_required)
Avi Kivity104f2262010-11-18 13:12:52 +02006519 return;
6520
6521 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
6522 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
6523 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
6524 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6525
6526 /* When single-stepping over STI and MOV SS, we must clear the
6527 * corresponding interruptibility bits in the guest state. Otherwise
6528 * vmentry fails as it then expects bit 14 (BS) in pending debug
6529 * exceptions being set, but that's not correct for the guest debugging
6530 * case. */
6531 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6532 vmx_set_interrupt_shadow(vcpu, 0);
6533
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006534 atomic_switch_perf_msrs(vmx);
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006535 debugctlmsr = get_debugctlmsr();
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006536
Nadav Har'Eld462b812011-05-24 15:26:10 +03006537 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02006538 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08006539 /* Store host registers */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006540 "push %%" _ASM_DX "; push %%" _ASM_BP ";"
6541 "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */
6542 "push %%" _ASM_CX " \n\t"
6543 "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03006544 "je 1f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006545 "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03006546 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03006547 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03006548 /* Reload cr2 if changed */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006549 "mov %c[cr2](%0), %%" _ASM_AX " \n\t"
6550 "mov %%cr2, %%" _ASM_DX " \n\t"
6551 "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03006552 "je 2f \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006553 "mov %%" _ASM_AX", %%cr2 \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03006554 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006555 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02006556 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006557 /* Load guest registers. Don't clobber flags. */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006558 "mov %c[rax](%0), %%" _ASM_AX " \n\t"
6559 "mov %c[rbx](%0), %%" _ASM_BX " \n\t"
6560 "mov %c[rdx](%0), %%" _ASM_DX " \n\t"
6561 "mov %c[rsi](%0), %%" _ASM_SI " \n\t"
6562 "mov %c[rdi](%0), %%" _ASM_DI " \n\t"
6563 "mov %c[rbp](%0), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006564#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006565 "mov %c[r8](%0), %%r8 \n\t"
6566 "mov %c[r9](%0), %%r9 \n\t"
6567 "mov %c[r10](%0), %%r10 \n\t"
6568 "mov %c[r11](%0), %%r11 \n\t"
6569 "mov %c[r12](%0), %%r12 \n\t"
6570 "mov %c[r13](%0), %%r13 \n\t"
6571 "mov %c[r14](%0), %%r14 \n\t"
6572 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006573#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03006574 "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */
Avi Kivityc8019492008-07-14 14:44:59 +03006575
Avi Kivity6aa8b732006-12-10 02:21:36 -08006576 /* Enter guest mode */
Avi Kivity83287ea422012-09-16 15:10:57 +03006577 "jne 1f \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03006578 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006579 "jmp 2f \n\t"
6580 "1: " __ex(ASM_VMX_VMRESUME) "\n\t"
6581 "2: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08006582 /* Save guest registers, load host registers, keep flags */
Avi Kivityb188c81f2012-09-16 15:10:58 +03006583 "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t"
Avi Kivity40712fa2011-01-06 18:09:12 +02006584 "pop %0 \n\t"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006585 "mov %%" _ASM_AX ", %c[rax](%0) \n\t"
6586 "mov %%" _ASM_BX ", %c[rbx](%0) \n\t"
6587 __ASM_SIZE(pop) " %c[rcx](%0) \n\t"
6588 "mov %%" _ASM_DX ", %c[rdx](%0) \n\t"
6589 "mov %%" _ASM_SI ", %c[rsi](%0) \n\t"
6590 "mov %%" _ASM_DI ", %c[rdi](%0) \n\t"
6591 "mov %%" _ASM_BP ", %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006592#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006593 "mov %%r8, %c[r8](%0) \n\t"
6594 "mov %%r9, %c[r9](%0) \n\t"
6595 "mov %%r10, %c[r10](%0) \n\t"
6596 "mov %%r11, %c[r11](%0) \n\t"
6597 "mov %%r12, %c[r12](%0) \n\t"
6598 "mov %%r13, %c[r13](%0) \n\t"
6599 "mov %%r14, %c[r14](%0) \n\t"
6600 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006601#endif
Avi Kivityb188c81f2012-09-16 15:10:58 +03006602 "mov %%cr2, %%" _ASM_AX " \n\t"
6603 "mov %%" _ASM_AX ", %c[cr2](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03006604
Avi Kivityb188c81f2012-09-16 15:10:58 +03006605 "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02006606 "setbe %c[fail](%0) \n\t"
Avi Kivity83287ea422012-09-16 15:10:57 +03006607 ".pushsection .rodata \n\t"
6608 ".global vmx_return \n\t"
6609 "vmx_return: " _ASM_PTR " 2b \n\t"
6610 ".popsection"
Avi Kivitye08aa782007-11-15 18:06:18 +02006611 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03006612 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02006613 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03006614 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006615 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
6616 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
6617 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
6618 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
6619 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
6620 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
6621 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006622#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006623 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
6624 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
6625 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
6626 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
6627 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
6628 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
6629 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
6630 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08006631#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02006632 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
6633 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02006634 : "cc", "memory"
6635#ifdef CONFIG_X86_64
Avi Kivityb188c81f2012-09-16 15:10:58 +03006636 , "rax", "rbx", "rdi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02006637 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Avi Kivityb188c81f2012-09-16 15:10:58 +03006638#else
6639 , "eax", "ebx", "edi", "esi"
Laurent Vivierc2036302007-10-25 14:18:52 +02006640#endif
6641 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08006642
Gleb Natapov2a7921b2012-08-12 16:12:29 +03006643 /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
6644 if (debugctlmsr)
6645 update_debugctlmsr(debugctlmsr);
6646
Avi Kivityaa67f602012-08-01 16:48:03 +03006647#ifndef CONFIG_X86_64
6648 /*
6649 * The sysexit path does not restore ds/es, so we must set them to
6650 * a reasonable value ourselves.
6651 *
6652 * We can't defer this to vmx_load_host_state() since that function
6653 * may be executed in interrupt context, which saves and restore segments
6654 * around it, nullifying its effect.
6655 */
6656 loadsegment(ds, __USER_DS);
6657 loadsegment(es, __USER_DS);
6658#endif
6659
Avi Kivity6de4f3a2009-05-31 22:58:47 +03006660 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02006661 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivity69c73022011-03-07 15:26:44 +02006662 | (1 << VCPU_EXREG_CPL)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006663 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03006664 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006665 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006666 vcpu->arch.regs_dirty = 0;
6667
Avi Kivity1155f762007-11-22 11:30:47 +02006668 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
6669
Nadav Har'El66c78ae2011-05-25 23:14:07 +03006670 if (is_guest_mode(vcpu)) {
6671 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6672 vmcs12->idt_vectoring_info_field = vmx->idt_vectoring_info;
6673 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
6674 vmcs12->idt_vectoring_error_code =
6675 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6676 vmcs12->vm_exit_instruction_len =
6677 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6678 }
6679 }
6680
Nadav Har'Eld462b812011-05-24 15:26:10 +03006681 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02006682
Avi Kivity51aa01d2010-07-20 14:31:20 +03006683 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02006684 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
Avi Kivity51aa01d2010-07-20 14:31:20 +03006685
6686 vmx_complete_atomic_exit(vmx);
6687 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006688 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006689}
6690
Avi Kivity6aa8b732006-12-10 02:21:36 -08006691static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6692{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006693 struct vcpu_vmx *vmx = to_vmx(vcpu);
6694
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08006695 free_vpid(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006696 free_nested(vmx);
Nadav Har'Eld462b812011-05-24 15:26:10 +03006697 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006698 kfree(vmx->guest_msrs);
6699 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10006700 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006701}
6702
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006703static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006704{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006705 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10006706 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03006707 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006708
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006709 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006710 return ERR_PTR(-ENOMEM);
6711
Sheng Yang2384d2b2008-01-17 15:14:33 +08006712 allocate_vpid(vmx);
6713
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006714 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
6715 if (err)
6716 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006717
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006718 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006719 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006720 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006721 goto uninit_vcpu;
6722 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08006723
Nadav Har'Eld462b812011-05-24 15:26:10 +03006724 vmx->loaded_vmcs = &vmx->vmcs01;
6725 vmx->loaded_vmcs->vmcs = alloc_vmcs();
6726 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006727 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03006728 if (!vmm_exclusive)
6729 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
6730 loaded_vmcs_init(vmx->loaded_vmcs);
6731 if (!vmm_exclusive)
6732 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006733
Avi Kivity15ad7142007-07-11 18:17:21 +03006734 cpu = get_cpu();
6735 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10006736 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10006737 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006738 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03006739 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006740 if (err)
6741 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006742 if (vm_need_virtualize_apic_accesses(kvm))
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006743 err = alloc_apic_access_page(kvm);
6744 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006745 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006746
Sheng Yangb927a3c2009-07-21 10:42:48 +08006747 if (enable_ept) {
6748 if (!kvm->arch.ept_identity_map_addr)
6749 kvm->arch.ept_identity_map_addr =
6750 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Gleb Natapov93ea5382011-02-21 12:07:59 +02006751 err = -ENOMEM;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006752 if (alloc_identity_pagetable(kvm) != 0)
6753 goto free_vmcs;
Gleb Natapov93ea5382011-02-21 12:07:59 +02006754 if (!init_rmode_identity_map(kvm))
6755 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08006756 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006757
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006758 vmx->nested.current_vmptr = -1ull;
6759 vmx->nested.current_vmcs12 = NULL;
6760
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006761 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006762
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006763free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08006764 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006765free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006766 kfree(vmx->guest_msrs);
6767uninit_vcpu:
6768 kvm_vcpu_uninit(&vmx->vcpu);
6769free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08006770 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10006771 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006772 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006773}
6774
Yang, Sheng002c7f72007-07-31 14:23:01 +03006775static void __init vmx_check_processor_compat(void *rtn)
6776{
6777 struct vmcs_config vmcs_conf;
6778
6779 *(int *)rtn = 0;
6780 if (setup_vmcs_config(&vmcs_conf) < 0)
6781 *(int *)rtn = -EIO;
6782 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
6783 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
6784 smp_processor_id());
6785 *(int *)rtn = -EIO;
6786 }
6787}
6788
Sheng Yang67253af2008-04-25 10:20:22 +08006789static int get_ept_level(void)
6790{
6791 return VMX_EPT_DEFAULT_GAW + 1;
6792}
6793
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006794static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08006795{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006796 u64 ret;
6797
Sheng Yang522c68c2009-04-27 20:35:43 +08006798 /* For VT-d and EPT combination
6799 * 1. MMIO: always map as UC
6800 * 2. EPT with VT-d:
6801 * a. VT-d without snooping control feature: can't guarantee the
6802 * result, try to trust guest.
6803 * b. VT-d with snooping control feature: snooping control feature of
6804 * VT-d engine can guarantee the cache correctness. Just set it
6805 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08006806 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08006807 * consistent with host MTRR
6808 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006809 if (is_mmio)
6810 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08006811 else if (vcpu->kvm->arch.iommu_domain &&
6812 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
6813 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
6814 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006815 else
Sheng Yang522c68c2009-04-27 20:35:43 +08006816 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08006817 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006818
6819 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08006820}
6821
Sheng Yang17cc3932010-01-05 19:02:27 +08006822static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02006823{
Sheng Yang878403b2010-01-05 19:02:29 +08006824 if (enable_ept && !cpu_has_vmx_ept_1g_page())
6825 return PT_DIRECTORY_LEVEL;
6826 else
6827 /* For shadow and EPT supported 1GB page */
6828 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02006829}
6830
Sheng Yang0e851882009-12-18 16:48:46 +08006831static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
6832{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006833 struct kvm_cpuid_entry2 *best;
6834 struct vcpu_vmx *vmx = to_vmx(vcpu);
6835 u32 exec_control;
6836
6837 vmx->rdtscp_enabled = false;
6838 if (vmx_rdtscp_supported()) {
6839 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6840 if (exec_control & SECONDARY_EXEC_RDTSCP) {
6841 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
6842 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
6843 vmx->rdtscp_enabled = true;
6844 else {
6845 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6846 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6847 exec_control);
6848 }
6849 }
6850 }
Mao, Junjiead756a12012-07-02 01:18:48 +00006851
Mao, Junjiead756a12012-07-02 01:18:48 +00006852 /* Exposing INVPCID only when PCID is exposed */
6853 best = kvm_find_cpuid_entry(vcpu, 0x7, 0);
6854 if (vmx_invpcid_supported() &&
Ren, Yongjie4f977042012-09-07 07:36:59 +00006855 best && (best->ebx & bit(X86_FEATURE_INVPCID)) &&
Mao, Junjiead756a12012-07-02 01:18:48 +00006856 guest_cpuid_has_pcid(vcpu)) {
Takashi Iwai29282fd2012-11-09 15:20:17 +01006857 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
Mao, Junjiead756a12012-07-02 01:18:48 +00006858 exec_control |= SECONDARY_EXEC_ENABLE_INVPCID;
6859 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6860 exec_control);
6861 } else {
Takashi Iwai29282fd2012-11-09 15:20:17 +01006862 if (cpu_has_secondary_exec_ctrls()) {
6863 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6864 exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
6865 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6866 exec_control);
6867 }
Mao, Junjiead756a12012-07-02 01:18:48 +00006868 if (best)
Ren, Yongjie4f977042012-09-07 07:36:59 +00006869 best->ebx &= ~bit(X86_FEATURE_INVPCID);
Mao, Junjiead756a12012-07-02 01:18:48 +00006870 }
Sheng Yang0e851882009-12-18 16:48:46 +08006871}
6872
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006873static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
6874{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03006875 if (func == 1 && nested)
6876 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006877}
6878
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006879/*
6880 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
6881 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
6882 * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2
6883 * guest in a way that will both be appropriate to L1's requests, and our
6884 * needs. In addition to modifying the active vmcs (which is vmcs02), this
6885 * function also has additional necessary side-effects, like setting various
6886 * vcpu->arch fields.
6887 */
6888static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
6889{
6890 struct vcpu_vmx *vmx = to_vmx(vcpu);
6891 u32 exec_control;
6892
6893 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
6894 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
6895 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
6896 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
6897 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
6898 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
6899 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
6900 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
6901 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
6902 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
6903 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
6904 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
6905 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
6906 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
6907 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
6908 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
6909 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
6910 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
6911 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
6912 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
6913 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
6914 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
6915 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
6916 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
6917 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
6918 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
6919 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
6920 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
6921 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
6922 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
6923 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
6924 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
6925 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
6926 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
6927 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
6928 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
6929
6930 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
6931 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6932 vmcs12->vm_entry_intr_info_field);
6933 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
6934 vmcs12->vm_entry_exception_error_code);
6935 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6936 vmcs12->vm_entry_instruction_len);
6937 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
6938 vmcs12->guest_interruptibility_info);
6939 vmcs_write32(GUEST_ACTIVITY_STATE, vmcs12->guest_activity_state);
6940 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
6941 vmcs_writel(GUEST_DR7, vmcs12->guest_dr7);
6942 vmcs_writel(GUEST_RFLAGS, vmcs12->guest_rflags);
6943 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
6944 vmcs12->guest_pending_dbg_exceptions);
6945 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
6946 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
6947
6948 vmcs_write64(VMCS_LINK_POINTER, -1ull);
6949
6950 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
6951 (vmcs_config.pin_based_exec_ctrl |
6952 vmcs12->pin_based_vm_exec_control));
6953
6954 /*
6955 * Whether page-faults are trapped is determined by a combination of
6956 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
6957 * If enable_ept, L0 doesn't care about page faults and we should
6958 * set all of these to L1's desires. However, if !enable_ept, L0 does
6959 * care about (at least some) page faults, and because it is not easy
6960 * (if at all possible?) to merge L0 and L1's desires, we simply ask
6961 * to exit on each and every L2 page fault. This is done by setting
6962 * MASK=MATCH=0 and (see below) EB.PF=1.
6963 * Note that below we don't need special code to set EB.PF beyond the
6964 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
6965 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
6966 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
6967 *
6968 * A problem with this approach (when !enable_ept) is that L1 may be
6969 * injected with more page faults than it asked for. This could have
6970 * caused problems, but in practice existing hypervisors don't care.
6971 * To fix this, we will need to emulate the PFEC checking (on the L1
6972 * page tables), using walk_addr(), when injecting PFs to L1.
6973 */
6974 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
6975 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
6976 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
6977 enable_ept ? vmcs12->page_fault_error_code_match : 0);
6978
6979 if (cpu_has_secondary_exec_ctrls()) {
6980 u32 exec_control = vmx_secondary_exec_control(vmx);
6981 if (!vmx->rdtscp_enabled)
6982 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6983 /* Take the following fields only from vmcs12 */
6984 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6985 if (nested_cpu_has(vmcs12,
6986 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
6987 exec_control |= vmcs12->secondary_vm_exec_control;
6988
6989 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
6990 /*
6991 * Translate L1 physical address to host physical
6992 * address for vmcs02. Keep the page pinned, so this
6993 * physical address remains valid. We keep a reference
6994 * to it so we can release it later.
6995 */
6996 if (vmx->nested.apic_access_page) /* shouldn't happen */
6997 nested_release_page(vmx->nested.apic_access_page);
6998 vmx->nested.apic_access_page =
6999 nested_get_page(vcpu, vmcs12->apic_access_addr);
7000 /*
7001 * If translation failed, no matter: This feature asks
7002 * to exit when accessing the given address, and if it
7003 * can never be accessed, this feature won't do
7004 * anything anyway.
7005 */
7006 if (!vmx->nested.apic_access_page)
7007 exec_control &=
7008 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
7009 else
7010 vmcs_write64(APIC_ACCESS_ADDR,
7011 page_to_phys(vmx->nested.apic_access_page));
7012 }
7013
7014 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
7015 }
7016
7017
7018 /*
7019 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
7020 * Some constant fields are set here by vmx_set_constant_host_state().
7021 * Other fields are different per CPU, and will be set later when
7022 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
7023 */
7024 vmx_set_constant_host_state();
7025
7026 /*
7027 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
7028 * entry, but only if the current (host) sp changed from the value
7029 * we wrote last (vmx->host_rsp). This cache is no longer relevant
7030 * if we switch vmcs, and rather than hold a separate cache per vmcs,
7031 * here we just force the write to happen on entry.
7032 */
7033 vmx->host_rsp = 0;
7034
7035 exec_control = vmx_exec_control(vmx); /* L0's desires */
7036 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
7037 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
7038 exec_control &= ~CPU_BASED_TPR_SHADOW;
7039 exec_control |= vmcs12->cpu_based_vm_exec_control;
7040 /*
7041 * Merging of IO and MSR bitmaps not currently supported.
7042 * Rather, exit every time.
7043 */
7044 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
7045 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
7046 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
7047
7048 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
7049
7050 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
7051 * bitwise-or of what L1 wants to trap for L2, and what we want to
7052 * trap. Note that CR0.TS also needs updating - we do this later.
7053 */
7054 update_exception_bitmap(vcpu);
7055 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
7056 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
7057
7058 /* Note: IA32_MODE, LOAD_IA32_EFER are modified by vmx_set_efer below */
7059 vmcs_write32(VM_EXIT_CONTROLS,
7060 vmcs12->vm_exit_controls | vmcs_config.vmexit_ctrl);
7061 vmcs_write32(VM_ENTRY_CONTROLS, vmcs12->vm_entry_controls |
7062 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
7063
7064 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)
7065 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
7066 else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
7067 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
7068
7069
7070 set_cr4_guest_host_mask(vmx);
7071
Nadav Har'El27fc51b2011-08-02 15:54:52 +03007072 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
7073 vmcs_write64(TSC_OFFSET,
7074 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
7075 else
7076 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03007077
7078 if (enable_vpid) {
7079 /*
7080 * Trivially support vpid by letting L2s share their parent
7081 * L1's vpid. TODO: move to a more elaborate solution, giving
7082 * each L2 its own vpid and exposing the vpid feature to L1.
7083 */
7084 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
7085 vmx_flush_tlb(vcpu);
7086 }
7087
7088 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
7089 vcpu->arch.efer = vmcs12->guest_ia32_efer;
7090 if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
7091 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
7092 else
7093 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
7094 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
7095 vmx_set_efer(vcpu, vcpu->arch.efer);
7096
7097 /*
7098 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
7099 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
7100 * The CR0_READ_SHADOW is what L2 should have expected to read given
7101 * the specifications by L1; It's not enough to take
7102 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
7103 * have more bits than L1 expected.
7104 */
7105 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
7106 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
7107
7108 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
7109 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
7110
7111 /* shadow page tables on either EPT or shadow page tables */
7112 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
7113 kvm_mmu_reset_context(vcpu);
7114
7115 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
7116 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
7117}
7118
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007119/*
7120 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
7121 * for running an L2 nested guest.
7122 */
7123static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
7124{
7125 struct vmcs12 *vmcs12;
7126 struct vcpu_vmx *vmx = to_vmx(vcpu);
7127 int cpu;
7128 struct loaded_vmcs *vmcs02;
7129
7130 if (!nested_vmx_check_permission(vcpu) ||
7131 !nested_vmx_check_vmcs12(vcpu))
7132 return 1;
7133
7134 skip_emulated_instruction(vcpu);
7135 vmcs12 = get_vmcs12(vcpu);
7136
Nadav Har'El7c177932011-05-25 23:12:04 +03007137 /*
7138 * The nested entry process starts with enforcing various prerequisites
7139 * on vmcs12 as required by the Intel SDM, and act appropriately when
7140 * they fail: As the SDM explains, some conditions should cause the
7141 * instruction to fail, while others will cause the instruction to seem
7142 * to succeed, but return an EXIT_REASON_INVALID_STATE.
7143 * To speed up the normal (success) code path, we should avoid checking
7144 * for misconfigurations which will anyway be caught by the processor
7145 * when using the merged vmcs02.
7146 */
7147 if (vmcs12->launch_state == launch) {
7148 nested_vmx_failValid(vcpu,
7149 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
7150 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
7151 return 1;
7152 }
7153
7154 if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
7155 !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) {
7156 /*TODO: Also verify bits beyond physical address width are 0*/
7157 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7158 return 1;
7159 }
7160
7161 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
7162 !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) {
7163 /*TODO: Also verify bits beyond physical address width are 0*/
7164 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7165 return 1;
7166 }
7167
7168 if (vmcs12->vm_entry_msr_load_count > 0 ||
7169 vmcs12->vm_exit_msr_load_count > 0 ||
7170 vmcs12->vm_exit_msr_store_count > 0) {
Jan Kiszkabd801582011-09-12 11:26:22 +02007171 pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n",
7172 __func__);
Nadav Har'El7c177932011-05-25 23:12:04 +03007173 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7174 return 1;
7175 }
7176
7177 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
7178 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) ||
7179 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
7180 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
7181 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
7182 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
7183 !vmx_control_verify(vmcs12->vm_exit_controls,
7184 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) ||
7185 !vmx_control_verify(vmcs12->vm_entry_controls,
7186 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high))
7187 {
7188 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
7189 return 1;
7190 }
7191
7192 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
7193 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
7194 nested_vmx_failValid(vcpu,
7195 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
7196 return 1;
7197 }
7198
7199 if (((vmcs12->guest_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
7200 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
7201 nested_vmx_entry_failure(vcpu, vmcs12,
7202 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
7203 return 1;
7204 }
7205 if (vmcs12->vmcs_link_pointer != -1ull) {
7206 nested_vmx_entry_failure(vcpu, vmcs12,
7207 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
7208 return 1;
7209 }
7210
7211 /*
7212 * We're finally done with prerequisite checking, and can start with
7213 * the nested entry.
7214 */
7215
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03007216 vmcs02 = nested_get_current_vmcs02(vmx);
7217 if (!vmcs02)
7218 return -ENOMEM;
7219
7220 enter_guest_mode(vcpu);
7221
7222 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
7223
7224 cpu = get_cpu();
7225 vmx->loaded_vmcs = vmcs02;
7226 vmx_vcpu_put(vcpu);
7227 vmx_vcpu_load(vcpu, cpu);
7228 vcpu->cpu = cpu;
7229 put_cpu();
7230
7231 vmcs12->launch_state = 1;
7232
7233 prepare_vmcs02(vcpu, vmcs12);
7234
7235 /*
7236 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
7237 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
7238 * returned as far as L1 is concerned. It will only return (and set
7239 * the success flag) when L2 exits (see nested_vmx_vmexit()).
7240 */
7241 return 1;
7242}
7243
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007244/*
7245 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
7246 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
7247 * This function returns the new value we should put in vmcs12.guest_cr0.
7248 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
7249 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
7250 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
7251 * didn't trap the bit, because if L1 did, so would L0).
7252 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
7253 * been modified by L2, and L1 knows it. So just leave the old value of
7254 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
7255 * isn't relevant, because if L0 traps this bit it can set it to anything.
7256 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
7257 * changed these bits, and therefore they need to be updated, but L0
7258 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
7259 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
7260 */
7261static inline unsigned long
7262vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7263{
7264 return
7265 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
7266 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
7267 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
7268 vcpu->arch.cr0_guest_owned_bits));
7269}
7270
7271static inline unsigned long
7272vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7273{
7274 return
7275 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
7276 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
7277 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
7278 vcpu->arch.cr4_guest_owned_bits));
7279}
7280
7281/*
7282 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
7283 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
7284 * and this function updates it to reflect the changes to the guest state while
7285 * L2 was running (and perhaps made some exits which were handled directly by L0
7286 * without going back to L1), and to reflect the exit reason.
7287 * Note that we do not have to copy here all VMCS fields, just those that
7288 * could have changed by the L2 guest or the exit - i.e., the guest-state and
7289 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
7290 * which already writes to vmcs12 directly.
7291 */
7292void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7293{
7294 /* update guest state fields: */
7295 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
7296 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
7297
7298 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
7299 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
7300 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
7301 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
7302
7303 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
7304 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
7305 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
7306 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
7307 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
7308 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
7309 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
7310 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
7311 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
7312 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
7313 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
7314 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
7315 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
7316 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
7317 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
7318 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
7319 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
7320 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
7321 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
7322 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
7323 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
7324 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
7325 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
7326 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
7327 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
7328 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
7329 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
7330 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
7331 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
7332 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
7333 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
7334 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
7335 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
7336 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
7337 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
7338 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
7339
7340 vmcs12->guest_activity_state = vmcs_read32(GUEST_ACTIVITY_STATE);
7341 vmcs12->guest_interruptibility_info =
7342 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
7343 vmcs12->guest_pending_dbg_exceptions =
7344 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
7345
7346 /* TODO: These cannot have changed unless we have MSR bitmaps and
7347 * the relevant bit asks not to trap the change */
7348 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
7349 if (vmcs12->vm_entry_controls & VM_EXIT_SAVE_IA32_PAT)
7350 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
7351 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
7352 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
7353 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
7354
7355 /* update exit information fields: */
7356
7357 vmcs12->vm_exit_reason = vmcs_read32(VM_EXIT_REASON);
7358 vmcs12->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
7359
7360 vmcs12->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7361 vmcs12->vm_exit_intr_error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
7362 vmcs12->idt_vectoring_info_field =
7363 vmcs_read32(IDT_VECTORING_INFO_FIELD);
7364 vmcs12->idt_vectoring_error_code =
7365 vmcs_read32(IDT_VECTORING_ERROR_CODE);
7366 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
7367 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
7368
7369 /* clear vm-entry fields which are to be cleared on exit */
7370 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
7371 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
7372}
7373
7374/*
7375 * A part of what we need to when the nested L2 guest exits and we want to
7376 * run its L1 parent, is to reset L1's guest state to the host state specified
7377 * in vmcs12.
7378 * This function is to be called not only on normal nested exit, but also on
7379 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
7380 * Failures During or After Loading Guest State").
7381 * This function should be called when the active VMCS is L1's (vmcs01).
7382 */
7383void load_vmcs12_host_state(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
7384{
7385 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
7386 vcpu->arch.efer = vmcs12->host_ia32_efer;
7387 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
7388 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
7389 else
7390 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
7391 vmx_set_efer(vcpu, vcpu->arch.efer);
7392
7393 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
7394 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
7395 /*
7396 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
7397 * actually changed, because it depends on the current state of
7398 * fpu_active (which may have changed).
7399 * Note that vmx_set_cr0 refers to efer set above.
7400 */
7401 kvm_set_cr0(vcpu, vmcs12->host_cr0);
7402 /*
7403 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
7404 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
7405 * but we also need to update cr0_guest_host_mask and exception_bitmap.
7406 */
7407 update_exception_bitmap(vcpu);
7408 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
7409 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
7410
7411 /*
7412 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
7413 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
7414 */
7415 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
7416 kvm_set_cr4(vcpu, vmcs12->host_cr4);
7417
7418 /* shadow page tables on either EPT or shadow page tables */
7419 kvm_set_cr3(vcpu, vmcs12->host_cr3);
7420 kvm_mmu_reset_context(vcpu);
7421
7422 if (enable_vpid) {
7423 /*
7424 * Trivially support vpid by letting L2s share their parent
7425 * L1's vpid. TODO: move to a more elaborate solution, giving
7426 * each L2 its own vpid and exposing the vpid feature to L1.
7427 */
7428 vmx_flush_tlb(vcpu);
7429 }
7430
7431
7432 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
7433 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
7434 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
7435 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
7436 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
7437 vmcs_writel(GUEST_TR_BASE, vmcs12->host_tr_base);
7438 vmcs_writel(GUEST_GS_BASE, vmcs12->host_gs_base);
7439 vmcs_writel(GUEST_FS_BASE, vmcs12->host_fs_base);
7440 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->host_es_selector);
7441 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->host_cs_selector);
7442 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->host_ss_selector);
7443 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->host_ds_selector);
7444 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->host_fs_selector);
7445 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->host_gs_selector);
7446 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->host_tr_selector);
7447
7448 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT)
7449 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
7450 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
7451 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
7452 vmcs12->host_ia32_perf_global_ctrl);
7453}
7454
7455/*
7456 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
7457 * and modify vmcs12 to make it see what it would expect to see there if
7458 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
7459 */
7460static void nested_vmx_vmexit(struct kvm_vcpu *vcpu)
7461{
7462 struct vcpu_vmx *vmx = to_vmx(vcpu);
7463 int cpu;
7464 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7465
7466 leave_guest_mode(vcpu);
7467 prepare_vmcs12(vcpu, vmcs12);
7468
7469 cpu = get_cpu();
7470 vmx->loaded_vmcs = &vmx->vmcs01;
7471 vmx_vcpu_put(vcpu);
7472 vmx_vcpu_load(vcpu, cpu);
7473 vcpu->cpu = cpu;
7474 put_cpu();
7475
7476 /* if no vmcs02 cache requested, remove the one we used */
7477 if (VMCS02_POOL_SIZE == 0)
7478 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
7479
7480 load_vmcs12_host_state(vcpu, vmcs12);
7481
Nadav Har'El27fc51b2011-08-02 15:54:52 +03007482 /* Update TSC_OFFSET if TSC was changed while L2 ran */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007483 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
7484
7485 /* This is needed for same reason as it was needed in prepare_vmcs02 */
7486 vmx->host_rsp = 0;
7487
7488 /* Unpin physical memory we referred to in vmcs02 */
7489 if (vmx->nested.apic_access_page) {
7490 nested_release_page(vmx->nested.apic_access_page);
7491 vmx->nested.apic_access_page = 0;
7492 }
7493
7494 /*
7495 * Exiting from L2 to L1, we're now back to L1 which thinks it just
7496 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
7497 * success or failure flag accordingly.
7498 */
7499 if (unlikely(vmx->fail)) {
7500 vmx->fail = 0;
7501 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
7502 } else
7503 nested_vmx_succeed(vcpu);
7504}
7505
Nadav Har'El7c177932011-05-25 23:12:04 +03007506/*
7507 * L1's failure to enter L2 is a subset of a normal exit, as explained in
7508 * 23.7 "VM-entry failures during or after loading guest state" (this also
7509 * lists the acceptable exit-reason and exit-qualification parameters).
7510 * It should only be called before L2 actually succeeded to run, and when
7511 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
7512 */
7513static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
7514 struct vmcs12 *vmcs12,
7515 u32 reason, unsigned long qualification)
7516{
7517 load_vmcs12_host_state(vcpu, vmcs12);
7518 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
7519 vmcs12->exit_qualification = qualification;
7520 nested_vmx_succeed(vcpu);
7521}
7522
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007523static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7524 struct x86_instruction_info *info,
7525 enum x86_intercept_stage stage)
7526{
7527 return X86EMUL_CONTINUE;
7528}
7529
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03007530static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007531 .cpu_has_kvm_support = cpu_has_kvm_support,
7532 .disabled_by_bios = vmx_disabled_by_bios,
7533 .hardware_setup = hardware_setup,
7534 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03007535 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007536 .hardware_enable = hardware_enable,
7537 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007538 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007539
7540 .vcpu_create = vmx_create_vcpu,
7541 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007542 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007543
Avi Kivity04d2cc72007-09-10 18:10:54 +03007544 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007545 .vcpu_load = vmx_vcpu_load,
7546 .vcpu_put = vmx_vcpu_put,
7547
Jan Kiszkac8639012012-09-21 05:42:55 +02007548 .update_db_bp_intercept = update_exception_bitmap,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007549 .get_msr = vmx_get_msr,
7550 .set_msr = vmx_set_msr,
7551 .get_segment_base = vmx_get_segment_base,
7552 .get_segment = vmx_get_segment,
7553 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007554 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007555 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02007556 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02007557 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03007558 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007559 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007560 .set_cr3 = vmx_set_cr3,
7561 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007562 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007563 .get_idt = vmx_get_idt,
7564 .set_idt = vmx_set_idt,
7565 .get_gdt = vmx_get_gdt,
7566 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03007567 .set_dr7 = vmx_set_dr7,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007568 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007569 .get_rflags = vmx_get_rflags,
7570 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02007571 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02007572 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007573
7574 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007575
Avi Kivity6aa8b732006-12-10 02:21:36 -08007576 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007577 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007578 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007579 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7580 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007581 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007582 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007583 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007584 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007585 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007586 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007587 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007588 .get_nmi_mask = vmx_get_nmi_mask,
7589 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007590 .enable_nmi_window = enable_nmi_window,
7591 .enable_irq_window = enable_irq_window,
7592 .update_cr8_intercept = update_cr8_intercept,
Yang Zhang8d146952013-01-25 10:18:50 +08007593 .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode,
Yang Zhangc7c9c562013-01-25 10:18:51 +08007594 .vm_has_apicv = vmx_vm_has_apicv,
7595 .load_eoi_exitmap = vmx_load_eoi_exitmap,
7596 .hwapic_irr_update = vmx_hwapic_irr_update,
7597 .hwapic_isr_update = vmx_hwapic_isr_update,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007598
Izik Eiduscbc94022007-10-25 00:29:55 +02007599 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08007600 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007601 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007602
Avi Kivity586f9602010-11-18 13:09:54 +02007603 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007604
Sheng Yang17cc3932010-01-05 19:02:27 +08007605 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08007606
7607 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007608
7609 .rdtscp_supported = vmx_rdtscp_supported,
Mao, Junjiead756a12012-07-02 01:18:48 +00007610 .invpcid_supported = vmx_invpcid_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007611
7612 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007613
7614 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007615
Joerg Roedel4051b182011-03-25 09:44:49 +01007616 .set_tsc_khz = vmx_set_tsc_khz,
Will Auldba904632012-11-29 12:42:50 -08007617 .read_tsc_offset = vmx_read_tsc_offset,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007618 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10007619 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01007620 .compute_tsc_offset = vmx_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03007621 .read_l1_tsc = vmx_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007622
7623 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007624
7625 .check_intercept = vmx_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007626};
7627
7628static int __init vmx_init(void)
7629{
Yang Zhang8d146952013-01-25 10:18:50 +08007630 int r, i, msr;
Avi Kivity26bb0982009-09-07 11:14:12 +03007631
7632 rdmsrl_safe(MSR_EFER, &host_efer);
7633
7634 for (i = 0; i < NR_VMX_MSR; ++i)
7635 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03007636
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007637 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03007638 if (!vmx_io_bitmap_a)
7639 return -ENOMEM;
7640
Guo Chao2106a542012-06-15 11:31:56 +08007641 r = -ENOMEM;
7642
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007643 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08007644 if (!vmx_io_bitmap_b)
He, Qingfdef3ad2007-04-30 09:45:24 +03007645 goto out;
He, Qingfdef3ad2007-04-30 09:45:24 +03007646
Avi Kivity58972972009-02-24 22:26:47 +02007647 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08007648 if (!vmx_msr_bitmap_legacy)
Sheng Yang25c5f222008-03-28 13:18:56 +08007649 goto out1;
Guo Chao2106a542012-06-15 11:31:56 +08007650
Yang Zhang8d146952013-01-25 10:18:50 +08007651 vmx_msr_bitmap_legacy_x2apic =
7652 (unsigned long *)__get_free_page(GFP_KERNEL);
7653 if (!vmx_msr_bitmap_legacy_x2apic)
7654 goto out2;
Sheng Yang25c5f222008-03-28 13:18:56 +08007655
Avi Kivity58972972009-02-24 22:26:47 +02007656 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
Guo Chao2106a542012-06-15 11:31:56 +08007657 if (!vmx_msr_bitmap_longmode)
Yang Zhang8d146952013-01-25 10:18:50 +08007658 goto out3;
Guo Chao2106a542012-06-15 11:31:56 +08007659
Yang Zhang8d146952013-01-25 10:18:50 +08007660 vmx_msr_bitmap_longmode_x2apic =
7661 (unsigned long *)__get_free_page(GFP_KERNEL);
7662 if (!vmx_msr_bitmap_longmode_x2apic)
7663 goto out4;
Avi Kivity58972972009-02-24 22:26:47 +02007664
He, Qingfdef3ad2007-04-30 09:45:24 +03007665 /*
7666 * Allow direct access to the PC debug port (it is often used for I/O
7667 * delays, but the vmexits simply slow things down).
7668 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007669 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
7670 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03007671
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007672 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03007673
Avi Kivity58972972009-02-24 22:26:47 +02007674 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
7675 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08007676
Sheng Yang2384d2b2008-01-17 15:14:33 +08007677 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7678
Avi Kivity0ee75be2010-04-28 15:39:01 +03007679 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
7680 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03007681 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02007682 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08007683
Zhang Yanfei8f536b72012-12-06 23:43:34 +08007684#ifdef CONFIG_KEXEC
7685 rcu_assign_pointer(crash_vmclear_loaded_vmcss,
7686 crash_vmclear_local_loaded_vmcss);
7687#endif
7688
Avi Kivity58972972009-02-24 22:26:47 +02007689 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
7690 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
7691 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
7692 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
7693 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
7694 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
Yang Zhang8d146952013-01-25 10:18:50 +08007695 memcpy(vmx_msr_bitmap_legacy_x2apic,
7696 vmx_msr_bitmap_legacy, PAGE_SIZE);
7697 memcpy(vmx_msr_bitmap_longmode_x2apic,
7698 vmx_msr_bitmap_longmode, PAGE_SIZE);
7699
Yang Zhangc7c9c562013-01-25 10:18:51 +08007700 if (enable_apicv_reg_vid) {
Yang Zhang8d146952013-01-25 10:18:50 +08007701 for (msr = 0x800; msr <= 0x8ff; msr++)
7702 vmx_disable_intercept_msr_read_x2apic(msr);
7703
7704 /* According SDM, in x2apic mode, the whole id reg is used.
7705 * But in KVM, it only use the highest eight bits. Need to
7706 * intercept it */
7707 vmx_enable_intercept_msr_read_x2apic(0x802);
7708 /* TMCCT */
7709 vmx_enable_intercept_msr_read_x2apic(0x839);
7710 /* TPR */
7711 vmx_disable_intercept_msr_write_x2apic(0x808);
Yang Zhangc7c9c562013-01-25 10:18:51 +08007712 /* EOI */
7713 vmx_disable_intercept_msr_write_x2apic(0x80b);
7714 /* SELF-IPI */
7715 vmx_disable_intercept_msr_write_x2apic(0x83f);
Yang Zhang8d146952013-01-25 10:18:50 +08007716 }
He, Qingfdef3ad2007-04-30 09:45:24 +03007717
Avi Kivity089d0342009-03-23 18:26:32 +02007718 if (enable_ept) {
Xudong Hao3f6d8c82012-05-22 11:23:15 +08007719 kvm_mmu_set_mask_ptes(0ull,
7720 (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull,
7721 (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull,
7722 0ull, VMX_EPT_EXECUTABLE_MASK);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08007723 ept_set_mmio_spte_mask();
Sheng Yang5fdbcb92008-07-16 09:25:40 +08007724 kvm_enable_tdp();
7725 } else
7726 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08007727
He, Qingfdef3ad2007-04-30 09:45:24 +03007728 return 0;
7729
Yang Zhang8d146952013-01-25 10:18:50 +08007730out4:
Avi Kivity58972972009-02-24 22:26:47 +02007731 free_page((unsigned long)vmx_msr_bitmap_longmode);
Yang Zhang8d146952013-01-25 10:18:50 +08007732out3:
7733 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
Sheng Yang25c5f222008-03-28 13:18:56 +08007734out2:
Avi Kivity58972972009-02-24 22:26:47 +02007735 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03007736out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007737 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03007738out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007739 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03007740 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007741}
7742
7743static void __exit vmx_exit(void)
7744{
Yang Zhang8d146952013-01-25 10:18:50 +08007745 free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic);
7746 free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic);
Avi Kivity58972972009-02-24 22:26:47 +02007747 free_page((unsigned long)vmx_msr_bitmap_legacy);
7748 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007749 free_page((unsigned long)vmx_io_bitmap_b);
7750 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03007751
Zhang Yanfei8f536b72012-12-06 23:43:34 +08007752#ifdef CONFIG_KEXEC
7753 rcu_assign_pointer(crash_vmclear_loaded_vmcss, NULL);
7754 synchronize_rcu();
7755#endif
7756
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08007757 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08007758}
7759
7760module_init(vmx_init)
7761module_exit(vmx_exit)