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> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 45 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 46 | #include "trace.h" |
| 47 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 48 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 49 | #define __ex_clear(x, reg) \ |
| 50 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 51 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 52 | MODULE_AUTHOR("Qumranet"); |
| 53 | MODULE_LICENSE("GPL"); |
| 54 | |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 55 | static const struct x86_cpu_id vmx_cpu_id[] = { |
| 56 | X86_FEATURE_MATCH(X86_FEATURE_VMX), |
| 57 | {} |
| 58 | }; |
| 59 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); |
| 60 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 61 | static bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 62 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 63 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 64 | static bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 65 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 66 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 67 | static bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 68 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 69 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 70 | static bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 71 | module_param_named(unrestricted_guest, |
| 72 | enable_unrestricted_guest, bool, S_IRUGO); |
| 73 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame^] | 74 | static bool __read_mostly enable_ept_ad_bits = 1; |
| 75 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 76 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 77 | static bool __read_mostly emulate_invalid_guest_state = 0; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 78 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 79 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 80 | static bool __read_mostly vmm_exclusive = 1; |
Dongxiao Xu | b923e62 | 2010-05-11 18:29:45 +0800 | [diff] [blame] | 81 | module_param(vmm_exclusive, bool, S_IRUGO); |
| 82 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 83 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 84 | module_param(fasteoi, bool, S_IRUGO); |
| 85 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 86 | /* |
| 87 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 88 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 89 | * use VMX instructions. |
| 90 | */ |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 91 | static bool __read_mostly nested = 0; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 92 | module_param(nested, bool, S_IRUGO); |
| 93 | |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 94 | #define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \ |
| 95 | (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD) |
| 96 | #define KVM_GUEST_CR0_MASK \ |
| 97 | (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE) |
| 98 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \ |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 99 | (X86_CR0_WP | X86_CR0_NE) |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 100 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 101 | (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] | 102 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 103 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
| 104 | | X86_CR4_OSXMMEXCPT) |
| 105 | |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 106 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 107 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 108 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 109 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 110 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 111 | /* |
| 112 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 113 | * ple_gap: upper bound on the amount of time between two successive |
| 114 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 115 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 116 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 117 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 118 | * less than 2^12 cycles |
| 119 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 120 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 121 | */ |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 122 | #define KVM_VMX_DEFAULT_PLE_GAP 128 |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 123 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 |
| 124 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
| 125 | module_param(ple_gap, int, S_IRUGO); |
| 126 | |
| 127 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 128 | module_param(ple_window, int, S_IRUGO); |
| 129 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 130 | #define NR_AUTOLOAD_MSRS 8 |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 131 | #define VMCS02_POOL_SIZE 1 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 132 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 133 | struct vmcs { |
| 134 | u32 revision_id; |
| 135 | u32 abort; |
| 136 | char data[0]; |
| 137 | }; |
| 138 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 139 | /* |
| 140 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 141 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 142 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 143 | */ |
| 144 | struct loaded_vmcs { |
| 145 | struct vmcs *vmcs; |
| 146 | int cpu; |
| 147 | int launched; |
| 148 | struct list_head loaded_vmcss_on_cpu_link; |
| 149 | }; |
| 150 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 151 | struct shared_msr_entry { |
| 152 | unsigned index; |
| 153 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 154 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 155 | }; |
| 156 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 157 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 158 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 159 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 160 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 161 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 162 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 163 | * More than one of these structures may exist, if L1 runs multiple L2 guests. |
| 164 | * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the |
| 165 | * underlying hardware which will be used to run L2. |
| 166 | * This structure is packed to ensure that its layout is identical across |
| 167 | * machines (necessary for live migration). |
| 168 | * If there are changes in this struct, VMCS12_REVISION must be changed. |
| 169 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 170 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 171 | struct __packed vmcs12 { |
| 172 | /* According to the Intel spec, a VMCS region must start with the |
| 173 | * following two fields. Then follow implementation-specific data. |
| 174 | */ |
| 175 | u32 revision_id; |
| 176 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 177 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 178 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 179 | u32 padding[7]; /* room for future expansion */ |
| 180 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 181 | u64 io_bitmap_a; |
| 182 | u64 io_bitmap_b; |
| 183 | u64 msr_bitmap; |
| 184 | u64 vm_exit_msr_store_addr; |
| 185 | u64 vm_exit_msr_load_addr; |
| 186 | u64 vm_entry_msr_load_addr; |
| 187 | u64 tsc_offset; |
| 188 | u64 virtual_apic_page_addr; |
| 189 | u64 apic_access_addr; |
| 190 | u64 ept_pointer; |
| 191 | u64 guest_physical_address; |
| 192 | u64 vmcs_link_pointer; |
| 193 | u64 guest_ia32_debugctl; |
| 194 | u64 guest_ia32_pat; |
| 195 | u64 guest_ia32_efer; |
| 196 | u64 guest_ia32_perf_global_ctrl; |
| 197 | u64 guest_pdptr0; |
| 198 | u64 guest_pdptr1; |
| 199 | u64 guest_pdptr2; |
| 200 | u64 guest_pdptr3; |
| 201 | u64 host_ia32_pat; |
| 202 | u64 host_ia32_efer; |
| 203 | u64 host_ia32_perf_global_ctrl; |
| 204 | u64 padding64[8]; /* room for future expansion */ |
| 205 | /* |
| 206 | * To allow migration of L1 (complete with its L2 guests) between |
| 207 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 208 | * unsigned long fields with no explict size. We use u64 (aliased |
| 209 | * natural_width) instead. Luckily, x86 is little-endian. |
| 210 | */ |
| 211 | natural_width cr0_guest_host_mask; |
| 212 | natural_width cr4_guest_host_mask; |
| 213 | natural_width cr0_read_shadow; |
| 214 | natural_width cr4_read_shadow; |
| 215 | natural_width cr3_target_value0; |
| 216 | natural_width cr3_target_value1; |
| 217 | natural_width cr3_target_value2; |
| 218 | natural_width cr3_target_value3; |
| 219 | natural_width exit_qualification; |
| 220 | natural_width guest_linear_address; |
| 221 | natural_width guest_cr0; |
| 222 | natural_width guest_cr3; |
| 223 | natural_width guest_cr4; |
| 224 | natural_width guest_es_base; |
| 225 | natural_width guest_cs_base; |
| 226 | natural_width guest_ss_base; |
| 227 | natural_width guest_ds_base; |
| 228 | natural_width guest_fs_base; |
| 229 | natural_width guest_gs_base; |
| 230 | natural_width guest_ldtr_base; |
| 231 | natural_width guest_tr_base; |
| 232 | natural_width guest_gdtr_base; |
| 233 | natural_width guest_idtr_base; |
| 234 | natural_width guest_dr7; |
| 235 | natural_width guest_rsp; |
| 236 | natural_width guest_rip; |
| 237 | natural_width guest_rflags; |
| 238 | natural_width guest_pending_dbg_exceptions; |
| 239 | natural_width guest_sysenter_esp; |
| 240 | natural_width guest_sysenter_eip; |
| 241 | natural_width host_cr0; |
| 242 | natural_width host_cr3; |
| 243 | natural_width host_cr4; |
| 244 | natural_width host_fs_base; |
| 245 | natural_width host_gs_base; |
| 246 | natural_width host_tr_base; |
| 247 | natural_width host_gdtr_base; |
| 248 | natural_width host_idtr_base; |
| 249 | natural_width host_ia32_sysenter_esp; |
| 250 | natural_width host_ia32_sysenter_eip; |
| 251 | natural_width host_rsp; |
| 252 | natural_width host_rip; |
| 253 | natural_width paddingl[8]; /* room for future expansion */ |
| 254 | u32 pin_based_vm_exec_control; |
| 255 | u32 cpu_based_vm_exec_control; |
| 256 | u32 exception_bitmap; |
| 257 | u32 page_fault_error_code_mask; |
| 258 | u32 page_fault_error_code_match; |
| 259 | u32 cr3_target_count; |
| 260 | u32 vm_exit_controls; |
| 261 | u32 vm_exit_msr_store_count; |
| 262 | u32 vm_exit_msr_load_count; |
| 263 | u32 vm_entry_controls; |
| 264 | u32 vm_entry_msr_load_count; |
| 265 | u32 vm_entry_intr_info_field; |
| 266 | u32 vm_entry_exception_error_code; |
| 267 | u32 vm_entry_instruction_len; |
| 268 | u32 tpr_threshold; |
| 269 | u32 secondary_vm_exec_control; |
| 270 | u32 vm_instruction_error; |
| 271 | u32 vm_exit_reason; |
| 272 | u32 vm_exit_intr_info; |
| 273 | u32 vm_exit_intr_error_code; |
| 274 | u32 idt_vectoring_info_field; |
| 275 | u32 idt_vectoring_error_code; |
| 276 | u32 vm_exit_instruction_len; |
| 277 | u32 vmx_instruction_info; |
| 278 | u32 guest_es_limit; |
| 279 | u32 guest_cs_limit; |
| 280 | u32 guest_ss_limit; |
| 281 | u32 guest_ds_limit; |
| 282 | u32 guest_fs_limit; |
| 283 | u32 guest_gs_limit; |
| 284 | u32 guest_ldtr_limit; |
| 285 | u32 guest_tr_limit; |
| 286 | u32 guest_gdtr_limit; |
| 287 | u32 guest_idtr_limit; |
| 288 | u32 guest_es_ar_bytes; |
| 289 | u32 guest_cs_ar_bytes; |
| 290 | u32 guest_ss_ar_bytes; |
| 291 | u32 guest_ds_ar_bytes; |
| 292 | u32 guest_fs_ar_bytes; |
| 293 | u32 guest_gs_ar_bytes; |
| 294 | u32 guest_ldtr_ar_bytes; |
| 295 | u32 guest_tr_ar_bytes; |
| 296 | u32 guest_interruptibility_info; |
| 297 | u32 guest_activity_state; |
| 298 | u32 guest_sysenter_cs; |
| 299 | u32 host_ia32_sysenter_cs; |
| 300 | u32 padding32[8]; /* room for future expansion */ |
| 301 | u16 virtual_processor_id; |
| 302 | u16 guest_es_selector; |
| 303 | u16 guest_cs_selector; |
| 304 | u16 guest_ss_selector; |
| 305 | u16 guest_ds_selector; |
| 306 | u16 guest_fs_selector; |
| 307 | u16 guest_gs_selector; |
| 308 | u16 guest_ldtr_selector; |
| 309 | u16 guest_tr_selector; |
| 310 | u16 host_es_selector; |
| 311 | u16 host_cs_selector; |
| 312 | u16 host_ss_selector; |
| 313 | u16 host_ds_selector; |
| 314 | u16 host_fs_selector; |
| 315 | u16 host_gs_selector; |
| 316 | u16 host_tr_selector; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 317 | }; |
| 318 | |
| 319 | /* |
| 320 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 321 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 322 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. |
| 323 | */ |
| 324 | #define VMCS12_REVISION 0x11e57ed0 |
| 325 | |
| 326 | /* |
| 327 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 328 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 329 | * current implementation, 4K are reserved to avoid future complications. |
| 330 | */ |
| 331 | #define VMCS12_SIZE 0x1000 |
| 332 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 333 | /* Used to remember the last vmcs02 used for some recently used vmcs12s */ |
| 334 | struct vmcs02_list { |
| 335 | struct list_head list; |
| 336 | gpa_t vmptr; |
| 337 | struct loaded_vmcs vmcs02; |
| 338 | }; |
| 339 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 340 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 341 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 342 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 343 | */ |
| 344 | struct nested_vmx { |
| 345 | /* Has the level1 guest done vmxon? */ |
| 346 | bool vmxon; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 347 | |
| 348 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 349 | gpa_t current_vmptr; |
| 350 | /* The host-usable pointer to the above */ |
| 351 | struct page *current_vmcs12_page; |
| 352 | struct vmcs12 *current_vmcs12; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 353 | |
| 354 | /* vmcs02_list cache of VMCSs recently used to run L2 guests */ |
| 355 | struct list_head vmcs02_pool; |
| 356 | int vmcs02_num; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 357 | u64 vmcs01_tsc_offset; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 358 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 359 | bool nested_run_pending; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 360 | /* |
| 361 | * Guest pages referred to in vmcs02 with host-physical pointers, so |
| 362 | * we must keep them pinned while L2 runs. |
| 363 | */ |
| 364 | struct page *apic_access_page; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 365 | }; |
| 366 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 367 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 368 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd49 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 369 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 370 | u8 fail; |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 371 | u8 cpl; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 372 | bool nmi_known_unmasked; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 373 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 374 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 375 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 376 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 377 | int nmsrs; |
| 378 | int save_nmsrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 379 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 380 | u64 msr_host_kernel_gs_base; |
| 381 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 382 | #endif |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 383 | /* |
| 384 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 385 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 386 | * guest (L2), it points to a different VMCS. |
| 387 | */ |
| 388 | struct loaded_vmcs vmcs01; |
| 389 | struct loaded_vmcs *loaded_vmcs; |
| 390 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 391 | struct msr_autoload { |
| 392 | unsigned nr; |
| 393 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 394 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 395 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 396 | struct { |
| 397 | int loaded; |
| 398 | u16 fs_sel, gs_sel, ldt_sel; |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 399 | #ifdef CONFIG_X86_64 |
| 400 | u16 ds_sel, es_sel; |
| 401 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 402 | int gs_ldt_reload_needed; |
| 403 | int fs_reload_needed; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 404 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 405 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 406 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 407 | ulong save_rflags; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 408 | struct kvm_save_segment { |
| 409 | u16 selector; |
| 410 | unsigned long base; |
| 411 | u32 limit; |
| 412 | u32 ar; |
| 413 | } tr, es, ds, fs, gs; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 414 | } rmode; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 415 | struct { |
| 416 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
| 417 | struct kvm_save_segment seg[8]; |
| 418 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 419 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 420 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 421 | |
| 422 | /* Support for vnmi-less CPUs */ |
| 423 | int soft_vnmi_blocked; |
| 424 | ktime_t entry_time; |
| 425 | s64 vnmi_blocked_time; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 426 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 427 | |
| 428 | bool rdtscp_enabled; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 429 | |
| 430 | /* Support for a guest hypervisor (nested VMX) */ |
| 431 | struct nested_vmx nested; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 432 | }; |
| 433 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 434 | enum segment_cache_field { |
| 435 | SEG_FIELD_SEL = 0, |
| 436 | SEG_FIELD_BASE = 1, |
| 437 | SEG_FIELD_LIMIT = 2, |
| 438 | SEG_FIELD_AR = 3, |
| 439 | |
| 440 | SEG_FIELD_NR = 4 |
| 441 | }; |
| 442 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 443 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 444 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 445 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 446 | } |
| 447 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 448 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
| 449 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) |
| 450 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ |
| 451 | [number##_HIGH] = VMCS12_OFFSET(name)+4 |
| 452 | |
| 453 | static unsigned short vmcs_field_to_offset_table[] = { |
| 454 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
| 455 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 456 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 457 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 458 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 459 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 460 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 461 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 462 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
| 463 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 464 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 465 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 466 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 467 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 468 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 469 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 470 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 471 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 472 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 473 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 474 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 475 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
| 476 | FIELD64(TSC_OFFSET, tsc_offset), |
| 477 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 478 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
| 479 | FIELD64(EPT_POINTER, ept_pointer), |
| 480 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 481 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 482 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 483 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 484 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 485 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 486 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 487 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 488 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 489 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
| 490 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 491 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 492 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 493 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 494 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 495 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 496 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 497 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 498 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 499 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 500 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 501 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 502 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 503 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 504 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 505 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 506 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 507 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 508 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 509 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 510 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 511 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 512 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 513 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 514 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 515 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 516 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 517 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 518 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 519 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 520 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 521 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 522 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 523 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 524 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 525 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 526 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 527 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 528 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 529 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 530 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 531 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 532 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 533 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 534 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 535 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 536 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 537 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 538 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
| 539 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 540 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 541 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 542 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 543 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 544 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 545 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 546 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 547 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 548 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 549 | FIELD(GUEST_CR0, guest_cr0), |
| 550 | FIELD(GUEST_CR3, guest_cr3), |
| 551 | FIELD(GUEST_CR4, guest_cr4), |
| 552 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 553 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 554 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 555 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 556 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 557 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 558 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 559 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 560 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 561 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 562 | FIELD(GUEST_DR7, guest_dr7), |
| 563 | FIELD(GUEST_RSP, guest_rsp), |
| 564 | FIELD(GUEST_RIP, guest_rip), |
| 565 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 566 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 567 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 568 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 569 | FIELD(HOST_CR0, host_cr0), |
| 570 | FIELD(HOST_CR3, host_cr3), |
| 571 | FIELD(HOST_CR4, host_cr4), |
| 572 | FIELD(HOST_FS_BASE, host_fs_base), |
| 573 | FIELD(HOST_GS_BASE, host_gs_base), |
| 574 | FIELD(HOST_TR_BASE, host_tr_base), |
| 575 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 576 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 577 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 578 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 579 | FIELD(HOST_RSP, host_rsp), |
| 580 | FIELD(HOST_RIP, host_rip), |
| 581 | }; |
| 582 | static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table); |
| 583 | |
| 584 | static inline short vmcs_field_to_offset(unsigned long field) |
| 585 | { |
| 586 | if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0) |
| 587 | return -1; |
| 588 | return vmcs_field_to_offset_table[field]; |
| 589 | } |
| 590 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 591 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 592 | { |
| 593 | return to_vmx(vcpu)->nested.current_vmcs12; |
| 594 | } |
| 595 | |
| 596 | static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr) |
| 597 | { |
| 598 | struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT); |
| 599 | if (is_error_page(page)) { |
| 600 | kvm_release_page_clean(page); |
| 601 | return NULL; |
| 602 | } |
| 603 | return page; |
| 604 | } |
| 605 | |
| 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); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 621 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 622 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 623 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 624 | /* |
| 625 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 626 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 627 | */ |
| 628 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 629 | static DEFINE_PER_CPU(struct desc_ptr, host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 630 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 631 | static unsigned long *vmx_io_bitmap_a; |
| 632 | static unsigned long *vmx_io_bitmap_b; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 633 | static unsigned long *vmx_msr_bitmap_legacy; |
| 634 | static unsigned long *vmx_msr_bitmap_longmode; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 635 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 636 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 637 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 638 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 639 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 640 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 641 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 642 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 643 | int size; |
| 644 | int order; |
| 645 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 646 | u32 pin_based_exec_ctrl; |
| 647 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 648 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 649 | u32 vmexit_ctrl; |
| 650 | u32 vmentry_ctrl; |
| 651 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 652 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 653 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 654 | u32 ept; |
| 655 | u32 vpid; |
| 656 | } vmx_capability; |
| 657 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 658 | #define VMX_SEGMENT_FIELD(seg) \ |
| 659 | [VCPU_SREG_##seg] = { \ |
| 660 | .selector = GUEST_##seg##_SELECTOR, \ |
| 661 | .base = GUEST_##seg##_BASE, \ |
| 662 | .limit = GUEST_##seg##_LIMIT, \ |
| 663 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 664 | } |
| 665 | |
| 666 | static struct kvm_vmx_segment_field { |
| 667 | unsigned selector; |
| 668 | unsigned base; |
| 669 | unsigned limit; |
| 670 | unsigned ar_bytes; |
| 671 | } kvm_vmx_segment_fields[] = { |
| 672 | VMX_SEGMENT_FIELD(CS), |
| 673 | VMX_SEGMENT_FIELD(DS), |
| 674 | VMX_SEGMENT_FIELD(ES), |
| 675 | VMX_SEGMENT_FIELD(FS), |
| 676 | VMX_SEGMENT_FIELD(GS), |
| 677 | VMX_SEGMENT_FIELD(SS), |
| 678 | VMX_SEGMENT_FIELD(TR), |
| 679 | VMX_SEGMENT_FIELD(LDTR), |
| 680 | }; |
| 681 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 682 | static u64 host_efer; |
| 683 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 684 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 685 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 686 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 687 | * 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] | 688 | * away by decrementing the array size. |
| 689 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 690 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 691 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 692 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 693 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 694 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 695 | }; |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 696 | #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 697 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 698 | static inline bool is_page_fault(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 699 | { |
| 700 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 701 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 702 | (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 703 | } |
| 704 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 705 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 706 | { |
| 707 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 708 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 709 | (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 710 | } |
| 711 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 712 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 713 | { |
| 714 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 715 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 716 | (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 717 | } |
| 718 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 719 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 720 | { |
| 721 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 722 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 723 | } |
| 724 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 725 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 726 | { |
| 727 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 728 | INTR_INFO_VALID_MASK)) == |
| 729 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 730 | } |
| 731 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 732 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 733 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 734 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 735 | } |
| 736 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 737 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 738 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 739 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 740 | } |
| 741 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 742 | static inline bool vm_need_tpr_shadow(struct kvm *kvm) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 743 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 744 | return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 745 | } |
| 746 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 747 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 748 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 749 | return vmcs_config.cpu_based_exec_ctrl & |
| 750 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 751 | } |
| 752 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 753 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 754 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 755 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 756 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 757 | } |
| 758 | |
| 759 | static inline bool cpu_has_vmx_flexpriority(void) |
| 760 | { |
| 761 | return cpu_has_vmx_tpr_shadow() && |
| 762 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 763 | } |
| 764 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 765 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 766 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 767 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | static inline bool cpu_has_vmx_eptp_uncacheable(void) |
| 771 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 772 | return vmx_capability.ept & VMX_EPTP_UC_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | static inline bool cpu_has_vmx_eptp_writeback(void) |
| 776 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 777 | return vmx_capability.ept & VMX_EPTP_WB_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 781 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 782 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 783 | } |
| 784 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 785 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 786 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 787 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 788 | } |
| 789 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 790 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 791 | { |
| 792 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 793 | } |
| 794 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame^] | 795 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 796 | { |
| 797 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 798 | } |
| 799 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 800 | static inline bool cpu_has_vmx_invept_individual_addr(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 801 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 802 | return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 803 | } |
| 804 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 805 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 806 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 807 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 808 | } |
| 809 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 810 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 811 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 812 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 813 | } |
| 814 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 815 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 816 | { |
| 817 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 818 | } |
| 819 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 820 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 821 | { |
| 822 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 823 | } |
| 824 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 825 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 826 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 827 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 828 | SECONDARY_EXEC_ENABLE_EPT; |
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_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 832 | { |
| 833 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 834 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 835 | } |
| 836 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 837 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 838 | { |
| 839 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 840 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 841 | } |
| 842 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 843 | static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 844 | { |
Gui Jianfeng | 6d3e435 | 2010-01-29 15:36:59 +0800 | [diff] [blame] | 845 | return flexpriority_enabled && irqchip_in_kernel(kvm); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 846 | } |
| 847 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 848 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 849 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 850 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 851 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 852 | } |
| 853 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 854 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 855 | { |
| 856 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 857 | SECONDARY_EXEC_RDTSCP; |
| 858 | } |
| 859 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 860 | static inline bool cpu_has_virtual_nmis(void) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 861 | { |
| 862 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 863 | } |
| 864 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 865 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 866 | { |
| 867 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 868 | SECONDARY_EXEC_WBINVD_EXITING; |
| 869 | } |
| 870 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 871 | static inline bool report_flexpriority(void) |
| 872 | { |
| 873 | return flexpriority_enabled; |
| 874 | } |
| 875 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 876 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 877 | { |
| 878 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 879 | } |
| 880 | |
| 881 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 882 | { |
| 883 | return (vmcs12->cpu_based_vm_exec_control & |
| 884 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 885 | (vmcs12->secondary_vm_exec_control & bit); |
| 886 | } |
| 887 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 888 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12, |
| 889 | struct kvm_vcpu *vcpu) |
| 890 | { |
| 891 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 892 | } |
| 893 | |
| 894 | static inline bool is_exception(u32 intr_info) |
| 895 | { |
| 896 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 897 | == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK); |
| 898 | } |
| 899 | |
| 900 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 901 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 902 | struct vmcs12 *vmcs12, |
| 903 | u32 reason, unsigned long qualification); |
| 904 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 905 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 906 | { |
| 907 | int i; |
| 908 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 909 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 910 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 911 | return i; |
| 912 | return -1; |
| 913 | } |
| 914 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 915 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 916 | { |
| 917 | struct { |
| 918 | u64 vpid : 16; |
| 919 | u64 rsvd : 48; |
| 920 | u64 gva; |
| 921 | } operand = { vpid, 0, gva }; |
| 922 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 923 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 924 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 925 | "; ja 1f ; ud2 ; 1:" |
| 926 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 927 | } |
| 928 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 929 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 930 | { |
| 931 | struct { |
| 932 | u64 eptp, gpa; |
| 933 | } operand = {eptp, gpa}; |
| 934 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 935 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 936 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 937 | "; ja 1f ; ud2 ; 1:\n" |
| 938 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 939 | } |
| 940 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 941 | 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] | 942 | { |
| 943 | int i; |
| 944 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 945 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 946 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 947 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 948 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 949 | } |
| 950 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 951 | static void vmcs_clear(struct vmcs *vmcs) |
| 952 | { |
| 953 | u64 phys_addr = __pa(vmcs); |
| 954 | u8 error; |
| 955 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 956 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 957 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 958 | : "cc", "memory"); |
| 959 | if (error) |
| 960 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 961 | vmcs, phys_addr); |
| 962 | } |
| 963 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 964 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 965 | { |
| 966 | vmcs_clear(loaded_vmcs->vmcs); |
| 967 | loaded_vmcs->cpu = -1; |
| 968 | loaded_vmcs->launched = 0; |
| 969 | } |
| 970 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 971 | static void vmcs_load(struct vmcs *vmcs) |
| 972 | { |
| 973 | u64 phys_addr = __pa(vmcs); |
| 974 | u8 error; |
| 975 | |
| 976 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 977 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 978 | : "cc", "memory"); |
| 979 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 980 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 981 | vmcs, phys_addr); |
| 982 | } |
| 983 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 984 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 985 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 986 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 987 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 988 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 989 | if (loaded_vmcs->cpu != cpu) |
| 990 | return; /* vcpu migration can race with cpu offline */ |
| 991 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 992 | per_cpu(current_vmcs, cpu) = NULL; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 993 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
| 994 | loaded_vmcs_init(loaded_vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 995 | } |
| 996 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 997 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 998 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 999 | if (loaded_vmcs->cpu != -1) |
| 1000 | smp_call_function_single( |
| 1001 | loaded_vmcs->cpu, __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
Gui Jianfeng | 1760dd4 | 2010-06-07 10:33:27 +0800 | [diff] [blame] | 1004 | static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1005 | { |
| 1006 | if (vmx->vpid == 0) |
| 1007 | return; |
| 1008 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1009 | if (cpu_has_vmx_invvpid_single()) |
| 1010 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1011 | } |
| 1012 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1013 | static inline void vpid_sync_vcpu_global(void) |
| 1014 | { |
| 1015 | if (cpu_has_vmx_invvpid_global()) |
| 1016 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 1017 | } |
| 1018 | |
| 1019 | static inline void vpid_sync_context(struct vcpu_vmx *vmx) |
| 1020 | { |
| 1021 | if (cpu_has_vmx_invvpid_single()) |
Gui Jianfeng | 1760dd4 | 2010-06-07 10:33:27 +0800 | [diff] [blame] | 1022 | vpid_sync_vcpu_single(vmx); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1023 | else |
| 1024 | vpid_sync_vcpu_global(); |
| 1025 | } |
| 1026 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1027 | static inline void ept_sync_global(void) |
| 1028 | { |
| 1029 | if (cpu_has_vmx_invept_global()) |
| 1030 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
| 1031 | } |
| 1032 | |
| 1033 | static inline void ept_sync_context(u64 eptp) |
| 1034 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1035 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1036 | if (cpu_has_vmx_invept_context()) |
| 1037 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 1038 | else |
| 1039 | ept_sync_global(); |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa) |
| 1044 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1045 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1046 | if (cpu_has_vmx_invept_individual_addr()) |
| 1047 | __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR, |
| 1048 | eptp, gpa); |
| 1049 | else |
| 1050 | ept_sync_context(eptp); |
| 1051 | } |
| 1052 | } |
| 1053 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1054 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1055 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1056 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1057 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1058 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 1059 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1060 | return value; |
| 1061 | } |
| 1062 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1063 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1064 | { |
| 1065 | return vmcs_readl(field); |
| 1066 | } |
| 1067 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1068 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1069 | { |
| 1070 | return vmcs_readl(field); |
| 1071 | } |
| 1072 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1073 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1074 | { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1075 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1076 | return vmcs_readl(field); |
| 1077 | #else |
| 1078 | return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32); |
| 1079 | #endif |
| 1080 | } |
| 1081 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1082 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 1083 | { |
| 1084 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 1085 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 1086 | dump_stack(); |
| 1087 | } |
| 1088 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1089 | static void vmcs_writel(unsigned long field, unsigned long value) |
| 1090 | { |
| 1091 | u8 error; |
| 1092 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1093 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1094 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1095 | if (unlikely(error)) |
| 1096 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | static void vmcs_write16(unsigned long field, u16 value) |
| 1100 | { |
| 1101 | vmcs_writel(field, value); |
| 1102 | } |
| 1103 | |
| 1104 | static void vmcs_write32(unsigned long field, u32 value) |
| 1105 | { |
| 1106 | vmcs_writel(field, value); |
| 1107 | } |
| 1108 | |
| 1109 | static void vmcs_write64(unsigned long field, u64 value) |
| 1110 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1111 | vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 1112 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1113 | asm volatile (""); |
| 1114 | vmcs_writel(field+1, value >> 32); |
| 1115 | #endif |
| 1116 | } |
| 1117 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1118 | static void vmcs_clear_bits(unsigned long field, u32 mask) |
| 1119 | { |
| 1120 | vmcs_writel(field, vmcs_readl(field) & ~mask); |
| 1121 | } |
| 1122 | |
| 1123 | static void vmcs_set_bits(unsigned long field, u32 mask) |
| 1124 | { |
| 1125 | vmcs_writel(field, vmcs_readl(field) | mask); |
| 1126 | } |
| 1127 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1128 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 1129 | { |
| 1130 | vmx->segment_cache.bitmask = 0; |
| 1131 | } |
| 1132 | |
| 1133 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 1134 | unsigned field) |
| 1135 | { |
| 1136 | bool ret; |
| 1137 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 1138 | |
| 1139 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 1140 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 1141 | vmx->segment_cache.bitmask = 0; |
| 1142 | } |
| 1143 | ret = vmx->segment_cache.bitmask & mask; |
| 1144 | vmx->segment_cache.bitmask |= mask; |
| 1145 | return ret; |
| 1146 | } |
| 1147 | |
| 1148 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 1149 | { |
| 1150 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 1151 | |
| 1152 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 1153 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 1154 | return *p; |
| 1155 | } |
| 1156 | |
| 1157 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 1158 | { |
| 1159 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 1160 | |
| 1161 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 1162 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 1163 | return *p; |
| 1164 | } |
| 1165 | |
| 1166 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 1167 | { |
| 1168 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 1169 | |
| 1170 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 1171 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 1172 | return *p; |
| 1173 | } |
| 1174 | |
| 1175 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 1176 | { |
| 1177 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 1178 | |
| 1179 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 1180 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 1181 | return *p; |
| 1182 | } |
| 1183 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1184 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 1185 | { |
| 1186 | u32 eb; |
| 1187 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1188 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
| 1189 | (1u << NM_VECTOR) | (1u << DB_VECTOR); |
| 1190 | if ((vcpu->guest_debug & |
| 1191 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 1192 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 1193 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1194 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1195 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1196 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1197 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1198 | if (vcpu->fpu_active) |
| 1199 | eb &= ~(1u << NM_VECTOR); |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1200 | |
| 1201 | /* When we are running a nested L2 guest and L1 specified for it a |
| 1202 | * certain exception bitmap, we must trap the same exceptions and pass |
| 1203 | * them to L1. When running L2, we will only handle the exceptions |
| 1204 | * specified above if L1 did not want them. |
| 1205 | */ |
| 1206 | if (is_guest_mode(vcpu)) |
| 1207 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 1208 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1209 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 1210 | } |
| 1211 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1212 | static void clear_atomic_switch_msr_special(unsigned long entry, |
| 1213 | unsigned long exit) |
| 1214 | { |
| 1215 | vmcs_clear_bits(VM_ENTRY_CONTROLS, entry); |
| 1216 | vmcs_clear_bits(VM_EXIT_CONTROLS, exit); |
| 1217 | } |
| 1218 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1219 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 1220 | { |
| 1221 | unsigned i; |
| 1222 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1223 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1224 | switch (msr) { |
| 1225 | case MSR_EFER: |
| 1226 | if (cpu_has_load_ia32_efer) { |
| 1227 | clear_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER, |
| 1228 | VM_EXIT_LOAD_IA32_EFER); |
| 1229 | return; |
| 1230 | } |
| 1231 | break; |
| 1232 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1233 | if (cpu_has_load_perf_global_ctrl) { |
| 1234 | clear_atomic_switch_msr_special( |
| 1235 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1236 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 1237 | return; |
| 1238 | } |
| 1239 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1240 | } |
| 1241 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1242 | for (i = 0; i < m->nr; ++i) |
| 1243 | if (m->guest[i].index == msr) |
| 1244 | break; |
| 1245 | |
| 1246 | if (i == m->nr) |
| 1247 | return; |
| 1248 | --m->nr; |
| 1249 | m->guest[i] = m->guest[m->nr]; |
| 1250 | m->host[i] = m->host[m->nr]; |
| 1251 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1252 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1253 | } |
| 1254 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1255 | static void add_atomic_switch_msr_special(unsigned long entry, |
| 1256 | unsigned long exit, unsigned long guest_val_vmcs, |
| 1257 | unsigned long host_val_vmcs, u64 guest_val, u64 host_val) |
| 1258 | { |
| 1259 | vmcs_write64(guest_val_vmcs, guest_val); |
| 1260 | vmcs_write64(host_val_vmcs, host_val); |
| 1261 | vmcs_set_bits(VM_ENTRY_CONTROLS, entry); |
| 1262 | vmcs_set_bits(VM_EXIT_CONTROLS, exit); |
| 1263 | } |
| 1264 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1265 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 1266 | u64 guest_val, u64 host_val) |
| 1267 | { |
| 1268 | unsigned i; |
| 1269 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1270 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1271 | switch (msr) { |
| 1272 | case MSR_EFER: |
| 1273 | if (cpu_has_load_ia32_efer) { |
| 1274 | add_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER, |
| 1275 | VM_EXIT_LOAD_IA32_EFER, |
| 1276 | GUEST_IA32_EFER, |
| 1277 | HOST_IA32_EFER, |
| 1278 | guest_val, host_val); |
| 1279 | return; |
| 1280 | } |
| 1281 | break; |
| 1282 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1283 | if (cpu_has_load_perf_global_ctrl) { |
| 1284 | add_atomic_switch_msr_special( |
| 1285 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1286 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1287 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 1288 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 1289 | guest_val, host_val); |
| 1290 | return; |
| 1291 | } |
| 1292 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1295 | for (i = 0; i < m->nr; ++i) |
| 1296 | if (m->guest[i].index == msr) |
| 1297 | break; |
| 1298 | |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 1299 | if (i == NR_AUTOLOAD_MSRS) { |
| 1300 | printk_once(KERN_WARNING"Not enough mst switch entries. " |
| 1301 | "Can't add msr %x\n", msr); |
| 1302 | return; |
| 1303 | } else if (i == m->nr) { |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1304 | ++m->nr; |
| 1305 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1306 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1307 | } |
| 1308 | |
| 1309 | m->guest[i].index = msr; |
| 1310 | m->guest[i].value = guest_val; |
| 1311 | m->host[i].index = msr; |
| 1312 | m->host[i].value = host_val; |
| 1313 | } |
| 1314 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1315 | static void reload_tss(void) |
| 1316 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1317 | /* |
| 1318 | * VT restores TR but not its size. Useless. |
| 1319 | */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1320 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 1321 | struct desc_struct *descs; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1322 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1323 | descs = (void *)gdt->address; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1324 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 1325 | load_TR_desc(); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1326 | } |
| 1327 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1328 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1329 | { |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 1330 | u64 guest_efer; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1331 | u64 ignore_bits; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1332 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1333 | guest_efer = vmx->vcpu.arch.efer; |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 1334 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1335 | /* |
| 1336 | * NX is emulated; LMA and LME handled by hardware; SCE meaninless |
| 1337 | * outside long mode |
| 1338 | */ |
| 1339 | ignore_bits = EFER_NX | EFER_SCE; |
| 1340 | #ifdef CONFIG_X86_64 |
| 1341 | ignore_bits |= EFER_LMA | EFER_LME; |
| 1342 | /* SCE is meaningful only in long mode on Intel */ |
| 1343 | if (guest_efer & EFER_LMA) |
| 1344 | ignore_bits &= ~(u64)EFER_SCE; |
| 1345 | #endif |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1346 | guest_efer &= ~ignore_bits; |
| 1347 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1348 | vmx->guest_msrs[efer_offset].data = guest_efer; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 1349 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 1350 | |
| 1351 | clear_atomic_switch_msr(vmx, MSR_EFER); |
| 1352 | /* On ept, can't emulate nx, and must switch nx atomically */ |
| 1353 | if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) { |
| 1354 | guest_efer = vmx->vcpu.arch.efer; |
| 1355 | if (!(guest_efer & EFER_LMA)) |
| 1356 | guest_efer &= ~EFER_LME; |
| 1357 | add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer); |
| 1358 | return false; |
| 1359 | } |
| 1360 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1361 | return true; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1362 | } |
| 1363 | |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1364 | static unsigned long segment_base(u16 selector) |
| 1365 | { |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1366 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1367 | struct desc_struct *d; |
| 1368 | unsigned long table_base; |
| 1369 | unsigned long v; |
| 1370 | |
| 1371 | if (!(selector & ~3)) |
| 1372 | return 0; |
| 1373 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1374 | table_base = gdt->address; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1375 | |
| 1376 | if (selector & 4) { /* from ldt */ |
| 1377 | u16 ldt_selector = kvm_read_ldt(); |
| 1378 | |
| 1379 | if (!(ldt_selector & ~3)) |
| 1380 | return 0; |
| 1381 | |
| 1382 | table_base = segment_base(ldt_selector); |
| 1383 | } |
| 1384 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
| 1385 | v = get_desc_base(d); |
| 1386 | #ifdef CONFIG_X86_64 |
| 1387 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 1388 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; |
| 1389 | #endif |
| 1390 | return v; |
| 1391 | } |
| 1392 | |
| 1393 | static inline unsigned long kvm_read_tr_base(void) |
| 1394 | { |
| 1395 | u16 tr; |
| 1396 | asm("str %0" : "=g"(tr)); |
| 1397 | return segment_base(tr); |
| 1398 | } |
| 1399 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1400 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1401 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1402 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1403 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1404 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1405 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1406 | return; |
| 1407 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1408 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1409 | /* |
| 1410 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 1411 | * allow segment selectors with cpl > 0 or ti == 1. |
| 1412 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1413 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1414 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1415 | savesegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1416 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1417 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1418 | vmx->host_state.fs_reload_needed = 0; |
| 1419 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1420 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1421 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1422 | } |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1423 | savesegment(gs, vmx->host_state.gs_sel); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1424 | if (!(vmx->host_state.gs_sel & 7)) |
| 1425 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1426 | else { |
| 1427 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1428 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | #ifdef CONFIG_X86_64 |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1432 | savesegment(ds, vmx->host_state.ds_sel); |
| 1433 | savesegment(es, vmx->host_state.es_sel); |
| 1434 | #endif |
| 1435 | |
| 1436 | #ifdef CONFIG_X86_64 |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1437 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 1438 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 1439 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1440 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 1441 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1442 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 1443 | |
| 1444 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1445 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
| 1446 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1447 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 1448 | #endif |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1449 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 1450 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 1451 | vmx->guest_msrs[i].data, |
| 1452 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1453 | } |
| 1454 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1455 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1456 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1457 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1458 | return; |
| 1459 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 1460 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1461 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1462 | #ifdef CONFIG_X86_64 |
| 1463 | if (is_long_mode(&vmx->vcpu)) |
| 1464 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 1465 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1466 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1467 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1468 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1469 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1470 | #else |
| 1471 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1472 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1473 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 1474 | if (vmx->host_state.fs_reload_needed) |
| 1475 | loadsegment(fs, vmx->host_state.fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1476 | #ifdef CONFIG_X86_64 |
| 1477 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { |
| 1478 | loadsegment(ds, vmx->host_state.ds_sel); |
| 1479 | loadsegment(es, vmx->host_state.es_sel); |
| 1480 | } |
| 1481 | #else |
| 1482 | /* |
| 1483 | * The sysexit path does not restore ds/es, so we must set them to |
| 1484 | * a reasonable value ourselves. |
| 1485 | */ |
| 1486 | loadsegment(ds, __USER_DS); |
| 1487 | loadsegment(es, __USER_DS); |
| 1488 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1489 | reload_tss(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1490 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1491 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1492 | #endif |
Linus Torvalds | 1361b83 | 2012-02-21 13:19:22 -0800 | [diff] [blame] | 1493 | if (user_has_fpu()) |
Avi Kivity | 1c11e71 | 2010-05-03 16:05:44 +0300 | [diff] [blame] | 1494 | clts(); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 1495 | load_gdt(&__get_cpu_var(host_gdt)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1496 | } |
| 1497 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1498 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 1499 | { |
| 1500 | preempt_disable(); |
| 1501 | __vmx_load_host_state(vmx); |
| 1502 | preempt_enable(); |
| 1503 | } |
| 1504 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1505 | /* |
| 1506 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 1507 | * vcpu mutex is already taken. |
| 1508 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1509 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1510 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1511 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1512 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1513 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1514 | if (!vmm_exclusive) |
| 1515 | kvm_cpu_vmxon(phys_addr); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1516 | else if (vmx->loaded_vmcs->cpu != cpu) |
| 1517 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1518 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1519 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 1520 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 1521 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1522 | } |
| 1523 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1524 | if (vmx->loaded_vmcs->cpu != cpu) { |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1525 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1526 | unsigned long sysenter_esp; |
| 1527 | |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 1528 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1529 | local_irq_disable(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1530 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 1531 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1532 | local_irq_enable(); |
| 1533 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1534 | /* |
| 1535 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 1536 | * processors. |
| 1537 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1538 | vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1539 | vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1540 | |
| 1541 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 1542 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1543 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1544 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 1548 | { |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1549 | __vmx_load_host_state(to_vmx(vcpu)); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1550 | if (!vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1551 | __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs); |
| 1552 | vcpu->cpu = -1; |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1553 | kvm_cpu_vmxoff(); |
| 1554 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1555 | } |
| 1556 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1557 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 1558 | { |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1559 | ulong cr0; |
| 1560 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1561 | if (vcpu->fpu_active) |
| 1562 | return; |
| 1563 | vcpu->fpu_active = 1; |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1564 | cr0 = vmcs_readl(GUEST_CR0); |
| 1565 | cr0 &= ~(X86_CR0_TS | X86_CR0_MP); |
| 1566 | cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP); |
| 1567 | vmcs_writel(GUEST_CR0, cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1568 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1569 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1570 | if (is_guest_mode(vcpu)) |
| 1571 | vcpu->arch.cr0_guest_owned_bits &= |
| 1572 | ~get_vmcs12(vcpu)->cr0_guest_host_mask; |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1573 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1574 | } |
| 1575 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1576 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 1577 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1578 | /* |
| 1579 | * Return the cr0 value that a nested guest would read. This is a combination |
| 1580 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 1581 | * its hypervisor (cr0_read_shadow). |
| 1582 | */ |
| 1583 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 1584 | { |
| 1585 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 1586 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 1587 | } |
| 1588 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 1589 | { |
| 1590 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 1591 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 1592 | } |
| 1593 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1594 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 1595 | { |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1596 | /* Note that there is no vcpu->fpu_active = 0 here. The caller must |
| 1597 | * set this *before* calling this function. |
| 1598 | */ |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1599 | vmx_decache_cr0_guest_bits(vcpu); |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1600 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1601 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1602 | vcpu->arch.cr0_guest_owned_bits = 0; |
| 1603 | 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] | 1604 | if (is_guest_mode(vcpu)) { |
| 1605 | /* |
| 1606 | * L1's specified read shadow might not contain the TS bit, |
| 1607 | * so now that we turned on shadowing of this bit, we need to |
| 1608 | * set this bit of the shadow. Like in nested_vmx_run we need |
| 1609 | * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet |
| 1610 | * up-to-date here because we just decached cr0.TS (and we'll |
| 1611 | * only update vmcs12->guest_cr0 on nested exit). |
| 1612 | */ |
| 1613 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1614 | vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) | |
| 1615 | (vcpu->arch.cr0 & X86_CR0_TS); |
| 1616 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 1617 | } else |
| 1618 | vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1619 | } |
| 1620 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1621 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 1622 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1623 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 1624 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1625 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 1626 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 1627 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 1628 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1629 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1630 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 1631 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1632 | } |
| 1633 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1634 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1635 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 1639 | { |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1640 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 1641 | __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1642 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1643 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1644 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1645 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1646 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1647 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 1648 | } |
| 1649 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1650 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 1651 | { |
| 1652 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 1653 | int ret = 0; |
| 1654 | |
| 1655 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1656 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1657 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1658 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1659 | |
| 1660 | return ret & mask; |
| 1661 | } |
| 1662 | |
| 1663 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 1664 | { |
| 1665 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 1666 | u32 interruptibility = interruptibility_old; |
| 1667 | |
| 1668 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 1669 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1670 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1671 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1672 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1673 | interruptibility |= GUEST_INTR_STATE_STI; |
| 1674 | |
| 1675 | if ((interruptibility != interruptibility_old)) |
| 1676 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 1677 | } |
| 1678 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1679 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 1680 | { |
| 1681 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1682 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1683 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1684 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1685 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1686 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1687 | /* skipping an emulated instruction also counts */ |
| 1688 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1689 | } |
| 1690 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 1691 | /* |
| 1692 | * KVM wants to inject page-faults which it got to the guest. This function |
| 1693 | * checks whether in a nested guest, we need to inject them to L1 or L2. |
| 1694 | * This function assumes it is called with the exit reason in vmcs02 being |
| 1695 | * a #PF exception (this is the only case in which KVM injects a #PF when L2 |
| 1696 | * is running). |
| 1697 | */ |
| 1698 | static int nested_pf_handled(struct kvm_vcpu *vcpu) |
| 1699 | { |
| 1700 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1701 | |
| 1702 | /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */ |
Nadav Har'El | 9587190 | 2012-03-06 16:39:22 +0200 | [diff] [blame] | 1703 | if (!(vmcs12->exception_bitmap & (1u << PF_VECTOR))) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 1704 | return 0; |
| 1705 | |
| 1706 | nested_vmx_vmexit(vcpu); |
| 1707 | return 1; |
| 1708 | } |
| 1709 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1710 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 1711 | bool has_error_code, u32 error_code, |
| 1712 | bool reinject) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1713 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1714 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1715 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1716 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 1717 | if (nr == PF_VECTOR && is_guest_mode(vcpu) && |
| 1718 | nested_pf_handled(vcpu)) |
| 1719 | return; |
| 1720 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1721 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1722 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1723 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 1724 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1725 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1726 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 1727 | int inc_eip = 0; |
| 1728 | if (kvm_exception_is_soft(nr)) |
| 1729 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 1730 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 1731 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1732 | return; |
| 1733 | } |
| 1734 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 1735 | if (kvm_exception_is_soft(nr)) { |
| 1736 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 1737 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1738 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 1739 | } else |
| 1740 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 1741 | |
| 1742 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1743 | } |
| 1744 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1745 | static bool vmx_rdtscp_supported(void) |
| 1746 | { |
| 1747 | return cpu_has_vmx_rdtscp(); |
| 1748 | } |
| 1749 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1750 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1751 | * Swap MSR entry in host/guest MSR entry array. |
| 1752 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1753 | 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] | 1754 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1755 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1756 | |
| 1757 | tmp = vmx->guest_msrs[to]; |
| 1758 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 1759 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1763 | * Set up the vmcs to automatically save and restore system |
| 1764 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 1765 | * mode, as fiddling with msrs is very expensive. |
| 1766 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1767 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1768 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1769 | int save_nmsrs, index; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 1770 | unsigned long *msr_bitmap; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1771 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1772 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1773 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1774 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1775 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1776 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1777 | move_msr_up(vmx, index, save_nmsrs++); |
| 1778 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1779 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1780 | move_msr_up(vmx, index, save_nmsrs++); |
| 1781 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1782 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1783 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1784 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
| 1785 | if (index >= 0 && vmx->rdtscp_enabled) |
| 1786 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1787 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1788 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1789 | * if efer.sce is enabled. |
| 1790 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1791 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1792 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1793 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1794 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1795 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1796 | index = __find_msr_index(vmx, MSR_EFER); |
| 1797 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1798 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1799 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1800 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 1801 | |
| 1802 | if (cpu_has_vmx_msr_bitmap()) { |
| 1803 | if (is_long_mode(&vmx->vcpu)) |
| 1804 | msr_bitmap = vmx_msr_bitmap_longmode; |
| 1805 | else |
| 1806 | msr_bitmap = vmx_msr_bitmap_legacy; |
| 1807 | |
| 1808 | vmcs_write64(MSR_BITMAP, __pa(msr_bitmap)); |
| 1809 | } |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1813 | * reads and returns guest's timestamp counter "register" |
| 1814 | * guest_tsc = host_tsc + tsc_offset -- 21.3 |
| 1815 | */ |
| 1816 | static u64 guest_read_tsc(void) |
| 1817 | { |
| 1818 | u64 host_tsc, tsc_offset; |
| 1819 | |
| 1820 | rdtscll(host_tsc); |
| 1821 | tsc_offset = vmcs_read64(TSC_OFFSET); |
| 1822 | return host_tsc + tsc_offset; |
| 1823 | } |
| 1824 | |
| 1825 | /* |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 1826 | * Like guest_read_tsc, but always returns L1's notion of the timestamp |
| 1827 | * counter, even if a nested guest (L2) is currently running. |
| 1828 | */ |
| 1829 | u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu) |
| 1830 | { |
| 1831 | u64 host_tsc, tsc_offset; |
| 1832 | |
| 1833 | rdtscll(host_tsc); |
| 1834 | tsc_offset = is_guest_mode(vcpu) ? |
| 1835 | to_vmx(vcpu)->nested.vmcs01_tsc_offset : |
| 1836 | vmcs_read64(TSC_OFFSET); |
| 1837 | return host_tsc + tsc_offset; |
| 1838 | } |
| 1839 | |
| 1840 | /* |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 1841 | * Engage any workarounds for mis-matched TSC rates. Currently limited to |
| 1842 | * software catchup for faster rates on slower CPUs. |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 1843 | */ |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 1844 | 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] | 1845 | { |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 1846 | if (!scale) |
| 1847 | return; |
| 1848 | |
| 1849 | if (user_tsc_khz > tsc_khz) { |
| 1850 | vcpu->arch.tsc_catchup = 1; |
| 1851 | vcpu->arch.tsc_always_catchup = 1; |
| 1852 | } else |
| 1853 | WARN(1, "user requested TSC rate below hardware speed\n"); |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1857 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1858 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1859 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1860 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 1861 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 1862 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 1863 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 1864 | * to the spec, this should set L1's TSC; The offset that L1 |
| 1865 | * set for L2 remains unchanged, and still needs to be added |
| 1866 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 1867 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 1868 | struct vmcs12 *vmcs12; |
| 1869 | to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset; |
| 1870 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 1871 | vmcs12 = get_vmcs12(vcpu); |
| 1872 | vmcs_write64(TSC_OFFSET, offset + |
| 1873 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 1874 | vmcs12->tsc_offset : 0)); |
| 1875 | } else { |
| 1876 | vmcs_write64(TSC_OFFSET, offset); |
| 1877 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1878 | } |
| 1879 | |
Marcelo Tosatti | f1e2b26 | 2012-02-03 15:43:55 -0200 | [diff] [blame] | 1880 | 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] | 1881 | { |
| 1882 | u64 offset = vmcs_read64(TSC_OFFSET); |
| 1883 | vmcs_write64(TSC_OFFSET, offset + adjustment); |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 1884 | if (is_guest_mode(vcpu)) { |
| 1885 | /* Even when running L2, the adjustment needs to apply to L1 */ |
| 1886 | to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment; |
| 1887 | } |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 1888 | } |
| 1889 | |
Joerg Roedel | 857e409 | 2011-03-25 09:44:50 +0100 | [diff] [blame] | 1890 | static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) |
| 1891 | { |
| 1892 | return target_tsc - native_read_tsc(); |
| 1893 | } |
| 1894 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 1895 | static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu) |
| 1896 | { |
| 1897 | struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 1898 | return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31))); |
| 1899 | } |
| 1900 | |
| 1901 | /* |
| 1902 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 1903 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 1904 | * all guests if the "nested" module option is off, and can also be disabled |
| 1905 | * for a single guest by disabling its VMX cpuid bit. |
| 1906 | */ |
| 1907 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 1908 | { |
| 1909 | return nested && guest_cpuid_has_vmx(vcpu); |
| 1910 | } |
| 1911 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1912 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 1913 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 1914 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 1915 | * The same values should also be used to verify that vmcs12 control fields are |
| 1916 | * valid during nested entry from L1 to L2. |
| 1917 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 1918 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 1919 | * bit in the high half is on if the corresponding bit in the control field |
| 1920 | * may be on. See also vmx_control_verify(). |
| 1921 | * TODO: allow these variables to be modified (downgraded) by module options |
| 1922 | * or other means. |
| 1923 | */ |
| 1924 | static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high; |
| 1925 | static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high; |
| 1926 | static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high; |
| 1927 | static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high; |
| 1928 | static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high; |
| 1929 | static __init void nested_vmx_setup_ctls_msrs(void) |
| 1930 | { |
| 1931 | /* |
| 1932 | * Note that as a general rule, the high half of the MSRs (bits in |
| 1933 | * the control fields which may be 1) should be initialized by the |
| 1934 | * intersection of the underlying hardware's MSR (i.e., features which |
| 1935 | * can be supported) and the list of features we want to expose - |
| 1936 | * because they are known to be properly supported in our code. |
| 1937 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 1938 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 1939 | * reason is that if one of these bits is necessary, it will appear |
| 1940 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 1941 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
| 1942 | * nested_vmx_exit_handled() will not pass related exits to L1. |
| 1943 | * These rules have exceptions below. |
| 1944 | */ |
| 1945 | |
| 1946 | /* pin-based controls */ |
| 1947 | /* |
| 1948 | * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is |
| 1949 | * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR. |
| 1950 | */ |
| 1951 | nested_vmx_pinbased_ctls_low = 0x16 ; |
| 1952 | nested_vmx_pinbased_ctls_high = 0x16 | |
| 1953 | PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING | |
| 1954 | PIN_BASED_VIRTUAL_NMIS; |
| 1955 | |
| 1956 | /* exit controls */ |
| 1957 | nested_vmx_exit_ctls_low = 0; |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 1958 | /* 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] | 1959 | #ifdef CONFIG_X86_64 |
| 1960 | nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 1961 | #else |
| 1962 | nested_vmx_exit_ctls_high = 0; |
| 1963 | #endif |
| 1964 | |
| 1965 | /* entry controls */ |
| 1966 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
| 1967 | nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high); |
| 1968 | nested_vmx_entry_ctls_low = 0; |
| 1969 | nested_vmx_entry_ctls_high &= |
| 1970 | VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE; |
| 1971 | |
| 1972 | /* cpu-based controls */ |
| 1973 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
| 1974 | nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high); |
| 1975 | nested_vmx_procbased_ctls_low = 0; |
| 1976 | nested_vmx_procbased_ctls_high &= |
| 1977 | CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
| 1978 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 1979 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 1980 | CPU_BASED_CR3_STORE_EXITING | |
| 1981 | #ifdef CONFIG_X86_64 |
| 1982 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 1983 | #endif |
| 1984 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
| 1985 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 1986 | CPU_BASED_RDPMC_EXITING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 1987 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
| 1988 | /* |
| 1989 | * We can allow some features even when not supported by the |
| 1990 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 1991 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 1992 | * without MSR bitmaps. |
| 1993 | */ |
| 1994 | nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS; |
| 1995 | |
| 1996 | /* secondary cpu-based controls */ |
| 1997 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
| 1998 | nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high); |
| 1999 | nested_vmx_secondary_ctls_low = 0; |
| 2000 | nested_vmx_secondary_ctls_high &= |
| 2001 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 2002 | } |
| 2003 | |
| 2004 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 2005 | { |
| 2006 | /* |
| 2007 | * Bits 0 in high must be 0, and bits 1 in low must be 1. |
| 2008 | */ |
| 2009 | return ((control & high) | low) == control; |
| 2010 | } |
| 2011 | |
| 2012 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 2013 | { |
| 2014 | return low | ((u64)high << 32); |
| 2015 | } |
| 2016 | |
| 2017 | /* |
| 2018 | * If we allow our guest to use VMX instructions (i.e., nested VMX), we should |
| 2019 | * also let it use VMX-specific MSRs. |
| 2020 | * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a |
| 2021 | * VMX-specific MSR, or 0 when we haven't (and the caller should handle it |
| 2022 | * like all other MSRs). |
| 2023 | */ |
| 2024 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 2025 | { |
| 2026 | if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC && |
| 2027 | msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) { |
| 2028 | /* |
| 2029 | * According to the spec, processors which do not support VMX |
| 2030 | * should throw a #GP(0) when VMX capability MSRs are read. |
| 2031 | */ |
| 2032 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 2033 | return 1; |
| 2034 | } |
| 2035 | |
| 2036 | switch (msr_index) { |
| 2037 | case MSR_IA32_FEATURE_CONTROL: |
| 2038 | *pdata = 0; |
| 2039 | break; |
| 2040 | case MSR_IA32_VMX_BASIC: |
| 2041 | /* |
| 2042 | * This MSR reports some information about VMX support. We |
| 2043 | * should return information about the VMX we emulate for the |
| 2044 | * guest, and the VMCS structure we give it - not about the |
| 2045 | * VMX support of the underlying hardware. |
| 2046 | */ |
| 2047 | *pdata = VMCS12_REVISION | |
| 2048 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 2049 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 2050 | break; |
| 2051 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 2052 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 2053 | *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low, |
| 2054 | nested_vmx_pinbased_ctls_high); |
| 2055 | break; |
| 2056 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 2057 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 2058 | *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low, |
| 2059 | nested_vmx_procbased_ctls_high); |
| 2060 | break; |
| 2061 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 2062 | case MSR_IA32_VMX_EXIT_CTLS: |
| 2063 | *pdata = vmx_control_msr(nested_vmx_exit_ctls_low, |
| 2064 | nested_vmx_exit_ctls_high); |
| 2065 | break; |
| 2066 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 2067 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 2068 | *pdata = vmx_control_msr(nested_vmx_entry_ctls_low, |
| 2069 | nested_vmx_entry_ctls_high); |
| 2070 | break; |
| 2071 | case MSR_IA32_VMX_MISC: |
| 2072 | *pdata = 0; |
| 2073 | break; |
| 2074 | /* |
| 2075 | * These MSRs specify bits which the guest must keep fixed (on or off) |
| 2076 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 2077 | * We picked the standard core2 setting. |
| 2078 | */ |
| 2079 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 2080 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 2081 | case MSR_IA32_VMX_CR0_FIXED0: |
| 2082 | *pdata = VMXON_CR0_ALWAYSON; |
| 2083 | break; |
| 2084 | case MSR_IA32_VMX_CR0_FIXED1: |
| 2085 | *pdata = -1ULL; |
| 2086 | break; |
| 2087 | case MSR_IA32_VMX_CR4_FIXED0: |
| 2088 | *pdata = VMXON_CR4_ALWAYSON; |
| 2089 | break; |
| 2090 | case MSR_IA32_VMX_CR4_FIXED1: |
| 2091 | *pdata = -1ULL; |
| 2092 | break; |
| 2093 | case MSR_IA32_VMX_VMCS_ENUM: |
| 2094 | *pdata = 0x1f; |
| 2095 | break; |
| 2096 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 2097 | *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low, |
| 2098 | nested_vmx_secondary_ctls_high); |
| 2099 | break; |
| 2100 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 2101 | /* Currently, no nested ept or nested vpid */ |
| 2102 | *pdata = 0; |
| 2103 | break; |
| 2104 | default: |
| 2105 | return 0; |
| 2106 | } |
| 2107 | |
| 2108 | return 1; |
| 2109 | } |
| 2110 | |
| 2111 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 2112 | { |
| 2113 | if (!nested_vmx_allowed(vcpu)) |
| 2114 | return 0; |
| 2115 | |
| 2116 | if (msr_index == MSR_IA32_FEATURE_CONTROL) |
| 2117 | /* TODO: the right thing. */ |
| 2118 | return 1; |
| 2119 | /* |
| 2120 | * No need to treat VMX capability MSRs specially: If we don't handle |
| 2121 | * them, handle_wrmsr will #GP(0), which is correct (they are readonly) |
| 2122 | */ |
| 2123 | return 0; |
| 2124 | } |
| 2125 | |
| 2126 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2127 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 2128 | * Returns 0 on success, non-0 otherwise. |
| 2129 | * Assumes vcpu_load() was already called. |
| 2130 | */ |
| 2131 | static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 2132 | { |
| 2133 | u64 data; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2134 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2135 | |
| 2136 | if (!pdata) { |
| 2137 | printk(KERN_ERR "BUG: get_msr called with NULL pdata\n"); |
| 2138 | return -EINVAL; |
| 2139 | } |
| 2140 | |
| 2141 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2142 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2143 | case MSR_FS_BASE: |
| 2144 | data = vmcs_readl(GUEST_FS_BASE); |
| 2145 | break; |
| 2146 | case MSR_GS_BASE: |
| 2147 | data = vmcs_readl(GUEST_GS_BASE); |
| 2148 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2149 | case MSR_KERNEL_GS_BASE: |
| 2150 | vmx_load_host_state(to_vmx(vcpu)); |
| 2151 | data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
| 2152 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2153 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2154 | case MSR_EFER: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2155 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2156 | case MSR_IA32_TSC: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2157 | data = guest_read_tsc(); |
| 2158 | break; |
| 2159 | case MSR_IA32_SYSENTER_CS: |
| 2160 | data = vmcs_read32(GUEST_SYSENTER_CS); |
| 2161 | break; |
| 2162 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2163 | data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2164 | break; |
| 2165 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2166 | data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2167 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2168 | case MSR_TSC_AUX: |
| 2169 | if (!to_vmx(vcpu)->rdtscp_enabled) |
| 2170 | return 1; |
| 2171 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2172 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2173 | if (vmx_get_vmx_msr(vcpu, msr_index, pdata)) |
| 2174 | return 0; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2175 | msr = find_msr_entry(to_vmx(vcpu), msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2176 | if (msr) { |
| 2177 | data = msr->data; |
| 2178 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2179 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2180 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | *pdata = data; |
| 2184 | return 0; |
| 2185 | } |
| 2186 | |
| 2187 | /* |
| 2188 | * Writes msr value into into the appropriate "register". |
| 2189 | * Returns 0 on success, non-0 otherwise. |
| 2190 | * Assumes vcpu_load() was already called. |
| 2191 | */ |
| 2192 | static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 2193 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2194 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2195 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2196 | int ret = 0; |
| 2197 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2198 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2199 | case MSR_EFER: |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2200 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2201 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 2202 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2203 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2204 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2205 | vmcs_writel(GUEST_FS_BASE, data); |
| 2206 | break; |
| 2207 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2208 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2209 | vmcs_writel(GUEST_GS_BASE, data); |
| 2210 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2211 | case MSR_KERNEL_GS_BASE: |
| 2212 | vmx_load_host_state(vmx); |
| 2213 | vmx->msr_guest_kernel_gs_base = data; |
| 2214 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2215 | #endif |
| 2216 | case MSR_IA32_SYSENTER_CS: |
| 2217 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 2218 | break; |
| 2219 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2220 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2221 | break; |
| 2222 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2223 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2224 | break; |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2225 | case MSR_IA32_TSC: |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2226 | kvm_write_tsc(vcpu, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2227 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2228 | case MSR_IA32_CR_PAT: |
| 2229 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 2230 | vmcs_write64(GUEST_IA32_PAT, data); |
| 2231 | vcpu->arch.pat = data; |
| 2232 | break; |
| 2233 | } |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2234 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
| 2235 | break; |
| 2236 | case MSR_TSC_AUX: |
| 2237 | if (!vmx->rdtscp_enabled) |
| 2238 | return 1; |
| 2239 | /* Check reserved bit, higher 32 bits should be zero */ |
| 2240 | if ((data >> 32) != 0) |
| 2241 | return 1; |
| 2242 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2243 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2244 | if (vmx_set_vmx_msr(vcpu, msr_index, data)) |
| 2245 | break; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2246 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2247 | if (msr) { |
| 2248 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 2249 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 2250 | preempt_disable(); |
Avi Kivity | 9ee7397 | 2012-03-06 14:16:33 +0200 | [diff] [blame] | 2251 | kvm_set_shared_msr(msr->index, msr->data, |
| 2252 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 2253 | preempt_enable(); |
| 2254 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2255 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2256 | } |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2257 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2258 | } |
| 2259 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2260 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2261 | } |
| 2262 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2263 | 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] | 2264 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2265 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 2266 | switch (reg) { |
| 2267 | case VCPU_REGS_RSP: |
| 2268 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 2269 | break; |
| 2270 | case VCPU_REGS_RIP: |
| 2271 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 2272 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2273 | case VCPU_EXREG_PDPTR: |
| 2274 | if (enable_ept) |
| 2275 | ept_save_pdptrs(vcpu); |
| 2276 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2277 | default: |
| 2278 | break; |
| 2279 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2280 | } |
| 2281 | |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 2282 | static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2283 | { |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2284 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 2285 | vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]); |
| 2286 | else |
| 2287 | vmcs_writel(GUEST_DR7, vcpu->arch.dr7); |
| 2288 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2289 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | static __init int cpu_has_kvm_support(void) |
| 2293 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 2294 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | static __init int vmx_disabled_by_bios(void) |
| 2298 | { |
| 2299 | u64 msr; |
| 2300 | |
| 2301 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2302 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2303 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2304 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 2305 | && tboot_enabled()) |
| 2306 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2307 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2308 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2309 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 2310 | && !tboot_enabled()) { |
| 2311 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2312 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2313 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 2314 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2315 | /* launched w/o TXT and VMX disabled */ |
| 2316 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 2317 | && !tboot_enabled()) |
| 2318 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2322 | } |
| 2323 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2324 | static void kvm_cpu_vmxon(u64 addr) |
| 2325 | { |
| 2326 | asm volatile (ASM_VMX_VMXON_RAX |
| 2327 | : : "a"(&addr), "m"(addr) |
| 2328 | : "memory", "cc"); |
| 2329 | } |
| 2330 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2331 | static int hardware_enable(void *garbage) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2332 | { |
| 2333 | int cpu = raw_smp_processor_id(); |
| 2334 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2335 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2336 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2337 | if (read_cr4() & X86_CR4_VMXE) |
| 2338 | return -EBUSY; |
| 2339 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2340 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2341 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2342 | |
| 2343 | test_bits = FEATURE_CONTROL_LOCKED; |
| 2344 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 2345 | if (tboot_enabled()) |
| 2346 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 2347 | |
| 2348 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2349 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2350 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 2351 | } |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2352 | write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */ |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2353 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2354 | if (vmm_exclusive) { |
| 2355 | kvm_cpu_vmxon(phys_addr); |
| 2356 | ept_sync_global(); |
| 2357 | } |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2358 | |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 2359 | store_gdt(&__get_cpu_var(host_gdt)); |
| 2360 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2361 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2362 | } |
| 2363 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2364 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2365 | { |
| 2366 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2367 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2368 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2369 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 2370 | loaded_vmcss_on_cpu_link) |
| 2371 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2372 | } |
| 2373 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 2374 | |
| 2375 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 2376 | * tricks. |
| 2377 | */ |
| 2378 | static void kvm_cpu_vmxoff(void) |
| 2379 | { |
| 2380 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 2381 | } |
| 2382 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2383 | static void hardware_disable(void *garbage) |
| 2384 | { |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2385 | if (vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2386 | vmclear_local_loaded_vmcss(); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2387 | kvm_cpu_vmxoff(); |
| 2388 | } |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2389 | write_cr4(read_cr4() & ~X86_CR4_VMXE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2390 | } |
| 2391 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2392 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2393 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2394 | { |
| 2395 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2396 | u32 ctl = ctl_min | ctl_opt; |
| 2397 | |
| 2398 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 2399 | |
| 2400 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 2401 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 2402 | |
| 2403 | /* Ensure minimum (required) set of control bits are supported. */ |
| 2404 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2405 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2406 | |
| 2407 | *result = ctl; |
| 2408 | return 0; |
| 2409 | } |
| 2410 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2411 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 2412 | { |
| 2413 | u32 vmx_msr_low, vmx_msr_high; |
| 2414 | |
| 2415 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 2416 | return vmx_msr_high & ctl; |
| 2417 | } |
| 2418 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2419 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2420 | { |
| 2421 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2422 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2423 | u32 _pin_based_exec_control = 0; |
| 2424 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2425 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2426 | u32 _vmexit_control = 0; |
| 2427 | u32 _vmentry_control = 0; |
| 2428 | |
| 2429 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2430 | opt = PIN_BASED_VIRTUAL_NMIS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2431 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 2432 | &_pin_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2433 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2434 | |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 2435 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2436 | #ifdef CONFIG_X86_64 |
| 2437 | CPU_BASED_CR8_LOAD_EXITING | |
| 2438 | CPU_BASED_CR8_STORE_EXITING | |
| 2439 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2440 | CPU_BASED_CR3_LOAD_EXITING | |
| 2441 | CPU_BASED_CR3_STORE_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2442 | CPU_BASED_USE_IO_BITMAPS | |
| 2443 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2444 | CPU_BASED_USE_TSC_OFFSETING | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 2445 | CPU_BASED_MWAIT_EXITING | |
| 2446 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 2447 | CPU_BASED_INVLPG_EXITING | |
| 2448 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 2449 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2450 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2451 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2452 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2453 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 2454 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2455 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2456 | #ifdef CONFIG_X86_64 |
| 2457 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 2458 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 2459 | ~CPU_BASED_CR8_STORE_EXITING; |
| 2460 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2461 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2462 | min2 = 0; |
| 2463 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2464 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2465 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2466 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2467 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2468 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
| 2469 | SECONDARY_EXEC_RDTSCP; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2470 | if (adjust_vmx_controls(min2, opt2, |
| 2471 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2472 | &_cpu_based_2nd_exec_control) < 0) |
| 2473 | return -EIO; |
| 2474 | } |
| 2475 | #ifndef CONFIG_X86_64 |
| 2476 | if (!(_cpu_based_2nd_exec_control & |
| 2477 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 2478 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2479 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2480 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2481 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 2482 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 2483 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 2484 | CPU_BASED_CR3_STORE_EXITING | |
| 2485 | CPU_BASED_INVLPG_EXITING); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2486 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
| 2487 | vmx_capability.ept, vmx_capability.vpid); |
| 2488 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2489 | |
| 2490 | min = 0; |
| 2491 | #ifdef CONFIG_X86_64 |
| 2492 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 2493 | #endif |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2494 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2495 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 2496 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2497 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2498 | |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2499 | min = 0; |
| 2500 | opt = VM_ENTRY_LOAD_IA32_PAT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2501 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 2502 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2503 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2504 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 2505 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2506 | |
| 2507 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 2508 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2509 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2510 | |
| 2511 | #ifdef CONFIG_X86_64 |
| 2512 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 2513 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2514 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2515 | #endif |
| 2516 | |
| 2517 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 2518 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2519 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2520 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2521 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
| 2522 | vmcs_conf->order = get_order(vmcs_config.size); |
| 2523 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2524 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2525 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 2526 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2527 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2528 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 2529 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2530 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2531 | cpu_has_load_ia32_efer = |
| 2532 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 2533 | VM_ENTRY_LOAD_IA32_EFER) |
| 2534 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 2535 | VM_EXIT_LOAD_IA32_EFER); |
| 2536 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2537 | cpu_has_load_perf_global_ctrl = |
| 2538 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 2539 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 2540 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 2541 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 2542 | |
| 2543 | /* |
| 2544 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
| 2545 | * but due to arrata below it can't be used. Workaround is to use |
| 2546 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 2547 | * |
| 2548 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 2549 | * |
| 2550 | * AAK155 (model 26) |
| 2551 | * AAP115 (model 30) |
| 2552 | * AAT100 (model 37) |
| 2553 | * BC86,AAY89,BD102 (model 44) |
| 2554 | * BA97 (model 46) |
| 2555 | * |
| 2556 | */ |
| 2557 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 2558 | switch (boot_cpu_data.x86_model) { |
| 2559 | case 26: |
| 2560 | case 30: |
| 2561 | case 37: |
| 2562 | case 44: |
| 2563 | case 46: |
| 2564 | cpu_has_load_perf_global_ctrl = false; |
| 2565 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 2566 | "does not work properly. Using workaround\n"); |
| 2567 | break; |
| 2568 | default: |
| 2569 | break; |
| 2570 | } |
| 2571 | } |
| 2572 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2573 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 2574 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2575 | |
| 2576 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 2577 | { |
| 2578 | int node = cpu_to_node(cpu); |
| 2579 | struct page *pages; |
| 2580 | struct vmcs *vmcs; |
| 2581 | |
Mel Gorman | 6484eb3 | 2009-06-16 15:31:54 -0700 | [diff] [blame] | 2582 | pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2583 | if (!pages) |
| 2584 | return NULL; |
| 2585 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2586 | memset(vmcs, 0, vmcs_config.size); |
| 2587 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2588 | return vmcs; |
| 2589 | } |
| 2590 | |
| 2591 | static struct vmcs *alloc_vmcs(void) |
| 2592 | { |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2593 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | static void free_vmcs(struct vmcs *vmcs) |
| 2597 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2598 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2599 | } |
| 2600 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2601 | /* |
| 2602 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 2603 | */ |
| 2604 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 2605 | { |
| 2606 | if (!loaded_vmcs->vmcs) |
| 2607 | return; |
| 2608 | loaded_vmcs_clear(loaded_vmcs); |
| 2609 | free_vmcs(loaded_vmcs->vmcs); |
| 2610 | loaded_vmcs->vmcs = NULL; |
| 2611 | } |
| 2612 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 2613 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2614 | { |
| 2615 | int cpu; |
| 2616 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2617 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2618 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2619 | per_cpu(vmxarea, cpu) = NULL; |
| 2620 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2621 | } |
| 2622 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2623 | static __init int alloc_kvm_area(void) |
| 2624 | { |
| 2625 | int cpu; |
| 2626 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2627 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2628 | struct vmcs *vmcs; |
| 2629 | |
| 2630 | vmcs = alloc_vmcs_cpu(cpu); |
| 2631 | if (!vmcs) { |
| 2632 | free_kvm_area(); |
| 2633 | return -ENOMEM; |
| 2634 | } |
| 2635 | |
| 2636 | per_cpu(vmxarea, cpu) = vmcs; |
| 2637 | } |
| 2638 | return 0; |
| 2639 | } |
| 2640 | |
| 2641 | static __init int hardware_setup(void) |
| 2642 | { |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2643 | if (setup_vmcs_config(&vmcs_config) < 0) |
| 2644 | return -EIO; |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 2645 | |
| 2646 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 2647 | kvm_enable_efer_bits(EFER_NX); |
| 2648 | |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 2649 | if (!cpu_has_vmx_vpid()) |
| 2650 | enable_vpid = 0; |
| 2651 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 2652 | if (!cpu_has_vmx_ept() || |
| 2653 | !cpu_has_vmx_ept_4levels()) { |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 2654 | enable_ept = 0; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2655 | enable_unrestricted_guest = 0; |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame^] | 2656 | enable_ept_ad_bits = 0; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2657 | } |
| 2658 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame^] | 2659 | if (!cpu_has_vmx_ept_ad_bits()) |
| 2660 | enable_ept_ad_bits = 0; |
| 2661 | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2662 | if (!cpu_has_vmx_unrestricted_guest()) |
| 2663 | enable_unrestricted_guest = 0; |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 2664 | |
| 2665 | if (!cpu_has_vmx_flexpriority()) |
| 2666 | flexpriority_enabled = 0; |
| 2667 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2668 | if (!cpu_has_vmx_tpr_shadow()) |
| 2669 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 2670 | |
Marcelo Tosatti | 54dee99 | 2009-06-11 12:07:44 -0300 | [diff] [blame] | 2671 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 2672 | kvm_disable_largepages(); |
| 2673 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2674 | if (!cpu_has_vmx_ple()) |
| 2675 | ple_gap = 0; |
| 2676 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2677 | if (nested) |
| 2678 | nested_vmx_setup_ctls_msrs(); |
| 2679 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2680 | return alloc_kvm_area(); |
| 2681 | } |
| 2682 | |
| 2683 | static __exit void hardware_unsetup(void) |
| 2684 | { |
| 2685 | free_kvm_area(); |
| 2686 | } |
| 2687 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2688 | static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save) |
| 2689 | { |
| 2690 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 2691 | |
Avi Kivity | 6af11b9 | 2007-03-19 13:18:10 +0200 | [diff] [blame] | 2692 | if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2693 | vmcs_write16(sf->selector, save->selector); |
| 2694 | vmcs_writel(sf->base, save->base); |
| 2695 | vmcs_write32(sf->limit, save->limit); |
| 2696 | vmcs_write32(sf->ar_bytes, save->ar); |
| 2697 | } else { |
| 2698 | u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK) |
| 2699 | << AR_DPL_SHIFT; |
| 2700 | vmcs_write32(sf->ar_bytes, 0x93 | dpl); |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 2705 | { |
| 2706 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2707 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2708 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2709 | vmx->emulation_required = 1; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2710 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2711 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2712 | vmx_segment_cache_clear(vmx); |
| 2713 | |
Avi Kivity | d0ba64f | 2011-01-03 14:28:51 +0200 | [diff] [blame] | 2714 | vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector); |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2715 | vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base); |
| 2716 | vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit); |
| 2717 | vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2718 | |
| 2719 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2720 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2721 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2722 | vmcs_writel(GUEST_RFLAGS, flags); |
| 2723 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2724 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 2725 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2726 | |
| 2727 | update_exception_bitmap(vcpu); |
| 2728 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2729 | if (emulate_invalid_guest_state) |
| 2730 | return; |
| 2731 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2732 | fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es); |
| 2733 | fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds); |
| 2734 | fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs); |
| 2735 | fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2736 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2737 | vmx_segment_cache_clear(vmx); |
| 2738 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2739 | vmcs_write16(GUEST_SS_SELECTOR, 0); |
| 2740 | vmcs_write32(GUEST_SS_AR_BYTES, 0x93); |
| 2741 | |
| 2742 | vmcs_write16(GUEST_CS_SELECTOR, |
| 2743 | vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK); |
| 2744 | vmcs_write32(GUEST_CS_AR_BYTES, 0x9b); |
| 2745 | } |
| 2746 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2747 | static gva_t rmode_tss_base(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2748 | { |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2749 | if (!kvm->arch.tss_addr) { |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 2750 | struct kvm_memslots *slots; |
Xiao Guangrong | 28a3754 | 2011-11-24 19:04:35 +0800 | [diff] [blame] | 2751 | struct kvm_memory_slot *slot; |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 2752 | gfn_t base_gfn; |
| 2753 | |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 2754 | slots = kvm_memslots(kvm); |
Xiao Guangrong | 28a3754 | 2011-11-24 19:04:35 +0800 | [diff] [blame] | 2755 | slot = id_to_memslot(slots, 0); |
| 2756 | base_gfn = slot->base_gfn + slot->npages - 3; |
| 2757 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2758 | return base_gfn << PAGE_SHIFT; |
| 2759 | } |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2760 | return kvm->arch.tss_addr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | static void fix_rmode_seg(int seg, struct kvm_save_segment *save) |
| 2764 | { |
| 2765 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 2766 | |
| 2767 | save->selector = vmcs_read16(sf->selector); |
| 2768 | save->base = vmcs_readl(sf->base); |
| 2769 | save->limit = vmcs_read32(sf->limit); |
| 2770 | save->ar = vmcs_read32(sf->ar_bytes); |
Jan Kiszka | 15b00f3 | 2007-11-19 10:21:45 +0100 | [diff] [blame] | 2771 | vmcs_write16(sf->selector, save->base >> 4); |
Gleb Natapov | 444e863 | 2010-12-27 17:25:04 +0200 | [diff] [blame] | 2772 | vmcs_write32(sf->base, save->base & 0xffff0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2773 | vmcs_write32(sf->limit, 0xffff); |
| 2774 | vmcs_write32(sf->ar_bytes, 0xf3); |
Gleb Natapov | 444e863 | 2010-12-27 17:25:04 +0200 | [diff] [blame] | 2775 | if (save->base & 0xf) |
| 2776 | printk_once(KERN_WARNING "kvm: segment base is not paragraph" |
| 2777 | " aligned when entering protected mode (seg=%d)", |
| 2778 | seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 2782 | { |
| 2783 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2784 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2785 | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2786 | if (enable_unrestricted_guest) |
| 2787 | return; |
| 2788 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2789 | vmx->emulation_required = 1; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2790 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2791 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 2792 | /* |
| 2793 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
| 2794 | * vcpu. Call it here with phys address pointing 16M below 4G. |
| 2795 | */ |
| 2796 | if (!vcpu->kvm->arch.tss_addr) { |
| 2797 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 2798 | "called before entering vcpu\n"); |
| 2799 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
| 2800 | vmx_set_tss_addr(vcpu->kvm, 0xfeffd000); |
| 2801 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 2802 | } |
| 2803 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2804 | vmx_segment_cache_clear(vmx); |
| 2805 | |
Avi Kivity | d0ba64f | 2011-01-03 14:28:51 +0200 | [diff] [blame] | 2806 | vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR); |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2807 | vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2808 | vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm)); |
| 2809 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2810 | vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2811 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
| 2812 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2813 | vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2814 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 2815 | |
| 2816 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2817 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2818 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2819 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2820 | |
| 2821 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2822 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2823 | update_exception_bitmap(vcpu); |
| 2824 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2825 | if (emulate_invalid_guest_state) |
| 2826 | goto continue_rmode; |
| 2827 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2828 | vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4); |
| 2829 | vmcs_write32(GUEST_SS_LIMIT, 0xffff); |
| 2830 | vmcs_write32(GUEST_SS_AR_BYTES, 0xf3); |
| 2831 | |
| 2832 | vmcs_write32(GUEST_CS_AR_BYTES, 0xf3); |
Michael Riepe | abacf8d | 2006-12-22 01:05:45 -0800 | [diff] [blame] | 2833 | vmcs_write32(GUEST_CS_LIMIT, 0xffff); |
Avi Kivity | 8cb5b03 | 2007-03-20 18:40:40 +0200 | [diff] [blame] | 2834 | if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000) |
| 2835 | vmcs_writel(GUEST_CS_BASE, 0xf0000); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2836 | vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4); |
| 2837 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2838 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es); |
| 2839 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds); |
| 2840 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs); |
| 2841 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 2842 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2843 | continue_rmode: |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 2844 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2845 | } |
| 2846 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2847 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 2848 | { |
| 2849 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2850 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 2851 | |
| 2852 | if (!msr) |
| 2853 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2854 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2855 | /* |
| 2856 | * Force kernel_gs_base reloading before EFER changes, as control |
| 2857 | * of this msr depends on is_long_mode(). |
| 2858 | */ |
| 2859 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2860 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2861 | if (efer & EFER_LMA) { |
| 2862 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 2863 | vmcs_read32(VM_ENTRY_CONTROLS) | |
| 2864 | VM_ENTRY_IA32E_MODE); |
| 2865 | msr->data = efer; |
| 2866 | } else { |
| 2867 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 2868 | vmcs_read32(VM_ENTRY_CONTROLS) & |
| 2869 | ~VM_ENTRY_IA32E_MODE); |
| 2870 | |
| 2871 | msr->data = efer & ~EFER_LME; |
| 2872 | } |
| 2873 | setup_msrs(vmx); |
| 2874 | } |
| 2875 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2876 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2877 | |
| 2878 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 2879 | { |
| 2880 | u32 guest_tr_ar; |
| 2881 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2882 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 2883 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2884 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
| 2885 | if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 2886 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 2887 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2888 | vmcs_write32(GUEST_TR_AR_BYTES, |
| 2889 | (guest_tr_ar & ~AR_TYPE_MASK) |
| 2890 | | AR_TYPE_BUSY_64_TSS); |
| 2891 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 2892 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 2896 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2897 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 2898 | vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 2899 | & ~VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 2900 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | #endif |
| 2904 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2905 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 2906 | { |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2907 | vpid_sync_context(to_vmx(vcpu)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 2908 | if (enable_ept) { |
| 2909 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 2910 | return; |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 2911 | ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 2912 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2913 | } |
| 2914 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 2915 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 2916 | { |
| 2917 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 2918 | |
| 2919 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 2920 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 2921 | } |
| 2922 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 2923 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 2924 | { |
| 2925 | if (enable_ept && is_paging(vcpu)) |
| 2926 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 2927 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 2928 | } |
| 2929 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2930 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2931 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2932 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 2933 | |
| 2934 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 2935 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2936 | } |
| 2937 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2938 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 2939 | { |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2940 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 2941 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 2942 | return; |
| 2943 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2944 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 2945 | vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]); |
| 2946 | vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]); |
| 2947 | vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]); |
| 2948 | vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2949 | } |
| 2950 | } |
| 2951 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2952 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 2953 | { |
| 2954 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 2955 | vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 2956 | vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 2957 | vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 2958 | vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2959 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2960 | |
| 2961 | __set_bit(VCPU_EXREG_PDPTR, |
| 2962 | (unsigned long *)&vcpu->arch.regs_avail); |
| 2963 | __set_bit(VCPU_EXREG_PDPTR, |
| 2964 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2965 | } |
| 2966 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2967 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2968 | |
| 2969 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 2970 | unsigned long cr0, |
| 2971 | struct kvm_vcpu *vcpu) |
| 2972 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 2973 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 2974 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2975 | if (!(cr0 & X86_CR0_PG)) { |
| 2976 | /* From paging/starting to nonpaging */ |
| 2977 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 2978 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2979 | (CPU_BASED_CR3_LOAD_EXITING | |
| 2980 | CPU_BASED_CR3_STORE_EXITING)); |
| 2981 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2982 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2983 | } else if (!is_paging(vcpu)) { |
| 2984 | /* From nonpaging to paging */ |
| 2985 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 2986 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2987 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 2988 | CPU_BASED_CR3_STORE_EXITING)); |
| 2989 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2990 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2991 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 2992 | |
| 2993 | if (!(cr0 & X86_CR0_WP)) |
| 2994 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2995 | } |
| 2996 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2997 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 2998 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2999 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3000 | unsigned long hw_cr0; |
| 3001 | |
| 3002 | if (enable_unrestricted_guest) |
| 3003 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST) |
| 3004 | | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
| 3005 | else |
| 3006 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3007 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3008 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3009 | enter_pmode(vcpu); |
| 3010 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3011 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3012 | enter_rmode(vcpu); |
| 3013 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3014 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3015 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 3016 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3017 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 3018 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3019 | exit_lmode(vcpu); |
| 3020 | } |
| 3021 | #endif |
| 3022 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3023 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3024 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 3025 | |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3026 | if (!vcpu->fpu_active) |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 3027 | hw_cr0 |= X86_CR0_TS | X86_CR0_MP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3028 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3029 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3030 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3031 | vcpu->arch.cr0 = cr0; |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3032 | __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3033 | } |
| 3034 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3035 | static u64 construct_eptp(unsigned long root_hpa) |
| 3036 | { |
| 3037 | u64 eptp; |
| 3038 | |
| 3039 | /* TODO write the value reading from MSR */ |
| 3040 | eptp = VMX_EPT_DEFAULT_MT | |
| 3041 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; |
| 3042 | eptp |= (root_hpa & PAGE_MASK); |
| 3043 | |
| 3044 | return eptp; |
| 3045 | } |
| 3046 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3047 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 3048 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3049 | unsigned long guest_cr3; |
| 3050 | u64 eptp; |
| 3051 | |
| 3052 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3053 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3054 | eptp = construct_eptp(cr3); |
| 3055 | vmcs_write64(EPT_POINTER, eptp); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 3056 | guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) : |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3057 | vcpu->kvm->arch.ept_identity_map_addr; |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 3058 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3059 | } |
| 3060 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3061 | vmx_flush_tlb(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3062 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3063 | } |
| 3064 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3065 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3066 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3067 | unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ? |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3068 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); |
| 3069 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3070 | if (cr4 & X86_CR4_VMXE) { |
| 3071 | /* |
| 3072 | * To use VMXON (and later other VMX instructions), a guest |
| 3073 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 3074 | * So basically the check on whether to allow nested VMX |
| 3075 | * is here. |
| 3076 | */ |
| 3077 | if (!nested_vmx_allowed(vcpu)) |
| 3078 | return 1; |
| 3079 | } else if (to_vmx(vcpu)->nested.vmxon) |
| 3080 | return 1; |
| 3081 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3082 | vcpu->arch.cr4 = cr4; |
Avi Kivity | bc23008 | 2009-12-08 12:14:42 +0200 | [diff] [blame] | 3083 | if (enable_ept) { |
| 3084 | if (!is_paging(vcpu)) { |
| 3085 | hw_cr4 &= ~X86_CR4_PAE; |
| 3086 | hw_cr4 |= X86_CR4_PSE; |
| 3087 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 3088 | hw_cr4 &= ~X86_CR4_PAE; |
| 3089 | } |
| 3090 | } |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3091 | |
| 3092 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 3093 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3094 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3095 | } |
| 3096 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3097 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 3098 | struct kvm_segment *var, int seg) |
| 3099 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3100 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3101 | struct kvm_save_segment *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3102 | u32 ar; |
| 3103 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3104 | if (vmx->rmode.vm86_active |
| 3105 | && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES |
| 3106 | || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS |
| 3107 | || seg == VCPU_SREG_GS) |
| 3108 | && !emulate_invalid_guest_state) { |
| 3109 | switch (seg) { |
| 3110 | case VCPU_SREG_TR: save = &vmx->rmode.tr; break; |
| 3111 | case VCPU_SREG_ES: save = &vmx->rmode.es; break; |
| 3112 | case VCPU_SREG_DS: save = &vmx->rmode.ds; break; |
| 3113 | case VCPU_SREG_FS: save = &vmx->rmode.fs; break; |
| 3114 | case VCPU_SREG_GS: save = &vmx->rmode.gs; break; |
| 3115 | default: BUG(); |
| 3116 | } |
| 3117 | var->selector = save->selector; |
| 3118 | var->base = save->base; |
| 3119 | var->limit = save->limit; |
| 3120 | ar = save->ar; |
| 3121 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3122 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3123 | goto use_saved_rmode_seg; |
| 3124 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3125 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 3126 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 3127 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 3128 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3129 | use_saved_rmode_seg: |
Avi Kivity | 9fd4a3b | 2009-01-04 23:43:42 +0200 | [diff] [blame] | 3130 | if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3131 | ar = 0; |
| 3132 | var->type = ar & 15; |
| 3133 | var->s = (ar >> 4) & 1; |
| 3134 | var->dpl = (ar >> 5) & 3; |
| 3135 | var->present = (ar >> 7) & 1; |
| 3136 | var->avl = (ar >> 12) & 1; |
| 3137 | var->l = (ar >> 13) & 1; |
| 3138 | var->db = (ar >> 14) & 1; |
| 3139 | var->g = (ar >> 15) & 1; |
| 3140 | var->unusable = (ar >> 16) & 1; |
| 3141 | } |
| 3142 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3143 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 3144 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3145 | struct kvm_segment s; |
| 3146 | |
| 3147 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 3148 | vmx_get_segment(vcpu, &s, seg); |
| 3149 | return s.base; |
| 3150 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3151 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3152 | } |
| 3153 | |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3154 | static int __vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3155 | { |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 3156 | if (!is_protmode(vcpu)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3157 | return 0; |
| 3158 | |
Avi Kivity | f4c63e5 | 2011-03-07 14:54:28 +0200 | [diff] [blame] | 3159 | if (!is_long_mode(vcpu) |
| 3160 | && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */ |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3161 | return 3; |
| 3162 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3163 | return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3; |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3164 | } |
| 3165 | |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3166 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
| 3167 | { |
| 3168 | if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) { |
| 3169 | __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
| 3170 | to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu); |
| 3171 | } |
| 3172 | return to_vmx(vcpu)->cpl; |
| 3173 | } |
| 3174 | |
| 3175 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3176 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3177 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3178 | u32 ar; |
| 3179 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3180 | if (var->unusable) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3181 | ar = 1 << 16; |
| 3182 | else { |
| 3183 | ar = var->type & 15; |
| 3184 | ar |= (var->s & 1) << 4; |
| 3185 | ar |= (var->dpl & 3) << 5; |
| 3186 | ar |= (var->present & 1) << 7; |
| 3187 | ar |= (var->avl & 1) << 12; |
| 3188 | ar |= (var->l & 1) << 13; |
| 3189 | ar |= (var->db & 1) << 14; |
| 3190 | ar |= (var->g & 1) << 15; |
| 3191 | } |
Uri Lublin | f7fbf1f | 2006-12-13 00:34:00 -0800 | [diff] [blame] | 3192 | if (ar == 0) /* a 0 value means unusable */ |
| 3193 | ar = AR_UNUSABLE_MASK; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3194 | |
| 3195 | return ar; |
| 3196 | } |
| 3197 | |
| 3198 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 3199 | struct kvm_segment *var, int seg) |
| 3200 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3201 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3202 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 3203 | u32 ar; |
| 3204 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3205 | vmx_segment_cache_clear(vmx); |
| 3206 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3207 | if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) { |
Gleb Natapov | a8ba6c2 | 2011-02-21 12:07:58 +0200 | [diff] [blame] | 3208 | vmcs_write16(sf->selector, var->selector); |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3209 | vmx->rmode.tr.selector = var->selector; |
| 3210 | vmx->rmode.tr.base = var->base; |
| 3211 | vmx->rmode.tr.limit = var->limit; |
| 3212 | vmx->rmode.tr.ar = vmx_segment_access_rights(var); |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3213 | return; |
| 3214 | } |
| 3215 | vmcs_writel(sf->base, var->base); |
| 3216 | vmcs_write32(sf->limit, var->limit); |
| 3217 | vmcs_write16(sf->selector, var->selector); |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3218 | if (vmx->rmode.vm86_active && var->s) { |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3219 | /* |
| 3220 | * Hack real-mode segments into vm86 compatibility. |
| 3221 | */ |
| 3222 | if (var->base == 0xffff0000 && var->selector == 0xf000) |
| 3223 | vmcs_writel(sf->base, 0xf0000); |
| 3224 | ar = 0xf3; |
| 3225 | } else |
| 3226 | ar = vmx_segment_access_rights(var); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3227 | |
| 3228 | /* |
| 3229 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 3230 | * qemu binaries. |
| 3231 | * IA32 arch specifies that at the time of processor reset the |
| 3232 | * "Accessed" bit in the AR field of segment registers is 1. And qemu |
| 3233 | * is setting it to 0 in the usedland code. This causes invalid guest |
| 3234 | * state vmexit when "unrestricted guest" mode is turned on. |
| 3235 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 3236 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 3237 | * kvm hack. |
| 3238 | */ |
| 3239 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
| 3240 | ar |= 0x1; /* Accessed */ |
| 3241 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3242 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3243 | __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3244 | } |
| 3245 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3246 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 3247 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3248 | 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] | 3249 | |
| 3250 | *db = (ar >> 14) & 1; |
| 3251 | *l = (ar >> 13) & 1; |
| 3252 | } |
| 3253 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3254 | 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] | 3255 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3256 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 3257 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3258 | } |
| 3259 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3260 | 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] | 3261 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3262 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 3263 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3264 | } |
| 3265 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3266 | 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] | 3267 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3268 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 3269 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3270 | } |
| 3271 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3272 | 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] | 3273 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3274 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 3275 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3276 | } |
| 3277 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3278 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 3279 | { |
| 3280 | struct kvm_segment var; |
| 3281 | u32 ar; |
| 3282 | |
| 3283 | vmx_get_segment(vcpu, &var, seg); |
| 3284 | ar = vmx_segment_access_rights(&var); |
| 3285 | |
| 3286 | if (var.base != (var.selector << 4)) |
| 3287 | return false; |
| 3288 | if (var.limit != 0xffff) |
| 3289 | return false; |
| 3290 | if (ar != 0xf3) |
| 3291 | return false; |
| 3292 | |
| 3293 | return true; |
| 3294 | } |
| 3295 | |
| 3296 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 3297 | { |
| 3298 | struct kvm_segment cs; |
| 3299 | unsigned int cs_rpl; |
| 3300 | |
| 3301 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 3302 | cs_rpl = cs.selector & SELECTOR_RPL_MASK; |
| 3303 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3304 | if (cs.unusable) |
| 3305 | return false; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3306 | if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK)) |
| 3307 | return false; |
| 3308 | if (!cs.s) |
| 3309 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3310 | if (cs.type & AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3311 | if (cs.dpl > cs_rpl) |
| 3312 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3313 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3314 | if (cs.dpl != cs_rpl) |
| 3315 | return false; |
| 3316 | } |
| 3317 | if (!cs.present) |
| 3318 | return false; |
| 3319 | |
| 3320 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 3321 | return true; |
| 3322 | } |
| 3323 | |
| 3324 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 3325 | { |
| 3326 | struct kvm_segment ss; |
| 3327 | unsigned int ss_rpl; |
| 3328 | |
| 3329 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 3330 | ss_rpl = ss.selector & SELECTOR_RPL_MASK; |
| 3331 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3332 | if (ss.unusable) |
| 3333 | return true; |
| 3334 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3335 | return false; |
| 3336 | if (!ss.s) |
| 3337 | return false; |
| 3338 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 3339 | return false; |
| 3340 | if (!ss.present) |
| 3341 | return false; |
| 3342 | |
| 3343 | return true; |
| 3344 | } |
| 3345 | |
| 3346 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 3347 | { |
| 3348 | struct kvm_segment var; |
| 3349 | unsigned int rpl; |
| 3350 | |
| 3351 | vmx_get_segment(vcpu, &var, seg); |
| 3352 | rpl = var.selector & SELECTOR_RPL_MASK; |
| 3353 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3354 | if (var.unusable) |
| 3355 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3356 | if (!var.s) |
| 3357 | return false; |
| 3358 | if (!var.present) |
| 3359 | return false; |
| 3360 | if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) { |
| 3361 | if (var.dpl < rpl) /* DPL < RPL */ |
| 3362 | return false; |
| 3363 | } |
| 3364 | |
| 3365 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 3366 | * rights flags |
| 3367 | */ |
| 3368 | return true; |
| 3369 | } |
| 3370 | |
| 3371 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 3372 | { |
| 3373 | struct kvm_segment tr; |
| 3374 | |
| 3375 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 3376 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3377 | if (tr.unusable) |
| 3378 | return false; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3379 | if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 3380 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3381 | 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] | 3382 | return false; |
| 3383 | if (!tr.present) |
| 3384 | return false; |
| 3385 | |
| 3386 | return true; |
| 3387 | } |
| 3388 | |
| 3389 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 3390 | { |
| 3391 | struct kvm_segment ldtr; |
| 3392 | |
| 3393 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 3394 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3395 | if (ldtr.unusable) |
| 3396 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3397 | if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 3398 | return false; |
| 3399 | if (ldtr.type != 2) |
| 3400 | return false; |
| 3401 | if (!ldtr.present) |
| 3402 | return false; |
| 3403 | |
| 3404 | return true; |
| 3405 | } |
| 3406 | |
| 3407 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 3408 | { |
| 3409 | struct kvm_segment cs, ss; |
| 3410 | |
| 3411 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 3412 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 3413 | |
| 3414 | return ((cs.selector & SELECTOR_RPL_MASK) == |
| 3415 | (ss.selector & SELECTOR_RPL_MASK)); |
| 3416 | } |
| 3417 | |
| 3418 | /* |
| 3419 | * Check if guest state is valid. Returns true if valid, false if |
| 3420 | * not. |
| 3421 | * We assume that registers are always usable |
| 3422 | */ |
| 3423 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 3424 | { |
| 3425 | /* real mode guest state checks */ |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 3426 | if (!is_protmode(vcpu)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3427 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 3428 | return false; |
| 3429 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 3430 | return false; |
| 3431 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 3432 | return false; |
| 3433 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 3434 | return false; |
| 3435 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 3436 | return false; |
| 3437 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 3438 | return false; |
| 3439 | } else { |
| 3440 | /* protected mode guest state checks */ |
| 3441 | if (!cs_ss_rpl_check(vcpu)) |
| 3442 | return false; |
| 3443 | if (!code_segment_valid(vcpu)) |
| 3444 | return false; |
| 3445 | if (!stack_segment_valid(vcpu)) |
| 3446 | return false; |
| 3447 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 3448 | return false; |
| 3449 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 3450 | return false; |
| 3451 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 3452 | return false; |
| 3453 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 3454 | return false; |
| 3455 | if (!tr_valid(vcpu)) |
| 3456 | return false; |
| 3457 | if (!ldtr_valid(vcpu)) |
| 3458 | return false; |
| 3459 | } |
| 3460 | /* TODO: |
| 3461 | * - Add checks on RIP |
| 3462 | * - Add checks on RFLAGS |
| 3463 | */ |
| 3464 | |
| 3465 | return true; |
| 3466 | } |
| 3467 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3468 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3469 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3470 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3471 | u16 data = 0; |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3472 | int r, idx, ret = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3473 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3474 | idx = srcu_read_lock(&kvm->srcu); |
| 3475 | fn = rmode_tss_base(kvm) >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3476 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3477 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3478 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3479 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 3480 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 3481 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3482 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3483 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3484 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 3485 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3486 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3487 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3488 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3489 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3490 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3491 | r = kvm_write_guest_page(kvm, fn, &data, |
| 3492 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 3493 | sizeof(u8)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3494 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3495 | goto out; |
| 3496 | |
| 3497 | ret = 1; |
| 3498 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3499 | srcu_read_unlock(&kvm->srcu, idx); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3500 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3501 | } |
| 3502 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3503 | static int init_rmode_identity_map(struct kvm *kvm) |
| 3504 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3505 | int i, idx, r, ret; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3506 | pfn_t identity_map_pfn; |
| 3507 | u32 tmp; |
| 3508 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3509 | if (!enable_ept) |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3510 | return 1; |
| 3511 | if (unlikely(!kvm->arch.ept_identity_pagetable)) { |
| 3512 | printk(KERN_ERR "EPT: identity-mapping pagetable " |
| 3513 | "haven't been allocated!\n"); |
| 3514 | return 0; |
| 3515 | } |
| 3516 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
| 3517 | return 1; |
| 3518 | ret = 0; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3519 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3520 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3521 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 3522 | if (r < 0) |
| 3523 | goto out; |
| 3524 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 3525 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 3526 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 3527 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 3528 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 3529 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 3530 | if (r < 0) |
| 3531 | goto out; |
| 3532 | } |
| 3533 | kvm->arch.ept_identity_pagetable_done = true; |
| 3534 | ret = 1; |
| 3535 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3536 | srcu_read_unlock(&kvm->srcu, idx); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3537 | return ret; |
| 3538 | } |
| 3539 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3540 | static void seg_setup(int seg) |
| 3541 | { |
| 3542 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3543 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3544 | |
| 3545 | vmcs_write16(sf->selector, 0); |
| 3546 | vmcs_writel(sf->base, 0); |
| 3547 | vmcs_write32(sf->limit, 0xffff); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3548 | if (enable_unrestricted_guest) { |
| 3549 | ar = 0x93; |
| 3550 | if (seg == VCPU_SREG_CS) |
| 3551 | ar |= 0x08; /* code segment */ |
| 3552 | } else |
| 3553 | ar = 0xf3; |
| 3554 | |
| 3555 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3556 | } |
| 3557 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3558 | static int alloc_apic_access_page(struct kvm *kvm) |
| 3559 | { |
| 3560 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 3561 | int r = 0; |
| 3562 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3563 | mutex_lock(&kvm->slots_lock); |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 3564 | if (kvm->arch.apic_access_page) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3565 | goto out; |
| 3566 | kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT; |
| 3567 | kvm_userspace_mem.flags = 0; |
| 3568 | kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL; |
| 3569 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
| 3570 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 3571 | if (r) |
| 3572 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3573 | |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 3574 | kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3575 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3576 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3577 | return r; |
| 3578 | } |
| 3579 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3580 | static int alloc_identity_pagetable(struct kvm *kvm) |
| 3581 | { |
| 3582 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 3583 | int r = 0; |
| 3584 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3585 | mutex_lock(&kvm->slots_lock); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3586 | if (kvm->arch.ept_identity_pagetable) |
| 3587 | goto out; |
| 3588 | kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT; |
| 3589 | kvm_userspace_mem.flags = 0; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3590 | kvm_userspace_mem.guest_phys_addr = |
| 3591 | kvm->arch.ept_identity_map_addr; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3592 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
| 3593 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 3594 | if (r) |
| 3595 | goto out; |
| 3596 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3597 | kvm->arch.ept_identity_pagetable = gfn_to_page(kvm, |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3598 | kvm->arch.ept_identity_map_addr >> PAGE_SHIFT); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3599 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3600 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3601 | return r; |
| 3602 | } |
| 3603 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3604 | static void allocate_vpid(struct vcpu_vmx *vmx) |
| 3605 | { |
| 3606 | int vpid; |
| 3607 | |
| 3608 | vmx->vpid = 0; |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 3609 | if (!enable_vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3610 | return; |
| 3611 | spin_lock(&vmx_vpid_lock); |
| 3612 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 3613 | if (vpid < VMX_NR_VPIDS) { |
| 3614 | vmx->vpid = vpid; |
| 3615 | __set_bit(vpid, vmx_vpid_bitmap); |
| 3616 | } |
| 3617 | spin_unlock(&vmx_vpid_lock); |
| 3618 | } |
| 3619 | |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 3620 | static void free_vpid(struct vcpu_vmx *vmx) |
| 3621 | { |
| 3622 | if (!enable_vpid) |
| 3623 | return; |
| 3624 | spin_lock(&vmx_vpid_lock); |
| 3625 | if (vmx->vpid != 0) |
| 3626 | __clear_bit(vmx->vpid, vmx_vpid_bitmap); |
| 3627 | spin_unlock(&vmx_vpid_lock); |
| 3628 | } |
| 3629 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3630 | static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3631 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3632 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3633 | |
| 3634 | if (!cpu_has_vmx_msr_bitmap()) |
| 3635 | return; |
| 3636 | |
| 3637 | /* |
| 3638 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 3639 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 3640 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 3641 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3642 | if (msr <= 0x1fff) { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3643 | __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */ |
| 3644 | __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3645 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 3646 | msr &= 0x1fff; |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3647 | __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */ |
| 3648 | __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3649 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3650 | } |
| 3651 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3652 | static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only) |
| 3653 | { |
| 3654 | if (!longmode_only) |
| 3655 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr); |
| 3656 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr); |
| 3657 | } |
| 3658 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3659 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3660 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 3661 | * will not change in the lifetime of the guest. |
| 3662 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 3663 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 3664 | */ |
| 3665 | static void vmx_set_constant_host_state(void) |
| 3666 | { |
| 3667 | u32 low32, high32; |
| 3668 | unsigned long tmpl; |
| 3669 | struct desc_ptr dt; |
| 3670 | |
| 3671 | vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */ |
| 3672 | vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */ |
| 3673 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 3674 | |
| 3675 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 3676 | #ifdef CONFIG_X86_64 |
| 3677 | /* |
| 3678 | * Load null selectors, so we can avoid reloading them in |
| 3679 | * __vmx_load_host_state(), in case userspace uses the null selectors |
| 3680 | * too (the expected case). |
| 3681 | */ |
| 3682 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 3683 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 3684 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3685 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3686 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 3687 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3688 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3689 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 3690 | |
| 3691 | native_store_idt(&dt); |
| 3692 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
| 3693 | |
| 3694 | asm("mov $.Lkvm_vmx_return, %0" : "=r"(tmpl)); |
| 3695 | vmcs_writel(HOST_RIP, tmpl); /* 22.2.5 */ |
| 3696 | |
| 3697 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 3698 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 3699 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 3700 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 3701 | |
| 3702 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 3703 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 3704 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 3705 | } |
| 3706 | } |
| 3707 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3708 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 3709 | { |
| 3710 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 3711 | if (enable_ept) |
| 3712 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 3713 | if (is_guest_mode(&vmx->vcpu)) |
| 3714 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 3715 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3716 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 3717 | } |
| 3718 | |
| 3719 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 3720 | { |
| 3721 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
| 3722 | if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) { |
| 3723 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 3724 | #ifdef CONFIG_X86_64 |
| 3725 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 3726 | CPU_BASED_CR8_LOAD_EXITING; |
| 3727 | #endif |
| 3728 | } |
| 3729 | if (!enable_ept) |
| 3730 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 3731 | CPU_BASED_CR3_LOAD_EXITING | |
| 3732 | CPU_BASED_INVLPG_EXITING; |
| 3733 | return exec_control; |
| 3734 | } |
| 3735 | |
| 3736 | static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) |
| 3737 | { |
| 3738 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
| 3739 | if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 3740 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 3741 | if (vmx->vpid == 0) |
| 3742 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 3743 | if (!enable_ept) { |
| 3744 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 3745 | enable_unrestricted_guest = 0; |
| 3746 | } |
| 3747 | if (!enable_unrestricted_guest) |
| 3748 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 3749 | if (!ple_gap) |
| 3750 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 3751 | return exec_control; |
| 3752 | } |
| 3753 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3754 | static void ept_set_mmio_spte_mask(void) |
| 3755 | { |
| 3756 | /* |
| 3757 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 3758 | * of an EPT paging-structure entry is 110b (write/execute). |
| 3759 | * Also, magic bits (0xffull << 49) is set to quickly identify mmio |
| 3760 | * spte. |
| 3761 | */ |
| 3762 | kvm_mmu_set_mmio_spte_mask(0xffull << 49 | 0x6ull); |
| 3763 | } |
| 3764 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3765 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3766 | * Sets up the vmcs for emulated real mode. |
| 3767 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3768 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3769 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 3770 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3771 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 3772 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3773 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3774 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3775 | /* I/O */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3776 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
| 3777 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3778 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3779 | if (cpu_has_vmx_msr_bitmap()) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3780 | vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3781 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3782 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 3783 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3784 | /* Control */ |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3785 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, |
| 3786 | vmcs_config.pin_based_exec_ctrl); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3787 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3788 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3789 | |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 3790 | if (cpu_has_secondary_exec_ctrls()) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3791 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 3792 | vmx_secondary_exec_control(vmx)); |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 3793 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3794 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3795 | if (ple_gap) { |
| 3796 | vmcs_write32(PLE_GAP, ple_gap); |
| 3797 | vmcs_write32(PLE_WINDOW, ple_window); |
| 3798 | } |
| 3799 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 3800 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 3801 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3802 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 3803 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 3804 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 3805 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3806 | vmx_set_constant_host_state(); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3807 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3808 | rdmsrl(MSR_FS_BASE, a); |
| 3809 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 3810 | rdmsrl(MSR_GS_BASE, a); |
| 3811 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 3812 | #else |
| 3813 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 3814 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 3815 | #endif |
| 3816 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3817 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 3818 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 3819 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3820 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 3821 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3822 | |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3823 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3824 | u32 msr_low, msr_high; |
| 3825 | u64 host_pat; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3826 | rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high); |
| 3827 | host_pat = msr_low | ((u64) msr_high << 32); |
| 3828 | /* Write the default value follow host pat */ |
| 3829 | vmcs_write64(GUEST_IA32_PAT, host_pat); |
| 3830 | /* Keep arch.pat sync with GUEST_IA32_PAT */ |
| 3831 | vmx->vcpu.arch.pat = host_pat; |
| 3832 | } |
| 3833 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3834 | for (i = 0; i < NR_VMX_MSR; ++i) { |
| 3835 | u32 index = vmx_msr_index[i]; |
| 3836 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3837 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3838 | |
| 3839 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 3840 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 3841 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 3842 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3843 | vmx->guest_msrs[j].index = i; |
| 3844 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 3845 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3846 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3847 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3848 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3849 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3850 | |
| 3851 | /* 22.2.1, 20.8.1 */ |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3852 | vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl); |
| 3853 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3854 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3855 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3856 | |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 3857 | kvm_write_tsc(&vmx->vcpu, 0); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3858 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3859 | return 0; |
| 3860 | } |
| 3861 | |
| 3862 | static int vmx_vcpu_reset(struct kvm_vcpu *vcpu) |
| 3863 | { |
| 3864 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3865 | u64 msr; |
Xiao Guangrong | 4b9d3a0 | 2010-06-08 10:15:51 +0800 | [diff] [blame] | 3866 | int ret; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3867 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3868 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3869 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3870 | vmx->rmode.vm86_active = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3871 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3872 | vmx->soft_vnmi_blocked = 0; |
| 3873 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3874 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 3875 | kvm_set_cr8(&vmx->vcpu, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3876 | msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 3877 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3878 | msr |= MSR_IA32_APICBASE_BSP; |
| 3879 | kvm_set_apic_base(&vmx->vcpu, msr); |
| 3880 | |
Jan Kiszka | 10ab25c | 2010-05-25 16:01:50 +0200 | [diff] [blame] | 3881 | ret = fx_init(&vmx->vcpu); |
| 3882 | if (ret != 0) |
| 3883 | goto out; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3884 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3885 | vmx_segment_cache_clear(vmx); |
| 3886 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 3887 | seg_setup(VCPU_SREG_CS); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3888 | /* |
| 3889 | * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode |
| 3890 | * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh. |
| 3891 | */ |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 3892 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) { |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3893 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
| 3894 | vmcs_writel(GUEST_CS_BASE, 0x000f0000); |
| 3895 | } else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3896 | vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8); |
| 3897 | vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3898 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3899 | |
| 3900 | seg_setup(VCPU_SREG_DS); |
| 3901 | seg_setup(VCPU_SREG_ES); |
| 3902 | seg_setup(VCPU_SREG_FS); |
| 3903 | seg_setup(VCPU_SREG_GS); |
| 3904 | seg_setup(VCPU_SREG_SS); |
| 3905 | |
| 3906 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 3907 | vmcs_writel(GUEST_TR_BASE, 0); |
| 3908 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 3909 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 3910 | |
| 3911 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 3912 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 3913 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 3914 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 3915 | |
| 3916 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 3917 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 3918 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 3919 | |
| 3920 | vmcs_writel(GUEST_RFLAGS, 0x02); |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 3921 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3922 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3923 | else |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3924 | kvm_rip_write(vcpu, 0); |
| 3925 | kvm_register_write(vcpu, VCPU_REGS_RSP, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3926 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3927 | vmcs_writel(GUEST_DR7, 0x400); |
| 3928 | |
| 3929 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 3930 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 3931 | |
| 3932 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 3933 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 3934 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 3935 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3936 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
| 3937 | vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
| 3938 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3939 | /* Special registers */ |
| 3940 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 3941 | |
| 3942 | setup_msrs(vmx); |
| 3943 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3944 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 3945 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3946 | if (cpu_has_vmx_tpr_shadow()) { |
| 3947 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
| 3948 | if (vm_need_tpr_shadow(vmx->vcpu.kvm)) |
| 3949 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Takuya Yoshikawa | afc2018 | 2011-03-05 12:40:20 +0900 | [diff] [blame] | 3950 | __pa(vmx->vcpu.arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3951 | vmcs_write32(TPR_THRESHOLD, 0); |
| 3952 | } |
| 3953 | |
| 3954 | if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 3955 | vmcs_write64(APIC_ACCESS_ADDR, |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 3956 | page_to_phys(vmx->vcpu.kvm->arch.apic_access_page)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3957 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3958 | if (vmx->vpid != 0) |
| 3959 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 3960 | |
Eduardo Habkost | fa40052 | 2009-10-24 02:49:58 -0200 | [diff] [blame] | 3961 | 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] | 3962 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 3963 | vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */ |
Marcelo Tosatti | 7a4f5ad | 2012-03-27 19:47:26 -0300 | [diff] [blame] | 3964 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3965 | vmx_set_cr4(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3966 | vmx_set_efer(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3967 | vmx_fpu_activate(&vmx->vcpu); |
| 3968 | update_exception_bitmap(&vmx->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3969 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 3970 | vpid_sync_context(vmx); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3971 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3972 | ret = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3973 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3974 | /* HACK: Don't enable emulation on guest boot/reset */ |
| 3975 | vmx->emulation_required = 0; |
| 3976 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3977 | out: |
| 3978 | return ret; |
| 3979 | } |
| 3980 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 3981 | /* |
| 3982 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 3983 | * For most existing hypervisors, this will always return true. |
| 3984 | */ |
| 3985 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 3986 | { |
| 3987 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 3988 | PIN_BASED_EXT_INTR_MASK; |
| 3989 | } |
| 3990 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3991 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
| 3992 | { |
| 3993 | u32 cpu_based_vm_exec_control; |
Nadav Har'El | d6185f2 | 2011-09-22 13:52:56 +0300 | [diff] [blame] | 3994 | if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) { |
| 3995 | /* |
| 3996 | * We get here if vmx_interrupt_allowed() said we can't |
| 3997 | * inject to L1 now because L2 must run. Ask L2 to exit |
| 3998 | * right after entry, so we can inject to L1 more promptly. |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 3999 | */ |
Nadav Har'El | d6185f2 | 2011-09-22 13:52:56 +0300 | [diff] [blame] | 4000 | kvm_make_request(KVM_REQ_IMMEDIATE_EXIT, vcpu); |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 4001 | return; |
Nadav Har'El | d6185f2 | 2011-09-22 13:52:56 +0300 | [diff] [blame] | 4002 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4003 | |
| 4004 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4005 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 4006 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 4007 | } |
| 4008 | |
| 4009 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
| 4010 | { |
| 4011 | u32 cpu_based_vm_exec_control; |
| 4012 | |
| 4013 | if (!cpu_has_virtual_nmis()) { |
| 4014 | enable_irq_window(vcpu); |
| 4015 | return; |
| 4016 | } |
| 4017 | |
Avi Kivity | 30bd0c4 | 2010-11-01 23:20:48 +0200 | [diff] [blame] | 4018 | if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
| 4019 | enable_irq_window(vcpu); |
| 4020 | return; |
| 4021 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4022 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4023 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING; |
| 4024 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 4025 | } |
| 4026 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4027 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4028 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 4029 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4030 | uint32_t intr; |
| 4031 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 4032 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4033 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4034 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 4035 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4036 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 4037 | int inc_eip = 0; |
| 4038 | if (vcpu->arch.interrupt.soft) |
| 4039 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 4040 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 4041 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4042 | return; |
| 4043 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4044 | intr = irq | INTR_INFO_VALID_MASK; |
| 4045 | if (vcpu->arch.interrupt.soft) { |
| 4046 | intr |= INTR_TYPE_SOFT_INTR; |
| 4047 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 4048 | vmx->vcpu.arch.event_exit_inst_len); |
| 4049 | } else |
| 4050 | intr |= INTR_TYPE_EXT_INTR; |
| 4051 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4052 | } |
| 4053 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4054 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 4055 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 4056 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4057 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 4058 | if (is_guest_mode(vcpu)) |
| 4059 | return; |
| 4060 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4061 | if (!cpu_has_virtual_nmis()) { |
| 4062 | /* |
| 4063 | * Tracking the NMI-blocked state in software is built upon |
| 4064 | * finding the next open IRQ window. This, in turn, depends on |
| 4065 | * well-behaving guests: They have to keep IRQs disabled at |
| 4066 | * least as long as the NMI handler runs. Otherwise we may |
| 4067 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 4068 | * highly unlikely, we can live with the residual risk. |
| 4069 | */ |
| 4070 | vmx->soft_vnmi_blocked = 1; |
| 4071 | vmx->vnmi_blocked_time = 0; |
| 4072 | } |
| 4073 | |
Jan Kiszka | 487b391 | 2008-09-26 09:30:56 +0200 | [diff] [blame] | 4074 | ++vcpu->stat.nmi_injections; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 4075 | vmx->nmi_known_unmasked = false; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4076 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 4077 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 4078 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 4079 | return; |
| 4080 | } |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4081 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 4082 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4083 | } |
| 4084 | |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4085 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 4086 | { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4087 | if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked) |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4088 | return 0; |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 4089 | |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4090 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
Avi Kivity | 30bd0c4 | 2010-11-01 23:20:48 +0200 | [diff] [blame] | 4091 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 4092 | | GUEST_INTR_STATE_NMI)); |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 4093 | } |
| 4094 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4095 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 4096 | { |
| 4097 | if (!cpu_has_virtual_nmis()) |
| 4098 | return to_vmx(vcpu)->soft_vnmi_blocked; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 4099 | if (to_vmx(vcpu)->nmi_known_unmasked) |
| 4100 | return false; |
Avi Kivity | c332c83 | 2010-05-04 12:24:12 +0300 | [diff] [blame] | 4101 | return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 4105 | { |
| 4106 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4107 | |
| 4108 | if (!cpu_has_virtual_nmis()) { |
| 4109 | if (vmx->soft_vnmi_blocked != masked) { |
| 4110 | vmx->soft_vnmi_blocked = masked; |
| 4111 | vmx->vnmi_blocked_time = 0; |
| 4112 | } |
| 4113 | } else { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 4114 | vmx->nmi_known_unmasked = !masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4115 | if (masked) |
| 4116 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 4117 | GUEST_INTR_STATE_NMI); |
| 4118 | else |
| 4119 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 4120 | GUEST_INTR_STATE_NMI); |
| 4121 | } |
| 4122 | } |
| 4123 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4124 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 4125 | { |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 4126 | if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) { |
Nadav Har'El | 51cfe38 | 2011-09-22 13:53:26 +0300 | [diff] [blame] | 4127 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4128 | if (to_vmx(vcpu)->nested.nested_run_pending || |
| 4129 | (vmcs12->idt_vectoring_info_field & |
| 4130 | VECTORING_INFO_VALID_MASK)) |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 4131 | return 0; |
| 4132 | nested_vmx_vmexit(vcpu); |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 4133 | vmcs12->vm_exit_reason = EXIT_REASON_EXTERNAL_INTERRUPT; |
| 4134 | vmcs12->vm_exit_intr_info = 0; |
| 4135 | /* fall through to normal code, but now in L1, not L2 */ |
| 4136 | } |
| 4137 | |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4138 | return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
| 4139 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 4140 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4141 | } |
| 4142 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4143 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 4144 | { |
| 4145 | int ret; |
| 4146 | struct kvm_userspace_memory_region tss_mem = { |
Sheng Yang | 6fe6397 | 2008-10-16 17:30:58 +0800 | [diff] [blame] | 4147 | .slot = TSS_PRIVATE_MEMSLOT, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4148 | .guest_phys_addr = addr, |
| 4149 | .memory_size = PAGE_SIZE * 3, |
| 4150 | .flags = 0, |
| 4151 | }; |
| 4152 | |
| 4153 | ret = kvm_set_memory_region(kvm, &tss_mem, 0); |
| 4154 | if (ret) |
| 4155 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 4156 | kvm->arch.tss_addr = addr; |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 4157 | if (!init_rmode_tss(kvm)) |
| 4158 | return -ENOMEM; |
| 4159 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4160 | return 0; |
| 4161 | } |
| 4162 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4163 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 4164 | int vec, u32 err_code) |
| 4165 | { |
Nitin A Kamble | b3f3770 | 2007-05-17 15:50:34 +0300 | [diff] [blame] | 4166 | /* |
| 4167 | * Instruction with address size override prefix opcode 0x67 |
| 4168 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 4169 | */ |
| 4170 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4171 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4172 | return 1; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4173 | /* |
| 4174 | * Forward all other exceptions that are valid in real mode. |
| 4175 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 4176 | * the required debugging infrastructure rework. |
| 4177 | */ |
| 4178 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4179 | case DB_VECTOR: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4180 | if (vcpu->guest_debug & |
| 4181 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 4182 | return 0; |
| 4183 | kvm_queue_exception(vcpu, vec); |
| 4184 | return 1; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4185 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 4186 | /* |
| 4187 | * Update instruction length as we may reinject the exception |
| 4188 | * from user space while in guest debugging mode. |
| 4189 | */ |
| 4190 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 4191 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4192 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 4193 | return 0; |
| 4194 | /* fall through */ |
| 4195 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4196 | case OF_VECTOR: |
| 4197 | case BR_VECTOR: |
| 4198 | case UD_VECTOR: |
| 4199 | case DF_VECTOR: |
| 4200 | case SS_VECTOR: |
| 4201 | case GP_VECTOR: |
| 4202 | case MF_VECTOR: |
| 4203 | kvm_queue_exception(vcpu, vec); |
| 4204 | return 1; |
| 4205 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4206 | return 0; |
| 4207 | } |
| 4208 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4209 | /* |
| 4210 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 4211 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 4212 | * We pass a fake environment to the machine check handler because we want |
| 4213 | * the guest to be always treated like user space, no matter what context |
| 4214 | * it used internally. |
| 4215 | */ |
| 4216 | static void kvm_machine_check(void) |
| 4217 | { |
| 4218 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 4219 | struct pt_regs regs = { |
| 4220 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 4221 | .flags = X86_EFLAGS_IF, |
| 4222 | }; |
| 4223 | |
| 4224 | do_machine_check(®s, 0); |
| 4225 | #endif |
| 4226 | } |
| 4227 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4228 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4229 | { |
| 4230 | /* already handled by vcpu_run */ |
| 4231 | return 1; |
| 4232 | } |
| 4233 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4234 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4235 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 4236 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4237 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4238 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4239 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4240 | u32 vect_info; |
| 4241 | enum emulation_result er; |
| 4242 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 4243 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 4244 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4245 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4246 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4247 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4248 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4249 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
Avi Kivity | 65ac726 | 2009-11-04 11:59:01 +0200 | [diff] [blame] | 4250 | !is_page_fault(intr_info)) { |
| 4251 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4252 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
| 4253 | vcpu->run->internal.ndata = 2; |
| 4254 | vcpu->run->internal.data[0] = vect_info; |
| 4255 | vcpu->run->internal.data[1] = intr_info; |
| 4256 | return 0; |
| 4257 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4258 | |
Jan Kiszka | e4a4188 | 2008-09-26 09:30:46 +0200 | [diff] [blame] | 4259 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 4260 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 4261 | |
| 4262 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 4263 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 4264 | return 1; |
| 4265 | } |
| 4266 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4267 | if (is_invalid_opcode(intr_info)) { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4268 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4269 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 4270 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4271 | return 1; |
| 4272 | } |
| 4273 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4274 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 4275 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4276 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 4277 | if (is_page_fault(intr_info)) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4278 | /* EPT won't cause page fault directly */ |
Julia Lawall | cf3ace7 | 2011-08-02 12:34:57 +0200 | [diff] [blame] | 4279 | BUG_ON(enable_ept); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4280 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4281 | trace_kvm_page_fault(cr2, error_code); |
| 4282 | |
Gleb Natapov | 3298b75 | 2009-05-11 13:35:46 +0300 | [diff] [blame] | 4283 | if (kvm_event_needs_reinjection(vcpu)) |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 4284 | kvm_mmu_unprotect_page_virt(vcpu, cr2); |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4285 | return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4286 | } |
| 4287 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4288 | if (vmx->rmode.vm86_active && |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4289 | handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK, |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 4290 | error_code)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4291 | if (vcpu->arch.halt_request) { |
| 4292 | vcpu->arch.halt_request = 0; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 4293 | return kvm_emulate_halt(vcpu); |
| 4294 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4295 | return 1; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 4296 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4297 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4298 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4299 | switch (ex_no) { |
| 4300 | case DB_VECTOR: |
| 4301 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 4302 | if (!(vcpu->guest_debug & |
| 4303 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
| 4304 | vcpu->arch.dr6 = dr6 | DR6_FIXED_1; |
| 4305 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 4306 | return 1; |
| 4307 | } |
| 4308 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 4309 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 4310 | /* fall through */ |
| 4311 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 4312 | /* |
| 4313 | * Update instruction length as we may reinject #BP from |
| 4314 | * user space while in guest debugging mode. Reading it for |
| 4315 | * #DB as well causes no harm, it is not used in that case. |
| 4316 | */ |
| 4317 | vmx->vcpu.arch.event_exit_inst_len = |
| 4318 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4319 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 4320 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4321 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 4322 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4323 | break; |
| 4324 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4325 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 4326 | kvm_run->ex.exception = ex_no; |
| 4327 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4328 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4329 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4330 | return 0; |
| 4331 | } |
| 4332 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4333 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4334 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 4335 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4336 | return 1; |
| 4337 | } |
| 4338 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4339 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4340 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4341 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4342 | return 0; |
| 4343 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4344 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4345 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4346 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4347 | unsigned long exit_qualification; |
Jan Kiszka | 34c33d1 | 2009-02-08 13:28:15 +0100 | [diff] [blame] | 4348 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4349 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4350 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4351 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4352 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 4353 | in = (exit_qualification & 8) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 4354 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4355 | ++vcpu->stat.io_exits; |
| 4356 | |
| 4357 | if (string || in) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4358 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4359 | |
| 4360 | port = exit_qualification >> 16; |
| 4361 | size = (exit_qualification & 7) + 1; |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 4362 | skip_emulated_instruction(vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4363 | |
| 4364 | return kvm_fast_pio_out(vcpu, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4365 | } |
| 4366 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4367 | static void |
| 4368 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 4369 | { |
| 4370 | /* |
| 4371 | * Patch in the VMCALL instruction: |
| 4372 | */ |
| 4373 | hypercall[0] = 0x0f; |
| 4374 | hypercall[1] = 0x01; |
| 4375 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4376 | } |
| 4377 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4378 | /* called to set cr0 as approriate for a mov-to-cr0 exit. */ |
| 4379 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 4380 | { |
| 4381 | if (to_vmx(vcpu)->nested.vmxon && |
| 4382 | ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON)) |
| 4383 | return 1; |
| 4384 | |
| 4385 | if (is_guest_mode(vcpu)) { |
| 4386 | /* |
| 4387 | * We get here when L2 changed cr0 in a way that did not change |
| 4388 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
| 4389 | * but did change L0 shadowed bits. This can currently happen |
| 4390 | * with the TS bit: L0 may want to leave TS on (for lazy fpu |
| 4391 | * loading) while pretending to allow the guest to change it. |
| 4392 | */ |
| 4393 | if (kvm_set_cr0(vcpu, (val & vcpu->arch.cr0_guest_owned_bits) | |
| 4394 | (vcpu->arch.cr0 & ~vcpu->arch.cr0_guest_owned_bits))) |
| 4395 | return 1; |
| 4396 | vmcs_writel(CR0_READ_SHADOW, val); |
| 4397 | return 0; |
| 4398 | } else |
| 4399 | return kvm_set_cr0(vcpu, val); |
| 4400 | } |
| 4401 | |
| 4402 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 4403 | { |
| 4404 | if (is_guest_mode(vcpu)) { |
| 4405 | if (kvm_set_cr4(vcpu, (val & vcpu->arch.cr4_guest_owned_bits) | |
| 4406 | (vcpu->arch.cr4 & ~vcpu->arch.cr4_guest_owned_bits))) |
| 4407 | return 1; |
| 4408 | vmcs_writel(CR4_READ_SHADOW, val); |
| 4409 | return 0; |
| 4410 | } else |
| 4411 | return kvm_set_cr4(vcpu, val); |
| 4412 | } |
| 4413 | |
| 4414 | /* called to set cr0 as approriate for clts instruction exit. */ |
| 4415 | static void handle_clts(struct kvm_vcpu *vcpu) |
| 4416 | { |
| 4417 | if (is_guest_mode(vcpu)) { |
| 4418 | /* |
| 4419 | * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS |
| 4420 | * but we did (!fpu_active). We need to keep GUEST_CR0.TS on, |
| 4421 | * just pretend it's off (also in arch.cr0 for fpu_activate). |
| 4422 | */ |
| 4423 | vmcs_writel(CR0_READ_SHADOW, |
| 4424 | vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS); |
| 4425 | vcpu->arch.cr0 &= ~X86_CR0_TS; |
| 4426 | } else |
| 4427 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
| 4428 | } |
| 4429 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4430 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4431 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4432 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4433 | int cr; |
| 4434 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 4435 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4436 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4437 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4438 | cr = exit_qualification & 15; |
| 4439 | reg = (exit_qualification >> 8) & 15; |
| 4440 | switch ((exit_qualification >> 4) & 3) { |
| 4441 | case 0: /* mov to cr */ |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4442 | val = kvm_register_read(vcpu, reg); |
| 4443 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4444 | switch (cr) { |
| 4445 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4446 | err = handle_set_cr0(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4447 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4448 | return 1; |
| 4449 | case 3: |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 4450 | err = kvm_set_cr3(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4451 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4452 | return 1; |
| 4453 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4454 | err = handle_set_cr4(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4455 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4456 | return 1; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4457 | case 8: { |
| 4458 | u8 cr8_prev = kvm_get_cr8(vcpu); |
| 4459 | u8 cr8 = kvm_register_read(vcpu, reg); |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 4460 | err = kvm_set_cr8(vcpu, cr8); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4461 | kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4462 | if (irqchip_in_kernel(vcpu->kvm)) |
| 4463 | return 1; |
| 4464 | if (cr8_prev <= cr8) |
| 4465 | return 1; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4466 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4467 | return 0; |
| 4468 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4469 | }; |
| 4470 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4471 | case 2: /* clts */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4472 | handle_clts(vcpu); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4473 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4474 | skip_emulated_instruction(vcpu); |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 4475 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4476 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4477 | case 1: /*mov from cr*/ |
| 4478 | switch (cr) { |
| 4479 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 4480 | val = kvm_read_cr3(vcpu); |
| 4481 | kvm_register_write(vcpu, reg, val); |
| 4482 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4483 | skip_emulated_instruction(vcpu); |
| 4484 | return 1; |
| 4485 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4486 | val = kvm_get_cr8(vcpu); |
| 4487 | kvm_register_write(vcpu, reg, val); |
| 4488 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4489 | skip_emulated_instruction(vcpu); |
| 4490 | return 1; |
| 4491 | } |
| 4492 | break; |
| 4493 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 4494 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4495 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 4496 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4497 | |
| 4498 | skip_emulated_instruction(vcpu); |
| 4499 | return 1; |
| 4500 | default: |
| 4501 | break; |
| 4502 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4503 | vcpu->run->exit_reason = 0; |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 4504 | pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4505 | (int)(exit_qualification >> 4) & 3, cr); |
| 4506 | return 0; |
| 4507 | } |
| 4508 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4509 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4510 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4511 | unsigned long exit_qualification; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4512 | int dr, reg; |
| 4513 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 4514 | /* 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] | 4515 | if (!kvm_require_cpl(vcpu, 0)) |
| 4516 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4517 | dr = vmcs_readl(GUEST_DR7); |
| 4518 | if (dr & DR7_GD) { |
| 4519 | /* |
| 4520 | * As the vm-exit takes precedence over the debug trap, we |
| 4521 | * need to emulate the latter, either for the host or the |
| 4522 | * guest debugging itself. |
| 4523 | */ |
| 4524 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4525 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
| 4526 | vcpu->run->debug.arch.dr7 = dr; |
| 4527 | vcpu->run->debug.arch.pc = |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4528 | vmcs_readl(GUEST_CS_BASE) + |
| 4529 | vmcs_readl(GUEST_RIP); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4530 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 4531 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4532 | return 0; |
| 4533 | } else { |
| 4534 | vcpu->arch.dr7 &= ~DR7_GD; |
| 4535 | vcpu->arch.dr6 |= DR6_BD; |
| 4536 | vmcs_writel(GUEST_DR7, vcpu->arch.dr7); |
| 4537 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 4538 | return 1; |
| 4539 | } |
| 4540 | } |
| 4541 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4542 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4543 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 4544 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 4545 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4546 | unsigned long val; |
| 4547 | if (!kvm_get_dr(vcpu, dr, &val)) |
| 4548 | kvm_register_write(vcpu, reg, val); |
| 4549 | } else |
| 4550 | kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4551 | skip_emulated_instruction(vcpu); |
| 4552 | return 1; |
| 4553 | } |
| 4554 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4555 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 4556 | { |
| 4557 | vmcs_writel(GUEST_DR7, val); |
| 4558 | } |
| 4559 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4560 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4561 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 4562 | kvm_emulate_cpuid(vcpu); |
| 4563 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4564 | } |
| 4565 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4566 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4567 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4568 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4569 | u64 data; |
| 4570 | |
| 4571 | if (vmx_get_msr(vcpu, ecx, &data)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4572 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4573 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4574 | return 1; |
| 4575 | } |
| 4576 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4577 | trace_kvm_msr_read(ecx, data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4578 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4579 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4580 | vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u; |
| 4581 | vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4582 | skip_emulated_instruction(vcpu); |
| 4583 | return 1; |
| 4584 | } |
| 4585 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4586 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4587 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4588 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 4589 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 4590 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4591 | |
| 4592 | if (vmx_set_msr(vcpu, ecx, data) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4593 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4594 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4595 | return 1; |
| 4596 | } |
| 4597 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4598 | trace_kvm_msr_write(ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4599 | skip_emulated_instruction(vcpu); |
| 4600 | return 1; |
| 4601 | } |
| 4602 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4603 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4604 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4605 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4606 | return 1; |
| 4607 | } |
| 4608 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4609 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4610 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4611 | u32 cpu_based_vm_exec_control; |
| 4612 | |
| 4613 | /* clear pending irq */ |
| 4614 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4615 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 4616 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4617 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4618 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4619 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 4620 | ++vcpu->stat.irq_window_exits; |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4621 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4622 | /* |
| 4623 | * If the user space waits to inject interrupts, exit as soon as |
| 4624 | * possible |
| 4625 | */ |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 4626 | if (!irqchip_in_kernel(vcpu->kvm) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4627 | vcpu->run->request_interrupt_window && |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 4628 | !kvm_cpu_has_interrupt(vcpu)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4629 | vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4630 | return 0; |
| 4631 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4632 | return 1; |
| 4633 | } |
| 4634 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4635 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4636 | { |
| 4637 | skip_emulated_instruction(vcpu); |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 4638 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4639 | } |
| 4640 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4641 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4642 | { |
Dor Laor | 510043d | 2007-02-19 18:25:43 +0200 | [diff] [blame] | 4643 | skip_emulated_instruction(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4644 | kvm_emulate_hypercall(vcpu); |
| 4645 | return 1; |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4646 | } |
| 4647 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 4648 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 4649 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4650 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 4651 | } |
| 4652 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4653 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4654 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4655 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4656 | |
| 4657 | kvm_mmu_invlpg(vcpu, exit_qualification); |
| 4658 | skip_emulated_instruction(vcpu); |
| 4659 | return 1; |
| 4660 | } |
| 4661 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 4662 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 4663 | { |
| 4664 | int err; |
| 4665 | |
| 4666 | err = kvm_rdpmc(vcpu); |
| 4667 | kvm_complete_insn_gp(vcpu, err); |
| 4668 | |
| 4669 | return 1; |
| 4670 | } |
| 4671 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4672 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 4673 | { |
| 4674 | skip_emulated_instruction(vcpu); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 4675 | kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 4676 | return 1; |
| 4677 | } |
| 4678 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 4679 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 4680 | { |
| 4681 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 4682 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4683 | |
| 4684 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
| 4685 | skip_emulated_instruction(vcpu); |
| 4686 | return 1; |
| 4687 | } |
| 4688 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4689 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4690 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 4691 | if (likely(fasteoi)) { |
| 4692 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4693 | int access_type, offset; |
| 4694 | |
| 4695 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 4696 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 4697 | /* |
| 4698 | * Sane guest uses MOV to write EOI, with written value |
| 4699 | * not cared. So make a short-circuit here by avoiding |
| 4700 | * heavy instruction emulation. |
| 4701 | */ |
| 4702 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 4703 | (offset == APIC_EOI)) { |
| 4704 | kvm_lapic_set_eoi(vcpu); |
| 4705 | skip_emulated_instruction(vcpu); |
| 4706 | return 1; |
| 4707 | } |
| 4708 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4709 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4710 | } |
| 4711 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4712 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4713 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 4714 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4715 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 4716 | bool has_error_code = false; |
| 4717 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4718 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 4719 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4720 | |
| 4721 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 4722 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4723 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4724 | |
| 4725 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4726 | |
| 4727 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4728 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 4729 | switch (type) { |
| 4730 | case INTR_TYPE_NMI_INTR: |
| 4731 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 4732 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4733 | break; |
| 4734 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4735 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4736 | kvm_clear_interrupt_queue(vcpu); |
| 4737 | break; |
| 4738 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 4739 | if (vmx->idt_vectoring_info & |
| 4740 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 4741 | has_error_code = true; |
| 4742 | error_code = |
| 4743 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 4744 | } |
| 4745 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4746 | case INTR_TYPE_SOFT_EXCEPTION: |
| 4747 | kvm_clear_exception_queue(vcpu); |
| 4748 | break; |
| 4749 | default: |
| 4750 | break; |
| 4751 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 4752 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4753 | tss_selector = exit_qualification; |
| 4754 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4755 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 4756 | type != INTR_TYPE_EXT_INTR && |
| 4757 | type != INTR_TYPE_NMI_INTR)) |
| 4758 | skip_emulated_instruction(vcpu); |
| 4759 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 4760 | if (kvm_task_switch(vcpu, tss_selector, |
| 4761 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 4762 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 4763 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4764 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 4765 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4766 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 4767 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4768 | |
| 4769 | /* clear all local breakpoint enable flags */ |
| 4770 | vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55); |
| 4771 | |
| 4772 | /* |
| 4773 | * TODO: What about debug traps on tss switch? |
| 4774 | * Are we supposed to inject them and update dr6? |
| 4775 | */ |
| 4776 | |
| 4777 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4778 | } |
| 4779 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4780 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4781 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4782 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4783 | gpa_t gpa; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4784 | int gla_validity; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4785 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4786 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4787 | |
| 4788 | if (exit_qualification & (1 << 6)) { |
| 4789 | printk(KERN_ERR "EPT: GPA exceeds GAW!\n"); |
Jan Kiszka | 7f582ab | 2009-07-22 23:53:01 +0200 | [diff] [blame] | 4790 | return -EINVAL; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | gla_validity = (exit_qualification >> 7) & 0x3; |
| 4794 | if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) { |
| 4795 | printk(KERN_ERR "EPT: Handling EPT violation failed!\n"); |
| 4796 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 4797 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4798 | vmcs_readl(GUEST_LINEAR_ADDRESS)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4799 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 4800 | (long unsigned int)exit_qualification); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4801 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 4802 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION; |
Avi Kivity | 596ae89 | 2009-06-03 14:12:10 +0300 | [diff] [blame] | 4803 | return 0; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4807 | trace_kvm_page_fault(gpa, exit_qualification); |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4808 | return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4809 | } |
| 4810 | |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4811 | static u64 ept_rsvd_mask(u64 spte, int level) |
| 4812 | { |
| 4813 | int i; |
| 4814 | u64 mask = 0; |
| 4815 | |
| 4816 | for (i = 51; i > boot_cpu_data.x86_phys_bits; i--) |
| 4817 | mask |= (1ULL << i); |
| 4818 | |
| 4819 | if (level > 2) |
| 4820 | /* bits 7:3 reserved */ |
| 4821 | mask |= 0xf8; |
| 4822 | else if (level == 2) { |
| 4823 | if (spte & (1ULL << 7)) |
| 4824 | /* 2MB ref, bits 20:12 reserved */ |
| 4825 | mask |= 0x1ff000; |
| 4826 | else |
| 4827 | /* bits 6:3 reserved */ |
| 4828 | mask |= 0x78; |
| 4829 | } |
| 4830 | |
| 4831 | return mask; |
| 4832 | } |
| 4833 | |
| 4834 | static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte, |
| 4835 | int level) |
| 4836 | { |
| 4837 | printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level); |
| 4838 | |
| 4839 | /* 010b (write-only) */ |
| 4840 | WARN_ON((spte & 0x7) == 0x2); |
| 4841 | |
| 4842 | /* 110b (write/execute) */ |
| 4843 | WARN_ON((spte & 0x7) == 0x6); |
| 4844 | |
| 4845 | /* 100b (execute-only) and value not supported by logical processor */ |
| 4846 | if (!cpu_has_vmx_ept_execute_only()) |
| 4847 | WARN_ON((spte & 0x7) == 0x4); |
| 4848 | |
| 4849 | /* not 000b */ |
| 4850 | if ((spte & 0x7)) { |
| 4851 | u64 rsvd_bits = spte & ept_rsvd_mask(spte, level); |
| 4852 | |
| 4853 | if (rsvd_bits != 0) { |
| 4854 | printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n", |
| 4855 | __func__, rsvd_bits); |
| 4856 | WARN_ON(1); |
| 4857 | } |
| 4858 | |
| 4859 | if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) { |
| 4860 | u64 ept_mem_type = (spte & 0x38) >> 3; |
| 4861 | |
| 4862 | if (ept_mem_type == 2 || ept_mem_type == 3 || |
| 4863 | ept_mem_type == 7) { |
| 4864 | printk(KERN_ERR "%s: ept_mem_type=0x%llx\n", |
| 4865 | __func__, ept_mem_type); |
| 4866 | WARN_ON(1); |
| 4867 | } |
| 4868 | } |
| 4869 | } |
| 4870 | } |
| 4871 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4872 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4873 | { |
| 4874 | u64 sptes[4]; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 4875 | int nr_sptes, i, ret; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4876 | gpa_t gpa; |
| 4877 | |
| 4878 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 4879 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 4880 | ret = handle_mmio_page_fault_common(vcpu, gpa, true); |
| 4881 | if (likely(ret == 1)) |
| 4882 | return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) == |
| 4883 | EMULATE_DONE; |
| 4884 | if (unlikely(!ret)) |
| 4885 | return 1; |
| 4886 | |
| 4887 | /* It is the real ept misconfig */ |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4888 | printk(KERN_ERR "EPT: Misconfiguration.\n"); |
| 4889 | printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa); |
| 4890 | |
| 4891 | nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes); |
| 4892 | |
| 4893 | for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i) |
| 4894 | ept_misconfig_inspect_spte(vcpu, sptes[i-1], i); |
| 4895 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4896 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 4897 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4898 | |
| 4899 | return 0; |
| 4900 | } |
| 4901 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4902 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4903 | { |
| 4904 | u32 cpu_based_vm_exec_control; |
| 4905 | |
| 4906 | /* clear pending NMI */ |
| 4907 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4908 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 4909 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 4910 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4911 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4912 | |
| 4913 | return 1; |
| 4914 | } |
| 4915 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4916 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4917 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 4918 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4919 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4920 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 4921 | u32 cpu_exec_ctrl; |
| 4922 | bool intr_window_requested; |
| 4923 | |
| 4924 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4925 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4926 | |
| 4927 | while (!guest_state_valid(vcpu)) { |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 4928 | if (intr_window_requested |
| 4929 | && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF)) |
| 4930 | return handle_interrupt_window(&vmx->vcpu); |
| 4931 | |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4932 | err = emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4933 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4934 | if (err == EMULATE_DO_MMIO) { |
| 4935 | ret = 0; |
| 4936 | goto out; |
| 4937 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 4938 | |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 4939 | if (err != EMULATE_DONE) |
| 4940 | return 0; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4941 | |
| 4942 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4943 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4944 | if (need_resched()) |
| 4945 | schedule(); |
| 4946 | } |
| 4947 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4948 | vmx->emulation_required = 0; |
| 4949 | out: |
| 4950 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4951 | } |
| 4952 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4953 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4954 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 4955 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 4956 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 4957 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4958 | { |
| 4959 | skip_emulated_instruction(vcpu); |
| 4960 | kvm_vcpu_on_spin(vcpu); |
| 4961 | |
| 4962 | return 1; |
| 4963 | } |
| 4964 | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 4965 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 4966 | { |
| 4967 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4968 | return 1; |
| 4969 | } |
| 4970 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4971 | /* |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 4972 | * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12. |
| 4973 | * We could reuse a single VMCS for all the L2 guests, but we also want the |
| 4974 | * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this |
| 4975 | * allows keeping them loaded on the processor, and in the future will allow |
| 4976 | * optimizations where prepare_vmcs02 doesn't need to set all the fields on |
| 4977 | * every entry if they never change. |
| 4978 | * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE |
| 4979 | * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first. |
| 4980 | * |
| 4981 | * The following functions allocate and free a vmcs02 in this pool. |
| 4982 | */ |
| 4983 | |
| 4984 | /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */ |
| 4985 | static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx) |
| 4986 | { |
| 4987 | struct vmcs02_list *item; |
| 4988 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 4989 | if (item->vmptr == vmx->nested.current_vmptr) { |
| 4990 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 4991 | return &item->vmcs02; |
| 4992 | } |
| 4993 | |
| 4994 | if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) { |
| 4995 | /* Recycle the least recently used VMCS. */ |
| 4996 | item = list_entry(vmx->nested.vmcs02_pool.prev, |
| 4997 | struct vmcs02_list, list); |
| 4998 | item->vmptr = vmx->nested.current_vmptr; |
| 4999 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 5000 | return &item->vmcs02; |
| 5001 | } |
| 5002 | |
| 5003 | /* Create a new VMCS */ |
| 5004 | item = (struct vmcs02_list *) |
| 5005 | kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL); |
| 5006 | if (!item) |
| 5007 | return NULL; |
| 5008 | item->vmcs02.vmcs = alloc_vmcs(); |
| 5009 | if (!item->vmcs02.vmcs) { |
| 5010 | kfree(item); |
| 5011 | return NULL; |
| 5012 | } |
| 5013 | loaded_vmcs_init(&item->vmcs02); |
| 5014 | item->vmptr = vmx->nested.current_vmptr; |
| 5015 | list_add(&(item->list), &(vmx->nested.vmcs02_pool)); |
| 5016 | vmx->nested.vmcs02_num++; |
| 5017 | return &item->vmcs02; |
| 5018 | } |
| 5019 | |
| 5020 | /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */ |
| 5021 | static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 5022 | { |
| 5023 | struct vmcs02_list *item; |
| 5024 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 5025 | if (item->vmptr == vmptr) { |
| 5026 | free_loaded_vmcs(&item->vmcs02); |
| 5027 | list_del(&item->list); |
| 5028 | kfree(item); |
| 5029 | vmx->nested.vmcs02_num--; |
| 5030 | return; |
| 5031 | } |
| 5032 | } |
| 5033 | |
| 5034 | /* |
| 5035 | * Free all VMCSs saved for this vcpu, except the one pointed by |
| 5036 | * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one |
| 5037 | * currently used, if running L2), and vmcs01 when running L2. |
| 5038 | */ |
| 5039 | static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx) |
| 5040 | { |
| 5041 | struct vmcs02_list *item, *n; |
| 5042 | list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) { |
| 5043 | if (vmx->loaded_vmcs != &item->vmcs02) |
| 5044 | free_loaded_vmcs(&item->vmcs02); |
| 5045 | list_del(&item->list); |
| 5046 | kfree(item); |
| 5047 | } |
| 5048 | vmx->nested.vmcs02_num = 0; |
| 5049 | |
| 5050 | if (vmx->loaded_vmcs != &vmx->vmcs01) |
| 5051 | free_loaded_vmcs(&vmx->vmcs01); |
| 5052 | } |
| 5053 | |
| 5054 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 5055 | * Emulate the VMXON instruction. |
| 5056 | * Currently, we just remember that VMX is active, and do not save or even |
| 5057 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 5058 | * do not currently need to store anything in that guest-allocated memory |
| 5059 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 5060 | * argument is different from the VMXON pointer (which the spec says they do). |
| 5061 | */ |
| 5062 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 5063 | { |
| 5064 | struct kvm_segment cs; |
| 5065 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5066 | |
| 5067 | /* The Intel VMX Instruction Reference lists a bunch of bits that |
| 5068 | * are prerequisite to running VMXON, most notably cr4.VMXE must be |
| 5069 | * set to 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 5070 | * Otherwise, we should fail with #UD. We test these now: |
| 5071 | */ |
| 5072 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) || |
| 5073 | !kvm_read_cr0_bits(vcpu, X86_CR0_PE) || |
| 5074 | (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
| 5075 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5076 | return 1; |
| 5077 | } |
| 5078 | |
| 5079 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 5080 | if (is_long_mode(vcpu) && !cs.l) { |
| 5081 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5082 | return 1; |
| 5083 | } |
| 5084 | |
| 5085 | if (vmx_get_cpl(vcpu)) { |
| 5086 | kvm_inject_gp(vcpu, 0); |
| 5087 | return 1; |
| 5088 | } |
| 5089 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 5090 | INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool)); |
| 5091 | vmx->nested.vmcs02_num = 0; |
| 5092 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 5093 | vmx->nested.vmxon = true; |
| 5094 | |
| 5095 | skip_emulated_instruction(vcpu); |
| 5096 | return 1; |
| 5097 | } |
| 5098 | |
| 5099 | /* |
| 5100 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 5101 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 5102 | * slightly different). It also specifies what exception to inject otherwise. |
| 5103 | */ |
| 5104 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 5105 | { |
| 5106 | struct kvm_segment cs; |
| 5107 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5108 | |
| 5109 | if (!vmx->nested.vmxon) { |
| 5110 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5111 | return 0; |
| 5112 | } |
| 5113 | |
| 5114 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 5115 | if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) || |
| 5116 | (is_long_mode(vcpu) && !cs.l)) { |
| 5117 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5118 | return 0; |
| 5119 | } |
| 5120 | |
| 5121 | if (vmx_get_cpl(vcpu)) { |
| 5122 | kvm_inject_gp(vcpu, 0); |
| 5123 | return 0; |
| 5124 | } |
| 5125 | |
| 5126 | return 1; |
| 5127 | } |
| 5128 | |
| 5129 | /* |
| 5130 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 5131 | * just stops using VMX. |
| 5132 | */ |
| 5133 | static void free_nested(struct vcpu_vmx *vmx) |
| 5134 | { |
| 5135 | if (!vmx->nested.vmxon) |
| 5136 | return; |
| 5137 | vmx->nested.vmxon = false; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 5138 | if (vmx->nested.current_vmptr != -1ull) { |
| 5139 | kunmap(vmx->nested.current_vmcs12_page); |
| 5140 | nested_release_page(vmx->nested.current_vmcs12_page); |
| 5141 | vmx->nested.current_vmptr = -1ull; |
| 5142 | vmx->nested.current_vmcs12 = NULL; |
| 5143 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 5144 | /* Unpin physical memory we referred to in current vmcs02 */ |
| 5145 | if (vmx->nested.apic_access_page) { |
| 5146 | nested_release_page(vmx->nested.apic_access_page); |
| 5147 | vmx->nested.apic_access_page = 0; |
| 5148 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 5149 | |
| 5150 | nested_free_all_saved_vmcss(vmx); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | /* Emulate the VMXOFF instruction */ |
| 5154 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 5155 | { |
| 5156 | if (!nested_vmx_check_permission(vcpu)) |
| 5157 | return 1; |
| 5158 | free_nested(to_vmx(vcpu)); |
| 5159 | skip_emulated_instruction(vcpu); |
| 5160 | return 1; |
| 5161 | } |
| 5162 | |
| 5163 | /* |
Nadav Har'El | 064aea7 | 2011-05-25 23:04:56 +0300 | [diff] [blame] | 5164 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 5165 | * exit caused by such an instruction (run by a guest hypervisor). |
| 5166 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 5167 | * #UD or #GP. |
| 5168 | */ |
| 5169 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 5170 | unsigned long exit_qualification, |
| 5171 | u32 vmx_instruction_info, gva_t *ret) |
| 5172 | { |
| 5173 | /* |
| 5174 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 5175 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 5176 | * addressing components of the operand. Only the displacement part |
| 5177 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 5178 | * For how an actual address is calculated from all these components, |
| 5179 | * refer to Vol. 1, "Operand Addressing". |
| 5180 | */ |
| 5181 | int scaling = vmx_instruction_info & 3; |
| 5182 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 5183 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 5184 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 5185 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 5186 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 5187 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 5188 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 5189 | |
| 5190 | if (is_reg) { |
| 5191 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5192 | return 1; |
| 5193 | } |
| 5194 | |
| 5195 | /* Addr = segment_base + offset */ |
| 5196 | /* offset = base + [index * scale] + displacement */ |
| 5197 | *ret = vmx_get_segment_base(vcpu, seg_reg); |
| 5198 | if (base_is_valid) |
| 5199 | *ret += kvm_register_read(vcpu, base_reg); |
| 5200 | if (index_is_valid) |
| 5201 | *ret += kvm_register_read(vcpu, index_reg)<<scaling; |
| 5202 | *ret += exit_qualification; /* holds the displacement */ |
| 5203 | |
| 5204 | if (addr_size == 1) /* 32 bit */ |
| 5205 | *ret &= 0xffffffff; |
| 5206 | |
| 5207 | /* |
| 5208 | * TODO: throw #GP (and return 1) in various cases that the VM* |
| 5209 | * instructions require it - e.g., offset beyond segment limit, |
| 5210 | * unusable or unreadable/unwritable segment, non-canonical 64-bit |
| 5211 | * address, and so on. Currently these are not checked. |
| 5212 | */ |
| 5213 | return 0; |
| 5214 | } |
| 5215 | |
| 5216 | /* |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 5217 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 5218 | * set the success or error code of an emulated VMX instruction, as specified |
| 5219 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 5220 | */ |
| 5221 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 5222 | { |
| 5223 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 5224 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 5225 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 5226 | } |
| 5227 | |
| 5228 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 5229 | { |
| 5230 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 5231 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 5232 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 5233 | | X86_EFLAGS_CF); |
| 5234 | } |
| 5235 | |
| 5236 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
| 5237 | u32 vm_instruction_error) |
| 5238 | { |
| 5239 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 5240 | /* |
| 5241 | * failValid writes the error number to the current VMCS, which |
| 5242 | * can't be done there isn't a current VMCS. |
| 5243 | */ |
| 5244 | nested_vmx_failInvalid(vcpu); |
| 5245 | return; |
| 5246 | } |
| 5247 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 5248 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 5249 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 5250 | | X86_EFLAGS_ZF); |
| 5251 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 5252 | } |
| 5253 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 5254 | /* Emulate the VMCLEAR instruction */ |
| 5255 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 5256 | { |
| 5257 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5258 | gva_t gva; |
| 5259 | gpa_t vmptr; |
| 5260 | struct vmcs12 *vmcs12; |
| 5261 | struct page *page; |
| 5262 | struct x86_exception e; |
| 5263 | |
| 5264 | if (!nested_vmx_check_permission(vcpu)) |
| 5265 | return 1; |
| 5266 | |
| 5267 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 5268 | vmcs_read32(VMX_INSTRUCTION_INFO), &gva)) |
| 5269 | return 1; |
| 5270 | |
| 5271 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr, |
| 5272 | sizeof(vmptr), &e)) { |
| 5273 | kvm_inject_page_fault(vcpu, &e); |
| 5274 | return 1; |
| 5275 | } |
| 5276 | |
| 5277 | if (!IS_ALIGNED(vmptr, PAGE_SIZE)) { |
| 5278 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 5279 | skip_emulated_instruction(vcpu); |
| 5280 | return 1; |
| 5281 | } |
| 5282 | |
| 5283 | if (vmptr == vmx->nested.current_vmptr) { |
| 5284 | kunmap(vmx->nested.current_vmcs12_page); |
| 5285 | nested_release_page(vmx->nested.current_vmcs12_page); |
| 5286 | vmx->nested.current_vmptr = -1ull; |
| 5287 | vmx->nested.current_vmcs12 = NULL; |
| 5288 | } |
| 5289 | |
| 5290 | page = nested_get_page(vcpu, vmptr); |
| 5291 | if (page == NULL) { |
| 5292 | /* |
| 5293 | * For accurate processor emulation, VMCLEAR beyond available |
| 5294 | * physical memory should do nothing at all. However, it is |
| 5295 | * possible that a nested vmx bug, not a guest hypervisor bug, |
| 5296 | * resulted in this case, so let's shut down before doing any |
| 5297 | * more damage: |
| 5298 | */ |
| 5299 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 5300 | return 1; |
| 5301 | } |
| 5302 | vmcs12 = kmap(page); |
| 5303 | vmcs12->launch_state = 0; |
| 5304 | kunmap(page); |
| 5305 | nested_release_page(page); |
| 5306 | |
| 5307 | nested_free_vmcs02(vmx, vmptr); |
| 5308 | |
| 5309 | skip_emulated_instruction(vcpu); |
| 5310 | nested_vmx_succeed(vcpu); |
| 5311 | return 1; |
| 5312 | } |
| 5313 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 5314 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 5315 | |
| 5316 | /* Emulate the VMLAUNCH instruction */ |
| 5317 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 5318 | { |
| 5319 | return nested_vmx_run(vcpu, true); |
| 5320 | } |
| 5321 | |
| 5322 | /* Emulate the VMRESUME instruction */ |
| 5323 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 5324 | { |
| 5325 | |
| 5326 | return nested_vmx_run(vcpu, false); |
| 5327 | } |
| 5328 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 5329 | enum vmcs_field_type { |
| 5330 | VMCS_FIELD_TYPE_U16 = 0, |
| 5331 | VMCS_FIELD_TYPE_U64 = 1, |
| 5332 | VMCS_FIELD_TYPE_U32 = 2, |
| 5333 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 |
| 5334 | }; |
| 5335 | |
| 5336 | static inline int vmcs_field_type(unsigned long field) |
| 5337 | { |
| 5338 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
| 5339 | return VMCS_FIELD_TYPE_U32; |
| 5340 | return (field >> 13) & 0x3 ; |
| 5341 | } |
| 5342 | |
| 5343 | static inline int vmcs_field_readonly(unsigned long field) |
| 5344 | { |
| 5345 | return (((field >> 10) & 0x3) == 1); |
| 5346 | } |
| 5347 | |
| 5348 | /* |
| 5349 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 5350 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 5351 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 5352 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 5353 | * 64-bit fields are to be returned). |
| 5354 | */ |
| 5355 | static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu, |
| 5356 | unsigned long field, u64 *ret) |
| 5357 | { |
| 5358 | short offset = vmcs_field_to_offset(field); |
| 5359 | char *p; |
| 5360 | |
| 5361 | if (offset < 0) |
| 5362 | return 0; |
| 5363 | |
| 5364 | p = ((char *)(get_vmcs12(vcpu))) + offset; |
| 5365 | |
| 5366 | switch (vmcs_field_type(field)) { |
| 5367 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 5368 | *ret = *((natural_width *)p); |
| 5369 | return 1; |
| 5370 | case VMCS_FIELD_TYPE_U16: |
| 5371 | *ret = *((u16 *)p); |
| 5372 | return 1; |
| 5373 | case VMCS_FIELD_TYPE_U32: |
| 5374 | *ret = *((u32 *)p); |
| 5375 | return 1; |
| 5376 | case VMCS_FIELD_TYPE_U64: |
| 5377 | *ret = *((u64 *)p); |
| 5378 | return 1; |
| 5379 | default: |
| 5380 | return 0; /* can never happen. */ |
| 5381 | } |
| 5382 | } |
| 5383 | |
| 5384 | /* |
| 5385 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 5386 | * used before) all generate the same failure when it is missing. |
| 5387 | */ |
| 5388 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 5389 | { |
| 5390 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5391 | if (vmx->nested.current_vmptr == -1ull) { |
| 5392 | nested_vmx_failInvalid(vcpu); |
| 5393 | skip_emulated_instruction(vcpu); |
| 5394 | return 0; |
| 5395 | } |
| 5396 | return 1; |
| 5397 | } |
| 5398 | |
| 5399 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 5400 | { |
| 5401 | unsigned long field; |
| 5402 | u64 field_value; |
| 5403 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5404 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5405 | gva_t gva = 0; |
| 5406 | |
| 5407 | if (!nested_vmx_check_permission(vcpu) || |
| 5408 | !nested_vmx_check_vmcs12(vcpu)) |
| 5409 | return 1; |
| 5410 | |
| 5411 | /* Decode instruction info and find the field to read */ |
| 5412 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5413 | /* Read the field, zero-extended to a u64 field_value */ |
| 5414 | if (!vmcs12_read_any(vcpu, field, &field_value)) { |
| 5415 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 5416 | skip_emulated_instruction(vcpu); |
| 5417 | return 1; |
| 5418 | } |
| 5419 | /* |
| 5420 | * Now copy part of this value to register or memory, as requested. |
| 5421 | * Note that the number of bits actually copied is 32 or 64 depending |
| 5422 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 5423 | */ |
| 5424 | if (vmx_instruction_info & (1u << 10)) { |
| 5425 | kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
| 5426 | field_value); |
| 5427 | } else { |
| 5428 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 5429 | vmx_instruction_info, &gva)) |
| 5430 | return 1; |
| 5431 | /* _system ok, as nested_vmx_check_permission verified cpl=0 */ |
| 5432 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
| 5433 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); |
| 5434 | } |
| 5435 | |
| 5436 | nested_vmx_succeed(vcpu); |
| 5437 | skip_emulated_instruction(vcpu); |
| 5438 | return 1; |
| 5439 | } |
| 5440 | |
| 5441 | |
| 5442 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 5443 | { |
| 5444 | unsigned long field; |
| 5445 | gva_t gva; |
| 5446 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5447 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5448 | char *p; |
| 5449 | short offset; |
| 5450 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 5451 | * mode, and eventually we need to write that into a field of several |
| 5452 | * possible lengths. The code below first zero-extends the value to 64 |
| 5453 | * bit (field_value), and then copies only the approriate number of |
| 5454 | * bits into the vmcs12 field. |
| 5455 | */ |
| 5456 | u64 field_value = 0; |
| 5457 | struct x86_exception e; |
| 5458 | |
| 5459 | if (!nested_vmx_check_permission(vcpu) || |
| 5460 | !nested_vmx_check_vmcs12(vcpu)) |
| 5461 | return 1; |
| 5462 | |
| 5463 | if (vmx_instruction_info & (1u << 10)) |
| 5464 | field_value = kvm_register_read(vcpu, |
| 5465 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 5466 | else { |
| 5467 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 5468 | vmx_instruction_info, &gva)) |
| 5469 | return 1; |
| 5470 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, |
| 5471 | &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) { |
| 5472 | kvm_inject_page_fault(vcpu, &e); |
| 5473 | return 1; |
| 5474 | } |
| 5475 | } |
| 5476 | |
| 5477 | |
| 5478 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5479 | if (vmcs_field_readonly(field)) { |
| 5480 | nested_vmx_failValid(vcpu, |
| 5481 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
| 5482 | skip_emulated_instruction(vcpu); |
| 5483 | return 1; |
| 5484 | } |
| 5485 | |
| 5486 | offset = vmcs_field_to_offset(field); |
| 5487 | if (offset < 0) { |
| 5488 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 5489 | skip_emulated_instruction(vcpu); |
| 5490 | return 1; |
| 5491 | } |
| 5492 | p = ((char *) get_vmcs12(vcpu)) + offset; |
| 5493 | |
| 5494 | switch (vmcs_field_type(field)) { |
| 5495 | case VMCS_FIELD_TYPE_U16: |
| 5496 | *(u16 *)p = field_value; |
| 5497 | break; |
| 5498 | case VMCS_FIELD_TYPE_U32: |
| 5499 | *(u32 *)p = field_value; |
| 5500 | break; |
| 5501 | case VMCS_FIELD_TYPE_U64: |
| 5502 | *(u64 *)p = field_value; |
| 5503 | break; |
| 5504 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 5505 | *(natural_width *)p = field_value; |
| 5506 | break; |
| 5507 | default: |
| 5508 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 5509 | skip_emulated_instruction(vcpu); |
| 5510 | return 1; |
| 5511 | } |
| 5512 | |
| 5513 | nested_vmx_succeed(vcpu); |
| 5514 | skip_emulated_instruction(vcpu); |
| 5515 | return 1; |
| 5516 | } |
| 5517 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 5518 | /* Emulate the VMPTRLD instruction */ |
| 5519 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 5520 | { |
| 5521 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5522 | gva_t gva; |
| 5523 | gpa_t vmptr; |
| 5524 | struct x86_exception e; |
| 5525 | |
| 5526 | if (!nested_vmx_check_permission(vcpu)) |
| 5527 | return 1; |
| 5528 | |
| 5529 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 5530 | vmcs_read32(VMX_INSTRUCTION_INFO), &gva)) |
| 5531 | return 1; |
| 5532 | |
| 5533 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr, |
| 5534 | sizeof(vmptr), &e)) { |
| 5535 | kvm_inject_page_fault(vcpu, &e); |
| 5536 | return 1; |
| 5537 | } |
| 5538 | |
| 5539 | if (!IS_ALIGNED(vmptr, PAGE_SIZE)) { |
| 5540 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 5541 | skip_emulated_instruction(vcpu); |
| 5542 | return 1; |
| 5543 | } |
| 5544 | |
| 5545 | if (vmx->nested.current_vmptr != vmptr) { |
| 5546 | struct vmcs12 *new_vmcs12; |
| 5547 | struct page *page; |
| 5548 | page = nested_get_page(vcpu, vmptr); |
| 5549 | if (page == NULL) { |
| 5550 | nested_vmx_failInvalid(vcpu); |
| 5551 | skip_emulated_instruction(vcpu); |
| 5552 | return 1; |
| 5553 | } |
| 5554 | new_vmcs12 = kmap(page); |
| 5555 | if (new_vmcs12->revision_id != VMCS12_REVISION) { |
| 5556 | kunmap(page); |
| 5557 | nested_release_page_clean(page); |
| 5558 | nested_vmx_failValid(vcpu, |
| 5559 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5560 | skip_emulated_instruction(vcpu); |
| 5561 | return 1; |
| 5562 | } |
| 5563 | if (vmx->nested.current_vmptr != -1ull) { |
| 5564 | kunmap(vmx->nested.current_vmcs12_page); |
| 5565 | nested_release_page(vmx->nested.current_vmcs12_page); |
| 5566 | } |
| 5567 | |
| 5568 | vmx->nested.current_vmptr = vmptr; |
| 5569 | vmx->nested.current_vmcs12 = new_vmcs12; |
| 5570 | vmx->nested.current_vmcs12_page = page; |
| 5571 | } |
| 5572 | |
| 5573 | nested_vmx_succeed(vcpu); |
| 5574 | skip_emulated_instruction(vcpu); |
| 5575 | return 1; |
| 5576 | } |
| 5577 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 5578 | /* Emulate the VMPTRST instruction */ |
| 5579 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 5580 | { |
| 5581 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5582 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5583 | gva_t vmcs_gva; |
| 5584 | struct x86_exception e; |
| 5585 | |
| 5586 | if (!nested_vmx_check_permission(vcpu)) |
| 5587 | return 1; |
| 5588 | |
| 5589 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 5590 | vmx_instruction_info, &vmcs_gva)) |
| 5591 | return 1; |
| 5592 | /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */ |
| 5593 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
| 5594 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 5595 | sizeof(u64), &e)) { |
| 5596 | kvm_inject_page_fault(vcpu, &e); |
| 5597 | return 1; |
| 5598 | } |
| 5599 | nested_vmx_succeed(vcpu); |
| 5600 | skip_emulated_instruction(vcpu); |
| 5601 | return 1; |
| 5602 | } |
| 5603 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 5604 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5605 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 5606 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 5607 | * to be done to userspace and return 0. |
| 5608 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5609 | static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5610 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 5611 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5612 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5613 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5614 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5615 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 5616 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 5617 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 5618 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 5619 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 5620 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 5621 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 5622 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5623 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 5624 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5625 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 5626 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 5627 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 5628 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 5629 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 5630 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 5631 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 5632 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 5633 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 5634 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5635 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 5636 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 5637 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 5638 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5639 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5640 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5641 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 5642 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5643 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 5644 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op, |
| 5645 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5646 | }; |
| 5647 | |
| 5648 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 5649 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5650 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 5651 | /* |
| 5652 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 5653 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 5654 | * disinterest in the current event (read or write a specific MSR) by using an |
| 5655 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 5656 | */ |
| 5657 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 5658 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 5659 | { |
| 5660 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 5661 | gpa_t bitmap; |
| 5662 | |
| 5663 | if (!nested_cpu_has(get_vmcs12(vcpu), CPU_BASED_USE_MSR_BITMAPS)) |
| 5664 | return 1; |
| 5665 | |
| 5666 | /* |
| 5667 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 5668 | * for the four combinations of read/write and low/high MSR numbers. |
| 5669 | * First we need to figure out which of the four to use: |
| 5670 | */ |
| 5671 | bitmap = vmcs12->msr_bitmap; |
| 5672 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 5673 | bitmap += 2048; |
| 5674 | if (msr_index >= 0xc0000000) { |
| 5675 | msr_index -= 0xc0000000; |
| 5676 | bitmap += 1024; |
| 5677 | } |
| 5678 | |
| 5679 | /* Then read the msr_index'th bit from this bitmap: */ |
| 5680 | if (msr_index < 1024*8) { |
| 5681 | unsigned char b; |
| 5682 | kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1); |
| 5683 | return 1 & (b >> (msr_index & 7)); |
| 5684 | } else |
| 5685 | return 1; /* let L1 handle the wrong parameter */ |
| 5686 | } |
| 5687 | |
| 5688 | /* |
| 5689 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 5690 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 5691 | * intercept (via guest_host_mask etc.) the current event. |
| 5692 | */ |
| 5693 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 5694 | struct vmcs12 *vmcs12) |
| 5695 | { |
| 5696 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5697 | int cr = exit_qualification & 15; |
| 5698 | int reg = (exit_qualification >> 8) & 15; |
| 5699 | unsigned long val = kvm_register_read(vcpu, reg); |
| 5700 | |
| 5701 | switch ((exit_qualification >> 4) & 3) { |
| 5702 | case 0: /* mov to cr */ |
| 5703 | switch (cr) { |
| 5704 | case 0: |
| 5705 | if (vmcs12->cr0_guest_host_mask & |
| 5706 | (val ^ vmcs12->cr0_read_shadow)) |
| 5707 | return 1; |
| 5708 | break; |
| 5709 | case 3: |
| 5710 | if ((vmcs12->cr3_target_count >= 1 && |
| 5711 | vmcs12->cr3_target_value0 == val) || |
| 5712 | (vmcs12->cr3_target_count >= 2 && |
| 5713 | vmcs12->cr3_target_value1 == val) || |
| 5714 | (vmcs12->cr3_target_count >= 3 && |
| 5715 | vmcs12->cr3_target_value2 == val) || |
| 5716 | (vmcs12->cr3_target_count >= 4 && |
| 5717 | vmcs12->cr3_target_value3 == val)) |
| 5718 | return 0; |
| 5719 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
| 5720 | return 1; |
| 5721 | break; |
| 5722 | case 4: |
| 5723 | if (vmcs12->cr4_guest_host_mask & |
| 5724 | (vmcs12->cr4_read_shadow ^ val)) |
| 5725 | return 1; |
| 5726 | break; |
| 5727 | case 8: |
| 5728 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
| 5729 | return 1; |
| 5730 | break; |
| 5731 | } |
| 5732 | break; |
| 5733 | case 2: /* clts */ |
| 5734 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 5735 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
| 5736 | return 1; |
| 5737 | break; |
| 5738 | case 1: /* mov from cr */ |
| 5739 | switch (cr) { |
| 5740 | case 3: |
| 5741 | if (vmcs12->cpu_based_vm_exec_control & |
| 5742 | CPU_BASED_CR3_STORE_EXITING) |
| 5743 | return 1; |
| 5744 | break; |
| 5745 | case 8: |
| 5746 | if (vmcs12->cpu_based_vm_exec_control & |
| 5747 | CPU_BASED_CR8_STORE_EXITING) |
| 5748 | return 1; |
| 5749 | break; |
| 5750 | } |
| 5751 | break; |
| 5752 | case 3: /* lmsw */ |
| 5753 | /* |
| 5754 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 5755 | * cr0. Other attempted changes are ignored, with no exit. |
| 5756 | */ |
| 5757 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 5758 | (val ^ vmcs12->cr0_read_shadow)) |
| 5759 | return 1; |
| 5760 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 5761 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 5762 | (val & 0x1)) |
| 5763 | return 1; |
| 5764 | break; |
| 5765 | } |
| 5766 | return 0; |
| 5767 | } |
| 5768 | |
| 5769 | /* |
| 5770 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 5771 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 5772 | * when in is_guest_mode (L2). |
| 5773 | */ |
| 5774 | static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu) |
| 5775 | { |
| 5776 | u32 exit_reason = vmcs_read32(VM_EXIT_REASON); |
| 5777 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 5778 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5779 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5780 | |
| 5781 | if (vmx->nested.nested_run_pending) |
| 5782 | return 0; |
| 5783 | |
| 5784 | if (unlikely(vmx->fail)) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 5785 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 5786 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 5787 | return 1; |
| 5788 | } |
| 5789 | |
| 5790 | switch (exit_reason) { |
| 5791 | case EXIT_REASON_EXCEPTION_NMI: |
| 5792 | if (!is_exception(intr_info)) |
| 5793 | return 0; |
| 5794 | else if (is_page_fault(intr_info)) |
| 5795 | return enable_ept; |
| 5796 | return vmcs12->exception_bitmap & |
| 5797 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 5798 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
| 5799 | return 0; |
| 5800 | case EXIT_REASON_TRIPLE_FAULT: |
| 5801 | return 1; |
| 5802 | case EXIT_REASON_PENDING_INTERRUPT: |
| 5803 | case EXIT_REASON_NMI_WINDOW: |
| 5804 | /* |
| 5805 | * prepare_vmcs02() set the CPU_BASED_VIRTUAL_INTR_PENDING bit |
| 5806 | * (aka Interrupt Window Exiting) only when L1 turned it on, |
| 5807 | * so if we got a PENDING_INTERRUPT exit, this must be for L1. |
| 5808 | * Same for NMI Window Exiting. |
| 5809 | */ |
| 5810 | return 1; |
| 5811 | case EXIT_REASON_TASK_SWITCH: |
| 5812 | return 1; |
| 5813 | case EXIT_REASON_CPUID: |
| 5814 | return 1; |
| 5815 | case EXIT_REASON_HLT: |
| 5816 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 5817 | case EXIT_REASON_INVD: |
| 5818 | return 1; |
| 5819 | case EXIT_REASON_INVLPG: |
| 5820 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5821 | case EXIT_REASON_RDPMC: |
| 5822 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
| 5823 | case EXIT_REASON_RDTSC: |
| 5824 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 5825 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 5826 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 5827 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: |
| 5828 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: |
| 5829 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
| 5830 | /* |
| 5831 | * VMX instructions trap unconditionally. This allows L1 to |
| 5832 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 5833 | */ |
| 5834 | return 1; |
| 5835 | case EXIT_REASON_CR_ACCESS: |
| 5836 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 5837 | case EXIT_REASON_DR_ACCESS: |
| 5838 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 5839 | case EXIT_REASON_IO_INSTRUCTION: |
| 5840 | /* TODO: support IO bitmaps */ |
| 5841 | return 1; |
| 5842 | case EXIT_REASON_MSR_READ: |
| 5843 | case EXIT_REASON_MSR_WRITE: |
| 5844 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 5845 | case EXIT_REASON_INVALID_STATE: |
| 5846 | return 1; |
| 5847 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 5848 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
| 5849 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 5850 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 5851 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 5852 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 5853 | nested_cpu_has2(vmcs12, |
| 5854 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 5855 | case EXIT_REASON_MCE_DURING_VMENTRY: |
| 5856 | return 0; |
| 5857 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
| 5858 | return 1; |
| 5859 | case EXIT_REASON_APIC_ACCESS: |
| 5860 | return nested_cpu_has2(vmcs12, |
| 5861 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 5862 | case EXIT_REASON_EPT_VIOLATION: |
| 5863 | case EXIT_REASON_EPT_MISCONFIG: |
| 5864 | return 0; |
| 5865 | case EXIT_REASON_WBINVD: |
| 5866 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 5867 | case EXIT_REASON_XSETBV: |
| 5868 | return 1; |
| 5869 | default: |
| 5870 | return 1; |
| 5871 | } |
| 5872 | } |
| 5873 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 5874 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 5875 | { |
| 5876 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 5877 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 5878 | } |
| 5879 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5880 | /* |
| 5881 | * The guest has exited. See if we can fix it or if we need userspace |
| 5882 | * assistance. |
| 5883 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5884 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5885 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5886 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5887 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5888 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5889 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5890 | /* If guest state is invalid, start emulating */ |
| 5891 | if (vmx->emulation_required && emulate_invalid_guest_state) |
| 5892 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 5893 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 5894 | /* |
| 5895 | * the KVM_REQ_EVENT optimization bit is only on for one entry, and if |
| 5896 | * we did not inject a still-pending event to L1 now because of |
| 5897 | * nested_run_pending, we need to re-enable this bit. |
| 5898 | */ |
| 5899 | if (vmx->nested.nested_run_pending) |
| 5900 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 5901 | |
Nadav Har'El | 509c75e | 2011-06-02 11:54:52 +0300 | [diff] [blame] | 5902 | if (!is_guest_mode(vcpu) && (exit_reason == EXIT_REASON_VMLAUNCH || |
| 5903 | exit_reason == EXIT_REASON_VMRESUME)) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 5904 | vmx->nested.nested_run_pending = 1; |
| 5905 | else |
| 5906 | vmx->nested.nested_run_pending = 0; |
| 5907 | |
| 5908 | if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) { |
| 5909 | nested_vmx_vmexit(vcpu); |
| 5910 | return 1; |
| 5911 | } |
| 5912 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 5913 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
| 5914 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 5915 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 5916 | = exit_reason; |
| 5917 | return 0; |
| 5918 | } |
| 5919 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5920 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5921 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 5922 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5923 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 5924 | return 0; |
| 5925 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5926 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 5927 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5928 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 5929 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
| 5930 | exit_reason != EXIT_REASON_TASK_SWITCH)) |
| 5931 | printk(KERN_WARNING "%s: unexpected, valid vectoring info " |
| 5932 | "(0x%x) and exit reason is 0x%x\n", |
| 5933 | __func__, vectoring_info, exit_reason); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5934 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 5935 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked && |
| 5936 | !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis( |
| 5937 | get_vmcs12(vcpu), vcpu)))) { |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 5938 | if (vmx_interrupt_allowed(vcpu)) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5939 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5940 | } else if (vmx->vnmi_blocked_time > 1000000000LL && |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 5941 | vcpu->arch.nmi_pending) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5942 | /* |
| 5943 | * This CPU don't support us in finding the end of an |
| 5944 | * NMI-blocked window if the guest runs with IRQs |
| 5945 | * disabled. So we pull the trigger after 1 s of |
| 5946 | * futile waiting, but inform the user about this. |
| 5947 | */ |
| 5948 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 5949 | "state on VCPU %d after 1 s timeout\n", |
| 5950 | __func__, vcpu->vcpu_id); |
| 5951 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5952 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5953 | } |
| 5954 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5955 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 5956 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5957 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5958 | else { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5959 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 5960 | vcpu->run->hw.hardware_exit_reason = exit_reason; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5961 | } |
| 5962 | return 0; |
| 5963 | } |
| 5964 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5965 | 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] | 5966 | { |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5967 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5968 | vmcs_write32(TPR_THRESHOLD, 0); |
| 5969 | return; |
| 5970 | } |
| 5971 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5972 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5973 | } |
| 5974 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5975 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5976 | { |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5977 | u32 exit_intr_info; |
| 5978 | |
| 5979 | if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 5980 | || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
| 5981 | return; |
| 5982 | |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 5983 | vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5984 | exit_intr_info = vmx->exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5985 | |
| 5986 | /* Handle machine checks before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5987 | if (is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5988 | kvm_machine_check(); |
| 5989 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5990 | /* We need to handle NMIs before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5991 | 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] | 5992 | (exit_intr_info & INTR_INFO_VALID_MASK)) { |
| 5993 | kvm_before_handle_nmi(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5994 | asm("int $2"); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 5995 | kvm_after_handle_nmi(&vmx->vcpu); |
| 5996 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5997 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5998 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5999 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 6000 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 6001 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6002 | bool unblock_nmi; |
| 6003 | u8 vector; |
| 6004 | bool idtv_info_valid; |
| 6005 | |
| 6006 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 6007 | |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6008 | if (cpu_has_virtual_nmis()) { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 6009 | if (vmx->nmi_known_unmasked) |
| 6010 | return; |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 6011 | /* |
| 6012 | * Can't use vmx->exit_intr_info since we're not sure what |
| 6013 | * the exit reason is. |
| 6014 | */ |
| 6015 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6016 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 6017 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 6018 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 6019 | * SDM 3: 27.7.1.2 (September 2008) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6020 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 6021 | * a guest IRET fault. |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 6022 | * SDM 3: 23.2.2 (September 2008) |
| 6023 | * Bit 12 is undefined in any of the following cases: |
| 6024 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 6025 | * information field. |
| 6026 | * If the VM exit is due to a double fault. |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6027 | */ |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 6028 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 6029 | vector != DF_VECTOR && !idtv_info_valid) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6030 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6031 | GUEST_INTR_STATE_NMI); |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 6032 | else |
| 6033 | vmx->nmi_known_unmasked = |
| 6034 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 6035 | & GUEST_INTR_STATE_NMI); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6036 | } else if (unlikely(vmx->soft_vnmi_blocked)) |
| 6037 | vmx->vnmi_blocked_time += |
| 6038 | ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6039 | } |
| 6040 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6041 | static void __vmx_complete_interrupts(struct vcpu_vmx *vmx, |
| 6042 | u32 idt_vectoring_info, |
| 6043 | int instr_len_field, |
| 6044 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6045 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6046 | u8 vector; |
| 6047 | int type; |
| 6048 | bool idtv_info_valid; |
| 6049 | |
| 6050 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6051 | |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6052 | vmx->vcpu.arch.nmi_injected = false; |
| 6053 | kvm_clear_exception_queue(&vmx->vcpu); |
| 6054 | kvm_clear_interrupt_queue(&vmx->vcpu); |
| 6055 | |
| 6056 | if (!idtv_info_valid) |
| 6057 | return; |
| 6058 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6059 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 6060 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6061 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 6062 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6063 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6064 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6065 | case INTR_TYPE_NMI_INTR: |
| 6066 | vmx->vcpu.arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6067 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 6068 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6069 | * Clear bit "block by NMI" before VM entry if a NMI |
| 6070 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6071 | */ |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 6072 | vmx_set_nmi_mask(&vmx->vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6073 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6074 | case INTR_TYPE_SOFT_EXCEPTION: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6075 | vmx->vcpu.arch.event_exit_inst_len = |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6076 | vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6077 | /* fall through */ |
| 6078 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 6079 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6080 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6081 | kvm_queue_exception_e(&vmx->vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 6082 | } else |
| 6083 | kvm_queue_exception(&vmx->vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6084 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6085 | case INTR_TYPE_SOFT_INTR: |
| 6086 | vmx->vcpu.arch.event_exit_inst_len = |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6087 | vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6088 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6089 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6090 | kvm_queue_interrupt(&vmx->vcpu, vector, |
| 6091 | type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6092 | break; |
| 6093 | default: |
| 6094 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 6095 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6096 | } |
| 6097 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6098 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 6099 | { |
Nadav Har'El | 66c78ae | 2011-05-25 23:14:07 +0300 | [diff] [blame] | 6100 | if (is_guest_mode(&vmx->vcpu)) |
| 6101 | return; |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6102 | __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info, |
| 6103 | VM_EXIT_INSTRUCTION_LEN, |
| 6104 | IDT_VECTORING_ERROR_CODE); |
| 6105 | } |
| 6106 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 6107 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 6108 | { |
Nadav Har'El | 66c78ae | 2011-05-25 23:14:07 +0300 | [diff] [blame] | 6109 | if (is_guest_mode(vcpu)) |
| 6110 | return; |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 6111 | __vmx_complete_interrupts(to_vmx(vcpu), |
| 6112 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 6113 | VM_ENTRY_INSTRUCTION_LEN, |
| 6114 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 6115 | |
| 6116 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 6117 | } |
| 6118 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 6119 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 6120 | { |
| 6121 | int i, nr_msrs; |
| 6122 | struct perf_guest_switch_msr *msrs; |
| 6123 | |
| 6124 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 6125 | |
| 6126 | if (!msrs) |
| 6127 | return; |
| 6128 | |
| 6129 | for (i = 0; i < nr_msrs; i++) |
| 6130 | if (msrs[i].host == msrs[i].guest) |
| 6131 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 6132 | else |
| 6133 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
| 6134 | msrs[i].host); |
| 6135 | } |
| 6136 | |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6137 | #ifdef CONFIG_X86_64 |
| 6138 | #define R "r" |
| 6139 | #define Q "q" |
| 6140 | #else |
| 6141 | #define R "e" |
| 6142 | #define Q "l" |
| 6143 | #endif |
| 6144 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 6145 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6146 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6147 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6148 | |
Nadav Har'El | 66c78ae | 2011-05-25 23:14:07 +0300 | [diff] [blame] | 6149 | if (is_guest_mode(vcpu) && !vmx->nested.nested_run_pending) { |
| 6150 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6151 | if (vmcs12->idt_vectoring_info_field & |
| 6152 | VECTORING_INFO_VALID_MASK) { |
| 6153 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 6154 | vmcs12->idt_vectoring_info_field); |
| 6155 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 6156 | vmcs12->vm_exit_instruction_len); |
| 6157 | if (vmcs12->idt_vectoring_info_field & |
| 6158 | VECTORING_INFO_DELIVER_CODE_MASK) |
| 6159 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 6160 | vmcs12->idt_vectoring_error_code); |
| 6161 | } |
| 6162 | } |
| 6163 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6164 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
| 6165 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) |
| 6166 | vmx->entry_time = ktime_get(); |
| 6167 | |
| 6168 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 6169 | start emulation until we arrive back to a valid state */ |
| 6170 | if (vmx->emulation_required && emulate_invalid_guest_state) |
| 6171 | return; |
| 6172 | |
| 6173 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 6174 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 6175 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 6176 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 6177 | |
| 6178 | /* When single-stepping over STI and MOV SS, we must clear the |
| 6179 | * corresponding interruptibility bits in the guest state. Otherwise |
| 6180 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 6181 | * exceptions being set, but that's not correct for the guest debugging |
| 6182 | * case. */ |
| 6183 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 6184 | vmx_set_interrupt_shadow(vcpu, 0); |
| 6185 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 6186 | atomic_switch_perf_msrs(vmx); |
| 6187 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6188 | vmx->__launched = vmx->loaded_vmcs->launched; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6189 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6190 | /* Store host registers */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6191 | "push %%"R"dx; push %%"R"bp;" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 6192 | "push %%"R"cx \n\t" /* placeholder for guest rcx */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6193 | "push %%"R"cx \n\t" |
Avi Kivity | 313dbd49 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6194 | "cmp %%"R"sp, %c[host_rsp](%0) \n\t" |
| 6195 | "je 1f \n\t" |
| 6196 | "mov %%"R"sp, %c[host_rsp](%0) \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 6197 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd49 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6198 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 6199 | /* Reload cr2 if changed */ |
| 6200 | "mov %c[cr2](%0), %%"R"ax \n\t" |
| 6201 | "mov %%cr2, %%"R"dx \n\t" |
| 6202 | "cmp %%"R"ax, %%"R"dx \n\t" |
| 6203 | "je 2f \n\t" |
| 6204 | "mov %%"R"ax, %%cr2 \n\t" |
| 6205 | "2: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6206 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6207 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6208 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6209 | "mov %c[rax](%0), %%"R"ax \n\t" |
| 6210 | "mov %c[rbx](%0), %%"R"bx \n\t" |
| 6211 | "mov %c[rdx](%0), %%"R"dx \n\t" |
| 6212 | "mov %c[rsi](%0), %%"R"si \n\t" |
| 6213 | "mov %c[rdi](%0), %%"R"di \n\t" |
| 6214 | "mov %c[rbp](%0), %%"R"bp \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6215 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6216 | "mov %c[r8](%0), %%r8 \n\t" |
| 6217 | "mov %c[r9](%0), %%r9 \n\t" |
| 6218 | "mov %c[r10](%0), %%r10 \n\t" |
| 6219 | "mov %c[r11](%0), %%r11 \n\t" |
| 6220 | "mov %c[r12](%0), %%r12 \n\t" |
| 6221 | "mov %c[r13](%0), %%r13 \n\t" |
| 6222 | "mov %c[r14](%0), %%r14 \n\t" |
| 6223 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6224 | #endif |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6225 | "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */ |
| 6226 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6227 | /* Enter guest mode */ |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 6228 | "jne .Llaunched \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 6229 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 6230 | "jmp .Lkvm_vmx_return \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 6231 | ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 6232 | ".Lkvm_vmx_return: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6233 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 6234 | "mov %0, %c[wordsize](%%"R"sp) \n\t" |
| 6235 | "pop %0 \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6236 | "mov %%"R"ax, %c[rax](%0) \n\t" |
| 6237 | "mov %%"R"bx, %c[rbx](%0) \n\t" |
Avi Kivity | 1c696d0 | 2011-01-06 18:09:11 +0200 | [diff] [blame] | 6238 | "pop"Q" %c[rcx](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6239 | "mov %%"R"dx, %c[rdx](%0) \n\t" |
| 6240 | "mov %%"R"si, %c[rsi](%0) \n\t" |
| 6241 | "mov %%"R"di, %c[rdi](%0) \n\t" |
| 6242 | "mov %%"R"bp, %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6243 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6244 | "mov %%r8, %c[r8](%0) \n\t" |
| 6245 | "mov %%r9, %c[r9](%0) \n\t" |
| 6246 | "mov %%r10, %c[r10](%0) \n\t" |
| 6247 | "mov %%r11, %c[r11](%0) \n\t" |
| 6248 | "mov %%r12, %c[r12](%0) \n\t" |
| 6249 | "mov %%r13, %c[r13](%0) \n\t" |
| 6250 | "mov %%r14, %c[r14](%0) \n\t" |
| 6251 | "mov %%r15, %c[r15](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6252 | #endif |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6253 | "mov %%cr2, %%"R"ax \n\t" |
| 6254 | "mov %%"R"ax, %c[cr2](%0) \n\t" |
| 6255 | |
Avi Kivity | 1c696d0 | 2011-01-06 18:09:11 +0200 | [diff] [blame] | 6256 | "pop %%"R"bp; pop %%"R"dx \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6257 | "setbe %c[fail](%0) \n\t" |
| 6258 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6259 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6260 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd49 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6261 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6262 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 6263 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 6264 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 6265 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 6266 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 6267 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 6268 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6269 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6270 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 6271 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 6272 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 6273 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 6274 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 6275 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 6276 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 6277 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6278 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 6279 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 6280 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6281 | : "cc", "memory" |
Jan Kiszka | 07d6f55 | 2010-09-28 16:37:42 +0200 | [diff] [blame] | 6282 | , R"ax", R"bx", R"di", R"si" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6283 | #ifdef CONFIG_X86_64 |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6284 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
| 6285 | #endif |
| 6286 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6287 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 6288 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 6289 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 6290 | | (1 << VCPU_EXREG_CPL) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 6291 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 6292 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 6293 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 6294 | vcpu->arch.regs_dirty = 0; |
| 6295 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6296 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 6297 | |
Nadav Har'El | 66c78ae | 2011-05-25 23:14:07 +0300 | [diff] [blame] | 6298 | if (is_guest_mode(vcpu)) { |
| 6299 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6300 | vmcs12->idt_vectoring_info_field = vmx->idt_vectoring_info; |
| 6301 | if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) { |
| 6302 | vmcs12->idt_vectoring_error_code = |
| 6303 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 6304 | vmcs12->vm_exit_instruction_len = |
| 6305 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 6306 | } |
| 6307 | } |
| 6308 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6309 | vmx->loaded_vmcs->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 6310 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6311 | vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); |
Jan Kiszka | 1e2b1dd | 2011-09-12 10:52:24 +0200 | [diff] [blame] | 6312 | trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6313 | |
| 6314 | vmx_complete_atomic_exit(vmx); |
| 6315 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6316 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6317 | } |
| 6318 | |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6319 | #undef R |
| 6320 | #undef Q |
| 6321 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6322 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 6323 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6324 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6325 | |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 6326 | free_vpid(vmx); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6327 | free_nested(vmx); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6328 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6329 | kfree(vmx->guest_msrs); |
| 6330 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 6331 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6332 | } |
| 6333 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6334 | 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] | 6335 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6336 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 6337 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6338 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6339 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6340 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6341 | return ERR_PTR(-ENOMEM); |
| 6342 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 6343 | allocate_vpid(vmx); |
| 6344 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6345 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 6346 | if (err) |
| 6347 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6348 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6349 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 6350 | err = -ENOMEM; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6351 | if (!vmx->guest_msrs) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6352 | goto uninit_vcpu; |
| 6353 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6354 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6355 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 6356 | vmx->loaded_vmcs->vmcs = alloc_vmcs(); |
| 6357 | if (!vmx->loaded_vmcs->vmcs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6358 | goto free_msrs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6359 | if (!vmm_exclusive) |
| 6360 | kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id()))); |
| 6361 | loaded_vmcs_init(vmx->loaded_vmcs); |
| 6362 | if (!vmm_exclusive) |
| 6363 | kvm_cpu_vmxoff(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6364 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6365 | cpu = get_cpu(); |
| 6366 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 6367 | vmx->vcpu.cpu = cpu; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 6368 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6369 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6370 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6371 | if (err) |
| 6372 | goto free_vmcs; |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 6373 | if (vm_need_virtualize_apic_accesses(kvm)) |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 6374 | err = alloc_apic_access_page(kvm); |
| 6375 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 6376 | goto free_vmcs; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6377 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 6378 | if (enable_ept) { |
| 6379 | if (!kvm->arch.ept_identity_map_addr) |
| 6380 | kvm->arch.ept_identity_map_addr = |
| 6381 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 6382 | err = -ENOMEM; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 6383 | if (alloc_identity_pagetable(kvm) != 0) |
| 6384 | goto free_vmcs; |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 6385 | if (!init_rmode_identity_map(kvm)) |
| 6386 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 6387 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 6388 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 6389 | vmx->nested.current_vmptr = -1ull; |
| 6390 | vmx->nested.current_vmcs12 = NULL; |
| 6391 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6392 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6393 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6394 | free_vmcs: |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 6395 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6396 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6397 | kfree(vmx->guest_msrs); |
| 6398 | uninit_vcpu: |
| 6399 | kvm_vcpu_uninit(&vmx->vcpu); |
| 6400 | free_vcpu: |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 6401 | free_vpid(vmx); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 6402 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6403 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6404 | } |
| 6405 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6406 | static void __init vmx_check_processor_compat(void *rtn) |
| 6407 | { |
| 6408 | struct vmcs_config vmcs_conf; |
| 6409 | |
| 6410 | *(int *)rtn = 0; |
| 6411 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 6412 | *(int *)rtn = -EIO; |
| 6413 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 6414 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 6415 | smp_processor_id()); |
| 6416 | *(int *)rtn = -EIO; |
| 6417 | } |
| 6418 | } |
| 6419 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 6420 | static int get_ept_level(void) |
| 6421 | { |
| 6422 | return VMX_EPT_DEFAULT_GAW + 1; |
| 6423 | } |
| 6424 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6425 | 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] | 6426 | { |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6427 | u64 ret; |
| 6428 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6429 | /* For VT-d and EPT combination |
| 6430 | * 1. MMIO: always map as UC |
| 6431 | * 2. EPT with VT-d: |
| 6432 | * a. VT-d without snooping control feature: can't guarantee the |
| 6433 | * result, try to trust guest. |
| 6434 | * b. VT-d with snooping control feature: snooping control feature of |
| 6435 | * VT-d engine can guarantee the cache correctness. Just set it |
| 6436 | * to WB to keep consistent with host. So the same as item 3. |
Sheng Yang | a19a6d1 | 2010-02-09 16:41:53 +0800 | [diff] [blame] | 6437 | * 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] | 6438 | * consistent with host MTRR |
| 6439 | */ |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6440 | if (is_mmio) |
| 6441 | ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT; |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6442 | else if (vcpu->kvm->arch.iommu_domain && |
| 6443 | !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)) |
| 6444 | ret = kvm_get_guest_memory_type(vcpu, gfn) << |
| 6445 | VMX_EPT_MT_EPTE_SHIFT; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6446 | else |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6447 | ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT) |
Sheng Yang | a19a6d1 | 2010-02-09 16:41:53 +0800 | [diff] [blame] | 6448 | | VMX_EPT_IPAT_BIT; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6449 | |
| 6450 | return ret; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 6451 | } |
| 6452 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 6453 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 6454 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 6455 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 6456 | return PT_DIRECTORY_LEVEL; |
| 6457 | else |
| 6458 | /* For shadow and EPT supported 1GB page */ |
| 6459 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 6460 | } |
| 6461 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 6462 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 6463 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 6464 | struct kvm_cpuid_entry2 *best; |
| 6465 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6466 | u32 exec_control; |
| 6467 | |
| 6468 | vmx->rdtscp_enabled = false; |
| 6469 | if (vmx_rdtscp_supported()) { |
| 6470 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 6471 | if (exec_control & SECONDARY_EXEC_RDTSCP) { |
| 6472 | best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 6473 | if (best && (best->edx & bit(X86_FEATURE_RDTSCP))) |
| 6474 | vmx->rdtscp_enabled = true; |
| 6475 | else { |
| 6476 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 6477 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 6478 | exec_control); |
| 6479 | } |
| 6480 | } |
| 6481 | } |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 6482 | } |
| 6483 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 6484 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 6485 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 6486 | if (func == 1 && nested) |
| 6487 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 6488 | } |
| 6489 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 6490 | /* |
| 6491 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 6492 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
| 6493 | * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2 |
| 6494 | * guest in a way that will both be appropriate to L1's requests, and our |
| 6495 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 6496 | * function also has additional necessary side-effects, like setting various |
| 6497 | * vcpu->arch fields. |
| 6498 | */ |
| 6499 | static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 6500 | { |
| 6501 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6502 | u32 exec_control; |
| 6503 | |
| 6504 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 6505 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 6506 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 6507 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 6508 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 6509 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 6510 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 6511 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 6512 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 6513 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 6514 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 6515 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 6516 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 6517 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 6518 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 6519 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 6520 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 6521 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 6522 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 6523 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 6524 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 6525 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 6526 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 6527 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 6528 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 6529 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 6530 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 6531 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 6532 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 6533 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 6534 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 6535 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 6536 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 6537 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 6538 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 6539 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
| 6540 | |
| 6541 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 6542 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 6543 | vmcs12->vm_entry_intr_info_field); |
| 6544 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 6545 | vmcs12->vm_entry_exception_error_code); |
| 6546 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 6547 | vmcs12->vm_entry_instruction_len); |
| 6548 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 6549 | vmcs12->guest_interruptibility_info); |
| 6550 | vmcs_write32(GUEST_ACTIVITY_STATE, vmcs12->guest_activity_state); |
| 6551 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 6552 | vmcs_writel(GUEST_DR7, vmcs12->guest_dr7); |
| 6553 | vmcs_writel(GUEST_RFLAGS, vmcs12->guest_rflags); |
| 6554 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 6555 | vmcs12->guest_pending_dbg_exceptions); |
| 6556 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 6557 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 6558 | |
| 6559 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 6560 | |
| 6561 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, |
| 6562 | (vmcs_config.pin_based_exec_ctrl | |
| 6563 | vmcs12->pin_based_vm_exec_control)); |
| 6564 | |
| 6565 | /* |
| 6566 | * Whether page-faults are trapped is determined by a combination of |
| 6567 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 6568 | * If enable_ept, L0 doesn't care about page faults and we should |
| 6569 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 6570 | * care about (at least some) page faults, and because it is not easy |
| 6571 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 6572 | * to exit on each and every L2 page fault. This is done by setting |
| 6573 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 6574 | * Note that below we don't need special code to set EB.PF beyond the |
| 6575 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 6576 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 6577 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 6578 | * |
| 6579 | * A problem with this approach (when !enable_ept) is that L1 may be |
| 6580 | * injected with more page faults than it asked for. This could have |
| 6581 | * caused problems, but in practice existing hypervisors don't care. |
| 6582 | * To fix this, we will need to emulate the PFEC checking (on the L1 |
| 6583 | * page tables), using walk_addr(), when injecting PFs to L1. |
| 6584 | */ |
| 6585 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 6586 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 6587 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 6588 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 6589 | |
| 6590 | if (cpu_has_secondary_exec_ctrls()) { |
| 6591 | u32 exec_control = vmx_secondary_exec_control(vmx); |
| 6592 | if (!vmx->rdtscp_enabled) |
| 6593 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 6594 | /* Take the following fields only from vmcs12 */ |
| 6595 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6596 | if (nested_cpu_has(vmcs12, |
| 6597 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 6598 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 6599 | |
| 6600 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) { |
| 6601 | /* |
| 6602 | * Translate L1 physical address to host physical |
| 6603 | * address for vmcs02. Keep the page pinned, so this |
| 6604 | * physical address remains valid. We keep a reference |
| 6605 | * to it so we can release it later. |
| 6606 | */ |
| 6607 | if (vmx->nested.apic_access_page) /* shouldn't happen */ |
| 6608 | nested_release_page(vmx->nested.apic_access_page); |
| 6609 | vmx->nested.apic_access_page = |
| 6610 | nested_get_page(vcpu, vmcs12->apic_access_addr); |
| 6611 | /* |
| 6612 | * If translation failed, no matter: This feature asks |
| 6613 | * to exit when accessing the given address, and if it |
| 6614 | * can never be accessed, this feature won't do |
| 6615 | * anything anyway. |
| 6616 | */ |
| 6617 | if (!vmx->nested.apic_access_page) |
| 6618 | exec_control &= |
| 6619 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6620 | else |
| 6621 | vmcs_write64(APIC_ACCESS_ADDR, |
| 6622 | page_to_phys(vmx->nested.apic_access_page)); |
| 6623 | } |
| 6624 | |
| 6625 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 6626 | } |
| 6627 | |
| 6628 | |
| 6629 | /* |
| 6630 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 6631 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 6632 | * Other fields are different per CPU, and will be set later when |
| 6633 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 6634 | */ |
| 6635 | vmx_set_constant_host_state(); |
| 6636 | |
| 6637 | /* |
| 6638 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 6639 | * entry, but only if the current (host) sp changed from the value |
| 6640 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 6641 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 6642 | * here we just force the write to happen on entry. |
| 6643 | */ |
| 6644 | vmx->host_rsp = 0; |
| 6645 | |
| 6646 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 6647 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 6648 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 6649 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 6650 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
| 6651 | /* |
| 6652 | * Merging of IO and MSR bitmaps not currently supported. |
| 6653 | * Rather, exit every time. |
| 6654 | */ |
| 6655 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 6656 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 6657 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 6658 | |
| 6659 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 6660 | |
| 6661 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 6662 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 6663 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 6664 | */ |
| 6665 | update_exception_bitmap(vcpu); |
| 6666 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 6667 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 6668 | |
| 6669 | /* Note: IA32_MODE, LOAD_IA32_EFER are modified by vmx_set_efer below */ |
| 6670 | vmcs_write32(VM_EXIT_CONTROLS, |
| 6671 | vmcs12->vm_exit_controls | vmcs_config.vmexit_ctrl); |
| 6672 | vmcs_write32(VM_ENTRY_CONTROLS, vmcs12->vm_entry_controls | |
| 6673 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 6674 | |
| 6675 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) |
| 6676 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
| 6677 | else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 6678 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 6679 | |
| 6680 | |
| 6681 | set_cr4_guest_host_mask(vmx); |
| 6682 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 6683 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 6684 | vmcs_write64(TSC_OFFSET, |
| 6685 | vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset); |
| 6686 | else |
| 6687 | vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 6688 | |
| 6689 | if (enable_vpid) { |
| 6690 | /* |
| 6691 | * Trivially support vpid by letting L2s share their parent |
| 6692 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 6693 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 6694 | */ |
| 6695 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 6696 | vmx_flush_tlb(vcpu); |
| 6697 | } |
| 6698 | |
| 6699 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) |
| 6700 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
| 6701 | if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 6702 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 6703 | else |
| 6704 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 6705 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 6706 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 6707 | |
| 6708 | /* |
| 6709 | * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified |
| 6710 | * TS bit (for lazy fpu) and bits which we consider mandatory enabled. |
| 6711 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 6712 | * the specifications by L1; It's not enough to take |
| 6713 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 6714 | * have more bits than L1 expected. |
| 6715 | */ |
| 6716 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 6717 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 6718 | |
| 6719 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 6720 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 6721 | |
| 6722 | /* shadow page tables on either EPT or shadow page tables */ |
| 6723 | kvm_set_cr3(vcpu, vmcs12->guest_cr3); |
| 6724 | kvm_mmu_reset_context(vcpu); |
| 6725 | |
| 6726 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 6727 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
| 6728 | } |
| 6729 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 6730 | /* |
| 6731 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 6732 | * for running an L2 nested guest. |
| 6733 | */ |
| 6734 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 6735 | { |
| 6736 | struct vmcs12 *vmcs12; |
| 6737 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6738 | int cpu; |
| 6739 | struct loaded_vmcs *vmcs02; |
| 6740 | |
| 6741 | if (!nested_vmx_check_permission(vcpu) || |
| 6742 | !nested_vmx_check_vmcs12(vcpu)) |
| 6743 | return 1; |
| 6744 | |
| 6745 | skip_emulated_instruction(vcpu); |
| 6746 | vmcs12 = get_vmcs12(vcpu); |
| 6747 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 6748 | /* |
| 6749 | * The nested entry process starts with enforcing various prerequisites |
| 6750 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 6751 | * they fail: As the SDM explains, some conditions should cause the |
| 6752 | * instruction to fail, while others will cause the instruction to seem |
| 6753 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 6754 | * To speed up the normal (success) code path, we should avoid checking |
| 6755 | * for misconfigurations which will anyway be caught by the processor |
| 6756 | * when using the merged vmcs02. |
| 6757 | */ |
| 6758 | if (vmcs12->launch_state == launch) { |
| 6759 | nested_vmx_failValid(vcpu, |
| 6760 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 6761 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 6762 | return 1; |
| 6763 | } |
| 6764 | |
| 6765 | if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) && |
| 6766 | !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) { |
| 6767 | /*TODO: Also verify bits beyond physical address width are 0*/ |
| 6768 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 6769 | return 1; |
| 6770 | } |
| 6771 | |
| 6772 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
| 6773 | !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) { |
| 6774 | /*TODO: Also verify bits beyond physical address width are 0*/ |
| 6775 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 6776 | return 1; |
| 6777 | } |
| 6778 | |
| 6779 | if (vmcs12->vm_entry_msr_load_count > 0 || |
| 6780 | vmcs12->vm_exit_msr_load_count > 0 || |
| 6781 | vmcs12->vm_exit_msr_store_count > 0) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 6782 | pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n", |
| 6783 | __func__); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 6784 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 6785 | return 1; |
| 6786 | } |
| 6787 | |
| 6788 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
| 6789 | nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) || |
| 6790 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
| 6791 | nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) || |
| 6792 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
| 6793 | nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) || |
| 6794 | !vmx_control_verify(vmcs12->vm_exit_controls, |
| 6795 | nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) || |
| 6796 | !vmx_control_verify(vmcs12->vm_entry_controls, |
| 6797 | nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high)) |
| 6798 | { |
| 6799 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 6800 | return 1; |
| 6801 | } |
| 6802 | |
| 6803 | if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) || |
| 6804 | ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 6805 | nested_vmx_failValid(vcpu, |
| 6806 | VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
| 6807 | return 1; |
| 6808 | } |
| 6809 | |
| 6810 | if (((vmcs12->guest_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) || |
| 6811 | ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 6812 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 6813 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 6814 | return 1; |
| 6815 | } |
| 6816 | if (vmcs12->vmcs_link_pointer != -1ull) { |
| 6817 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 6818 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR); |
| 6819 | return 1; |
| 6820 | } |
| 6821 | |
| 6822 | /* |
| 6823 | * We're finally done with prerequisite checking, and can start with |
| 6824 | * the nested entry. |
| 6825 | */ |
| 6826 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 6827 | vmcs02 = nested_get_current_vmcs02(vmx); |
| 6828 | if (!vmcs02) |
| 6829 | return -ENOMEM; |
| 6830 | |
| 6831 | enter_guest_mode(vcpu); |
| 6832 | |
| 6833 | vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET); |
| 6834 | |
| 6835 | cpu = get_cpu(); |
| 6836 | vmx->loaded_vmcs = vmcs02; |
| 6837 | vmx_vcpu_put(vcpu); |
| 6838 | vmx_vcpu_load(vcpu, cpu); |
| 6839 | vcpu->cpu = cpu; |
| 6840 | put_cpu(); |
| 6841 | |
| 6842 | vmcs12->launch_state = 1; |
| 6843 | |
| 6844 | prepare_vmcs02(vcpu, vmcs12); |
| 6845 | |
| 6846 | /* |
| 6847 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 6848 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 6849 | * returned as far as L1 is concerned. It will only return (and set |
| 6850 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 6851 | */ |
| 6852 | return 1; |
| 6853 | } |
| 6854 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 6855 | /* |
| 6856 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 6857 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 6858 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 6859 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 6860 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 6861 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 6862 | * didn't trap the bit, because if L1 did, so would L0). |
| 6863 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 6864 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 6865 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 6866 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 6867 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 6868 | * changed these bits, and therefore they need to be updated, but L0 |
| 6869 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 6870 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 6871 | */ |
| 6872 | static inline unsigned long |
| 6873 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 6874 | { |
| 6875 | return |
| 6876 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 6877 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 6878 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 6879 | vcpu->arch.cr0_guest_owned_bits)); |
| 6880 | } |
| 6881 | |
| 6882 | static inline unsigned long |
| 6883 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 6884 | { |
| 6885 | return |
| 6886 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 6887 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 6888 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 6889 | vcpu->arch.cr4_guest_owned_bits)); |
| 6890 | } |
| 6891 | |
| 6892 | /* |
| 6893 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 6894 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 6895 | * and this function updates it to reflect the changes to the guest state while |
| 6896 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 6897 | * without going back to L1), and to reflect the exit reason. |
| 6898 | * Note that we do not have to copy here all VMCS fields, just those that |
| 6899 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 6900 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 6901 | * which already writes to vmcs12 directly. |
| 6902 | */ |
| 6903 | void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 6904 | { |
| 6905 | /* update guest state fields: */ |
| 6906 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 6907 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 6908 | |
| 6909 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 6910 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 6911 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 6912 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 6913 | |
| 6914 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 6915 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 6916 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 6917 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 6918 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 6919 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 6920 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 6921 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 6922 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 6923 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 6924 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 6925 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 6926 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 6927 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 6928 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 6929 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 6930 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 6931 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 6932 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 6933 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 6934 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 6935 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 6936 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 6937 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 6938 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 6939 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 6940 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 6941 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 6942 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 6943 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 6944 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 6945 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 6946 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 6947 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 6948 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 6949 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 6950 | |
| 6951 | vmcs12->guest_activity_state = vmcs_read32(GUEST_ACTIVITY_STATE); |
| 6952 | vmcs12->guest_interruptibility_info = |
| 6953 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 6954 | vmcs12->guest_pending_dbg_exceptions = |
| 6955 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
| 6956 | |
| 6957 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 6958 | * the relevant bit asks not to trap the change */ |
| 6959 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 6960 | if (vmcs12->vm_entry_controls & VM_EXIT_SAVE_IA32_PAT) |
| 6961 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
| 6962 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 6963 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 6964 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
| 6965 | |
| 6966 | /* update exit information fields: */ |
| 6967 | |
| 6968 | vmcs12->vm_exit_reason = vmcs_read32(VM_EXIT_REASON); |
| 6969 | vmcs12->exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6970 | |
| 6971 | vmcs12->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 6972 | vmcs12->vm_exit_intr_error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 6973 | vmcs12->idt_vectoring_info_field = |
| 6974 | vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 6975 | vmcs12->idt_vectoring_error_code = |
| 6976 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 6977 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 6978 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 6979 | |
| 6980 | /* clear vm-entry fields which are to be cleared on exit */ |
| 6981 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 6982 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
| 6983 | } |
| 6984 | |
| 6985 | /* |
| 6986 | * A part of what we need to when the nested L2 guest exits and we want to |
| 6987 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 6988 | * in vmcs12. |
| 6989 | * This function is to be called not only on normal nested exit, but also on |
| 6990 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 6991 | * Failures During or After Loading Guest State"). |
| 6992 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 6993 | */ |
| 6994 | void load_vmcs12_host_state(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 6995 | { |
| 6996 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 6997 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
| 6998 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 6999 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 7000 | else |
| 7001 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 7002 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 7003 | |
| 7004 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 7005 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
| 7006 | /* |
| 7007 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 7008 | * actually changed, because it depends on the current state of |
| 7009 | * fpu_active (which may have changed). |
| 7010 | * Note that vmx_set_cr0 refers to efer set above. |
| 7011 | */ |
| 7012 | kvm_set_cr0(vcpu, vmcs12->host_cr0); |
| 7013 | /* |
| 7014 | * If we did fpu_activate()/fpu_deactivate() during L2's run, we need |
| 7015 | * to apply the same changes to L1's vmcs. We just set cr0 correctly, |
| 7016 | * but we also need to update cr0_guest_host_mask and exception_bitmap. |
| 7017 | */ |
| 7018 | update_exception_bitmap(vcpu); |
| 7019 | vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0); |
| 7020 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 7021 | |
| 7022 | /* |
| 7023 | * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01 |
| 7024 | * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask(); |
| 7025 | */ |
| 7026 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 7027 | kvm_set_cr4(vcpu, vmcs12->host_cr4); |
| 7028 | |
| 7029 | /* shadow page tables on either EPT or shadow page tables */ |
| 7030 | kvm_set_cr3(vcpu, vmcs12->host_cr3); |
| 7031 | kvm_mmu_reset_context(vcpu); |
| 7032 | |
| 7033 | if (enable_vpid) { |
| 7034 | /* |
| 7035 | * Trivially support vpid by letting L2s share their parent |
| 7036 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 7037 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 7038 | */ |
| 7039 | vmx_flush_tlb(vcpu); |
| 7040 | } |
| 7041 | |
| 7042 | |
| 7043 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 7044 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 7045 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 7046 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 7047 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
| 7048 | vmcs_writel(GUEST_TR_BASE, vmcs12->host_tr_base); |
| 7049 | vmcs_writel(GUEST_GS_BASE, vmcs12->host_gs_base); |
| 7050 | vmcs_writel(GUEST_FS_BASE, vmcs12->host_fs_base); |
| 7051 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->host_es_selector); |
| 7052 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->host_cs_selector); |
| 7053 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->host_ss_selector); |
| 7054 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->host_ds_selector); |
| 7055 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->host_fs_selector); |
| 7056 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->host_gs_selector); |
| 7057 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->host_tr_selector); |
| 7058 | |
| 7059 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) |
| 7060 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
| 7061 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 7062 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 7063 | vmcs12->host_ia32_perf_global_ctrl); |
| 7064 | } |
| 7065 | |
| 7066 | /* |
| 7067 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 7068 | * and modify vmcs12 to make it see what it would expect to see there if |
| 7069 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 7070 | */ |
| 7071 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu) |
| 7072 | { |
| 7073 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7074 | int cpu; |
| 7075 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 7076 | |
| 7077 | leave_guest_mode(vcpu); |
| 7078 | prepare_vmcs12(vcpu, vmcs12); |
| 7079 | |
| 7080 | cpu = get_cpu(); |
| 7081 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 7082 | vmx_vcpu_put(vcpu); |
| 7083 | vmx_vcpu_load(vcpu, cpu); |
| 7084 | vcpu->cpu = cpu; |
| 7085 | put_cpu(); |
| 7086 | |
| 7087 | /* if no vmcs02 cache requested, remove the one we used */ |
| 7088 | if (VMCS02_POOL_SIZE == 0) |
| 7089 | nested_free_vmcs02(vmx, vmx->nested.current_vmptr); |
| 7090 | |
| 7091 | load_vmcs12_host_state(vcpu, vmcs12); |
| 7092 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 7093 | /* Update TSC_OFFSET if TSC was changed while L2 ran */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 7094 | vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset); |
| 7095 | |
| 7096 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 7097 | vmx->host_rsp = 0; |
| 7098 | |
| 7099 | /* Unpin physical memory we referred to in vmcs02 */ |
| 7100 | if (vmx->nested.apic_access_page) { |
| 7101 | nested_release_page(vmx->nested.apic_access_page); |
| 7102 | vmx->nested.apic_access_page = 0; |
| 7103 | } |
| 7104 | |
| 7105 | /* |
| 7106 | * Exiting from L2 to L1, we're now back to L1 which thinks it just |
| 7107 | * finished a VMLAUNCH or VMRESUME instruction, so we need to set the |
| 7108 | * success or failure flag accordingly. |
| 7109 | */ |
| 7110 | if (unlikely(vmx->fail)) { |
| 7111 | vmx->fail = 0; |
| 7112 | nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 7113 | } else |
| 7114 | nested_vmx_succeed(vcpu); |
| 7115 | } |
| 7116 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 7117 | /* |
| 7118 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 7119 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 7120 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 7121 | * It should only be called before L2 actually succeeded to run, and when |
| 7122 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 7123 | */ |
| 7124 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 7125 | struct vmcs12 *vmcs12, |
| 7126 | u32 reason, unsigned long qualification) |
| 7127 | { |
| 7128 | load_vmcs12_host_state(vcpu, vmcs12); |
| 7129 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 7130 | vmcs12->exit_qualification = qualification; |
| 7131 | nested_vmx_succeed(vcpu); |
| 7132 | } |
| 7133 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 7134 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 7135 | struct x86_instruction_info *info, |
| 7136 | enum x86_intercept_stage stage) |
| 7137 | { |
| 7138 | return X86EMUL_CONTINUE; |
| 7139 | } |
| 7140 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 7141 | static struct kvm_x86_ops vmx_x86_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7142 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 7143 | .disabled_by_bios = vmx_disabled_by_bios, |
| 7144 | .hardware_setup = hardware_setup, |
| 7145 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 7146 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7147 | .hardware_enable = hardware_enable, |
| 7148 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 7149 | .cpu_has_accelerated_tpr = report_flexpriority, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7150 | |
| 7151 | .vcpu_create = vmx_create_vcpu, |
| 7152 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 7153 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7154 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 7155 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7156 | .vcpu_load = vmx_vcpu_load, |
| 7157 | .vcpu_put = vmx_vcpu_put, |
| 7158 | |
| 7159 | .set_guest_debug = set_guest_debug, |
| 7160 | .get_msr = vmx_get_msr, |
| 7161 | .set_msr = vmx_set_msr, |
| 7162 | .get_segment_base = vmx_get_segment_base, |
| 7163 | .get_segment = vmx_get_segment, |
| 7164 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 7165 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7166 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 7167 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 7168 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 7169 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7170 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7171 | .set_cr3 = vmx_set_cr3, |
| 7172 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7173 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7174 | .get_idt = vmx_get_idt, |
| 7175 | .set_idt = vmx_set_idt, |
| 7176 | .get_gdt = vmx_get_gdt, |
| 7177 | .set_gdt = vmx_set_gdt, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7178 | .set_dr7 = vmx_set_dr7, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 7179 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7180 | .get_rflags = vmx_get_rflags, |
| 7181 | .set_rflags = vmx_set_rflags, |
Avi Kivity | ebcbab4 | 2010-02-07 11:56:52 +0200 | [diff] [blame] | 7182 | .fpu_activate = vmx_fpu_activate, |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 7183 | .fpu_deactivate = vmx_fpu_deactivate, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7184 | |
| 7185 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7186 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7187 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 7188 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7189 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 7190 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 7191 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7192 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 7193 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7194 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 7195 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 7196 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 7197 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7198 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 7199 | .get_nmi_mask = vmx_get_nmi_mask, |
| 7200 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7201 | .enable_nmi_window = enable_nmi_window, |
| 7202 | .enable_irq_window = enable_irq_window, |
| 7203 | .update_cr8_intercept = update_cr8_intercept, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7204 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 7205 | .set_tss_addr = vmx_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 7206 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 7207 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7208 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 7209 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 7210 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 7211 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 7212 | |
| 7213 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 7214 | |
| 7215 | .rdtscp_supported = vmx_rdtscp_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 7216 | |
| 7217 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 7218 | |
| 7219 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 7220 | |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 7221 | .set_tsc_khz = vmx_set_tsc_khz, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 7222 | .write_tsc_offset = vmx_write_tsc_offset, |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 7223 | .adjust_tsc_offset = vmx_adjust_tsc_offset, |
Joerg Roedel | 857e409 | 2011-03-25 09:44:50 +0100 | [diff] [blame] | 7224 | .compute_tsc_offset = vmx_compute_tsc_offset, |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 7225 | .read_l1_tsc = vmx_read_l1_tsc, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 7226 | |
| 7227 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 7228 | |
| 7229 | .check_intercept = vmx_check_intercept, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7230 | }; |
| 7231 | |
| 7232 | static int __init vmx_init(void) |
| 7233 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 7234 | int r, i; |
| 7235 | |
| 7236 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 7237 | |
| 7238 | for (i = 0; i < NR_VMX_MSR; ++i) |
| 7239 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7240 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7241 | vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7242 | if (!vmx_io_bitmap_a) |
| 7243 | return -ENOMEM; |
| 7244 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7245 | vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7246 | if (!vmx_io_bitmap_b) { |
| 7247 | r = -ENOMEM; |
| 7248 | goto out; |
| 7249 | } |
| 7250 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7251 | vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7252 | if (!vmx_msr_bitmap_legacy) { |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7253 | r = -ENOMEM; |
| 7254 | goto out1; |
| 7255 | } |
| 7256 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7257 | vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7258 | if (!vmx_msr_bitmap_longmode) { |
| 7259 | r = -ENOMEM; |
| 7260 | goto out2; |
| 7261 | } |
| 7262 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7263 | /* |
| 7264 | * Allow direct access to the PC debug port (it is often used for I/O |
| 7265 | * delays, but the vmexits simply slow things down). |
| 7266 | */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7267 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); |
| 7268 | clear_bit(0x80, vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7269 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7270 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7271 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7272 | memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE); |
| 7273 | memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7274 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 7275 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 7276 | |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 7277 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
| 7278 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7279 | if (r) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7280 | goto out3; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7281 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7282 | vmx_disable_intercept_for_msr(MSR_FS_BASE, false); |
| 7283 | vmx_disable_intercept_for_msr(MSR_GS_BASE, false); |
| 7284 | vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true); |
| 7285 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false); |
| 7286 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false); |
| 7287 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7288 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 7289 | if (enable_ept) { |
Sheng Yang | 534e38b | 2008-09-08 15:12:30 +0800 | [diff] [blame] | 7290 | kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 7291 | VMX_EPT_EXECUTABLE_MASK); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 7292 | ept_set_mmio_spte_mask(); |
Sheng Yang | 5fdbcb9 | 2008-07-16 09:25:40 +0800 | [diff] [blame] | 7293 | kvm_enable_tdp(); |
| 7294 | } else |
| 7295 | kvm_disable_tdp(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7296 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7297 | return 0; |
| 7298 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7299 | out3: |
| 7300 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7301 | out2: |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7302 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7303 | out1: |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7304 | free_page((unsigned long)vmx_io_bitmap_b); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7305 | out: |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7306 | free_page((unsigned long)vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7307 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7308 | } |
| 7309 | |
| 7310 | static void __exit vmx_exit(void) |
| 7311 | { |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7312 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
| 7313 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7314 | free_page((unsigned long)vmx_io_bitmap_b); |
| 7315 | free_page((unsigned long)vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7316 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 7317 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7318 | } |
| 7319 | |
| 7320 | module_init(vmx_init) |
| 7321 | module_exit(vmx_exit) |