Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 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 Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 8 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9 | * |
| 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 Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 19 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 20 | #include "mmu.h" |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 21 | #include "cpuid.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 22 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 23 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 24 | #include <linux/module.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 26 | #include <linux/mm.h> |
| 27 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 28 | #include <linux/sched.h> |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 29 | #include <linux/moduleparam.h> |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 30 | #include <linux/mod_devicetable.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 31 | #include <linux/ftrace_event.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 33 | #include <linux/tboot.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 34 | #include "kvm_cache_regs.h" |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 35 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 36 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 37 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 38 | #include <asm/desc.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 39 | #include <asm/vmx.h> |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 40 | #include <asm/virtext.h> |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 41 | #include <asm/mce.h> |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 42 | #include <asm/i387.h> |
| 43 | #include <asm/xcr.h> |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 44 | #include <asm/perf_event.h> |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 45 | #include <asm/kexec.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 46 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 47 | #include "trace.h" |
| 48 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 49 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 50 | #define __ex_clear(x, reg) \ |
| 51 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 52 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 53 | MODULE_AUTHOR("Qumranet"); |
| 54 | MODULE_LICENSE("GPL"); |
| 55 | |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 56 | static const struct x86_cpu_id vmx_cpu_id[] = { |
| 57 | X86_FEATURE_MATCH(X86_FEATURE_VMX), |
| 58 | {} |
| 59 | }; |
| 60 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); |
| 61 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 62 | static bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 63 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 64 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 65 | static bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 66 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 67 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 68 | static bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 69 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 70 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 71 | static bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 72 | module_param_named(unrestricted_guest, |
| 73 | enable_unrestricted_guest, bool, S_IRUGO); |
| 74 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 75 | static bool __read_mostly enable_ept_ad_bits = 1; |
| 76 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 77 | |
Avi Kivity | a27685c | 2012-06-12 20:30:18 +0300 | [diff] [blame] | 78 | static bool __read_mostly emulate_invalid_guest_state = true; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 79 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 80 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 81 | static bool __read_mostly vmm_exclusive = 1; |
Dongxiao Xu | b923e62 | 2010-05-11 18:29:45 +0800 | [diff] [blame] | 82 | module_param(vmm_exclusive, bool, S_IRUGO); |
| 83 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 84 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 85 | module_param(fasteoi, bool, S_IRUGO); |
| 86 | |
Yang Zhang | 257090f | 2013-02-10 22:57:18 +0800 | [diff] [blame] | 87 | static bool __read_mostly enable_apicv_reg_vid; |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 88 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 89 | /* |
| 90 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 91 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 92 | * use VMX instructions. |
| 93 | */ |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 94 | static bool __read_mostly nested = 0; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 95 | module_param(nested, bool, S_IRUGO); |
| 96 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 97 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
| 98 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE) |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 99 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 100 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 101 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 102 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
| 103 | | X86_CR4_OSXMMEXCPT) |
| 104 | |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 105 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 106 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 107 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 108 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 109 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 110 | /* |
| 111 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 112 | * ple_gap: upper bound on the amount of time between two successive |
| 113 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 114 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 115 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 116 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 117 | * less than 2^12 cycles |
| 118 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 119 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 120 | */ |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 121 | #define KVM_VMX_DEFAULT_PLE_GAP 128 |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 122 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 |
| 123 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
| 124 | module_param(ple_gap, int, S_IRUGO); |
| 125 | |
| 126 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 127 | module_param(ple_window, int, S_IRUGO); |
| 128 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 129 | extern const ulong vmx_return; |
| 130 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 131 | #define NR_AUTOLOAD_MSRS 8 |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 132 | #define VMCS02_POOL_SIZE 1 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 133 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 134 | struct vmcs { |
| 135 | u32 revision_id; |
| 136 | u32 abort; |
| 137 | char data[0]; |
| 138 | }; |
| 139 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 140 | /* |
| 141 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 142 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 143 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 144 | */ |
| 145 | struct loaded_vmcs { |
| 146 | struct vmcs *vmcs; |
| 147 | int cpu; |
| 148 | int launched; |
| 149 | struct list_head loaded_vmcss_on_cpu_link; |
| 150 | }; |
| 151 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 152 | struct shared_msr_entry { |
| 153 | unsigned index; |
| 154 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 155 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 156 | }; |
| 157 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 158 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 159 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 160 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 161 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 162 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 163 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 164 | * More than one of these structures may exist, if L1 runs multiple L2 guests. |
| 165 | * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the |
| 166 | * underlying hardware which will be used to run L2. |
| 167 | * This structure is packed to ensure that its layout is identical across |
| 168 | * machines (necessary for live migration). |
| 169 | * If there are changes in this struct, VMCS12_REVISION must be changed. |
| 170 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 171 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 172 | struct __packed vmcs12 { |
| 173 | /* According to the Intel spec, a VMCS region must start with the |
| 174 | * following two fields. Then follow implementation-specific data. |
| 175 | */ |
| 176 | u32 revision_id; |
| 177 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 178 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 179 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 180 | u32 padding[7]; /* room for future expansion */ |
| 181 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 182 | u64 io_bitmap_a; |
| 183 | u64 io_bitmap_b; |
| 184 | u64 msr_bitmap; |
| 185 | u64 vm_exit_msr_store_addr; |
| 186 | u64 vm_exit_msr_load_addr; |
| 187 | u64 vm_entry_msr_load_addr; |
| 188 | u64 tsc_offset; |
| 189 | u64 virtual_apic_page_addr; |
| 190 | u64 apic_access_addr; |
| 191 | u64 ept_pointer; |
| 192 | u64 guest_physical_address; |
| 193 | u64 vmcs_link_pointer; |
| 194 | u64 guest_ia32_debugctl; |
| 195 | u64 guest_ia32_pat; |
| 196 | u64 guest_ia32_efer; |
| 197 | u64 guest_ia32_perf_global_ctrl; |
| 198 | u64 guest_pdptr0; |
| 199 | u64 guest_pdptr1; |
| 200 | u64 guest_pdptr2; |
| 201 | u64 guest_pdptr3; |
| 202 | u64 host_ia32_pat; |
| 203 | u64 host_ia32_efer; |
| 204 | u64 host_ia32_perf_global_ctrl; |
| 205 | u64 padding64[8]; /* room for future expansion */ |
| 206 | /* |
| 207 | * To allow migration of L1 (complete with its L2 guests) between |
| 208 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 209 | * unsigned long fields with no explict size. We use u64 (aliased |
| 210 | * natural_width) instead. Luckily, x86 is little-endian. |
| 211 | */ |
| 212 | natural_width cr0_guest_host_mask; |
| 213 | natural_width cr4_guest_host_mask; |
| 214 | natural_width cr0_read_shadow; |
| 215 | natural_width cr4_read_shadow; |
| 216 | natural_width cr3_target_value0; |
| 217 | natural_width cr3_target_value1; |
| 218 | natural_width cr3_target_value2; |
| 219 | natural_width cr3_target_value3; |
| 220 | natural_width exit_qualification; |
| 221 | natural_width guest_linear_address; |
| 222 | natural_width guest_cr0; |
| 223 | natural_width guest_cr3; |
| 224 | natural_width guest_cr4; |
| 225 | natural_width guest_es_base; |
| 226 | natural_width guest_cs_base; |
| 227 | natural_width guest_ss_base; |
| 228 | natural_width guest_ds_base; |
| 229 | natural_width guest_fs_base; |
| 230 | natural_width guest_gs_base; |
| 231 | natural_width guest_ldtr_base; |
| 232 | natural_width guest_tr_base; |
| 233 | natural_width guest_gdtr_base; |
| 234 | natural_width guest_idtr_base; |
| 235 | natural_width guest_dr7; |
| 236 | natural_width guest_rsp; |
| 237 | natural_width guest_rip; |
| 238 | natural_width guest_rflags; |
| 239 | natural_width guest_pending_dbg_exceptions; |
| 240 | natural_width guest_sysenter_esp; |
| 241 | natural_width guest_sysenter_eip; |
| 242 | natural_width host_cr0; |
| 243 | natural_width host_cr3; |
| 244 | natural_width host_cr4; |
| 245 | natural_width host_fs_base; |
| 246 | natural_width host_gs_base; |
| 247 | natural_width host_tr_base; |
| 248 | natural_width host_gdtr_base; |
| 249 | natural_width host_idtr_base; |
| 250 | natural_width host_ia32_sysenter_esp; |
| 251 | natural_width host_ia32_sysenter_eip; |
| 252 | natural_width host_rsp; |
| 253 | natural_width host_rip; |
| 254 | natural_width paddingl[8]; /* room for future expansion */ |
| 255 | u32 pin_based_vm_exec_control; |
| 256 | u32 cpu_based_vm_exec_control; |
| 257 | u32 exception_bitmap; |
| 258 | u32 page_fault_error_code_mask; |
| 259 | u32 page_fault_error_code_match; |
| 260 | u32 cr3_target_count; |
| 261 | u32 vm_exit_controls; |
| 262 | u32 vm_exit_msr_store_count; |
| 263 | u32 vm_exit_msr_load_count; |
| 264 | u32 vm_entry_controls; |
| 265 | u32 vm_entry_msr_load_count; |
| 266 | u32 vm_entry_intr_info_field; |
| 267 | u32 vm_entry_exception_error_code; |
| 268 | u32 vm_entry_instruction_len; |
| 269 | u32 tpr_threshold; |
| 270 | u32 secondary_vm_exec_control; |
| 271 | u32 vm_instruction_error; |
| 272 | u32 vm_exit_reason; |
| 273 | u32 vm_exit_intr_info; |
| 274 | u32 vm_exit_intr_error_code; |
| 275 | u32 idt_vectoring_info_field; |
| 276 | u32 idt_vectoring_error_code; |
| 277 | u32 vm_exit_instruction_len; |
| 278 | u32 vmx_instruction_info; |
| 279 | u32 guest_es_limit; |
| 280 | u32 guest_cs_limit; |
| 281 | u32 guest_ss_limit; |
| 282 | u32 guest_ds_limit; |
| 283 | u32 guest_fs_limit; |
| 284 | u32 guest_gs_limit; |
| 285 | u32 guest_ldtr_limit; |
| 286 | u32 guest_tr_limit; |
| 287 | u32 guest_gdtr_limit; |
| 288 | u32 guest_idtr_limit; |
| 289 | u32 guest_es_ar_bytes; |
| 290 | u32 guest_cs_ar_bytes; |
| 291 | u32 guest_ss_ar_bytes; |
| 292 | u32 guest_ds_ar_bytes; |
| 293 | u32 guest_fs_ar_bytes; |
| 294 | u32 guest_gs_ar_bytes; |
| 295 | u32 guest_ldtr_ar_bytes; |
| 296 | u32 guest_tr_ar_bytes; |
| 297 | u32 guest_interruptibility_info; |
| 298 | u32 guest_activity_state; |
| 299 | u32 guest_sysenter_cs; |
| 300 | u32 host_ia32_sysenter_cs; |
| 301 | u32 padding32[8]; /* room for future expansion */ |
| 302 | u16 virtual_processor_id; |
| 303 | u16 guest_es_selector; |
| 304 | u16 guest_cs_selector; |
| 305 | u16 guest_ss_selector; |
| 306 | u16 guest_ds_selector; |
| 307 | u16 guest_fs_selector; |
| 308 | u16 guest_gs_selector; |
| 309 | u16 guest_ldtr_selector; |
| 310 | u16 guest_tr_selector; |
| 311 | u16 host_es_selector; |
| 312 | u16 host_cs_selector; |
| 313 | u16 host_ss_selector; |
| 314 | u16 host_ds_selector; |
| 315 | u16 host_fs_selector; |
| 316 | u16 host_gs_selector; |
| 317 | u16 host_tr_selector; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | /* |
| 321 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 322 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 323 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. |
| 324 | */ |
| 325 | #define VMCS12_REVISION 0x11e57ed0 |
| 326 | |
| 327 | /* |
| 328 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 329 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 330 | * current implementation, 4K are reserved to avoid future complications. |
| 331 | */ |
| 332 | #define VMCS12_SIZE 0x1000 |
| 333 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 334 | /* Used to remember the last vmcs02 used for some recently used vmcs12s */ |
| 335 | struct vmcs02_list { |
| 336 | struct list_head list; |
| 337 | gpa_t vmptr; |
| 338 | struct loaded_vmcs vmcs02; |
| 339 | }; |
| 340 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 341 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 342 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 343 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 344 | */ |
| 345 | struct nested_vmx { |
| 346 | /* Has the level1 guest done vmxon? */ |
| 347 | bool vmxon; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 348 | |
| 349 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 350 | gpa_t current_vmptr; |
| 351 | /* The host-usable pointer to the above */ |
| 352 | struct page *current_vmcs12_page; |
| 353 | struct vmcs12 *current_vmcs12; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 354 | |
| 355 | /* vmcs02_list cache of VMCSs recently used to run L2 guests */ |
| 356 | struct list_head vmcs02_pool; |
| 357 | int vmcs02_num; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 358 | u64 vmcs01_tsc_offset; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 359 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 360 | bool nested_run_pending; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 361 | /* |
| 362 | * Guest pages referred to in vmcs02 with host-physical pointers, so |
| 363 | * we must keep them pinned while L2 runs. |
| 364 | */ |
| 365 | struct page *apic_access_page; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 366 | }; |
| 367 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 368 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 369 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 370 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 371 | u8 fail; |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 372 | u8 cpl; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 373 | bool nmi_known_unmasked; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 374 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 375 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 376 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 377 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 378 | int nmsrs; |
| 379 | int save_nmsrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 380 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 381 | u64 msr_host_kernel_gs_base; |
| 382 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 383 | #endif |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 384 | /* |
| 385 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 386 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 387 | * guest (L2), it points to a different VMCS. |
| 388 | */ |
| 389 | struct loaded_vmcs vmcs01; |
| 390 | struct loaded_vmcs *loaded_vmcs; |
| 391 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 392 | struct msr_autoload { |
| 393 | unsigned nr; |
| 394 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 395 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 396 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 397 | struct { |
| 398 | int loaded; |
| 399 | u16 fs_sel, gs_sel, ldt_sel; |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 400 | #ifdef CONFIG_X86_64 |
| 401 | u16 ds_sel, es_sel; |
| 402 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 403 | int gs_ldt_reload_needed; |
| 404 | int fs_reload_needed; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 405 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 406 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 407 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 408 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 409 | struct kvm_segment segs[8]; |
| 410 | } rmode; |
| 411 | struct { |
| 412 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 413 | struct kvm_save_segment { |
| 414 | u16 selector; |
| 415 | unsigned long base; |
| 416 | u32 limit; |
| 417 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 418 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 419 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 420 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 421 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 422 | |
| 423 | /* Support for vnmi-less CPUs */ |
| 424 | int soft_vnmi_blocked; |
| 425 | ktime_t entry_time; |
| 426 | s64 vnmi_blocked_time; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 427 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 428 | |
| 429 | bool rdtscp_enabled; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 430 | |
| 431 | /* Support for a guest hypervisor (nested VMX) */ |
| 432 | struct nested_vmx nested; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 433 | }; |
| 434 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 435 | enum segment_cache_field { |
| 436 | SEG_FIELD_SEL = 0, |
| 437 | SEG_FIELD_BASE = 1, |
| 438 | SEG_FIELD_LIMIT = 2, |
| 439 | SEG_FIELD_AR = 3, |
| 440 | |
| 441 | SEG_FIELD_NR = 4 |
| 442 | }; |
| 443 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 444 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 445 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 446 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 447 | } |
| 448 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 449 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
| 450 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) |
| 451 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ |
| 452 | [number##_HIGH] = VMCS12_OFFSET(name)+4 |
| 453 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 454 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 455 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
| 456 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 457 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 458 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 459 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 460 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 461 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 462 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 463 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
| 464 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 465 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 466 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 467 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 468 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 469 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 470 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 471 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 472 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 473 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 474 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 475 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 476 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
| 477 | FIELD64(TSC_OFFSET, tsc_offset), |
| 478 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 479 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
| 480 | FIELD64(EPT_POINTER, ept_pointer), |
| 481 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 482 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 483 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 484 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 485 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 486 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 487 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 488 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 489 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 490 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
| 491 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 492 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 493 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 494 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 495 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 496 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 497 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 498 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 499 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 500 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 501 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 502 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 503 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 504 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 505 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 506 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 507 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 508 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 509 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 510 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 511 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 512 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 513 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 514 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 515 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 516 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 517 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 518 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 519 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 520 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 521 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 522 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 523 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 524 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 525 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 526 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 527 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 528 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 529 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 530 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 531 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 532 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 533 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 534 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 535 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 536 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 537 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 538 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 539 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
| 540 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 541 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 542 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 543 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 544 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 545 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 546 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 547 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 548 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 549 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 550 | FIELD(GUEST_CR0, guest_cr0), |
| 551 | FIELD(GUEST_CR3, guest_cr3), |
| 552 | FIELD(GUEST_CR4, guest_cr4), |
| 553 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 554 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 555 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 556 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 557 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 558 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 559 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 560 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 561 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 562 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 563 | FIELD(GUEST_DR7, guest_dr7), |
| 564 | FIELD(GUEST_RSP, guest_rsp), |
| 565 | FIELD(GUEST_RIP, guest_rip), |
| 566 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 567 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 568 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 569 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 570 | FIELD(HOST_CR0, host_cr0), |
| 571 | FIELD(HOST_CR3, host_cr3), |
| 572 | FIELD(HOST_CR4, host_cr4), |
| 573 | FIELD(HOST_FS_BASE, host_fs_base), |
| 574 | FIELD(HOST_GS_BASE, host_gs_base), |
| 575 | FIELD(HOST_TR_BASE, host_tr_base), |
| 576 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 577 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 578 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 579 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 580 | FIELD(HOST_RSP, host_rsp), |
| 581 | FIELD(HOST_RIP, host_rip), |
| 582 | }; |
| 583 | static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table); |
| 584 | |
| 585 | static inline short vmcs_field_to_offset(unsigned long field) |
| 586 | { |
| 587 | if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0) |
| 588 | return -1; |
| 589 | return vmcs_field_to_offset_table[field]; |
| 590 | } |
| 591 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 592 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 593 | { |
| 594 | return to_vmx(vcpu)->nested.current_vmcs12; |
| 595 | } |
| 596 | |
| 597 | static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr) |
| 598 | { |
| 599 | struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT); |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 600 | if (is_error_page(page)) |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 601 | return NULL; |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 602 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 603 | return page; |
| 604 | } |
| 605 | |
| 606 | static void nested_release_page(struct page *page) |
| 607 | { |
| 608 | kvm_release_page_dirty(page); |
| 609 | } |
| 610 | |
| 611 | static void nested_release_page_clean(struct page *page) |
| 612 | { |
| 613 | kvm_release_page_clean(page); |
| 614 | } |
| 615 | |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 616 | static u64 construct_eptp(unsigned long root_hpa); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 617 | static void kvm_cpu_vmxon(u64 addr); |
| 618 | static void kvm_cpu_vmxoff(void); |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 619 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 620 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 621 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 622 | struct kvm_segment *var, int seg); |
| 623 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 624 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 625 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 626 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 627 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 628 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 629 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 630 | /* |
| 631 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 632 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 633 | */ |
| 634 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 635 | static DEFINE_PER_CPU(struct desc_ptr, host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 636 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 637 | static unsigned long *vmx_io_bitmap_a; |
| 638 | static unsigned long *vmx_io_bitmap_b; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 639 | static unsigned long *vmx_msr_bitmap_legacy; |
| 640 | static unsigned long *vmx_msr_bitmap_longmode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 641 | static unsigned long *vmx_msr_bitmap_legacy_x2apic; |
| 642 | static unsigned long *vmx_msr_bitmap_longmode_x2apic; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 643 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 644 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 645 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 646 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 647 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 648 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 649 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 650 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 651 | int size; |
| 652 | int order; |
| 653 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 654 | u32 pin_based_exec_ctrl; |
| 655 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 656 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 657 | u32 vmexit_ctrl; |
| 658 | u32 vmentry_ctrl; |
| 659 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 660 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 661 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 662 | u32 ept; |
| 663 | u32 vpid; |
| 664 | } vmx_capability; |
| 665 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 666 | #define VMX_SEGMENT_FIELD(seg) \ |
| 667 | [VCPU_SREG_##seg] = { \ |
| 668 | .selector = GUEST_##seg##_SELECTOR, \ |
| 669 | .base = GUEST_##seg##_BASE, \ |
| 670 | .limit = GUEST_##seg##_LIMIT, \ |
| 671 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 672 | } |
| 673 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 674 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 675 | unsigned selector; |
| 676 | unsigned base; |
| 677 | unsigned limit; |
| 678 | unsigned ar_bytes; |
| 679 | } kvm_vmx_segment_fields[] = { |
| 680 | VMX_SEGMENT_FIELD(CS), |
| 681 | VMX_SEGMENT_FIELD(DS), |
| 682 | VMX_SEGMENT_FIELD(ES), |
| 683 | VMX_SEGMENT_FIELD(FS), |
| 684 | VMX_SEGMENT_FIELD(GS), |
| 685 | VMX_SEGMENT_FIELD(SS), |
| 686 | VMX_SEGMENT_FIELD(TR), |
| 687 | VMX_SEGMENT_FIELD(LDTR), |
| 688 | }; |
| 689 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 690 | static u64 host_efer; |
| 691 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 692 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 693 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 694 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 695 | * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 696 | * away by decrementing the array size. |
| 697 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 698 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 699 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 700 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 701 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 702 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 703 | }; |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 704 | #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 705 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 706 | static inline bool is_page_fault(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 707 | { |
| 708 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 709 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 710 | (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 711 | } |
| 712 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 713 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 714 | { |
| 715 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 716 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 717 | (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 718 | } |
| 719 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 720 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 721 | { |
| 722 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 723 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 724 | (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 725 | } |
| 726 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 727 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 728 | { |
| 729 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 730 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 731 | } |
| 732 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 733 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 734 | { |
| 735 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 736 | INTR_INFO_VALID_MASK)) == |
| 737 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 738 | } |
| 739 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 740 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 741 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 742 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 743 | } |
| 744 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 745 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 746 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 747 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 748 | } |
| 749 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 750 | static inline bool vm_need_tpr_shadow(struct kvm *kvm) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 751 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 752 | return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 753 | } |
| 754 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 755 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 756 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 757 | return vmcs_config.cpu_based_exec_ctrl & |
| 758 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 759 | } |
| 760 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 761 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 762 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 763 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 764 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 765 | } |
| 766 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 767 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 768 | { |
| 769 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 770 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 771 | } |
| 772 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 773 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 774 | { |
| 775 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 776 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 777 | } |
| 778 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 779 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 780 | { |
| 781 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 782 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 783 | } |
| 784 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 785 | static inline bool cpu_has_vmx_flexpriority(void) |
| 786 | { |
| 787 | return cpu_has_vmx_tpr_shadow() && |
| 788 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 789 | } |
| 790 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 791 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 792 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 793 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | static inline bool cpu_has_vmx_eptp_uncacheable(void) |
| 797 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 798 | return vmx_capability.ept & VMX_EPTP_UC_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | static inline bool cpu_has_vmx_eptp_writeback(void) |
| 802 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 803 | return vmx_capability.ept & VMX_EPTP_WB_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 807 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 808 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 809 | } |
| 810 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 811 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 812 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 813 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 814 | } |
| 815 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 816 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 817 | { |
| 818 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 819 | } |
| 820 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 821 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 822 | { |
| 823 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 824 | } |
| 825 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 826 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 827 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 828 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 829 | } |
| 830 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 831 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 832 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 833 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 834 | } |
| 835 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 836 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 837 | { |
| 838 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 839 | } |
| 840 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 841 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 842 | { |
| 843 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 844 | } |
| 845 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 846 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 847 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 848 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 849 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 850 | } |
| 851 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 852 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 853 | { |
| 854 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 855 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 856 | } |
| 857 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 858 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 859 | { |
| 860 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 861 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 862 | } |
| 863 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 864 | static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 865 | { |
Gui Jianfeng | 6d3e435 | 2010-01-29 15:36:59 +0800 | [diff] [blame] | 866 | return flexpriority_enabled && irqchip_in_kernel(kvm); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 867 | } |
| 868 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 869 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 870 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 871 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 872 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 873 | } |
| 874 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 875 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 876 | { |
| 877 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 878 | SECONDARY_EXEC_RDTSCP; |
| 879 | } |
| 880 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 881 | static inline bool cpu_has_vmx_invpcid(void) |
| 882 | { |
| 883 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 884 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 885 | } |
| 886 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 887 | static inline bool cpu_has_virtual_nmis(void) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 888 | { |
| 889 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 890 | } |
| 891 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 892 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 893 | { |
| 894 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 895 | SECONDARY_EXEC_WBINVD_EXITING; |
| 896 | } |
| 897 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 898 | static inline bool report_flexpriority(void) |
| 899 | { |
| 900 | return flexpriority_enabled; |
| 901 | } |
| 902 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 903 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 904 | { |
| 905 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 906 | } |
| 907 | |
| 908 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 909 | { |
| 910 | return (vmcs12->cpu_based_vm_exec_control & |
| 911 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 912 | (vmcs12->secondary_vm_exec_control & bit); |
| 913 | } |
| 914 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 915 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12, |
| 916 | struct kvm_vcpu *vcpu) |
| 917 | { |
| 918 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 919 | } |
| 920 | |
| 921 | static inline bool is_exception(u32 intr_info) |
| 922 | { |
| 923 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 924 | == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK); |
| 925 | } |
| 926 | |
| 927 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 928 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 929 | struct vmcs12 *vmcs12, |
| 930 | u32 reason, unsigned long qualification); |
| 931 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 932 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 933 | { |
| 934 | int i; |
| 935 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 936 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 937 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 938 | return i; |
| 939 | return -1; |
| 940 | } |
| 941 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 942 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 943 | { |
| 944 | struct { |
| 945 | u64 vpid : 16; |
| 946 | u64 rsvd : 48; |
| 947 | u64 gva; |
| 948 | } operand = { vpid, 0, gva }; |
| 949 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 950 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 951 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 952 | "; ja 1f ; ud2 ; 1:" |
| 953 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 954 | } |
| 955 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 956 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 957 | { |
| 958 | struct { |
| 959 | u64 eptp, gpa; |
| 960 | } operand = {eptp, gpa}; |
| 961 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 962 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 963 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 964 | "; ja 1f ; ud2 ; 1:\n" |
| 965 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 966 | } |
| 967 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 968 | static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 969 | { |
| 970 | int i; |
| 971 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 972 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 973 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 974 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 975 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 976 | } |
| 977 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 978 | static void vmcs_clear(struct vmcs *vmcs) |
| 979 | { |
| 980 | u64 phys_addr = __pa(vmcs); |
| 981 | u8 error; |
| 982 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 983 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 984 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 985 | : "cc", "memory"); |
| 986 | if (error) |
| 987 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 988 | vmcs, phys_addr); |
| 989 | } |
| 990 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 991 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 992 | { |
| 993 | vmcs_clear(loaded_vmcs->vmcs); |
| 994 | loaded_vmcs->cpu = -1; |
| 995 | loaded_vmcs->launched = 0; |
| 996 | } |
| 997 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 998 | static void vmcs_load(struct vmcs *vmcs) |
| 999 | { |
| 1000 | u64 phys_addr = __pa(vmcs); |
| 1001 | u8 error; |
| 1002 | |
| 1003 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1004 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1005 | : "cc", "memory"); |
| 1006 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 1007 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1008 | vmcs, phys_addr); |
| 1009 | } |
| 1010 | |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1011 | #ifdef CONFIG_KEXEC |
| 1012 | /* |
| 1013 | * This bitmap is used to indicate whether the vmclear |
| 1014 | * operation is enabled on all cpus. All disabled by |
| 1015 | * default. |
| 1016 | */ |
| 1017 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 1018 | |
| 1019 | static inline void crash_enable_local_vmclear(int cpu) |
| 1020 | { |
| 1021 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1022 | } |
| 1023 | |
| 1024 | static inline void crash_disable_local_vmclear(int cpu) |
| 1025 | { |
| 1026 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1027 | } |
| 1028 | |
| 1029 | static inline int crash_local_vmclear_enabled(int cpu) |
| 1030 | { |
| 1031 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1032 | } |
| 1033 | |
| 1034 | static void crash_vmclear_local_loaded_vmcss(void) |
| 1035 | { |
| 1036 | int cpu = raw_smp_processor_id(); |
| 1037 | struct loaded_vmcs *v; |
| 1038 | |
| 1039 | if (!crash_local_vmclear_enabled(cpu)) |
| 1040 | return; |
| 1041 | |
| 1042 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 1043 | loaded_vmcss_on_cpu_link) |
| 1044 | vmcs_clear(v->vmcs); |
| 1045 | } |
| 1046 | #else |
| 1047 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 1048 | static inline void crash_disable_local_vmclear(int cpu) { } |
| 1049 | #endif /* CONFIG_KEXEC */ |
| 1050 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1051 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1052 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1053 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1054 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1055 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1056 | if (loaded_vmcs->cpu != cpu) |
| 1057 | return; /* vcpu migration can race with cpu offline */ |
| 1058 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1059 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1060 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1061 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1062 | |
| 1063 | /* |
| 1064 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 1065 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 1066 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 1067 | * then adds the vmcs into percpu list before it is deleted. |
| 1068 | */ |
| 1069 | smp_wmb(); |
| 1070 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1071 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1072 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1073 | } |
| 1074 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1075 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1076 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 1077 | int cpu = loaded_vmcs->cpu; |
| 1078 | |
| 1079 | if (cpu != -1) |
| 1080 | smp_call_function_single(cpu, |
| 1081 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
Gui Jianfeng | 1760dd4 | 2010-06-07 10:33:27 +0800 | [diff] [blame] | 1084 | static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1085 | { |
| 1086 | if (vmx->vpid == 0) |
| 1087 | return; |
| 1088 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1089 | if (cpu_has_vmx_invvpid_single()) |
| 1090 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1091 | } |
| 1092 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1093 | static inline void vpid_sync_vcpu_global(void) |
| 1094 | { |
| 1095 | if (cpu_has_vmx_invvpid_global()) |
| 1096 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 1097 | } |
| 1098 | |
| 1099 | static inline void vpid_sync_context(struct vcpu_vmx *vmx) |
| 1100 | { |
| 1101 | if (cpu_has_vmx_invvpid_single()) |
Gui Jianfeng | 1760dd4 | 2010-06-07 10:33:27 +0800 | [diff] [blame] | 1102 | vpid_sync_vcpu_single(vmx); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1103 | else |
| 1104 | vpid_sync_vcpu_global(); |
| 1105 | } |
| 1106 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1107 | static inline void ept_sync_global(void) |
| 1108 | { |
| 1109 | if (cpu_has_vmx_invept_global()) |
| 1110 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
| 1111 | } |
| 1112 | |
| 1113 | static inline void ept_sync_context(u64 eptp) |
| 1114 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1115 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1116 | if (cpu_has_vmx_invept_context()) |
| 1117 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 1118 | else |
| 1119 | ept_sync_global(); |
| 1120 | } |
| 1121 | } |
| 1122 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1123 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1124 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1125 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1126 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1127 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 1128 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1129 | return value; |
| 1130 | } |
| 1131 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1132 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1133 | { |
| 1134 | return vmcs_readl(field); |
| 1135 | } |
| 1136 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1137 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1138 | { |
| 1139 | return vmcs_readl(field); |
| 1140 | } |
| 1141 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1142 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1143 | { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1144 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1145 | return vmcs_readl(field); |
| 1146 | #else |
| 1147 | return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32); |
| 1148 | #endif |
| 1149 | } |
| 1150 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1151 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 1152 | { |
| 1153 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 1154 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 1155 | dump_stack(); |
| 1156 | } |
| 1157 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1158 | static void vmcs_writel(unsigned long field, unsigned long value) |
| 1159 | { |
| 1160 | u8 error; |
| 1161 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1162 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1163 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1164 | if (unlikely(error)) |
| 1165 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | static void vmcs_write16(unsigned long field, u16 value) |
| 1169 | { |
| 1170 | vmcs_writel(field, value); |
| 1171 | } |
| 1172 | |
| 1173 | static void vmcs_write32(unsigned long field, u32 value) |
| 1174 | { |
| 1175 | vmcs_writel(field, value); |
| 1176 | } |
| 1177 | |
| 1178 | static void vmcs_write64(unsigned long field, u64 value) |
| 1179 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1180 | vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 1181 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1182 | asm volatile (""); |
| 1183 | vmcs_writel(field+1, value >> 32); |
| 1184 | #endif |
| 1185 | } |
| 1186 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1187 | static void vmcs_clear_bits(unsigned long field, u32 mask) |
| 1188 | { |
| 1189 | vmcs_writel(field, vmcs_readl(field) & ~mask); |
| 1190 | } |
| 1191 | |
| 1192 | static void vmcs_set_bits(unsigned long field, u32 mask) |
| 1193 | { |
| 1194 | vmcs_writel(field, vmcs_readl(field) | mask); |
| 1195 | } |
| 1196 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1197 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 1198 | { |
| 1199 | vmx->segment_cache.bitmask = 0; |
| 1200 | } |
| 1201 | |
| 1202 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 1203 | unsigned field) |
| 1204 | { |
| 1205 | bool ret; |
| 1206 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 1207 | |
| 1208 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 1209 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 1210 | vmx->segment_cache.bitmask = 0; |
| 1211 | } |
| 1212 | ret = vmx->segment_cache.bitmask & mask; |
| 1213 | vmx->segment_cache.bitmask |= mask; |
| 1214 | return ret; |
| 1215 | } |
| 1216 | |
| 1217 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 1218 | { |
| 1219 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 1220 | |
| 1221 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 1222 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 1223 | return *p; |
| 1224 | } |
| 1225 | |
| 1226 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 1227 | { |
| 1228 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 1229 | |
| 1230 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 1231 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 1232 | return *p; |
| 1233 | } |
| 1234 | |
| 1235 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 1236 | { |
| 1237 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 1238 | |
| 1239 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 1240 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 1241 | return *p; |
| 1242 | } |
| 1243 | |
| 1244 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 1245 | { |
| 1246 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 1247 | |
| 1248 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 1249 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 1250 | return *p; |
| 1251 | } |
| 1252 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1253 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 1254 | { |
| 1255 | u32 eb; |
| 1256 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1257 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
| 1258 | (1u << NM_VECTOR) | (1u << DB_VECTOR); |
| 1259 | if ((vcpu->guest_debug & |
| 1260 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 1261 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 1262 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1263 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1264 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1265 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1266 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1267 | if (vcpu->fpu_active) |
| 1268 | eb &= ~(1u << NM_VECTOR); |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1269 | |
| 1270 | /* When we are running a nested L2 guest and L1 specified for it a |
| 1271 | * certain exception bitmap, we must trap the same exceptions and pass |
| 1272 | * them to L1. When running L2, we will only handle the exceptions |
| 1273 | * specified above if L1 did not want them. |
| 1274 | */ |
| 1275 | if (is_guest_mode(vcpu)) |
| 1276 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 1277 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1278 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 1279 | } |
| 1280 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1281 | static void clear_atomic_switch_msr_special(unsigned long entry, |
| 1282 | unsigned long exit) |
| 1283 | { |
| 1284 | vmcs_clear_bits(VM_ENTRY_CONTROLS, entry); |
| 1285 | vmcs_clear_bits(VM_EXIT_CONTROLS, exit); |
| 1286 | } |
| 1287 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1288 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 1289 | { |
| 1290 | unsigned i; |
| 1291 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1292 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1293 | switch (msr) { |
| 1294 | case MSR_EFER: |
| 1295 | if (cpu_has_load_ia32_efer) { |
| 1296 | clear_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER, |
| 1297 | VM_EXIT_LOAD_IA32_EFER); |
| 1298 | return; |
| 1299 | } |
| 1300 | break; |
| 1301 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1302 | if (cpu_has_load_perf_global_ctrl) { |
| 1303 | clear_atomic_switch_msr_special( |
| 1304 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1305 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 1306 | return; |
| 1307 | } |
| 1308 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1309 | } |
| 1310 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1311 | for (i = 0; i < m->nr; ++i) |
| 1312 | if (m->guest[i].index == msr) |
| 1313 | break; |
| 1314 | |
| 1315 | if (i == m->nr) |
| 1316 | return; |
| 1317 | --m->nr; |
| 1318 | m->guest[i] = m->guest[m->nr]; |
| 1319 | m->host[i] = m->host[m->nr]; |
| 1320 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1321 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1322 | } |
| 1323 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1324 | static void add_atomic_switch_msr_special(unsigned long entry, |
| 1325 | unsigned long exit, unsigned long guest_val_vmcs, |
| 1326 | unsigned long host_val_vmcs, u64 guest_val, u64 host_val) |
| 1327 | { |
| 1328 | vmcs_write64(guest_val_vmcs, guest_val); |
| 1329 | vmcs_write64(host_val_vmcs, host_val); |
| 1330 | vmcs_set_bits(VM_ENTRY_CONTROLS, entry); |
| 1331 | vmcs_set_bits(VM_EXIT_CONTROLS, exit); |
| 1332 | } |
| 1333 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1334 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 1335 | u64 guest_val, u64 host_val) |
| 1336 | { |
| 1337 | unsigned i; |
| 1338 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1339 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1340 | switch (msr) { |
| 1341 | case MSR_EFER: |
| 1342 | if (cpu_has_load_ia32_efer) { |
| 1343 | add_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER, |
| 1344 | VM_EXIT_LOAD_IA32_EFER, |
| 1345 | GUEST_IA32_EFER, |
| 1346 | HOST_IA32_EFER, |
| 1347 | guest_val, host_val); |
| 1348 | return; |
| 1349 | } |
| 1350 | break; |
| 1351 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1352 | if (cpu_has_load_perf_global_ctrl) { |
| 1353 | add_atomic_switch_msr_special( |
| 1354 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1355 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1356 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 1357 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 1358 | guest_val, host_val); |
| 1359 | return; |
| 1360 | } |
| 1361 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1362 | } |
| 1363 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1364 | for (i = 0; i < m->nr; ++i) |
| 1365 | if (m->guest[i].index == msr) |
| 1366 | break; |
| 1367 | |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 1368 | if (i == NR_AUTOLOAD_MSRS) { |
| 1369 | printk_once(KERN_WARNING"Not enough mst switch entries. " |
| 1370 | "Can't add msr %x\n", msr); |
| 1371 | return; |
| 1372 | } else if (i == m->nr) { |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1373 | ++m->nr; |
| 1374 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1375 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1376 | } |
| 1377 | |
| 1378 | m->guest[i].index = msr; |
| 1379 | m->guest[i].value = guest_val; |
| 1380 | m->host[i].index = msr; |
| 1381 | m->host[i].value = host_val; |
| 1382 | } |
| 1383 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1384 | static void reload_tss(void) |
| 1385 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1386 | /* |
| 1387 | * VT restores TR but not its size. Useless. |
| 1388 | */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1389 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 1390 | struct desc_struct *descs; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1391 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1392 | descs = (void *)gdt->address; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1393 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 1394 | load_TR_desc(); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1395 | } |
| 1396 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1397 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1398 | { |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 1399 | u64 guest_efer; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1400 | u64 ignore_bits; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1401 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1402 | guest_efer = vmx->vcpu.arch.efer; |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 1403 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1404 | /* |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 1405 | * NX is emulated; LMA and LME handled by hardware; SCE meaningless |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1406 | * outside long mode |
| 1407 | */ |
| 1408 | ignore_bits = EFER_NX | EFER_SCE; |
| 1409 | #ifdef CONFIG_X86_64 |
| 1410 | ignore_bits |= EFER_LMA | EFER_LME; |
| 1411 | /* SCE is meaningful only in long mode on Intel */ |
| 1412 | if (guest_efer & EFER_LMA) |
| 1413 | ignore_bits &= ~(u64)EFER_SCE; |
| 1414 | #endif |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1415 | guest_efer &= ~ignore_bits; |
| 1416 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1417 | vmx->guest_msrs[efer_offset].data = guest_efer; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 1418 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 1419 | |
| 1420 | clear_atomic_switch_msr(vmx, MSR_EFER); |
| 1421 | /* On ept, can't emulate nx, and must switch nx atomically */ |
| 1422 | if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) { |
| 1423 | guest_efer = vmx->vcpu.arch.efer; |
| 1424 | if (!(guest_efer & EFER_LMA)) |
| 1425 | guest_efer &= ~EFER_LME; |
| 1426 | add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer); |
| 1427 | return false; |
| 1428 | } |
| 1429 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1430 | return true; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1431 | } |
| 1432 | |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1433 | static unsigned long segment_base(u16 selector) |
| 1434 | { |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1435 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1436 | struct desc_struct *d; |
| 1437 | unsigned long table_base; |
| 1438 | unsigned long v; |
| 1439 | |
| 1440 | if (!(selector & ~3)) |
| 1441 | return 0; |
| 1442 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1443 | table_base = gdt->address; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1444 | |
| 1445 | if (selector & 4) { /* from ldt */ |
| 1446 | u16 ldt_selector = kvm_read_ldt(); |
| 1447 | |
| 1448 | if (!(ldt_selector & ~3)) |
| 1449 | return 0; |
| 1450 | |
| 1451 | table_base = segment_base(ldt_selector); |
| 1452 | } |
| 1453 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
| 1454 | v = get_desc_base(d); |
| 1455 | #ifdef CONFIG_X86_64 |
| 1456 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 1457 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; |
| 1458 | #endif |
| 1459 | return v; |
| 1460 | } |
| 1461 | |
| 1462 | static inline unsigned long kvm_read_tr_base(void) |
| 1463 | { |
| 1464 | u16 tr; |
| 1465 | asm("str %0" : "=g"(tr)); |
| 1466 | return segment_base(tr); |
| 1467 | } |
| 1468 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1469 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1470 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1471 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1472 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1473 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1474 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1475 | return; |
| 1476 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1477 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1478 | /* |
| 1479 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 1480 | * allow segment selectors with cpl > 0 or ti == 1. |
| 1481 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1482 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1483 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1484 | savesegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1485 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1486 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1487 | vmx->host_state.fs_reload_needed = 0; |
| 1488 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1489 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1490 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1491 | } |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1492 | savesegment(gs, vmx->host_state.gs_sel); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1493 | if (!(vmx->host_state.gs_sel & 7)) |
| 1494 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1495 | else { |
| 1496 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1497 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | #ifdef CONFIG_X86_64 |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1501 | savesegment(ds, vmx->host_state.ds_sel); |
| 1502 | savesegment(es, vmx->host_state.es_sel); |
| 1503 | #endif |
| 1504 | |
| 1505 | #ifdef CONFIG_X86_64 |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1506 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 1507 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 1508 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1509 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 1510 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1511 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 1512 | |
| 1513 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1514 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
| 1515 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1516 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 1517 | #endif |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1518 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 1519 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 1520 | vmx->guest_msrs[i].data, |
| 1521 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1522 | } |
| 1523 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1524 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1525 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1526 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1527 | return; |
| 1528 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 1529 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1530 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1531 | #ifdef CONFIG_X86_64 |
| 1532 | if (is_long_mode(&vmx->vcpu)) |
| 1533 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 1534 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1535 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1536 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1537 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1538 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1539 | #else |
| 1540 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1541 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1542 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 1543 | if (vmx->host_state.fs_reload_needed) |
| 1544 | loadsegment(fs, vmx->host_state.fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1545 | #ifdef CONFIG_X86_64 |
| 1546 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { |
| 1547 | loadsegment(ds, vmx->host_state.ds_sel); |
| 1548 | loadsegment(es, vmx->host_state.es_sel); |
| 1549 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1550 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1551 | reload_tss(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1552 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1553 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1554 | #endif |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 1555 | /* |
| 1556 | * If the FPU is not active (through the host task or |
| 1557 | * the guest vcpu), then restore the cr0.TS bit. |
| 1558 | */ |
| 1559 | if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded) |
| 1560 | stts(); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 1561 | load_gdt(&__get_cpu_var(host_gdt)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1562 | } |
| 1563 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1564 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 1565 | { |
| 1566 | preempt_disable(); |
| 1567 | __vmx_load_host_state(vmx); |
| 1568 | preempt_enable(); |
| 1569 | } |
| 1570 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1571 | /* |
| 1572 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 1573 | * vcpu mutex is already taken. |
| 1574 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1575 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1576 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1577 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1578 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1579 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1580 | if (!vmm_exclusive) |
| 1581 | kvm_cpu_vmxon(phys_addr); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1582 | else if (vmx->loaded_vmcs->cpu != cpu) |
| 1583 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1584 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1585 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 1586 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 1587 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1588 | } |
| 1589 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1590 | if (vmx->loaded_vmcs->cpu != cpu) { |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1591 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1592 | unsigned long sysenter_esp; |
| 1593 | |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 1594 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1595 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1596 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1597 | |
| 1598 | /* |
| 1599 | * Read loaded_vmcs->cpu should be before fetching |
| 1600 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 1601 | * See the comments in __loaded_vmcs_clear(). |
| 1602 | */ |
| 1603 | smp_rmb(); |
| 1604 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1605 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 1606 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1607 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1608 | local_irq_enable(); |
| 1609 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1610 | /* |
| 1611 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 1612 | * processors. |
| 1613 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1614 | vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1615 | vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1616 | |
| 1617 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 1618 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1619 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1620 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 1624 | { |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1625 | __vmx_load_host_state(to_vmx(vcpu)); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1626 | if (!vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1627 | __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs); |
| 1628 | vcpu->cpu = -1; |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1629 | kvm_cpu_vmxoff(); |
| 1630 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1631 | } |
| 1632 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1633 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 1634 | { |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1635 | ulong cr0; |
| 1636 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1637 | if (vcpu->fpu_active) |
| 1638 | return; |
| 1639 | vcpu->fpu_active = 1; |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1640 | cr0 = vmcs_readl(GUEST_CR0); |
| 1641 | cr0 &= ~(X86_CR0_TS | X86_CR0_MP); |
| 1642 | cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP); |
| 1643 | vmcs_writel(GUEST_CR0, cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1644 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1645 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1646 | if (is_guest_mode(vcpu)) |
| 1647 | vcpu->arch.cr0_guest_owned_bits &= |
| 1648 | ~get_vmcs12(vcpu)->cr0_guest_host_mask; |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1649 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1650 | } |
| 1651 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1652 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 1653 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1654 | /* |
| 1655 | * Return the cr0 value that a nested guest would read. This is a combination |
| 1656 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 1657 | * its hypervisor (cr0_read_shadow). |
| 1658 | */ |
| 1659 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 1660 | { |
| 1661 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 1662 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 1663 | } |
| 1664 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 1665 | { |
| 1666 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 1667 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 1668 | } |
| 1669 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1670 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 1671 | { |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1672 | /* Note that there is no vcpu->fpu_active = 0 here. The caller must |
| 1673 | * set this *before* calling this function. |
| 1674 | */ |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1675 | vmx_decache_cr0_guest_bits(vcpu); |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1676 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1677 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1678 | vcpu->arch.cr0_guest_owned_bits = 0; |
| 1679 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1680 | if (is_guest_mode(vcpu)) { |
| 1681 | /* |
| 1682 | * L1's specified read shadow might not contain the TS bit, |
| 1683 | * so now that we turned on shadowing of this bit, we need to |
| 1684 | * set this bit of the shadow. Like in nested_vmx_run we need |
| 1685 | * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet |
| 1686 | * up-to-date here because we just decached cr0.TS (and we'll |
| 1687 | * only update vmcs12->guest_cr0 on nested exit). |
| 1688 | */ |
| 1689 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1690 | vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) | |
| 1691 | (vcpu->arch.cr0 & X86_CR0_TS); |
| 1692 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 1693 | } else |
| 1694 | vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1695 | } |
| 1696 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1697 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 1698 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1699 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 1700 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1701 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 1702 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 1703 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 1704 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1705 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1706 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 1707 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1708 | } |
| 1709 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1710 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1711 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 1715 | { |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1716 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 1717 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1718 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1719 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1720 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1721 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1722 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 1723 | } |
| 1724 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1725 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 1726 | { |
| 1727 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 1728 | int ret = 0; |
| 1729 | |
| 1730 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1731 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1732 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1733 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1734 | |
| 1735 | return ret & mask; |
| 1736 | } |
| 1737 | |
| 1738 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 1739 | { |
| 1740 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 1741 | u32 interruptibility = interruptibility_old; |
| 1742 | |
| 1743 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 1744 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1745 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1746 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1747 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1748 | interruptibility |= GUEST_INTR_STATE_STI; |
| 1749 | |
| 1750 | if ((interruptibility != interruptibility_old)) |
| 1751 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 1752 | } |
| 1753 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1754 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 1755 | { |
| 1756 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1757 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1758 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1759 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1760 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1761 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1762 | /* skipping an emulated instruction also counts */ |
| 1763 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1764 | } |
| 1765 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 1766 | /* |
| 1767 | * KVM wants to inject page-faults which it got to the guest. This function |
| 1768 | * checks whether in a nested guest, we need to inject them to L1 or L2. |
| 1769 | * This function assumes it is called with the exit reason in vmcs02 being |
| 1770 | * a #PF exception (this is the only case in which KVM injects a #PF when L2 |
| 1771 | * is running). |
| 1772 | */ |
| 1773 | static int nested_pf_handled(struct kvm_vcpu *vcpu) |
| 1774 | { |
| 1775 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1776 | |
| 1777 | /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */ |
Nadav Har'El | 9587190 | 2012-03-06 16:39:22 +0200 | [diff] [blame] | 1778 | if (!(vmcs12->exception_bitmap & (1u << PF_VECTOR))) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 1779 | return 0; |
| 1780 | |
| 1781 | nested_vmx_vmexit(vcpu); |
| 1782 | return 1; |
| 1783 | } |
| 1784 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1785 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 1786 | bool has_error_code, u32 error_code, |
| 1787 | bool reinject) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1788 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1789 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1790 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1791 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 1792 | if (nr == PF_VECTOR && is_guest_mode(vcpu) && |
| 1793 | nested_pf_handled(vcpu)) |
| 1794 | return; |
| 1795 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1796 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1797 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1798 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 1799 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1800 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1801 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 1802 | int inc_eip = 0; |
| 1803 | if (kvm_exception_is_soft(nr)) |
| 1804 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 1805 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 1806 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1807 | return; |
| 1808 | } |
| 1809 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 1810 | if (kvm_exception_is_soft(nr)) { |
| 1811 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 1812 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1813 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 1814 | } else |
| 1815 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 1816 | |
| 1817 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1818 | } |
| 1819 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1820 | static bool vmx_rdtscp_supported(void) |
| 1821 | { |
| 1822 | return cpu_has_vmx_rdtscp(); |
| 1823 | } |
| 1824 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1825 | static bool vmx_invpcid_supported(void) |
| 1826 | { |
| 1827 | return cpu_has_vmx_invpcid() && enable_ept; |
| 1828 | } |
| 1829 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1830 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1831 | * Swap MSR entry in host/guest MSR entry array. |
| 1832 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1833 | static void move_msr_up(struct vcpu_vmx *vmx, int from, int to) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1834 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1835 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1836 | |
| 1837 | tmp = vmx->guest_msrs[to]; |
| 1838 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 1839 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1840 | } |
| 1841 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1842 | static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu) |
| 1843 | { |
| 1844 | unsigned long *msr_bitmap; |
| 1845 | |
| 1846 | if (irqchip_in_kernel(vcpu->kvm) && apic_x2apic_mode(vcpu->arch.apic)) { |
| 1847 | if (is_long_mode(vcpu)) |
| 1848 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic; |
| 1849 | else |
| 1850 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic; |
| 1851 | } else { |
| 1852 | if (is_long_mode(vcpu)) |
| 1853 | msr_bitmap = vmx_msr_bitmap_longmode; |
| 1854 | else |
| 1855 | msr_bitmap = vmx_msr_bitmap_legacy; |
| 1856 | } |
| 1857 | |
| 1858 | vmcs_write64(MSR_BITMAP, __pa(msr_bitmap)); |
| 1859 | } |
| 1860 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1861 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1862 | * Set up the vmcs to automatically save and restore system |
| 1863 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 1864 | * mode, as fiddling with msrs is very expensive. |
| 1865 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1866 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1867 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1868 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1869 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1870 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1871 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1872 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1873 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1874 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1875 | move_msr_up(vmx, index, save_nmsrs++); |
| 1876 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1877 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1878 | move_msr_up(vmx, index, save_nmsrs++); |
| 1879 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1880 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1881 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1882 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
| 1883 | if (index >= 0 && vmx->rdtscp_enabled) |
| 1884 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1885 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1886 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1887 | * if efer.sce is enabled. |
| 1888 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1889 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1890 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1891 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1892 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1893 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1894 | index = __find_msr_index(vmx, MSR_EFER); |
| 1895 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1896 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1897 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1898 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 1899 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1900 | if (cpu_has_vmx_msr_bitmap()) |
| 1901 | vmx_set_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1905 | * reads and returns guest's timestamp counter "register" |
| 1906 | * guest_tsc = host_tsc + tsc_offset -- 21.3 |
| 1907 | */ |
| 1908 | static u64 guest_read_tsc(void) |
| 1909 | { |
| 1910 | u64 host_tsc, tsc_offset; |
| 1911 | |
| 1912 | rdtscll(host_tsc); |
| 1913 | tsc_offset = vmcs_read64(TSC_OFFSET); |
| 1914 | return host_tsc + tsc_offset; |
| 1915 | } |
| 1916 | |
| 1917 | /* |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 1918 | * Like guest_read_tsc, but always returns L1's notion of the timestamp |
| 1919 | * counter, even if a nested guest (L2) is currently running. |
| 1920 | */ |
Marcelo Tosatti | 886b470 | 2012-11-27 23:28:58 -0200 | [diff] [blame] | 1921 | u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 1922 | { |
Marcelo Tosatti | 886b470 | 2012-11-27 23:28:58 -0200 | [diff] [blame] | 1923 | u64 tsc_offset; |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 1924 | |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 1925 | tsc_offset = is_guest_mode(vcpu) ? |
| 1926 | to_vmx(vcpu)->nested.vmcs01_tsc_offset : |
| 1927 | vmcs_read64(TSC_OFFSET); |
| 1928 | return host_tsc + tsc_offset; |
| 1929 | } |
| 1930 | |
| 1931 | /* |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 1932 | * Engage any workarounds for mis-matched TSC rates. Currently limited to |
| 1933 | * software catchup for faster rates on slower CPUs. |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 1934 | */ |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 1935 | static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale) |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 1936 | { |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 1937 | if (!scale) |
| 1938 | return; |
| 1939 | |
| 1940 | if (user_tsc_khz > tsc_khz) { |
| 1941 | vcpu->arch.tsc_catchup = 1; |
| 1942 | vcpu->arch.tsc_always_catchup = 1; |
| 1943 | } else |
| 1944 | WARN(1, "user requested TSC rate below hardware speed\n"); |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 1945 | } |
| 1946 | |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 1947 | static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu) |
| 1948 | { |
| 1949 | return vmcs_read64(TSC_OFFSET); |
| 1950 | } |
| 1951 | |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 1952 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1953 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1954 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1955 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1956 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 1957 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 1958 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 1959 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 1960 | * to the spec, this should set L1's TSC; The offset that L1 |
| 1961 | * set for L2 remains unchanged, and still needs to be added |
| 1962 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 1963 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 1964 | struct vmcs12 *vmcs12; |
| 1965 | to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset; |
| 1966 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 1967 | vmcs12 = get_vmcs12(vcpu); |
| 1968 | vmcs_write64(TSC_OFFSET, offset + |
| 1969 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 1970 | vmcs12->tsc_offset : 0)); |
| 1971 | } else { |
| 1972 | vmcs_write64(TSC_OFFSET, offset); |
| 1973 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1974 | } |
| 1975 | |
Marcelo Tosatti | f1e2b26 | 2012-02-03 15:43:55 -0200 | [diff] [blame] | 1976 | static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host) |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 1977 | { |
| 1978 | u64 offset = vmcs_read64(TSC_OFFSET); |
| 1979 | vmcs_write64(TSC_OFFSET, offset + adjustment); |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 1980 | if (is_guest_mode(vcpu)) { |
| 1981 | /* Even when running L2, the adjustment needs to apply to L1 */ |
| 1982 | to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment; |
| 1983 | } |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 1984 | } |
| 1985 | |
Joerg Roedel | 857e409 | 2011-03-25 09:44:50 +0100 | [diff] [blame] | 1986 | static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) |
| 1987 | { |
| 1988 | return target_tsc - native_read_tsc(); |
| 1989 | } |
| 1990 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 1991 | static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu) |
| 1992 | { |
| 1993 | struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 1994 | return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31))); |
| 1995 | } |
| 1996 | |
| 1997 | /* |
| 1998 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 1999 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 2000 | * all guests if the "nested" module option is off, and can also be disabled |
| 2001 | * for a single guest by disabling its VMX cpuid bit. |
| 2002 | */ |
| 2003 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 2004 | { |
| 2005 | return nested && guest_cpuid_has_vmx(vcpu); |
| 2006 | } |
| 2007 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2008 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2009 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 2010 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 2011 | * The same values should also be used to verify that vmcs12 control fields are |
| 2012 | * valid during nested entry from L1 to L2. |
| 2013 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 2014 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 2015 | * bit in the high half is on if the corresponding bit in the control field |
| 2016 | * may be on. See also vmx_control_verify(). |
| 2017 | * TODO: allow these variables to be modified (downgraded) by module options |
| 2018 | * or other means. |
| 2019 | */ |
| 2020 | static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high; |
| 2021 | static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high; |
| 2022 | static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high; |
| 2023 | static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high; |
| 2024 | static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high; |
| 2025 | static __init void nested_vmx_setup_ctls_msrs(void) |
| 2026 | { |
| 2027 | /* |
| 2028 | * Note that as a general rule, the high half of the MSRs (bits in |
| 2029 | * the control fields which may be 1) should be initialized by the |
| 2030 | * intersection of the underlying hardware's MSR (i.e., features which |
| 2031 | * can be supported) and the list of features we want to expose - |
| 2032 | * because they are known to be properly supported in our code. |
| 2033 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 2034 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 2035 | * reason is that if one of these bits is necessary, it will appear |
| 2036 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 2037 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
| 2038 | * nested_vmx_exit_handled() will not pass related exits to L1. |
| 2039 | * These rules have exceptions below. |
| 2040 | */ |
| 2041 | |
| 2042 | /* pin-based controls */ |
| 2043 | /* |
| 2044 | * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is |
| 2045 | * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR. |
| 2046 | */ |
| 2047 | nested_vmx_pinbased_ctls_low = 0x16 ; |
| 2048 | nested_vmx_pinbased_ctls_high = 0x16 | |
| 2049 | PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING | |
| 2050 | PIN_BASED_VIRTUAL_NMIS; |
| 2051 | |
| 2052 | /* exit controls */ |
| 2053 | nested_vmx_exit_ctls_low = 0; |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 2054 | /* Note that guest use of VM_EXIT_ACK_INTR_ON_EXIT is not supported. */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2055 | #ifdef CONFIG_X86_64 |
| 2056 | nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 2057 | #else |
| 2058 | nested_vmx_exit_ctls_high = 0; |
| 2059 | #endif |
| 2060 | |
| 2061 | /* entry controls */ |
| 2062 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
| 2063 | nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high); |
| 2064 | nested_vmx_entry_ctls_low = 0; |
| 2065 | nested_vmx_entry_ctls_high &= |
| 2066 | VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE; |
| 2067 | |
| 2068 | /* cpu-based controls */ |
| 2069 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
| 2070 | nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high); |
| 2071 | nested_vmx_procbased_ctls_low = 0; |
| 2072 | nested_vmx_procbased_ctls_high &= |
| 2073 | CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
| 2074 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 2075 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 2076 | CPU_BASED_CR3_STORE_EXITING | |
| 2077 | #ifdef CONFIG_X86_64 |
| 2078 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 2079 | #endif |
| 2080 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
| 2081 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | dbcb4e7 | 2012-08-13 15:38:22 +0300 | [diff] [blame] | 2082 | CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2083 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
| 2084 | /* |
| 2085 | * We can allow some features even when not supported by the |
| 2086 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 2087 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 2088 | * without MSR bitmaps. |
| 2089 | */ |
| 2090 | nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS; |
| 2091 | |
| 2092 | /* secondary cpu-based controls */ |
| 2093 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
| 2094 | nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high); |
| 2095 | nested_vmx_secondary_ctls_low = 0; |
| 2096 | nested_vmx_secondary_ctls_high &= |
| 2097 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 2098 | } |
| 2099 | |
| 2100 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 2101 | { |
| 2102 | /* |
| 2103 | * Bits 0 in high must be 0, and bits 1 in low must be 1. |
| 2104 | */ |
| 2105 | return ((control & high) | low) == control; |
| 2106 | } |
| 2107 | |
| 2108 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 2109 | { |
| 2110 | return low | ((u64)high << 32); |
| 2111 | } |
| 2112 | |
| 2113 | /* |
| 2114 | * If we allow our guest to use VMX instructions (i.e., nested VMX), we should |
| 2115 | * also let it use VMX-specific MSRs. |
| 2116 | * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a |
| 2117 | * VMX-specific MSR, or 0 when we haven't (and the caller should handle it |
| 2118 | * like all other MSRs). |
| 2119 | */ |
| 2120 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 2121 | { |
| 2122 | if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC && |
| 2123 | msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) { |
| 2124 | /* |
| 2125 | * According to the spec, processors which do not support VMX |
| 2126 | * should throw a #GP(0) when VMX capability MSRs are read. |
| 2127 | */ |
| 2128 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 2129 | return 1; |
| 2130 | } |
| 2131 | |
| 2132 | switch (msr_index) { |
| 2133 | case MSR_IA32_FEATURE_CONTROL: |
| 2134 | *pdata = 0; |
| 2135 | break; |
| 2136 | case MSR_IA32_VMX_BASIC: |
| 2137 | /* |
| 2138 | * This MSR reports some information about VMX support. We |
| 2139 | * should return information about the VMX we emulate for the |
| 2140 | * guest, and the VMCS structure we give it - not about the |
| 2141 | * VMX support of the underlying hardware. |
| 2142 | */ |
| 2143 | *pdata = VMCS12_REVISION | |
| 2144 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 2145 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 2146 | break; |
| 2147 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 2148 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 2149 | *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low, |
| 2150 | nested_vmx_pinbased_ctls_high); |
| 2151 | break; |
| 2152 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 2153 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 2154 | *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low, |
| 2155 | nested_vmx_procbased_ctls_high); |
| 2156 | break; |
| 2157 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 2158 | case MSR_IA32_VMX_EXIT_CTLS: |
| 2159 | *pdata = vmx_control_msr(nested_vmx_exit_ctls_low, |
| 2160 | nested_vmx_exit_ctls_high); |
| 2161 | break; |
| 2162 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 2163 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 2164 | *pdata = vmx_control_msr(nested_vmx_entry_ctls_low, |
| 2165 | nested_vmx_entry_ctls_high); |
| 2166 | break; |
| 2167 | case MSR_IA32_VMX_MISC: |
| 2168 | *pdata = 0; |
| 2169 | break; |
| 2170 | /* |
| 2171 | * These MSRs specify bits which the guest must keep fixed (on or off) |
| 2172 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 2173 | * We picked the standard core2 setting. |
| 2174 | */ |
| 2175 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 2176 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 2177 | case MSR_IA32_VMX_CR0_FIXED0: |
| 2178 | *pdata = VMXON_CR0_ALWAYSON; |
| 2179 | break; |
| 2180 | case MSR_IA32_VMX_CR0_FIXED1: |
| 2181 | *pdata = -1ULL; |
| 2182 | break; |
| 2183 | case MSR_IA32_VMX_CR4_FIXED0: |
| 2184 | *pdata = VMXON_CR4_ALWAYSON; |
| 2185 | break; |
| 2186 | case MSR_IA32_VMX_CR4_FIXED1: |
| 2187 | *pdata = -1ULL; |
| 2188 | break; |
| 2189 | case MSR_IA32_VMX_VMCS_ENUM: |
| 2190 | *pdata = 0x1f; |
| 2191 | break; |
| 2192 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 2193 | *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low, |
| 2194 | nested_vmx_secondary_ctls_high); |
| 2195 | break; |
| 2196 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 2197 | /* Currently, no nested ept or nested vpid */ |
| 2198 | *pdata = 0; |
| 2199 | break; |
| 2200 | default: |
| 2201 | return 0; |
| 2202 | } |
| 2203 | |
| 2204 | return 1; |
| 2205 | } |
| 2206 | |
| 2207 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 2208 | { |
| 2209 | if (!nested_vmx_allowed(vcpu)) |
| 2210 | return 0; |
| 2211 | |
| 2212 | if (msr_index == MSR_IA32_FEATURE_CONTROL) |
| 2213 | /* TODO: the right thing. */ |
| 2214 | return 1; |
| 2215 | /* |
| 2216 | * No need to treat VMX capability MSRs specially: If we don't handle |
| 2217 | * them, handle_wrmsr will #GP(0), which is correct (they are readonly) |
| 2218 | */ |
| 2219 | return 0; |
| 2220 | } |
| 2221 | |
| 2222 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2223 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 2224 | * Returns 0 on success, non-0 otherwise. |
| 2225 | * Assumes vcpu_load() was already called. |
| 2226 | */ |
| 2227 | static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 2228 | { |
| 2229 | u64 data; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2230 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2231 | |
| 2232 | if (!pdata) { |
| 2233 | printk(KERN_ERR "BUG: get_msr called with NULL pdata\n"); |
| 2234 | return -EINVAL; |
| 2235 | } |
| 2236 | |
| 2237 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2238 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2239 | case MSR_FS_BASE: |
| 2240 | data = vmcs_readl(GUEST_FS_BASE); |
| 2241 | break; |
| 2242 | case MSR_GS_BASE: |
| 2243 | data = vmcs_readl(GUEST_GS_BASE); |
| 2244 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2245 | case MSR_KERNEL_GS_BASE: |
| 2246 | vmx_load_host_state(to_vmx(vcpu)); |
| 2247 | data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
| 2248 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2249 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2250 | case MSR_EFER: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2251 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2252 | case MSR_IA32_TSC: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2253 | data = guest_read_tsc(); |
| 2254 | break; |
| 2255 | case MSR_IA32_SYSENTER_CS: |
| 2256 | data = vmcs_read32(GUEST_SYSENTER_CS); |
| 2257 | break; |
| 2258 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2259 | data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2260 | break; |
| 2261 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2262 | data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2263 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2264 | case MSR_TSC_AUX: |
| 2265 | if (!to_vmx(vcpu)->rdtscp_enabled) |
| 2266 | return 1; |
| 2267 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2268 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2269 | if (vmx_get_vmx_msr(vcpu, msr_index, pdata)) |
| 2270 | return 0; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2271 | msr = find_msr_entry(to_vmx(vcpu), msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2272 | if (msr) { |
| 2273 | data = msr->data; |
| 2274 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2275 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2276 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | *pdata = data; |
| 2280 | return 0; |
| 2281 | } |
| 2282 | |
| 2283 | /* |
| 2284 | * Writes msr value into into the appropriate "register". |
| 2285 | * Returns 0 on success, non-0 otherwise. |
| 2286 | * Assumes vcpu_load() was already called. |
| 2287 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2288 | static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2289 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2290 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2291 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2292 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2293 | u32 msr_index = msr_info->index; |
| 2294 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2295 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2296 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2297 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2298 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2299 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 2300 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2301 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2302 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2303 | vmcs_writel(GUEST_FS_BASE, data); |
| 2304 | break; |
| 2305 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2306 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2307 | vmcs_writel(GUEST_GS_BASE, data); |
| 2308 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2309 | case MSR_KERNEL_GS_BASE: |
| 2310 | vmx_load_host_state(vmx); |
| 2311 | vmx->msr_guest_kernel_gs_base = data; |
| 2312 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2313 | #endif |
| 2314 | case MSR_IA32_SYSENTER_CS: |
| 2315 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 2316 | break; |
| 2317 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2318 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2319 | break; |
| 2320 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2321 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2322 | break; |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2323 | case MSR_IA32_TSC: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2324 | kvm_write_tsc(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2325 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2326 | case MSR_IA32_CR_PAT: |
| 2327 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 2328 | vmcs_write64(GUEST_IA32_PAT, data); |
| 2329 | vcpu->arch.pat = data; |
| 2330 | break; |
| 2331 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2332 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2333 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 2334 | case MSR_IA32_TSC_ADJUST: |
| 2335 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2336 | break; |
| 2337 | case MSR_TSC_AUX: |
| 2338 | if (!vmx->rdtscp_enabled) |
| 2339 | return 1; |
| 2340 | /* Check reserved bit, higher 32 bits should be zero */ |
| 2341 | if ((data >> 32) != 0) |
| 2342 | return 1; |
| 2343 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2344 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2345 | if (vmx_set_vmx_msr(vcpu, msr_index, data)) |
| 2346 | break; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2347 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2348 | if (msr) { |
| 2349 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 2350 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 2351 | preempt_disable(); |
Avi Kivity | 9ee7397 | 2012-03-06 14:16:33 +0200 | [diff] [blame] | 2352 | kvm_set_shared_msr(msr->index, msr->data, |
| 2353 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 2354 | preempt_enable(); |
| 2355 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2356 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2357 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2358 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2359 | } |
| 2360 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2361 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2362 | } |
| 2363 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2364 | static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2365 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2366 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 2367 | switch (reg) { |
| 2368 | case VCPU_REGS_RSP: |
| 2369 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 2370 | break; |
| 2371 | case VCPU_REGS_RIP: |
| 2372 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 2373 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2374 | case VCPU_EXREG_PDPTR: |
| 2375 | if (enable_ept) |
| 2376 | ept_save_pdptrs(vcpu); |
| 2377 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2378 | default: |
| 2379 | break; |
| 2380 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2381 | } |
| 2382 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2383 | static __init int cpu_has_kvm_support(void) |
| 2384 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 2385 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | static __init int vmx_disabled_by_bios(void) |
| 2389 | { |
| 2390 | u64 msr; |
| 2391 | |
| 2392 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2393 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2394 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2395 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 2396 | && tboot_enabled()) |
| 2397 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2398 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2399 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2400 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 2401 | && !tboot_enabled()) { |
| 2402 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2403 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2404 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 2405 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2406 | /* launched w/o TXT and VMX disabled */ |
| 2407 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 2408 | && !tboot_enabled()) |
| 2409 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2413 | } |
| 2414 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2415 | static void kvm_cpu_vmxon(u64 addr) |
| 2416 | { |
| 2417 | asm volatile (ASM_VMX_VMXON_RAX |
| 2418 | : : "a"(&addr), "m"(addr) |
| 2419 | : "memory", "cc"); |
| 2420 | } |
| 2421 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2422 | static int hardware_enable(void *garbage) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2423 | { |
| 2424 | int cpu = raw_smp_processor_id(); |
| 2425 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2426 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2427 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2428 | if (read_cr4() & X86_CR4_VMXE) |
| 2429 | return -EBUSY; |
| 2430 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2431 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2432 | |
| 2433 | /* |
| 2434 | * Now we can enable the vmclear operation in kdump |
| 2435 | * since the loaded_vmcss_on_cpu list on this cpu |
| 2436 | * has been initialized. |
| 2437 | * |
| 2438 | * Though the cpu is not in VMX operation now, there |
| 2439 | * is no problem to enable the vmclear operation |
| 2440 | * for the loaded_vmcss_on_cpu list is empty! |
| 2441 | */ |
| 2442 | crash_enable_local_vmclear(cpu); |
| 2443 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2444 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2445 | |
| 2446 | test_bits = FEATURE_CONTROL_LOCKED; |
| 2447 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 2448 | if (tboot_enabled()) |
| 2449 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 2450 | |
| 2451 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2452 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2453 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 2454 | } |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2455 | write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */ |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2456 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2457 | if (vmm_exclusive) { |
| 2458 | kvm_cpu_vmxon(phys_addr); |
| 2459 | ept_sync_global(); |
| 2460 | } |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2461 | |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 2462 | store_gdt(&__get_cpu_var(host_gdt)); |
| 2463 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2464 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2465 | } |
| 2466 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2467 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2468 | { |
| 2469 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2470 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2471 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2472 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 2473 | loaded_vmcss_on_cpu_link) |
| 2474 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2475 | } |
| 2476 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 2477 | |
| 2478 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 2479 | * tricks. |
| 2480 | */ |
| 2481 | static void kvm_cpu_vmxoff(void) |
| 2482 | { |
| 2483 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 2484 | } |
| 2485 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2486 | static void hardware_disable(void *garbage) |
| 2487 | { |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2488 | if (vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2489 | vmclear_local_loaded_vmcss(); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2490 | kvm_cpu_vmxoff(); |
| 2491 | } |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2492 | write_cr4(read_cr4() & ~X86_CR4_VMXE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2493 | } |
| 2494 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2495 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2496 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2497 | { |
| 2498 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2499 | u32 ctl = ctl_min | ctl_opt; |
| 2500 | |
| 2501 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 2502 | |
| 2503 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 2504 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 2505 | |
| 2506 | /* Ensure minimum (required) set of control bits are supported. */ |
| 2507 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2508 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2509 | |
| 2510 | *result = ctl; |
| 2511 | return 0; |
| 2512 | } |
| 2513 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2514 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 2515 | { |
| 2516 | u32 vmx_msr_low, vmx_msr_high; |
| 2517 | |
| 2518 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 2519 | return vmx_msr_high & ctl; |
| 2520 | } |
| 2521 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2522 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2523 | { |
| 2524 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2525 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2526 | u32 _pin_based_exec_control = 0; |
| 2527 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2528 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2529 | u32 _vmexit_control = 0; |
| 2530 | u32 _vmentry_control = 0; |
| 2531 | |
| 2532 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2533 | opt = PIN_BASED_VIRTUAL_NMIS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2534 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 2535 | &_pin_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2536 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2537 | |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 2538 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2539 | #ifdef CONFIG_X86_64 |
| 2540 | CPU_BASED_CR8_LOAD_EXITING | |
| 2541 | CPU_BASED_CR8_STORE_EXITING | |
| 2542 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2543 | CPU_BASED_CR3_LOAD_EXITING | |
| 2544 | CPU_BASED_CR3_STORE_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2545 | CPU_BASED_USE_IO_BITMAPS | |
| 2546 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2547 | CPU_BASED_USE_TSC_OFFSETING | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 2548 | CPU_BASED_MWAIT_EXITING | |
| 2549 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 2550 | CPU_BASED_INVLPG_EXITING | |
| 2551 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 2552 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2553 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2554 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2555 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2556 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 2557 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2558 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2559 | #ifdef CONFIG_X86_64 |
| 2560 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 2561 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 2562 | ~CPU_BASED_CR8_STORE_EXITING; |
| 2563 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2564 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2565 | min2 = 0; |
| 2566 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2567 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2568 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2569 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2570 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2571 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2572 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 2573 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 2574 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 2575 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 2576 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2577 | if (adjust_vmx_controls(min2, opt2, |
| 2578 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2579 | &_cpu_based_2nd_exec_control) < 0) |
| 2580 | return -EIO; |
| 2581 | } |
| 2582 | #ifndef CONFIG_X86_64 |
| 2583 | if (!(_cpu_based_2nd_exec_control & |
| 2584 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 2585 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2586 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 2587 | |
| 2588 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 2589 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2590 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 2591 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 2592 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 2593 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2594 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2595 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 2596 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 2597 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 2598 | CPU_BASED_CR3_STORE_EXITING | |
| 2599 | CPU_BASED_INVLPG_EXITING); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2600 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
| 2601 | vmx_capability.ept, vmx_capability.vpid); |
| 2602 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2603 | |
| 2604 | min = 0; |
| 2605 | #ifdef CONFIG_X86_64 |
| 2606 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 2607 | #endif |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2608 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2609 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 2610 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2611 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2612 | |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2613 | min = 0; |
| 2614 | opt = VM_ENTRY_LOAD_IA32_PAT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2615 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 2616 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2617 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2618 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 2619 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2620 | |
| 2621 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 2622 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2623 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2624 | |
| 2625 | #ifdef CONFIG_X86_64 |
| 2626 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 2627 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2628 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2629 | #endif |
| 2630 | |
| 2631 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 2632 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2633 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2634 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2635 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
| 2636 | vmcs_conf->order = get_order(vmcs_config.size); |
| 2637 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2638 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2639 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 2640 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2641 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2642 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 2643 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2644 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2645 | cpu_has_load_ia32_efer = |
| 2646 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 2647 | VM_ENTRY_LOAD_IA32_EFER) |
| 2648 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 2649 | VM_EXIT_LOAD_IA32_EFER); |
| 2650 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2651 | cpu_has_load_perf_global_ctrl = |
| 2652 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 2653 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 2654 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 2655 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 2656 | |
| 2657 | /* |
| 2658 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
| 2659 | * but due to arrata below it can't be used. Workaround is to use |
| 2660 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 2661 | * |
| 2662 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 2663 | * |
| 2664 | * AAK155 (model 26) |
| 2665 | * AAP115 (model 30) |
| 2666 | * AAT100 (model 37) |
| 2667 | * BC86,AAY89,BD102 (model 44) |
| 2668 | * BA97 (model 46) |
| 2669 | * |
| 2670 | */ |
| 2671 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 2672 | switch (boot_cpu_data.x86_model) { |
| 2673 | case 26: |
| 2674 | case 30: |
| 2675 | case 37: |
| 2676 | case 44: |
| 2677 | case 46: |
| 2678 | cpu_has_load_perf_global_ctrl = false; |
| 2679 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 2680 | "does not work properly. Using workaround\n"); |
| 2681 | break; |
| 2682 | default: |
| 2683 | break; |
| 2684 | } |
| 2685 | } |
| 2686 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2687 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 2688 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2689 | |
| 2690 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 2691 | { |
| 2692 | int node = cpu_to_node(cpu); |
| 2693 | struct page *pages; |
| 2694 | struct vmcs *vmcs; |
| 2695 | |
Mel Gorman | 6484eb3 | 2009-06-16 15:31:54 -0700 | [diff] [blame] | 2696 | pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2697 | if (!pages) |
| 2698 | return NULL; |
| 2699 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2700 | memset(vmcs, 0, vmcs_config.size); |
| 2701 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2702 | return vmcs; |
| 2703 | } |
| 2704 | |
| 2705 | static struct vmcs *alloc_vmcs(void) |
| 2706 | { |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2707 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | static void free_vmcs(struct vmcs *vmcs) |
| 2711 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2712 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2713 | } |
| 2714 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2715 | /* |
| 2716 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 2717 | */ |
| 2718 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 2719 | { |
| 2720 | if (!loaded_vmcs->vmcs) |
| 2721 | return; |
| 2722 | loaded_vmcs_clear(loaded_vmcs); |
| 2723 | free_vmcs(loaded_vmcs->vmcs); |
| 2724 | loaded_vmcs->vmcs = NULL; |
| 2725 | } |
| 2726 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 2727 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2728 | { |
| 2729 | int cpu; |
| 2730 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2731 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2732 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2733 | per_cpu(vmxarea, cpu) = NULL; |
| 2734 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2735 | } |
| 2736 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2737 | static __init int alloc_kvm_area(void) |
| 2738 | { |
| 2739 | int cpu; |
| 2740 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2741 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2742 | struct vmcs *vmcs; |
| 2743 | |
| 2744 | vmcs = alloc_vmcs_cpu(cpu); |
| 2745 | if (!vmcs) { |
| 2746 | free_kvm_area(); |
| 2747 | return -ENOMEM; |
| 2748 | } |
| 2749 | |
| 2750 | per_cpu(vmxarea, cpu) = vmcs; |
| 2751 | } |
| 2752 | return 0; |
| 2753 | } |
| 2754 | |
| 2755 | static __init int hardware_setup(void) |
| 2756 | { |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2757 | if (setup_vmcs_config(&vmcs_config) < 0) |
| 2758 | return -EIO; |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 2759 | |
| 2760 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 2761 | kvm_enable_efer_bits(EFER_NX); |
| 2762 | |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 2763 | if (!cpu_has_vmx_vpid()) |
| 2764 | enable_vpid = 0; |
| 2765 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 2766 | if (!cpu_has_vmx_ept() || |
| 2767 | !cpu_has_vmx_ept_4levels()) { |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 2768 | enable_ept = 0; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2769 | enable_unrestricted_guest = 0; |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 2770 | enable_ept_ad_bits = 0; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2771 | } |
| 2772 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 2773 | if (!cpu_has_vmx_ept_ad_bits()) |
| 2774 | enable_ept_ad_bits = 0; |
| 2775 | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2776 | if (!cpu_has_vmx_unrestricted_guest()) |
| 2777 | enable_unrestricted_guest = 0; |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 2778 | |
| 2779 | if (!cpu_has_vmx_flexpriority()) |
| 2780 | flexpriority_enabled = 0; |
| 2781 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2782 | if (!cpu_has_vmx_tpr_shadow()) |
| 2783 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 2784 | |
Marcelo Tosatti | 54dee99 | 2009-06-11 12:07:44 -0300 | [diff] [blame] | 2785 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 2786 | kvm_disable_largepages(); |
| 2787 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2788 | if (!cpu_has_vmx_ple()) |
| 2789 | ple_gap = 0; |
| 2790 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 2791 | if (!cpu_has_vmx_apic_register_virt() || |
| 2792 | !cpu_has_vmx_virtual_intr_delivery()) |
| 2793 | enable_apicv_reg_vid = 0; |
| 2794 | |
| 2795 | if (enable_apicv_reg_vid) |
| 2796 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 2797 | else |
| 2798 | kvm_x86_ops->hwapic_irr_update = NULL; |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 2799 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2800 | if (nested) |
| 2801 | nested_vmx_setup_ctls_msrs(); |
| 2802 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2803 | return alloc_kvm_area(); |
| 2804 | } |
| 2805 | |
| 2806 | static __exit void hardware_unsetup(void) |
| 2807 | { |
| 2808 | free_kvm_area(); |
| 2809 | } |
| 2810 | |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 2811 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 2812 | { |
| 2813 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 2814 | } |
| 2815 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 2816 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2817 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2818 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2819 | if (!emulate_invalid_guest_state) { |
| 2820 | /* |
| 2821 | * CS and SS RPL should be equal during guest entry according |
| 2822 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 2823 | * is in the middle of the transition from real mode to |
| 2824 | * protected mode it is safe to assume that RPL 0 is a good |
| 2825 | * default value. |
| 2826 | */ |
| 2827 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
| 2828 | save->selector &= ~SELECTOR_RPL_MASK; |
| 2829 | save->dpl = save->selector & SELECTOR_RPL_MASK; |
| 2830 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2831 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2832 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 2836 | { |
| 2837 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2838 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2839 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2840 | /* |
| 2841 | * Update real mode segment cache. It may be not up-to-date if sement |
| 2842 | * register was written while vcpu was in a guest mode. |
| 2843 | */ |
| 2844 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 2845 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 2846 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 2847 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 2848 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 2849 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 2850 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2851 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2852 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2853 | vmx_segment_cache_clear(vmx); |
| 2854 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2855 | vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2856 | |
| 2857 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2858 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2859 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2860 | vmcs_writel(GUEST_RFLAGS, flags); |
| 2861 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2862 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 2863 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2864 | |
| 2865 | update_exception_bitmap(vcpu); |
| 2866 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 2867 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 2868 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 2869 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 2870 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 2871 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 2872 | fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
Gleb Natapov | 1f3141e | 2013-01-21 15:36:41 +0200 | [diff] [blame] | 2873 | |
| 2874 | /* CPL is always 0 when CPU enters protected mode */ |
| 2875 | __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
| 2876 | vmx->cpl = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2877 | } |
| 2878 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2879 | static gva_t rmode_tss_base(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2880 | { |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2881 | if (!kvm->arch.tss_addr) { |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 2882 | struct kvm_memslots *slots; |
Xiao Guangrong | 28a3754 | 2011-11-24 19:04:35 +0800 | [diff] [blame] | 2883 | struct kvm_memory_slot *slot; |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 2884 | gfn_t base_gfn; |
| 2885 | |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 2886 | slots = kvm_memslots(kvm); |
Xiao Guangrong | 28a3754 | 2011-11-24 19:04:35 +0800 | [diff] [blame] | 2887 | slot = id_to_memslot(slots, 0); |
| 2888 | base_gfn = slot->base_gfn + slot->npages - 3; |
| 2889 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2890 | return base_gfn << PAGE_SHIFT; |
| 2891 | } |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2892 | return kvm->arch.tss_addr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2893 | } |
| 2894 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2895 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2896 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 2897 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2898 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2899 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2900 | var.dpl = 0x3; |
| 2901 | if (seg == VCPU_SREG_CS) |
| 2902 | var.type = 0x3; |
| 2903 | |
| 2904 | if (!emulate_invalid_guest_state) { |
| 2905 | var.selector = var.base >> 4; |
| 2906 | var.base = var.base & 0xffff0; |
| 2907 | var.limit = 0xffff; |
| 2908 | var.g = 0; |
| 2909 | var.db = 0; |
| 2910 | var.present = 1; |
| 2911 | var.s = 1; |
| 2912 | var.l = 0; |
| 2913 | var.unusable = 0; |
| 2914 | var.type = 0x3; |
| 2915 | var.avl = 0; |
| 2916 | if (save->base & 0xf) |
| 2917 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 2918 | "paragraph aligned when entering " |
| 2919 | "protected mode (seg=%d)", seg); |
| 2920 | } |
| 2921 | |
| 2922 | vmcs_write16(sf->selector, var.selector); |
| 2923 | vmcs_write32(sf->base, var.base); |
| 2924 | vmcs_write32(sf->limit, var.limit); |
| 2925 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2926 | } |
| 2927 | |
| 2928 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 2929 | { |
| 2930 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2931 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2932 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2933 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 2934 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 2935 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 2936 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 2937 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 2938 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 2939 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2940 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2941 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2942 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 2943 | /* |
| 2944 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
| 2945 | * vcpu. Call it here with phys address pointing 16M below 4G. |
| 2946 | */ |
| 2947 | if (!vcpu->kvm->arch.tss_addr) { |
| 2948 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 2949 | "called before entering vcpu\n"); |
| 2950 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
| 2951 | vmx_set_tss_addr(vcpu->kvm, 0xfeffd000); |
| 2952 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 2953 | } |
| 2954 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2955 | vmx_segment_cache_clear(vmx); |
| 2956 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2957 | vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2958 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2959 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 2960 | |
| 2961 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2962 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2963 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2964 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2965 | |
| 2966 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2967 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2968 | update_exception_bitmap(vcpu); |
| 2969 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2970 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 2971 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 2972 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 2973 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 2974 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 2975 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2976 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 2977 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2978 | } |
| 2979 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2980 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 2981 | { |
| 2982 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2983 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 2984 | |
| 2985 | if (!msr) |
| 2986 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2987 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2988 | /* |
| 2989 | * Force kernel_gs_base reloading before EFER changes, as control |
| 2990 | * of this msr depends on is_long_mode(). |
| 2991 | */ |
| 2992 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2993 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2994 | if (efer & EFER_LMA) { |
| 2995 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 2996 | vmcs_read32(VM_ENTRY_CONTROLS) | |
| 2997 | VM_ENTRY_IA32E_MODE); |
| 2998 | msr->data = efer; |
| 2999 | } else { |
| 3000 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 3001 | vmcs_read32(VM_ENTRY_CONTROLS) & |
| 3002 | ~VM_ENTRY_IA32E_MODE); |
| 3003 | |
| 3004 | msr->data = efer & ~EFER_LME; |
| 3005 | } |
| 3006 | setup_msrs(vmx); |
| 3007 | } |
| 3008 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3009 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3010 | |
| 3011 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 3012 | { |
| 3013 | u32 guest_tr_ar; |
| 3014 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3015 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 3016 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3017 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
| 3018 | if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 3019 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 3020 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3021 | vmcs_write32(GUEST_TR_AR_BYTES, |
| 3022 | (guest_tr_ar & ~AR_TYPE_MASK) |
| 3023 | | AR_TYPE_BUSY_64_TSS); |
| 3024 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 3025 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 3029 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3030 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 3031 | vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 3032 | & ~VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 3033 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | #endif |
| 3037 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3038 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 3039 | { |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 3040 | vpid_sync_context(to_vmx(vcpu)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 3041 | if (enable_ept) { |
| 3042 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 3043 | return; |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 3044 | ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 3045 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3046 | } |
| 3047 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 3048 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 3049 | { |
| 3050 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 3051 | |
| 3052 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 3053 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 3054 | } |
| 3055 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 3056 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 3057 | { |
| 3058 | if (enable_ept && is_paging(vcpu)) |
| 3059 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 3060 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 3061 | } |
| 3062 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 3063 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 3064 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3065 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 3066 | |
| 3067 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 3068 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 3069 | } |
| 3070 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3071 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 3072 | { |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3073 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 3074 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 3075 | return; |
| 3076 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3077 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 3078 | vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]); |
| 3079 | vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]); |
| 3080 | vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]); |
| 3081 | vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3082 | } |
| 3083 | } |
| 3084 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3085 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 3086 | { |
| 3087 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 3088 | vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 3089 | vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 3090 | vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 3091 | vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3092 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3093 | |
| 3094 | __set_bit(VCPU_EXREG_PDPTR, |
| 3095 | (unsigned long *)&vcpu->arch.regs_avail); |
| 3096 | __set_bit(VCPU_EXREG_PDPTR, |
| 3097 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3098 | } |
| 3099 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3100 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3101 | |
| 3102 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 3103 | unsigned long cr0, |
| 3104 | struct kvm_vcpu *vcpu) |
| 3105 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 3106 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 3107 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3108 | if (!(cr0 & X86_CR0_PG)) { |
| 3109 | /* From paging/starting to nonpaging */ |
| 3110 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 3111 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3112 | (CPU_BASED_CR3_LOAD_EXITING | |
| 3113 | CPU_BASED_CR3_STORE_EXITING)); |
| 3114 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3115 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3116 | } else if (!is_paging(vcpu)) { |
| 3117 | /* From nonpaging to paging */ |
| 3118 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 3119 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3120 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 3121 | CPU_BASED_CR3_STORE_EXITING)); |
| 3122 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3123 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3124 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 3125 | |
| 3126 | if (!(cr0 & X86_CR0_WP)) |
| 3127 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3128 | } |
| 3129 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3130 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 3131 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3132 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3133 | unsigned long hw_cr0; |
| 3134 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 3135 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3136 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 3137 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 3138 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 3139 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3140 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 3141 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 3142 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3143 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 3144 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 3145 | enter_rmode(vcpu); |
| 3146 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3147 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3148 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3149 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 3150 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3151 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 3152 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3153 | exit_lmode(vcpu); |
| 3154 | } |
| 3155 | #endif |
| 3156 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3157 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3158 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 3159 | |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3160 | if (!vcpu->fpu_active) |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 3161 | hw_cr0 |= X86_CR0_TS | X86_CR0_MP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3162 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3163 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3164 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3165 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 3166 | |
| 3167 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 3168 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3169 | } |
| 3170 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3171 | static u64 construct_eptp(unsigned long root_hpa) |
| 3172 | { |
| 3173 | u64 eptp; |
| 3174 | |
| 3175 | /* TODO write the value reading from MSR */ |
| 3176 | eptp = VMX_EPT_DEFAULT_MT | |
| 3177 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; |
Xudong Hao | b38f993 | 2012-05-28 19:33:36 +0800 | [diff] [blame] | 3178 | if (enable_ept_ad_bits) |
| 3179 | eptp |= VMX_EPT_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3180 | eptp |= (root_hpa & PAGE_MASK); |
| 3181 | |
| 3182 | return eptp; |
| 3183 | } |
| 3184 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3185 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 3186 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3187 | unsigned long guest_cr3; |
| 3188 | u64 eptp; |
| 3189 | |
| 3190 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3191 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3192 | eptp = construct_eptp(cr3); |
| 3193 | vmcs_write64(EPT_POINTER, eptp); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 3194 | guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) : |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3195 | vcpu->kvm->arch.ept_identity_map_addr; |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 3196 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3197 | } |
| 3198 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3199 | vmx_flush_tlb(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3200 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3201 | } |
| 3202 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3203 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3204 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3205 | unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ? |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3206 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); |
| 3207 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3208 | if (cr4 & X86_CR4_VMXE) { |
| 3209 | /* |
| 3210 | * To use VMXON (and later other VMX instructions), a guest |
| 3211 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 3212 | * So basically the check on whether to allow nested VMX |
| 3213 | * is here. |
| 3214 | */ |
| 3215 | if (!nested_vmx_allowed(vcpu)) |
| 3216 | return 1; |
| 3217 | } else if (to_vmx(vcpu)->nested.vmxon) |
| 3218 | return 1; |
| 3219 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3220 | vcpu->arch.cr4 = cr4; |
Avi Kivity | bc23008 | 2009-12-08 12:14:42 +0200 | [diff] [blame] | 3221 | if (enable_ept) { |
| 3222 | if (!is_paging(vcpu)) { |
| 3223 | hw_cr4 &= ~X86_CR4_PAE; |
| 3224 | hw_cr4 |= X86_CR4_PSE; |
Dongxiao Xu | c08800a | 2013-02-04 11:50:43 +0800 | [diff] [blame] | 3225 | /* |
| 3226 | * SMEP is disabled if CPU is in non-paging mode in |
| 3227 | * hardware. However KVM always uses paging mode to |
| 3228 | * emulate guest non-paging mode with TDP. |
| 3229 | * To emulate this behavior, SMEP needs to be manually |
| 3230 | * disabled when guest switches to non-paging mode. |
| 3231 | */ |
| 3232 | hw_cr4 &= ~X86_CR4_SMEP; |
Avi Kivity | bc23008 | 2009-12-08 12:14:42 +0200 | [diff] [blame] | 3233 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 3234 | hw_cr4 &= ~X86_CR4_PAE; |
| 3235 | } |
| 3236 | } |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3237 | |
| 3238 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 3239 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3240 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3241 | } |
| 3242 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3243 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 3244 | struct kvm_segment *var, int seg) |
| 3245 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3246 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3247 | u32 ar; |
| 3248 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 3249 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3250 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3251 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3252 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3253 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 3254 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 3255 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 3256 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3257 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3258 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 3259 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 3260 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 3261 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3262 | var->type = ar & 15; |
| 3263 | var->s = (ar >> 4) & 1; |
| 3264 | var->dpl = (ar >> 5) & 3; |
| 3265 | var->present = (ar >> 7) & 1; |
| 3266 | var->avl = (ar >> 12) & 1; |
| 3267 | var->l = (ar >> 13) & 1; |
| 3268 | var->db = (ar >> 14) & 1; |
| 3269 | var->g = (ar >> 15) & 1; |
| 3270 | var->unusable = (ar >> 16) & 1; |
| 3271 | } |
| 3272 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3273 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 3274 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3275 | struct kvm_segment s; |
| 3276 | |
| 3277 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 3278 | vmx_get_segment(vcpu, &s, seg); |
| 3279 | return s.base; |
| 3280 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3281 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3282 | } |
| 3283 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 3284 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3285 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 3286 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3287 | |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 3288 | if (!is_protmode(vcpu)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3289 | return 0; |
| 3290 | |
Avi Kivity | f4c63e5 | 2011-03-07 14:54:28 +0200 | [diff] [blame] | 3291 | if (!is_long_mode(vcpu) |
| 3292 | && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */ |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3293 | return 3; |
| 3294 | |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3295 | if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) { |
| 3296 | __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 3297 | vmx->cpl = vmx_read_guest_seg_selector(vmx, VCPU_SREG_CS) & 3; |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3298 | } |
Avi Kivity | d881e6f | 2012-06-06 18:36:48 +0300 | [diff] [blame] | 3299 | |
| 3300 | return vmx->cpl; |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3304 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3305 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3306 | u32 ar; |
| 3307 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 3308 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3309 | ar = 1 << 16; |
| 3310 | else { |
| 3311 | ar = var->type & 15; |
| 3312 | ar |= (var->s & 1) << 4; |
| 3313 | ar |= (var->dpl & 3) << 5; |
| 3314 | ar |= (var->present & 1) << 7; |
| 3315 | ar |= (var->avl & 1) << 12; |
| 3316 | ar |= (var->l & 1) << 13; |
| 3317 | ar |= (var->db & 1) << 14; |
| 3318 | ar |= (var->g & 1) << 15; |
| 3319 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3320 | |
| 3321 | return ar; |
| 3322 | } |
| 3323 | |
| 3324 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 3325 | struct kvm_segment *var, int seg) |
| 3326 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3327 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 3328 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3329 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3330 | vmx_segment_cache_clear(vmx); |
Gleb Natapov | 2f14324 | 2013-01-21 15:36:42 +0200 | [diff] [blame] | 3331 | if (seg == VCPU_SREG_CS) |
| 3332 | __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3333 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 3334 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 3335 | vmx->rmode.segs[seg] = *var; |
| 3336 | if (seg == VCPU_SREG_TR) |
| 3337 | vmcs_write16(sf->selector, var->selector); |
| 3338 | else if (var->s) |
| 3339 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3340 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3341 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 3342 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3343 | vmcs_writel(sf->base, var->base); |
| 3344 | vmcs_write32(sf->limit, var->limit); |
| 3345 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3346 | |
| 3347 | /* |
| 3348 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 3349 | * qemu binaries. |
| 3350 | * IA32 arch specifies that at the time of processor reset the |
| 3351 | * "Accessed" bit in the AR field of segment registers is 1. And qemu |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 3352 | * is setting it to 0 in the userland code. This causes invalid guest |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3353 | * state vmexit when "unrestricted guest" mode is turned on. |
| 3354 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 3355 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 3356 | * kvm hack. |
| 3357 | */ |
| 3358 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 3359 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3360 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 3361 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3362 | |
| 3363 | out: |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 3364 | vmx->emulation_required |= emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3365 | } |
| 3366 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3367 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 3368 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3369 | u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3370 | |
| 3371 | *db = (ar >> 14) & 1; |
| 3372 | *l = (ar >> 13) & 1; |
| 3373 | } |
| 3374 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3375 | static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3376 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3377 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 3378 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3379 | } |
| 3380 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3381 | static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3382 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3383 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 3384 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3385 | } |
| 3386 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3387 | static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3388 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3389 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 3390 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3391 | } |
| 3392 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3393 | static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3394 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3395 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 3396 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3397 | } |
| 3398 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3399 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 3400 | { |
| 3401 | struct kvm_segment var; |
| 3402 | u32 ar; |
| 3403 | |
| 3404 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 3405 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 3406 | if (seg == VCPU_SREG_CS) |
| 3407 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3408 | ar = vmx_segment_access_rights(&var); |
| 3409 | |
| 3410 | if (var.base != (var.selector << 4)) |
| 3411 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 3412 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3413 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 3414 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3415 | return false; |
| 3416 | |
| 3417 | return true; |
| 3418 | } |
| 3419 | |
| 3420 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 3421 | { |
| 3422 | struct kvm_segment cs; |
| 3423 | unsigned int cs_rpl; |
| 3424 | |
| 3425 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 3426 | cs_rpl = cs.selector & SELECTOR_RPL_MASK; |
| 3427 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3428 | if (cs.unusable) |
| 3429 | return false; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3430 | if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK)) |
| 3431 | return false; |
| 3432 | if (!cs.s) |
| 3433 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3434 | if (cs.type & AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3435 | if (cs.dpl > cs_rpl) |
| 3436 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3437 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3438 | if (cs.dpl != cs_rpl) |
| 3439 | return false; |
| 3440 | } |
| 3441 | if (!cs.present) |
| 3442 | return false; |
| 3443 | |
| 3444 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 3445 | return true; |
| 3446 | } |
| 3447 | |
| 3448 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 3449 | { |
| 3450 | struct kvm_segment ss; |
| 3451 | unsigned int ss_rpl; |
| 3452 | |
| 3453 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 3454 | ss_rpl = ss.selector & SELECTOR_RPL_MASK; |
| 3455 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3456 | if (ss.unusable) |
| 3457 | return true; |
| 3458 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3459 | return false; |
| 3460 | if (!ss.s) |
| 3461 | return false; |
| 3462 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 3463 | return false; |
| 3464 | if (!ss.present) |
| 3465 | return false; |
| 3466 | |
| 3467 | return true; |
| 3468 | } |
| 3469 | |
| 3470 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 3471 | { |
| 3472 | struct kvm_segment var; |
| 3473 | unsigned int rpl; |
| 3474 | |
| 3475 | vmx_get_segment(vcpu, &var, seg); |
| 3476 | rpl = var.selector & SELECTOR_RPL_MASK; |
| 3477 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3478 | if (var.unusable) |
| 3479 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3480 | if (!var.s) |
| 3481 | return false; |
| 3482 | if (!var.present) |
| 3483 | return false; |
| 3484 | if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) { |
| 3485 | if (var.dpl < rpl) /* DPL < RPL */ |
| 3486 | return false; |
| 3487 | } |
| 3488 | |
| 3489 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 3490 | * rights flags |
| 3491 | */ |
| 3492 | return true; |
| 3493 | } |
| 3494 | |
| 3495 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 3496 | { |
| 3497 | struct kvm_segment tr; |
| 3498 | |
| 3499 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 3500 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3501 | if (tr.unusable) |
| 3502 | return false; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3503 | if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 3504 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3505 | if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3506 | return false; |
| 3507 | if (!tr.present) |
| 3508 | return false; |
| 3509 | |
| 3510 | return true; |
| 3511 | } |
| 3512 | |
| 3513 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 3514 | { |
| 3515 | struct kvm_segment ldtr; |
| 3516 | |
| 3517 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 3518 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3519 | if (ldtr.unusable) |
| 3520 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3521 | if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 3522 | return false; |
| 3523 | if (ldtr.type != 2) |
| 3524 | return false; |
| 3525 | if (!ldtr.present) |
| 3526 | return false; |
| 3527 | |
| 3528 | return true; |
| 3529 | } |
| 3530 | |
| 3531 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 3532 | { |
| 3533 | struct kvm_segment cs, ss; |
| 3534 | |
| 3535 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 3536 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 3537 | |
| 3538 | return ((cs.selector & SELECTOR_RPL_MASK) == |
| 3539 | (ss.selector & SELECTOR_RPL_MASK)); |
| 3540 | } |
| 3541 | |
| 3542 | /* |
| 3543 | * Check if guest state is valid. Returns true if valid, false if |
| 3544 | * not. |
| 3545 | * We assume that registers are always usable |
| 3546 | */ |
| 3547 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 3548 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 3549 | if (enable_unrestricted_guest) |
| 3550 | return true; |
| 3551 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3552 | /* real mode guest state checks */ |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 3553 | if (!is_protmode(vcpu)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3554 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 3555 | return false; |
| 3556 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 3557 | return false; |
| 3558 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 3559 | return false; |
| 3560 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 3561 | return false; |
| 3562 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 3563 | return false; |
| 3564 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 3565 | return false; |
| 3566 | } else { |
| 3567 | /* protected mode guest state checks */ |
| 3568 | if (!cs_ss_rpl_check(vcpu)) |
| 3569 | return false; |
| 3570 | if (!code_segment_valid(vcpu)) |
| 3571 | return false; |
| 3572 | if (!stack_segment_valid(vcpu)) |
| 3573 | return false; |
| 3574 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 3575 | return false; |
| 3576 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 3577 | return false; |
| 3578 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 3579 | return false; |
| 3580 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 3581 | return false; |
| 3582 | if (!tr_valid(vcpu)) |
| 3583 | return false; |
| 3584 | if (!ldtr_valid(vcpu)) |
| 3585 | return false; |
| 3586 | } |
| 3587 | /* TODO: |
| 3588 | * - Add checks on RIP |
| 3589 | * - Add checks on RFLAGS |
| 3590 | */ |
| 3591 | |
| 3592 | return true; |
| 3593 | } |
| 3594 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3595 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3596 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3597 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3598 | u16 data = 0; |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3599 | int r, idx, ret = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3600 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3601 | idx = srcu_read_lock(&kvm->srcu); |
| 3602 | fn = rmode_tss_base(kvm) >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3603 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3604 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3605 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3606 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 3607 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 3608 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3609 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3610 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3611 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 3612 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3613 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3614 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3615 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3616 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3617 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3618 | r = kvm_write_guest_page(kvm, fn, &data, |
| 3619 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 3620 | sizeof(u8)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3621 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3622 | goto out; |
| 3623 | |
| 3624 | ret = 1; |
| 3625 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3626 | srcu_read_unlock(&kvm->srcu, idx); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3627 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3628 | } |
| 3629 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3630 | static int init_rmode_identity_map(struct kvm *kvm) |
| 3631 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3632 | int i, idx, r, ret; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3633 | pfn_t identity_map_pfn; |
| 3634 | u32 tmp; |
| 3635 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3636 | if (!enable_ept) |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3637 | return 1; |
| 3638 | if (unlikely(!kvm->arch.ept_identity_pagetable)) { |
| 3639 | printk(KERN_ERR "EPT: identity-mapping pagetable " |
| 3640 | "haven't been allocated!\n"); |
| 3641 | return 0; |
| 3642 | } |
| 3643 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
| 3644 | return 1; |
| 3645 | ret = 0; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3646 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3647 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3648 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 3649 | if (r < 0) |
| 3650 | goto out; |
| 3651 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 3652 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 3653 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 3654 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 3655 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 3656 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 3657 | if (r < 0) |
| 3658 | goto out; |
| 3659 | } |
| 3660 | kvm->arch.ept_identity_pagetable_done = true; |
| 3661 | ret = 1; |
| 3662 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3663 | srcu_read_unlock(&kvm->srcu, idx); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3664 | return ret; |
| 3665 | } |
| 3666 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3667 | static void seg_setup(int seg) |
| 3668 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 3669 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3670 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3671 | |
| 3672 | vmcs_write16(sf->selector, 0); |
| 3673 | vmcs_writel(sf->base, 0); |
| 3674 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 3675 | ar = 0x93; |
| 3676 | if (seg == VCPU_SREG_CS) |
| 3677 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3678 | |
| 3679 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3680 | } |
| 3681 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3682 | static int alloc_apic_access_page(struct kvm *kvm) |
| 3683 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 3684 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3685 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 3686 | int r = 0; |
| 3687 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3688 | mutex_lock(&kvm->slots_lock); |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 3689 | if (kvm->arch.apic_access_page) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3690 | goto out; |
| 3691 | kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT; |
| 3692 | kvm_userspace_mem.flags = 0; |
| 3693 | kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL; |
| 3694 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
Alex Williamson | f82a8cf | 2012-12-10 10:33:21 -0700 | [diff] [blame] | 3695 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, false); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3696 | if (r) |
| 3697 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3698 | |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 3699 | page = gfn_to_page(kvm, 0xfee00); |
| 3700 | if (is_error_page(page)) { |
| 3701 | r = -EFAULT; |
| 3702 | goto out; |
| 3703 | } |
| 3704 | |
| 3705 | kvm->arch.apic_access_page = page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3706 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3707 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3708 | return r; |
| 3709 | } |
| 3710 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3711 | static int alloc_identity_pagetable(struct kvm *kvm) |
| 3712 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 3713 | struct page *page; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3714 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 3715 | int r = 0; |
| 3716 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3717 | mutex_lock(&kvm->slots_lock); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3718 | if (kvm->arch.ept_identity_pagetable) |
| 3719 | goto out; |
| 3720 | kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT; |
| 3721 | kvm_userspace_mem.flags = 0; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3722 | kvm_userspace_mem.guest_phys_addr = |
| 3723 | kvm->arch.ept_identity_map_addr; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3724 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
Alex Williamson | f82a8cf | 2012-12-10 10:33:21 -0700 | [diff] [blame] | 3725 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, false); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3726 | if (r) |
| 3727 | goto out; |
| 3728 | |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 3729 | page = gfn_to_page(kvm, kvm->arch.ept_identity_map_addr >> PAGE_SHIFT); |
| 3730 | if (is_error_page(page)) { |
| 3731 | r = -EFAULT; |
| 3732 | goto out; |
| 3733 | } |
| 3734 | |
| 3735 | kvm->arch.ept_identity_pagetable = page; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3736 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3737 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3738 | return r; |
| 3739 | } |
| 3740 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3741 | static void allocate_vpid(struct vcpu_vmx *vmx) |
| 3742 | { |
| 3743 | int vpid; |
| 3744 | |
| 3745 | vmx->vpid = 0; |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 3746 | if (!enable_vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3747 | return; |
| 3748 | spin_lock(&vmx_vpid_lock); |
| 3749 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 3750 | if (vpid < VMX_NR_VPIDS) { |
| 3751 | vmx->vpid = vpid; |
| 3752 | __set_bit(vpid, vmx_vpid_bitmap); |
| 3753 | } |
| 3754 | spin_unlock(&vmx_vpid_lock); |
| 3755 | } |
| 3756 | |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 3757 | static void free_vpid(struct vcpu_vmx *vmx) |
| 3758 | { |
| 3759 | if (!enable_vpid) |
| 3760 | return; |
| 3761 | spin_lock(&vmx_vpid_lock); |
| 3762 | if (vmx->vpid != 0) |
| 3763 | __clear_bit(vmx->vpid, vmx_vpid_bitmap); |
| 3764 | spin_unlock(&vmx_vpid_lock); |
| 3765 | } |
| 3766 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3767 | #define MSR_TYPE_R 1 |
| 3768 | #define MSR_TYPE_W 2 |
| 3769 | static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 3770 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3771 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3772 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3773 | |
| 3774 | if (!cpu_has_vmx_msr_bitmap()) |
| 3775 | return; |
| 3776 | |
| 3777 | /* |
| 3778 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 3779 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 3780 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 3781 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3782 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3783 | if (type & MSR_TYPE_R) |
| 3784 | /* read-low */ |
| 3785 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 3786 | |
| 3787 | if (type & MSR_TYPE_W) |
| 3788 | /* write-low */ |
| 3789 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 3790 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3791 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 3792 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3793 | if (type & MSR_TYPE_R) |
| 3794 | /* read-high */ |
| 3795 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 3796 | |
| 3797 | if (type & MSR_TYPE_W) |
| 3798 | /* write-high */ |
| 3799 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 3800 | |
| 3801 | } |
| 3802 | } |
| 3803 | |
| 3804 | static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
| 3805 | u32 msr, int type) |
| 3806 | { |
| 3807 | int f = sizeof(unsigned long); |
| 3808 | |
| 3809 | if (!cpu_has_vmx_msr_bitmap()) |
| 3810 | return; |
| 3811 | |
| 3812 | /* |
| 3813 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 3814 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 3815 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 3816 | */ |
| 3817 | if (msr <= 0x1fff) { |
| 3818 | if (type & MSR_TYPE_R) |
| 3819 | /* read-low */ |
| 3820 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 3821 | |
| 3822 | if (type & MSR_TYPE_W) |
| 3823 | /* write-low */ |
| 3824 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 3825 | |
| 3826 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 3827 | msr &= 0x1fff; |
| 3828 | if (type & MSR_TYPE_R) |
| 3829 | /* read-high */ |
| 3830 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 3831 | |
| 3832 | if (type & MSR_TYPE_W) |
| 3833 | /* write-high */ |
| 3834 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 3835 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3836 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3837 | } |
| 3838 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3839 | static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only) |
| 3840 | { |
| 3841 | if (!longmode_only) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3842 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, |
| 3843 | msr, MSR_TYPE_R | MSR_TYPE_W); |
| 3844 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, |
| 3845 | msr, MSR_TYPE_R | MSR_TYPE_W); |
| 3846 | } |
| 3847 | |
| 3848 | static void vmx_enable_intercept_msr_read_x2apic(u32 msr) |
| 3849 | { |
| 3850 | __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
| 3851 | msr, MSR_TYPE_R); |
| 3852 | __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
| 3853 | msr, MSR_TYPE_R); |
| 3854 | } |
| 3855 | |
| 3856 | static void vmx_disable_intercept_msr_read_x2apic(u32 msr) |
| 3857 | { |
| 3858 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
| 3859 | msr, MSR_TYPE_R); |
| 3860 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
| 3861 | msr, MSR_TYPE_R); |
| 3862 | } |
| 3863 | |
| 3864 | static void vmx_disable_intercept_msr_write_x2apic(u32 msr) |
| 3865 | { |
| 3866 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
| 3867 | msr, MSR_TYPE_W); |
| 3868 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
| 3869 | msr, MSR_TYPE_W); |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3870 | } |
| 3871 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3872 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3873 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 3874 | * will not change in the lifetime of the guest. |
| 3875 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 3876 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 3877 | */ |
| 3878 | static void vmx_set_constant_host_state(void) |
| 3879 | { |
| 3880 | u32 low32, high32; |
| 3881 | unsigned long tmpl; |
| 3882 | struct desc_ptr dt; |
| 3883 | |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 3884 | vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3885 | vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */ |
| 3886 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 3887 | |
| 3888 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 3889 | #ifdef CONFIG_X86_64 |
| 3890 | /* |
| 3891 | * Load null selectors, so we can avoid reloading them in |
| 3892 | * __vmx_load_host_state(), in case userspace uses the null selectors |
| 3893 | * too (the expected case). |
| 3894 | */ |
| 3895 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 3896 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 3897 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3898 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3899 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 3900 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3901 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3902 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 3903 | |
| 3904 | native_store_idt(&dt); |
| 3905 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
| 3906 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 3907 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3908 | |
| 3909 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 3910 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 3911 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 3912 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 3913 | |
| 3914 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 3915 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 3916 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 3917 | } |
| 3918 | } |
| 3919 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3920 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 3921 | { |
| 3922 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 3923 | if (enable_ept) |
| 3924 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 3925 | if (is_guest_mode(&vmx->vcpu)) |
| 3926 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 3927 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3928 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 3929 | } |
| 3930 | |
| 3931 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 3932 | { |
| 3933 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
| 3934 | if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) { |
| 3935 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 3936 | #ifdef CONFIG_X86_64 |
| 3937 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 3938 | CPU_BASED_CR8_LOAD_EXITING; |
| 3939 | #endif |
| 3940 | } |
| 3941 | if (!enable_ept) |
| 3942 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 3943 | CPU_BASED_CR3_LOAD_EXITING | |
| 3944 | CPU_BASED_INVLPG_EXITING; |
| 3945 | return exec_control; |
| 3946 | } |
| 3947 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3948 | static int vmx_vm_has_apicv(struct kvm *kvm) |
| 3949 | { |
| 3950 | return enable_apicv_reg_vid && irqchip_in_kernel(kvm); |
| 3951 | } |
| 3952 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3953 | static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) |
| 3954 | { |
| 3955 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
| 3956 | if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 3957 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 3958 | if (vmx->vpid == 0) |
| 3959 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 3960 | if (!enable_ept) { |
| 3961 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 3962 | enable_unrestricted_guest = 0; |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3963 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
| 3964 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3965 | } |
| 3966 | if (!enable_unrestricted_guest) |
| 3967 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 3968 | if (!ple_gap) |
| 3969 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3970 | if (!vmx_vm_has_apicv(vmx->vcpu.kvm)) |
| 3971 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 3972 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3973 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3974 | return exec_control; |
| 3975 | } |
| 3976 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3977 | static void ept_set_mmio_spte_mask(void) |
| 3978 | { |
| 3979 | /* |
| 3980 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 3981 | * of an EPT paging-structure entry is 110b (write/execute). |
| 3982 | * Also, magic bits (0xffull << 49) is set to quickly identify mmio |
| 3983 | * spte. |
| 3984 | */ |
| 3985 | kvm_mmu_set_mmio_spte_mask(0xffull << 49 | 0x6ull); |
| 3986 | } |
| 3987 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3988 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3989 | * Sets up the vmcs for emulated real mode. |
| 3990 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3991 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3992 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 3993 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3994 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 3995 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3996 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3997 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3998 | /* I/O */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3999 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
| 4000 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4001 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4002 | if (cpu_has_vmx_msr_bitmap()) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4003 | vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4004 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4005 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 4006 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4007 | /* Control */ |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4008 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, |
| 4009 | vmcs_config.pin_based_exec_ctrl); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4010 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4011 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4012 | |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 4013 | if (cpu_has_secondary_exec_ctrls()) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4014 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 4015 | vmx_secondary_exec_control(vmx)); |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 4016 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4017 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4018 | if (enable_apicv_reg_vid) { |
| 4019 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 4020 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 4021 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 4022 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 4023 | |
| 4024 | vmcs_write16(GUEST_INTR_STATUS, 0); |
| 4025 | } |
| 4026 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4027 | if (ple_gap) { |
| 4028 | vmcs_write32(PLE_GAP, ple_gap); |
| 4029 | vmcs_write32(PLE_WINDOW, ple_window); |
| 4030 | } |
| 4031 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 4032 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 4033 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4034 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 4035 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 4036 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 4037 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4038 | vmx_set_constant_host_state(); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4039 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4040 | rdmsrl(MSR_FS_BASE, a); |
| 4041 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 4042 | rdmsrl(MSR_GS_BASE, a); |
| 4043 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 4044 | #else |
| 4045 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 4046 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 4047 | #endif |
| 4048 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4049 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 4050 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 4051 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4052 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 4053 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4054 | |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 4055 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4056 | u32 msr_low, msr_high; |
| 4057 | u64 host_pat; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 4058 | rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high); |
| 4059 | host_pat = msr_low | ((u64) msr_high << 32); |
| 4060 | /* Write the default value follow host pat */ |
| 4061 | vmcs_write64(GUEST_IA32_PAT, host_pat); |
| 4062 | /* Keep arch.pat sync with GUEST_IA32_PAT */ |
| 4063 | vmx->vcpu.arch.pat = host_pat; |
| 4064 | } |
| 4065 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4066 | for (i = 0; i < NR_VMX_MSR; ++i) { |
| 4067 | u32 index = vmx_msr_index[i]; |
| 4068 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4069 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4070 | |
| 4071 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 4072 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 4073 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 4074 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4075 | vmx->guest_msrs[j].index = i; |
| 4076 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 4077 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4078 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4079 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4080 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4081 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4082 | |
| 4083 | /* 22.2.1, 20.8.1 */ |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4084 | vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl); |
| 4085 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4086 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4087 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4088 | |
| 4089 | return 0; |
| 4090 | } |
| 4091 | |
| 4092 | static int vmx_vcpu_reset(struct kvm_vcpu *vcpu) |
| 4093 | { |
| 4094 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4095 | u64 msr; |
Xiao Guangrong | 4b9d3a0 | 2010-06-08 10:15:51 +0800 | [diff] [blame] | 4096 | int ret; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4097 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4098 | vmx->rmode.vm86_active = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4099 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4100 | vmx->soft_vnmi_blocked = 0; |
| 4101 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4102 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4103 | kvm_set_cr8(&vmx->vcpu, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4104 | msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 4105 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4106 | msr |= MSR_IA32_APICBASE_BSP; |
| 4107 | kvm_set_apic_base(&vmx->vcpu, msr); |
| 4108 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4109 | vmx_segment_cache_clear(vmx); |
| 4110 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 4111 | seg_setup(VCPU_SREG_CS); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 4112 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4113 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 4114 | else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4115 | vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8); |
| 4116 | vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4117 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4118 | |
| 4119 | seg_setup(VCPU_SREG_DS); |
| 4120 | seg_setup(VCPU_SREG_ES); |
| 4121 | seg_setup(VCPU_SREG_FS); |
| 4122 | seg_setup(VCPU_SREG_GS); |
| 4123 | seg_setup(VCPU_SREG_SS); |
| 4124 | |
| 4125 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 4126 | vmcs_writel(GUEST_TR_BASE, 0); |
| 4127 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 4128 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 4129 | |
| 4130 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 4131 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 4132 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 4133 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 4134 | |
| 4135 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 4136 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 4137 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 4138 | |
| 4139 | vmcs_writel(GUEST_RFLAGS, 0x02); |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 4140 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4141 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4142 | else |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4143 | kvm_rip_write(vcpu, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4144 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4145 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 4146 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 4147 | |
| 4148 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 4149 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 4150 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 4151 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4152 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
| 4153 | vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
| 4154 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4155 | /* Special registers */ |
| 4156 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 4157 | |
| 4158 | setup_msrs(vmx); |
| 4159 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4160 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 4161 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4162 | if (cpu_has_vmx_tpr_shadow()) { |
| 4163 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
| 4164 | if (vm_need_tpr_shadow(vmx->vcpu.kvm)) |
| 4165 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Takuya Yoshikawa | afc2018 | 2011-03-05 12:40:20 +0900 | [diff] [blame] | 4166 | __pa(vmx->vcpu.arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4167 | vmcs_write32(TPR_THRESHOLD, 0); |
| 4168 | } |
| 4169 | |
| 4170 | if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 4171 | vmcs_write64(APIC_ACCESS_ADDR, |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 4172 | page_to_phys(vmx->vcpu.kvm->arch.apic_access_page)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4173 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4174 | if (vmx->vpid != 0) |
| 4175 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 4176 | |
Eduardo Habkost | fa40052 | 2009-10-24 02:49:58 -0200 | [diff] [blame] | 4177 | vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Marcelo Tosatti | 7a4f5ad | 2012-03-27 19:47:26 -0300 | [diff] [blame] | 4178 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4179 | vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */ |
Marcelo Tosatti | 7a4f5ad | 2012-03-27 19:47:26 -0300 | [diff] [blame] | 4180 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 4181 | vmx_set_cr4(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 4182 | vmx_set_efer(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 4183 | vmx_fpu_activate(&vmx->vcpu); |
| 4184 | update_exception_bitmap(&vmx->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4185 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 4186 | vpid_sync_context(vmx); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4187 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 4188 | ret = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4189 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4190 | return ret; |
| 4191 | } |
| 4192 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 4193 | /* |
| 4194 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 4195 | * For most existing hypervisors, this will always return true. |
| 4196 | */ |
| 4197 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 4198 | { |
| 4199 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 4200 | PIN_BASED_EXT_INTR_MASK; |
| 4201 | } |
| 4202 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4203 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
| 4204 | { |
| 4205 | u32 cpu_based_vm_exec_control; |
Nadav Har'El | d6185f2 | 2011-09-22 13:52:56 +0300 | [diff] [blame] | 4206 | if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) { |
| 4207 | /* |
| 4208 | * We get here if vmx_interrupt_allowed() said we can't |
| 4209 | * inject to L1 now because L2 must run. Ask L2 to exit |
| 4210 | * right after entry, so we can inject to L1 more promptly. |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 4211 | */ |
Nadav Har'El | d6185f2 | 2011-09-22 13:52:56 +0300 | [diff] [blame] | 4212 | kvm_make_request(KVM_REQ_IMMEDIATE_EXIT, vcpu); |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 4213 | return; |
Nadav Har'El | d6185f2 | 2011-09-22 13:52:56 +0300 | [diff] [blame] | 4214 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4215 | |
| 4216 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4217 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 4218 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 4219 | } |
| 4220 | |
| 4221 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
| 4222 | { |
| 4223 | u32 cpu_based_vm_exec_control; |
| 4224 | |
| 4225 | if (!cpu_has_virtual_nmis()) { |
| 4226 | enable_irq_window(vcpu); |
| 4227 | return; |
| 4228 | } |
| 4229 | |
Avi Kivity | 30bd0c4 | 2010-11-01 23:20:48 +0200 | [diff] [blame] | 4230 | if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
| 4231 | enable_irq_window(vcpu); |
| 4232 | return; |
| 4233 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4234 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4235 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING; |
| 4236 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 4237 | } |
| 4238 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4239 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4240 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 4241 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4242 | uint32_t intr; |
| 4243 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 4244 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4245 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4246 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 4247 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4248 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 4249 | int inc_eip = 0; |
| 4250 | if (vcpu->arch.interrupt.soft) |
| 4251 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 4252 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 4253 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4254 | return; |
| 4255 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4256 | intr = irq | INTR_INFO_VALID_MASK; |
| 4257 | if (vcpu->arch.interrupt.soft) { |
| 4258 | intr |= INTR_TYPE_SOFT_INTR; |
| 4259 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 4260 | vmx->vcpu.arch.event_exit_inst_len); |
| 4261 | } else |
| 4262 | intr |= INTR_TYPE_EXT_INTR; |
| 4263 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4264 | } |
| 4265 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4266 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 4267 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 4268 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4269 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 4270 | if (is_guest_mode(vcpu)) |
| 4271 | return; |
| 4272 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4273 | if (!cpu_has_virtual_nmis()) { |
| 4274 | /* |
| 4275 | * Tracking the NMI-blocked state in software is built upon |
| 4276 | * finding the next open IRQ window. This, in turn, depends on |
| 4277 | * well-behaving guests: They have to keep IRQs disabled at |
| 4278 | * least as long as the NMI handler runs. Otherwise we may |
| 4279 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 4280 | * highly unlikely, we can live with the residual risk. |
| 4281 | */ |
| 4282 | vmx->soft_vnmi_blocked = 1; |
| 4283 | vmx->vnmi_blocked_time = 0; |
| 4284 | } |
| 4285 | |
Jan Kiszka | 487b391 | 2008-09-26 09:30:56 +0200 | [diff] [blame] | 4286 | ++vcpu->stat.nmi_injections; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 4287 | vmx->nmi_known_unmasked = false; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4288 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 4289 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 4290 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 4291 | return; |
| 4292 | } |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4293 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 4294 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4295 | } |
| 4296 | |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4297 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 4298 | { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4299 | if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked) |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4300 | return 0; |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 4301 | |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4302 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
Avi Kivity | 30bd0c4 | 2010-11-01 23:20:48 +0200 | [diff] [blame] | 4303 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 4304 | | GUEST_INTR_STATE_NMI)); |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 4305 | } |
| 4306 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4307 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 4308 | { |
| 4309 | if (!cpu_has_virtual_nmis()) |
| 4310 | return to_vmx(vcpu)->soft_vnmi_blocked; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 4311 | if (to_vmx(vcpu)->nmi_known_unmasked) |
| 4312 | return false; |
Avi Kivity | c332c83 | 2010-05-04 12:24:12 +0300 | [diff] [blame] | 4313 | return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 4317 | { |
| 4318 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4319 | |
| 4320 | if (!cpu_has_virtual_nmis()) { |
| 4321 | if (vmx->soft_vnmi_blocked != masked) { |
| 4322 | vmx->soft_vnmi_blocked = masked; |
| 4323 | vmx->vnmi_blocked_time = 0; |
| 4324 | } |
| 4325 | } else { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 4326 | vmx->nmi_known_unmasked = !masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4327 | if (masked) |
| 4328 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 4329 | GUEST_INTR_STATE_NMI); |
| 4330 | else |
| 4331 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 4332 | GUEST_INTR_STATE_NMI); |
| 4333 | } |
| 4334 | } |
| 4335 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4336 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 4337 | { |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 4338 | if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) { |
Nadav Har'El | 51cfe38 | 2011-09-22 13:53:26 +0300 | [diff] [blame] | 4339 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4340 | if (to_vmx(vcpu)->nested.nested_run_pending || |
| 4341 | (vmcs12->idt_vectoring_info_field & |
| 4342 | VECTORING_INFO_VALID_MASK)) |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 4343 | return 0; |
| 4344 | nested_vmx_vmexit(vcpu); |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 4345 | vmcs12->vm_exit_reason = EXIT_REASON_EXTERNAL_INTERRUPT; |
| 4346 | vmcs12->vm_exit_intr_info = 0; |
| 4347 | /* fall through to normal code, but now in L1, not L2 */ |
| 4348 | } |
| 4349 | |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4350 | return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
| 4351 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 4352 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4353 | } |
| 4354 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4355 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 4356 | { |
| 4357 | int ret; |
| 4358 | struct kvm_userspace_memory_region tss_mem = { |
Sheng Yang | 6fe6397 | 2008-10-16 17:30:58 +0800 | [diff] [blame] | 4359 | .slot = TSS_PRIVATE_MEMSLOT, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4360 | .guest_phys_addr = addr, |
| 4361 | .memory_size = PAGE_SIZE * 3, |
| 4362 | .flags = 0, |
| 4363 | }; |
| 4364 | |
Alex Williamson | f82a8cf | 2012-12-10 10:33:21 -0700 | [diff] [blame] | 4365 | ret = kvm_set_memory_region(kvm, &tss_mem, false); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4366 | if (ret) |
| 4367 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 4368 | kvm->arch.tss_addr = addr; |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 4369 | if (!init_rmode_tss(kvm)) |
| 4370 | return -ENOMEM; |
| 4371 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4372 | return 0; |
| 4373 | } |
| 4374 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4375 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4376 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4377 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4378 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 4379 | /* |
| 4380 | * Update instruction length as we may reinject the exception |
| 4381 | * from user space while in guest debugging mode. |
| 4382 | */ |
| 4383 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 4384 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4385 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4386 | return false; |
| 4387 | /* fall through */ |
| 4388 | case DB_VECTOR: |
| 4389 | if (vcpu->guest_debug & |
| 4390 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 4391 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4392 | /* fall through */ |
| 4393 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4394 | case OF_VECTOR: |
| 4395 | case BR_VECTOR: |
| 4396 | case UD_VECTOR: |
| 4397 | case DF_VECTOR: |
| 4398 | case SS_VECTOR: |
| 4399 | case GP_VECTOR: |
| 4400 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4401 | return true; |
| 4402 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4403 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4404 | return false; |
| 4405 | } |
| 4406 | |
| 4407 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 4408 | int vec, u32 err_code) |
| 4409 | { |
| 4410 | /* |
| 4411 | * Instruction with address size override prefix opcode 0x67 |
| 4412 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 4413 | */ |
| 4414 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
| 4415 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
| 4416 | if (vcpu->arch.halt_request) { |
| 4417 | vcpu->arch.halt_request = 0; |
| 4418 | return kvm_emulate_halt(vcpu); |
| 4419 | } |
| 4420 | return 1; |
| 4421 | } |
| 4422 | return 0; |
| 4423 | } |
| 4424 | |
| 4425 | /* |
| 4426 | * Forward all other exceptions that are valid in real mode. |
| 4427 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 4428 | * the required debugging infrastructure rework. |
| 4429 | */ |
| 4430 | kvm_queue_exception(vcpu, vec); |
| 4431 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4432 | } |
| 4433 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4434 | /* |
| 4435 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 4436 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 4437 | * We pass a fake environment to the machine check handler because we want |
| 4438 | * the guest to be always treated like user space, no matter what context |
| 4439 | * it used internally. |
| 4440 | */ |
| 4441 | static void kvm_machine_check(void) |
| 4442 | { |
| 4443 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 4444 | struct pt_regs regs = { |
| 4445 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 4446 | .flags = X86_EFLAGS_IF, |
| 4447 | }; |
| 4448 | |
| 4449 | do_machine_check(®s, 0); |
| 4450 | #endif |
| 4451 | } |
| 4452 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4453 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4454 | { |
| 4455 | /* already handled by vcpu_run */ |
| 4456 | return 1; |
| 4457 | } |
| 4458 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4459 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4460 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 4461 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4462 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4463 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4464 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4465 | u32 vect_info; |
| 4466 | enum emulation_result er; |
| 4467 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 4468 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 4469 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4470 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4471 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4472 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4473 | |
Jan Kiszka | e4a4188 | 2008-09-26 09:30:46 +0200 | [diff] [blame] | 4474 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 4475 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 4476 | |
| 4477 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 4478 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 4479 | return 1; |
| 4480 | } |
| 4481 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4482 | if (is_invalid_opcode(intr_info)) { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4483 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4484 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 4485 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4486 | return 1; |
| 4487 | } |
| 4488 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4489 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 4490 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4491 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 4492 | |
| 4493 | /* |
| 4494 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 4495 | * MMIO, it is better to report an internal error. |
| 4496 | * See the comments in vmx_handle_exit. |
| 4497 | */ |
| 4498 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 4499 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 4500 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4501 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
| 4502 | vcpu->run->internal.ndata = 2; |
| 4503 | vcpu->run->internal.data[0] = vect_info; |
| 4504 | vcpu->run->internal.data[1] = intr_info; |
| 4505 | return 0; |
| 4506 | } |
| 4507 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4508 | if (is_page_fault(intr_info)) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4509 | /* EPT won't cause page fault directly */ |
Julia Lawall | cf3ace7 | 2011-08-02 12:34:57 +0200 | [diff] [blame] | 4510 | BUG_ON(enable_ept); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4511 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4512 | trace_kvm_page_fault(cr2, error_code); |
| 4513 | |
Gleb Natapov | 3298b75 | 2009-05-11 13:35:46 +0300 | [diff] [blame] | 4514 | if (kvm_event_needs_reinjection(vcpu)) |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 4515 | kvm_mmu_unprotect_page_virt(vcpu, cr2); |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4516 | return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4517 | } |
| 4518 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4519 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4520 | |
| 4521 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 4522 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 4523 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4524 | switch (ex_no) { |
| 4525 | case DB_VECTOR: |
| 4526 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 4527 | if (!(vcpu->guest_debug & |
| 4528 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
| 4529 | vcpu->arch.dr6 = dr6 | DR6_FIXED_1; |
| 4530 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 4531 | return 1; |
| 4532 | } |
| 4533 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 4534 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 4535 | /* fall through */ |
| 4536 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 4537 | /* |
| 4538 | * Update instruction length as we may reinject #BP from |
| 4539 | * user space while in guest debugging mode. Reading it for |
| 4540 | * #DB as well causes no harm, it is not used in that case. |
| 4541 | */ |
| 4542 | vmx->vcpu.arch.event_exit_inst_len = |
| 4543 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4544 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 4545 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4546 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 4547 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4548 | break; |
| 4549 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4550 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 4551 | kvm_run->ex.exception = ex_no; |
| 4552 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4553 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4554 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4555 | return 0; |
| 4556 | } |
| 4557 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4558 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4559 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 4560 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4561 | return 1; |
| 4562 | } |
| 4563 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4564 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4565 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4566 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4567 | return 0; |
| 4568 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4569 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4570 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4571 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4572 | unsigned long exit_qualification; |
Jan Kiszka | 34c33d1 | 2009-02-08 13:28:15 +0100 | [diff] [blame] | 4573 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4574 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4575 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4576 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4577 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 4578 | in = (exit_qualification & 8) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 4579 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4580 | ++vcpu->stat.io_exits; |
| 4581 | |
| 4582 | if (string || in) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4583 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4584 | |
| 4585 | port = exit_qualification >> 16; |
| 4586 | size = (exit_qualification & 7) + 1; |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 4587 | skip_emulated_instruction(vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4588 | |
| 4589 | return kvm_fast_pio_out(vcpu, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4590 | } |
| 4591 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4592 | static void |
| 4593 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 4594 | { |
| 4595 | /* |
| 4596 | * Patch in the VMCALL instruction: |
| 4597 | */ |
| 4598 | hypercall[0] = 0x0f; |
| 4599 | hypercall[1] = 0x01; |
| 4600 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4601 | } |
| 4602 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 4603 | /* called to set cr0 as appropriate for a mov-to-cr0 exit. */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4604 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 4605 | { |
| 4606 | if (to_vmx(vcpu)->nested.vmxon && |
| 4607 | ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON)) |
| 4608 | return 1; |
| 4609 | |
| 4610 | if (is_guest_mode(vcpu)) { |
| 4611 | /* |
| 4612 | * We get here when L2 changed cr0 in a way that did not change |
| 4613 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
| 4614 | * but did change L0 shadowed bits. This can currently happen |
| 4615 | * with the TS bit: L0 may want to leave TS on (for lazy fpu |
| 4616 | * loading) while pretending to allow the guest to change it. |
| 4617 | */ |
| 4618 | if (kvm_set_cr0(vcpu, (val & vcpu->arch.cr0_guest_owned_bits) | |
| 4619 | (vcpu->arch.cr0 & ~vcpu->arch.cr0_guest_owned_bits))) |
| 4620 | return 1; |
| 4621 | vmcs_writel(CR0_READ_SHADOW, val); |
| 4622 | return 0; |
| 4623 | } else |
| 4624 | return kvm_set_cr0(vcpu, val); |
| 4625 | } |
| 4626 | |
| 4627 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 4628 | { |
| 4629 | if (is_guest_mode(vcpu)) { |
| 4630 | if (kvm_set_cr4(vcpu, (val & vcpu->arch.cr4_guest_owned_bits) | |
| 4631 | (vcpu->arch.cr4 & ~vcpu->arch.cr4_guest_owned_bits))) |
| 4632 | return 1; |
| 4633 | vmcs_writel(CR4_READ_SHADOW, val); |
| 4634 | return 0; |
| 4635 | } else |
| 4636 | return kvm_set_cr4(vcpu, val); |
| 4637 | } |
| 4638 | |
| 4639 | /* called to set cr0 as approriate for clts instruction exit. */ |
| 4640 | static void handle_clts(struct kvm_vcpu *vcpu) |
| 4641 | { |
| 4642 | if (is_guest_mode(vcpu)) { |
| 4643 | /* |
| 4644 | * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS |
| 4645 | * but we did (!fpu_active). We need to keep GUEST_CR0.TS on, |
| 4646 | * just pretend it's off (also in arch.cr0 for fpu_activate). |
| 4647 | */ |
| 4648 | vmcs_writel(CR0_READ_SHADOW, |
| 4649 | vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS); |
| 4650 | vcpu->arch.cr0 &= ~X86_CR0_TS; |
| 4651 | } else |
| 4652 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
| 4653 | } |
| 4654 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4655 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4656 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4657 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4658 | int cr; |
| 4659 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 4660 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4661 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4662 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4663 | cr = exit_qualification & 15; |
| 4664 | reg = (exit_qualification >> 8) & 15; |
| 4665 | switch ((exit_qualification >> 4) & 3) { |
| 4666 | case 0: /* mov to cr */ |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4667 | val = kvm_register_read(vcpu, reg); |
| 4668 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4669 | switch (cr) { |
| 4670 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4671 | err = handle_set_cr0(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4672 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4673 | return 1; |
| 4674 | case 3: |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 4675 | err = kvm_set_cr3(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4676 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4677 | return 1; |
| 4678 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4679 | err = handle_set_cr4(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4680 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4681 | return 1; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4682 | case 8: { |
| 4683 | u8 cr8_prev = kvm_get_cr8(vcpu); |
| 4684 | u8 cr8 = kvm_register_read(vcpu, reg); |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 4685 | err = kvm_set_cr8(vcpu, cr8); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4686 | kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4687 | if (irqchip_in_kernel(vcpu->kvm)) |
| 4688 | return 1; |
| 4689 | if (cr8_prev <= cr8) |
| 4690 | return 1; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4691 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4692 | return 0; |
| 4693 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 4694 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4695 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4696 | case 2: /* clts */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4697 | handle_clts(vcpu); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4698 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4699 | skip_emulated_instruction(vcpu); |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 4700 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4701 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4702 | case 1: /*mov from cr*/ |
| 4703 | switch (cr) { |
| 4704 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 4705 | val = kvm_read_cr3(vcpu); |
| 4706 | kvm_register_write(vcpu, reg, val); |
| 4707 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4708 | skip_emulated_instruction(vcpu); |
| 4709 | return 1; |
| 4710 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4711 | val = kvm_get_cr8(vcpu); |
| 4712 | kvm_register_write(vcpu, reg, val); |
| 4713 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4714 | skip_emulated_instruction(vcpu); |
| 4715 | return 1; |
| 4716 | } |
| 4717 | break; |
| 4718 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 4719 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4720 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 4721 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4722 | |
| 4723 | skip_emulated_instruction(vcpu); |
| 4724 | return 1; |
| 4725 | default: |
| 4726 | break; |
| 4727 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4728 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 4729 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4730 | (int)(exit_qualification >> 4) & 3, cr); |
| 4731 | return 0; |
| 4732 | } |
| 4733 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4734 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4735 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4736 | unsigned long exit_qualification; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4737 | int dr, reg; |
| 4738 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 4739 | /* Do not handle if the CPL > 0, will trigger GP on re-entry */ |
Avi Kivity | 0a79b00 | 2009-09-01 12:03:25 +0300 | [diff] [blame] | 4740 | if (!kvm_require_cpl(vcpu, 0)) |
| 4741 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4742 | dr = vmcs_readl(GUEST_DR7); |
| 4743 | if (dr & DR7_GD) { |
| 4744 | /* |
| 4745 | * As the vm-exit takes precedence over the debug trap, we |
| 4746 | * need to emulate the latter, either for the host or the |
| 4747 | * guest debugging itself. |
| 4748 | */ |
| 4749 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4750 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
| 4751 | vcpu->run->debug.arch.dr7 = dr; |
| 4752 | vcpu->run->debug.arch.pc = |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4753 | vmcs_readl(GUEST_CS_BASE) + |
| 4754 | vmcs_readl(GUEST_RIP); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4755 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 4756 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4757 | return 0; |
| 4758 | } else { |
| 4759 | vcpu->arch.dr7 &= ~DR7_GD; |
| 4760 | vcpu->arch.dr6 |= DR6_BD; |
| 4761 | vmcs_writel(GUEST_DR7, vcpu->arch.dr7); |
| 4762 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 4763 | return 1; |
| 4764 | } |
| 4765 | } |
| 4766 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4767 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4768 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 4769 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 4770 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4771 | unsigned long val; |
| 4772 | if (!kvm_get_dr(vcpu, dr, &val)) |
| 4773 | kvm_register_write(vcpu, reg, val); |
| 4774 | } else |
| 4775 | kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4776 | skip_emulated_instruction(vcpu); |
| 4777 | return 1; |
| 4778 | } |
| 4779 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4780 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 4781 | { |
| 4782 | vmcs_writel(GUEST_DR7, val); |
| 4783 | } |
| 4784 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4785 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4786 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 4787 | kvm_emulate_cpuid(vcpu); |
| 4788 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4789 | } |
| 4790 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4791 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4792 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4793 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4794 | u64 data; |
| 4795 | |
| 4796 | if (vmx_get_msr(vcpu, ecx, &data)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4797 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4798 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4799 | return 1; |
| 4800 | } |
| 4801 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4802 | trace_kvm_msr_read(ecx, data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4803 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4804 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4805 | vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u; |
| 4806 | vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4807 | skip_emulated_instruction(vcpu); |
| 4808 | return 1; |
| 4809 | } |
| 4810 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4811 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4812 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4813 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4814 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 4815 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 4816 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4817 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4818 | msr.data = data; |
| 4819 | msr.index = ecx; |
| 4820 | msr.host_initiated = false; |
| 4821 | if (vmx_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4822 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4823 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4824 | return 1; |
| 4825 | } |
| 4826 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4827 | trace_kvm_msr_write(ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4828 | skip_emulated_instruction(vcpu); |
| 4829 | return 1; |
| 4830 | } |
| 4831 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4832 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4833 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4834 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4835 | return 1; |
| 4836 | } |
| 4837 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4838 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4839 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4840 | u32 cpu_based_vm_exec_control; |
| 4841 | |
| 4842 | /* clear pending irq */ |
| 4843 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4844 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 4845 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4846 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4847 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4848 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 4849 | ++vcpu->stat.irq_window_exits; |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4850 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4851 | /* |
| 4852 | * If the user space waits to inject interrupts, exit as soon as |
| 4853 | * possible |
| 4854 | */ |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 4855 | if (!irqchip_in_kernel(vcpu->kvm) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4856 | vcpu->run->request_interrupt_window && |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 4857 | !kvm_cpu_has_interrupt(vcpu)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4858 | vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4859 | return 0; |
| 4860 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4861 | return 1; |
| 4862 | } |
| 4863 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4864 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4865 | { |
| 4866 | skip_emulated_instruction(vcpu); |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 4867 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4868 | } |
| 4869 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4870 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4871 | { |
Dor Laor | 510043d | 2007-02-19 18:25:43 +0200 | [diff] [blame] | 4872 | skip_emulated_instruction(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4873 | kvm_emulate_hypercall(vcpu); |
| 4874 | return 1; |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4875 | } |
| 4876 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 4877 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 4878 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4879 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 4880 | } |
| 4881 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4882 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4883 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4884 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4885 | |
| 4886 | kvm_mmu_invlpg(vcpu, exit_qualification); |
| 4887 | skip_emulated_instruction(vcpu); |
| 4888 | return 1; |
| 4889 | } |
| 4890 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 4891 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 4892 | { |
| 4893 | int err; |
| 4894 | |
| 4895 | err = kvm_rdpmc(vcpu); |
| 4896 | kvm_complete_insn_gp(vcpu, err); |
| 4897 | |
| 4898 | return 1; |
| 4899 | } |
| 4900 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4901 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 4902 | { |
| 4903 | skip_emulated_instruction(vcpu); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 4904 | kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 4905 | return 1; |
| 4906 | } |
| 4907 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 4908 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 4909 | { |
| 4910 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 4911 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4912 | |
| 4913 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
| 4914 | skip_emulated_instruction(vcpu); |
| 4915 | return 1; |
| 4916 | } |
| 4917 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4918 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4919 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 4920 | if (likely(fasteoi)) { |
| 4921 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4922 | int access_type, offset; |
| 4923 | |
| 4924 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 4925 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 4926 | /* |
| 4927 | * Sane guest uses MOV to write EOI, with written value |
| 4928 | * not cared. So make a short-circuit here by avoiding |
| 4929 | * heavy instruction emulation. |
| 4930 | */ |
| 4931 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 4932 | (offset == APIC_EOI)) { |
| 4933 | kvm_lapic_set_eoi(vcpu); |
| 4934 | skip_emulated_instruction(vcpu); |
| 4935 | return 1; |
| 4936 | } |
| 4937 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4938 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4939 | } |
| 4940 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4941 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 4942 | { |
| 4943 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4944 | int vector = exit_qualification & 0xff; |
| 4945 | |
| 4946 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 4947 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 4948 | return 1; |
| 4949 | } |
| 4950 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4951 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 4952 | { |
| 4953 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4954 | u32 offset = exit_qualification & 0xfff; |
| 4955 | |
| 4956 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 4957 | kvm_apic_write_nodecode(vcpu, offset); |
| 4958 | return 1; |
| 4959 | } |
| 4960 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4961 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4962 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 4963 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4964 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 4965 | bool has_error_code = false; |
| 4966 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4967 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 4968 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4969 | |
| 4970 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 4971 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4972 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4973 | |
| 4974 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4975 | |
| 4976 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4977 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 4978 | switch (type) { |
| 4979 | case INTR_TYPE_NMI_INTR: |
| 4980 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 4981 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4982 | break; |
| 4983 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4984 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4985 | kvm_clear_interrupt_queue(vcpu); |
| 4986 | break; |
| 4987 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 4988 | if (vmx->idt_vectoring_info & |
| 4989 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 4990 | has_error_code = true; |
| 4991 | error_code = |
| 4992 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 4993 | } |
| 4994 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4995 | case INTR_TYPE_SOFT_EXCEPTION: |
| 4996 | kvm_clear_exception_queue(vcpu); |
| 4997 | break; |
| 4998 | default: |
| 4999 | break; |
| 5000 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 5001 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5002 | tss_selector = exit_qualification; |
| 5003 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5004 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 5005 | type != INTR_TYPE_EXT_INTR && |
| 5006 | type != INTR_TYPE_NMI_INTR)) |
| 5007 | skip_emulated_instruction(vcpu); |
| 5008 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 5009 | if (kvm_task_switch(vcpu, tss_selector, |
| 5010 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 5011 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 5012 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 5013 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 5014 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5015 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 5016 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5017 | |
| 5018 | /* clear all local breakpoint enable flags */ |
| 5019 | vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55); |
| 5020 | |
| 5021 | /* |
| 5022 | * TODO: What about debug traps on tss switch? |
| 5023 | * Are we supposed to inject them and update dr6? |
| 5024 | */ |
| 5025 | |
| 5026 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5027 | } |
| 5028 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5029 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5030 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 5031 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5032 | gpa_t gpa; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 5033 | u32 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5034 | int gla_validity; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5035 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 5036 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5037 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5038 | gla_validity = (exit_qualification >> 7) & 0x3; |
| 5039 | if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) { |
| 5040 | printk(KERN_ERR "EPT: Handling EPT violation failed!\n"); |
| 5041 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 5042 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 5043 | vmcs_readl(GUEST_LINEAR_ADDRESS)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5044 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 5045 | (long unsigned int)exit_qualification); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5046 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 5047 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION; |
Avi Kivity | 596ae89 | 2009-06-03 14:12:10 +0300 | [diff] [blame] | 5048 | return 0; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5052 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 5053 | |
| 5054 | /* It is a write fault? */ |
| 5055 | error_code = exit_qualification & (1U << 1); |
| 5056 | /* ept page table is present? */ |
| 5057 | error_code |= (exit_qualification >> 3) & 0x1; |
| 5058 | |
| 5059 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5060 | } |
| 5061 | |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5062 | static u64 ept_rsvd_mask(u64 spte, int level) |
| 5063 | { |
| 5064 | int i; |
| 5065 | u64 mask = 0; |
| 5066 | |
| 5067 | for (i = 51; i > boot_cpu_data.x86_phys_bits; i--) |
| 5068 | mask |= (1ULL << i); |
| 5069 | |
| 5070 | if (level > 2) |
| 5071 | /* bits 7:3 reserved */ |
| 5072 | mask |= 0xf8; |
| 5073 | else if (level == 2) { |
| 5074 | if (spte & (1ULL << 7)) |
| 5075 | /* 2MB ref, bits 20:12 reserved */ |
| 5076 | mask |= 0x1ff000; |
| 5077 | else |
| 5078 | /* bits 6:3 reserved */ |
| 5079 | mask |= 0x78; |
| 5080 | } |
| 5081 | |
| 5082 | return mask; |
| 5083 | } |
| 5084 | |
| 5085 | static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte, |
| 5086 | int level) |
| 5087 | { |
| 5088 | printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level); |
| 5089 | |
| 5090 | /* 010b (write-only) */ |
| 5091 | WARN_ON((spte & 0x7) == 0x2); |
| 5092 | |
| 5093 | /* 110b (write/execute) */ |
| 5094 | WARN_ON((spte & 0x7) == 0x6); |
| 5095 | |
| 5096 | /* 100b (execute-only) and value not supported by logical processor */ |
| 5097 | if (!cpu_has_vmx_ept_execute_only()) |
| 5098 | WARN_ON((spte & 0x7) == 0x4); |
| 5099 | |
| 5100 | /* not 000b */ |
| 5101 | if ((spte & 0x7)) { |
| 5102 | u64 rsvd_bits = spte & ept_rsvd_mask(spte, level); |
| 5103 | |
| 5104 | if (rsvd_bits != 0) { |
| 5105 | printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n", |
| 5106 | __func__, rsvd_bits); |
| 5107 | WARN_ON(1); |
| 5108 | } |
| 5109 | |
| 5110 | if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) { |
| 5111 | u64 ept_mem_type = (spte & 0x38) >> 3; |
| 5112 | |
| 5113 | if (ept_mem_type == 2 || ept_mem_type == 3 || |
| 5114 | ept_mem_type == 7) { |
| 5115 | printk(KERN_ERR "%s: ept_mem_type=0x%llx\n", |
| 5116 | __func__, ept_mem_type); |
| 5117 | WARN_ON(1); |
| 5118 | } |
| 5119 | } |
| 5120 | } |
| 5121 | } |
| 5122 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5123 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5124 | { |
| 5125 | u64 sptes[4]; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5126 | int nr_sptes, i, ret; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5127 | gpa_t gpa; |
| 5128 | |
| 5129 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 5130 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5131 | ret = handle_mmio_page_fault_common(vcpu, gpa, true); |
| 5132 | if (likely(ret == 1)) |
| 5133 | return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) == |
| 5134 | EMULATE_DONE; |
| 5135 | if (unlikely(!ret)) |
| 5136 | return 1; |
| 5137 | |
| 5138 | /* It is the real ept misconfig */ |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5139 | printk(KERN_ERR "EPT: Misconfiguration.\n"); |
| 5140 | printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa); |
| 5141 | |
| 5142 | nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes); |
| 5143 | |
| 5144 | for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i) |
| 5145 | ept_misconfig_inspect_spte(vcpu, sptes[i-1], i); |
| 5146 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5147 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 5148 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5149 | |
| 5150 | return 0; |
| 5151 | } |
| 5152 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5153 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5154 | { |
| 5155 | u32 cpu_based_vm_exec_control; |
| 5156 | |
| 5157 | /* clear pending NMI */ |
| 5158 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5159 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 5160 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 5161 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5162 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5163 | |
| 5164 | return 1; |
| 5165 | } |
| 5166 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5167 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5168 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 5169 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5170 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5171 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 5172 | u32 cpu_exec_ctrl; |
| 5173 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 5174 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 5175 | |
| 5176 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5177 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5178 | |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 5179 | while (!guest_state_valid(vcpu) && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 5180 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 5181 | return handle_interrupt_window(&vmx->vcpu); |
| 5182 | |
Avi Kivity | de87dcdd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 5183 | if (test_bit(KVM_REQ_EVENT, &vcpu->requests)) |
| 5184 | return 1; |
| 5185 | |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5186 | err = emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5187 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5188 | if (err == EMULATE_DO_MMIO) { |
| 5189 | ret = 0; |
| 5190 | goto out; |
| 5191 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 5192 | |
Avi Kivity | de5f70e | 2012-06-12 20:22:28 +0300 | [diff] [blame] | 5193 | if (err != EMULATE_DONE) { |
| 5194 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 5195 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 5196 | vcpu->run->internal.ndata = 0; |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 5197 | return 0; |
Avi Kivity | de5f70e | 2012-06-12 20:22:28 +0300 | [diff] [blame] | 5198 | } |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5199 | |
| 5200 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5201 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5202 | if (need_resched()) |
| 5203 | schedule(); |
| 5204 | } |
| 5205 | |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 5206 | vmx->emulation_required = emulation_required(vcpu); |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5207 | out: |
| 5208 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5209 | } |
| 5210 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5211 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5212 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 5213 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 5214 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 5215 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5216 | { |
| 5217 | skip_emulated_instruction(vcpu); |
| 5218 | kvm_vcpu_on_spin(vcpu); |
| 5219 | |
| 5220 | return 1; |
| 5221 | } |
| 5222 | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 5223 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 5224 | { |
| 5225 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5226 | return 1; |
| 5227 | } |
| 5228 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5229 | /* |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 5230 | * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12. |
| 5231 | * We could reuse a single VMCS for all the L2 guests, but we also want the |
| 5232 | * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this |
| 5233 | * allows keeping them loaded on the processor, and in the future will allow |
| 5234 | * optimizations where prepare_vmcs02 doesn't need to set all the fields on |
| 5235 | * every entry if they never change. |
| 5236 | * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE |
| 5237 | * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first. |
| 5238 | * |
| 5239 | * The following functions allocate and free a vmcs02 in this pool. |
| 5240 | */ |
| 5241 | |
| 5242 | /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */ |
| 5243 | static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx) |
| 5244 | { |
| 5245 | struct vmcs02_list *item; |
| 5246 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 5247 | if (item->vmptr == vmx->nested.current_vmptr) { |
| 5248 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 5249 | return &item->vmcs02; |
| 5250 | } |
| 5251 | |
| 5252 | if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) { |
| 5253 | /* Recycle the least recently used VMCS. */ |
| 5254 | item = list_entry(vmx->nested.vmcs02_pool.prev, |
| 5255 | struct vmcs02_list, list); |
| 5256 | item->vmptr = vmx->nested.current_vmptr; |
| 5257 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 5258 | return &item->vmcs02; |
| 5259 | } |
| 5260 | |
| 5261 | /* Create a new VMCS */ |
| 5262 | item = (struct vmcs02_list *) |
| 5263 | kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL); |
| 5264 | if (!item) |
| 5265 | return NULL; |
| 5266 | item->vmcs02.vmcs = alloc_vmcs(); |
| 5267 | if (!item->vmcs02.vmcs) { |
| 5268 | kfree(item); |
| 5269 | return NULL; |
| 5270 | } |
| 5271 | loaded_vmcs_init(&item->vmcs02); |
| 5272 | item->vmptr = vmx->nested.current_vmptr; |
| 5273 | list_add(&(item->list), &(vmx->nested.vmcs02_pool)); |
| 5274 | vmx->nested.vmcs02_num++; |
| 5275 | return &item->vmcs02; |
| 5276 | } |
| 5277 | |
| 5278 | /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */ |
| 5279 | static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 5280 | { |
| 5281 | struct vmcs02_list *item; |
| 5282 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 5283 | if (item->vmptr == vmptr) { |
| 5284 | free_loaded_vmcs(&item->vmcs02); |
| 5285 | list_del(&item->list); |
| 5286 | kfree(item); |
| 5287 | vmx->nested.vmcs02_num--; |
| 5288 | return; |
| 5289 | } |
| 5290 | } |
| 5291 | |
| 5292 | /* |
| 5293 | * Free all VMCSs saved for this vcpu, except the one pointed by |
| 5294 | * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one |
| 5295 | * currently used, if running L2), and vmcs01 when running L2. |
| 5296 | */ |
| 5297 | static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx) |
| 5298 | { |
| 5299 | struct vmcs02_list *item, *n; |
| 5300 | list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) { |
| 5301 | if (vmx->loaded_vmcs != &item->vmcs02) |
| 5302 | free_loaded_vmcs(&item->vmcs02); |
| 5303 | list_del(&item->list); |
| 5304 | kfree(item); |
| 5305 | } |
| 5306 | vmx->nested.vmcs02_num = 0; |
| 5307 | |
| 5308 | if (vmx->loaded_vmcs != &vmx->vmcs01) |
| 5309 | free_loaded_vmcs(&vmx->vmcs01); |
| 5310 | } |
| 5311 | |
| 5312 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 5313 | * Emulate the VMXON instruction. |
| 5314 | * Currently, we just remember that VMX is active, and do not save or even |
| 5315 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 5316 | * do not currently need to store anything in that guest-allocated memory |
| 5317 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 5318 | * argument is different from the VMXON pointer (which the spec says they do). |
| 5319 | */ |
| 5320 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 5321 | { |
| 5322 | struct kvm_segment cs; |
| 5323 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5324 | |
| 5325 | /* The Intel VMX Instruction Reference lists a bunch of bits that |
| 5326 | * are prerequisite to running VMXON, most notably cr4.VMXE must be |
| 5327 | * set to 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 5328 | * Otherwise, we should fail with #UD. We test these now: |
| 5329 | */ |
| 5330 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) || |
| 5331 | !kvm_read_cr0_bits(vcpu, X86_CR0_PE) || |
| 5332 | (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
| 5333 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5334 | return 1; |
| 5335 | } |
| 5336 | |
| 5337 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 5338 | if (is_long_mode(vcpu) && !cs.l) { |
| 5339 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5340 | return 1; |
| 5341 | } |
| 5342 | |
| 5343 | if (vmx_get_cpl(vcpu)) { |
| 5344 | kvm_inject_gp(vcpu, 0); |
| 5345 | return 1; |
| 5346 | } |
| 5347 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 5348 | INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool)); |
| 5349 | vmx->nested.vmcs02_num = 0; |
| 5350 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 5351 | vmx->nested.vmxon = true; |
| 5352 | |
| 5353 | skip_emulated_instruction(vcpu); |
| 5354 | return 1; |
| 5355 | } |
| 5356 | |
| 5357 | /* |
| 5358 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 5359 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 5360 | * slightly different). It also specifies what exception to inject otherwise. |
| 5361 | */ |
| 5362 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 5363 | { |
| 5364 | struct kvm_segment cs; |
| 5365 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5366 | |
| 5367 | if (!vmx->nested.vmxon) { |
| 5368 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5369 | return 0; |
| 5370 | } |
| 5371 | |
| 5372 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 5373 | if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) || |
| 5374 | (is_long_mode(vcpu) && !cs.l)) { |
| 5375 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5376 | return 0; |
| 5377 | } |
| 5378 | |
| 5379 | if (vmx_get_cpl(vcpu)) { |
| 5380 | kvm_inject_gp(vcpu, 0); |
| 5381 | return 0; |
| 5382 | } |
| 5383 | |
| 5384 | return 1; |
| 5385 | } |
| 5386 | |
| 5387 | /* |
| 5388 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 5389 | * just stops using VMX. |
| 5390 | */ |
| 5391 | static void free_nested(struct vcpu_vmx *vmx) |
| 5392 | { |
| 5393 | if (!vmx->nested.vmxon) |
| 5394 | return; |
| 5395 | vmx->nested.vmxon = false; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 5396 | if (vmx->nested.current_vmptr != -1ull) { |
| 5397 | kunmap(vmx->nested.current_vmcs12_page); |
| 5398 | nested_release_page(vmx->nested.current_vmcs12_page); |
| 5399 | vmx->nested.current_vmptr = -1ull; |
| 5400 | vmx->nested.current_vmcs12 = NULL; |
| 5401 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 5402 | /* Unpin physical memory we referred to in current vmcs02 */ |
| 5403 | if (vmx->nested.apic_access_page) { |
| 5404 | nested_release_page(vmx->nested.apic_access_page); |
| 5405 | vmx->nested.apic_access_page = 0; |
| 5406 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 5407 | |
| 5408 | nested_free_all_saved_vmcss(vmx); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 5409 | } |
| 5410 | |
| 5411 | /* Emulate the VMXOFF instruction */ |
| 5412 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 5413 | { |
| 5414 | if (!nested_vmx_check_permission(vcpu)) |
| 5415 | return 1; |
| 5416 | free_nested(to_vmx(vcpu)); |
| 5417 | skip_emulated_instruction(vcpu); |
| 5418 | return 1; |
| 5419 | } |
| 5420 | |
| 5421 | /* |
Nadav Har'El | 064aea7 | 2011-05-25 23:04:56 +0300 | [diff] [blame] | 5422 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 5423 | * exit caused by such an instruction (run by a guest hypervisor). |
| 5424 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 5425 | * #UD or #GP. |
| 5426 | */ |
| 5427 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 5428 | unsigned long exit_qualification, |
| 5429 | u32 vmx_instruction_info, gva_t *ret) |
| 5430 | { |
| 5431 | /* |
| 5432 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 5433 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 5434 | * addressing components of the operand. Only the displacement part |
| 5435 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 5436 | * For how an actual address is calculated from all these components, |
| 5437 | * refer to Vol. 1, "Operand Addressing". |
| 5438 | */ |
| 5439 | int scaling = vmx_instruction_info & 3; |
| 5440 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 5441 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 5442 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 5443 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 5444 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 5445 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 5446 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 5447 | |
| 5448 | if (is_reg) { |
| 5449 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5450 | return 1; |
| 5451 | } |
| 5452 | |
| 5453 | /* Addr = segment_base + offset */ |
| 5454 | /* offset = base + [index * scale] + displacement */ |
| 5455 | *ret = vmx_get_segment_base(vcpu, seg_reg); |
| 5456 | if (base_is_valid) |
| 5457 | *ret += kvm_register_read(vcpu, base_reg); |
| 5458 | if (index_is_valid) |
| 5459 | *ret += kvm_register_read(vcpu, index_reg)<<scaling; |
| 5460 | *ret += exit_qualification; /* holds the displacement */ |
| 5461 | |
| 5462 | if (addr_size == 1) /* 32 bit */ |
| 5463 | *ret &= 0xffffffff; |
| 5464 | |
| 5465 | /* |
| 5466 | * TODO: throw #GP (and return 1) in various cases that the VM* |
| 5467 | * instructions require it - e.g., offset beyond segment limit, |
| 5468 | * unusable or unreadable/unwritable segment, non-canonical 64-bit |
| 5469 | * address, and so on. Currently these are not checked. |
| 5470 | */ |
| 5471 | return 0; |
| 5472 | } |
| 5473 | |
| 5474 | /* |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 5475 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 5476 | * set the success or error code of an emulated VMX instruction, as specified |
| 5477 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 5478 | */ |
| 5479 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 5480 | { |
| 5481 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 5482 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 5483 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 5484 | } |
| 5485 | |
| 5486 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 5487 | { |
| 5488 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 5489 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 5490 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 5491 | | X86_EFLAGS_CF); |
| 5492 | } |
| 5493 | |
| 5494 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
| 5495 | u32 vm_instruction_error) |
| 5496 | { |
| 5497 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 5498 | /* |
| 5499 | * failValid writes the error number to the current VMCS, which |
| 5500 | * can't be done there isn't a current VMCS. |
| 5501 | */ |
| 5502 | nested_vmx_failInvalid(vcpu); |
| 5503 | return; |
| 5504 | } |
| 5505 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 5506 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 5507 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 5508 | | X86_EFLAGS_ZF); |
| 5509 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 5510 | } |
| 5511 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 5512 | /* Emulate the VMCLEAR instruction */ |
| 5513 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 5514 | { |
| 5515 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5516 | gva_t gva; |
| 5517 | gpa_t vmptr; |
| 5518 | struct vmcs12 *vmcs12; |
| 5519 | struct page *page; |
| 5520 | struct x86_exception e; |
| 5521 | |
| 5522 | if (!nested_vmx_check_permission(vcpu)) |
| 5523 | return 1; |
| 5524 | |
| 5525 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 5526 | vmcs_read32(VMX_INSTRUCTION_INFO), &gva)) |
| 5527 | return 1; |
| 5528 | |
| 5529 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr, |
| 5530 | sizeof(vmptr), &e)) { |
| 5531 | kvm_inject_page_fault(vcpu, &e); |
| 5532 | return 1; |
| 5533 | } |
| 5534 | |
| 5535 | if (!IS_ALIGNED(vmptr, PAGE_SIZE)) { |
| 5536 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 5537 | skip_emulated_instruction(vcpu); |
| 5538 | return 1; |
| 5539 | } |
| 5540 | |
| 5541 | if (vmptr == vmx->nested.current_vmptr) { |
| 5542 | kunmap(vmx->nested.current_vmcs12_page); |
| 5543 | nested_release_page(vmx->nested.current_vmcs12_page); |
| 5544 | vmx->nested.current_vmptr = -1ull; |
| 5545 | vmx->nested.current_vmcs12 = NULL; |
| 5546 | } |
| 5547 | |
| 5548 | page = nested_get_page(vcpu, vmptr); |
| 5549 | if (page == NULL) { |
| 5550 | /* |
| 5551 | * For accurate processor emulation, VMCLEAR beyond available |
| 5552 | * physical memory should do nothing at all. However, it is |
| 5553 | * possible that a nested vmx bug, not a guest hypervisor bug, |
| 5554 | * resulted in this case, so let's shut down before doing any |
| 5555 | * more damage: |
| 5556 | */ |
| 5557 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 5558 | return 1; |
| 5559 | } |
| 5560 | vmcs12 = kmap(page); |
| 5561 | vmcs12->launch_state = 0; |
| 5562 | kunmap(page); |
| 5563 | nested_release_page(page); |
| 5564 | |
| 5565 | nested_free_vmcs02(vmx, vmptr); |
| 5566 | |
| 5567 | skip_emulated_instruction(vcpu); |
| 5568 | nested_vmx_succeed(vcpu); |
| 5569 | return 1; |
| 5570 | } |
| 5571 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 5572 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 5573 | |
| 5574 | /* Emulate the VMLAUNCH instruction */ |
| 5575 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 5576 | { |
| 5577 | return nested_vmx_run(vcpu, true); |
| 5578 | } |
| 5579 | |
| 5580 | /* Emulate the VMRESUME instruction */ |
| 5581 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 5582 | { |
| 5583 | |
| 5584 | return nested_vmx_run(vcpu, false); |
| 5585 | } |
| 5586 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 5587 | enum vmcs_field_type { |
| 5588 | VMCS_FIELD_TYPE_U16 = 0, |
| 5589 | VMCS_FIELD_TYPE_U64 = 1, |
| 5590 | VMCS_FIELD_TYPE_U32 = 2, |
| 5591 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 |
| 5592 | }; |
| 5593 | |
| 5594 | static inline int vmcs_field_type(unsigned long field) |
| 5595 | { |
| 5596 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
| 5597 | return VMCS_FIELD_TYPE_U32; |
| 5598 | return (field >> 13) & 0x3 ; |
| 5599 | } |
| 5600 | |
| 5601 | static inline int vmcs_field_readonly(unsigned long field) |
| 5602 | { |
| 5603 | return (((field >> 10) & 0x3) == 1); |
| 5604 | } |
| 5605 | |
| 5606 | /* |
| 5607 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 5608 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 5609 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 5610 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 5611 | * 64-bit fields are to be returned). |
| 5612 | */ |
| 5613 | static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu, |
| 5614 | unsigned long field, u64 *ret) |
| 5615 | { |
| 5616 | short offset = vmcs_field_to_offset(field); |
| 5617 | char *p; |
| 5618 | |
| 5619 | if (offset < 0) |
| 5620 | return 0; |
| 5621 | |
| 5622 | p = ((char *)(get_vmcs12(vcpu))) + offset; |
| 5623 | |
| 5624 | switch (vmcs_field_type(field)) { |
| 5625 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 5626 | *ret = *((natural_width *)p); |
| 5627 | return 1; |
| 5628 | case VMCS_FIELD_TYPE_U16: |
| 5629 | *ret = *((u16 *)p); |
| 5630 | return 1; |
| 5631 | case VMCS_FIELD_TYPE_U32: |
| 5632 | *ret = *((u32 *)p); |
| 5633 | return 1; |
| 5634 | case VMCS_FIELD_TYPE_U64: |
| 5635 | *ret = *((u64 *)p); |
| 5636 | return 1; |
| 5637 | default: |
| 5638 | return 0; /* can never happen. */ |
| 5639 | } |
| 5640 | } |
| 5641 | |
| 5642 | /* |
| 5643 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 5644 | * used before) all generate the same failure when it is missing. |
| 5645 | */ |
| 5646 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 5647 | { |
| 5648 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5649 | if (vmx->nested.current_vmptr == -1ull) { |
| 5650 | nested_vmx_failInvalid(vcpu); |
| 5651 | skip_emulated_instruction(vcpu); |
| 5652 | return 0; |
| 5653 | } |
| 5654 | return 1; |
| 5655 | } |
| 5656 | |
| 5657 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 5658 | { |
| 5659 | unsigned long field; |
| 5660 | u64 field_value; |
| 5661 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5662 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5663 | gva_t gva = 0; |
| 5664 | |
| 5665 | if (!nested_vmx_check_permission(vcpu) || |
| 5666 | !nested_vmx_check_vmcs12(vcpu)) |
| 5667 | return 1; |
| 5668 | |
| 5669 | /* Decode instruction info and find the field to read */ |
| 5670 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5671 | /* Read the field, zero-extended to a u64 field_value */ |
| 5672 | if (!vmcs12_read_any(vcpu, field, &field_value)) { |
| 5673 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 5674 | skip_emulated_instruction(vcpu); |
| 5675 | return 1; |
| 5676 | } |
| 5677 | /* |
| 5678 | * Now copy part of this value to register or memory, as requested. |
| 5679 | * Note that the number of bits actually copied is 32 or 64 depending |
| 5680 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 5681 | */ |
| 5682 | if (vmx_instruction_info & (1u << 10)) { |
| 5683 | kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
| 5684 | field_value); |
| 5685 | } else { |
| 5686 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 5687 | vmx_instruction_info, &gva)) |
| 5688 | return 1; |
| 5689 | /* _system ok, as nested_vmx_check_permission verified cpl=0 */ |
| 5690 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
| 5691 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); |
| 5692 | } |
| 5693 | |
| 5694 | nested_vmx_succeed(vcpu); |
| 5695 | skip_emulated_instruction(vcpu); |
| 5696 | return 1; |
| 5697 | } |
| 5698 | |
| 5699 | |
| 5700 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 5701 | { |
| 5702 | unsigned long field; |
| 5703 | gva_t gva; |
| 5704 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5705 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5706 | char *p; |
| 5707 | short offset; |
| 5708 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 5709 | * mode, and eventually we need to write that into a field of several |
| 5710 | * possible lengths. The code below first zero-extends the value to 64 |
| 5711 | * bit (field_value), and then copies only the approriate number of |
| 5712 | * bits into the vmcs12 field. |
| 5713 | */ |
| 5714 | u64 field_value = 0; |
| 5715 | struct x86_exception e; |
| 5716 | |
| 5717 | if (!nested_vmx_check_permission(vcpu) || |
| 5718 | !nested_vmx_check_vmcs12(vcpu)) |
| 5719 | return 1; |
| 5720 | |
| 5721 | if (vmx_instruction_info & (1u << 10)) |
| 5722 | field_value = kvm_register_read(vcpu, |
| 5723 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 5724 | else { |
| 5725 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 5726 | vmx_instruction_info, &gva)) |
| 5727 | return 1; |
| 5728 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, |
| 5729 | &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) { |
| 5730 | kvm_inject_page_fault(vcpu, &e); |
| 5731 | return 1; |
| 5732 | } |
| 5733 | } |
| 5734 | |
| 5735 | |
| 5736 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5737 | if (vmcs_field_readonly(field)) { |
| 5738 | nested_vmx_failValid(vcpu, |
| 5739 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
| 5740 | skip_emulated_instruction(vcpu); |
| 5741 | return 1; |
| 5742 | } |
| 5743 | |
| 5744 | offset = vmcs_field_to_offset(field); |
| 5745 | if (offset < 0) { |
| 5746 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 5747 | skip_emulated_instruction(vcpu); |
| 5748 | return 1; |
| 5749 | } |
| 5750 | p = ((char *) get_vmcs12(vcpu)) + offset; |
| 5751 | |
| 5752 | switch (vmcs_field_type(field)) { |
| 5753 | case VMCS_FIELD_TYPE_U16: |
| 5754 | *(u16 *)p = field_value; |
| 5755 | break; |
| 5756 | case VMCS_FIELD_TYPE_U32: |
| 5757 | *(u32 *)p = field_value; |
| 5758 | break; |
| 5759 | case VMCS_FIELD_TYPE_U64: |
| 5760 | *(u64 *)p = field_value; |
| 5761 | break; |
| 5762 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 5763 | *(natural_width *)p = field_value; |
| 5764 | break; |
| 5765 | default: |
| 5766 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 5767 | skip_emulated_instruction(vcpu); |
| 5768 | return 1; |
| 5769 | } |
| 5770 | |
| 5771 | nested_vmx_succeed(vcpu); |
| 5772 | skip_emulated_instruction(vcpu); |
| 5773 | return 1; |
| 5774 | } |
| 5775 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 5776 | /* Emulate the VMPTRLD instruction */ |
| 5777 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 5778 | { |
| 5779 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5780 | gva_t gva; |
| 5781 | gpa_t vmptr; |
| 5782 | struct x86_exception e; |
| 5783 | |
| 5784 | if (!nested_vmx_check_permission(vcpu)) |
| 5785 | return 1; |
| 5786 | |
| 5787 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 5788 | vmcs_read32(VMX_INSTRUCTION_INFO), &gva)) |
| 5789 | return 1; |
| 5790 | |
| 5791 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr, |
| 5792 | sizeof(vmptr), &e)) { |
| 5793 | kvm_inject_page_fault(vcpu, &e); |
| 5794 | return 1; |
| 5795 | } |
| 5796 | |
| 5797 | if (!IS_ALIGNED(vmptr, PAGE_SIZE)) { |
| 5798 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 5799 | skip_emulated_instruction(vcpu); |
| 5800 | return 1; |
| 5801 | } |
| 5802 | |
| 5803 | if (vmx->nested.current_vmptr != vmptr) { |
| 5804 | struct vmcs12 *new_vmcs12; |
| 5805 | struct page *page; |
| 5806 | page = nested_get_page(vcpu, vmptr); |
| 5807 | if (page == NULL) { |
| 5808 | nested_vmx_failInvalid(vcpu); |
| 5809 | skip_emulated_instruction(vcpu); |
| 5810 | return 1; |
| 5811 | } |
| 5812 | new_vmcs12 = kmap(page); |
| 5813 | if (new_vmcs12->revision_id != VMCS12_REVISION) { |
| 5814 | kunmap(page); |
| 5815 | nested_release_page_clean(page); |
| 5816 | nested_vmx_failValid(vcpu, |
| 5817 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5818 | skip_emulated_instruction(vcpu); |
| 5819 | return 1; |
| 5820 | } |
| 5821 | if (vmx->nested.current_vmptr != -1ull) { |
| 5822 | kunmap(vmx->nested.current_vmcs12_page); |
| 5823 | nested_release_page(vmx->nested.current_vmcs12_page); |
| 5824 | } |
| 5825 | |
| 5826 | vmx->nested.current_vmptr = vmptr; |
| 5827 | vmx->nested.current_vmcs12 = new_vmcs12; |
| 5828 | vmx->nested.current_vmcs12_page = page; |
| 5829 | } |
| 5830 | |
| 5831 | nested_vmx_succeed(vcpu); |
| 5832 | skip_emulated_instruction(vcpu); |
| 5833 | return 1; |
| 5834 | } |
| 5835 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 5836 | /* Emulate the VMPTRST instruction */ |
| 5837 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 5838 | { |
| 5839 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5840 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5841 | gva_t vmcs_gva; |
| 5842 | struct x86_exception e; |
| 5843 | |
| 5844 | if (!nested_vmx_check_permission(vcpu)) |
| 5845 | return 1; |
| 5846 | |
| 5847 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 5848 | vmx_instruction_info, &vmcs_gva)) |
| 5849 | return 1; |
| 5850 | /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */ |
| 5851 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
| 5852 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 5853 | sizeof(u64), &e)) { |
| 5854 | kvm_inject_page_fault(vcpu, &e); |
| 5855 | return 1; |
| 5856 | } |
| 5857 | nested_vmx_succeed(vcpu); |
| 5858 | skip_emulated_instruction(vcpu); |
| 5859 | return 1; |
| 5860 | } |
| 5861 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 5862 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5863 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 5864 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 5865 | * to be done to userspace and return 0. |
| 5866 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5867 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5868 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 5869 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5870 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5871 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5872 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5873 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 5874 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 5875 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 5876 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 5877 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 5878 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 5879 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 5880 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5881 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 5882 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5883 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 5884 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 5885 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 5886 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 5887 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 5888 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 5889 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 5890 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 5891 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 5892 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5893 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 5894 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 5895 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5896 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 5897 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 5898 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5899 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5900 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5901 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 5902 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5903 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 5904 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op, |
| 5905 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5906 | }; |
| 5907 | |
| 5908 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 5909 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5910 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 5911 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 5912 | struct vmcs12 *vmcs12) |
| 5913 | { |
| 5914 | unsigned long exit_qualification; |
| 5915 | gpa_t bitmap, last_bitmap; |
| 5916 | unsigned int port; |
| 5917 | int size; |
| 5918 | u8 b; |
| 5919 | |
| 5920 | if (nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING)) |
| 5921 | return 1; |
| 5922 | |
| 5923 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 5924 | return 0; |
| 5925 | |
| 5926 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5927 | |
| 5928 | port = exit_qualification >> 16; |
| 5929 | size = (exit_qualification & 7) + 1; |
| 5930 | |
| 5931 | last_bitmap = (gpa_t)-1; |
| 5932 | b = -1; |
| 5933 | |
| 5934 | while (size > 0) { |
| 5935 | if (port < 0x8000) |
| 5936 | bitmap = vmcs12->io_bitmap_a; |
| 5937 | else if (port < 0x10000) |
| 5938 | bitmap = vmcs12->io_bitmap_b; |
| 5939 | else |
| 5940 | return 1; |
| 5941 | bitmap += (port & 0x7fff) / 8; |
| 5942 | |
| 5943 | if (last_bitmap != bitmap) |
| 5944 | if (kvm_read_guest(vcpu->kvm, bitmap, &b, 1)) |
| 5945 | return 1; |
| 5946 | if (b & (1 << (port & 7))) |
| 5947 | return 1; |
| 5948 | |
| 5949 | port++; |
| 5950 | size--; |
| 5951 | last_bitmap = bitmap; |
| 5952 | } |
| 5953 | |
| 5954 | return 0; |
| 5955 | } |
| 5956 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 5957 | /* |
| 5958 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 5959 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 5960 | * disinterest in the current event (read or write a specific MSR) by using an |
| 5961 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 5962 | */ |
| 5963 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 5964 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 5965 | { |
| 5966 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 5967 | gpa_t bitmap; |
| 5968 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 5969 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 5970 | return 1; |
| 5971 | |
| 5972 | /* |
| 5973 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 5974 | * for the four combinations of read/write and low/high MSR numbers. |
| 5975 | * First we need to figure out which of the four to use: |
| 5976 | */ |
| 5977 | bitmap = vmcs12->msr_bitmap; |
| 5978 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 5979 | bitmap += 2048; |
| 5980 | if (msr_index >= 0xc0000000) { |
| 5981 | msr_index -= 0xc0000000; |
| 5982 | bitmap += 1024; |
| 5983 | } |
| 5984 | |
| 5985 | /* Then read the msr_index'th bit from this bitmap: */ |
| 5986 | if (msr_index < 1024*8) { |
| 5987 | unsigned char b; |
Jan Kiszka | bd31a7f | 2013-02-14 19:46:27 +0100 | [diff] [blame^] | 5988 | if (kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1)) |
| 5989 | return 1; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 5990 | return 1 & (b >> (msr_index & 7)); |
| 5991 | } else |
| 5992 | return 1; /* let L1 handle the wrong parameter */ |
| 5993 | } |
| 5994 | |
| 5995 | /* |
| 5996 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 5997 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 5998 | * intercept (via guest_host_mask etc.) the current event. |
| 5999 | */ |
| 6000 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 6001 | struct vmcs12 *vmcs12) |
| 6002 | { |
| 6003 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6004 | int cr = exit_qualification & 15; |
| 6005 | int reg = (exit_qualification >> 8) & 15; |
| 6006 | unsigned long val = kvm_register_read(vcpu, reg); |
| 6007 | |
| 6008 | switch ((exit_qualification >> 4) & 3) { |
| 6009 | case 0: /* mov to cr */ |
| 6010 | switch (cr) { |
| 6011 | case 0: |
| 6012 | if (vmcs12->cr0_guest_host_mask & |
| 6013 | (val ^ vmcs12->cr0_read_shadow)) |
| 6014 | return 1; |
| 6015 | break; |
| 6016 | case 3: |
| 6017 | if ((vmcs12->cr3_target_count >= 1 && |
| 6018 | vmcs12->cr3_target_value0 == val) || |
| 6019 | (vmcs12->cr3_target_count >= 2 && |
| 6020 | vmcs12->cr3_target_value1 == val) || |
| 6021 | (vmcs12->cr3_target_count >= 3 && |
| 6022 | vmcs12->cr3_target_value2 == val) || |
| 6023 | (vmcs12->cr3_target_count >= 4 && |
| 6024 | vmcs12->cr3_target_value3 == val)) |
| 6025 | return 0; |
| 6026 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
| 6027 | return 1; |
| 6028 | break; |
| 6029 | case 4: |
| 6030 | if (vmcs12->cr4_guest_host_mask & |
| 6031 | (vmcs12->cr4_read_shadow ^ val)) |
| 6032 | return 1; |
| 6033 | break; |
| 6034 | case 8: |
| 6035 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
| 6036 | return 1; |
| 6037 | break; |
| 6038 | } |
| 6039 | break; |
| 6040 | case 2: /* clts */ |
| 6041 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 6042 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
| 6043 | return 1; |
| 6044 | break; |
| 6045 | case 1: /* mov from cr */ |
| 6046 | switch (cr) { |
| 6047 | case 3: |
| 6048 | if (vmcs12->cpu_based_vm_exec_control & |
| 6049 | CPU_BASED_CR3_STORE_EXITING) |
| 6050 | return 1; |
| 6051 | break; |
| 6052 | case 8: |
| 6053 | if (vmcs12->cpu_based_vm_exec_control & |
| 6054 | CPU_BASED_CR8_STORE_EXITING) |
| 6055 | return 1; |
| 6056 | break; |
| 6057 | } |
| 6058 | break; |
| 6059 | case 3: /* lmsw */ |
| 6060 | /* |
| 6061 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 6062 | * cr0. Other attempted changes are ignored, with no exit. |
| 6063 | */ |
| 6064 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 6065 | (val ^ vmcs12->cr0_read_shadow)) |
| 6066 | return 1; |
| 6067 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 6068 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 6069 | (val & 0x1)) |
| 6070 | return 1; |
| 6071 | break; |
| 6072 | } |
| 6073 | return 0; |
| 6074 | } |
| 6075 | |
| 6076 | /* |
| 6077 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 6078 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 6079 | * when in is_guest_mode (L2). |
| 6080 | */ |
| 6081 | static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu) |
| 6082 | { |
| 6083 | u32 exit_reason = vmcs_read32(VM_EXIT_REASON); |
| 6084 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 6085 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6086 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6087 | |
| 6088 | if (vmx->nested.nested_run_pending) |
| 6089 | return 0; |
| 6090 | |
| 6091 | if (unlikely(vmx->fail)) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 6092 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 6093 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 6094 | return 1; |
| 6095 | } |
| 6096 | |
| 6097 | switch (exit_reason) { |
| 6098 | case EXIT_REASON_EXCEPTION_NMI: |
| 6099 | if (!is_exception(intr_info)) |
| 6100 | return 0; |
| 6101 | else if (is_page_fault(intr_info)) |
| 6102 | return enable_ept; |
| 6103 | return vmcs12->exception_bitmap & |
| 6104 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 6105 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
| 6106 | return 0; |
| 6107 | case EXIT_REASON_TRIPLE_FAULT: |
| 6108 | return 1; |
| 6109 | case EXIT_REASON_PENDING_INTERRUPT: |
| 6110 | case EXIT_REASON_NMI_WINDOW: |
| 6111 | /* |
| 6112 | * prepare_vmcs02() set the CPU_BASED_VIRTUAL_INTR_PENDING bit |
| 6113 | * (aka Interrupt Window Exiting) only when L1 turned it on, |
| 6114 | * so if we got a PENDING_INTERRUPT exit, this must be for L1. |
| 6115 | * Same for NMI Window Exiting. |
| 6116 | */ |
| 6117 | return 1; |
| 6118 | case EXIT_REASON_TASK_SWITCH: |
| 6119 | return 1; |
| 6120 | case EXIT_REASON_CPUID: |
| 6121 | return 1; |
| 6122 | case EXIT_REASON_HLT: |
| 6123 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 6124 | case EXIT_REASON_INVD: |
| 6125 | return 1; |
| 6126 | case EXIT_REASON_INVLPG: |
| 6127 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 6128 | case EXIT_REASON_RDPMC: |
| 6129 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
| 6130 | case EXIT_REASON_RDTSC: |
| 6131 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 6132 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 6133 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 6134 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: |
| 6135 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: |
| 6136 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
| 6137 | /* |
| 6138 | * VMX instructions trap unconditionally. This allows L1 to |
| 6139 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 6140 | */ |
| 6141 | return 1; |
| 6142 | case EXIT_REASON_CR_ACCESS: |
| 6143 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 6144 | case EXIT_REASON_DR_ACCESS: |
| 6145 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 6146 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 6147 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 6148 | case EXIT_REASON_MSR_READ: |
| 6149 | case EXIT_REASON_MSR_WRITE: |
| 6150 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 6151 | case EXIT_REASON_INVALID_STATE: |
| 6152 | return 1; |
| 6153 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 6154 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
| 6155 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 6156 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 6157 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 6158 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 6159 | nested_cpu_has2(vmcs12, |
| 6160 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 6161 | case EXIT_REASON_MCE_DURING_VMENTRY: |
| 6162 | return 0; |
| 6163 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
| 6164 | return 1; |
| 6165 | case EXIT_REASON_APIC_ACCESS: |
| 6166 | return nested_cpu_has2(vmcs12, |
| 6167 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 6168 | case EXIT_REASON_EPT_VIOLATION: |
| 6169 | case EXIT_REASON_EPT_MISCONFIG: |
| 6170 | return 0; |
| 6171 | case EXIT_REASON_WBINVD: |
| 6172 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 6173 | case EXIT_REASON_XSETBV: |
| 6174 | return 1; |
| 6175 | default: |
| 6176 | return 1; |
| 6177 | } |
| 6178 | } |
| 6179 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 6180 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 6181 | { |
| 6182 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 6183 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 6184 | } |
| 6185 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6186 | /* |
| 6187 | * The guest has exited. See if we can fix it or if we need userspace |
| 6188 | * assistance. |
| 6189 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6190 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6191 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 6192 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6193 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6194 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 6195 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6196 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 6197 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6198 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 6199 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 6200 | /* |
| 6201 | * the KVM_REQ_EVENT optimization bit is only on for one entry, and if |
| 6202 | * we did not inject a still-pending event to L1 now because of |
| 6203 | * nested_run_pending, we need to re-enable this bit. |
| 6204 | */ |
| 6205 | if (vmx->nested.nested_run_pending) |
| 6206 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 6207 | |
Nadav Har'El | 509c75e | 2011-06-02 11:54:52 +0300 | [diff] [blame] | 6208 | if (!is_guest_mode(vcpu) && (exit_reason == EXIT_REASON_VMLAUNCH || |
| 6209 | exit_reason == EXIT_REASON_VMRESUME)) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 6210 | vmx->nested.nested_run_pending = 1; |
| 6211 | else |
| 6212 | vmx->nested.nested_run_pending = 0; |
| 6213 | |
| 6214 | if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) { |
| 6215 | nested_vmx_vmexit(vcpu); |
| 6216 | return 1; |
| 6217 | } |
| 6218 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 6219 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
| 6220 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 6221 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 6222 | = exit_reason; |
| 6223 | return 0; |
| 6224 | } |
| 6225 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 6226 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6227 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 6228 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 6229 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 6230 | return 0; |
| 6231 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6232 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 6233 | /* |
| 6234 | * Note: |
| 6235 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 6236 | * delivery event since it indicates guest is accessing MMIO. |
| 6237 | * The vm-exit can be triggered again after return to guest that |
| 6238 | * will cause infinite loop. |
| 6239 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 6240 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6241 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 6242 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 6243 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 6244 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6245 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
| 6246 | vcpu->run->internal.ndata = 2; |
| 6247 | vcpu->run->internal.data[0] = vectoring_info; |
| 6248 | vcpu->run->internal.data[1] = exit_reason; |
| 6249 | return 0; |
| 6250 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6251 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 6252 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked && |
| 6253 | !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis( |
| 6254 | get_vmcs12(vcpu), vcpu)))) { |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 6255 | if (vmx_interrupt_allowed(vcpu)) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6256 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6257 | } else if (vmx->vnmi_blocked_time > 1000000000LL && |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 6258 | vcpu->arch.nmi_pending) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6259 | /* |
| 6260 | * This CPU don't support us in finding the end of an |
| 6261 | * NMI-blocked window if the guest runs with IRQs |
| 6262 | * disabled. So we pull the trigger after 1 s of |
| 6263 | * futile waiting, but inform the user about this. |
| 6264 | */ |
| 6265 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 6266 | "state on VCPU %d after 1 s timeout\n", |
| 6267 | __func__, vcpu->vcpu_id); |
| 6268 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6269 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6270 | } |
| 6271 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6272 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 6273 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6274 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6275 | else { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6276 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 6277 | vcpu->run->hw.hardware_exit_reason = exit_reason; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6278 | } |
| 6279 | return 0; |
| 6280 | } |
| 6281 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 6282 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6283 | { |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 6284 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6285 | vmcs_write32(TPR_THRESHOLD, 0); |
| 6286 | return; |
| 6287 | } |
| 6288 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 6289 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6290 | } |
| 6291 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 6292 | static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
| 6293 | { |
| 6294 | u32 sec_exec_control; |
| 6295 | |
| 6296 | /* |
| 6297 | * There is not point to enable virtualize x2apic without enable |
| 6298 | * apicv |
| 6299 | */ |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6300 | if (!cpu_has_vmx_virtualize_x2apic_mode() || |
| 6301 | !vmx_vm_has_apicv(vcpu->kvm)) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 6302 | return; |
| 6303 | |
| 6304 | if (!vm_need_tpr_shadow(vcpu->kvm)) |
| 6305 | return; |
| 6306 | |
| 6307 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 6308 | |
| 6309 | if (set) { |
| 6310 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6311 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 6312 | } else { |
| 6313 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 6314 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6315 | } |
| 6316 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 6317 | |
| 6318 | vmx_set_msr_bitmap(vcpu); |
| 6319 | } |
| 6320 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6321 | static void vmx_hwapic_isr_update(struct kvm *kvm, int isr) |
| 6322 | { |
| 6323 | u16 status; |
| 6324 | u8 old; |
| 6325 | |
| 6326 | if (!vmx_vm_has_apicv(kvm)) |
| 6327 | return; |
| 6328 | |
| 6329 | if (isr == -1) |
| 6330 | isr = 0; |
| 6331 | |
| 6332 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 6333 | old = status >> 8; |
| 6334 | if (isr != old) { |
| 6335 | status &= 0xff; |
| 6336 | status |= isr << 8; |
| 6337 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 6338 | } |
| 6339 | } |
| 6340 | |
| 6341 | static void vmx_set_rvi(int vector) |
| 6342 | { |
| 6343 | u16 status; |
| 6344 | u8 old; |
| 6345 | |
| 6346 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 6347 | old = (u8)status & 0xff; |
| 6348 | if ((u8)vector != old) { |
| 6349 | status &= ~0xff; |
| 6350 | status |= (u8)vector; |
| 6351 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 6352 | } |
| 6353 | } |
| 6354 | |
| 6355 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 6356 | { |
| 6357 | if (max_irr == -1) |
| 6358 | return; |
| 6359 | |
| 6360 | vmx_set_rvi(max_irr); |
| 6361 | } |
| 6362 | |
| 6363 | static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) |
| 6364 | { |
| 6365 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 6366 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 6367 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 6368 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 6369 | } |
| 6370 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6371 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6372 | { |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 6373 | u32 exit_intr_info; |
| 6374 | |
| 6375 | if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 6376 | || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
| 6377 | return; |
| 6378 | |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 6379 | vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 6380 | exit_intr_info = vmx->exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6381 | |
| 6382 | /* Handle machine checks before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 6383 | if (is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6384 | kvm_machine_check(); |
| 6385 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 6386 | /* We need to handle NMIs before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 6387 | if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR && |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 6388 | (exit_intr_info & INTR_INFO_VALID_MASK)) { |
| 6389 | kvm_before_handle_nmi(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 6390 | asm("int $2"); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 6391 | kvm_after_handle_nmi(&vmx->vcpu); |
| 6392 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6393 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 6394 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6395 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 6396 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 6397 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6398 | bool unblock_nmi; |
| 6399 | u8 vector; |
| 6400 | bool idtv_info_valid; |
| 6401 | |
| 6402 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 6403 | |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6404 | if (cpu_has_virtual_nmis()) { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 6405 | if (vmx->nmi_known_unmasked) |
| 6406 | return; |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 6407 | /* |
| 6408 | * Can't use vmx->exit_intr_info since we're not sure what |
| 6409 | * the exit reason is. |
| 6410 | */ |
| 6411 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6412 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 6413 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 6414 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 6415 | * SDM 3: 27.7.1.2 (September 2008) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6416 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 6417 | * a guest IRET fault. |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 6418 | * SDM 3: 23.2.2 (September 2008) |
| 6419 | * Bit 12 is undefined in any of the following cases: |
| 6420 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 6421 | * information field. |
| 6422 | * If the VM exit is due to a double fault. |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6423 | */ |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 6424 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 6425 | vector != DF_VECTOR && !idtv_info_valid) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6426 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6427 | GUEST_INTR_STATE_NMI); |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 6428 | else |
| 6429 | vmx->nmi_known_unmasked = |
| 6430 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 6431 | & GUEST_INTR_STATE_NMI); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6432 | } else if (unlikely(vmx->soft_vnmi_blocked)) |
| 6433 | vmx->vnmi_blocked_time += |
| 6434 | ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6435 | } |
| 6436 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6437 | static void __vmx_complete_interrupts(struct vcpu_vmx *vmx, |
| 6438 | u32 idt_vectoring_info, |
| 6439 | int instr_len_field, |
| 6440 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6441 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6442 | u8 vector; |
| 6443 | int type; |
| 6444 | bool idtv_info_valid; |
| 6445 | |
| 6446 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6447 | |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6448 | vmx->vcpu.arch.nmi_injected = false; |
| 6449 | kvm_clear_exception_queue(&vmx->vcpu); |
| 6450 | kvm_clear_interrupt_queue(&vmx->vcpu); |
| 6451 | |
| 6452 | if (!idtv_info_valid) |
| 6453 | return; |
| 6454 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6455 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 6456 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6457 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 6458 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6459 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6460 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6461 | case INTR_TYPE_NMI_INTR: |
| 6462 | vmx->vcpu.arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6463 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 6464 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6465 | * Clear bit "block by NMI" before VM entry if a NMI |
| 6466 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6467 | */ |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 6468 | vmx_set_nmi_mask(&vmx->vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6469 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6470 | case INTR_TYPE_SOFT_EXCEPTION: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6471 | vmx->vcpu.arch.event_exit_inst_len = |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6472 | vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6473 | /* fall through */ |
| 6474 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 6475 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6476 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6477 | kvm_queue_exception_e(&vmx->vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 6478 | } else |
| 6479 | kvm_queue_exception(&vmx->vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6480 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6481 | case INTR_TYPE_SOFT_INTR: |
| 6482 | vmx->vcpu.arch.event_exit_inst_len = |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6483 | vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6484 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6485 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6486 | kvm_queue_interrupt(&vmx->vcpu, vector, |
| 6487 | type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6488 | break; |
| 6489 | default: |
| 6490 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 6491 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6492 | } |
| 6493 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6494 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 6495 | { |
Nadav Har'El | 66c78ae | 2011-05-25 23:14:07 +0300 | [diff] [blame] | 6496 | if (is_guest_mode(&vmx->vcpu)) |
| 6497 | return; |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6498 | __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info, |
| 6499 | VM_EXIT_INSTRUCTION_LEN, |
| 6500 | IDT_VECTORING_ERROR_CODE); |
| 6501 | } |
| 6502 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 6503 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 6504 | { |
Nadav Har'El | 66c78ae | 2011-05-25 23:14:07 +0300 | [diff] [blame] | 6505 | if (is_guest_mode(vcpu)) |
| 6506 | return; |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 6507 | __vmx_complete_interrupts(to_vmx(vcpu), |
| 6508 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 6509 | VM_ENTRY_INSTRUCTION_LEN, |
| 6510 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 6511 | |
| 6512 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 6513 | } |
| 6514 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 6515 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 6516 | { |
| 6517 | int i, nr_msrs; |
| 6518 | struct perf_guest_switch_msr *msrs; |
| 6519 | |
| 6520 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 6521 | |
| 6522 | if (!msrs) |
| 6523 | return; |
| 6524 | |
| 6525 | for (i = 0; i < nr_msrs; i++) |
| 6526 | if (msrs[i].host == msrs[i].guest) |
| 6527 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 6528 | else |
| 6529 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
| 6530 | msrs[i].host); |
| 6531 | } |
| 6532 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 6533 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6534 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6535 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 6536 | unsigned long debugctlmsr; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6537 | |
Nadav Har'El | 66c78ae | 2011-05-25 23:14:07 +0300 | [diff] [blame] | 6538 | if (is_guest_mode(vcpu) && !vmx->nested.nested_run_pending) { |
| 6539 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6540 | if (vmcs12->idt_vectoring_info_field & |
| 6541 | VECTORING_INFO_VALID_MASK) { |
| 6542 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 6543 | vmcs12->idt_vectoring_info_field); |
| 6544 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 6545 | vmcs12->vm_exit_instruction_len); |
| 6546 | if (vmcs12->idt_vectoring_info_field & |
| 6547 | VECTORING_INFO_DELIVER_CODE_MASK) |
| 6548 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 6549 | vmcs12->idt_vectoring_error_code); |
| 6550 | } |
| 6551 | } |
| 6552 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6553 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
| 6554 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) |
| 6555 | vmx->entry_time = ktime_get(); |
| 6556 | |
| 6557 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 6558 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 6559 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6560 | return; |
| 6561 | |
| 6562 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 6563 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 6564 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 6565 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 6566 | |
| 6567 | /* When single-stepping over STI and MOV SS, we must clear the |
| 6568 | * corresponding interruptibility bits in the guest state. Otherwise |
| 6569 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 6570 | * exceptions being set, but that's not correct for the guest debugging |
| 6571 | * case. */ |
| 6572 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 6573 | vmx_set_interrupt_shadow(vcpu, 0); |
| 6574 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 6575 | atomic_switch_perf_msrs(vmx); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 6576 | debugctlmsr = get_debugctlmsr(); |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 6577 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6578 | vmx->__launched = vmx->loaded_vmcs->launched; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6579 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6580 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6581 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 6582 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 6583 | "push %%" _ASM_CX " \n\t" |
| 6584 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6585 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6586 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 6587 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6588 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 6589 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6590 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 6591 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 6592 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 6593 | "je 2f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6594 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 6595 | "2: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6596 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6597 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6598 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6599 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 6600 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 6601 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 6602 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 6603 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 6604 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6605 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6606 | "mov %c[r8](%0), %%r8 \n\t" |
| 6607 | "mov %c[r9](%0), %%r9 \n\t" |
| 6608 | "mov %c[r10](%0), %%r10 \n\t" |
| 6609 | "mov %c[r11](%0), %%r11 \n\t" |
| 6610 | "mov %c[r12](%0), %%r12 \n\t" |
| 6611 | "mov %c[r13](%0), %%r13 \n\t" |
| 6612 | "mov %c[r14](%0), %%r14 \n\t" |
| 6613 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6614 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6615 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6616 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6617 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 6618 | "jne 1f \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 6619 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 6620 | "jmp 2f \n\t" |
| 6621 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" |
| 6622 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6623 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6624 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 6625 | "pop %0 \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6626 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 6627 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 6628 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 6629 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 6630 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 6631 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 6632 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6633 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6634 | "mov %%r8, %c[r8](%0) \n\t" |
| 6635 | "mov %%r9, %c[r9](%0) \n\t" |
| 6636 | "mov %%r10, %c[r10](%0) \n\t" |
| 6637 | "mov %%r11, %c[r11](%0) \n\t" |
| 6638 | "mov %%r12, %c[r12](%0) \n\t" |
| 6639 | "mov %%r13, %c[r13](%0) \n\t" |
| 6640 | "mov %%r14, %c[r14](%0) \n\t" |
| 6641 | "mov %%r15, %c[r15](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6642 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6643 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 6644 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6645 | |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6646 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6647 | "setbe %c[fail](%0) \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 6648 | ".pushsection .rodata \n\t" |
| 6649 | ".global vmx_return \n\t" |
| 6650 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 6651 | ".popsection" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6652 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6653 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6654 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6655 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6656 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 6657 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 6658 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 6659 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 6660 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 6661 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 6662 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6663 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6664 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 6665 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 6666 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 6667 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 6668 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 6669 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 6670 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 6671 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6672 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 6673 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 6674 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6675 | : "cc", "memory" |
| 6676 | #ifdef CONFIG_X86_64 |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6677 | , "rax", "rbx", "rdi", "rsi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6678 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6679 | #else |
| 6680 | , "eax", "ebx", "edi", "esi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6681 | #endif |
| 6682 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6683 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 6684 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
| 6685 | if (debugctlmsr) |
| 6686 | update_debugctlmsr(debugctlmsr); |
| 6687 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 6688 | #ifndef CONFIG_X86_64 |
| 6689 | /* |
| 6690 | * The sysexit path does not restore ds/es, so we must set them to |
| 6691 | * a reasonable value ourselves. |
| 6692 | * |
| 6693 | * We can't defer this to vmx_load_host_state() since that function |
| 6694 | * may be executed in interrupt context, which saves and restore segments |
| 6695 | * around it, nullifying its effect. |
| 6696 | */ |
| 6697 | loadsegment(ds, __USER_DS); |
| 6698 | loadsegment(es, __USER_DS); |
| 6699 | #endif |
| 6700 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 6701 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 6702 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 6703 | | (1 << VCPU_EXREG_CPL) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 6704 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 6705 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 6706 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 6707 | vcpu->arch.regs_dirty = 0; |
| 6708 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6709 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 6710 | |
Nadav Har'El | 66c78ae | 2011-05-25 23:14:07 +0300 | [diff] [blame] | 6711 | if (is_guest_mode(vcpu)) { |
| 6712 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6713 | vmcs12->idt_vectoring_info_field = vmx->idt_vectoring_info; |
| 6714 | if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) { |
| 6715 | vmcs12->idt_vectoring_error_code = |
| 6716 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 6717 | vmcs12->vm_exit_instruction_len = |
| 6718 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 6719 | } |
| 6720 | } |
| 6721 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6722 | vmx->loaded_vmcs->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 6723 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6724 | vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); |
Jan Kiszka | 1e2b1dd | 2011-09-12 10:52:24 +0200 | [diff] [blame] | 6725 | trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6726 | |
| 6727 | vmx_complete_atomic_exit(vmx); |
| 6728 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6729 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6730 | } |
| 6731 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6732 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 6733 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6734 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6735 | |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 6736 | free_vpid(vmx); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6737 | free_nested(vmx); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6738 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6739 | kfree(vmx->guest_msrs); |
| 6740 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 6741 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6742 | } |
| 6743 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6744 | static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6745 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6746 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 6747 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6748 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6749 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6750 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6751 | return ERR_PTR(-ENOMEM); |
| 6752 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 6753 | allocate_vpid(vmx); |
| 6754 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6755 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 6756 | if (err) |
| 6757 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6758 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6759 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 6760 | err = -ENOMEM; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6761 | if (!vmx->guest_msrs) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6762 | goto uninit_vcpu; |
| 6763 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6764 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6765 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 6766 | vmx->loaded_vmcs->vmcs = alloc_vmcs(); |
| 6767 | if (!vmx->loaded_vmcs->vmcs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6768 | goto free_msrs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6769 | if (!vmm_exclusive) |
| 6770 | kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id()))); |
| 6771 | loaded_vmcs_init(vmx->loaded_vmcs); |
| 6772 | if (!vmm_exclusive) |
| 6773 | kvm_cpu_vmxoff(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6774 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6775 | cpu = get_cpu(); |
| 6776 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 6777 | vmx->vcpu.cpu = cpu; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 6778 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6779 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6780 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6781 | if (err) |
| 6782 | goto free_vmcs; |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 6783 | if (vm_need_virtualize_apic_accesses(kvm)) |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 6784 | err = alloc_apic_access_page(kvm); |
| 6785 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 6786 | goto free_vmcs; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6787 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 6788 | if (enable_ept) { |
| 6789 | if (!kvm->arch.ept_identity_map_addr) |
| 6790 | kvm->arch.ept_identity_map_addr = |
| 6791 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 6792 | err = -ENOMEM; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 6793 | if (alloc_identity_pagetable(kvm) != 0) |
| 6794 | goto free_vmcs; |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 6795 | if (!init_rmode_identity_map(kvm)) |
| 6796 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 6797 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 6798 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 6799 | vmx->nested.current_vmptr = -1ull; |
| 6800 | vmx->nested.current_vmcs12 = NULL; |
| 6801 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6802 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6803 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6804 | free_vmcs: |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 6805 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6806 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6807 | kfree(vmx->guest_msrs); |
| 6808 | uninit_vcpu: |
| 6809 | kvm_vcpu_uninit(&vmx->vcpu); |
| 6810 | free_vcpu: |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 6811 | free_vpid(vmx); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 6812 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6813 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6814 | } |
| 6815 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6816 | static void __init vmx_check_processor_compat(void *rtn) |
| 6817 | { |
| 6818 | struct vmcs_config vmcs_conf; |
| 6819 | |
| 6820 | *(int *)rtn = 0; |
| 6821 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 6822 | *(int *)rtn = -EIO; |
| 6823 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 6824 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 6825 | smp_processor_id()); |
| 6826 | *(int *)rtn = -EIO; |
| 6827 | } |
| 6828 | } |
| 6829 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 6830 | static int get_ept_level(void) |
| 6831 | { |
| 6832 | return VMX_EPT_DEFAULT_GAW + 1; |
| 6833 | } |
| 6834 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6835 | static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 6836 | { |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6837 | u64 ret; |
| 6838 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6839 | /* For VT-d and EPT combination |
| 6840 | * 1. MMIO: always map as UC |
| 6841 | * 2. EPT with VT-d: |
| 6842 | * a. VT-d without snooping control feature: can't guarantee the |
| 6843 | * result, try to trust guest. |
| 6844 | * b. VT-d with snooping control feature: snooping control feature of |
| 6845 | * VT-d engine can guarantee the cache correctness. Just set it |
| 6846 | * to WB to keep consistent with host. So the same as item 3. |
Sheng Yang | a19a6d1 | 2010-02-09 16:41:53 +0800 | [diff] [blame] | 6847 | * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6848 | * consistent with host MTRR |
| 6849 | */ |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6850 | if (is_mmio) |
| 6851 | ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT; |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6852 | else if (vcpu->kvm->arch.iommu_domain && |
| 6853 | !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)) |
| 6854 | ret = kvm_get_guest_memory_type(vcpu, gfn) << |
| 6855 | VMX_EPT_MT_EPTE_SHIFT; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6856 | else |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6857 | ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT) |
Sheng Yang | a19a6d1 | 2010-02-09 16:41:53 +0800 | [diff] [blame] | 6858 | | VMX_EPT_IPAT_BIT; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6859 | |
| 6860 | return ret; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 6861 | } |
| 6862 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 6863 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 6864 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 6865 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 6866 | return PT_DIRECTORY_LEVEL; |
| 6867 | else |
| 6868 | /* For shadow and EPT supported 1GB page */ |
| 6869 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 6870 | } |
| 6871 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 6872 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 6873 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 6874 | struct kvm_cpuid_entry2 *best; |
| 6875 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6876 | u32 exec_control; |
| 6877 | |
| 6878 | vmx->rdtscp_enabled = false; |
| 6879 | if (vmx_rdtscp_supported()) { |
| 6880 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 6881 | if (exec_control & SECONDARY_EXEC_RDTSCP) { |
| 6882 | best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 6883 | if (best && (best->edx & bit(X86_FEATURE_RDTSCP))) |
| 6884 | vmx->rdtscp_enabled = true; |
| 6885 | else { |
| 6886 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 6887 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 6888 | exec_control); |
| 6889 | } |
| 6890 | } |
| 6891 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 6892 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 6893 | /* Exposing INVPCID only when PCID is exposed */ |
| 6894 | best = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 6895 | if (vmx_invpcid_supported() && |
Ren, Yongjie | 4f97704 | 2012-09-07 07:36:59 +0000 | [diff] [blame] | 6896 | best && (best->ebx & bit(X86_FEATURE_INVPCID)) && |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 6897 | guest_cpuid_has_pcid(vcpu)) { |
Takashi Iwai | 29282fd | 2012-11-09 15:20:17 +0100 | [diff] [blame] | 6898 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 6899 | exec_control |= SECONDARY_EXEC_ENABLE_INVPCID; |
| 6900 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 6901 | exec_control); |
| 6902 | } else { |
Takashi Iwai | 29282fd | 2012-11-09 15:20:17 +0100 | [diff] [blame] | 6903 | if (cpu_has_secondary_exec_ctrls()) { |
| 6904 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 6905 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 6906 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 6907 | exec_control); |
| 6908 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 6909 | if (best) |
Ren, Yongjie | 4f97704 | 2012-09-07 07:36:59 +0000 | [diff] [blame] | 6910 | best->ebx &= ~bit(X86_FEATURE_INVPCID); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 6911 | } |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 6912 | } |
| 6913 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 6914 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 6915 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 6916 | if (func == 1 && nested) |
| 6917 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 6918 | } |
| 6919 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 6920 | /* |
| 6921 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 6922 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
| 6923 | * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2 |
| 6924 | * guest in a way that will both be appropriate to L1's requests, and our |
| 6925 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 6926 | * function also has additional necessary side-effects, like setting various |
| 6927 | * vcpu->arch fields. |
| 6928 | */ |
| 6929 | static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 6930 | { |
| 6931 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6932 | u32 exec_control; |
| 6933 | |
| 6934 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 6935 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 6936 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 6937 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 6938 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 6939 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 6940 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 6941 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 6942 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 6943 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 6944 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 6945 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 6946 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 6947 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 6948 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 6949 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 6950 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 6951 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 6952 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 6953 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 6954 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 6955 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 6956 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 6957 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 6958 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 6959 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 6960 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 6961 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 6962 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 6963 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 6964 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 6965 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 6966 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 6967 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 6968 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 6969 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
| 6970 | |
| 6971 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 6972 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 6973 | vmcs12->vm_entry_intr_info_field); |
| 6974 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 6975 | vmcs12->vm_entry_exception_error_code); |
| 6976 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 6977 | vmcs12->vm_entry_instruction_len); |
| 6978 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 6979 | vmcs12->guest_interruptibility_info); |
| 6980 | vmcs_write32(GUEST_ACTIVITY_STATE, vmcs12->guest_activity_state); |
| 6981 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 6982 | vmcs_writel(GUEST_DR7, vmcs12->guest_dr7); |
| 6983 | vmcs_writel(GUEST_RFLAGS, vmcs12->guest_rflags); |
| 6984 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 6985 | vmcs12->guest_pending_dbg_exceptions); |
| 6986 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 6987 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 6988 | |
| 6989 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 6990 | |
| 6991 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, |
| 6992 | (vmcs_config.pin_based_exec_ctrl | |
| 6993 | vmcs12->pin_based_vm_exec_control)); |
| 6994 | |
| 6995 | /* |
| 6996 | * Whether page-faults are trapped is determined by a combination of |
| 6997 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 6998 | * If enable_ept, L0 doesn't care about page faults and we should |
| 6999 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 7000 | * care about (at least some) page faults, and because it is not easy |
| 7001 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 7002 | * to exit on each and every L2 page fault. This is done by setting |
| 7003 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 7004 | * Note that below we don't need special code to set EB.PF beyond the |
| 7005 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 7006 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 7007 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 7008 | * |
| 7009 | * A problem with this approach (when !enable_ept) is that L1 may be |
| 7010 | * injected with more page faults than it asked for. This could have |
| 7011 | * caused problems, but in practice existing hypervisors don't care. |
| 7012 | * To fix this, we will need to emulate the PFEC checking (on the L1 |
| 7013 | * page tables), using walk_addr(), when injecting PFs to L1. |
| 7014 | */ |
| 7015 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 7016 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 7017 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 7018 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 7019 | |
| 7020 | if (cpu_has_secondary_exec_ctrls()) { |
| 7021 | u32 exec_control = vmx_secondary_exec_control(vmx); |
| 7022 | if (!vmx->rdtscp_enabled) |
| 7023 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 7024 | /* Take the following fields only from vmcs12 */ |
| 7025 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 7026 | if (nested_cpu_has(vmcs12, |
| 7027 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 7028 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 7029 | |
| 7030 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) { |
| 7031 | /* |
| 7032 | * Translate L1 physical address to host physical |
| 7033 | * address for vmcs02. Keep the page pinned, so this |
| 7034 | * physical address remains valid. We keep a reference |
| 7035 | * to it so we can release it later. |
| 7036 | */ |
| 7037 | if (vmx->nested.apic_access_page) /* shouldn't happen */ |
| 7038 | nested_release_page(vmx->nested.apic_access_page); |
| 7039 | vmx->nested.apic_access_page = |
| 7040 | nested_get_page(vcpu, vmcs12->apic_access_addr); |
| 7041 | /* |
| 7042 | * If translation failed, no matter: This feature asks |
| 7043 | * to exit when accessing the given address, and if it |
| 7044 | * can never be accessed, this feature won't do |
| 7045 | * anything anyway. |
| 7046 | */ |
| 7047 | if (!vmx->nested.apic_access_page) |
| 7048 | exec_control &= |
| 7049 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 7050 | else |
| 7051 | vmcs_write64(APIC_ACCESS_ADDR, |
| 7052 | page_to_phys(vmx->nested.apic_access_page)); |
| 7053 | } |
| 7054 | |
| 7055 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 7056 | } |
| 7057 | |
| 7058 | |
| 7059 | /* |
| 7060 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 7061 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 7062 | * Other fields are different per CPU, and will be set later when |
| 7063 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 7064 | */ |
| 7065 | vmx_set_constant_host_state(); |
| 7066 | |
| 7067 | /* |
| 7068 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 7069 | * entry, but only if the current (host) sp changed from the value |
| 7070 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 7071 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 7072 | * here we just force the write to happen on entry. |
| 7073 | */ |
| 7074 | vmx->host_rsp = 0; |
| 7075 | |
| 7076 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 7077 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 7078 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 7079 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 7080 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
| 7081 | /* |
| 7082 | * Merging of IO and MSR bitmaps not currently supported. |
| 7083 | * Rather, exit every time. |
| 7084 | */ |
| 7085 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 7086 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 7087 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 7088 | |
| 7089 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 7090 | |
| 7091 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 7092 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 7093 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 7094 | */ |
| 7095 | update_exception_bitmap(vcpu); |
| 7096 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 7097 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 7098 | |
| 7099 | /* Note: IA32_MODE, LOAD_IA32_EFER are modified by vmx_set_efer below */ |
| 7100 | vmcs_write32(VM_EXIT_CONTROLS, |
| 7101 | vmcs12->vm_exit_controls | vmcs_config.vmexit_ctrl); |
| 7102 | vmcs_write32(VM_ENTRY_CONTROLS, vmcs12->vm_entry_controls | |
| 7103 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 7104 | |
| 7105 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) |
| 7106 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
| 7107 | else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 7108 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 7109 | |
| 7110 | |
| 7111 | set_cr4_guest_host_mask(vmx); |
| 7112 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 7113 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 7114 | vmcs_write64(TSC_OFFSET, |
| 7115 | vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset); |
| 7116 | else |
| 7117 | vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 7118 | |
| 7119 | if (enable_vpid) { |
| 7120 | /* |
| 7121 | * Trivially support vpid by letting L2s share their parent |
| 7122 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 7123 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 7124 | */ |
| 7125 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 7126 | vmx_flush_tlb(vcpu); |
| 7127 | } |
| 7128 | |
| 7129 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) |
| 7130 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
| 7131 | if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 7132 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 7133 | else |
| 7134 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 7135 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 7136 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 7137 | |
| 7138 | /* |
| 7139 | * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified |
| 7140 | * TS bit (for lazy fpu) and bits which we consider mandatory enabled. |
| 7141 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 7142 | * the specifications by L1; It's not enough to take |
| 7143 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 7144 | * have more bits than L1 expected. |
| 7145 | */ |
| 7146 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 7147 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 7148 | |
| 7149 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 7150 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 7151 | |
| 7152 | /* shadow page tables on either EPT or shadow page tables */ |
| 7153 | kvm_set_cr3(vcpu, vmcs12->guest_cr3); |
| 7154 | kvm_mmu_reset_context(vcpu); |
| 7155 | |
| 7156 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 7157 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
| 7158 | } |
| 7159 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7160 | /* |
| 7161 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 7162 | * for running an L2 nested guest. |
| 7163 | */ |
| 7164 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 7165 | { |
| 7166 | struct vmcs12 *vmcs12; |
| 7167 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7168 | int cpu; |
| 7169 | struct loaded_vmcs *vmcs02; |
| 7170 | |
| 7171 | if (!nested_vmx_check_permission(vcpu) || |
| 7172 | !nested_vmx_check_vmcs12(vcpu)) |
| 7173 | return 1; |
| 7174 | |
| 7175 | skip_emulated_instruction(vcpu); |
| 7176 | vmcs12 = get_vmcs12(vcpu); |
| 7177 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 7178 | /* |
| 7179 | * The nested entry process starts with enforcing various prerequisites |
| 7180 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 7181 | * they fail: As the SDM explains, some conditions should cause the |
| 7182 | * instruction to fail, while others will cause the instruction to seem |
| 7183 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 7184 | * To speed up the normal (success) code path, we should avoid checking |
| 7185 | * for misconfigurations which will anyway be caught by the processor |
| 7186 | * when using the merged vmcs02. |
| 7187 | */ |
| 7188 | if (vmcs12->launch_state == launch) { |
| 7189 | nested_vmx_failValid(vcpu, |
| 7190 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 7191 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 7192 | return 1; |
| 7193 | } |
| 7194 | |
| 7195 | if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) && |
| 7196 | !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) { |
| 7197 | /*TODO: Also verify bits beyond physical address width are 0*/ |
| 7198 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 7199 | return 1; |
| 7200 | } |
| 7201 | |
| 7202 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
| 7203 | !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) { |
| 7204 | /*TODO: Also verify bits beyond physical address width are 0*/ |
| 7205 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 7206 | return 1; |
| 7207 | } |
| 7208 | |
| 7209 | if (vmcs12->vm_entry_msr_load_count > 0 || |
| 7210 | vmcs12->vm_exit_msr_load_count > 0 || |
| 7211 | vmcs12->vm_exit_msr_store_count > 0) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 7212 | pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n", |
| 7213 | __func__); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 7214 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 7215 | return 1; |
| 7216 | } |
| 7217 | |
| 7218 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
| 7219 | nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) || |
| 7220 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
| 7221 | nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) || |
| 7222 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
| 7223 | nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) || |
| 7224 | !vmx_control_verify(vmcs12->vm_exit_controls, |
| 7225 | nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) || |
| 7226 | !vmx_control_verify(vmcs12->vm_entry_controls, |
| 7227 | nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high)) |
| 7228 | { |
| 7229 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 7230 | return 1; |
| 7231 | } |
| 7232 | |
| 7233 | if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) || |
| 7234 | ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 7235 | nested_vmx_failValid(vcpu, |
| 7236 | VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
| 7237 | return 1; |
| 7238 | } |
| 7239 | |
| 7240 | if (((vmcs12->guest_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) || |
| 7241 | ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 7242 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 7243 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 7244 | return 1; |
| 7245 | } |
| 7246 | if (vmcs12->vmcs_link_pointer != -1ull) { |
| 7247 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 7248 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR); |
| 7249 | return 1; |
| 7250 | } |
| 7251 | |
| 7252 | /* |
| 7253 | * We're finally done with prerequisite checking, and can start with |
| 7254 | * the nested entry. |
| 7255 | */ |
| 7256 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7257 | vmcs02 = nested_get_current_vmcs02(vmx); |
| 7258 | if (!vmcs02) |
| 7259 | return -ENOMEM; |
| 7260 | |
| 7261 | enter_guest_mode(vcpu); |
| 7262 | |
| 7263 | vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET); |
| 7264 | |
| 7265 | cpu = get_cpu(); |
| 7266 | vmx->loaded_vmcs = vmcs02; |
| 7267 | vmx_vcpu_put(vcpu); |
| 7268 | vmx_vcpu_load(vcpu, cpu); |
| 7269 | vcpu->cpu = cpu; |
| 7270 | put_cpu(); |
| 7271 | |
| 7272 | vmcs12->launch_state = 1; |
| 7273 | |
| 7274 | prepare_vmcs02(vcpu, vmcs12); |
| 7275 | |
| 7276 | /* |
| 7277 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 7278 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 7279 | * returned as far as L1 is concerned. It will only return (and set |
| 7280 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 7281 | */ |
| 7282 | return 1; |
| 7283 | } |
| 7284 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 7285 | /* |
| 7286 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 7287 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 7288 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 7289 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 7290 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 7291 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 7292 | * didn't trap the bit, because if L1 did, so would L0). |
| 7293 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 7294 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 7295 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 7296 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 7297 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 7298 | * changed these bits, and therefore they need to be updated, but L0 |
| 7299 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 7300 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 7301 | */ |
| 7302 | static inline unsigned long |
| 7303 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 7304 | { |
| 7305 | return |
| 7306 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 7307 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 7308 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 7309 | vcpu->arch.cr0_guest_owned_bits)); |
| 7310 | } |
| 7311 | |
| 7312 | static inline unsigned long |
| 7313 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 7314 | { |
| 7315 | return |
| 7316 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 7317 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 7318 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 7319 | vcpu->arch.cr4_guest_owned_bits)); |
| 7320 | } |
| 7321 | |
| 7322 | /* |
| 7323 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 7324 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 7325 | * and this function updates it to reflect the changes to the guest state while |
| 7326 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 7327 | * without going back to L1), and to reflect the exit reason. |
| 7328 | * Note that we do not have to copy here all VMCS fields, just those that |
| 7329 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 7330 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 7331 | * which already writes to vmcs12 directly. |
| 7332 | */ |
| 7333 | void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 7334 | { |
| 7335 | /* update guest state fields: */ |
| 7336 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 7337 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 7338 | |
| 7339 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 7340 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 7341 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 7342 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 7343 | |
| 7344 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 7345 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 7346 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 7347 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 7348 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 7349 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 7350 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 7351 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 7352 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 7353 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 7354 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 7355 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 7356 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 7357 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 7358 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 7359 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 7360 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 7361 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 7362 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 7363 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 7364 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 7365 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 7366 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 7367 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 7368 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 7369 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 7370 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 7371 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 7372 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 7373 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 7374 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 7375 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 7376 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 7377 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 7378 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 7379 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 7380 | |
| 7381 | vmcs12->guest_activity_state = vmcs_read32(GUEST_ACTIVITY_STATE); |
| 7382 | vmcs12->guest_interruptibility_info = |
| 7383 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 7384 | vmcs12->guest_pending_dbg_exceptions = |
| 7385 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
| 7386 | |
| 7387 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 7388 | * the relevant bit asks not to trap the change */ |
| 7389 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 7390 | if (vmcs12->vm_entry_controls & VM_EXIT_SAVE_IA32_PAT) |
| 7391 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
| 7392 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 7393 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 7394 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
| 7395 | |
| 7396 | /* update exit information fields: */ |
| 7397 | |
| 7398 | vmcs12->vm_exit_reason = vmcs_read32(VM_EXIT_REASON); |
| 7399 | vmcs12->exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7400 | |
| 7401 | vmcs12->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 7402 | vmcs12->vm_exit_intr_error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 7403 | vmcs12->idt_vectoring_info_field = |
| 7404 | vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 7405 | vmcs12->idt_vectoring_error_code = |
| 7406 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 7407 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 7408 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7409 | |
| 7410 | /* clear vm-entry fields which are to be cleared on exit */ |
| 7411 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 7412 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
| 7413 | } |
| 7414 | |
| 7415 | /* |
| 7416 | * A part of what we need to when the nested L2 guest exits and we want to |
| 7417 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 7418 | * in vmcs12. |
| 7419 | * This function is to be called not only on normal nested exit, but also on |
| 7420 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 7421 | * Failures During or After Loading Guest State"). |
| 7422 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 7423 | */ |
| 7424 | void load_vmcs12_host_state(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 7425 | { |
| 7426 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 7427 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
| 7428 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 7429 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 7430 | else |
| 7431 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 7432 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 7433 | |
| 7434 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 7435 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
| 7436 | /* |
| 7437 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 7438 | * actually changed, because it depends on the current state of |
| 7439 | * fpu_active (which may have changed). |
| 7440 | * Note that vmx_set_cr0 refers to efer set above. |
| 7441 | */ |
| 7442 | kvm_set_cr0(vcpu, vmcs12->host_cr0); |
| 7443 | /* |
| 7444 | * If we did fpu_activate()/fpu_deactivate() during L2's run, we need |
| 7445 | * to apply the same changes to L1's vmcs. We just set cr0 correctly, |
| 7446 | * but we also need to update cr0_guest_host_mask and exception_bitmap. |
| 7447 | */ |
| 7448 | update_exception_bitmap(vcpu); |
| 7449 | vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0); |
| 7450 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 7451 | |
| 7452 | /* |
| 7453 | * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01 |
| 7454 | * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask(); |
| 7455 | */ |
| 7456 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 7457 | kvm_set_cr4(vcpu, vmcs12->host_cr4); |
| 7458 | |
| 7459 | /* shadow page tables on either EPT or shadow page tables */ |
| 7460 | kvm_set_cr3(vcpu, vmcs12->host_cr3); |
| 7461 | kvm_mmu_reset_context(vcpu); |
| 7462 | |
| 7463 | if (enable_vpid) { |
| 7464 | /* |
| 7465 | * Trivially support vpid by letting L2s share their parent |
| 7466 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 7467 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 7468 | */ |
| 7469 | vmx_flush_tlb(vcpu); |
| 7470 | } |
| 7471 | |
| 7472 | |
| 7473 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 7474 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 7475 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 7476 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 7477 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
| 7478 | vmcs_writel(GUEST_TR_BASE, vmcs12->host_tr_base); |
| 7479 | vmcs_writel(GUEST_GS_BASE, vmcs12->host_gs_base); |
| 7480 | vmcs_writel(GUEST_FS_BASE, vmcs12->host_fs_base); |
| 7481 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->host_es_selector); |
| 7482 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->host_cs_selector); |
| 7483 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->host_ss_selector); |
| 7484 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->host_ds_selector); |
| 7485 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->host_fs_selector); |
| 7486 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->host_gs_selector); |
| 7487 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->host_tr_selector); |
| 7488 | |
| 7489 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) |
| 7490 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
| 7491 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 7492 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 7493 | vmcs12->host_ia32_perf_global_ctrl); |
| 7494 | } |
| 7495 | |
| 7496 | /* |
| 7497 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 7498 | * and modify vmcs12 to make it see what it would expect to see there if |
| 7499 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 7500 | */ |
| 7501 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu) |
| 7502 | { |
| 7503 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7504 | int cpu; |
| 7505 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 7506 | |
| 7507 | leave_guest_mode(vcpu); |
| 7508 | prepare_vmcs12(vcpu, vmcs12); |
| 7509 | |
| 7510 | cpu = get_cpu(); |
| 7511 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 7512 | vmx_vcpu_put(vcpu); |
| 7513 | vmx_vcpu_load(vcpu, cpu); |
| 7514 | vcpu->cpu = cpu; |
| 7515 | put_cpu(); |
| 7516 | |
| 7517 | /* if no vmcs02 cache requested, remove the one we used */ |
| 7518 | if (VMCS02_POOL_SIZE == 0) |
| 7519 | nested_free_vmcs02(vmx, vmx->nested.current_vmptr); |
| 7520 | |
| 7521 | load_vmcs12_host_state(vcpu, vmcs12); |
| 7522 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 7523 | /* Update TSC_OFFSET if TSC was changed while L2 ran */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 7524 | vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset); |
| 7525 | |
| 7526 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 7527 | vmx->host_rsp = 0; |
| 7528 | |
| 7529 | /* Unpin physical memory we referred to in vmcs02 */ |
| 7530 | if (vmx->nested.apic_access_page) { |
| 7531 | nested_release_page(vmx->nested.apic_access_page); |
| 7532 | vmx->nested.apic_access_page = 0; |
| 7533 | } |
| 7534 | |
| 7535 | /* |
| 7536 | * Exiting from L2 to L1, we're now back to L1 which thinks it just |
| 7537 | * finished a VMLAUNCH or VMRESUME instruction, so we need to set the |
| 7538 | * success or failure flag accordingly. |
| 7539 | */ |
| 7540 | if (unlikely(vmx->fail)) { |
| 7541 | vmx->fail = 0; |
| 7542 | nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 7543 | } else |
| 7544 | nested_vmx_succeed(vcpu); |
| 7545 | } |
| 7546 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 7547 | /* |
| 7548 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 7549 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 7550 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 7551 | * It should only be called before L2 actually succeeded to run, and when |
| 7552 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 7553 | */ |
| 7554 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 7555 | struct vmcs12 *vmcs12, |
| 7556 | u32 reason, unsigned long qualification) |
| 7557 | { |
| 7558 | load_vmcs12_host_state(vcpu, vmcs12); |
| 7559 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 7560 | vmcs12->exit_qualification = qualification; |
| 7561 | nested_vmx_succeed(vcpu); |
| 7562 | } |
| 7563 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 7564 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 7565 | struct x86_instruction_info *info, |
| 7566 | enum x86_intercept_stage stage) |
| 7567 | { |
| 7568 | return X86EMUL_CONTINUE; |
| 7569 | } |
| 7570 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 7571 | static struct kvm_x86_ops vmx_x86_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7572 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 7573 | .disabled_by_bios = vmx_disabled_by_bios, |
| 7574 | .hardware_setup = hardware_setup, |
| 7575 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 7576 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7577 | .hardware_enable = hardware_enable, |
| 7578 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 7579 | .cpu_has_accelerated_tpr = report_flexpriority, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7580 | |
| 7581 | .vcpu_create = vmx_create_vcpu, |
| 7582 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 7583 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7584 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 7585 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7586 | .vcpu_load = vmx_vcpu_load, |
| 7587 | .vcpu_put = vmx_vcpu_put, |
| 7588 | |
Jan Kiszka | c863901 | 2012-09-21 05:42:55 +0200 | [diff] [blame] | 7589 | .update_db_bp_intercept = update_exception_bitmap, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7590 | .get_msr = vmx_get_msr, |
| 7591 | .set_msr = vmx_set_msr, |
| 7592 | .get_segment_base = vmx_get_segment_base, |
| 7593 | .get_segment = vmx_get_segment, |
| 7594 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 7595 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7596 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 7597 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 7598 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 7599 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7600 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7601 | .set_cr3 = vmx_set_cr3, |
| 7602 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7603 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7604 | .get_idt = vmx_get_idt, |
| 7605 | .set_idt = vmx_set_idt, |
| 7606 | .get_gdt = vmx_get_gdt, |
| 7607 | .set_gdt = vmx_set_gdt, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7608 | .set_dr7 = vmx_set_dr7, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 7609 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7610 | .get_rflags = vmx_get_rflags, |
| 7611 | .set_rflags = vmx_set_rflags, |
Avi Kivity | ebcbab4 | 2010-02-07 11:56:52 +0200 | [diff] [blame] | 7612 | .fpu_activate = vmx_fpu_activate, |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 7613 | .fpu_deactivate = vmx_fpu_deactivate, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7614 | |
| 7615 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7616 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7617 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 7618 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7619 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 7620 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 7621 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7622 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 7623 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7624 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 7625 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 7626 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 7627 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7628 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 7629 | .get_nmi_mask = vmx_get_nmi_mask, |
| 7630 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7631 | .enable_nmi_window = enable_nmi_window, |
| 7632 | .enable_irq_window = enable_irq_window, |
| 7633 | .update_cr8_intercept = update_cr8_intercept, |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 7634 | .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7635 | .vm_has_apicv = vmx_vm_has_apicv, |
| 7636 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
| 7637 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 7638 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7639 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 7640 | .set_tss_addr = vmx_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 7641 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 7642 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7643 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 7644 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 7645 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 7646 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 7647 | |
| 7648 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 7649 | |
| 7650 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 7651 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 7652 | |
| 7653 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 7654 | |
| 7655 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 7656 | |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 7657 | .set_tsc_khz = vmx_set_tsc_khz, |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 7658 | .read_tsc_offset = vmx_read_tsc_offset, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 7659 | .write_tsc_offset = vmx_write_tsc_offset, |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 7660 | .adjust_tsc_offset = vmx_adjust_tsc_offset, |
Joerg Roedel | 857e409 | 2011-03-25 09:44:50 +0100 | [diff] [blame] | 7661 | .compute_tsc_offset = vmx_compute_tsc_offset, |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 7662 | .read_l1_tsc = vmx_read_l1_tsc, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 7663 | |
| 7664 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 7665 | |
| 7666 | .check_intercept = vmx_check_intercept, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7667 | }; |
| 7668 | |
| 7669 | static int __init vmx_init(void) |
| 7670 | { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 7671 | int r, i, msr; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 7672 | |
| 7673 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 7674 | |
| 7675 | for (i = 0; i < NR_VMX_MSR; ++i) |
| 7676 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7677 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7678 | vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7679 | if (!vmx_io_bitmap_a) |
| 7680 | return -ENOMEM; |
| 7681 | |
Guo Chao | 2106a54 | 2012-06-15 11:31:56 +0800 | [diff] [blame] | 7682 | r = -ENOMEM; |
| 7683 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7684 | vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL); |
Guo Chao | 2106a54 | 2012-06-15 11:31:56 +0800 | [diff] [blame] | 7685 | if (!vmx_io_bitmap_b) |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7686 | goto out; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7687 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7688 | vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL); |
Guo Chao | 2106a54 | 2012-06-15 11:31:56 +0800 | [diff] [blame] | 7689 | if (!vmx_msr_bitmap_legacy) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7690 | goto out1; |
Guo Chao | 2106a54 | 2012-06-15 11:31:56 +0800 | [diff] [blame] | 7691 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 7692 | vmx_msr_bitmap_legacy_x2apic = |
| 7693 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7694 | if (!vmx_msr_bitmap_legacy_x2apic) |
| 7695 | goto out2; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7696 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7697 | vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL); |
Guo Chao | 2106a54 | 2012-06-15 11:31:56 +0800 | [diff] [blame] | 7698 | if (!vmx_msr_bitmap_longmode) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 7699 | goto out3; |
Guo Chao | 2106a54 | 2012-06-15 11:31:56 +0800 | [diff] [blame] | 7700 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 7701 | vmx_msr_bitmap_longmode_x2apic = |
| 7702 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7703 | if (!vmx_msr_bitmap_longmode_x2apic) |
| 7704 | goto out4; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7705 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7706 | /* |
| 7707 | * Allow direct access to the PC debug port (it is often used for I/O |
| 7708 | * delays, but the vmexits simply slow things down). |
| 7709 | */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7710 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); |
| 7711 | clear_bit(0x80, vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7712 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7713 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7714 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7715 | memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE); |
| 7716 | memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7717 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 7718 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 7719 | |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 7720 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
| 7721 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7722 | if (r) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7723 | goto out3; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7724 | |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 7725 | #ifdef CONFIG_KEXEC |
| 7726 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 7727 | crash_vmclear_local_loaded_vmcss); |
| 7728 | #endif |
| 7729 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7730 | vmx_disable_intercept_for_msr(MSR_FS_BASE, false); |
| 7731 | vmx_disable_intercept_for_msr(MSR_GS_BASE, false); |
| 7732 | vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true); |
| 7733 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false); |
| 7734 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false); |
| 7735 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 7736 | memcpy(vmx_msr_bitmap_legacy_x2apic, |
| 7737 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
| 7738 | memcpy(vmx_msr_bitmap_longmode_x2apic, |
| 7739 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
| 7740 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7741 | if (enable_apicv_reg_vid) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 7742 | for (msr = 0x800; msr <= 0x8ff; msr++) |
| 7743 | vmx_disable_intercept_msr_read_x2apic(msr); |
| 7744 | |
| 7745 | /* According SDM, in x2apic mode, the whole id reg is used. |
| 7746 | * But in KVM, it only use the highest eight bits. Need to |
| 7747 | * intercept it */ |
| 7748 | vmx_enable_intercept_msr_read_x2apic(0x802); |
| 7749 | /* TMCCT */ |
| 7750 | vmx_enable_intercept_msr_read_x2apic(0x839); |
| 7751 | /* TPR */ |
| 7752 | vmx_disable_intercept_msr_write_x2apic(0x808); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7753 | /* EOI */ |
| 7754 | vmx_disable_intercept_msr_write_x2apic(0x80b); |
| 7755 | /* SELF-IPI */ |
| 7756 | vmx_disable_intercept_msr_write_x2apic(0x83f); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 7757 | } |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7758 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 7759 | if (enable_ept) { |
Xudong Hao | 3f6d8c8 | 2012-05-22 11:23:15 +0800 | [diff] [blame] | 7760 | kvm_mmu_set_mask_ptes(0ull, |
| 7761 | (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull, |
| 7762 | (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull, |
| 7763 | 0ull, VMX_EPT_EXECUTABLE_MASK); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 7764 | ept_set_mmio_spte_mask(); |
Sheng Yang | 5fdbcb9 | 2008-07-16 09:25:40 +0800 | [diff] [blame] | 7765 | kvm_enable_tdp(); |
| 7766 | } else |
| 7767 | kvm_disable_tdp(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7768 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7769 | return 0; |
| 7770 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 7771 | out4: |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7772 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 7773 | out3: |
| 7774 | free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7775 | out2: |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7776 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7777 | out1: |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7778 | free_page((unsigned long)vmx_io_bitmap_b); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7779 | out: |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7780 | free_page((unsigned long)vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7781 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7782 | } |
| 7783 | |
| 7784 | static void __exit vmx_exit(void) |
| 7785 | { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 7786 | free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic); |
| 7787 | free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic); |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7788 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
| 7789 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7790 | free_page((unsigned long)vmx_io_bitmap_b); |
| 7791 | free_page((unsigned long)vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7792 | |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 7793 | #ifdef CONFIG_KEXEC |
| 7794 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, NULL); |
| 7795 | synchronize_rcu(); |
| 7796 | #endif |
| 7797 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 7798 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7799 | } |
| 7800 | |
| 7801 | module_init(vmx_init) |
| 7802 | module_exit(vmx_exit) |