blob: 18590e003bd21f6a90975acf0b686ea6c85f1084 [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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080045
Marcelo Tosatti229456f2009-06-17 09:22:14 -030046#include "trace.h"
47
Avi Kivity4ecac3f2008-05-13 13:23:38 +030048#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040049#define __ex_clear(x, reg) \
50 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030051
Avi Kivity6aa8b732006-12-10 02:21:36 -080052MODULE_AUTHOR("Qumranet");
53MODULE_LICENSE("GPL");
54
Josh Triplette9bda3b2012-03-20 23:33:51 -070055static const struct x86_cpu_id vmx_cpu_id[] = {
56 X86_FEATURE_MATCH(X86_FEATURE_VMX),
57 {}
58};
59MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
60
Rusty Russell476bc002012-01-13 09:32:18 +103061static bool __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020062module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080063
Rusty Russell476bc002012-01-13 09:32:18 +103064static bool __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020065module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020066
Rusty Russell476bc002012-01-13 09:32:18 +103067static bool __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020068module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080069
Rusty Russell476bc002012-01-13 09:32:18 +103070static bool __read_mostly enable_unrestricted_guest = 1;
Nitin A Kamble3a624e22009-06-08 11:34:16 -070071module_param_named(unrestricted_guest,
72 enable_unrestricted_guest, bool, S_IRUGO);
73
Xudong Hao83c3a332012-05-28 19:33:35 +080074static bool __read_mostly enable_ept_ad_bits = 1;
75module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
76
Rusty Russell476bc002012-01-13 09:32:18 +103077static bool __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020078module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030079
Rusty Russell476bc002012-01-13 09:32:18 +103080static bool __read_mostly vmm_exclusive = 1;
Dongxiao Xub923e622010-05-11 18:29:45 +080081module_param(vmm_exclusive, bool, S_IRUGO);
82
Rusty Russell476bc002012-01-13 09:32:18 +103083static bool __read_mostly fasteoi = 1;
Kevin Tian58fbbf22011-08-30 13:56:17 +030084module_param(fasteoi, bool, S_IRUGO);
85
Nadav Har'El801d3422011-05-25 23:02:23 +030086/*
87 * If nested=1, nested virtualization is supported, i.e., guests may use
88 * VMX and be a hypervisor for its own guests. If nested=0, guests may not
89 * use VMX instructions.
90 */
Rusty Russell476bc002012-01-13 09:32:18 +103091static bool __read_mostly nested = 0;
Nadav Har'El801d3422011-05-25 23:02:23 +030092module_param(nested, bool, S_IRUGO);
93
Avi Kivitycdc0e242009-12-06 17:21:14 +020094#define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
95 (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
96#define KVM_GUEST_CR0_MASK \
97 (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
98#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
Avi Kivity81231c62010-01-24 16:26:40 +020099 (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +0200100#define KVM_VM_CR0_ALWAYS_ON \
101 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +0200102#define KVM_CR4_GUEST_OWNED_BITS \
103 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
104 | X86_CR4_OSXMMEXCPT)
105
Avi Kivitycdc0e242009-12-06 17:21:14 +0200106#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
107#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
108
Avi Kivity78ac8b42010-04-08 18:19:35 +0300109#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
110
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800111/*
112 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
113 * ple_gap: upper bound on the amount of time between two successive
114 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -0500115 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800116 * ple_window: upper bound on the amount of time a guest is allowed to execute
117 * in a PAUSE loop. Tests indicate that most spinlocks are held for
118 * less than 2^12 cycles
119 * Time is measured based on a counter that runs at the same rate as the TSC,
120 * refer SDM volume 3b section 21.6.13 & 22.1.3.
121 */
Rik van Riel00c25bc2011-01-04 09:51:33 -0500122#define KVM_VMX_DEFAULT_PLE_GAP 128
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800123#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
124static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
125module_param(ple_gap, int, S_IRUGO);
126
127static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
128module_param(ple_window, int, S_IRUGO);
129
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200130#define NR_AUTOLOAD_MSRS 8
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300131#define VMCS02_POOL_SIZE 1
Avi Kivity61d2ef22010-04-28 16:40:38 +0300132
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400133struct vmcs {
134 u32 revision_id;
135 u32 abort;
136 char data[0];
137};
138
Nadav Har'Eld462b812011-05-24 15:26:10 +0300139/*
140 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
141 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
142 * loaded on this CPU (so we can clear them if the CPU goes down).
143 */
144struct loaded_vmcs {
145 struct vmcs *vmcs;
146 int cpu;
147 int launched;
148 struct list_head loaded_vmcss_on_cpu_link;
149};
150
Avi Kivity26bb0982009-09-07 11:14:12 +0300151struct shared_msr_entry {
152 unsigned index;
153 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200154 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300155};
156
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300157/*
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300158 * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a
159 * single nested guest (L2), hence the name vmcs12. Any VMX implementation has
160 * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is
161 * stored in guest memory specified by VMPTRLD, but is opaque to the guest,
162 * which must access it using VMREAD/VMWRITE/VMCLEAR instructions.
163 * More than one of these structures may exist, if L1 runs multiple L2 guests.
164 * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the
165 * underlying hardware which will be used to run L2.
166 * This structure is packed to ensure that its layout is identical across
167 * machines (necessary for live migration).
168 * If there are changes in this struct, VMCS12_REVISION must be changed.
169 */
Nadav Har'El22bd0352011-05-25 23:05:57 +0300170typedef u64 natural_width;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300171struct __packed vmcs12 {
172 /* According to the Intel spec, a VMCS region must start with the
173 * following two fields. Then follow implementation-specific data.
174 */
175 u32 revision_id;
176 u32 abort;
Nadav Har'El22bd0352011-05-25 23:05:57 +0300177
Nadav Har'El27d6c862011-05-25 23:06:59 +0300178 u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */
179 u32 padding[7]; /* room for future expansion */
180
Nadav Har'El22bd0352011-05-25 23:05:57 +0300181 u64 io_bitmap_a;
182 u64 io_bitmap_b;
183 u64 msr_bitmap;
184 u64 vm_exit_msr_store_addr;
185 u64 vm_exit_msr_load_addr;
186 u64 vm_entry_msr_load_addr;
187 u64 tsc_offset;
188 u64 virtual_apic_page_addr;
189 u64 apic_access_addr;
190 u64 ept_pointer;
191 u64 guest_physical_address;
192 u64 vmcs_link_pointer;
193 u64 guest_ia32_debugctl;
194 u64 guest_ia32_pat;
195 u64 guest_ia32_efer;
196 u64 guest_ia32_perf_global_ctrl;
197 u64 guest_pdptr0;
198 u64 guest_pdptr1;
199 u64 guest_pdptr2;
200 u64 guest_pdptr3;
201 u64 host_ia32_pat;
202 u64 host_ia32_efer;
203 u64 host_ia32_perf_global_ctrl;
204 u64 padding64[8]; /* room for future expansion */
205 /*
206 * To allow migration of L1 (complete with its L2 guests) between
207 * machines of different natural widths (32 or 64 bit), we cannot have
208 * unsigned long fields with no explict size. We use u64 (aliased
209 * natural_width) instead. Luckily, x86 is little-endian.
210 */
211 natural_width cr0_guest_host_mask;
212 natural_width cr4_guest_host_mask;
213 natural_width cr0_read_shadow;
214 natural_width cr4_read_shadow;
215 natural_width cr3_target_value0;
216 natural_width cr3_target_value1;
217 natural_width cr3_target_value2;
218 natural_width cr3_target_value3;
219 natural_width exit_qualification;
220 natural_width guest_linear_address;
221 natural_width guest_cr0;
222 natural_width guest_cr3;
223 natural_width guest_cr4;
224 natural_width guest_es_base;
225 natural_width guest_cs_base;
226 natural_width guest_ss_base;
227 natural_width guest_ds_base;
228 natural_width guest_fs_base;
229 natural_width guest_gs_base;
230 natural_width guest_ldtr_base;
231 natural_width guest_tr_base;
232 natural_width guest_gdtr_base;
233 natural_width guest_idtr_base;
234 natural_width guest_dr7;
235 natural_width guest_rsp;
236 natural_width guest_rip;
237 natural_width guest_rflags;
238 natural_width guest_pending_dbg_exceptions;
239 natural_width guest_sysenter_esp;
240 natural_width guest_sysenter_eip;
241 natural_width host_cr0;
242 natural_width host_cr3;
243 natural_width host_cr4;
244 natural_width host_fs_base;
245 natural_width host_gs_base;
246 natural_width host_tr_base;
247 natural_width host_gdtr_base;
248 natural_width host_idtr_base;
249 natural_width host_ia32_sysenter_esp;
250 natural_width host_ia32_sysenter_eip;
251 natural_width host_rsp;
252 natural_width host_rip;
253 natural_width paddingl[8]; /* room for future expansion */
254 u32 pin_based_vm_exec_control;
255 u32 cpu_based_vm_exec_control;
256 u32 exception_bitmap;
257 u32 page_fault_error_code_mask;
258 u32 page_fault_error_code_match;
259 u32 cr3_target_count;
260 u32 vm_exit_controls;
261 u32 vm_exit_msr_store_count;
262 u32 vm_exit_msr_load_count;
263 u32 vm_entry_controls;
264 u32 vm_entry_msr_load_count;
265 u32 vm_entry_intr_info_field;
266 u32 vm_entry_exception_error_code;
267 u32 vm_entry_instruction_len;
268 u32 tpr_threshold;
269 u32 secondary_vm_exec_control;
270 u32 vm_instruction_error;
271 u32 vm_exit_reason;
272 u32 vm_exit_intr_info;
273 u32 vm_exit_intr_error_code;
274 u32 idt_vectoring_info_field;
275 u32 idt_vectoring_error_code;
276 u32 vm_exit_instruction_len;
277 u32 vmx_instruction_info;
278 u32 guest_es_limit;
279 u32 guest_cs_limit;
280 u32 guest_ss_limit;
281 u32 guest_ds_limit;
282 u32 guest_fs_limit;
283 u32 guest_gs_limit;
284 u32 guest_ldtr_limit;
285 u32 guest_tr_limit;
286 u32 guest_gdtr_limit;
287 u32 guest_idtr_limit;
288 u32 guest_es_ar_bytes;
289 u32 guest_cs_ar_bytes;
290 u32 guest_ss_ar_bytes;
291 u32 guest_ds_ar_bytes;
292 u32 guest_fs_ar_bytes;
293 u32 guest_gs_ar_bytes;
294 u32 guest_ldtr_ar_bytes;
295 u32 guest_tr_ar_bytes;
296 u32 guest_interruptibility_info;
297 u32 guest_activity_state;
298 u32 guest_sysenter_cs;
299 u32 host_ia32_sysenter_cs;
300 u32 padding32[8]; /* room for future expansion */
301 u16 virtual_processor_id;
302 u16 guest_es_selector;
303 u16 guest_cs_selector;
304 u16 guest_ss_selector;
305 u16 guest_ds_selector;
306 u16 guest_fs_selector;
307 u16 guest_gs_selector;
308 u16 guest_ldtr_selector;
309 u16 guest_tr_selector;
310 u16 host_es_selector;
311 u16 host_cs_selector;
312 u16 host_ss_selector;
313 u16 host_ds_selector;
314 u16 host_fs_selector;
315 u16 host_gs_selector;
316 u16 host_tr_selector;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300317};
318
319/*
320 * VMCS12_REVISION is an arbitrary id that should be changed if the content or
321 * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and
322 * VMPTRLD verifies that the VMCS region that L1 is loading contains this id.
323 */
324#define VMCS12_REVISION 0x11e57ed0
325
326/*
327 * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region
328 * and any VMCS region. Although only sizeof(struct vmcs12) are used by the
329 * current implementation, 4K are reserved to avoid future complications.
330 */
331#define VMCS12_SIZE 0x1000
332
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300333/* Used to remember the last vmcs02 used for some recently used vmcs12s */
334struct vmcs02_list {
335 struct list_head list;
336 gpa_t vmptr;
337 struct loaded_vmcs vmcs02;
338};
339
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300340/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300341 * The nested_vmx structure is part of vcpu_vmx, and holds information we need
342 * for correct emulation of VMX (i.e., nested VMX) on this vcpu.
343 */
344struct nested_vmx {
345 /* Has the level1 guest done vmxon? */
346 bool vmxon;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300347
348 /* The guest-physical address of the current VMCS L1 keeps for L2 */
349 gpa_t current_vmptr;
350 /* The host-usable pointer to the above */
351 struct page *current_vmcs12_page;
352 struct vmcs12 *current_vmcs12;
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +0300353
354 /* vmcs02_list cache of VMCSs recently used to run L2 guests */
355 struct list_head vmcs02_pool;
356 int vmcs02_num;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300357 u64 vmcs01_tsc_offset;
Nadav Har'El644d7112011-05-25 23:12:35 +0300358 /* L2 must run next, and mustn't decide to exit to L1. */
359 bool nested_run_pending;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300360 /*
361 * Guest pages referred to in vmcs02 with host-physical pointers, so
362 * we must keep them pinned while L2 runs.
363 */
364 struct page *apic_access_page;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300365};
366
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400367struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000368 struct kvm_vcpu vcpu;
Avi Kivity313dbd492008-07-17 18:04:30 +0300369 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300370 u8 fail;
Avi Kivity69c73022011-03-07 15:26:44 +0200371 u8 cpl;
Avi Kivity9d58b932011-03-07 16:52:07 +0200372 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300373 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200374 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200375 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300376 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400377 int nmsrs;
378 int save_nmsrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400379#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300380 u64 msr_host_kernel_gs_base;
381 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400382#endif
Nadav Har'Eld462b812011-05-24 15:26:10 +0300383 /*
384 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
385 * non-nested (L1) guest, it always points to vmcs01. For a nested
386 * guest (L2), it points to a different VMCS.
387 */
388 struct loaded_vmcs vmcs01;
389 struct loaded_vmcs *loaded_vmcs;
390 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300391 struct msr_autoload {
392 unsigned nr;
393 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
394 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
395 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400396 struct {
397 int loaded;
398 u16 fs_sel, gs_sel, ldt_sel;
Avi Kivityb2da15a2012-05-13 19:53:24 +0300399#ifdef CONFIG_X86_64
400 u16 ds_sel, es_sel;
401#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200402 int gs_ldt_reload_needed;
403 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400404 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200405 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300406 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300407 ulong save_rflags;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300408 struct kvm_save_segment {
409 u16 selector;
410 unsigned long base;
411 u32 limit;
412 u32 ar;
413 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200414 } rmode;
Avi Kivity2fb92db2011-04-27 19:42:18 +0300415 struct {
416 u32 bitmask; /* 4 bits per segment (1 bit per field) */
417 struct kvm_save_segment seg[8];
418 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800419 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300420 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200421
422 /* Support for vnmi-less CPUs */
423 int soft_vnmi_blocked;
424 ktime_t entry_time;
425 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800426 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800427
428 bool rdtscp_enabled;
Nadav Har'Elec378ae2011-05-25 23:02:54 +0300429
430 /* Support for a guest hypervisor (nested VMX) */
431 struct nested_vmx nested;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400432};
433
Avi Kivity2fb92db2011-04-27 19:42:18 +0300434enum segment_cache_field {
435 SEG_FIELD_SEL = 0,
436 SEG_FIELD_BASE = 1,
437 SEG_FIELD_LIMIT = 2,
438 SEG_FIELD_AR = 3,
439
440 SEG_FIELD_NR = 4
441};
442
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400443static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
444{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000445 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400446}
447
Nadav Har'El22bd0352011-05-25 23:05:57 +0300448#define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
449#define FIELD(number, name) [number] = VMCS12_OFFSET(name)
450#define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \
451 [number##_HIGH] = VMCS12_OFFSET(name)+4
452
453static unsigned short vmcs_field_to_offset_table[] = {
454 FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id),
455 FIELD(GUEST_ES_SELECTOR, guest_es_selector),
456 FIELD(GUEST_CS_SELECTOR, guest_cs_selector),
457 FIELD(GUEST_SS_SELECTOR, guest_ss_selector),
458 FIELD(GUEST_DS_SELECTOR, guest_ds_selector),
459 FIELD(GUEST_FS_SELECTOR, guest_fs_selector),
460 FIELD(GUEST_GS_SELECTOR, guest_gs_selector),
461 FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector),
462 FIELD(GUEST_TR_SELECTOR, guest_tr_selector),
463 FIELD(HOST_ES_SELECTOR, host_es_selector),
464 FIELD(HOST_CS_SELECTOR, host_cs_selector),
465 FIELD(HOST_SS_SELECTOR, host_ss_selector),
466 FIELD(HOST_DS_SELECTOR, host_ds_selector),
467 FIELD(HOST_FS_SELECTOR, host_fs_selector),
468 FIELD(HOST_GS_SELECTOR, host_gs_selector),
469 FIELD(HOST_TR_SELECTOR, host_tr_selector),
470 FIELD64(IO_BITMAP_A, io_bitmap_a),
471 FIELD64(IO_BITMAP_B, io_bitmap_b),
472 FIELD64(MSR_BITMAP, msr_bitmap),
473 FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr),
474 FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr),
475 FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr),
476 FIELD64(TSC_OFFSET, tsc_offset),
477 FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr),
478 FIELD64(APIC_ACCESS_ADDR, apic_access_addr),
479 FIELD64(EPT_POINTER, ept_pointer),
480 FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address),
481 FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer),
482 FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl),
483 FIELD64(GUEST_IA32_PAT, guest_ia32_pat),
484 FIELD64(GUEST_IA32_EFER, guest_ia32_efer),
485 FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl),
486 FIELD64(GUEST_PDPTR0, guest_pdptr0),
487 FIELD64(GUEST_PDPTR1, guest_pdptr1),
488 FIELD64(GUEST_PDPTR2, guest_pdptr2),
489 FIELD64(GUEST_PDPTR3, guest_pdptr3),
490 FIELD64(HOST_IA32_PAT, host_ia32_pat),
491 FIELD64(HOST_IA32_EFER, host_ia32_efer),
492 FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl),
493 FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control),
494 FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control),
495 FIELD(EXCEPTION_BITMAP, exception_bitmap),
496 FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask),
497 FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match),
498 FIELD(CR3_TARGET_COUNT, cr3_target_count),
499 FIELD(VM_EXIT_CONTROLS, vm_exit_controls),
500 FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count),
501 FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count),
502 FIELD(VM_ENTRY_CONTROLS, vm_entry_controls),
503 FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count),
504 FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field),
505 FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code),
506 FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len),
507 FIELD(TPR_THRESHOLD, tpr_threshold),
508 FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control),
509 FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error),
510 FIELD(VM_EXIT_REASON, vm_exit_reason),
511 FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info),
512 FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code),
513 FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field),
514 FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code),
515 FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len),
516 FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info),
517 FIELD(GUEST_ES_LIMIT, guest_es_limit),
518 FIELD(GUEST_CS_LIMIT, guest_cs_limit),
519 FIELD(GUEST_SS_LIMIT, guest_ss_limit),
520 FIELD(GUEST_DS_LIMIT, guest_ds_limit),
521 FIELD(GUEST_FS_LIMIT, guest_fs_limit),
522 FIELD(GUEST_GS_LIMIT, guest_gs_limit),
523 FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit),
524 FIELD(GUEST_TR_LIMIT, guest_tr_limit),
525 FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit),
526 FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit),
527 FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes),
528 FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes),
529 FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes),
530 FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes),
531 FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes),
532 FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes),
533 FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes),
534 FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes),
535 FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info),
536 FIELD(GUEST_ACTIVITY_STATE, guest_activity_state),
537 FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs),
538 FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs),
539 FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask),
540 FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask),
541 FIELD(CR0_READ_SHADOW, cr0_read_shadow),
542 FIELD(CR4_READ_SHADOW, cr4_read_shadow),
543 FIELD(CR3_TARGET_VALUE0, cr3_target_value0),
544 FIELD(CR3_TARGET_VALUE1, cr3_target_value1),
545 FIELD(CR3_TARGET_VALUE2, cr3_target_value2),
546 FIELD(CR3_TARGET_VALUE3, cr3_target_value3),
547 FIELD(EXIT_QUALIFICATION, exit_qualification),
548 FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address),
549 FIELD(GUEST_CR0, guest_cr0),
550 FIELD(GUEST_CR3, guest_cr3),
551 FIELD(GUEST_CR4, guest_cr4),
552 FIELD(GUEST_ES_BASE, guest_es_base),
553 FIELD(GUEST_CS_BASE, guest_cs_base),
554 FIELD(GUEST_SS_BASE, guest_ss_base),
555 FIELD(GUEST_DS_BASE, guest_ds_base),
556 FIELD(GUEST_FS_BASE, guest_fs_base),
557 FIELD(GUEST_GS_BASE, guest_gs_base),
558 FIELD(GUEST_LDTR_BASE, guest_ldtr_base),
559 FIELD(GUEST_TR_BASE, guest_tr_base),
560 FIELD(GUEST_GDTR_BASE, guest_gdtr_base),
561 FIELD(GUEST_IDTR_BASE, guest_idtr_base),
562 FIELD(GUEST_DR7, guest_dr7),
563 FIELD(GUEST_RSP, guest_rsp),
564 FIELD(GUEST_RIP, guest_rip),
565 FIELD(GUEST_RFLAGS, guest_rflags),
566 FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions),
567 FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp),
568 FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip),
569 FIELD(HOST_CR0, host_cr0),
570 FIELD(HOST_CR3, host_cr3),
571 FIELD(HOST_CR4, host_cr4),
572 FIELD(HOST_FS_BASE, host_fs_base),
573 FIELD(HOST_GS_BASE, host_gs_base),
574 FIELD(HOST_TR_BASE, host_tr_base),
575 FIELD(HOST_GDTR_BASE, host_gdtr_base),
576 FIELD(HOST_IDTR_BASE, host_idtr_base),
577 FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp),
578 FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip),
579 FIELD(HOST_RSP, host_rsp),
580 FIELD(HOST_RIP, host_rip),
581};
582static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table);
583
584static inline short vmcs_field_to_offset(unsigned long field)
585{
586 if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0)
587 return -1;
588 return vmcs_field_to_offset_table[field];
589}
590
Nadav Har'Ela9d30f32011-05-25 23:03:55 +0300591static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu)
592{
593 return to_vmx(vcpu)->nested.current_vmcs12;
594}
595
596static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr)
597{
598 struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT);
599 if (is_error_page(page)) {
600 kvm_release_page_clean(page);
601 return NULL;
602 }
603 return page;
604}
605
606static void nested_release_page(struct page *page)
607{
608 kvm_release_page_dirty(page);
609}
610
611static void nested_release_page_clean(struct page *page)
612{
613 kvm_release_page_clean(page);
614}
615
Sheng Yang4e1096d2008-07-06 19:16:51 +0800616static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800617static void kvm_cpu_vmxon(u64 addr);
618static void kvm_cpu_vmxoff(void);
Avi Kivityaff48ba2010-12-05 18:56:11 +0200619static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200620static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Avi Kivity75880a02007-06-20 11:20:04 +0300621
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622static DEFINE_PER_CPU(struct vmcs *, vmxarea);
623static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300624/*
625 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
626 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
627 */
628static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300629static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800630
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200631static unsigned long *vmx_io_bitmap_a;
632static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200633static unsigned long *vmx_msr_bitmap_legacy;
634static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300635
Avi Kivity110312c2010-12-21 12:54:20 +0200636static bool cpu_has_load_ia32_efer;
Gleb Natapov8bf00a52011-10-05 14:01:22 +0200637static bool cpu_has_load_perf_global_ctrl;
Avi Kivity110312c2010-12-21 12:54:20 +0200638
Sheng Yang2384d2b2008-01-17 15:14:33 +0800639static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
640static DEFINE_SPINLOCK(vmx_vpid_lock);
641
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300642static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643 int size;
644 int order;
645 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300646 u32 pin_based_exec_ctrl;
647 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800648 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300649 u32 vmexit_ctrl;
650 u32 vmentry_ctrl;
651} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800652
Hannes Ederefff9e52008-11-28 17:02:06 +0100653static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800654 u32 ept;
655 u32 vpid;
656} vmx_capability;
657
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658#define VMX_SEGMENT_FIELD(seg) \
659 [VCPU_SREG_##seg] = { \
660 .selector = GUEST_##seg##_SELECTOR, \
661 .base = GUEST_##seg##_BASE, \
662 .limit = GUEST_##seg##_LIMIT, \
663 .ar_bytes = GUEST_##seg##_AR_BYTES, \
664 }
665
666static struct kvm_vmx_segment_field {
667 unsigned selector;
668 unsigned base;
669 unsigned limit;
670 unsigned ar_bytes;
671} kvm_vmx_segment_fields[] = {
672 VMX_SEGMENT_FIELD(CS),
673 VMX_SEGMENT_FIELD(DS),
674 VMX_SEGMENT_FIELD(ES),
675 VMX_SEGMENT_FIELD(FS),
676 VMX_SEGMENT_FIELD(GS),
677 VMX_SEGMENT_FIELD(SS),
678 VMX_SEGMENT_FIELD(TR),
679 VMX_SEGMENT_FIELD(LDTR),
680};
681
Avi Kivity26bb0982009-09-07 11:14:12 +0300682static u64 host_efer;
683
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300684static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
685
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300686/*
Brian Gerst8c065852010-07-17 09:03:26 -0400687 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300688 * away by decrementing the array size.
689 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800691#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300692 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800693#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400694 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800695};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200696#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800697
Gui Jianfeng31299942010-03-15 17:29:09 +0800698static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699{
700 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
701 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100702 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703}
704
Gui Jianfeng31299942010-03-15 17:29:09 +0800705static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300706{
707 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
708 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100709 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300710}
711
Gui Jianfeng31299942010-03-15 17:29:09 +0800712static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500713{
714 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
715 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100716 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500717}
718
Gui Jianfeng31299942010-03-15 17:29:09 +0800719static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800720{
721 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
722 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
723}
724
Gui Jianfeng31299942010-03-15 17:29:09 +0800725static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800726{
727 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
728 INTR_INFO_VALID_MASK)) ==
729 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
730}
731
Gui Jianfeng31299942010-03-15 17:29:09 +0800732static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800733{
Sheng Yang04547152009-04-01 15:52:31 +0800734 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800735}
736
Gui Jianfeng31299942010-03-15 17:29:09 +0800737static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800738{
Sheng Yang04547152009-04-01 15:52:31 +0800739 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800740}
741
Gui Jianfeng31299942010-03-15 17:29:09 +0800742static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800743{
Sheng Yang04547152009-04-01 15:52:31 +0800744 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800745}
746
Gui Jianfeng31299942010-03-15 17:29:09 +0800747static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800748{
Sheng Yang04547152009-04-01 15:52:31 +0800749 return vmcs_config.cpu_based_exec_ctrl &
750 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800751}
752
Avi Kivity774ead32007-12-26 13:57:04 +0200753static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800754{
Sheng Yang04547152009-04-01 15:52:31 +0800755 return vmcs_config.cpu_based_2nd_exec_ctrl &
756 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
757}
758
759static inline bool cpu_has_vmx_flexpriority(void)
760{
761 return cpu_has_vmx_tpr_shadow() &&
762 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800763}
764
Marcelo Tosattie7997942009-06-11 12:07:40 -0300765static inline bool cpu_has_vmx_ept_execute_only(void)
766{
Gui Jianfeng31299942010-03-15 17:29:09 +0800767 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300768}
769
770static inline bool cpu_has_vmx_eptp_uncacheable(void)
771{
Gui Jianfeng31299942010-03-15 17:29:09 +0800772 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300773}
774
775static inline bool cpu_has_vmx_eptp_writeback(void)
776{
Gui Jianfeng31299942010-03-15 17:29:09 +0800777 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300778}
779
780static inline bool cpu_has_vmx_ept_2m_page(void)
781{
Gui Jianfeng31299942010-03-15 17:29:09 +0800782 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300783}
784
Sheng Yang878403b2010-01-05 19:02:29 +0800785static inline bool cpu_has_vmx_ept_1g_page(void)
786{
Gui Jianfeng31299942010-03-15 17:29:09 +0800787 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800788}
789
Sheng Yang4bc9b982010-06-02 14:05:24 +0800790static inline bool cpu_has_vmx_ept_4levels(void)
791{
792 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
793}
794
Xudong Hao83c3a332012-05-28 19:33:35 +0800795static inline bool cpu_has_vmx_ept_ad_bits(void)
796{
797 return vmx_capability.ept & VMX_EPT_AD_BIT;
798}
799
Gui Jianfeng31299942010-03-15 17:29:09 +0800800static inline bool cpu_has_vmx_invept_individual_addr(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800801{
Gui Jianfeng31299942010-03-15 17:29:09 +0800802 return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800803}
804
Gui Jianfeng31299942010-03-15 17:29:09 +0800805static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800806{
Gui Jianfeng31299942010-03-15 17:29:09 +0800807 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800808}
809
Gui Jianfeng31299942010-03-15 17:29:09 +0800810static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800811{
Gui Jianfeng31299942010-03-15 17:29:09 +0800812 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800813}
814
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800815static inline bool cpu_has_vmx_invvpid_single(void)
816{
817 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
818}
819
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800820static inline bool cpu_has_vmx_invvpid_global(void)
821{
822 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
823}
824
Gui Jianfeng31299942010-03-15 17:29:09 +0800825static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800826{
Sheng Yang04547152009-04-01 15:52:31 +0800827 return vmcs_config.cpu_based_2nd_exec_ctrl &
828 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800829}
830
Gui Jianfeng31299942010-03-15 17:29:09 +0800831static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700832{
833 return vmcs_config.cpu_based_2nd_exec_ctrl &
834 SECONDARY_EXEC_UNRESTRICTED_GUEST;
835}
836
Gui Jianfeng31299942010-03-15 17:29:09 +0800837static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800838{
839 return vmcs_config.cpu_based_2nd_exec_ctrl &
840 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
841}
842
Gui Jianfeng31299942010-03-15 17:29:09 +0800843static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800844{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800845 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800846}
847
Gui Jianfeng31299942010-03-15 17:29:09 +0800848static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800849{
Sheng Yang04547152009-04-01 15:52:31 +0800850 return vmcs_config.cpu_based_2nd_exec_ctrl &
851 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800852}
853
Gui Jianfeng31299942010-03-15 17:29:09 +0800854static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800855{
856 return vmcs_config.cpu_based_2nd_exec_ctrl &
857 SECONDARY_EXEC_RDTSCP;
858}
859
Gui Jianfeng31299942010-03-15 17:29:09 +0800860static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +0800861{
862 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
863}
864
Sheng Yangf5f48ee2010-06-30 12:25:15 +0800865static inline bool cpu_has_vmx_wbinvd_exit(void)
866{
867 return vmcs_config.cpu_based_2nd_exec_ctrl &
868 SECONDARY_EXEC_WBINVD_EXITING;
869}
870
Sheng Yang04547152009-04-01 15:52:31 +0800871static inline bool report_flexpriority(void)
872{
873 return flexpriority_enabled;
874}
875
Nadav Har'Elfe3ef052011-05-25 23:10:02 +0300876static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit)
877{
878 return vmcs12->cpu_based_vm_exec_control & bit;
879}
880
881static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit)
882{
883 return (vmcs12->cpu_based_vm_exec_control &
884 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) &&
885 (vmcs12->secondary_vm_exec_control & bit);
886}
887
Nadav Har'El644d7112011-05-25 23:12:35 +0300888static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12,
889 struct kvm_vcpu *vcpu)
890{
891 return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS;
892}
893
894static inline bool is_exception(u32 intr_info)
895{
896 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
897 == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK);
898}
899
900static void nested_vmx_vmexit(struct kvm_vcpu *vcpu);
Nadav Har'El7c177932011-05-25 23:12:04 +0300901static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
902 struct vmcs12 *vmcs12,
903 u32 reason, unsigned long qualification);
904
Rusty Russell8b9cf982007-07-30 16:31:43 +1000905static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800906{
907 int i;
908
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400909 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300910 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300911 return i;
912 return -1;
913}
914
Sheng Yang2384d2b2008-01-17 15:14:33 +0800915static inline void __invvpid(int ext, u16 vpid, gva_t gva)
916{
917 struct {
918 u64 vpid : 16;
919 u64 rsvd : 48;
920 u64 gva;
921 } operand = { vpid, 0, gva };
922
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300923 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800924 /* CF==1 or ZF==1 --> rc = -1 */
925 "; ja 1f ; ud2 ; 1:"
926 : : "a"(&operand), "c"(ext) : "cc", "memory");
927}
928
Sheng Yang14394422008-04-28 12:24:45 +0800929static inline void __invept(int ext, u64 eptp, gpa_t gpa)
930{
931 struct {
932 u64 eptp, gpa;
933 } operand = {eptp, gpa};
934
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300935 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800936 /* CF==1 or ZF==1 --> rc = -1 */
937 "; ja 1f ; ud2 ; 1:\n"
938 : : "a" (&operand), "c" (ext) : "cc", "memory");
939}
940
Avi Kivity26bb0982009-09-07 11:14:12 +0300941static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300942{
943 int i;
944
Rusty Russell8b9cf982007-07-30 16:31:43 +1000945 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300946 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400947 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000948 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800949}
950
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951static void vmcs_clear(struct vmcs *vmcs)
952{
953 u64 phys_addr = __pa(vmcs);
954 u8 error;
955
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300956 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200957 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 : "cc", "memory");
959 if (error)
960 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
961 vmcs, phys_addr);
962}
963
Nadav Har'Eld462b812011-05-24 15:26:10 +0300964static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
965{
966 vmcs_clear(loaded_vmcs->vmcs);
967 loaded_vmcs->cpu = -1;
968 loaded_vmcs->launched = 0;
969}
970
Dongxiao Xu7725b892010-05-11 18:29:38 +0800971static void vmcs_load(struct vmcs *vmcs)
972{
973 u64 phys_addr = __pa(vmcs);
974 u8 error;
975
976 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200977 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +0800978 : "cc", "memory");
979 if (error)
Nadav Har'El2844d842011-05-25 23:16:40 +0300980 printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n",
Dongxiao Xu7725b892010-05-11 18:29:38 +0800981 vmcs, phys_addr);
982}
983
Nadav Har'Eld462b812011-05-24 15:26:10 +0300984static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300986 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800987 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988
Nadav Har'Eld462b812011-05-24 15:26:10 +0300989 if (loaded_vmcs->cpu != cpu)
990 return; /* vcpu migration can race with cpu offline */
991 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 per_cpu(current_vmcs, cpu) = NULL;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300993 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
994 loaded_vmcs_init(loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995}
996
Nadav Har'Eld462b812011-05-24 15:26:10 +0300997static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800998{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300999 if (loaded_vmcs->cpu != -1)
1000 smp_call_function_single(
1001 loaded_vmcs->cpu, __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -08001002}
1003
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001004static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +08001005{
1006 if (vmx->vpid == 0)
1007 return;
1008
Gui Jianfeng518c8ae2010-06-04 08:51:39 +08001009 if (cpu_has_vmx_invvpid_single())
1010 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +08001011}
1012
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001013static inline void vpid_sync_vcpu_global(void)
1014{
1015 if (cpu_has_vmx_invvpid_global())
1016 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
1017}
1018
1019static inline void vpid_sync_context(struct vcpu_vmx *vmx)
1020{
1021 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +08001022 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001023 else
1024 vpid_sync_vcpu_global();
1025}
1026
Sheng Yang14394422008-04-28 12:24:45 +08001027static inline void ept_sync_global(void)
1028{
1029 if (cpu_has_vmx_invept_global())
1030 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
1031}
1032
1033static inline void ept_sync_context(u64 eptp)
1034{
Avi Kivity089d0342009-03-23 18:26:32 +02001035 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001036 if (cpu_has_vmx_invept_context())
1037 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
1038 else
1039 ept_sync_global();
1040 }
1041}
1042
1043static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
1044{
Avi Kivity089d0342009-03-23 18:26:32 +02001045 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001046 if (cpu_has_vmx_invept_individual_addr())
1047 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
1048 eptp, gpa);
1049 else
1050 ept_sync_context(eptp);
1051 }
1052}
1053
Avi Kivity96304212011-05-15 10:13:13 -04001054static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055{
Avi Kivity5e520e62011-05-15 10:13:12 -04001056 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057
Avi Kivity5e520e62011-05-15 10:13:12 -04001058 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
1059 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060 return value;
1061}
1062
Avi Kivity96304212011-05-15 10:13:13 -04001063static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064{
1065 return vmcs_readl(field);
1066}
1067
Avi Kivity96304212011-05-15 10:13:13 -04001068static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069{
1070 return vmcs_readl(field);
1071}
1072
Avi Kivity96304212011-05-15 10:13:13 -04001073static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074{
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001075#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076 return vmcs_readl(field);
1077#else
1078 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
1079#endif
1080}
1081
Avi Kivitye52de1b2007-01-05 16:36:56 -08001082static noinline void vmwrite_error(unsigned long field, unsigned long value)
1083{
1084 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
1085 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1086 dump_stack();
1087}
1088
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089static void vmcs_writel(unsigned long field, unsigned long value)
1090{
1091 u8 error;
1092
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001093 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -04001094 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -08001095 if (unlikely(error))
1096 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097}
1098
1099static void vmcs_write16(unsigned long field, u16 value)
1100{
1101 vmcs_writel(field, value);
1102}
1103
1104static void vmcs_write32(unsigned long field, u32 value)
1105{
1106 vmcs_writel(field, value);
1107}
1108
1109static void vmcs_write64(unsigned long field, u64 value)
1110{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +03001112#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001113 asm volatile ("");
1114 vmcs_writel(field+1, value >> 32);
1115#endif
1116}
1117
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001118static void vmcs_clear_bits(unsigned long field, u32 mask)
1119{
1120 vmcs_writel(field, vmcs_readl(field) & ~mask);
1121}
1122
1123static void vmcs_set_bits(unsigned long field, u32 mask)
1124{
1125 vmcs_writel(field, vmcs_readl(field) | mask);
1126}
1127
Avi Kivity2fb92db2011-04-27 19:42:18 +03001128static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
1129{
1130 vmx->segment_cache.bitmask = 0;
1131}
1132
1133static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1134 unsigned field)
1135{
1136 bool ret;
1137 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1138
1139 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
1140 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
1141 vmx->segment_cache.bitmask = 0;
1142 }
1143 ret = vmx->segment_cache.bitmask & mask;
1144 vmx->segment_cache.bitmask |= mask;
1145 return ret;
1146}
1147
1148static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1149{
1150 u16 *p = &vmx->segment_cache.seg[seg].selector;
1151
1152 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1153 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1154 return *p;
1155}
1156
1157static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1158{
1159 ulong *p = &vmx->segment_cache.seg[seg].base;
1160
1161 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1162 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1163 return *p;
1164}
1165
1166static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1167{
1168 u32 *p = &vmx->segment_cache.seg[seg].limit;
1169
1170 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1171 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1172 return *p;
1173}
1174
1175static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1176{
1177 u32 *p = &vmx->segment_cache.seg[seg].ar;
1178
1179 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1180 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1181 return *p;
1182}
1183
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001184static void update_exception_bitmap(struct kvm_vcpu *vcpu)
1185{
1186 u32 eb;
1187
Jan Kiszkafd7373c2010-01-20 18:20:20 +01001188 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1189 (1u << NM_VECTOR) | (1u << DB_VECTOR);
1190 if ((vcpu->guest_debug &
1191 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1192 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1193 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001194 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001195 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +02001196 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001197 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +02001198 if (vcpu->fpu_active)
1199 eb &= ~(1u << NM_VECTOR);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001200
1201 /* When we are running a nested L2 guest and L1 specified for it a
1202 * certain exception bitmap, we must trap the same exceptions and pass
1203 * them to L1. When running L2, we will only handle the exceptions
1204 * specified above if L1 did not want them.
1205 */
1206 if (is_guest_mode(vcpu))
1207 eb |= get_vmcs12(vcpu)->exception_bitmap;
1208
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001209 vmcs_write32(EXCEPTION_BITMAP, eb);
1210}
1211
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001212static void clear_atomic_switch_msr_special(unsigned long entry,
1213 unsigned long exit)
1214{
1215 vmcs_clear_bits(VM_ENTRY_CONTROLS, entry);
1216 vmcs_clear_bits(VM_EXIT_CONTROLS, exit);
1217}
1218
Avi Kivity61d2ef22010-04-28 16:40:38 +03001219static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1220{
1221 unsigned i;
1222 struct msr_autoload *m = &vmx->msr_autoload;
1223
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001224 switch (msr) {
1225 case MSR_EFER:
1226 if (cpu_has_load_ia32_efer) {
1227 clear_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
1228 VM_EXIT_LOAD_IA32_EFER);
1229 return;
1230 }
1231 break;
1232 case MSR_CORE_PERF_GLOBAL_CTRL:
1233 if (cpu_has_load_perf_global_ctrl) {
1234 clear_atomic_switch_msr_special(
1235 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1236 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1237 return;
1238 }
1239 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001240 }
1241
Avi Kivity61d2ef22010-04-28 16:40:38 +03001242 for (i = 0; i < m->nr; ++i)
1243 if (m->guest[i].index == msr)
1244 break;
1245
1246 if (i == m->nr)
1247 return;
1248 --m->nr;
1249 m->guest[i] = m->guest[m->nr];
1250 m->host[i] = m->host[m->nr];
1251 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1252 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1253}
1254
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001255static void add_atomic_switch_msr_special(unsigned long entry,
1256 unsigned long exit, unsigned long guest_val_vmcs,
1257 unsigned long host_val_vmcs, u64 guest_val, u64 host_val)
1258{
1259 vmcs_write64(guest_val_vmcs, guest_val);
1260 vmcs_write64(host_val_vmcs, host_val);
1261 vmcs_set_bits(VM_ENTRY_CONTROLS, entry);
1262 vmcs_set_bits(VM_EXIT_CONTROLS, exit);
1263}
1264
Avi Kivity61d2ef22010-04-28 16:40:38 +03001265static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1266 u64 guest_val, u64 host_val)
1267{
1268 unsigned i;
1269 struct msr_autoload *m = &vmx->msr_autoload;
1270
Gleb Natapov8bf00a52011-10-05 14:01:22 +02001271 switch (msr) {
1272 case MSR_EFER:
1273 if (cpu_has_load_ia32_efer) {
1274 add_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER,
1275 VM_EXIT_LOAD_IA32_EFER,
1276 GUEST_IA32_EFER,
1277 HOST_IA32_EFER,
1278 guest_val, host_val);
1279 return;
1280 }
1281 break;
1282 case MSR_CORE_PERF_GLOBAL_CTRL:
1283 if (cpu_has_load_perf_global_ctrl) {
1284 add_atomic_switch_msr_special(
1285 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1286 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1287 GUEST_IA32_PERF_GLOBAL_CTRL,
1288 HOST_IA32_PERF_GLOBAL_CTRL,
1289 guest_val, host_val);
1290 return;
1291 }
1292 break;
Avi Kivity110312c2010-12-21 12:54:20 +02001293 }
1294
Avi Kivity61d2ef22010-04-28 16:40:38 +03001295 for (i = 0; i < m->nr; ++i)
1296 if (m->guest[i].index == msr)
1297 break;
1298
Gleb Natapove7fc6f93b2011-10-05 14:01:24 +02001299 if (i == NR_AUTOLOAD_MSRS) {
1300 printk_once(KERN_WARNING"Not enough mst switch entries. "
1301 "Can't add msr %x\n", msr);
1302 return;
1303 } else if (i == m->nr) {
Avi Kivity61d2ef22010-04-28 16:40:38 +03001304 ++m->nr;
1305 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
1306 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
1307 }
1308
1309 m->guest[i].index = msr;
1310 m->guest[i].value = guest_val;
1311 m->host[i].index = msr;
1312 m->host[i].value = host_val;
1313}
1314
Avi Kivity33ed6322007-05-02 16:54:03 +03001315static void reload_tss(void)
1316{
Avi Kivity33ed6322007-05-02 16:54:03 +03001317 /*
1318 * VT restores TR but not its size. Useless.
1319 */
Avi Kivityd3591922010-07-26 18:32:39 +03001320 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +02001321 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +03001322
Avi Kivityd3591922010-07-26 18:32:39 +03001323 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +03001324 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
1325 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +03001326}
1327
Avi Kivity92c0d902009-10-29 11:00:16 +02001328static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +03001329{
Roel Kluin3a34a882009-08-04 02:08:45 -07001330 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001331 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +03001332
Avi Kivityf6801df2010-01-21 15:31:50 +02001333 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -07001334
Avi Kivity51c6cf62007-08-29 03:48:05 +03001335 /*
1336 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
1337 * outside long mode
1338 */
1339 ignore_bits = EFER_NX | EFER_SCE;
1340#ifdef CONFIG_X86_64
1341 ignore_bits |= EFER_LMA | EFER_LME;
1342 /* SCE is meaningful only in long mode on Intel */
1343 if (guest_efer & EFER_LMA)
1344 ignore_bits &= ~(u64)EFER_SCE;
1345#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +03001346 guest_efer &= ~ignore_bits;
1347 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +03001348 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +02001349 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +03001350
1351 clear_atomic_switch_msr(vmx, MSR_EFER);
1352 /* On ept, can't emulate nx, and must switch nx atomically */
1353 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
1354 guest_efer = vmx->vcpu.arch.efer;
1355 if (!(guest_efer & EFER_LMA))
1356 guest_efer &= ~EFER_LME;
1357 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
1358 return false;
1359 }
1360
Avi Kivity26bb0982009-09-07 11:14:12 +03001361 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +03001362}
1363
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001364static unsigned long segment_base(u16 selector)
1365{
Avi Kivityd3591922010-07-26 18:32:39 +03001366 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001367 struct desc_struct *d;
1368 unsigned long table_base;
1369 unsigned long v;
1370
1371 if (!(selector & ~3))
1372 return 0;
1373
Avi Kivityd3591922010-07-26 18:32:39 +03001374 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +02001375
1376 if (selector & 4) { /* from ldt */
1377 u16 ldt_selector = kvm_read_ldt();
1378
1379 if (!(ldt_selector & ~3))
1380 return 0;
1381
1382 table_base = segment_base(ldt_selector);
1383 }
1384 d = (struct desc_struct *)(table_base + (selector & ~7));
1385 v = get_desc_base(d);
1386#ifdef CONFIG_X86_64
1387 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
1388 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
1389#endif
1390 return v;
1391}
1392
1393static inline unsigned long kvm_read_tr_base(void)
1394{
1395 u16 tr;
1396 asm("str %0" : "=g"(tr));
1397 return segment_base(tr);
1398}
1399
Avi Kivity04d2cc72007-09-10 18:10:54 +03001400static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +03001401{
Avi Kivity04d2cc72007-09-10 18:10:54 +03001402 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001403 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001404
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001405 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001406 return;
1407
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001408 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001409 /*
1410 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
1411 * allow segment selectors with cpl > 0 or ti == 1.
1412 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001413 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +02001414 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +02001415 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001416 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001417 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001418 vmx->host_state.fs_reload_needed = 0;
1419 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +03001420 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001421 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001422 }
Avi Kivity9581d442010-10-19 16:46:55 +02001423 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001424 if (!(vmx->host_state.gs_sel & 7))
1425 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001426 else {
1427 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +02001428 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +03001429 }
1430
1431#ifdef CONFIG_X86_64
Avi Kivityb2da15a2012-05-13 19:53:24 +03001432 savesegment(ds, vmx->host_state.ds_sel);
1433 savesegment(es, vmx->host_state.es_sel);
1434#endif
1435
1436#ifdef CONFIG_X86_64
Avi Kivity33ed6322007-05-02 16:54:03 +03001437 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
1438 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
1439#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001440 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
1441 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +03001442#endif
Avi Kivity707c0872007-05-02 17:33:43 +03001443
1444#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001445 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
1446 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +03001447 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +03001448#endif
Avi Kivity26bb0982009-09-07 11:14:12 +03001449 for (i = 0; i < vmx->save_nmsrs; ++i)
1450 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +02001451 vmx->guest_msrs[i].data,
1452 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +03001453}
1454
Avi Kivitya9b21b62008-06-24 11:48:49 +03001455static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +03001456{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001457 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +03001458 return;
1459
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001460 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001461 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +02001462#ifdef CONFIG_X86_64
1463 if (is_long_mode(&vmx->vcpu))
1464 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
1465#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001466 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001467 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001468#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +02001469 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +02001470#else
1471 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +03001472#endif
Avi Kivity33ed6322007-05-02 16:54:03 +03001473 }
Avi Kivity0a77fe42010-10-19 18:48:35 +02001474 if (vmx->host_state.fs_reload_needed)
1475 loadsegment(fs, vmx->host_state.fs_sel);
Avi Kivityb2da15a2012-05-13 19:53:24 +03001476#ifdef CONFIG_X86_64
1477 if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) {
1478 loadsegment(ds, vmx->host_state.ds_sel);
1479 loadsegment(es, vmx->host_state.es_sel);
1480 }
1481#else
1482 /*
1483 * The sysexit path does not restore ds/es, so we must set them to
1484 * a reasonable value ourselves.
1485 */
1486 loadsegment(ds, __USER_DS);
1487 loadsegment(es, __USER_DS);
1488#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +02001489 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +03001490#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +02001491 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +03001492#endif
Linus Torvalds1361b832012-02-21 13:19:22 -08001493 if (user_has_fpu())
Avi Kivity1c11e712010-05-03 16:05:44 +03001494 clts();
Avi Kivity3444d7d2010-07-26 18:32:38 +03001495 load_gdt(&__get_cpu_var(host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +03001496}
1497
Avi Kivitya9b21b62008-06-24 11:48:49 +03001498static void vmx_load_host_state(struct vcpu_vmx *vmx)
1499{
1500 preempt_disable();
1501 __vmx_load_host_state(vmx);
1502 preempt_enable();
1503}
1504
Avi Kivity6aa8b732006-12-10 02:21:36 -08001505/*
1506 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
1507 * vcpu mutex is already taken.
1508 */
Avi Kivity15ad7142007-07-11 18:17:21 +03001509static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001510{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001511 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001512 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001514 if (!vmm_exclusive)
1515 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001516 else if (vmx->loaded_vmcs->cpu != cpu)
1517 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518
Nadav Har'Eld462b812011-05-24 15:26:10 +03001519 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1520 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1521 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522 }
1523
Nadav Har'Eld462b812011-05-24 15:26:10 +03001524 if (vmx->loaded_vmcs->cpu != cpu) {
Avi Kivityd3591922010-07-26 18:32:39 +03001525 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526 unsigned long sysenter_esp;
1527
Avi Kivitya8eeb042010-05-10 12:34:53 +03001528 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001529 local_irq_disable();
Nadav Har'Eld462b812011-05-24 15:26:10 +03001530 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1531 &per_cpu(loaded_vmcss_on_cpu, cpu));
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001532 local_irq_enable();
1533
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534 /*
1535 * Linux uses per-cpu TSS and GDT, so set these when switching
1536 * processors.
1537 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001538 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03001539 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001540
1541 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1542 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Nadav Har'Eld462b812011-05-24 15:26:10 +03001543 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001544 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545}
1546
1547static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1548{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001549 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001550 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001551 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1552 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001553 kvm_cpu_vmxoff();
1554 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001555}
1556
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001557static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1558{
Avi Kivity81231c62010-01-24 16:26:40 +02001559 ulong cr0;
1560
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001561 if (vcpu->fpu_active)
1562 return;
1563 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001564 cr0 = vmcs_readl(GUEST_CR0);
1565 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1566 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1567 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001568 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001569 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001570 if (is_guest_mode(vcpu))
1571 vcpu->arch.cr0_guest_owned_bits &=
1572 ~get_vmcs12(vcpu)->cr0_guest_host_mask;
Avi Kivityedcafe32009-12-30 18:07:40 +02001573 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001574}
1575
Avi Kivityedcafe32009-12-30 18:07:40 +02001576static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1577
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03001578/*
1579 * Return the cr0 value that a nested guest would read. This is a combination
1580 * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by
1581 * its hypervisor (cr0_read_shadow).
1582 */
1583static inline unsigned long nested_read_cr0(struct vmcs12 *fields)
1584{
1585 return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) |
1586 (fields->cr0_read_shadow & fields->cr0_guest_host_mask);
1587}
1588static inline unsigned long nested_read_cr4(struct vmcs12 *fields)
1589{
1590 return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) |
1591 (fields->cr4_read_shadow & fields->cr4_guest_host_mask);
1592}
1593
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001594static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1595{
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001596 /* Note that there is no vcpu->fpu_active = 0 here. The caller must
1597 * set this *before* calling this function.
1598 */
Avi Kivityedcafe32009-12-30 18:07:40 +02001599 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001600 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001601 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001602 vcpu->arch.cr0_guest_owned_bits = 0;
1603 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Nadav Har'El36cf24e2011-05-25 23:15:08 +03001604 if (is_guest_mode(vcpu)) {
1605 /*
1606 * L1's specified read shadow might not contain the TS bit,
1607 * so now that we turned on shadowing of this bit, we need to
1608 * set this bit of the shadow. Like in nested_vmx_run we need
1609 * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet
1610 * up-to-date here because we just decached cr0.TS (and we'll
1611 * only update vmcs12->guest_cr0 on nested exit).
1612 */
1613 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1614 vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) |
1615 (vcpu->arch.cr0 & X86_CR0_TS);
1616 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
1617 } else
1618 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001619}
1620
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1622{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001623 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001624
Avi Kivity6de12732011-03-07 12:51:22 +02001625 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1626 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1627 rflags = vmcs_readl(GUEST_RFLAGS);
1628 if (to_vmx(vcpu)->rmode.vm86_active) {
1629 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1630 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1631 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1632 }
1633 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001634 }
Avi Kivity6de12732011-03-07 12:51:22 +02001635 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636}
1637
1638static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1639{
Avi Kivity6de12732011-03-07 12:51:22 +02001640 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity69c73022011-03-07 15:26:44 +02001641 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6de12732011-03-07 12:51:22 +02001642 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001643 if (to_vmx(vcpu)->rmode.vm86_active) {
1644 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001645 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001646 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001647 vmcs_writel(GUEST_RFLAGS, rflags);
1648}
1649
Glauber Costa2809f5d2009-05-12 16:21:05 -04001650static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1651{
1652 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1653 int ret = 0;
1654
1655 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001656 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001657 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001658 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001659
1660 return ret & mask;
1661}
1662
1663static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1664{
1665 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1666 u32 interruptibility = interruptibility_old;
1667
1668 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1669
Jan Kiszka48005f62010-02-19 19:38:07 +01001670 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001671 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001672 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001673 interruptibility |= GUEST_INTR_STATE_STI;
1674
1675 if ((interruptibility != interruptibility_old))
1676 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1677}
1678
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1680{
1681 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001683 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001685 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686
Glauber Costa2809f5d2009-05-12 16:21:05 -04001687 /* skipping an emulated instruction also counts */
1688 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001689}
1690
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001691/*
1692 * KVM wants to inject page-faults which it got to the guest. This function
1693 * checks whether in a nested guest, we need to inject them to L1 or L2.
1694 * This function assumes it is called with the exit reason in vmcs02 being
1695 * a #PF exception (this is the only case in which KVM injects a #PF when L2
1696 * is running).
1697 */
1698static int nested_pf_handled(struct kvm_vcpu *vcpu)
1699{
1700 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
1701
1702 /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */
Nadav Har'El95871902012-03-06 16:39:22 +02001703 if (!(vmcs12->exception_bitmap & (1u << PF_VECTOR)))
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001704 return 0;
1705
1706 nested_vmx_vmexit(vcpu);
1707 return 1;
1708}
1709
Avi Kivity298101d2007-11-25 13:41:11 +02001710static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02001711 bool has_error_code, u32 error_code,
1712 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02001713{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001714 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001715 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001716
Nadav Har'El0b6ac342011-05-25 23:13:36 +03001717 if (nr == PF_VECTOR && is_guest_mode(vcpu) &&
1718 nested_pf_handled(vcpu))
1719 return;
1720
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001721 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001722 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001723 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1724 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001725
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001726 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001727 int inc_eip = 0;
1728 if (kvm_exception_is_soft(nr))
1729 inc_eip = vcpu->arch.event_exit_inst_len;
1730 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02001731 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001732 return;
1733 }
1734
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001735 if (kvm_exception_is_soft(nr)) {
1736 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1737 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001738 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1739 } else
1740 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1741
1742 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02001743}
1744
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001745static bool vmx_rdtscp_supported(void)
1746{
1747 return cpu_has_vmx_rdtscp();
1748}
1749
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750/*
Eddie Donga75beee2007-05-17 18:55:15 +03001751 * Swap MSR entry in host/guest MSR entry array.
1752 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001753static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001754{
Avi Kivity26bb0982009-09-07 11:14:12 +03001755 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001756
1757 tmp = vmx->guest_msrs[to];
1758 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1759 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001760}
1761
1762/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001763 * Set up the vmcs to automatically save and restore system
1764 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1765 * mode, as fiddling with msrs is very expensive.
1766 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001767static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001768{
Avi Kivity26bb0982009-09-07 11:14:12 +03001769 int save_nmsrs, index;
Avi Kivity58972972009-02-24 22:26:47 +02001770 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +03001771
Eddie Donga75beee2007-05-17 18:55:15 +03001772 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001773#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001774 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10001775 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03001776 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001777 move_msr_up(vmx, index, save_nmsrs++);
1778 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001779 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001780 move_msr_up(vmx, index, save_nmsrs++);
1781 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001782 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001783 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001784 index = __find_msr_index(vmx, MSR_TSC_AUX);
1785 if (index >= 0 && vmx->rdtscp_enabled)
1786 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03001787 /*
Brian Gerst8c065852010-07-17 09:03:26 -04001788 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03001789 * if efer.sce is enabled.
1790 */
Brian Gerst8c065852010-07-17 09:03:26 -04001791 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02001792 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10001793 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001794 }
Eddie Donga75beee2007-05-17 18:55:15 +03001795#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001796 index = __find_msr_index(vmx, MSR_EFER);
1797 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001798 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001799
Avi Kivity26bb0982009-09-07 11:14:12 +03001800 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02001801
1802 if (cpu_has_vmx_msr_bitmap()) {
1803 if (is_long_mode(&vmx->vcpu))
1804 msr_bitmap = vmx_msr_bitmap_longmode;
1805 else
1806 msr_bitmap = vmx_msr_bitmap_legacy;
1807
1808 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1809 }
Avi Kivitye38aea32007-04-19 13:22:48 +03001810}
1811
1812/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001813 * reads and returns guest's timestamp counter "register"
1814 * guest_tsc = host_tsc + tsc_offset -- 21.3
1815 */
1816static u64 guest_read_tsc(void)
1817{
1818 u64 host_tsc, tsc_offset;
1819
1820 rdtscll(host_tsc);
1821 tsc_offset = vmcs_read64(TSC_OFFSET);
1822 return host_tsc + tsc_offset;
1823}
1824
1825/*
Nadav Har'Eld5c17852011-08-02 15:54:20 +03001826 * Like guest_read_tsc, but always returns L1's notion of the timestamp
1827 * counter, even if a nested guest (L2) is currently running.
1828 */
1829u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu)
1830{
1831 u64 host_tsc, tsc_offset;
1832
1833 rdtscll(host_tsc);
1834 tsc_offset = is_guest_mode(vcpu) ?
1835 to_vmx(vcpu)->nested.vmcs01_tsc_offset :
1836 vmcs_read64(TSC_OFFSET);
1837 return host_tsc + tsc_offset;
1838}
1839
1840/*
Zachary Amsdencc578282012-02-03 15:43:50 -02001841 * Engage any workarounds for mis-matched TSC rates. Currently limited to
1842 * software catchup for faster rates on slower CPUs.
Joerg Roedel4051b182011-03-25 09:44:49 +01001843 */
Zachary Amsdencc578282012-02-03 15:43:50 -02001844static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
Joerg Roedel4051b182011-03-25 09:44:49 +01001845{
Zachary Amsdencc578282012-02-03 15:43:50 -02001846 if (!scale)
1847 return;
1848
1849 if (user_tsc_khz > tsc_khz) {
1850 vcpu->arch.tsc_catchup = 1;
1851 vcpu->arch.tsc_always_catchup = 1;
1852 } else
1853 WARN(1, "user requested TSC rate below hardware speed\n");
Joerg Roedel4051b182011-03-25 09:44:49 +01001854}
1855
1856/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10001857 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10001859static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001860{
Nadav Har'El27fc51b2011-08-02 15:54:52 +03001861 if (is_guest_mode(vcpu)) {
Nadav Har'El79918252011-05-25 23:15:39 +03001862 /*
Nadav Har'El27fc51b2011-08-02 15:54:52 +03001863 * We're here if L1 chose not to trap WRMSR to TSC. According
1864 * to the spec, this should set L1's TSC; The offset that L1
1865 * set for L2 remains unchanged, and still needs to be added
1866 * to the newly set TSC to get L2's TSC.
Nadav Har'El79918252011-05-25 23:15:39 +03001867 */
Nadav Har'El27fc51b2011-08-02 15:54:52 +03001868 struct vmcs12 *vmcs12;
1869 to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset;
1870 /* recalculate vmcs02.TSC_OFFSET: */
1871 vmcs12 = get_vmcs12(vcpu);
1872 vmcs_write64(TSC_OFFSET, offset +
1873 (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ?
1874 vmcs12->tsc_offset : 0));
1875 } else {
1876 vmcs_write64(TSC_OFFSET, offset);
1877 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878}
1879
Marcelo Tosattif1e2b262012-02-03 15:43:55 -02001880static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host)
Zachary Amsdene48672f2010-08-19 22:07:23 -10001881{
1882 u64 offset = vmcs_read64(TSC_OFFSET);
1883 vmcs_write64(TSC_OFFSET, offset + adjustment);
Nadav Har'El79918252011-05-25 23:15:39 +03001884 if (is_guest_mode(vcpu)) {
1885 /* Even when running L2, the adjustment needs to apply to L1 */
1886 to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment;
1887 }
Zachary Amsdene48672f2010-08-19 22:07:23 -10001888}
1889
Joerg Roedel857e4092011-03-25 09:44:50 +01001890static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1891{
1892 return target_tsc - native_read_tsc();
1893}
1894
Nadav Har'El801d3422011-05-25 23:02:23 +03001895static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu)
1896{
1897 struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0);
1898 return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31)));
1899}
1900
1901/*
1902 * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
1903 * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
1904 * all guests if the "nested" module option is off, and can also be disabled
1905 * for a single guest by disabling its VMX cpuid bit.
1906 */
1907static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
1908{
1909 return nested && guest_cpuid_has_vmx(vcpu);
1910}
1911
Avi Kivity6aa8b732006-12-10 02:21:36 -08001912/*
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001913 * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be
1914 * returned for the various VMX controls MSRs when nested VMX is enabled.
1915 * The same values should also be used to verify that vmcs12 control fields are
1916 * valid during nested entry from L1 to L2.
1917 * Each of these control msrs has a low and high 32-bit half: A low bit is on
1918 * if the corresponding bit in the (32-bit) control field *must* be on, and a
1919 * bit in the high half is on if the corresponding bit in the control field
1920 * may be on. See also vmx_control_verify().
1921 * TODO: allow these variables to be modified (downgraded) by module options
1922 * or other means.
1923 */
1924static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high;
1925static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high;
1926static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high;
1927static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high;
1928static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high;
1929static __init void nested_vmx_setup_ctls_msrs(void)
1930{
1931 /*
1932 * Note that as a general rule, the high half of the MSRs (bits in
1933 * the control fields which may be 1) should be initialized by the
1934 * intersection of the underlying hardware's MSR (i.e., features which
1935 * can be supported) and the list of features we want to expose -
1936 * because they are known to be properly supported in our code.
1937 * Also, usually, the low half of the MSRs (bits which must be 1) can
1938 * be set to 0, meaning that L1 may turn off any of these bits. The
1939 * reason is that if one of these bits is necessary, it will appear
1940 * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control
1941 * fields of vmcs01 and vmcs02, will turn these bits off - and
1942 * nested_vmx_exit_handled() will not pass related exits to L1.
1943 * These rules have exceptions below.
1944 */
1945
1946 /* pin-based controls */
1947 /*
1948 * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is
1949 * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR.
1950 */
1951 nested_vmx_pinbased_ctls_low = 0x16 ;
1952 nested_vmx_pinbased_ctls_high = 0x16 |
1953 PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING |
1954 PIN_BASED_VIRTUAL_NMIS;
1955
1956 /* exit controls */
1957 nested_vmx_exit_ctls_low = 0;
Nadav Har'Elb6f12502011-05-25 23:13:06 +03001958 /* Note that guest use of VM_EXIT_ACK_INTR_ON_EXIT is not supported. */
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001959#ifdef CONFIG_X86_64
1960 nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE;
1961#else
1962 nested_vmx_exit_ctls_high = 0;
1963#endif
1964
1965 /* entry controls */
1966 rdmsr(MSR_IA32_VMX_ENTRY_CTLS,
1967 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high);
1968 nested_vmx_entry_ctls_low = 0;
1969 nested_vmx_entry_ctls_high &=
1970 VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE;
1971
1972 /* cpu-based controls */
1973 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS,
1974 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high);
1975 nested_vmx_procbased_ctls_low = 0;
1976 nested_vmx_procbased_ctls_high &=
1977 CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING |
1978 CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING |
1979 CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING |
1980 CPU_BASED_CR3_STORE_EXITING |
1981#ifdef CONFIG_X86_64
1982 CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING |
1983#endif
1984 CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING |
1985 CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02001986 CPU_BASED_RDPMC_EXITING |
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03001987 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
1988 /*
1989 * We can allow some features even when not supported by the
1990 * hardware. For example, L1 can specify an MSR bitmap - and we
1991 * can use it to avoid exits to L1 - even when L0 runs L2
1992 * without MSR bitmaps.
1993 */
1994 nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS;
1995
1996 /* secondary cpu-based controls */
1997 rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
1998 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high);
1999 nested_vmx_secondary_ctls_low = 0;
2000 nested_vmx_secondary_ctls_high &=
2001 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
2002}
2003
2004static inline bool vmx_control_verify(u32 control, u32 low, u32 high)
2005{
2006 /*
2007 * Bits 0 in high must be 0, and bits 1 in low must be 1.
2008 */
2009 return ((control & high) | low) == control;
2010}
2011
2012static inline u64 vmx_control_msr(u32 low, u32 high)
2013{
2014 return low | ((u64)high << 32);
2015}
2016
2017/*
2018 * If we allow our guest to use VMX instructions (i.e., nested VMX), we should
2019 * also let it use VMX-specific MSRs.
2020 * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a
2021 * VMX-specific MSR, or 0 when we haven't (and the caller should handle it
2022 * like all other MSRs).
2023 */
2024static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2025{
2026 if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC &&
2027 msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) {
2028 /*
2029 * According to the spec, processors which do not support VMX
2030 * should throw a #GP(0) when VMX capability MSRs are read.
2031 */
2032 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
2033 return 1;
2034 }
2035
2036 switch (msr_index) {
2037 case MSR_IA32_FEATURE_CONTROL:
2038 *pdata = 0;
2039 break;
2040 case MSR_IA32_VMX_BASIC:
2041 /*
2042 * This MSR reports some information about VMX support. We
2043 * should return information about the VMX we emulate for the
2044 * guest, and the VMCS structure we give it - not about the
2045 * VMX support of the underlying hardware.
2046 */
2047 *pdata = VMCS12_REVISION |
2048 ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) |
2049 (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT);
2050 break;
2051 case MSR_IA32_VMX_TRUE_PINBASED_CTLS:
2052 case MSR_IA32_VMX_PINBASED_CTLS:
2053 *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low,
2054 nested_vmx_pinbased_ctls_high);
2055 break;
2056 case MSR_IA32_VMX_TRUE_PROCBASED_CTLS:
2057 case MSR_IA32_VMX_PROCBASED_CTLS:
2058 *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low,
2059 nested_vmx_procbased_ctls_high);
2060 break;
2061 case MSR_IA32_VMX_TRUE_EXIT_CTLS:
2062 case MSR_IA32_VMX_EXIT_CTLS:
2063 *pdata = vmx_control_msr(nested_vmx_exit_ctls_low,
2064 nested_vmx_exit_ctls_high);
2065 break;
2066 case MSR_IA32_VMX_TRUE_ENTRY_CTLS:
2067 case MSR_IA32_VMX_ENTRY_CTLS:
2068 *pdata = vmx_control_msr(nested_vmx_entry_ctls_low,
2069 nested_vmx_entry_ctls_high);
2070 break;
2071 case MSR_IA32_VMX_MISC:
2072 *pdata = 0;
2073 break;
2074 /*
2075 * These MSRs specify bits which the guest must keep fixed (on or off)
2076 * while L1 is in VMXON mode (in L1's root mode, or running an L2).
2077 * We picked the standard core2 setting.
2078 */
2079#define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE)
2080#define VMXON_CR4_ALWAYSON X86_CR4_VMXE
2081 case MSR_IA32_VMX_CR0_FIXED0:
2082 *pdata = VMXON_CR0_ALWAYSON;
2083 break;
2084 case MSR_IA32_VMX_CR0_FIXED1:
2085 *pdata = -1ULL;
2086 break;
2087 case MSR_IA32_VMX_CR4_FIXED0:
2088 *pdata = VMXON_CR4_ALWAYSON;
2089 break;
2090 case MSR_IA32_VMX_CR4_FIXED1:
2091 *pdata = -1ULL;
2092 break;
2093 case MSR_IA32_VMX_VMCS_ENUM:
2094 *pdata = 0x1f;
2095 break;
2096 case MSR_IA32_VMX_PROCBASED_CTLS2:
2097 *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low,
2098 nested_vmx_secondary_ctls_high);
2099 break;
2100 case MSR_IA32_VMX_EPT_VPID_CAP:
2101 /* Currently, no nested ept or nested vpid */
2102 *pdata = 0;
2103 break;
2104 default:
2105 return 0;
2106 }
2107
2108 return 1;
2109}
2110
2111static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
2112{
2113 if (!nested_vmx_allowed(vcpu))
2114 return 0;
2115
2116 if (msr_index == MSR_IA32_FEATURE_CONTROL)
2117 /* TODO: the right thing. */
2118 return 1;
2119 /*
2120 * No need to treat VMX capability MSRs specially: If we don't handle
2121 * them, handle_wrmsr will #GP(0), which is correct (they are readonly)
2122 */
2123 return 0;
2124}
2125
2126/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08002127 * Reads an msr value (of 'msr_index') into 'pdata'.
2128 * Returns 0 on success, non-0 otherwise.
2129 * Assumes vcpu_load() was already called.
2130 */
2131static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
2132{
2133 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03002134 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135
2136 if (!pdata) {
2137 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
2138 return -EINVAL;
2139 }
2140
2141 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002142#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143 case MSR_FS_BASE:
2144 data = vmcs_readl(GUEST_FS_BASE);
2145 break;
2146 case MSR_GS_BASE:
2147 data = vmcs_readl(GUEST_GS_BASE);
2148 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002149 case MSR_KERNEL_GS_BASE:
2150 vmx_load_host_state(to_vmx(vcpu));
2151 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
2152 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03002153#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08002154 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002155 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302156 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157 data = guest_read_tsc();
2158 break;
2159 case MSR_IA32_SYSENTER_CS:
2160 data = vmcs_read32(GUEST_SYSENTER_CS);
2161 break;
2162 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002163 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002164 break;
2165 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002166 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002168 case MSR_TSC_AUX:
2169 if (!to_vmx(vcpu)->rdtscp_enabled)
2170 return 1;
2171 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002173 if (vmx_get_vmx_msr(vcpu, msr_index, pdata))
2174 return 0;
Rusty Russell8b9cf982007-07-30 16:31:43 +10002175 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002176 if (msr) {
2177 data = msr->data;
2178 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002179 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002180 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181 }
2182
2183 *pdata = data;
2184 return 0;
2185}
2186
2187/*
2188 * Writes msr value into into the appropriate "register".
2189 * Returns 0 on success, non-0 otherwise.
2190 * Assumes vcpu_load() was already called.
2191 */
2192static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
2193{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002194 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002195 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03002196 int ret = 0;
2197
Avi Kivity6aa8b732006-12-10 02:21:36 -08002198 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08002199 case MSR_EFER:
Eddie Dong2cc51562007-05-21 07:28:09 +03002200 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03002201 break;
Avi Kivity16175a72009-03-23 22:13:44 +02002202#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002204 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002205 vmcs_writel(GUEST_FS_BASE, data);
2206 break;
2207 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03002208 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002209 vmcs_writel(GUEST_GS_BASE, data);
2210 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03002211 case MSR_KERNEL_GS_BASE:
2212 vmx_load_host_state(vmx);
2213 vmx->msr_guest_kernel_gs_base = data;
2214 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215#endif
2216 case MSR_IA32_SYSENTER_CS:
2217 vmcs_write32(GUEST_SYSENTER_CS, data);
2218 break;
2219 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002220 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002221 break;
2222 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02002223 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302225 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10002226 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227 break;
Sheng Yang468d4722008-10-09 16:01:55 +08002228 case MSR_IA32_CR_PAT:
2229 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2230 vmcs_write64(GUEST_IA32_PAT, data);
2231 vcpu->arch.pat = data;
2232 break;
2233 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002234 ret = kvm_set_msr_common(vcpu, msr_index, data);
2235 break;
2236 case MSR_TSC_AUX:
2237 if (!vmx->rdtscp_enabled)
2238 return 1;
2239 /* Check reserved bit, higher 32 bits should be zero */
2240 if ((data >> 32) != 0)
2241 return 1;
2242 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243 default:
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002244 if (vmx_set_vmx_msr(vcpu, msr_index, data))
2245 break;
Rusty Russell8b9cf982007-07-30 16:31:43 +10002246 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08002247 if (msr) {
2248 msr->data = data;
Avi Kivity2225fd52012-04-18 15:03:04 +03002249 if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
2250 preempt_disable();
Avi Kivity9ee73972012-03-06 14:16:33 +02002251 kvm_set_shared_msr(msr->index, msr->data,
2252 msr->mask);
Avi Kivity2225fd52012-04-18 15:03:04 +03002253 preempt_enable();
2254 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08002255 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002256 }
Eddie Dong2cc51562007-05-21 07:28:09 +03002257 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002258 }
2259
Eddie Dong2cc51562007-05-21 07:28:09 +03002260 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261}
2262
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002263static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002264{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002265 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
2266 switch (reg) {
2267 case VCPU_REGS_RSP:
2268 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
2269 break;
2270 case VCPU_REGS_RIP:
2271 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
2272 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002273 case VCPU_EXREG_PDPTR:
2274 if (enable_ept)
2275 ept_save_pdptrs(vcpu);
2276 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002277 default:
2278 break;
2279 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280}
2281
Jan Kiszka355be0b2009-10-03 00:31:21 +02002282static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01002284 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
2285 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
2286 else
2287 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2288
Avi Kivityabd3f2d2007-05-02 17:57:40 +03002289 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002290}
2291
2292static __init int cpu_has_kvm_support(void)
2293{
Eduardo Habkost6210e372008-11-17 19:03:16 -02002294 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295}
2296
2297static __init int vmx_disabled_by_bios(void)
2298{
2299 u64 msr;
2300
2301 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04002302 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08002303 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04002304 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
2305 && tboot_enabled())
2306 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08002307 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04002308 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08002309 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08002310 && !tboot_enabled()) {
2311 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08002312 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04002313 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08002314 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08002315 /* launched w/o TXT and VMX disabled */
2316 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
2317 && !tboot_enabled())
2318 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04002319 }
2320
2321 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322}
2323
Dongxiao Xu7725b892010-05-11 18:29:38 +08002324static void kvm_cpu_vmxon(u64 addr)
2325{
2326 asm volatile (ASM_VMX_VMXON_RAX
2327 : : "a"(&addr), "m"(addr)
2328 : "memory", "cc");
2329}
2330
Alexander Graf10474ae2009-09-15 11:37:46 +02002331static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002332{
2333 int cpu = raw_smp_processor_id();
2334 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04002335 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336
Alexander Graf10474ae2009-09-15 11:37:46 +02002337 if (read_cr4() & X86_CR4_VMXE)
2338 return -EBUSY;
2339
Nadav Har'Eld462b812011-05-24 15:26:10 +03002340 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002341 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04002342
2343 test_bits = FEATURE_CONTROL_LOCKED;
2344 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
2345 if (tboot_enabled())
2346 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
2347
2348 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04002350 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
2351 }
Rusty Russell66aee912007-07-17 23:34:16 +10002352 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02002353
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002354 if (vmm_exclusive) {
2355 kvm_cpu_vmxon(phys_addr);
2356 ept_sync_global();
2357 }
Alexander Graf10474ae2009-09-15 11:37:46 +02002358
Avi Kivity3444d7d2010-07-26 18:32:38 +03002359 store_gdt(&__get_cpu_var(host_gdt));
2360
Alexander Graf10474ae2009-09-15 11:37:46 +02002361 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362}
2363
Nadav Har'Eld462b812011-05-24 15:26:10 +03002364static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03002365{
2366 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03002367 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03002368
Nadav Har'Eld462b812011-05-24 15:26:10 +03002369 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
2370 loaded_vmcss_on_cpu_link)
2371 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03002372}
2373
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002374
2375/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
2376 * tricks.
2377 */
2378static void kvm_cpu_vmxoff(void)
2379{
2380 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02002381}
2382
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383static void hardware_disable(void *garbage)
2384{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002385 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03002386 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08002387 kvm_cpu_vmxoff();
2388 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08002389 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390}
2391
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002392static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04002393 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002394{
2395 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002396 u32 ctl = ctl_min | ctl_opt;
2397
2398 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2399
2400 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
2401 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
2402
2403 /* Ensure minimum (required) set of control bits are supported. */
2404 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002405 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002406
2407 *result = ctl;
2408 return 0;
2409}
2410
Avi Kivity110312c2010-12-21 12:54:20 +02002411static __init bool allow_1_setting(u32 msr, u32 ctl)
2412{
2413 u32 vmx_msr_low, vmx_msr_high;
2414
2415 rdmsr(msr, vmx_msr_low, vmx_msr_high);
2416 return vmx_msr_high & ctl;
2417}
2418
Yang, Sheng002c7f72007-07-31 14:23:01 +03002419static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002420{
2421 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08002422 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002423 u32 _pin_based_exec_control = 0;
2424 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002425 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002426 u32 _vmexit_control = 0;
2427 u32 _vmentry_control = 0;
2428
2429 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08002430 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002431 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
2432 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002433 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002434
Raghavendra K T10166742012-02-07 23:19:20 +05302435 min = CPU_BASED_HLT_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002436#ifdef CONFIG_X86_64
2437 CPU_BASED_CR8_LOAD_EXITING |
2438 CPU_BASED_CR8_STORE_EXITING |
2439#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002440 CPU_BASED_CR3_LOAD_EXITING |
2441 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002442 CPU_BASED_USE_IO_BITMAPS |
2443 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03002444 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08002445 CPU_BASED_MWAIT_EXITING |
2446 CPU_BASED_MONITOR_EXITING |
Avi Kivityfee84b02011-11-10 14:57:25 +02002447 CPU_BASED_INVLPG_EXITING |
2448 CPU_BASED_RDPMC_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06002449
Sheng Yangf78e0e22007-10-29 09:40:42 +08002450 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08002451 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08002452 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002453 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
2454 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002455 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002456#ifdef CONFIG_X86_64
2457 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
2458 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
2459 ~CPU_BASED_CR8_STORE_EXITING;
2460#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08002461 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002462 min2 = 0;
2463 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08002464 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08002465 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002466 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002467 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08002468 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
2469 SECONDARY_EXEC_RDTSCP;
Sheng Yangd56f5462008-04-25 10:13:16 +08002470 if (adjust_vmx_controls(min2, opt2,
2471 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002472 &_cpu_based_2nd_exec_control) < 0)
2473 return -EIO;
2474 }
2475#ifndef CONFIG_X86_64
2476 if (!(_cpu_based_2nd_exec_control &
2477 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
2478 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
2479#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08002480 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03002481 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
2482 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03002483 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
2484 CPU_BASED_CR3_STORE_EXITING |
2485 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08002486 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
2487 vmx_capability.ept, vmx_capability.vpid);
2488 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002489
2490 min = 0;
2491#ifdef CONFIG_X86_64
2492 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
2493#endif
Sheng Yang468d4722008-10-09 16:01:55 +08002494 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002495 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
2496 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002497 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002498
Sheng Yang468d4722008-10-09 16:01:55 +08002499 min = 0;
2500 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002501 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
2502 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002503 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002505 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002506
2507 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
2508 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002509 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002510
2511#ifdef CONFIG_X86_64
2512 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
2513 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03002514 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002515#endif
2516
2517 /* Require Write-Back (WB) memory type for VMCS accesses. */
2518 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03002519 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002520
Yang, Sheng002c7f72007-07-31 14:23:01 +03002521 vmcs_conf->size = vmx_msr_high & 0x1fff;
2522 vmcs_conf->order = get_order(vmcs_config.size);
2523 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002524
Yang, Sheng002c7f72007-07-31 14:23:01 +03002525 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
2526 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08002527 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03002528 vmcs_conf->vmexit_ctrl = _vmexit_control;
2529 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002530
Avi Kivity110312c2010-12-21 12:54:20 +02002531 cpu_has_load_ia32_efer =
2532 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2533 VM_ENTRY_LOAD_IA32_EFER)
2534 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2535 VM_EXIT_LOAD_IA32_EFER);
2536
Gleb Natapov8bf00a52011-10-05 14:01:22 +02002537 cpu_has_load_perf_global_ctrl =
2538 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
2539 VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
2540 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
2541 VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
2542
2543 /*
2544 * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL
2545 * but due to arrata below it can't be used. Workaround is to use
2546 * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL.
2547 *
2548 * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32]
2549 *
2550 * AAK155 (model 26)
2551 * AAP115 (model 30)
2552 * AAT100 (model 37)
2553 * BC86,AAY89,BD102 (model 44)
2554 * BA97 (model 46)
2555 *
2556 */
2557 if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) {
2558 switch (boot_cpu_data.x86_model) {
2559 case 26:
2560 case 30:
2561 case 37:
2562 case 44:
2563 case 46:
2564 cpu_has_load_perf_global_ctrl = false;
2565 printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
2566 "does not work properly. Using workaround\n");
2567 break;
2568 default:
2569 break;
2570 }
2571 }
2572
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002573 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08002574}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575
2576static struct vmcs *alloc_vmcs_cpu(int cpu)
2577{
2578 int node = cpu_to_node(cpu);
2579 struct page *pages;
2580 struct vmcs *vmcs;
2581
Mel Gorman6484eb32009-06-16 15:31:54 -07002582 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583 if (!pages)
2584 return NULL;
2585 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002586 memset(vmcs, 0, vmcs_config.size);
2587 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002588 return vmcs;
2589}
2590
2591static struct vmcs *alloc_vmcs(void)
2592{
Ingo Molnard3b2c332007-01-05 16:36:23 -08002593 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594}
2595
2596static void free_vmcs(struct vmcs *vmcs)
2597{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002598 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002599}
2600
Nadav Har'Eld462b812011-05-24 15:26:10 +03002601/*
2602 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
2603 */
2604static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
2605{
2606 if (!loaded_vmcs->vmcs)
2607 return;
2608 loaded_vmcs_clear(loaded_vmcs);
2609 free_vmcs(loaded_vmcs->vmcs);
2610 loaded_vmcs->vmcs = NULL;
2611}
2612
Sam Ravnborg39959582007-06-01 00:47:13 -07002613static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002614{
2615 int cpu;
2616
Zachary Amsden3230bb42009-09-29 11:38:37 -10002617 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002618 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10002619 per_cpu(vmxarea, cpu) = NULL;
2620 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621}
2622
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623static __init int alloc_kvm_area(void)
2624{
2625 int cpu;
2626
Zachary Amsden3230bb42009-09-29 11:38:37 -10002627 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628 struct vmcs *vmcs;
2629
2630 vmcs = alloc_vmcs_cpu(cpu);
2631 if (!vmcs) {
2632 free_kvm_area();
2633 return -ENOMEM;
2634 }
2635
2636 per_cpu(vmxarea, cpu) = vmcs;
2637 }
2638 return 0;
2639}
2640
2641static __init int hardware_setup(void)
2642{
Yang, Sheng002c7f72007-07-31 14:23:01 +03002643 if (setup_vmcs_config(&vmcs_config) < 0)
2644 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01002645
2646 if (boot_cpu_has(X86_FEATURE_NX))
2647 kvm_enable_efer_bits(EFER_NX);
2648
Sheng Yang93ba03c2009-04-01 15:52:32 +08002649 if (!cpu_has_vmx_vpid())
2650 enable_vpid = 0;
2651
Sheng Yang4bc9b982010-06-02 14:05:24 +08002652 if (!cpu_has_vmx_ept() ||
2653 !cpu_has_vmx_ept_4levels()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08002654 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002655 enable_unrestricted_guest = 0;
Xudong Hao83c3a332012-05-28 19:33:35 +08002656 enable_ept_ad_bits = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002657 }
2658
Xudong Hao83c3a332012-05-28 19:33:35 +08002659 if (!cpu_has_vmx_ept_ad_bits())
2660 enable_ept_ad_bits = 0;
2661
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002662 if (!cpu_has_vmx_unrestricted_guest())
2663 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08002664
2665 if (!cpu_has_vmx_flexpriority())
2666 flexpriority_enabled = 0;
2667
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002668 if (!cpu_has_vmx_tpr_shadow())
2669 kvm_x86_ops->update_cr8_intercept = NULL;
2670
Marcelo Tosatti54dee992009-06-11 12:07:44 -03002671 if (enable_ept && !cpu_has_vmx_ept_2m_page())
2672 kvm_disable_largepages();
2673
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002674 if (!cpu_has_vmx_ple())
2675 ple_gap = 0;
2676
Nadav Har'Elb87a51a2011-05-25 23:04:25 +03002677 if (nested)
2678 nested_vmx_setup_ctls_msrs();
2679
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 return alloc_kvm_area();
2681}
2682
2683static __exit void hardware_unsetup(void)
2684{
2685 free_kvm_area();
2686}
2687
Avi Kivity6aa8b732006-12-10 02:21:36 -08002688static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
2689{
2690 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2691
Avi Kivity6af11b92007-03-19 13:18:10 +02002692 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002693 vmcs_write16(sf->selector, save->selector);
2694 vmcs_writel(sf->base, save->base);
2695 vmcs_write32(sf->limit, save->limit);
2696 vmcs_write32(sf->ar_bytes, save->ar);
2697 } else {
2698 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
2699 << AR_DPL_SHIFT;
2700 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
2701 }
2702}
2703
2704static void enter_pmode(struct kvm_vcpu *vcpu)
2705{
2706 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002707 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002709 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002710 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711
Avi Kivity2fb92db2011-04-27 19:42:18 +03002712 vmx_segment_cache_clear(vmx);
2713
Avi Kivityd0ba64f2011-01-03 14:28:51 +02002714 vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002715 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
2716 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
2717 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002718
2719 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002720 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2721 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722 vmcs_writel(GUEST_RFLAGS, flags);
2723
Rusty Russell66aee912007-07-17 23:34:16 +10002724 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
2725 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726
2727 update_exception_bitmap(vcpu);
2728
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002729 if (emulate_invalid_guest_state)
2730 return;
2731
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002732 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
2733 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
2734 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
2735 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736
Avi Kivity2fb92db2011-04-27 19:42:18 +03002737 vmx_segment_cache_clear(vmx);
2738
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 vmcs_write16(GUEST_SS_SELECTOR, 0);
2740 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
2741
2742 vmcs_write16(GUEST_CS_SELECTOR,
2743 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
2744 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
2745}
2746
Mike Dayd77c26f2007-10-08 09:02:08 -04002747static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002749 if (!kvm->arch.tss_addr) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02002750 struct kvm_memslots *slots;
Xiao Guangrong28a37542011-11-24 19:04:35 +08002751 struct kvm_memory_slot *slot;
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02002752 gfn_t base_gfn;
2753
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08002754 slots = kvm_memslots(kvm);
Xiao Guangrong28a37542011-11-24 19:04:35 +08002755 slot = id_to_memslot(slots, 0);
2756 base_gfn = slot->base_gfn + slot->npages - 3;
2757
Izik Eiduscbc94022007-10-25 00:29:55 +02002758 return base_gfn << PAGE_SHIFT;
2759 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002760 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761}
2762
2763static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
2764{
2765 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2766
2767 save->selector = vmcs_read16(sf->selector);
2768 save->base = vmcs_readl(sf->base);
2769 save->limit = vmcs_read32(sf->limit);
2770 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01002771 vmcs_write16(sf->selector, save->base >> 4);
Gleb Natapov444e8632010-12-27 17:25:04 +02002772 vmcs_write32(sf->base, save->base & 0xffff0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002773 vmcs_write32(sf->limit, 0xffff);
2774 vmcs_write32(sf->ar_bytes, 0xf3);
Gleb Natapov444e8632010-12-27 17:25:04 +02002775 if (save->base & 0xf)
2776 printk_once(KERN_WARNING "kvm: segment base is not paragraph"
2777 " aligned when entering protected mode (seg=%d)",
2778 seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779}
2780
2781static void enter_rmode(struct kvm_vcpu *vcpu)
2782{
2783 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002784 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002786 if (enable_unrestricted_guest)
2787 return;
2788
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002789 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002790 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791
Gleb Natapov776e58e2011-03-13 12:34:27 +02002792 /*
2793 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
2794 * vcpu. Call it here with phys address pointing 16M below 4G.
2795 */
2796 if (!vcpu->kvm->arch.tss_addr) {
2797 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
2798 "called before entering vcpu\n");
2799 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
2800 vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
2801 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2802 }
2803
Avi Kivity2fb92db2011-04-27 19:42:18 +03002804 vmx_segment_cache_clear(vmx);
2805
Avi Kivityd0ba64f2011-01-03 14:28:51 +02002806 vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002807 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
2809
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002810 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
2812
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002813 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2815
2816 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03002817 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01002819 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002820
2821 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10002822 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823 update_exception_bitmap(vcpu);
2824
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002825 if (emulate_invalid_guest_state)
2826 goto continue_rmode;
2827
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
2829 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
2830 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
2831
2832 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08002833 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02002834 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
2835 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002836 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
2837
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002838 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
2839 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
2840 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
2841 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03002842
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002843continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08002844 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845}
2846
Amit Shah401d10d2009-02-20 22:53:37 +05302847static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
2848{
2849 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03002850 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
2851
2852 if (!msr)
2853 return;
Amit Shah401d10d2009-02-20 22:53:37 +05302854
Avi Kivity44ea2b12009-09-06 15:55:37 +03002855 /*
2856 * Force kernel_gs_base reloading before EFER changes, as control
2857 * of this msr depends on is_long_mode().
2858 */
2859 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02002860 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05302861 if (efer & EFER_LMA) {
2862 vmcs_write32(VM_ENTRY_CONTROLS,
2863 vmcs_read32(VM_ENTRY_CONTROLS) |
2864 VM_ENTRY_IA32E_MODE);
2865 msr->data = efer;
2866 } else {
2867 vmcs_write32(VM_ENTRY_CONTROLS,
2868 vmcs_read32(VM_ENTRY_CONTROLS) &
2869 ~VM_ENTRY_IA32E_MODE);
2870
2871 msr->data = efer & ~EFER_LME;
2872 }
2873 setup_msrs(vmx);
2874}
2875
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002876#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877
2878static void enter_lmode(struct kvm_vcpu *vcpu)
2879{
2880 u32 guest_tr_ar;
2881
Avi Kivity2fb92db2011-04-27 19:42:18 +03002882 vmx_segment_cache_clear(to_vmx(vcpu));
2883
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
2885 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
Jan Kiszkabd801582011-09-12 11:26:22 +02002886 pr_debug_ratelimited("%s: tss fixup for long mode. \n",
2887 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888 vmcs_write32(GUEST_TR_AR_BYTES,
2889 (guest_tr_ar & ~AR_TYPE_MASK)
2890 | AR_TYPE_BUSY_64_TSS);
2891 }
Avi Kivityda38f432010-07-06 11:30:49 +03002892 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002893}
2894
2895static void exit_lmode(struct kvm_vcpu *vcpu)
2896{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897 vmcs_write32(VM_ENTRY_CONTROLS,
2898 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03002899 & ~VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03002900 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901}
2902
2903#endif
2904
Sheng Yang2384d2b2008-01-17 15:14:33 +08002905static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2906{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002907 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08002908 if (enable_ept) {
2909 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2910 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08002911 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08002912 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08002913}
2914
Avi Kivitye8467fd2009-12-29 18:43:06 +02002915static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2916{
2917 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2918
2919 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
2920 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
2921}
2922
Avi Kivityaff48ba2010-12-05 18:56:11 +02002923static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
2924{
2925 if (enable_ept && is_paging(vcpu))
2926 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2927 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
2928}
2929
Anthony Liguori25c4c272007-04-27 09:29:21 +03002930static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002931{
Avi Kivityfc78f512009-12-07 12:16:48 +02002932 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2933
2934 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
2935 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08002936}
2937
Sheng Yang14394422008-04-28 12:24:45 +08002938static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
2939{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002940 if (!test_bit(VCPU_EXREG_PDPTR,
2941 (unsigned long *)&vcpu->arch.regs_dirty))
2942 return;
2943
Sheng Yang14394422008-04-28 12:24:45 +08002944 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02002945 vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
2946 vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
2947 vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
2948 vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002949 }
2950}
2951
Avi Kivity8f5d5492009-05-31 18:41:29 +03002952static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
2953{
2954 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02002955 vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2956 vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2957 vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2958 vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002959 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002960
2961 __set_bit(VCPU_EXREG_PDPTR,
2962 (unsigned long *)&vcpu->arch.regs_avail);
2963 __set_bit(VCPU_EXREG_PDPTR,
2964 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002965}
2966
Nadav Har'El5e1746d2011-05-25 23:03:24 +03002967static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
Sheng Yang14394422008-04-28 12:24:45 +08002968
2969static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
2970 unsigned long cr0,
2971 struct kvm_vcpu *vcpu)
2972{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03002973 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
2974 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002975 if (!(cr0 & X86_CR0_PG)) {
2976 /* From paging/starting to nonpaging */
2977 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002978 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08002979 (CPU_BASED_CR3_LOAD_EXITING |
2980 CPU_BASED_CR3_STORE_EXITING));
2981 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002982 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002983 } else if (!is_paging(vcpu)) {
2984 /* From nonpaging to paging */
2985 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002986 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08002987 ~(CPU_BASED_CR3_LOAD_EXITING |
2988 CPU_BASED_CR3_STORE_EXITING));
2989 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002990 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002991 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08002992
2993 if (!(cr0 & X86_CR0_WP))
2994 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08002995}
2996
Avi Kivity6aa8b732006-12-10 02:21:36 -08002997static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2998{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002999 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003000 unsigned long hw_cr0;
3001
3002 if (enable_unrestricted_guest)
3003 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
3004 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
3005 else
3006 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08003007
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003008 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003009 enter_pmode(vcpu);
3010
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003011 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003012 enter_rmode(vcpu);
3013
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003014#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02003015 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10003016 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10003018 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003019 exit_lmode(vcpu);
3020 }
3021#endif
3022
Avi Kivity089d0342009-03-23 18:26:32 +02003023 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003024 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3025
Avi Kivity02daab22009-12-30 12:40:26 +02003026 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02003027 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02003028
Avi Kivity6aa8b732006-12-10 02:21:36 -08003029 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08003030 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003031 vcpu->arch.cr0 = cr0;
Avi Kivity69c73022011-03-07 15:26:44 +02003032 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033}
3034
Sheng Yang14394422008-04-28 12:24:45 +08003035static u64 construct_eptp(unsigned long root_hpa)
3036{
3037 u64 eptp;
3038
3039 /* TODO write the value reading from MSR */
3040 eptp = VMX_EPT_DEFAULT_MT |
3041 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
3042 eptp |= (root_hpa & PAGE_MASK);
3043
3044 return eptp;
3045}
3046
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3048{
Sheng Yang14394422008-04-28 12:24:45 +08003049 unsigned long guest_cr3;
3050 u64 eptp;
3051
3052 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02003053 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08003054 eptp = construct_eptp(cr3);
3055 vmcs_write64(EPT_POINTER, eptp);
Avi Kivity9f8fe502010-12-05 17:30:00 +02003056 guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
Sheng Yangb927a3c2009-07-21 10:42:48 +08003057 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02003058 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003059 }
3060
Sheng Yang2384d2b2008-01-17 15:14:33 +08003061 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08003062 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003063}
3064
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003065static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003067 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08003068 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
3069
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003070 if (cr4 & X86_CR4_VMXE) {
3071 /*
3072 * To use VMXON (and later other VMX instructions), a guest
3073 * must first be able to turn on cr4.VMXE (see handle_vmon()).
3074 * So basically the check on whether to allow nested VMX
3075 * is here.
3076 */
3077 if (!nested_vmx_allowed(vcpu))
3078 return 1;
3079 } else if (to_vmx(vcpu)->nested.vmxon)
3080 return 1;
3081
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003082 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02003083 if (enable_ept) {
3084 if (!is_paging(vcpu)) {
3085 hw_cr4 &= ~X86_CR4_PAE;
3086 hw_cr4 |= X86_CR4_PSE;
3087 } else if (!(cr4 & X86_CR4_PAE)) {
3088 hw_cr4 &= ~X86_CR4_PAE;
3089 }
3090 }
Sheng Yang14394422008-04-28 12:24:45 +08003091
3092 vmcs_writel(CR4_READ_SHADOW, cr4);
3093 vmcs_writel(GUEST_CR4, hw_cr4);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03003094 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095}
3096
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097static void vmx_get_segment(struct kvm_vcpu *vcpu,
3098 struct kvm_segment *var, int seg)
3099{
Avi Kivitya9179492011-01-03 14:28:52 +02003100 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitya9179492011-01-03 14:28:52 +02003101 struct kvm_save_segment *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102 u32 ar;
3103
Avi Kivitya9179492011-01-03 14:28:52 +02003104 if (vmx->rmode.vm86_active
3105 && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES
3106 || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS
3107 || seg == VCPU_SREG_GS)
3108 && !emulate_invalid_guest_state) {
3109 switch (seg) {
3110 case VCPU_SREG_TR: save = &vmx->rmode.tr; break;
3111 case VCPU_SREG_ES: save = &vmx->rmode.es; break;
3112 case VCPU_SREG_DS: save = &vmx->rmode.ds; break;
3113 case VCPU_SREG_FS: save = &vmx->rmode.fs; break;
3114 case VCPU_SREG_GS: save = &vmx->rmode.gs; break;
3115 default: BUG();
3116 }
3117 var->selector = save->selector;
3118 var->base = save->base;
3119 var->limit = save->limit;
3120 ar = save->ar;
3121 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03003122 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivitya9179492011-01-03 14:28:52 +02003123 goto use_saved_rmode_seg;
3124 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003125 var->base = vmx_read_guest_seg_base(vmx, seg);
3126 var->limit = vmx_read_guest_seg_limit(vmx, seg);
3127 var->selector = vmx_read_guest_seg_selector(vmx, seg);
3128 ar = vmx_read_guest_seg_ar(vmx, seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003129use_saved_rmode_seg:
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02003130 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003131 ar = 0;
3132 var->type = ar & 15;
3133 var->s = (ar >> 4) & 1;
3134 var->dpl = (ar >> 5) & 3;
3135 var->present = (ar >> 7) & 1;
3136 var->avl = (ar >> 12) & 1;
3137 var->l = (ar >> 13) & 1;
3138 var->db = (ar >> 14) & 1;
3139 var->g = (ar >> 15) & 1;
3140 var->unusable = (ar >> 16) & 1;
3141}
3142
Avi Kivitya9179492011-01-03 14:28:52 +02003143static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3144{
Avi Kivitya9179492011-01-03 14:28:52 +02003145 struct kvm_segment s;
3146
3147 if (to_vmx(vcpu)->rmode.vm86_active) {
3148 vmx_get_segment(vcpu, &s, seg);
3149 return s.base;
3150 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03003151 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02003152}
3153
Avi Kivity69c73022011-03-07 15:26:44 +02003154static int __vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02003155{
Avi Kivity3eeb3282010-01-21 15:31:48 +02003156 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02003157 return 0;
3158
Avi Kivityf4c63e52011-03-07 14:54:28 +02003159 if (!is_long_mode(vcpu)
3160 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
Izik Eidus2e4d2652008-03-24 19:38:34 +02003161 return 3;
3162
Avi Kivity2fb92db2011-04-27 19:42:18 +03003163 return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02003164}
3165
Avi Kivity69c73022011-03-07 15:26:44 +02003166static int vmx_get_cpl(struct kvm_vcpu *vcpu)
3167{
3168 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
3169 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
3170 to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu);
3171 }
3172 return to_vmx(vcpu)->cpl;
3173}
3174
3175
Avi Kivity653e3102007-05-07 10:55:37 +03003176static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003177{
Avi Kivity6aa8b732006-12-10 02:21:36 -08003178 u32 ar;
3179
Avi Kivity653e3102007-05-07 10:55:37 +03003180 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181 ar = 1 << 16;
3182 else {
3183 ar = var->type & 15;
3184 ar |= (var->s & 1) << 4;
3185 ar |= (var->dpl & 3) << 5;
3186 ar |= (var->present & 1) << 7;
3187 ar |= (var->avl & 1) << 12;
3188 ar |= (var->l & 1) << 13;
3189 ar |= (var->db & 1) << 14;
3190 ar |= (var->g & 1) << 15;
3191 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08003192 if (ar == 0) /* a 0 value means unusable */
3193 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03003194
3195 return ar;
3196}
3197
3198static void vmx_set_segment(struct kvm_vcpu *vcpu,
3199 struct kvm_segment *var, int seg)
3200{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003201 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03003202 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
3203 u32 ar;
3204
Avi Kivity2fb92db2011-04-27 19:42:18 +03003205 vmx_segment_cache_clear(vmx);
3206
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003207 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
Gleb Natapova8ba6c22011-02-21 12:07:58 +02003208 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003209 vmx->rmode.tr.selector = var->selector;
3210 vmx->rmode.tr.base = var->base;
3211 vmx->rmode.tr.limit = var->limit;
3212 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03003213 return;
3214 }
3215 vmcs_writel(sf->base, var->base);
3216 vmcs_write32(sf->limit, var->limit);
3217 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003218 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03003219 /*
3220 * Hack real-mode segments into vm86 compatibility.
3221 */
3222 if (var->base == 0xffff0000 && var->selector == 0xf000)
3223 vmcs_writel(sf->base, 0xf0000);
3224 ar = 0xf3;
3225 } else
3226 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003227
3228 /*
3229 * Fix the "Accessed" bit in AR field of segment registers for older
3230 * qemu binaries.
3231 * IA32 arch specifies that at the time of processor reset the
3232 * "Accessed" bit in the AR field of segment registers is 1. And qemu
3233 * is setting it to 0 in the usedland code. This causes invalid guest
3234 * state vmexit when "unrestricted guest" mode is turned on.
3235 * Fix for this setup issue in cpu_reset is being pushed in the qemu
3236 * tree. Newer qemu binaries with that qemu fix would not need this
3237 * kvm hack.
3238 */
3239 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
3240 ar |= 0x1; /* Accessed */
3241
Avi Kivity6aa8b732006-12-10 02:21:36 -08003242 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity69c73022011-03-07 15:26:44 +02003243 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003244}
3245
Avi Kivity6aa8b732006-12-10 02:21:36 -08003246static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
3247{
Avi Kivity2fb92db2011-04-27 19:42:18 +03003248 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003249
3250 *db = (ar >> 14) & 1;
3251 *l = (ar >> 13) & 1;
3252}
3253
Gleb Natapov89a27f42010-02-16 10:51:48 +02003254static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003255{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003256 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
3257 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258}
3259
Gleb Natapov89a27f42010-02-16 10:51:48 +02003260static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003261{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003262 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
3263 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003264}
3265
Gleb Natapov89a27f42010-02-16 10:51:48 +02003266static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003268 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
3269 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003270}
3271
Gleb Natapov89a27f42010-02-16 10:51:48 +02003272static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003273{
Gleb Natapov89a27f42010-02-16 10:51:48 +02003274 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
3275 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276}
3277
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003278static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
3279{
3280 struct kvm_segment var;
3281 u32 ar;
3282
3283 vmx_get_segment(vcpu, &var, seg);
3284 ar = vmx_segment_access_rights(&var);
3285
3286 if (var.base != (var.selector << 4))
3287 return false;
3288 if (var.limit != 0xffff)
3289 return false;
3290 if (ar != 0xf3)
3291 return false;
3292
3293 return true;
3294}
3295
3296static bool code_segment_valid(struct kvm_vcpu *vcpu)
3297{
3298 struct kvm_segment cs;
3299 unsigned int cs_rpl;
3300
3301 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3302 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
3303
Avi Kivity1872a3f2009-01-04 23:26:52 +02003304 if (cs.unusable)
3305 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003306 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
3307 return false;
3308 if (!cs.s)
3309 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003310 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003311 if (cs.dpl > cs_rpl)
3312 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003313 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003314 if (cs.dpl != cs_rpl)
3315 return false;
3316 }
3317 if (!cs.present)
3318 return false;
3319
3320 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
3321 return true;
3322}
3323
3324static bool stack_segment_valid(struct kvm_vcpu *vcpu)
3325{
3326 struct kvm_segment ss;
3327 unsigned int ss_rpl;
3328
3329 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3330 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
3331
Avi Kivity1872a3f2009-01-04 23:26:52 +02003332 if (ss.unusable)
3333 return true;
3334 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003335 return false;
3336 if (!ss.s)
3337 return false;
3338 if (ss.dpl != ss_rpl) /* DPL != RPL */
3339 return false;
3340 if (!ss.present)
3341 return false;
3342
3343 return true;
3344}
3345
3346static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
3347{
3348 struct kvm_segment var;
3349 unsigned int rpl;
3350
3351 vmx_get_segment(vcpu, &var, seg);
3352 rpl = var.selector & SELECTOR_RPL_MASK;
3353
Avi Kivity1872a3f2009-01-04 23:26:52 +02003354 if (var.unusable)
3355 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003356 if (!var.s)
3357 return false;
3358 if (!var.present)
3359 return false;
3360 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
3361 if (var.dpl < rpl) /* DPL < RPL */
3362 return false;
3363 }
3364
3365 /* TODO: Add other members to kvm_segment_field to allow checking for other access
3366 * rights flags
3367 */
3368 return true;
3369}
3370
3371static bool tr_valid(struct kvm_vcpu *vcpu)
3372{
3373 struct kvm_segment tr;
3374
3375 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
3376
Avi Kivity1872a3f2009-01-04 23:26:52 +02003377 if (tr.unusable)
3378 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003379 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3380 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02003381 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003382 return false;
3383 if (!tr.present)
3384 return false;
3385
3386 return true;
3387}
3388
3389static bool ldtr_valid(struct kvm_vcpu *vcpu)
3390{
3391 struct kvm_segment ldtr;
3392
3393 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
3394
Avi Kivity1872a3f2009-01-04 23:26:52 +02003395 if (ldtr.unusable)
3396 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003397 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
3398 return false;
3399 if (ldtr.type != 2)
3400 return false;
3401 if (!ldtr.present)
3402 return false;
3403
3404 return true;
3405}
3406
3407static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
3408{
3409 struct kvm_segment cs, ss;
3410
3411 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
3412 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
3413
3414 return ((cs.selector & SELECTOR_RPL_MASK) ==
3415 (ss.selector & SELECTOR_RPL_MASK));
3416}
3417
3418/*
3419 * Check if guest state is valid. Returns true if valid, false if
3420 * not.
3421 * We assume that registers are always usable
3422 */
3423static bool guest_state_valid(struct kvm_vcpu *vcpu)
3424{
3425 /* real mode guest state checks */
Avi Kivity3eeb3282010-01-21 15:31:48 +02003426 if (!is_protmode(vcpu)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03003427 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
3428 return false;
3429 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
3430 return false;
3431 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
3432 return false;
3433 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
3434 return false;
3435 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
3436 return false;
3437 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
3438 return false;
3439 } else {
3440 /* protected mode guest state checks */
3441 if (!cs_ss_rpl_check(vcpu))
3442 return false;
3443 if (!code_segment_valid(vcpu))
3444 return false;
3445 if (!stack_segment_valid(vcpu))
3446 return false;
3447 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
3448 return false;
3449 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
3450 return false;
3451 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
3452 return false;
3453 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
3454 return false;
3455 if (!tr_valid(vcpu))
3456 return false;
3457 if (!ldtr_valid(vcpu))
3458 return false;
3459 }
3460 /* TODO:
3461 * - Add checks on RIP
3462 * - Add checks on RFLAGS
3463 */
3464
3465 return true;
3466}
3467
Mike Dayd77c26f2007-10-08 09:02:08 -04003468static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003469{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003470 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02003471 u16 data = 0;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003472 int r, idx, ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003473
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003474 idx = srcu_read_lock(&kvm->srcu);
3475 fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02003476 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3477 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003478 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003479 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08003480 r = kvm_write_guest_page(kvm, fn++, &data,
3481 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02003482 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003483 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003484 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
3485 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003486 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003487 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
3488 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003489 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02003490 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003491 r = kvm_write_guest_page(kvm, fn, &data,
3492 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
3493 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02003494 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003495 goto out;
3496
3497 ret = 1;
3498out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003499 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05003500 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003501}
3502
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003503static int init_rmode_identity_map(struct kvm *kvm)
3504{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003505 int i, idx, r, ret;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003506 pfn_t identity_map_pfn;
3507 u32 tmp;
3508
Avi Kivity089d0342009-03-23 18:26:32 +02003509 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003510 return 1;
3511 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
3512 printk(KERN_ERR "EPT: identity-mapping pagetable "
3513 "haven't been allocated!\n");
3514 return 0;
3515 }
3516 if (likely(kvm->arch.ept_identity_pagetable_done))
3517 return 1;
3518 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003519 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003520 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003521 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
3522 if (r < 0)
3523 goto out;
3524 /* Set up identity-mapping pagetable for EPT in real mode */
3525 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
3526 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
3527 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
3528 r = kvm_write_guest_page(kvm, identity_map_pfn,
3529 &tmp, i * sizeof(tmp), sizeof(tmp));
3530 if (r < 0)
3531 goto out;
3532 }
3533 kvm->arch.ept_identity_pagetable_done = true;
3534 ret = 1;
3535out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08003536 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003537 return ret;
3538}
3539
Avi Kivity6aa8b732006-12-10 02:21:36 -08003540static void seg_setup(int seg)
3541{
3542 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003543 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003544
3545 vmcs_write16(sf->selector, 0);
3546 vmcs_writel(sf->base, 0);
3547 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07003548 if (enable_unrestricted_guest) {
3549 ar = 0x93;
3550 if (seg == VCPU_SREG_CS)
3551 ar |= 0x08; /* code segment */
3552 } else
3553 ar = 0xf3;
3554
3555 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003556}
3557
Sheng Yangf78e0e22007-10-29 09:40:42 +08003558static int alloc_apic_access_page(struct kvm *kvm)
3559{
3560 struct kvm_userspace_memory_region kvm_userspace_mem;
3561 int r = 0;
3562
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003563 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003564 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003565 goto out;
3566 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
3567 kvm_userspace_mem.flags = 0;
3568 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
3569 kvm_userspace_mem.memory_size = PAGE_SIZE;
3570 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
3571 if (r)
3572 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02003573
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003574 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003575out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003576 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003577 return r;
3578}
3579
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003580static int alloc_identity_pagetable(struct kvm *kvm)
3581{
3582 struct kvm_userspace_memory_region kvm_userspace_mem;
3583 int r = 0;
3584
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003585 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003586 if (kvm->arch.ept_identity_pagetable)
3587 goto out;
3588 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
3589 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003590 kvm_userspace_mem.guest_phys_addr =
3591 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003592 kvm_userspace_mem.memory_size = PAGE_SIZE;
3593 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
3594 if (r)
3595 goto out;
3596
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003597 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08003598 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003599out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02003600 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003601 return r;
3602}
3603
Sheng Yang2384d2b2008-01-17 15:14:33 +08003604static void allocate_vpid(struct vcpu_vmx *vmx)
3605{
3606 int vpid;
3607
3608 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02003609 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08003610 return;
3611 spin_lock(&vmx_vpid_lock);
3612 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
3613 if (vpid < VMX_NR_VPIDS) {
3614 vmx->vpid = vpid;
3615 __set_bit(vpid, vmx_vpid_bitmap);
3616 }
3617 spin_unlock(&vmx_vpid_lock);
3618}
3619
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003620static void free_vpid(struct vcpu_vmx *vmx)
3621{
3622 if (!enable_vpid)
3623 return;
3624 spin_lock(&vmx_vpid_lock);
3625 if (vmx->vpid != 0)
3626 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3627 spin_unlock(&vmx_vpid_lock);
3628}
3629
Avi Kivity58972972009-02-24 22:26:47 +02003630static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08003631{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003632 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08003633
3634 if (!cpu_has_vmx_msr_bitmap())
3635 return;
3636
3637 /*
3638 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
3639 * have the write-low and read-high bitmap offsets the wrong way round.
3640 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
3641 */
Sheng Yang25c5f222008-03-28 13:18:56 +08003642 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003643 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
3644 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08003645 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
3646 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003647 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
3648 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08003649 }
Sheng Yang25c5f222008-03-28 13:18:56 +08003650}
3651
Avi Kivity58972972009-02-24 22:26:47 +02003652static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
3653{
3654 if (!longmode_only)
3655 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
3656 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
3657}
3658
Avi Kivity6aa8b732006-12-10 02:21:36 -08003659/*
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003660 * Set up the vmcs's constant host-state fields, i.e., host-state fields that
3661 * will not change in the lifetime of the guest.
3662 * Note that host-state that does change is set elsewhere. E.g., host-state
3663 * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
3664 */
3665static void vmx_set_constant_host_state(void)
3666{
3667 u32 low32, high32;
3668 unsigned long tmpl;
3669 struct desc_ptr dt;
3670
3671 vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
3672 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
3673 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
3674
3675 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003676#ifdef CONFIG_X86_64
3677 /*
3678 * Load null selectors, so we can avoid reloading them in
3679 * __vmx_load_host_state(), in case userspace uses the null selectors
3680 * too (the expected case).
3681 */
3682 vmcs_write16(HOST_DS_SELECTOR, 0);
3683 vmcs_write16(HOST_ES_SELECTOR, 0);
3684#else
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003685 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3686 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityb2da15a2012-05-13 19:53:24 +03003687#endif
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003688 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
3689 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
3690
3691 native_store_idt(&dt);
3692 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
3693
3694 asm("mov $.Lkvm_vmx_return, %0" : "=r"(tmpl));
3695 vmcs_writel(HOST_RIP, tmpl); /* 22.2.5 */
3696
3697 rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
3698 vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
3699 rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
3700 vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
3701
3702 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
3703 rdmsr(MSR_IA32_CR_PAT, low32, high32);
3704 vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
3705 }
3706}
3707
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003708static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
3709{
3710 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
3711 if (enable_ept)
3712 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03003713 if (is_guest_mode(&vmx->vcpu))
3714 vmx->vcpu.arch.cr4_guest_owned_bits &=
3715 ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003716 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
3717}
3718
3719static u32 vmx_exec_control(struct vcpu_vmx *vmx)
3720{
3721 u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
3722 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
3723 exec_control &= ~CPU_BASED_TPR_SHADOW;
3724#ifdef CONFIG_X86_64
3725 exec_control |= CPU_BASED_CR8_STORE_EXITING |
3726 CPU_BASED_CR8_LOAD_EXITING;
3727#endif
3728 }
3729 if (!enable_ept)
3730 exec_control |= CPU_BASED_CR3_STORE_EXITING |
3731 CPU_BASED_CR3_LOAD_EXITING |
3732 CPU_BASED_INVLPG_EXITING;
3733 return exec_control;
3734}
3735
3736static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)
3737{
3738 u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
3739 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
3740 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
3741 if (vmx->vpid == 0)
3742 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
3743 if (!enable_ept) {
3744 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
3745 enable_unrestricted_guest = 0;
3746 }
3747 if (!enable_unrestricted_guest)
3748 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
3749 if (!ple_gap)
3750 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
3751 return exec_control;
3752}
3753
Xiao Guangrongce88dec2011-07-12 03:33:44 +08003754static void ept_set_mmio_spte_mask(void)
3755{
3756 /*
3757 * EPT Misconfigurations can be generated if the value of bits 2:0
3758 * of an EPT paging-structure entry is 110b (write/execute).
3759 * Also, magic bits (0xffull << 49) is set to quickly identify mmio
3760 * spte.
3761 */
3762 kvm_mmu_set_mmio_spte_mask(0xffull << 49 | 0x6ull);
3763}
3764
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003765/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003766 * Sets up the vmcs for emulated real mode.
3767 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003768static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003769{
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02003770#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003771 unsigned long a;
Jan Kiszka2e4ce7f2011-06-01 12:57:30 +02003772#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003773 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003774
Avi Kivity6aa8b732006-12-10 02:21:36 -08003775 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003776 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
3777 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003778
Sheng Yang25c5f222008-03-28 13:18:56 +08003779 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02003780 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08003781
Avi Kivity6aa8b732006-12-10 02:21:36 -08003782 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
3783
Avi Kivity6aa8b732006-12-10 02:21:36 -08003784 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003785 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
3786 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003787
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003788 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003789
Sheng Yang83ff3b92007-11-21 14:33:25 +08003790 if (cpu_has_secondary_exec_ctrls()) {
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003791 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
3792 vmx_secondary_exec_control(vmx));
Sheng Yang83ff3b92007-11-21 14:33:25 +08003793 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08003794
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003795 if (ple_gap) {
3796 vmcs_write32(PLE_GAP, ple_gap);
3797 vmcs_write32(PLE_WINDOW, ple_window);
3798 }
3799
Xiao Guangrongc3707952011-07-12 03:28:04 +08003800 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
3801 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003802 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
3803
Avi Kivity9581d442010-10-19 16:46:55 +02003804 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
3805 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003806 vmx_set_constant_host_state();
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003807#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08003808 rdmsrl(MSR_FS_BASE, a);
3809 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
3810 rdmsrl(MSR_GS_BASE, a);
3811 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
3812#else
3813 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
3814 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
3815#endif
3816
Eddie Dong2cc51562007-05-21 07:28:09 +03003817 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
3818 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03003819 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03003820 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03003821 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003822
Sheng Yang468d4722008-10-09 16:01:55 +08003823 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
Nadav Har'Ela3a8ff82011-05-25 23:09:01 +03003824 u32 msr_low, msr_high;
3825 u64 host_pat;
Sheng Yang468d4722008-10-09 16:01:55 +08003826 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
3827 host_pat = msr_low | ((u64) msr_high << 32);
3828 /* Write the default value follow host pat */
3829 vmcs_write64(GUEST_IA32_PAT, host_pat);
3830 /* Keep arch.pat sync with GUEST_IA32_PAT */
3831 vmx->vcpu.arch.pat = host_pat;
3832 }
3833
Avi Kivity6aa8b732006-12-10 02:21:36 -08003834 for (i = 0; i < NR_VMX_MSR; ++i) {
3835 u32 index = vmx_msr_index[i];
3836 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003837 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003838
3839 if (rdmsr_safe(index, &data_low, &data_high) < 0)
3840 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08003841 if (wrmsr_safe(index, data_low, data_high) < 0)
3842 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03003843 vmx->guest_msrs[j].index = i;
3844 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02003845 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003846 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003848
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003849 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850
3851 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03003852 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
3853
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003854 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Nadav Har'Elbf8179a2011-05-25 23:09:31 +03003855 set_cr4_guest_host_mask(vmx);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003856
Zachary Amsden99e3e302010-08-19 22:07:17 -10003857 kvm_write_tsc(&vmx->vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003858
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003859 return 0;
3860}
3861
3862static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
3863{
3864 struct vcpu_vmx *vmx = to_vmx(vcpu);
3865 u64 msr;
Xiao Guangrong4b9d3a02010-06-08 10:15:51 +08003866 int ret;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003867
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003868 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003869
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003870 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003871
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003872 vmx->soft_vnmi_blocked = 0;
3873
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003874 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003875 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003876 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03003877 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003878 msr |= MSR_IA32_APICBASE_BSP;
3879 kvm_set_apic_base(&vmx->vcpu, msr);
3880
Jan Kiszka10ab25c2010-05-25 16:01:50 +02003881 ret = fx_init(&vmx->vcpu);
3882 if (ret != 0)
3883 goto out;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003884
Avi Kivity2fb92db2011-04-27 19:42:18 +03003885 vmx_segment_cache_clear(vmx);
3886
Avi Kivity5706be02008-08-20 15:07:31 +03003887 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003888 /*
3889 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
3890 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
3891 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03003892 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003893 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
3894 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
3895 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003896 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
3897 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003898 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003899
3900 seg_setup(VCPU_SREG_DS);
3901 seg_setup(VCPU_SREG_ES);
3902 seg_setup(VCPU_SREG_FS);
3903 seg_setup(VCPU_SREG_GS);
3904 seg_setup(VCPU_SREG_SS);
3905
3906 vmcs_write16(GUEST_TR_SELECTOR, 0);
3907 vmcs_writel(GUEST_TR_BASE, 0);
3908 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
3909 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3910
3911 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
3912 vmcs_writel(GUEST_LDTR_BASE, 0);
3913 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
3914 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
3915
3916 vmcs_write32(GUEST_SYSENTER_CS, 0);
3917 vmcs_writel(GUEST_SYSENTER_ESP, 0);
3918 vmcs_writel(GUEST_SYSENTER_EIP, 0);
3919
3920 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03003921 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003922 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003923 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003924 kvm_rip_write(vcpu, 0);
3925 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003926
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003927 vmcs_writel(GUEST_DR7, 0x400);
3928
3929 vmcs_writel(GUEST_GDTR_BASE, 0);
3930 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
3931
3932 vmcs_writel(GUEST_IDTR_BASE, 0);
3933 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
3934
Anthony Liguori443381a2010-12-06 10:53:38 -06003935 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003936 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
3937 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
3938
Avi Kivitye00c8cf2007-10-21 11:00:39 +02003939 /* Special registers */
3940 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
3941
3942 setup_msrs(vmx);
3943
Avi Kivity6aa8b732006-12-10 02:21:36 -08003944 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
3945
Sheng Yangf78e0e22007-10-29 09:40:42 +08003946 if (cpu_has_vmx_tpr_shadow()) {
3947 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
3948 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
3949 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09003950 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08003951 vmcs_write32(TPR_THRESHOLD, 0);
3952 }
3953
3954 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
3955 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003956 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003957
Sheng Yang2384d2b2008-01-17 15:14:33 +08003958 if (vmx->vpid != 0)
3959 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
3960
Eduardo Habkostfa400522009-10-24 02:49:58 -02003961 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Marcelo Tosatti7a4f5ad2012-03-27 19:47:26 -03003962 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02003963 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Marcelo Tosatti7a4f5ad2012-03-27 19:47:26 -03003964 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003965 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003966 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003967 vmx_fpu_activate(&vmx->vcpu);
3968 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003969
Gui Jianfengb9d762f2010-06-07 10:32:29 +08003970 vpid_sync_context(vmx);
Sheng Yang2384d2b2008-01-17 15:14:33 +08003971
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003972 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003973
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003974 /* HACK: Don't enable emulation on guest boot/reset */
3975 vmx->emulation_required = 0;
3976
Avi Kivity6aa8b732006-12-10 02:21:36 -08003977out:
3978 return ret;
3979}
3980
Nadav Har'Elb6f12502011-05-25 23:13:06 +03003981/*
3982 * In nested virtualization, check if L1 asked to exit on external interrupts.
3983 * For most existing hypervisors, this will always return true.
3984 */
3985static bool nested_exit_on_intr(struct kvm_vcpu *vcpu)
3986{
3987 return get_vmcs12(vcpu)->pin_based_vm_exec_control &
3988 PIN_BASED_EXT_INTR_MASK;
3989}
3990
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003991static void enable_irq_window(struct kvm_vcpu *vcpu)
3992{
3993 u32 cpu_based_vm_exec_control;
Nadav Har'Eld6185f22011-09-22 13:52:56 +03003994 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
3995 /*
3996 * We get here if vmx_interrupt_allowed() said we can't
3997 * inject to L1 now because L2 must run. Ask L2 to exit
3998 * right after entry, so we can inject to L1 more promptly.
Nadav Har'Elb6f12502011-05-25 23:13:06 +03003999 */
Nadav Har'Eld6185f22011-09-22 13:52:56 +03004000 kvm_make_request(KVM_REQ_IMMEDIATE_EXIT, vcpu);
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004001 return;
Nadav Har'Eld6185f22011-09-22 13:52:56 +03004002 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004003
4004 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4005 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
4006 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4007}
4008
4009static void enable_nmi_window(struct kvm_vcpu *vcpu)
4010{
4011 u32 cpu_based_vm_exec_control;
4012
4013 if (!cpu_has_virtual_nmis()) {
4014 enable_irq_window(vcpu);
4015 return;
4016 }
4017
Avi Kivity30bd0c42010-11-01 23:20:48 +02004018 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
4019 enable_irq_window(vcpu);
4020 return;
4021 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004022 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4023 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
4024 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4025}
4026
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004027static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03004028{
Avi Kivity9c8cba32007-11-22 11:42:59 +02004029 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004030 uint32_t intr;
4031 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02004032
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004033 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004034
Avi Kivityfa89a812008-09-01 15:57:51 +03004035 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004036 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004037 int inc_eip = 0;
4038 if (vcpu->arch.interrupt.soft)
4039 inc_eip = vcpu->arch.event_exit_inst_len;
4040 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004041 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03004042 return;
4043 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004044 intr = irq | INTR_INFO_VALID_MASK;
4045 if (vcpu->arch.interrupt.soft) {
4046 intr |= INTR_TYPE_SOFT_INTR;
4047 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
4048 vmx->vcpu.arch.event_exit_inst_len);
4049 } else
4050 intr |= INTR_TYPE_EXT_INTR;
4051 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03004052}
4053
Sheng Yangf08864b2008-05-15 18:23:25 +08004054static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
4055{
Jan Kiszka66a5a342008-09-26 09:30:51 +02004056 struct vcpu_vmx *vmx = to_vmx(vcpu);
4057
Nadav Har'El0b6ac342011-05-25 23:13:36 +03004058 if (is_guest_mode(vcpu))
4059 return;
4060
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004061 if (!cpu_has_virtual_nmis()) {
4062 /*
4063 * Tracking the NMI-blocked state in software is built upon
4064 * finding the next open IRQ window. This, in turn, depends on
4065 * well-behaving guests: They have to keep IRQs disabled at
4066 * least as long as the NMI handler runs. Otherwise we may
4067 * cause NMI nesting, maybe breaking the guest. But as this is
4068 * highly unlikely, we can live with the residual risk.
4069 */
4070 vmx->soft_vnmi_blocked = 1;
4071 vmx->vnmi_blocked_time = 0;
4072 }
4073
Jan Kiszka487b3912008-09-26 09:30:56 +02004074 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02004075 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004076 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05004077 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02004078 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02004079 return;
4080 }
Sheng Yangf08864b2008-05-15 18:23:25 +08004081 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
4082 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08004083}
4084
Gleb Natapovc4282df2009-04-21 17:45:07 +03004085static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02004086{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004087 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03004088 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02004089
Gleb Natapovc4282df2009-04-21 17:45:07 +03004090 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
Avi Kivity30bd0c42010-11-01 23:20:48 +02004091 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
4092 | GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02004093}
4094
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004095static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
4096{
4097 if (!cpu_has_virtual_nmis())
4098 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02004099 if (to_vmx(vcpu)->nmi_known_unmasked)
4100 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03004101 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004102}
4103
4104static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
4105{
4106 struct vcpu_vmx *vmx = to_vmx(vcpu);
4107
4108 if (!cpu_has_virtual_nmis()) {
4109 if (vmx->soft_vnmi_blocked != masked) {
4110 vmx->soft_vnmi_blocked = masked;
4111 vmx->vnmi_blocked_time = 0;
4112 }
4113 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02004114 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004115 if (masked)
4116 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4117 GUEST_INTR_STATE_NMI);
4118 else
4119 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
4120 GUEST_INTR_STATE_NMI);
4121 }
4122}
4123
Gleb Natapov78646122009-03-23 12:12:11 +02004124static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
4125{
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004126 if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) {
Nadav Har'El51cfe382011-09-22 13:53:26 +03004127 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
4128 if (to_vmx(vcpu)->nested.nested_run_pending ||
4129 (vmcs12->idt_vectoring_info_field &
4130 VECTORING_INFO_VALID_MASK))
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004131 return 0;
4132 nested_vmx_vmexit(vcpu);
Nadav Har'Elb6f12502011-05-25 23:13:06 +03004133 vmcs12->vm_exit_reason = EXIT_REASON_EXTERNAL_INTERRUPT;
4134 vmcs12->vm_exit_intr_info = 0;
4135 /* fall through to normal code, but now in L1, not L2 */
4136 }
4137
Gleb Natapovc4282df2009-04-21 17:45:07 +03004138 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
4139 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
4140 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02004141}
4142
Izik Eiduscbc94022007-10-25 00:29:55 +02004143static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
4144{
4145 int ret;
4146 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08004147 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02004148 .guest_phys_addr = addr,
4149 .memory_size = PAGE_SIZE * 3,
4150 .flags = 0,
4151 };
4152
4153 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
4154 if (ret)
4155 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08004156 kvm->arch.tss_addr = addr;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004157 if (!init_rmode_tss(kvm))
4158 return -ENOMEM;
4159
Izik Eiduscbc94022007-10-25 00:29:55 +02004160 return 0;
4161}
4162
Avi Kivity6aa8b732006-12-10 02:21:36 -08004163static int handle_rmode_exception(struct kvm_vcpu *vcpu,
4164 int vec, u32 err_code)
4165{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03004166 /*
4167 * Instruction with address size override prefix opcode 0x67
4168 * Cause the #SS fault with 0 error code in VM86 mode.
4169 */
4170 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Andre Przywara51d8b662010-12-21 11:12:02 +01004171 if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004173 /*
4174 * Forward all other exceptions that are valid in real mode.
4175 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
4176 * the required debugging infrastructure rework.
4177 */
4178 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004179 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004180 if (vcpu->guest_debug &
4181 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
4182 return 0;
4183 kvm_queue_exception(vcpu, vec);
4184 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004185 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01004186 /*
4187 * Update instruction length as we may reinject the exception
4188 * from user space while in guest debugging mode.
4189 */
4190 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
4191 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004192 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
4193 return 0;
4194 /* fall through */
4195 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02004196 case OF_VECTOR:
4197 case BR_VECTOR:
4198 case UD_VECTOR:
4199 case DF_VECTOR:
4200 case SS_VECTOR:
4201 case GP_VECTOR:
4202 case MF_VECTOR:
4203 kvm_queue_exception(vcpu, vec);
4204 return 1;
4205 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004206 return 0;
4207}
4208
Andi Kleena0861c02009-06-08 17:37:09 +08004209/*
4210 * Trigger machine check on the host. We assume all the MSRs are already set up
4211 * by the CPU and that we still run on the same CPU as the MCE occurred on.
4212 * We pass a fake environment to the machine check handler because we want
4213 * the guest to be always treated like user space, no matter what context
4214 * it used internally.
4215 */
4216static void kvm_machine_check(void)
4217{
4218#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
4219 struct pt_regs regs = {
4220 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
4221 .flags = X86_EFLAGS_IF,
4222 };
4223
4224 do_machine_check(&regs, 0);
4225#endif
4226}
4227
Avi Kivity851ba692009-08-24 11:10:17 +03004228static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08004229{
4230 /* already handled by vcpu_run */
4231 return 1;
4232}
4233
Avi Kivity851ba692009-08-24 11:10:17 +03004234static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004235{
Avi Kivity1155f762007-11-22 11:30:47 +02004236 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03004237 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004238 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004239 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004240 u32 vect_info;
4241 enum emulation_result er;
4242
Avi Kivity1155f762007-11-22 11:30:47 +02004243 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02004244 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004245
Andi Kleena0861c02009-06-08 17:37:09 +08004246 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03004247 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08004248
Avi Kivity6aa8b732006-12-10 02:21:36 -08004249 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Avi Kivity65ac7262009-11-04 11:59:01 +02004250 !is_page_fault(intr_info)) {
4251 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4252 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
4253 vcpu->run->internal.ndata = 2;
4254 vcpu->run->internal.data[0] = vect_info;
4255 vcpu->run->internal.data[1] = intr_info;
4256 return 0;
4257 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004258
Jan Kiszkae4a41882008-09-26 09:30:46 +02004259 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02004260 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004261
4262 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03004263 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03004264 return 1;
4265 }
4266
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004267 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01004268 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004269 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02004270 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004271 return 1;
4272 }
4273
Avi Kivity6aa8b732006-12-10 02:21:36 -08004274 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06004275 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004276 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
4277 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08004278 /* EPT won't cause page fault directly */
Julia Lawallcf3ace72011-08-02 12:34:57 +02004279 BUG_ON(enable_ept);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004280 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004281 trace_kvm_page_fault(cr2, error_code);
4282
Gleb Natapov3298b752009-05-11 13:35:46 +03004283 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03004284 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01004285 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004286 }
4287
Avi Kivity7ffd92c2009-06-09 14:10:45 +03004288 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08004289 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03004290 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004291 if (vcpu->arch.halt_request) {
4292 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03004293 return kvm_emulate_halt(vcpu);
4294 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004295 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03004296 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004297
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004298 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004299 switch (ex_no) {
4300 case DB_VECTOR:
4301 dr6 = vmcs_readl(EXIT_QUALIFICATION);
4302 if (!(vcpu->guest_debug &
4303 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
4304 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
4305 kvm_queue_exception(vcpu, DB_VECTOR);
4306 return 1;
4307 }
4308 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
4309 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
4310 /* fall through */
4311 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01004312 /*
4313 * Update instruction length as we may reinject #BP from
4314 * user space while in guest debugging mode. Reading it for
4315 * #DB as well causes no harm, it is not used in that case.
4316 */
4317 vmx->vcpu.arch.event_exit_inst_len =
4318 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004319 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03004320 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004321 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
4322 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004323 break;
4324 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01004325 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
4326 kvm_run->ex.exception = ex_no;
4327 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004328 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004329 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004330 return 0;
4331}
4332
Avi Kivity851ba692009-08-24 11:10:17 +03004333static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004334{
Avi Kivity1165f5f2007-04-19 17:27:43 +03004335 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336 return 1;
4337}
4338
Avi Kivity851ba692009-08-24 11:10:17 +03004339static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08004340{
Avi Kivity851ba692009-08-24 11:10:17 +03004341 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08004342 return 0;
4343}
Avi Kivity6aa8b732006-12-10 02:21:36 -08004344
Avi Kivity851ba692009-08-24 11:10:17 +03004345static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004346{
He, Qingbfdaab02007-09-12 14:18:28 +08004347 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01004348 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02004349 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004350
He, Qingbfdaab02007-09-12 14:18:28 +08004351 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02004352 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004353 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03004354
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004355 ++vcpu->stat.io_exits;
4356
4357 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01004358 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004359
4360 port = exit_qualification >> 16;
4361 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01004362 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02004363
4364 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004365}
4366
Ingo Molnar102d8322007-02-19 14:37:47 +02004367static void
4368vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4369{
4370 /*
4371 * Patch in the VMCALL instruction:
4372 */
4373 hypercall[0] = 0x0f;
4374 hypercall[1] = 0x01;
4375 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02004376}
4377
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004378/* called to set cr0 as approriate for a mov-to-cr0 exit. */
4379static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
4380{
4381 if (to_vmx(vcpu)->nested.vmxon &&
4382 ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON))
4383 return 1;
4384
4385 if (is_guest_mode(vcpu)) {
4386 /*
4387 * We get here when L2 changed cr0 in a way that did not change
4388 * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
4389 * but did change L0 shadowed bits. This can currently happen
4390 * with the TS bit: L0 may want to leave TS on (for lazy fpu
4391 * loading) while pretending to allow the guest to change it.
4392 */
4393 if (kvm_set_cr0(vcpu, (val & vcpu->arch.cr0_guest_owned_bits) |
4394 (vcpu->arch.cr0 & ~vcpu->arch.cr0_guest_owned_bits)))
4395 return 1;
4396 vmcs_writel(CR0_READ_SHADOW, val);
4397 return 0;
4398 } else
4399 return kvm_set_cr0(vcpu, val);
4400}
4401
4402static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
4403{
4404 if (is_guest_mode(vcpu)) {
4405 if (kvm_set_cr4(vcpu, (val & vcpu->arch.cr4_guest_owned_bits) |
4406 (vcpu->arch.cr4 & ~vcpu->arch.cr4_guest_owned_bits)))
4407 return 1;
4408 vmcs_writel(CR4_READ_SHADOW, val);
4409 return 0;
4410 } else
4411 return kvm_set_cr4(vcpu, val);
4412}
4413
4414/* called to set cr0 as approriate for clts instruction exit. */
4415static void handle_clts(struct kvm_vcpu *vcpu)
4416{
4417 if (is_guest_mode(vcpu)) {
4418 /*
4419 * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS
4420 * but we did (!fpu_active). We need to keep GUEST_CR0.TS on,
4421 * just pretend it's off (also in arch.cr0 for fpu_activate).
4422 */
4423 vmcs_writel(CR0_READ_SHADOW,
4424 vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS);
4425 vcpu->arch.cr0 &= ~X86_CR0_TS;
4426 } else
4427 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
4428}
4429
Avi Kivity851ba692009-08-24 11:10:17 +03004430static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004431{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004432 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004433 int cr;
4434 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03004435 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004436
He, Qingbfdaab02007-09-12 14:18:28 +08004437 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004438 cr = exit_qualification & 15;
4439 reg = (exit_qualification >> 8) & 15;
4440 switch ((exit_qualification >> 4) & 3) {
4441 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004442 val = kvm_register_read(vcpu, reg);
4443 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004444 switch (cr) {
4445 case 0:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004446 err = handle_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004447 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004448 return 1;
4449 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03004450 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004451 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004452 return 1;
4453 case 4:
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004454 err = handle_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004455 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004456 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004457 case 8: {
4458 u8 cr8_prev = kvm_get_cr8(vcpu);
4459 u8 cr8 = kvm_register_read(vcpu, reg);
Andre Przywaraeea1cff2010-12-21 11:12:00 +01004460 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01004461 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004462 if (irqchip_in_kernel(vcpu->kvm))
4463 return 1;
4464 if (cr8_prev <= cr8)
4465 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03004466 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03004467 return 0;
4468 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004469 };
4470 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03004471 case 2: /* clts */
Nadav Har'Eleeadf9e2011-05-25 23:14:38 +03004472 handle_clts(vcpu);
Avi Kivity4d4ec082009-12-29 18:07:30 +02004473 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03004474 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02004475 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03004476 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004477 case 1: /*mov from cr*/
4478 switch (cr) {
4479 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02004480 val = kvm_read_cr3(vcpu);
4481 kvm_register_write(vcpu, reg, val);
4482 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004483 skip_emulated_instruction(vcpu);
4484 return 1;
4485 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004486 val = kvm_get_cr8(vcpu);
4487 kvm_register_write(vcpu, reg, val);
4488 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004489 skip_emulated_instruction(vcpu);
4490 return 1;
4491 }
4492 break;
4493 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02004494 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02004495 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02004496 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004497
4498 skip_emulated_instruction(vcpu);
4499 return 1;
4500 default:
4501 break;
4502 }
Avi Kivity851ba692009-08-24 11:10:17 +03004503 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10004504 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08004505 (int)(exit_qualification >> 4) & 3, cr);
4506 return 0;
4507}
4508
Avi Kivity851ba692009-08-24 11:10:17 +03004509static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004510{
He, Qingbfdaab02007-09-12 14:18:28 +08004511 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512 int dr, reg;
4513
Jan Kiszkaf2483412010-01-20 18:20:20 +01004514 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03004515 if (!kvm_require_cpl(vcpu, 0))
4516 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004517 dr = vmcs_readl(GUEST_DR7);
4518 if (dr & DR7_GD) {
4519 /*
4520 * As the vm-exit takes precedence over the debug trap, we
4521 * need to emulate the latter, either for the host or the
4522 * guest debugging itself.
4523 */
4524 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03004525 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
4526 vcpu->run->debug.arch.dr7 = dr;
4527 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004528 vmcs_readl(GUEST_CS_BASE) +
4529 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03004530 vcpu->run->debug.arch.exception = DB_VECTOR;
4531 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004532 return 0;
4533 } else {
4534 vcpu->arch.dr7 &= ~DR7_GD;
4535 vcpu->arch.dr6 |= DR6_BD;
4536 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
4537 kvm_queue_exception(vcpu, DB_VECTOR);
4538 return 1;
4539 }
4540 }
4541
He, Qingbfdaab02007-09-12 14:18:28 +08004542 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004543 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
4544 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
4545 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03004546 unsigned long val;
4547 if (!kvm_get_dr(vcpu, dr, &val))
4548 kvm_register_write(vcpu, reg, val);
4549 } else
4550 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004551 skip_emulated_instruction(vcpu);
4552 return 1;
4553}
4554
Gleb Natapov020df072010-04-13 10:05:23 +03004555static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
4556{
4557 vmcs_writel(GUEST_DR7, val);
4558}
4559
Avi Kivity851ba692009-08-24 11:10:17 +03004560static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004561{
Avi Kivity06465c52007-02-28 20:46:53 +02004562 kvm_emulate_cpuid(vcpu);
4563 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004564}
4565
Avi Kivity851ba692009-08-24 11:10:17 +03004566static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004567{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004568 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08004569 u64 data;
4570
4571 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02004572 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004573 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004574 return 1;
4575 }
4576
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004577 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004578
Avi Kivity6aa8b732006-12-10 02:21:36 -08004579 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004580 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
4581 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004582 skip_emulated_instruction(vcpu);
4583 return 1;
4584}
4585
Avi Kivity851ba692009-08-24 11:10:17 +03004586static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004587{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004588 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
4589 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
4590 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004591
4592 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02004593 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02004594 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004595 return 1;
4596 }
4597
Avi Kivity59200272010-01-25 19:47:02 +02004598 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004599 skip_emulated_instruction(vcpu);
4600 return 1;
4601}
4602
Avi Kivity851ba692009-08-24 11:10:17 +03004603static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004604{
Avi Kivity3842d132010-07-27 12:30:24 +03004605 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004606 return 1;
4607}
4608
Avi Kivity851ba692009-08-24 11:10:17 +03004609static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004610{
Eddie Dong85f455f2007-07-06 12:20:49 +03004611 u32 cpu_based_vm_exec_control;
4612
4613 /* clear pending irq */
4614 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4615 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
4616 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004617
Avi Kivity3842d132010-07-27 12:30:24 +03004618 kvm_make_request(KVM_REQ_EVENT, vcpu);
4619
Jan Kiszkaa26bf122008-09-26 09:30:45 +02004620 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04004621
Dor Laorc1150d82007-01-05 16:36:24 -08004622 /*
4623 * If the user space waits to inject interrupts, exit as soon as
4624 * possible
4625 */
Gleb Natapov80618232009-04-21 17:44:56 +03004626 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03004627 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03004628 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03004629 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08004630 return 0;
4631 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004632 return 1;
4633}
4634
Avi Kivity851ba692009-08-24 11:10:17 +03004635static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004636{
4637 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03004638 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004639}
4640
Avi Kivity851ba692009-08-24 11:10:17 +03004641static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02004642{
Dor Laor510043d2007-02-19 18:25:43 +02004643 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05004644 kvm_emulate_hypercall(vcpu);
4645 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02004646}
4647
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004648static int handle_invd(struct kvm_vcpu *vcpu)
4649{
Andre Przywara51d8b662010-12-21 11:12:02 +01004650 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02004651}
4652
Avi Kivity851ba692009-08-24 11:10:17 +03004653static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03004654{
Sheng Yangf9c617f2009-03-25 10:08:52 +08004655 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03004656
4657 kvm_mmu_invlpg(vcpu, exit_qualification);
4658 skip_emulated_instruction(vcpu);
4659 return 1;
4660}
4661
Avi Kivityfee84b02011-11-10 14:57:25 +02004662static int handle_rdpmc(struct kvm_vcpu *vcpu)
4663{
4664 int err;
4665
4666 err = kvm_rdpmc(vcpu);
4667 kvm_complete_insn_gp(vcpu, err);
4668
4669 return 1;
4670}
4671
Avi Kivity851ba692009-08-24 11:10:17 +03004672static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02004673{
4674 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004675 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02004676 return 1;
4677}
4678
Dexuan Cui2acf9232010-06-10 11:27:12 +08004679static int handle_xsetbv(struct kvm_vcpu *vcpu)
4680{
4681 u64 new_bv = kvm_read_edx_eax(vcpu);
4682 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
4683
4684 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
4685 skip_emulated_instruction(vcpu);
4686 return 1;
4687}
4688
Avi Kivity851ba692009-08-24 11:10:17 +03004689static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08004690{
Kevin Tian58fbbf22011-08-30 13:56:17 +03004691 if (likely(fasteoi)) {
4692 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4693 int access_type, offset;
4694
4695 access_type = exit_qualification & APIC_ACCESS_TYPE;
4696 offset = exit_qualification & APIC_ACCESS_OFFSET;
4697 /*
4698 * Sane guest uses MOV to write EOI, with written value
4699 * not cared. So make a short-circuit here by avoiding
4700 * heavy instruction emulation.
4701 */
4702 if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
4703 (offset == APIC_EOI)) {
4704 kvm_lapic_set_eoi(vcpu);
4705 skip_emulated_instruction(vcpu);
4706 return 1;
4707 }
4708 }
Andre Przywara51d8b662010-12-21 11:12:02 +01004709 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08004710}
4711
Avi Kivity851ba692009-08-24 11:10:17 +03004712static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02004713{
Jan Kiszka60637aa2008-09-26 09:30:47 +02004714 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02004715 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02004716 bool has_error_code = false;
4717 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02004718 u16 tss_selector;
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004719 int reason, type, idt_v, idt_index;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004720
4721 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004722 idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004723 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02004724
4725 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
4726
4727 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004728 if (reason == TASK_SWITCH_GATE && idt_v) {
4729 switch (type) {
4730 case INTR_TYPE_NMI_INTR:
4731 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02004732 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004733 break;
4734 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004735 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004736 kvm_clear_interrupt_queue(vcpu);
4737 break;
4738 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02004739 if (vmx->idt_vectoring_info &
4740 VECTORING_INFO_DELIVER_CODE_MASK) {
4741 has_error_code = true;
4742 error_code =
4743 vmcs_read32(IDT_VECTORING_ERROR_CODE);
4744 }
4745 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004746 case INTR_TYPE_SOFT_EXCEPTION:
4747 kvm_clear_exception_queue(vcpu);
4748 break;
4749 default:
4750 break;
4751 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02004752 }
Izik Eidus37817f22008-03-24 23:14:53 +02004753 tss_selector = exit_qualification;
4754
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004755 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
4756 type != INTR_TYPE_EXT_INTR &&
4757 type != INTR_TYPE_NMI_INTR))
4758 skip_emulated_instruction(vcpu);
4759
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01004760 if (kvm_task_switch(vcpu, tss_selector,
4761 type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason,
4762 has_error_code, error_code) == EMULATE_FAIL) {
Gleb Natapovacb54512010-04-15 21:03:50 +03004763 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4764 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
4765 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004766 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03004767 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01004768
4769 /* clear all local breakpoint enable flags */
4770 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
4771
4772 /*
4773 * TODO: What about debug traps on tss switch?
4774 * Are we supposed to inject them and update dr6?
4775 */
4776
4777 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02004778}
4779
Avi Kivity851ba692009-08-24 11:10:17 +03004780static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08004781{
Sheng Yangf9c617f2009-03-25 10:08:52 +08004782 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08004783 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08004784 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08004785
Sheng Yangf9c617f2009-03-25 10:08:52 +08004786 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08004787
4788 if (exit_qualification & (1 << 6)) {
4789 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02004790 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08004791 }
4792
4793 gla_validity = (exit_qualification >> 7) & 0x3;
4794 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
4795 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
4796 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
4797 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08004798 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08004799 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
4800 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03004801 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
4802 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03004803 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08004804 }
4805
4806 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004807 trace_kvm_page_fault(gpa, exit_qualification);
Andre Przywaradc25e892010-12-21 11:12:07 +01004808 return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08004809}
4810
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004811static u64 ept_rsvd_mask(u64 spte, int level)
4812{
4813 int i;
4814 u64 mask = 0;
4815
4816 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
4817 mask |= (1ULL << i);
4818
4819 if (level > 2)
4820 /* bits 7:3 reserved */
4821 mask |= 0xf8;
4822 else if (level == 2) {
4823 if (spte & (1ULL << 7))
4824 /* 2MB ref, bits 20:12 reserved */
4825 mask |= 0x1ff000;
4826 else
4827 /* bits 6:3 reserved */
4828 mask |= 0x78;
4829 }
4830
4831 return mask;
4832}
4833
4834static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
4835 int level)
4836{
4837 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
4838
4839 /* 010b (write-only) */
4840 WARN_ON((spte & 0x7) == 0x2);
4841
4842 /* 110b (write/execute) */
4843 WARN_ON((spte & 0x7) == 0x6);
4844
4845 /* 100b (execute-only) and value not supported by logical processor */
4846 if (!cpu_has_vmx_ept_execute_only())
4847 WARN_ON((spte & 0x7) == 0x4);
4848
4849 /* not 000b */
4850 if ((spte & 0x7)) {
4851 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
4852
4853 if (rsvd_bits != 0) {
4854 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
4855 __func__, rsvd_bits);
4856 WARN_ON(1);
4857 }
4858
4859 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
4860 u64 ept_mem_type = (spte & 0x38) >> 3;
4861
4862 if (ept_mem_type == 2 || ept_mem_type == 3 ||
4863 ept_mem_type == 7) {
4864 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
4865 __func__, ept_mem_type);
4866 WARN_ON(1);
4867 }
4868 }
4869 }
4870}
4871
Avi Kivity851ba692009-08-24 11:10:17 +03004872static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004873{
4874 u64 sptes[4];
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004875 int nr_sptes, i, ret;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004876 gpa_t gpa;
4877
4878 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
4879
Xiao Guangrongce88dec2011-07-12 03:33:44 +08004880 ret = handle_mmio_page_fault_common(vcpu, gpa, true);
4881 if (likely(ret == 1))
4882 return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==
4883 EMULATE_DONE;
4884 if (unlikely(!ret))
4885 return 1;
4886
4887 /* It is the real ept misconfig */
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004888 printk(KERN_ERR "EPT: Misconfiguration.\n");
4889 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
4890
4891 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
4892
4893 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
4894 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
4895
Avi Kivity851ba692009-08-24 11:10:17 +03004896 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
4897 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03004898
4899 return 0;
4900}
4901
Avi Kivity851ba692009-08-24 11:10:17 +03004902static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08004903{
4904 u32 cpu_based_vm_exec_control;
4905
4906 /* clear pending NMI */
4907 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4908 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
4909 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
4910 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03004911 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08004912
4913 return 1;
4914}
4915
Mohammed Gamal80ced182009-09-01 12:48:18 +02004916static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004917{
Avi Kivity8b3079a2009-01-05 12:10:54 +02004918 struct vcpu_vmx *vmx = to_vmx(vcpu);
4919 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02004920 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02004921 u32 cpu_exec_ctrl;
4922 bool intr_window_requested;
4923
4924 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
4925 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004926
4927 while (!guest_state_valid(vcpu)) {
Avi Kivity49e9d552010-09-19 14:34:08 +02004928 if (intr_window_requested
4929 && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF))
4930 return handle_interrupt_window(&vmx->vcpu);
4931
Andre Przywara51d8b662010-12-21 11:12:02 +01004932 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004933
Mohammed Gamal80ced182009-09-01 12:48:18 +02004934 if (err == EMULATE_DO_MMIO) {
4935 ret = 0;
4936 goto out;
4937 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01004938
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03004939 if (err != EMULATE_DONE)
4940 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004941
4942 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02004943 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004944 if (need_resched())
4945 schedule();
4946 }
4947
Mohammed Gamal80ced182009-09-01 12:48:18 +02004948 vmx->emulation_required = 0;
4949out:
4950 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03004951}
4952
Avi Kivity6aa8b732006-12-10 02:21:36 -08004953/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004954 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
4955 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
4956 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03004957static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004958{
4959 skip_emulated_instruction(vcpu);
4960 kvm_vcpu_on_spin(vcpu);
4961
4962 return 1;
4963}
4964
Sheng Yang59708672009-12-15 13:29:54 +08004965static int handle_invalid_op(struct kvm_vcpu *vcpu)
4966{
4967 kvm_queue_exception(vcpu, UD_VECTOR);
4968 return 1;
4969}
4970
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08004971/*
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03004972 * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12.
4973 * We could reuse a single VMCS for all the L2 guests, but we also want the
4974 * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this
4975 * allows keeping them loaded on the processor, and in the future will allow
4976 * optimizations where prepare_vmcs02 doesn't need to set all the fields on
4977 * every entry if they never change.
4978 * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE
4979 * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first.
4980 *
4981 * The following functions allocate and free a vmcs02 in this pool.
4982 */
4983
4984/* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */
4985static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx)
4986{
4987 struct vmcs02_list *item;
4988 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
4989 if (item->vmptr == vmx->nested.current_vmptr) {
4990 list_move(&item->list, &vmx->nested.vmcs02_pool);
4991 return &item->vmcs02;
4992 }
4993
4994 if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) {
4995 /* Recycle the least recently used VMCS. */
4996 item = list_entry(vmx->nested.vmcs02_pool.prev,
4997 struct vmcs02_list, list);
4998 item->vmptr = vmx->nested.current_vmptr;
4999 list_move(&item->list, &vmx->nested.vmcs02_pool);
5000 return &item->vmcs02;
5001 }
5002
5003 /* Create a new VMCS */
5004 item = (struct vmcs02_list *)
5005 kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL);
5006 if (!item)
5007 return NULL;
5008 item->vmcs02.vmcs = alloc_vmcs();
5009 if (!item->vmcs02.vmcs) {
5010 kfree(item);
5011 return NULL;
5012 }
5013 loaded_vmcs_init(&item->vmcs02);
5014 item->vmptr = vmx->nested.current_vmptr;
5015 list_add(&(item->list), &(vmx->nested.vmcs02_pool));
5016 vmx->nested.vmcs02_num++;
5017 return &item->vmcs02;
5018}
5019
5020/* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */
5021static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr)
5022{
5023 struct vmcs02_list *item;
5024 list_for_each_entry(item, &vmx->nested.vmcs02_pool, list)
5025 if (item->vmptr == vmptr) {
5026 free_loaded_vmcs(&item->vmcs02);
5027 list_del(&item->list);
5028 kfree(item);
5029 vmx->nested.vmcs02_num--;
5030 return;
5031 }
5032}
5033
5034/*
5035 * Free all VMCSs saved for this vcpu, except the one pointed by
5036 * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one
5037 * currently used, if running L2), and vmcs01 when running L2.
5038 */
5039static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx)
5040{
5041 struct vmcs02_list *item, *n;
5042 list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) {
5043 if (vmx->loaded_vmcs != &item->vmcs02)
5044 free_loaded_vmcs(&item->vmcs02);
5045 list_del(&item->list);
5046 kfree(item);
5047 }
5048 vmx->nested.vmcs02_num = 0;
5049
5050 if (vmx->loaded_vmcs != &vmx->vmcs01)
5051 free_loaded_vmcs(&vmx->vmcs01);
5052}
5053
5054/*
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005055 * Emulate the VMXON instruction.
5056 * Currently, we just remember that VMX is active, and do not save or even
5057 * inspect the argument to VMXON (the so-called "VMXON pointer") because we
5058 * do not currently need to store anything in that guest-allocated memory
5059 * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their
5060 * argument is different from the VMXON pointer (which the spec says they do).
5061 */
5062static int handle_vmon(struct kvm_vcpu *vcpu)
5063{
5064 struct kvm_segment cs;
5065 struct vcpu_vmx *vmx = to_vmx(vcpu);
5066
5067 /* The Intel VMX Instruction Reference lists a bunch of bits that
5068 * are prerequisite to running VMXON, most notably cr4.VMXE must be
5069 * set to 1 (see vmx_set_cr4() for when we allow the guest to set this).
5070 * Otherwise, we should fail with #UD. We test these now:
5071 */
5072 if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) ||
5073 !kvm_read_cr0_bits(vcpu, X86_CR0_PE) ||
5074 (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
5075 kvm_queue_exception(vcpu, UD_VECTOR);
5076 return 1;
5077 }
5078
5079 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5080 if (is_long_mode(vcpu) && !cs.l) {
5081 kvm_queue_exception(vcpu, UD_VECTOR);
5082 return 1;
5083 }
5084
5085 if (vmx_get_cpl(vcpu)) {
5086 kvm_inject_gp(vcpu, 0);
5087 return 1;
5088 }
5089
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005090 INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool));
5091 vmx->nested.vmcs02_num = 0;
5092
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005093 vmx->nested.vmxon = true;
5094
5095 skip_emulated_instruction(vcpu);
5096 return 1;
5097}
5098
5099/*
5100 * Intel's VMX Instruction Reference specifies a common set of prerequisites
5101 * for running VMX instructions (except VMXON, whose prerequisites are
5102 * slightly different). It also specifies what exception to inject otherwise.
5103 */
5104static int nested_vmx_check_permission(struct kvm_vcpu *vcpu)
5105{
5106 struct kvm_segment cs;
5107 struct vcpu_vmx *vmx = to_vmx(vcpu);
5108
5109 if (!vmx->nested.vmxon) {
5110 kvm_queue_exception(vcpu, UD_VECTOR);
5111 return 0;
5112 }
5113
5114 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
5115 if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) ||
5116 (is_long_mode(vcpu) && !cs.l)) {
5117 kvm_queue_exception(vcpu, UD_VECTOR);
5118 return 0;
5119 }
5120
5121 if (vmx_get_cpl(vcpu)) {
5122 kvm_inject_gp(vcpu, 0);
5123 return 0;
5124 }
5125
5126 return 1;
5127}
5128
5129/*
5130 * Free whatever needs to be freed from vmx->nested when L1 goes down, or
5131 * just stops using VMX.
5132 */
5133static void free_nested(struct vcpu_vmx *vmx)
5134{
5135 if (!vmx->nested.vmxon)
5136 return;
5137 vmx->nested.vmxon = false;
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03005138 if (vmx->nested.current_vmptr != -1ull) {
5139 kunmap(vmx->nested.current_vmcs12_page);
5140 nested_release_page(vmx->nested.current_vmcs12_page);
5141 vmx->nested.current_vmptr = -1ull;
5142 vmx->nested.current_vmcs12 = NULL;
5143 }
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03005144 /* Unpin physical memory we referred to in current vmcs02 */
5145 if (vmx->nested.apic_access_page) {
5146 nested_release_page(vmx->nested.apic_access_page);
5147 vmx->nested.apic_access_page = 0;
5148 }
Nadav Har'Elff2f6fe2011-05-25 23:05:27 +03005149
5150 nested_free_all_saved_vmcss(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005151}
5152
5153/* Emulate the VMXOFF instruction */
5154static int handle_vmoff(struct kvm_vcpu *vcpu)
5155{
5156 if (!nested_vmx_check_permission(vcpu))
5157 return 1;
5158 free_nested(to_vmx(vcpu));
5159 skip_emulated_instruction(vcpu);
5160 return 1;
5161}
5162
5163/*
Nadav Har'El064aea72011-05-25 23:04:56 +03005164 * Decode the memory-address operand of a vmx instruction, as recorded on an
5165 * exit caused by such an instruction (run by a guest hypervisor).
5166 * On success, returns 0. When the operand is invalid, returns 1 and throws
5167 * #UD or #GP.
5168 */
5169static int get_vmx_mem_address(struct kvm_vcpu *vcpu,
5170 unsigned long exit_qualification,
5171 u32 vmx_instruction_info, gva_t *ret)
5172{
5173 /*
5174 * According to Vol. 3B, "Information for VM Exits Due to Instruction
5175 * Execution", on an exit, vmx_instruction_info holds most of the
5176 * addressing components of the operand. Only the displacement part
5177 * is put in exit_qualification (see 3B, "Basic VM-Exit Information").
5178 * For how an actual address is calculated from all these components,
5179 * refer to Vol. 1, "Operand Addressing".
5180 */
5181 int scaling = vmx_instruction_info & 3;
5182 int addr_size = (vmx_instruction_info >> 7) & 7;
5183 bool is_reg = vmx_instruction_info & (1u << 10);
5184 int seg_reg = (vmx_instruction_info >> 15) & 7;
5185 int index_reg = (vmx_instruction_info >> 18) & 0xf;
5186 bool index_is_valid = !(vmx_instruction_info & (1u << 22));
5187 int base_reg = (vmx_instruction_info >> 23) & 0xf;
5188 bool base_is_valid = !(vmx_instruction_info & (1u << 27));
5189
5190 if (is_reg) {
5191 kvm_queue_exception(vcpu, UD_VECTOR);
5192 return 1;
5193 }
5194
5195 /* Addr = segment_base + offset */
5196 /* offset = base + [index * scale] + displacement */
5197 *ret = vmx_get_segment_base(vcpu, seg_reg);
5198 if (base_is_valid)
5199 *ret += kvm_register_read(vcpu, base_reg);
5200 if (index_is_valid)
5201 *ret += kvm_register_read(vcpu, index_reg)<<scaling;
5202 *ret += exit_qualification; /* holds the displacement */
5203
5204 if (addr_size == 1) /* 32 bit */
5205 *ret &= 0xffffffff;
5206
5207 /*
5208 * TODO: throw #GP (and return 1) in various cases that the VM*
5209 * instructions require it - e.g., offset beyond segment limit,
5210 * unusable or unreadable/unwritable segment, non-canonical 64-bit
5211 * address, and so on. Currently these are not checked.
5212 */
5213 return 0;
5214}
5215
5216/*
Nadav Har'El0140cae2011-05-25 23:06:28 +03005217 * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(),
5218 * set the success or error code of an emulated VMX instruction, as specified
5219 * by Vol 2B, VMX Instruction Reference, "Conventions".
5220 */
5221static void nested_vmx_succeed(struct kvm_vcpu *vcpu)
5222{
5223 vmx_set_rflags(vcpu, vmx_get_rflags(vcpu)
5224 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5225 X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF));
5226}
5227
5228static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu)
5229{
5230 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5231 & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF |
5232 X86_EFLAGS_SF | X86_EFLAGS_OF))
5233 | X86_EFLAGS_CF);
5234}
5235
5236static void nested_vmx_failValid(struct kvm_vcpu *vcpu,
5237 u32 vm_instruction_error)
5238{
5239 if (to_vmx(vcpu)->nested.current_vmptr == -1ull) {
5240 /*
5241 * failValid writes the error number to the current VMCS, which
5242 * can't be done there isn't a current VMCS.
5243 */
5244 nested_vmx_failInvalid(vcpu);
5245 return;
5246 }
5247 vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu)
5248 & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF |
5249 X86_EFLAGS_SF | X86_EFLAGS_OF))
5250 | X86_EFLAGS_ZF);
5251 get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error;
5252}
5253
Nadav Har'El27d6c862011-05-25 23:06:59 +03005254/* Emulate the VMCLEAR instruction */
5255static int handle_vmclear(struct kvm_vcpu *vcpu)
5256{
5257 struct vcpu_vmx *vmx = to_vmx(vcpu);
5258 gva_t gva;
5259 gpa_t vmptr;
5260 struct vmcs12 *vmcs12;
5261 struct page *page;
5262 struct x86_exception e;
5263
5264 if (!nested_vmx_check_permission(vcpu))
5265 return 1;
5266
5267 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5268 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5269 return 1;
5270
5271 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5272 sizeof(vmptr), &e)) {
5273 kvm_inject_page_fault(vcpu, &e);
5274 return 1;
5275 }
5276
5277 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
5278 nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS);
5279 skip_emulated_instruction(vcpu);
5280 return 1;
5281 }
5282
5283 if (vmptr == vmx->nested.current_vmptr) {
5284 kunmap(vmx->nested.current_vmcs12_page);
5285 nested_release_page(vmx->nested.current_vmcs12_page);
5286 vmx->nested.current_vmptr = -1ull;
5287 vmx->nested.current_vmcs12 = NULL;
5288 }
5289
5290 page = nested_get_page(vcpu, vmptr);
5291 if (page == NULL) {
5292 /*
5293 * For accurate processor emulation, VMCLEAR beyond available
5294 * physical memory should do nothing at all. However, it is
5295 * possible that a nested vmx bug, not a guest hypervisor bug,
5296 * resulted in this case, so let's shut down before doing any
5297 * more damage:
5298 */
5299 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5300 return 1;
5301 }
5302 vmcs12 = kmap(page);
5303 vmcs12->launch_state = 0;
5304 kunmap(page);
5305 nested_release_page(page);
5306
5307 nested_free_vmcs02(vmx, vmptr);
5308
5309 skip_emulated_instruction(vcpu);
5310 nested_vmx_succeed(vcpu);
5311 return 1;
5312}
5313
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03005314static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch);
5315
5316/* Emulate the VMLAUNCH instruction */
5317static int handle_vmlaunch(struct kvm_vcpu *vcpu)
5318{
5319 return nested_vmx_run(vcpu, true);
5320}
5321
5322/* Emulate the VMRESUME instruction */
5323static int handle_vmresume(struct kvm_vcpu *vcpu)
5324{
5325
5326 return nested_vmx_run(vcpu, false);
5327}
5328
Nadav Har'El49f705c2011-05-25 23:08:30 +03005329enum vmcs_field_type {
5330 VMCS_FIELD_TYPE_U16 = 0,
5331 VMCS_FIELD_TYPE_U64 = 1,
5332 VMCS_FIELD_TYPE_U32 = 2,
5333 VMCS_FIELD_TYPE_NATURAL_WIDTH = 3
5334};
5335
5336static inline int vmcs_field_type(unsigned long field)
5337{
5338 if (0x1 & field) /* the *_HIGH fields are all 32 bit */
5339 return VMCS_FIELD_TYPE_U32;
5340 return (field >> 13) & 0x3 ;
5341}
5342
5343static inline int vmcs_field_readonly(unsigned long field)
5344{
5345 return (((field >> 10) & 0x3) == 1);
5346}
5347
5348/*
5349 * Read a vmcs12 field. Since these can have varying lengths and we return
5350 * one type, we chose the biggest type (u64) and zero-extend the return value
5351 * to that size. Note that the caller, handle_vmread, might need to use only
5352 * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of
5353 * 64-bit fields are to be returned).
5354 */
5355static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu,
5356 unsigned long field, u64 *ret)
5357{
5358 short offset = vmcs_field_to_offset(field);
5359 char *p;
5360
5361 if (offset < 0)
5362 return 0;
5363
5364 p = ((char *)(get_vmcs12(vcpu))) + offset;
5365
5366 switch (vmcs_field_type(field)) {
5367 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
5368 *ret = *((natural_width *)p);
5369 return 1;
5370 case VMCS_FIELD_TYPE_U16:
5371 *ret = *((u16 *)p);
5372 return 1;
5373 case VMCS_FIELD_TYPE_U32:
5374 *ret = *((u32 *)p);
5375 return 1;
5376 case VMCS_FIELD_TYPE_U64:
5377 *ret = *((u64 *)p);
5378 return 1;
5379 default:
5380 return 0; /* can never happen. */
5381 }
5382}
5383
5384/*
5385 * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was
5386 * used before) all generate the same failure when it is missing.
5387 */
5388static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu)
5389{
5390 struct vcpu_vmx *vmx = to_vmx(vcpu);
5391 if (vmx->nested.current_vmptr == -1ull) {
5392 nested_vmx_failInvalid(vcpu);
5393 skip_emulated_instruction(vcpu);
5394 return 0;
5395 }
5396 return 1;
5397}
5398
5399static int handle_vmread(struct kvm_vcpu *vcpu)
5400{
5401 unsigned long field;
5402 u64 field_value;
5403 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5404 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5405 gva_t gva = 0;
5406
5407 if (!nested_vmx_check_permission(vcpu) ||
5408 !nested_vmx_check_vmcs12(vcpu))
5409 return 1;
5410
5411 /* Decode instruction info and find the field to read */
5412 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5413 /* Read the field, zero-extended to a u64 field_value */
5414 if (!vmcs12_read_any(vcpu, field, &field_value)) {
5415 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5416 skip_emulated_instruction(vcpu);
5417 return 1;
5418 }
5419 /*
5420 * Now copy part of this value to register or memory, as requested.
5421 * Note that the number of bits actually copied is 32 or 64 depending
5422 * on the guest's mode (32 or 64 bit), not on the given field's length.
5423 */
5424 if (vmx_instruction_info & (1u << 10)) {
5425 kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf),
5426 field_value);
5427 } else {
5428 if (get_vmx_mem_address(vcpu, exit_qualification,
5429 vmx_instruction_info, &gva))
5430 return 1;
5431 /* _system ok, as nested_vmx_check_permission verified cpl=0 */
5432 kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva,
5433 &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL);
5434 }
5435
5436 nested_vmx_succeed(vcpu);
5437 skip_emulated_instruction(vcpu);
5438 return 1;
5439}
5440
5441
5442static int handle_vmwrite(struct kvm_vcpu *vcpu)
5443{
5444 unsigned long field;
5445 gva_t gva;
5446 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5447 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5448 char *p;
5449 short offset;
5450 /* The value to write might be 32 or 64 bits, depending on L1's long
5451 * mode, and eventually we need to write that into a field of several
5452 * possible lengths. The code below first zero-extends the value to 64
5453 * bit (field_value), and then copies only the approriate number of
5454 * bits into the vmcs12 field.
5455 */
5456 u64 field_value = 0;
5457 struct x86_exception e;
5458
5459 if (!nested_vmx_check_permission(vcpu) ||
5460 !nested_vmx_check_vmcs12(vcpu))
5461 return 1;
5462
5463 if (vmx_instruction_info & (1u << 10))
5464 field_value = kvm_register_read(vcpu,
5465 (((vmx_instruction_info) >> 3) & 0xf));
5466 else {
5467 if (get_vmx_mem_address(vcpu, exit_qualification,
5468 vmx_instruction_info, &gva))
5469 return 1;
5470 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva,
5471 &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) {
5472 kvm_inject_page_fault(vcpu, &e);
5473 return 1;
5474 }
5475 }
5476
5477
5478 field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf));
5479 if (vmcs_field_readonly(field)) {
5480 nested_vmx_failValid(vcpu,
5481 VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT);
5482 skip_emulated_instruction(vcpu);
5483 return 1;
5484 }
5485
5486 offset = vmcs_field_to_offset(field);
5487 if (offset < 0) {
5488 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5489 skip_emulated_instruction(vcpu);
5490 return 1;
5491 }
5492 p = ((char *) get_vmcs12(vcpu)) + offset;
5493
5494 switch (vmcs_field_type(field)) {
5495 case VMCS_FIELD_TYPE_U16:
5496 *(u16 *)p = field_value;
5497 break;
5498 case VMCS_FIELD_TYPE_U32:
5499 *(u32 *)p = field_value;
5500 break;
5501 case VMCS_FIELD_TYPE_U64:
5502 *(u64 *)p = field_value;
5503 break;
5504 case VMCS_FIELD_TYPE_NATURAL_WIDTH:
5505 *(natural_width *)p = field_value;
5506 break;
5507 default:
5508 nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5509 skip_emulated_instruction(vcpu);
5510 return 1;
5511 }
5512
5513 nested_vmx_succeed(vcpu);
5514 skip_emulated_instruction(vcpu);
5515 return 1;
5516}
5517
Nadav Har'El63846662011-05-25 23:07:29 +03005518/* Emulate the VMPTRLD instruction */
5519static int handle_vmptrld(struct kvm_vcpu *vcpu)
5520{
5521 struct vcpu_vmx *vmx = to_vmx(vcpu);
5522 gva_t gva;
5523 gpa_t vmptr;
5524 struct x86_exception e;
5525
5526 if (!nested_vmx_check_permission(vcpu))
5527 return 1;
5528
5529 if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
5530 vmcs_read32(VMX_INSTRUCTION_INFO), &gva))
5531 return 1;
5532
5533 if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr,
5534 sizeof(vmptr), &e)) {
5535 kvm_inject_page_fault(vcpu, &e);
5536 return 1;
5537 }
5538
5539 if (!IS_ALIGNED(vmptr, PAGE_SIZE)) {
5540 nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS);
5541 skip_emulated_instruction(vcpu);
5542 return 1;
5543 }
5544
5545 if (vmx->nested.current_vmptr != vmptr) {
5546 struct vmcs12 *new_vmcs12;
5547 struct page *page;
5548 page = nested_get_page(vcpu, vmptr);
5549 if (page == NULL) {
5550 nested_vmx_failInvalid(vcpu);
5551 skip_emulated_instruction(vcpu);
5552 return 1;
5553 }
5554 new_vmcs12 = kmap(page);
5555 if (new_vmcs12->revision_id != VMCS12_REVISION) {
5556 kunmap(page);
5557 nested_release_page_clean(page);
5558 nested_vmx_failValid(vcpu,
5559 VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID);
5560 skip_emulated_instruction(vcpu);
5561 return 1;
5562 }
5563 if (vmx->nested.current_vmptr != -1ull) {
5564 kunmap(vmx->nested.current_vmcs12_page);
5565 nested_release_page(vmx->nested.current_vmcs12_page);
5566 }
5567
5568 vmx->nested.current_vmptr = vmptr;
5569 vmx->nested.current_vmcs12 = new_vmcs12;
5570 vmx->nested.current_vmcs12_page = page;
5571 }
5572
5573 nested_vmx_succeed(vcpu);
5574 skip_emulated_instruction(vcpu);
5575 return 1;
5576}
5577
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005578/* Emulate the VMPTRST instruction */
5579static int handle_vmptrst(struct kvm_vcpu *vcpu)
5580{
5581 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5582 u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
5583 gva_t vmcs_gva;
5584 struct x86_exception e;
5585
5586 if (!nested_vmx_check_permission(vcpu))
5587 return 1;
5588
5589 if (get_vmx_mem_address(vcpu, exit_qualification,
5590 vmx_instruction_info, &vmcs_gva))
5591 return 1;
5592 /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */
5593 if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva,
5594 (void *)&to_vmx(vcpu)->nested.current_vmptr,
5595 sizeof(u64), &e)) {
5596 kvm_inject_page_fault(vcpu, &e);
5597 return 1;
5598 }
5599 nested_vmx_succeed(vcpu);
5600 skip_emulated_instruction(vcpu);
5601 return 1;
5602}
5603
Nadav Har'El0140cae2011-05-25 23:06:28 +03005604/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08005605 * The exit handlers return 1 if the exit was handled fully and guest execution
5606 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
5607 * to be done to userspace and return 0.
5608 */
Avi Kivity851ba692009-08-24 11:10:17 +03005609static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08005610 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
5611 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08005612 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08005613 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005614 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005615 [EXIT_REASON_CR_ACCESS] = handle_cr,
5616 [EXIT_REASON_DR_ACCESS] = handle_dr,
5617 [EXIT_REASON_CPUID] = handle_cpuid,
5618 [EXIT_REASON_MSR_READ] = handle_rdmsr,
5619 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
5620 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
5621 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02005622 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03005623 [EXIT_REASON_INVLPG] = handle_invlpg,
Avi Kivityfee84b02011-11-10 14:57:25 +02005624 [EXIT_REASON_RDPMC] = handle_rdpmc,
Ingo Molnarc21415e2007-02-19 14:37:47 +02005625 [EXIT_REASON_VMCALL] = handle_vmcall,
Nadav Har'El27d6c862011-05-25 23:06:59 +03005626 [EXIT_REASON_VMCLEAR] = handle_vmclear,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03005627 [EXIT_REASON_VMLAUNCH] = handle_vmlaunch,
Nadav Har'El63846662011-05-25 23:07:29 +03005628 [EXIT_REASON_VMPTRLD] = handle_vmptrld,
Nadav Har'El6a4d7552011-05-25 23:08:00 +03005629 [EXIT_REASON_VMPTRST] = handle_vmptrst,
Nadav Har'El49f705c2011-05-25 23:08:30 +03005630 [EXIT_REASON_VMREAD] = handle_vmread,
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03005631 [EXIT_REASON_VMRESUME] = handle_vmresume,
Nadav Har'El49f705c2011-05-25 23:08:30 +03005632 [EXIT_REASON_VMWRITE] = handle_vmwrite,
Nadav Har'Elec378ae2011-05-25 23:02:54 +03005633 [EXIT_REASON_VMOFF] = handle_vmoff,
5634 [EXIT_REASON_VMON] = handle_vmon,
Sheng Yangf78e0e22007-10-29 09:40:42 +08005635 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
5636 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02005637 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08005638 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02005639 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08005640 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03005641 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
5642 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08005643 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08005644 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
5645 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005646};
5647
5648static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04005649 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005650
Nadav Har'El644d7112011-05-25 23:12:35 +03005651/*
5652 * Return 1 if we should exit from L2 to L1 to handle an MSR access access,
5653 * rather than handle it ourselves in L0. I.e., check whether L1 expressed
5654 * disinterest in the current event (read or write a specific MSR) by using an
5655 * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps.
5656 */
5657static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu,
5658 struct vmcs12 *vmcs12, u32 exit_reason)
5659{
5660 u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX];
5661 gpa_t bitmap;
5662
5663 if (!nested_cpu_has(get_vmcs12(vcpu), CPU_BASED_USE_MSR_BITMAPS))
5664 return 1;
5665
5666 /*
5667 * The MSR_BITMAP page is divided into four 1024-byte bitmaps,
5668 * for the four combinations of read/write and low/high MSR numbers.
5669 * First we need to figure out which of the four to use:
5670 */
5671 bitmap = vmcs12->msr_bitmap;
5672 if (exit_reason == EXIT_REASON_MSR_WRITE)
5673 bitmap += 2048;
5674 if (msr_index >= 0xc0000000) {
5675 msr_index -= 0xc0000000;
5676 bitmap += 1024;
5677 }
5678
5679 /* Then read the msr_index'th bit from this bitmap: */
5680 if (msr_index < 1024*8) {
5681 unsigned char b;
5682 kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1);
5683 return 1 & (b >> (msr_index & 7));
5684 } else
5685 return 1; /* let L1 handle the wrong parameter */
5686}
5687
5688/*
5689 * Return 1 if we should exit from L2 to L1 to handle a CR access exit,
5690 * rather than handle it ourselves in L0. I.e., check if L1 wanted to
5691 * intercept (via guest_host_mask etc.) the current event.
5692 */
5693static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu,
5694 struct vmcs12 *vmcs12)
5695{
5696 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5697 int cr = exit_qualification & 15;
5698 int reg = (exit_qualification >> 8) & 15;
5699 unsigned long val = kvm_register_read(vcpu, reg);
5700
5701 switch ((exit_qualification >> 4) & 3) {
5702 case 0: /* mov to cr */
5703 switch (cr) {
5704 case 0:
5705 if (vmcs12->cr0_guest_host_mask &
5706 (val ^ vmcs12->cr0_read_shadow))
5707 return 1;
5708 break;
5709 case 3:
5710 if ((vmcs12->cr3_target_count >= 1 &&
5711 vmcs12->cr3_target_value0 == val) ||
5712 (vmcs12->cr3_target_count >= 2 &&
5713 vmcs12->cr3_target_value1 == val) ||
5714 (vmcs12->cr3_target_count >= 3 &&
5715 vmcs12->cr3_target_value2 == val) ||
5716 (vmcs12->cr3_target_count >= 4 &&
5717 vmcs12->cr3_target_value3 == val))
5718 return 0;
5719 if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING))
5720 return 1;
5721 break;
5722 case 4:
5723 if (vmcs12->cr4_guest_host_mask &
5724 (vmcs12->cr4_read_shadow ^ val))
5725 return 1;
5726 break;
5727 case 8:
5728 if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING))
5729 return 1;
5730 break;
5731 }
5732 break;
5733 case 2: /* clts */
5734 if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) &&
5735 (vmcs12->cr0_read_shadow & X86_CR0_TS))
5736 return 1;
5737 break;
5738 case 1: /* mov from cr */
5739 switch (cr) {
5740 case 3:
5741 if (vmcs12->cpu_based_vm_exec_control &
5742 CPU_BASED_CR3_STORE_EXITING)
5743 return 1;
5744 break;
5745 case 8:
5746 if (vmcs12->cpu_based_vm_exec_control &
5747 CPU_BASED_CR8_STORE_EXITING)
5748 return 1;
5749 break;
5750 }
5751 break;
5752 case 3: /* lmsw */
5753 /*
5754 * lmsw can change bits 1..3 of cr0, and only set bit 0 of
5755 * cr0. Other attempted changes are ignored, with no exit.
5756 */
5757 if (vmcs12->cr0_guest_host_mask & 0xe &
5758 (val ^ vmcs12->cr0_read_shadow))
5759 return 1;
5760 if ((vmcs12->cr0_guest_host_mask & 0x1) &&
5761 !(vmcs12->cr0_read_shadow & 0x1) &&
5762 (val & 0x1))
5763 return 1;
5764 break;
5765 }
5766 return 0;
5767}
5768
5769/*
5770 * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we
5771 * should handle it ourselves in L0 (and then continue L2). Only call this
5772 * when in is_guest_mode (L2).
5773 */
5774static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu)
5775{
5776 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
5777 u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
5778 struct vcpu_vmx *vmx = to_vmx(vcpu);
5779 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5780
5781 if (vmx->nested.nested_run_pending)
5782 return 0;
5783
5784 if (unlikely(vmx->fail)) {
Jan Kiszkabd801582011-09-12 11:26:22 +02005785 pr_info_ratelimited("%s failed vm entry %x\n", __func__,
5786 vmcs_read32(VM_INSTRUCTION_ERROR));
Nadav Har'El644d7112011-05-25 23:12:35 +03005787 return 1;
5788 }
5789
5790 switch (exit_reason) {
5791 case EXIT_REASON_EXCEPTION_NMI:
5792 if (!is_exception(intr_info))
5793 return 0;
5794 else if (is_page_fault(intr_info))
5795 return enable_ept;
5796 return vmcs12->exception_bitmap &
5797 (1u << (intr_info & INTR_INFO_VECTOR_MASK));
5798 case EXIT_REASON_EXTERNAL_INTERRUPT:
5799 return 0;
5800 case EXIT_REASON_TRIPLE_FAULT:
5801 return 1;
5802 case EXIT_REASON_PENDING_INTERRUPT:
5803 case EXIT_REASON_NMI_WINDOW:
5804 /*
5805 * prepare_vmcs02() set the CPU_BASED_VIRTUAL_INTR_PENDING bit
5806 * (aka Interrupt Window Exiting) only when L1 turned it on,
5807 * so if we got a PENDING_INTERRUPT exit, this must be for L1.
5808 * Same for NMI Window Exiting.
5809 */
5810 return 1;
5811 case EXIT_REASON_TASK_SWITCH:
5812 return 1;
5813 case EXIT_REASON_CPUID:
5814 return 1;
5815 case EXIT_REASON_HLT:
5816 return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING);
5817 case EXIT_REASON_INVD:
5818 return 1;
5819 case EXIT_REASON_INVLPG:
5820 return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING);
5821 case EXIT_REASON_RDPMC:
5822 return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING);
5823 case EXIT_REASON_RDTSC:
5824 return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING);
5825 case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR:
5826 case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD:
5827 case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD:
5828 case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE:
5829 case EXIT_REASON_VMOFF: case EXIT_REASON_VMON:
5830 /*
5831 * VMX instructions trap unconditionally. This allows L1 to
5832 * emulate them for its L2 guest, i.e., allows 3-level nesting!
5833 */
5834 return 1;
5835 case EXIT_REASON_CR_ACCESS:
5836 return nested_vmx_exit_handled_cr(vcpu, vmcs12);
5837 case EXIT_REASON_DR_ACCESS:
5838 return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING);
5839 case EXIT_REASON_IO_INSTRUCTION:
5840 /* TODO: support IO bitmaps */
5841 return 1;
5842 case EXIT_REASON_MSR_READ:
5843 case EXIT_REASON_MSR_WRITE:
5844 return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason);
5845 case EXIT_REASON_INVALID_STATE:
5846 return 1;
5847 case EXIT_REASON_MWAIT_INSTRUCTION:
5848 return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING);
5849 case EXIT_REASON_MONITOR_INSTRUCTION:
5850 return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING);
5851 case EXIT_REASON_PAUSE_INSTRUCTION:
5852 return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) ||
5853 nested_cpu_has2(vmcs12,
5854 SECONDARY_EXEC_PAUSE_LOOP_EXITING);
5855 case EXIT_REASON_MCE_DURING_VMENTRY:
5856 return 0;
5857 case EXIT_REASON_TPR_BELOW_THRESHOLD:
5858 return 1;
5859 case EXIT_REASON_APIC_ACCESS:
5860 return nested_cpu_has2(vmcs12,
5861 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
5862 case EXIT_REASON_EPT_VIOLATION:
5863 case EXIT_REASON_EPT_MISCONFIG:
5864 return 0;
5865 case EXIT_REASON_WBINVD:
5866 return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING);
5867 case EXIT_REASON_XSETBV:
5868 return 1;
5869 default:
5870 return 1;
5871 }
5872}
5873
Avi Kivity586f9602010-11-18 13:09:54 +02005874static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
5875{
5876 *info1 = vmcs_readl(EXIT_QUALIFICATION);
5877 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
5878}
5879
Avi Kivity6aa8b732006-12-10 02:21:36 -08005880/*
5881 * The guest has exited. See if we can fix it or if we need userspace
5882 * assistance.
5883 */
Avi Kivity851ba692009-08-24 11:10:17 +03005884static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08005885{
Avi Kivity29bd8a72007-09-10 17:27:03 +03005886 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08005887 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02005888 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03005889
Mohammed Gamal80ced182009-09-01 12:48:18 +02005890 /* If guest state is invalid, start emulating */
5891 if (vmx->emulation_required && emulate_invalid_guest_state)
5892 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01005893
Nadav Har'Elb6f12502011-05-25 23:13:06 +03005894 /*
5895 * the KVM_REQ_EVENT optimization bit is only on for one entry, and if
5896 * we did not inject a still-pending event to L1 now because of
5897 * nested_run_pending, we need to re-enable this bit.
5898 */
5899 if (vmx->nested.nested_run_pending)
5900 kvm_make_request(KVM_REQ_EVENT, vcpu);
5901
Nadav Har'El509c75e2011-06-02 11:54:52 +03005902 if (!is_guest_mode(vcpu) && (exit_reason == EXIT_REASON_VMLAUNCH ||
5903 exit_reason == EXIT_REASON_VMRESUME))
Nadav Har'El644d7112011-05-25 23:12:35 +03005904 vmx->nested.nested_run_pending = 1;
5905 else
5906 vmx->nested.nested_run_pending = 0;
5907
5908 if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) {
5909 nested_vmx_vmexit(vcpu);
5910 return 1;
5911 }
5912
Mohammed Gamal51207022010-05-31 22:40:54 +03005913 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
5914 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5915 vcpu->run->fail_entry.hardware_entry_failure_reason
5916 = exit_reason;
5917 return 0;
5918 }
5919
Avi Kivity29bd8a72007-09-10 17:27:03 +03005920 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03005921 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
5922 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03005923 = vmcs_read32(VM_INSTRUCTION_ERROR);
5924 return 0;
5925 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08005926
Mike Dayd77c26f2007-10-08 09:02:08 -04005927 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08005928 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02005929 exit_reason != EXIT_REASON_EPT_VIOLATION &&
5930 exit_reason != EXIT_REASON_TASK_SWITCH))
5931 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
5932 "(0x%x) and exit reason is 0x%x\n",
5933 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005934
Nadav Har'El644d7112011-05-25 23:12:35 +03005935 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked &&
5936 !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis(
5937 get_vmcs12(vcpu), vcpu)))) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03005938 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005939 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005940 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01005941 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005942 /*
5943 * This CPU don't support us in finding the end of an
5944 * NMI-blocked window if the guest runs with IRQs
5945 * disabled. So we pull the trigger after 1 s of
5946 * futile waiting, but inform the user about this.
5947 */
5948 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
5949 "state on VCPU %d after 1 s timeout\n",
5950 __func__, vcpu->vcpu_id);
5951 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005952 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02005953 }
5954
Avi Kivity6aa8b732006-12-10 02:21:36 -08005955 if (exit_reason < kvm_vmx_max_exit_handlers
5956 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03005957 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005958 else {
Avi Kivity851ba692009-08-24 11:10:17 +03005959 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
5960 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08005961 }
5962 return 0;
5963}
5964
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005965static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005966{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005967 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005968 vmcs_write32(TPR_THRESHOLD, 0);
5969 return;
5970 }
5971
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005972 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08005973}
5974
Avi Kivity51aa01d2010-07-20 14:31:20 +03005975static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03005976{
Avi Kivity00eba012011-03-07 17:24:54 +02005977 u32 exit_intr_info;
5978
5979 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
5980 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
5981 return;
5982
Avi Kivityc5ca8e52011-03-07 17:37:37 +02005983 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02005984 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08005985
5986 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02005987 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08005988 kvm_machine_check();
5989
Gleb Natapov20f65982009-05-11 13:35:55 +03005990 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02005991 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005992 (exit_intr_info & INTR_INFO_VALID_MASK)) {
5993 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03005994 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08005995 kvm_after_handle_nmi(&vmx->vcpu);
5996 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03005997}
Gleb Natapov20f65982009-05-11 13:35:55 +03005998
Avi Kivity51aa01d2010-07-20 14:31:20 +03005999static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
6000{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006001 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03006002 bool unblock_nmi;
6003 u8 vector;
6004 bool idtv_info_valid;
6005
6006 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03006007
Avi Kivitycf393f72008-07-01 16:20:21 +03006008 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02006009 if (vmx->nmi_known_unmasked)
6010 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02006011 /*
6012 * Can't use vmx->exit_intr_info since we're not sure what
6013 * the exit reason is.
6014 */
6015 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03006016 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
6017 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
6018 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006019 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03006020 * Re-set bit "block by NMI" before VM entry if vmexit caused by
6021 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006022 * SDM 3: 23.2.2 (September 2008)
6023 * Bit 12 is undefined in any of the following cases:
6024 * If the VM exit sets the valid bit in the IDT-vectoring
6025 * information field.
6026 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03006027 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006028 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
6029 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03006030 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6031 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02006032 else
6033 vmx->nmi_known_unmasked =
6034 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
6035 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02006036 } else if (unlikely(vmx->soft_vnmi_blocked))
6037 vmx->vnmi_blocked_time +=
6038 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03006039}
6040
Avi Kivity83422e12010-07-20 14:43:23 +03006041static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
6042 u32 idt_vectoring_info,
6043 int instr_len_field,
6044 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03006045{
Avi Kivity51aa01d2010-07-20 14:31:20 +03006046 u8 vector;
6047 int type;
6048 bool idtv_info_valid;
6049
6050 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03006051
Gleb Natapov37b96e92009-03-30 16:03:13 +03006052 vmx->vcpu.arch.nmi_injected = false;
6053 kvm_clear_exception_queue(&vmx->vcpu);
6054 kvm_clear_interrupt_queue(&vmx->vcpu);
6055
6056 if (!idtv_info_valid)
6057 return;
6058
Avi Kivity3842d132010-07-27 12:30:24 +03006059 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
6060
Avi Kivity668f6122008-07-02 09:28:55 +03006061 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
6062 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006063
Gleb Natapov64a7ec02009-03-30 16:03:29 +03006064 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03006065 case INTR_TYPE_NMI_INTR:
6066 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03006067 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03006068 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03006069 * Clear bit "block by NMI" before VM entry if a NMI
6070 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03006071 */
Avi Kivity654f06f2011-03-23 15:02:47 +02006072 vmx_set_nmi_mask(&vmx->vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006073 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03006074 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006075 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03006076 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006077 /* fall through */
6078 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03006079 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03006080 u32 err = vmcs_read32(error_code_field);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006081 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03006082 } else
6083 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006084 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006085 case INTR_TYPE_SOFT_INTR:
6086 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03006087 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006088 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03006089 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03006090 kvm_queue_interrupt(&vmx->vcpu, vector,
6091 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03006092 break;
6093 default:
6094 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03006095 }
Avi Kivitycf393f72008-07-01 16:20:21 +03006096}
6097
Avi Kivity83422e12010-07-20 14:43:23 +03006098static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
6099{
Nadav Har'El66c78ae2011-05-25 23:14:07 +03006100 if (is_guest_mode(&vmx->vcpu))
6101 return;
Avi Kivity83422e12010-07-20 14:43:23 +03006102 __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
6103 VM_EXIT_INSTRUCTION_LEN,
6104 IDT_VECTORING_ERROR_CODE);
6105}
6106
Avi Kivityb463a6f2010-07-20 15:06:17 +03006107static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
6108{
Nadav Har'El66c78ae2011-05-25 23:14:07 +03006109 if (is_guest_mode(vcpu))
6110 return;
Avi Kivityb463a6f2010-07-20 15:06:17 +03006111 __vmx_complete_interrupts(to_vmx(vcpu),
6112 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6113 VM_ENTRY_INSTRUCTION_LEN,
6114 VM_ENTRY_EXCEPTION_ERROR_CODE);
6115
6116 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
6117}
6118
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006119static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
6120{
6121 int i, nr_msrs;
6122 struct perf_guest_switch_msr *msrs;
6123
6124 msrs = perf_guest_get_msrs(&nr_msrs);
6125
6126 if (!msrs)
6127 return;
6128
6129 for (i = 0; i < nr_msrs; i++)
6130 if (msrs[i].host == msrs[i].guest)
6131 clear_atomic_switch_msr(vmx, msrs[i].msr);
6132 else
6133 add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
6134 msrs[i].host);
6135}
6136
Avi Kivityc8019492008-07-14 14:44:59 +03006137#ifdef CONFIG_X86_64
6138#define R "r"
6139#define Q "q"
6140#else
6141#define R "e"
6142#define Q "l"
6143#endif
6144
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08006145static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006146{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006147 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity104f2262010-11-18 13:12:52 +02006148
Nadav Har'El66c78ae2011-05-25 23:14:07 +03006149 if (is_guest_mode(vcpu) && !vmx->nested.nested_run_pending) {
6150 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6151 if (vmcs12->idt_vectoring_info_field &
6152 VECTORING_INFO_VALID_MASK) {
6153 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6154 vmcs12->idt_vectoring_info_field);
6155 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6156 vmcs12->vm_exit_instruction_len);
6157 if (vmcs12->idt_vectoring_info_field &
6158 VECTORING_INFO_DELIVER_CODE_MASK)
6159 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
6160 vmcs12->idt_vectoring_error_code);
6161 }
6162 }
6163
Avi Kivity104f2262010-11-18 13:12:52 +02006164 /* Record the guest's net vcpu time for enforced NMI injections. */
6165 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
6166 vmx->entry_time = ktime_get();
6167
6168 /* Don't enter VMX if guest state is invalid, let the exit handler
6169 start emulation until we arrive back to a valid state */
6170 if (vmx->emulation_required && emulate_invalid_guest_state)
6171 return;
6172
6173 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
6174 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
6175 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
6176 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
6177
6178 /* When single-stepping over STI and MOV SS, we must clear the
6179 * corresponding interruptibility bits in the guest state. Otherwise
6180 * vmentry fails as it then expects bit 14 (BS) in pending debug
6181 * exceptions being set, but that's not correct for the guest debugging
6182 * case. */
6183 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
6184 vmx_set_interrupt_shadow(vcpu, 0);
6185
Gleb Natapovd7cd9792011-10-05 14:01:23 +02006186 atomic_switch_perf_msrs(vmx);
6187
Nadav Har'Eld462b812011-05-24 15:26:10 +03006188 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02006189 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08006190 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03006191 "push %%"R"dx; push %%"R"bp;"
Avi Kivity40712fa2011-01-06 18:09:12 +02006192 "push %%"R"cx \n\t" /* placeholder for guest rcx */
Avi Kivityc8019492008-07-14 14:44:59 +03006193 "push %%"R"cx \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03006194 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
6195 "je 1f \n\t"
6196 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03006197 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03006198 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03006199 /* Reload cr2 if changed */
6200 "mov %c[cr2](%0), %%"R"ax \n\t"
6201 "mov %%cr2, %%"R"dx \n\t"
6202 "cmp %%"R"ax, %%"R"dx \n\t"
6203 "je 2f \n\t"
6204 "mov %%"R"ax, %%cr2 \n\t"
6205 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006206 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02006207 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006208 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03006209 "mov %c[rax](%0), %%"R"ax \n\t"
6210 "mov %c[rbx](%0), %%"R"bx \n\t"
6211 "mov %c[rdx](%0), %%"R"dx \n\t"
6212 "mov %c[rsi](%0), %%"R"si \n\t"
6213 "mov %c[rdi](%0), %%"R"di \n\t"
6214 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006215#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006216 "mov %c[r8](%0), %%r8 \n\t"
6217 "mov %c[r9](%0), %%r9 \n\t"
6218 "mov %c[r10](%0), %%r10 \n\t"
6219 "mov %c[r11](%0), %%r11 \n\t"
6220 "mov %c[r12](%0), %%r12 \n\t"
6221 "mov %c[r13](%0), %%r13 \n\t"
6222 "mov %c[r14](%0), %%r14 \n\t"
6223 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006224#endif
Avi Kivityc8019492008-07-14 14:44:59 +03006225 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
6226
Avi Kivity6aa8b732006-12-10 02:21:36 -08006227 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03006228 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03006229 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03006230 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03006231 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03006232 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08006233 /* Save guest registers, load host registers, keep flags */
Avi Kivity40712fa2011-01-06 18:09:12 +02006234 "mov %0, %c[wordsize](%%"R"sp) \n\t"
6235 "pop %0 \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03006236 "mov %%"R"ax, %c[rax](%0) \n\t"
6237 "mov %%"R"bx, %c[rbx](%0) \n\t"
Avi Kivity1c696d02011-01-06 18:09:11 +02006238 "pop"Q" %c[rcx](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03006239 "mov %%"R"dx, %c[rdx](%0) \n\t"
6240 "mov %%"R"si, %c[rsi](%0) \n\t"
6241 "mov %%"R"di, %c[rdi](%0) \n\t"
6242 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006243#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02006244 "mov %%r8, %c[r8](%0) \n\t"
6245 "mov %%r9, %c[r9](%0) \n\t"
6246 "mov %%r10, %c[r10](%0) \n\t"
6247 "mov %%r11, %c[r11](%0) \n\t"
6248 "mov %%r12, %c[r12](%0) \n\t"
6249 "mov %%r13, %c[r13](%0) \n\t"
6250 "mov %%r14, %c[r14](%0) \n\t"
6251 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08006252#endif
Avi Kivityc8019492008-07-14 14:44:59 +03006253 "mov %%cr2, %%"R"ax \n\t"
6254 "mov %%"R"ax, %c[cr2](%0) \n\t"
6255
Avi Kivity1c696d02011-01-06 18:09:11 +02006256 "pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02006257 "setbe %c[fail](%0) \n\t"
6258 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03006259 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02006260 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03006261 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006262 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
6263 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
6264 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
6265 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
6266 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
6267 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
6268 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08006269#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08006270 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
6271 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
6272 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
6273 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
6274 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
6275 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
6276 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
6277 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08006278#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02006279 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
6280 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02006281 : "cc", "memory"
Jan Kiszka07d6f552010-09-28 16:37:42 +02006282 , R"ax", R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02006283#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02006284 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
6285#endif
6286 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08006287
Avi Kivity6de4f3a2009-05-31 22:58:47 +03006288 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02006289 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivity69c73022011-03-07 15:26:44 +02006290 | (1 << VCPU_EXREG_CPL)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006291 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03006292 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02006293 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03006294 vcpu->arch.regs_dirty = 0;
6295
Avi Kivity1155f762007-11-22 11:30:47 +02006296 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
6297
Nadav Har'El66c78ae2011-05-25 23:14:07 +03006298 if (is_guest_mode(vcpu)) {
6299 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6300 vmcs12->idt_vectoring_info_field = vmx->idt_vectoring_info;
6301 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
6302 vmcs12->idt_vectoring_error_code =
6303 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6304 vmcs12->vm_exit_instruction_len =
6305 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6306 }
6307 }
6308
Nadav Har'Eld462b812011-05-24 15:26:10 +03006309 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02006310
Avi Kivity51aa01d2010-07-20 14:31:20 +03006311 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Jan Kiszka1e2b1dd2011-09-12 10:52:24 +02006312 trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX);
Avi Kivity51aa01d2010-07-20 14:31:20 +03006313
6314 vmx_complete_atomic_exit(vmx);
6315 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03006316 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006317}
6318
Avi Kivityc8019492008-07-14 14:44:59 +03006319#undef R
6320#undef Q
6321
Avi Kivity6aa8b732006-12-10 02:21:36 -08006322static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
6323{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006324 struct vcpu_vmx *vmx = to_vmx(vcpu);
6325
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08006326 free_vpid(vmx);
Nadav Har'Elec378ae2011-05-25 23:02:54 +03006327 free_nested(vmx);
Nadav Har'Eld462b812011-05-24 15:26:10 +03006328 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006329 kfree(vmx->guest_msrs);
6330 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10006331 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006332}
6333
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006334static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08006335{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006336 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10006337 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03006338 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08006339
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006340 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006341 return ERR_PTR(-ENOMEM);
6342
Sheng Yang2384d2b2008-01-17 15:14:33 +08006343 allocate_vpid(vmx);
6344
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006345 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
6346 if (err)
6347 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006348
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006349 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006350 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006351 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006352 goto uninit_vcpu;
6353 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08006354
Nadav Har'Eld462b812011-05-24 15:26:10 +03006355 vmx->loaded_vmcs = &vmx->vmcs01;
6356 vmx->loaded_vmcs->vmcs = alloc_vmcs();
6357 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006358 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03006359 if (!vmm_exclusive)
6360 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
6361 loaded_vmcs_init(vmx->loaded_vmcs);
6362 if (!vmm_exclusive)
6363 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04006364
Avi Kivity15ad7142007-07-11 18:17:21 +03006365 cpu = get_cpu();
6366 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10006367 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10006368 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006369 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03006370 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006371 if (err)
6372 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006373 if (vm_need_virtualize_apic_accesses(kvm))
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02006374 err = alloc_apic_access_page(kvm);
6375 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02006376 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006377
Sheng Yangb927a3c2009-07-21 10:42:48 +08006378 if (enable_ept) {
6379 if (!kvm->arch.ept_identity_map_addr)
6380 kvm->arch.ept_identity_map_addr =
6381 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Gleb Natapov93ea5382011-02-21 12:07:59 +02006382 err = -ENOMEM;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006383 if (alloc_identity_pagetable(kvm) != 0)
6384 goto free_vmcs;
Gleb Natapov93ea5382011-02-21 12:07:59 +02006385 if (!init_rmode_identity_map(kvm))
6386 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08006387 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08006388
Nadav Har'Ela9d30f32011-05-25 23:03:55 +03006389 vmx->nested.current_vmptr = -1ull;
6390 vmx->nested.current_vmcs12 = NULL;
6391
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006392 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08006393
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006394free_vmcs:
Xiao Guangrong5f3fbc32012-05-14 14:58:58 +08006395 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006396free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006397 kfree(vmx->guest_msrs);
6398uninit_vcpu:
6399 kvm_vcpu_uninit(&vmx->vcpu);
6400free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08006401 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10006402 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10006403 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08006404}
6405
Yang, Sheng002c7f72007-07-31 14:23:01 +03006406static void __init vmx_check_processor_compat(void *rtn)
6407{
6408 struct vmcs_config vmcs_conf;
6409
6410 *(int *)rtn = 0;
6411 if (setup_vmcs_config(&vmcs_conf) < 0)
6412 *(int *)rtn = -EIO;
6413 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
6414 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
6415 smp_processor_id());
6416 *(int *)rtn = -EIO;
6417 }
6418}
6419
Sheng Yang67253af2008-04-25 10:20:22 +08006420static int get_ept_level(void)
6421{
6422 return VMX_EPT_DEFAULT_GAW + 1;
6423}
6424
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006425static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08006426{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006427 u64 ret;
6428
Sheng Yang522c68c2009-04-27 20:35:43 +08006429 /* For VT-d and EPT combination
6430 * 1. MMIO: always map as UC
6431 * 2. EPT with VT-d:
6432 * a. VT-d without snooping control feature: can't guarantee the
6433 * result, try to trust guest.
6434 * b. VT-d with snooping control feature: snooping control feature of
6435 * VT-d engine can guarantee the cache correctness. Just set it
6436 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08006437 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08006438 * consistent with host MTRR
6439 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006440 if (is_mmio)
6441 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08006442 else if (vcpu->kvm->arch.iommu_domain &&
6443 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
6444 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
6445 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006446 else
Sheng Yang522c68c2009-04-27 20:35:43 +08006447 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08006448 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08006449
6450 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08006451}
6452
Sheng Yang17cc3932010-01-05 19:02:27 +08006453static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02006454{
Sheng Yang878403b2010-01-05 19:02:29 +08006455 if (enable_ept && !cpu_has_vmx_ept_1g_page())
6456 return PT_DIRECTORY_LEVEL;
6457 else
6458 /* For shadow and EPT supported 1GB page */
6459 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02006460}
6461
Sheng Yang0e851882009-12-18 16:48:46 +08006462static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
6463{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08006464 struct kvm_cpuid_entry2 *best;
6465 struct vcpu_vmx *vmx = to_vmx(vcpu);
6466 u32 exec_control;
6467
6468 vmx->rdtscp_enabled = false;
6469 if (vmx_rdtscp_supported()) {
6470 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6471 if (exec_control & SECONDARY_EXEC_RDTSCP) {
6472 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
6473 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
6474 vmx->rdtscp_enabled = true;
6475 else {
6476 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6477 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
6478 exec_control);
6479 }
6480 }
6481 }
Sheng Yang0e851882009-12-18 16:48:46 +08006482}
6483
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006484static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
6485{
Nadav Har'El7b8050f2011-05-25 23:16:10 +03006486 if (func == 1 && nested)
6487 entry->ecx |= bit(X86_FEATURE_VMX);
Joerg Roedeld4330ef2010-04-22 12:33:11 +02006488}
6489
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006490/*
6491 * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested
6492 * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it
6493 * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2
6494 * guest in a way that will both be appropriate to L1's requests, and our
6495 * needs. In addition to modifying the active vmcs (which is vmcs02), this
6496 * function also has additional necessary side-effects, like setting various
6497 * vcpu->arch fields.
6498 */
6499static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
6500{
6501 struct vcpu_vmx *vmx = to_vmx(vcpu);
6502 u32 exec_control;
6503
6504 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector);
6505 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector);
6506 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector);
6507 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector);
6508 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector);
6509 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector);
6510 vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector);
6511 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector);
6512 vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit);
6513 vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit);
6514 vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit);
6515 vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit);
6516 vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit);
6517 vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit);
6518 vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit);
6519 vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit);
6520 vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit);
6521 vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit);
6522 vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes);
6523 vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes);
6524 vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes);
6525 vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes);
6526 vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes);
6527 vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes);
6528 vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes);
6529 vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes);
6530 vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base);
6531 vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base);
6532 vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base);
6533 vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base);
6534 vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base);
6535 vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base);
6536 vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base);
6537 vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base);
6538 vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base);
6539 vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base);
6540
6541 vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl);
6542 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
6543 vmcs12->vm_entry_intr_info_field);
6544 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
6545 vmcs12->vm_entry_exception_error_code);
6546 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
6547 vmcs12->vm_entry_instruction_len);
6548 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
6549 vmcs12->guest_interruptibility_info);
6550 vmcs_write32(GUEST_ACTIVITY_STATE, vmcs12->guest_activity_state);
6551 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs);
6552 vmcs_writel(GUEST_DR7, vmcs12->guest_dr7);
6553 vmcs_writel(GUEST_RFLAGS, vmcs12->guest_rflags);
6554 vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS,
6555 vmcs12->guest_pending_dbg_exceptions);
6556 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp);
6557 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip);
6558
6559 vmcs_write64(VMCS_LINK_POINTER, -1ull);
6560
6561 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
6562 (vmcs_config.pin_based_exec_ctrl |
6563 vmcs12->pin_based_vm_exec_control));
6564
6565 /*
6566 * Whether page-faults are trapped is determined by a combination of
6567 * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF.
6568 * If enable_ept, L0 doesn't care about page faults and we should
6569 * set all of these to L1's desires. However, if !enable_ept, L0 does
6570 * care about (at least some) page faults, and because it is not easy
6571 * (if at all possible?) to merge L0 and L1's desires, we simply ask
6572 * to exit on each and every L2 page fault. This is done by setting
6573 * MASK=MATCH=0 and (see below) EB.PF=1.
6574 * Note that below we don't need special code to set EB.PF beyond the
6575 * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept,
6576 * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when
6577 * !enable_ept, EB.PF is 1, so the "or" will always be 1.
6578 *
6579 * A problem with this approach (when !enable_ept) is that L1 may be
6580 * injected with more page faults than it asked for. This could have
6581 * caused problems, but in practice existing hypervisors don't care.
6582 * To fix this, we will need to emulate the PFEC checking (on the L1
6583 * page tables), using walk_addr(), when injecting PFs to L1.
6584 */
6585 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK,
6586 enable_ept ? vmcs12->page_fault_error_code_mask : 0);
6587 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH,
6588 enable_ept ? vmcs12->page_fault_error_code_match : 0);
6589
6590 if (cpu_has_secondary_exec_ctrls()) {
6591 u32 exec_control = vmx_secondary_exec_control(vmx);
6592 if (!vmx->rdtscp_enabled)
6593 exec_control &= ~SECONDARY_EXEC_RDTSCP;
6594 /* Take the following fields only from vmcs12 */
6595 exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6596 if (nested_cpu_has(vmcs12,
6597 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS))
6598 exec_control |= vmcs12->secondary_vm_exec_control;
6599
6600 if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) {
6601 /*
6602 * Translate L1 physical address to host physical
6603 * address for vmcs02. Keep the page pinned, so this
6604 * physical address remains valid. We keep a reference
6605 * to it so we can release it later.
6606 */
6607 if (vmx->nested.apic_access_page) /* shouldn't happen */
6608 nested_release_page(vmx->nested.apic_access_page);
6609 vmx->nested.apic_access_page =
6610 nested_get_page(vcpu, vmcs12->apic_access_addr);
6611 /*
6612 * If translation failed, no matter: This feature asks
6613 * to exit when accessing the given address, and if it
6614 * can never be accessed, this feature won't do
6615 * anything anyway.
6616 */
6617 if (!vmx->nested.apic_access_page)
6618 exec_control &=
6619 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6620 else
6621 vmcs_write64(APIC_ACCESS_ADDR,
6622 page_to_phys(vmx->nested.apic_access_page));
6623 }
6624
6625 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
6626 }
6627
6628
6629 /*
6630 * Set host-state according to L0's settings (vmcs12 is irrelevant here)
6631 * Some constant fields are set here by vmx_set_constant_host_state().
6632 * Other fields are different per CPU, and will be set later when
6633 * vmx_vcpu_load() is called, and when vmx_save_host_state() is called.
6634 */
6635 vmx_set_constant_host_state();
6636
6637 /*
6638 * HOST_RSP is normally set correctly in vmx_vcpu_run() just before
6639 * entry, but only if the current (host) sp changed from the value
6640 * we wrote last (vmx->host_rsp). This cache is no longer relevant
6641 * if we switch vmcs, and rather than hold a separate cache per vmcs,
6642 * here we just force the write to happen on entry.
6643 */
6644 vmx->host_rsp = 0;
6645
6646 exec_control = vmx_exec_control(vmx); /* L0's desires */
6647 exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
6648 exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
6649 exec_control &= ~CPU_BASED_TPR_SHADOW;
6650 exec_control |= vmcs12->cpu_based_vm_exec_control;
6651 /*
6652 * Merging of IO and MSR bitmaps not currently supported.
6653 * Rather, exit every time.
6654 */
6655 exec_control &= ~CPU_BASED_USE_MSR_BITMAPS;
6656 exec_control &= ~CPU_BASED_USE_IO_BITMAPS;
6657 exec_control |= CPU_BASED_UNCOND_IO_EXITING;
6658
6659 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
6660
6661 /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the
6662 * bitwise-or of what L1 wants to trap for L2, and what we want to
6663 * trap. Note that CR0.TS also needs updating - we do this later.
6664 */
6665 update_exception_bitmap(vcpu);
6666 vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask;
6667 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
6668
6669 /* Note: IA32_MODE, LOAD_IA32_EFER are modified by vmx_set_efer below */
6670 vmcs_write32(VM_EXIT_CONTROLS,
6671 vmcs12->vm_exit_controls | vmcs_config.vmexit_ctrl);
6672 vmcs_write32(VM_ENTRY_CONTROLS, vmcs12->vm_entry_controls |
6673 (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE));
6674
6675 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)
6676 vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat);
6677 else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
6678 vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
6679
6680
6681 set_cr4_guest_host_mask(vmx);
6682
Nadav Har'El27fc51b2011-08-02 15:54:52 +03006683 if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)
6684 vmcs_write64(TSC_OFFSET,
6685 vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset);
6686 else
6687 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
Nadav Har'Elfe3ef052011-05-25 23:10:02 +03006688
6689 if (enable_vpid) {
6690 /*
6691 * Trivially support vpid by letting L2s share their parent
6692 * L1's vpid. TODO: move to a more elaborate solution, giving
6693 * each L2 its own vpid and exposing the vpid feature to L1.
6694 */
6695 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
6696 vmx_flush_tlb(vcpu);
6697 }
6698
6699 if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)
6700 vcpu->arch.efer = vmcs12->guest_ia32_efer;
6701 if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE)
6702 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
6703 else
6704 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
6705 /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */
6706 vmx_set_efer(vcpu, vcpu->arch.efer);
6707
6708 /*
6709 * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified
6710 * TS bit (for lazy fpu) and bits which we consider mandatory enabled.
6711 * The CR0_READ_SHADOW is what L2 should have expected to read given
6712 * the specifications by L1; It's not enough to take
6713 * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we
6714 * have more bits than L1 expected.
6715 */
6716 vmx_set_cr0(vcpu, vmcs12->guest_cr0);
6717 vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12));
6718
6719 vmx_set_cr4(vcpu, vmcs12->guest_cr4);
6720 vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12));
6721
6722 /* shadow page tables on either EPT or shadow page tables */
6723 kvm_set_cr3(vcpu, vmcs12->guest_cr3);
6724 kvm_mmu_reset_context(vcpu);
6725
6726 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp);
6727 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip);
6728}
6729
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006730/*
6731 * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1
6732 * for running an L2 nested guest.
6733 */
6734static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch)
6735{
6736 struct vmcs12 *vmcs12;
6737 struct vcpu_vmx *vmx = to_vmx(vcpu);
6738 int cpu;
6739 struct loaded_vmcs *vmcs02;
6740
6741 if (!nested_vmx_check_permission(vcpu) ||
6742 !nested_vmx_check_vmcs12(vcpu))
6743 return 1;
6744
6745 skip_emulated_instruction(vcpu);
6746 vmcs12 = get_vmcs12(vcpu);
6747
Nadav Har'El7c177932011-05-25 23:12:04 +03006748 /*
6749 * The nested entry process starts with enforcing various prerequisites
6750 * on vmcs12 as required by the Intel SDM, and act appropriately when
6751 * they fail: As the SDM explains, some conditions should cause the
6752 * instruction to fail, while others will cause the instruction to seem
6753 * to succeed, but return an EXIT_REASON_INVALID_STATE.
6754 * To speed up the normal (success) code path, we should avoid checking
6755 * for misconfigurations which will anyway be caught by the processor
6756 * when using the merged vmcs02.
6757 */
6758 if (vmcs12->launch_state == launch) {
6759 nested_vmx_failValid(vcpu,
6760 launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS
6761 : VMXERR_VMRESUME_NONLAUNCHED_VMCS);
6762 return 1;
6763 }
6764
6765 if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) &&
6766 !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) {
6767 /*TODO: Also verify bits beyond physical address width are 0*/
6768 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
6769 return 1;
6770 }
6771
6772 if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) &&
6773 !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) {
6774 /*TODO: Also verify bits beyond physical address width are 0*/
6775 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
6776 return 1;
6777 }
6778
6779 if (vmcs12->vm_entry_msr_load_count > 0 ||
6780 vmcs12->vm_exit_msr_load_count > 0 ||
6781 vmcs12->vm_exit_msr_store_count > 0) {
Jan Kiszkabd801582011-09-12 11:26:22 +02006782 pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n",
6783 __func__);
Nadav Har'El7c177932011-05-25 23:12:04 +03006784 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
6785 return 1;
6786 }
6787
6788 if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control,
6789 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) ||
6790 !vmx_control_verify(vmcs12->secondary_vm_exec_control,
6791 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) ||
6792 !vmx_control_verify(vmcs12->pin_based_vm_exec_control,
6793 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) ||
6794 !vmx_control_verify(vmcs12->vm_exit_controls,
6795 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) ||
6796 !vmx_control_verify(vmcs12->vm_entry_controls,
6797 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high))
6798 {
6799 nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD);
6800 return 1;
6801 }
6802
6803 if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
6804 ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
6805 nested_vmx_failValid(vcpu,
6806 VMXERR_ENTRY_INVALID_HOST_STATE_FIELD);
6807 return 1;
6808 }
6809
6810 if (((vmcs12->guest_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) ||
6811 ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) {
6812 nested_vmx_entry_failure(vcpu, vmcs12,
6813 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT);
6814 return 1;
6815 }
6816 if (vmcs12->vmcs_link_pointer != -1ull) {
6817 nested_vmx_entry_failure(vcpu, vmcs12,
6818 EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR);
6819 return 1;
6820 }
6821
6822 /*
6823 * We're finally done with prerequisite checking, and can start with
6824 * the nested entry.
6825 */
6826
Nadav Har'Elcd232ad2011-05-25 23:10:33 +03006827 vmcs02 = nested_get_current_vmcs02(vmx);
6828 if (!vmcs02)
6829 return -ENOMEM;
6830
6831 enter_guest_mode(vcpu);
6832
6833 vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET);
6834
6835 cpu = get_cpu();
6836 vmx->loaded_vmcs = vmcs02;
6837 vmx_vcpu_put(vcpu);
6838 vmx_vcpu_load(vcpu, cpu);
6839 vcpu->cpu = cpu;
6840 put_cpu();
6841
6842 vmcs12->launch_state = 1;
6843
6844 prepare_vmcs02(vcpu, vmcs12);
6845
6846 /*
6847 * Note no nested_vmx_succeed or nested_vmx_fail here. At this point
6848 * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet
6849 * returned as far as L1 is concerned. It will only return (and set
6850 * the success flag) when L2 exits (see nested_vmx_vmexit()).
6851 */
6852 return 1;
6853}
6854
Nadav Har'El4704d0b2011-05-25 23:11:34 +03006855/*
6856 * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date
6857 * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK).
6858 * This function returns the new value we should put in vmcs12.guest_cr0.
6859 * It's not enough to just return the vmcs02 GUEST_CR0. Rather,
6860 * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now
6861 * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0
6862 * didn't trap the bit, because if L1 did, so would L0).
6863 * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have
6864 * been modified by L2, and L1 knows it. So just leave the old value of
6865 * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0
6866 * isn't relevant, because if L0 traps this bit it can set it to anything.
6867 * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have
6868 * changed these bits, and therefore they need to be updated, but L0
6869 * didn't necessarily allow them to be changed in GUEST_CR0 - and rather
6870 * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there.
6871 */
6872static inline unsigned long
6873vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
6874{
6875 return
6876 /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) |
6877 /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) |
6878 /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask |
6879 vcpu->arch.cr0_guest_owned_bits));
6880}
6881
6882static inline unsigned long
6883vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
6884{
6885 return
6886 /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) |
6887 /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) |
6888 /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask |
6889 vcpu->arch.cr4_guest_owned_bits));
6890}
6891
6892/*
6893 * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits
6894 * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12),
6895 * and this function updates it to reflect the changes to the guest state while
6896 * L2 was running (and perhaps made some exits which were handled directly by L0
6897 * without going back to L1), and to reflect the exit reason.
6898 * Note that we do not have to copy here all VMCS fields, just those that
6899 * could have changed by the L2 guest or the exit - i.e., the guest-state and
6900 * exit-information fields only. Other fields are modified by L1 with VMWRITE,
6901 * which already writes to vmcs12 directly.
6902 */
6903void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
6904{
6905 /* update guest state fields: */
6906 vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12);
6907 vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12);
6908
6909 kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7);
6910 vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
6911 vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP);
6912 vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS);
6913
6914 vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR);
6915 vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR);
6916 vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR);
6917 vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR);
6918 vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR);
6919 vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR);
6920 vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR);
6921 vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR);
6922 vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT);
6923 vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT);
6924 vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT);
6925 vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT);
6926 vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT);
6927 vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT);
6928 vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT);
6929 vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT);
6930 vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT);
6931 vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT);
6932 vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES);
6933 vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES);
6934 vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES);
6935 vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES);
6936 vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES);
6937 vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES);
6938 vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES);
6939 vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES);
6940 vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE);
6941 vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE);
6942 vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE);
6943 vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE);
6944 vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE);
6945 vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE);
6946 vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE);
6947 vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE);
6948 vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE);
6949 vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE);
6950
6951 vmcs12->guest_activity_state = vmcs_read32(GUEST_ACTIVITY_STATE);
6952 vmcs12->guest_interruptibility_info =
6953 vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
6954 vmcs12->guest_pending_dbg_exceptions =
6955 vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS);
6956
6957 /* TODO: These cannot have changed unless we have MSR bitmaps and
6958 * the relevant bit asks not to trap the change */
6959 vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL);
6960 if (vmcs12->vm_entry_controls & VM_EXIT_SAVE_IA32_PAT)
6961 vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT);
6962 vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS);
6963 vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP);
6964 vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP);
6965
6966 /* update exit information fields: */
6967
6968 vmcs12->vm_exit_reason = vmcs_read32(VM_EXIT_REASON);
6969 vmcs12->exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6970
6971 vmcs12->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
6972 vmcs12->vm_exit_intr_error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
6973 vmcs12->idt_vectoring_info_field =
6974 vmcs_read32(IDT_VECTORING_INFO_FIELD);
6975 vmcs12->idt_vectoring_error_code =
6976 vmcs_read32(IDT_VECTORING_ERROR_CODE);
6977 vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
6978 vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6979
6980 /* clear vm-entry fields which are to be cleared on exit */
6981 if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
6982 vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK;
6983}
6984
6985/*
6986 * A part of what we need to when the nested L2 guest exits and we want to
6987 * run its L1 parent, is to reset L1's guest state to the host state specified
6988 * in vmcs12.
6989 * This function is to be called not only on normal nested exit, but also on
6990 * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry
6991 * Failures During or After Loading Guest State").
6992 * This function should be called when the active VMCS is L1's (vmcs01).
6993 */
6994void load_vmcs12_host_state(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12)
6995{
6996 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER)
6997 vcpu->arch.efer = vmcs12->host_ia32_efer;
6998 if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE)
6999 vcpu->arch.efer |= (EFER_LMA | EFER_LME);
7000 else
7001 vcpu->arch.efer &= ~(EFER_LMA | EFER_LME);
7002 vmx_set_efer(vcpu, vcpu->arch.efer);
7003
7004 kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp);
7005 kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip);
7006 /*
7007 * Note that calling vmx_set_cr0 is important, even if cr0 hasn't
7008 * actually changed, because it depends on the current state of
7009 * fpu_active (which may have changed).
7010 * Note that vmx_set_cr0 refers to efer set above.
7011 */
7012 kvm_set_cr0(vcpu, vmcs12->host_cr0);
7013 /*
7014 * If we did fpu_activate()/fpu_deactivate() during L2's run, we need
7015 * to apply the same changes to L1's vmcs. We just set cr0 correctly,
7016 * but we also need to update cr0_guest_host_mask and exception_bitmap.
7017 */
7018 update_exception_bitmap(vcpu);
7019 vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0);
7020 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
7021
7022 /*
7023 * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01
7024 * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask();
7025 */
7026 vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);
7027 kvm_set_cr4(vcpu, vmcs12->host_cr4);
7028
7029 /* shadow page tables on either EPT or shadow page tables */
7030 kvm_set_cr3(vcpu, vmcs12->host_cr3);
7031 kvm_mmu_reset_context(vcpu);
7032
7033 if (enable_vpid) {
7034 /*
7035 * Trivially support vpid by letting L2s share their parent
7036 * L1's vpid. TODO: move to a more elaborate solution, giving
7037 * each L2 its own vpid and exposing the vpid feature to L1.
7038 */
7039 vmx_flush_tlb(vcpu);
7040 }
7041
7042
7043 vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs);
7044 vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp);
7045 vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip);
7046 vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base);
7047 vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base);
7048 vmcs_writel(GUEST_TR_BASE, vmcs12->host_tr_base);
7049 vmcs_writel(GUEST_GS_BASE, vmcs12->host_gs_base);
7050 vmcs_writel(GUEST_FS_BASE, vmcs12->host_fs_base);
7051 vmcs_write16(GUEST_ES_SELECTOR, vmcs12->host_es_selector);
7052 vmcs_write16(GUEST_CS_SELECTOR, vmcs12->host_cs_selector);
7053 vmcs_write16(GUEST_SS_SELECTOR, vmcs12->host_ss_selector);
7054 vmcs_write16(GUEST_DS_SELECTOR, vmcs12->host_ds_selector);
7055 vmcs_write16(GUEST_FS_SELECTOR, vmcs12->host_fs_selector);
7056 vmcs_write16(GUEST_GS_SELECTOR, vmcs12->host_gs_selector);
7057 vmcs_write16(GUEST_TR_SELECTOR, vmcs12->host_tr_selector);
7058
7059 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT)
7060 vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat);
7061 if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
7062 vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL,
7063 vmcs12->host_ia32_perf_global_ctrl);
7064}
7065
7066/*
7067 * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1
7068 * and modify vmcs12 to make it see what it would expect to see there if
7069 * L2 was its real guest. Must only be called when in L2 (is_guest_mode())
7070 */
7071static void nested_vmx_vmexit(struct kvm_vcpu *vcpu)
7072{
7073 struct vcpu_vmx *vmx = to_vmx(vcpu);
7074 int cpu;
7075 struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
7076
7077 leave_guest_mode(vcpu);
7078 prepare_vmcs12(vcpu, vmcs12);
7079
7080 cpu = get_cpu();
7081 vmx->loaded_vmcs = &vmx->vmcs01;
7082 vmx_vcpu_put(vcpu);
7083 vmx_vcpu_load(vcpu, cpu);
7084 vcpu->cpu = cpu;
7085 put_cpu();
7086
7087 /* if no vmcs02 cache requested, remove the one we used */
7088 if (VMCS02_POOL_SIZE == 0)
7089 nested_free_vmcs02(vmx, vmx->nested.current_vmptr);
7090
7091 load_vmcs12_host_state(vcpu, vmcs12);
7092
Nadav Har'El27fc51b2011-08-02 15:54:52 +03007093 /* Update TSC_OFFSET if TSC was changed while L2 ran */
Nadav Har'El4704d0b2011-05-25 23:11:34 +03007094 vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset);
7095
7096 /* This is needed for same reason as it was needed in prepare_vmcs02 */
7097 vmx->host_rsp = 0;
7098
7099 /* Unpin physical memory we referred to in vmcs02 */
7100 if (vmx->nested.apic_access_page) {
7101 nested_release_page(vmx->nested.apic_access_page);
7102 vmx->nested.apic_access_page = 0;
7103 }
7104
7105 /*
7106 * Exiting from L2 to L1, we're now back to L1 which thinks it just
7107 * finished a VMLAUNCH or VMRESUME instruction, so we need to set the
7108 * success or failure flag accordingly.
7109 */
7110 if (unlikely(vmx->fail)) {
7111 vmx->fail = 0;
7112 nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR));
7113 } else
7114 nested_vmx_succeed(vcpu);
7115}
7116
Nadav Har'El7c177932011-05-25 23:12:04 +03007117/*
7118 * L1's failure to enter L2 is a subset of a normal exit, as explained in
7119 * 23.7 "VM-entry failures during or after loading guest state" (this also
7120 * lists the acceptable exit-reason and exit-qualification parameters).
7121 * It should only be called before L2 actually succeeded to run, and when
7122 * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss).
7123 */
7124static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu,
7125 struct vmcs12 *vmcs12,
7126 u32 reason, unsigned long qualification)
7127{
7128 load_vmcs12_host_state(vcpu, vmcs12);
7129 vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY;
7130 vmcs12->exit_qualification = qualification;
7131 nested_vmx_succeed(vcpu);
7132}
7133
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007134static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7135 struct x86_instruction_info *info,
7136 enum x86_intercept_stage stage)
7137{
7138 return X86EMUL_CONTINUE;
7139}
7140
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03007141static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08007142 .cpu_has_kvm_support = cpu_has_kvm_support,
7143 .disabled_by_bios = vmx_disabled_by_bios,
7144 .hardware_setup = hardware_setup,
7145 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03007146 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007147 .hardware_enable = hardware_enable,
7148 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08007149 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007150
7151 .vcpu_create = vmx_create_vcpu,
7152 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03007153 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007154
Avi Kivity04d2cc72007-09-10 18:10:54 +03007155 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007156 .vcpu_load = vmx_vcpu_load,
7157 .vcpu_put = vmx_vcpu_put,
7158
7159 .set_guest_debug = set_guest_debug,
7160 .get_msr = vmx_get_msr,
7161 .set_msr = vmx_set_msr,
7162 .get_segment_base = vmx_get_segment_base,
7163 .get_segment = vmx_get_segment,
7164 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02007165 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007166 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02007167 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02007168 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03007169 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007170 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007171 .set_cr3 = vmx_set_cr3,
7172 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007173 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007174 .get_idt = vmx_get_idt,
7175 .set_idt = vmx_set_idt,
7176 .get_gdt = vmx_get_gdt,
7177 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03007178 .set_dr7 = vmx_set_dr7,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007179 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007180 .get_rflags = vmx_get_rflags,
7181 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02007182 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02007183 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007184
7185 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007186
Avi Kivity6aa8b732006-12-10 02:21:36 -08007187 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02007188 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007189 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04007190 .set_interrupt_shadow = vmx_set_interrupt_shadow,
7191 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02007192 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03007193 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007194 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02007195 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03007196 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02007197 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007198 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01007199 .get_nmi_mask = vmx_get_nmi_mask,
7200 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007201 .enable_nmi_window = enable_nmi_window,
7202 .enable_irq_window = enable_irq_window,
7203 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03007204
Izik Eiduscbc94022007-10-25 00:29:55 +02007205 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08007206 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007207 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03007208
Avi Kivity586f9602010-11-18 13:09:54 +02007209 .get_exit_info = vmx_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02007210
Sheng Yang17cc3932010-01-05 19:02:27 +08007211 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08007212
7213 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08007214
7215 .rdtscp_supported = vmx_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02007216
7217 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08007218
7219 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007220
Joerg Roedel4051b182011-03-25 09:44:49 +01007221 .set_tsc_khz = vmx_set_tsc_khz,
Zachary Amsden99e3e302010-08-19 22:07:17 -10007222 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10007223 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01007224 .compute_tsc_offset = vmx_compute_tsc_offset,
Nadav Har'Eld5c17852011-08-02 15:54:20 +03007225 .read_l1_tsc = vmx_read_l1_tsc,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02007226
7227 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02007228
7229 .check_intercept = vmx_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08007230};
7231
7232static int __init vmx_init(void)
7233{
Avi Kivity26bb0982009-09-07 11:14:12 +03007234 int r, i;
7235
7236 rdmsrl_safe(MSR_EFER, &host_efer);
7237
7238 for (i = 0; i < NR_VMX_MSR; ++i)
7239 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03007240
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007241 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03007242 if (!vmx_io_bitmap_a)
7243 return -ENOMEM;
7244
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007245 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03007246 if (!vmx_io_bitmap_b) {
7247 r = -ENOMEM;
7248 goto out;
7249 }
7250
Avi Kivity58972972009-02-24 22:26:47 +02007251 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
7252 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08007253 r = -ENOMEM;
7254 goto out1;
7255 }
7256
Avi Kivity58972972009-02-24 22:26:47 +02007257 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
7258 if (!vmx_msr_bitmap_longmode) {
7259 r = -ENOMEM;
7260 goto out2;
7261 }
7262
He, Qingfdef3ad2007-04-30 09:45:24 +03007263 /*
7264 * Allow direct access to the PC debug port (it is often used for I/O
7265 * delays, but the vmexits simply slow things down).
7266 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007267 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
7268 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03007269
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007270 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03007271
Avi Kivity58972972009-02-24 22:26:47 +02007272 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
7273 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08007274
Sheng Yang2384d2b2008-01-17 15:14:33 +08007275 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
7276
Avi Kivity0ee75be2010-04-28 15:39:01 +03007277 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
7278 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03007279 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02007280 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08007281
Avi Kivity58972972009-02-24 22:26:47 +02007282 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
7283 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
7284 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
7285 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
7286 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
7287 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03007288
Avi Kivity089d0342009-03-23 18:26:32 +02007289 if (enable_ept) {
Sheng Yang534e38b2008-09-08 15:12:30 +08007290 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08007291 VMX_EPT_EXECUTABLE_MASK);
Xiao Guangrongce88dec2011-07-12 03:33:44 +08007292 ept_set_mmio_spte_mask();
Sheng Yang5fdbcb92008-07-16 09:25:40 +08007293 kvm_enable_tdp();
7294 } else
7295 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08007296
He, Qingfdef3ad2007-04-30 09:45:24 +03007297 return 0;
7298
Avi Kivity58972972009-02-24 22:26:47 +02007299out3:
7300 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08007301out2:
Avi Kivity58972972009-02-24 22:26:47 +02007302 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03007303out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007304 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03007305out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007306 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03007307 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08007308}
7309
7310static void __exit vmx_exit(void)
7311{
Avi Kivity58972972009-02-24 22:26:47 +02007312 free_page((unsigned long)vmx_msr_bitmap_legacy);
7313 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02007314 free_page((unsigned long)vmx_io_bitmap_b);
7315 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03007316
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08007317 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08007318}
7319
7320module_init(vmx_init)
7321module_exit(vmx_exit)