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 | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 21 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 22 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 23 | #include <linux/module.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 24 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/mm.h> |
| 26 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 27 | #include <linux/sched.h> |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 28 | #include <linux/moduleparam.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 29 | #include <linux/ftrace_event.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 31 | #include <linux/tboot.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 32 | #include "kvm_cache_regs.h" |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 33 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 34 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 35 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 36 | #include <asm/desc.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 37 | #include <asm/vmx.h> |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 38 | #include <asm/virtext.h> |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 39 | #include <asm/mce.h> |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 40 | #include <asm/i387.h> |
| 41 | #include <asm/xcr.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 42 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 43 | #include "trace.h" |
| 44 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 45 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 46 | #define __ex_clear(x, reg) \ |
| 47 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 48 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 49 | MODULE_AUTHOR("Qumranet"); |
| 50 | MODULE_LICENSE("GPL"); |
| 51 | |
Avi Kivity | 4462d21 | 2009-03-23 17:53:37 +0200 | [diff] [blame] | 52 | static int __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 53 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 54 | |
Avi Kivity | 4462d21 | 2009-03-23 17:53:37 +0200 | [diff] [blame] | 55 | static int __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 56 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 57 | |
Avi Kivity | 4462d21 | 2009-03-23 17:53:37 +0200 | [diff] [blame] | 58 | static int __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 59 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 60 | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 61 | static int __read_mostly enable_unrestricted_guest = 1; |
| 62 | module_param_named(unrestricted_guest, |
| 63 | enable_unrestricted_guest, bool, S_IRUGO); |
| 64 | |
Avi Kivity | 4462d21 | 2009-03-23 17:53:37 +0200 | [diff] [blame] | 65 | static int __read_mostly emulate_invalid_guest_state = 0; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 66 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 67 | |
Dongxiao Xu | b923e62 | 2010-05-11 18:29:45 +0800 | [diff] [blame] | 68 | static int __read_mostly vmm_exclusive = 1; |
| 69 | module_param(vmm_exclusive, bool, S_IRUGO); |
| 70 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 71 | static int __read_mostly yield_on_hlt = 1; |
| 72 | module_param(yield_on_hlt, bool, S_IRUGO); |
| 73 | |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 74 | static int __read_mostly fasteoi = 1; |
| 75 | module_param(fasteoi, bool, S_IRUGO); |
| 76 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 77 | /* |
| 78 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 79 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 80 | * use VMX instructions. |
| 81 | */ |
| 82 | static int __read_mostly nested = 0; |
| 83 | module_param(nested, bool, S_IRUGO); |
| 84 | |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 85 | #define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \ |
| 86 | (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD) |
| 87 | #define KVM_GUEST_CR0_MASK \ |
| 88 | (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE) |
| 89 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \ |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 90 | (X86_CR0_WP | X86_CR0_NE) |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 91 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 92 | (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] | 93 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 94 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
| 95 | | X86_CR4_OSXMMEXCPT) |
| 96 | |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 97 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 98 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 99 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 100 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 101 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 102 | /* |
| 103 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 104 | * ple_gap: upper bound on the amount of time between two successive |
| 105 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 106 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 107 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 108 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 109 | * less than 2^12 cycles |
| 110 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 111 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 112 | */ |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 113 | #define KVM_VMX_DEFAULT_PLE_GAP 128 |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 114 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 |
| 115 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
| 116 | module_param(ple_gap, int, S_IRUGO); |
| 117 | |
| 118 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 119 | module_param(ple_window, int, S_IRUGO); |
| 120 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame^] | 121 | #define NR_AUTOLOAD_MSRS 8 |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 122 | #define VMCS02_POOL_SIZE 1 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 123 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 124 | struct vmcs { |
| 125 | u32 revision_id; |
| 126 | u32 abort; |
| 127 | char data[0]; |
| 128 | }; |
| 129 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 130 | /* |
| 131 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 132 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 133 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 134 | */ |
| 135 | struct loaded_vmcs { |
| 136 | struct vmcs *vmcs; |
| 137 | int cpu; |
| 138 | int launched; |
| 139 | struct list_head loaded_vmcss_on_cpu_link; |
| 140 | }; |
| 141 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 142 | struct shared_msr_entry { |
| 143 | unsigned index; |
| 144 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 145 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 146 | }; |
| 147 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 148 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 149 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 150 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 151 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 152 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 153 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 154 | * More than one of these structures may exist, if L1 runs multiple L2 guests. |
| 155 | * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the |
| 156 | * underlying hardware which will be used to run L2. |
| 157 | * This structure is packed to ensure that its layout is identical across |
| 158 | * machines (necessary for live migration). |
| 159 | * If there are changes in this struct, VMCS12_REVISION must be changed. |
| 160 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 161 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 162 | struct __packed vmcs12 { |
| 163 | /* According to the Intel spec, a VMCS region must start with the |
| 164 | * following two fields. Then follow implementation-specific data. |
| 165 | */ |
| 166 | u32 revision_id; |
| 167 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 168 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 169 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 170 | u32 padding[7]; /* room for future expansion */ |
| 171 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 172 | u64 io_bitmap_a; |
| 173 | u64 io_bitmap_b; |
| 174 | u64 msr_bitmap; |
| 175 | u64 vm_exit_msr_store_addr; |
| 176 | u64 vm_exit_msr_load_addr; |
| 177 | u64 vm_entry_msr_load_addr; |
| 178 | u64 tsc_offset; |
| 179 | u64 virtual_apic_page_addr; |
| 180 | u64 apic_access_addr; |
| 181 | u64 ept_pointer; |
| 182 | u64 guest_physical_address; |
| 183 | u64 vmcs_link_pointer; |
| 184 | u64 guest_ia32_debugctl; |
| 185 | u64 guest_ia32_pat; |
| 186 | u64 guest_ia32_efer; |
| 187 | u64 guest_ia32_perf_global_ctrl; |
| 188 | u64 guest_pdptr0; |
| 189 | u64 guest_pdptr1; |
| 190 | u64 guest_pdptr2; |
| 191 | u64 guest_pdptr3; |
| 192 | u64 host_ia32_pat; |
| 193 | u64 host_ia32_efer; |
| 194 | u64 host_ia32_perf_global_ctrl; |
| 195 | u64 padding64[8]; /* room for future expansion */ |
| 196 | /* |
| 197 | * To allow migration of L1 (complete with its L2 guests) between |
| 198 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 199 | * unsigned long fields with no explict size. We use u64 (aliased |
| 200 | * natural_width) instead. Luckily, x86 is little-endian. |
| 201 | */ |
| 202 | natural_width cr0_guest_host_mask; |
| 203 | natural_width cr4_guest_host_mask; |
| 204 | natural_width cr0_read_shadow; |
| 205 | natural_width cr4_read_shadow; |
| 206 | natural_width cr3_target_value0; |
| 207 | natural_width cr3_target_value1; |
| 208 | natural_width cr3_target_value2; |
| 209 | natural_width cr3_target_value3; |
| 210 | natural_width exit_qualification; |
| 211 | natural_width guest_linear_address; |
| 212 | natural_width guest_cr0; |
| 213 | natural_width guest_cr3; |
| 214 | natural_width guest_cr4; |
| 215 | natural_width guest_es_base; |
| 216 | natural_width guest_cs_base; |
| 217 | natural_width guest_ss_base; |
| 218 | natural_width guest_ds_base; |
| 219 | natural_width guest_fs_base; |
| 220 | natural_width guest_gs_base; |
| 221 | natural_width guest_ldtr_base; |
| 222 | natural_width guest_tr_base; |
| 223 | natural_width guest_gdtr_base; |
| 224 | natural_width guest_idtr_base; |
| 225 | natural_width guest_dr7; |
| 226 | natural_width guest_rsp; |
| 227 | natural_width guest_rip; |
| 228 | natural_width guest_rflags; |
| 229 | natural_width guest_pending_dbg_exceptions; |
| 230 | natural_width guest_sysenter_esp; |
| 231 | natural_width guest_sysenter_eip; |
| 232 | natural_width host_cr0; |
| 233 | natural_width host_cr3; |
| 234 | natural_width host_cr4; |
| 235 | natural_width host_fs_base; |
| 236 | natural_width host_gs_base; |
| 237 | natural_width host_tr_base; |
| 238 | natural_width host_gdtr_base; |
| 239 | natural_width host_idtr_base; |
| 240 | natural_width host_ia32_sysenter_esp; |
| 241 | natural_width host_ia32_sysenter_eip; |
| 242 | natural_width host_rsp; |
| 243 | natural_width host_rip; |
| 244 | natural_width paddingl[8]; /* room for future expansion */ |
| 245 | u32 pin_based_vm_exec_control; |
| 246 | u32 cpu_based_vm_exec_control; |
| 247 | u32 exception_bitmap; |
| 248 | u32 page_fault_error_code_mask; |
| 249 | u32 page_fault_error_code_match; |
| 250 | u32 cr3_target_count; |
| 251 | u32 vm_exit_controls; |
| 252 | u32 vm_exit_msr_store_count; |
| 253 | u32 vm_exit_msr_load_count; |
| 254 | u32 vm_entry_controls; |
| 255 | u32 vm_entry_msr_load_count; |
| 256 | u32 vm_entry_intr_info_field; |
| 257 | u32 vm_entry_exception_error_code; |
| 258 | u32 vm_entry_instruction_len; |
| 259 | u32 tpr_threshold; |
| 260 | u32 secondary_vm_exec_control; |
| 261 | u32 vm_instruction_error; |
| 262 | u32 vm_exit_reason; |
| 263 | u32 vm_exit_intr_info; |
| 264 | u32 vm_exit_intr_error_code; |
| 265 | u32 idt_vectoring_info_field; |
| 266 | u32 idt_vectoring_error_code; |
| 267 | u32 vm_exit_instruction_len; |
| 268 | u32 vmx_instruction_info; |
| 269 | u32 guest_es_limit; |
| 270 | u32 guest_cs_limit; |
| 271 | u32 guest_ss_limit; |
| 272 | u32 guest_ds_limit; |
| 273 | u32 guest_fs_limit; |
| 274 | u32 guest_gs_limit; |
| 275 | u32 guest_ldtr_limit; |
| 276 | u32 guest_tr_limit; |
| 277 | u32 guest_gdtr_limit; |
| 278 | u32 guest_idtr_limit; |
| 279 | u32 guest_es_ar_bytes; |
| 280 | u32 guest_cs_ar_bytes; |
| 281 | u32 guest_ss_ar_bytes; |
| 282 | u32 guest_ds_ar_bytes; |
| 283 | u32 guest_fs_ar_bytes; |
| 284 | u32 guest_gs_ar_bytes; |
| 285 | u32 guest_ldtr_ar_bytes; |
| 286 | u32 guest_tr_ar_bytes; |
| 287 | u32 guest_interruptibility_info; |
| 288 | u32 guest_activity_state; |
| 289 | u32 guest_sysenter_cs; |
| 290 | u32 host_ia32_sysenter_cs; |
| 291 | u32 padding32[8]; /* room for future expansion */ |
| 292 | u16 virtual_processor_id; |
| 293 | u16 guest_es_selector; |
| 294 | u16 guest_cs_selector; |
| 295 | u16 guest_ss_selector; |
| 296 | u16 guest_ds_selector; |
| 297 | u16 guest_fs_selector; |
| 298 | u16 guest_gs_selector; |
| 299 | u16 guest_ldtr_selector; |
| 300 | u16 guest_tr_selector; |
| 301 | u16 host_es_selector; |
| 302 | u16 host_cs_selector; |
| 303 | u16 host_ss_selector; |
| 304 | u16 host_ds_selector; |
| 305 | u16 host_fs_selector; |
| 306 | u16 host_gs_selector; |
| 307 | u16 host_tr_selector; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | /* |
| 311 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 312 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 313 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. |
| 314 | */ |
| 315 | #define VMCS12_REVISION 0x11e57ed0 |
| 316 | |
| 317 | /* |
| 318 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 319 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 320 | * current implementation, 4K are reserved to avoid future complications. |
| 321 | */ |
| 322 | #define VMCS12_SIZE 0x1000 |
| 323 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 324 | /* Used to remember the last vmcs02 used for some recently used vmcs12s */ |
| 325 | struct vmcs02_list { |
| 326 | struct list_head list; |
| 327 | gpa_t vmptr; |
| 328 | struct loaded_vmcs vmcs02; |
| 329 | }; |
| 330 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 331 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 332 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 333 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 334 | */ |
| 335 | struct nested_vmx { |
| 336 | /* Has the level1 guest done vmxon? */ |
| 337 | bool vmxon; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 338 | |
| 339 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 340 | gpa_t current_vmptr; |
| 341 | /* The host-usable pointer to the above */ |
| 342 | struct page *current_vmcs12_page; |
| 343 | struct vmcs12 *current_vmcs12; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 344 | |
| 345 | /* vmcs02_list cache of VMCSs recently used to run L2 guests */ |
| 346 | struct list_head vmcs02_pool; |
| 347 | int vmcs02_num; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 348 | u64 vmcs01_tsc_offset; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 349 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 350 | bool nested_run_pending; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 351 | /* |
| 352 | * Guest pages referred to in vmcs02 with host-physical pointers, so |
| 353 | * we must keep them pinned while L2 runs. |
| 354 | */ |
| 355 | struct page *apic_access_page; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 356 | }; |
| 357 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 358 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 359 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 360 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 361 | u8 fail; |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 362 | u8 cpl; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 363 | bool nmi_known_unmasked; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 364 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 365 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 366 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 367 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 368 | int nmsrs; |
| 369 | int save_nmsrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 370 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 371 | u64 msr_host_kernel_gs_base; |
| 372 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 373 | #endif |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 374 | /* |
| 375 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 376 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 377 | * guest (L2), it points to a different VMCS. |
| 378 | */ |
| 379 | struct loaded_vmcs vmcs01; |
| 380 | struct loaded_vmcs *loaded_vmcs; |
| 381 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 382 | struct msr_autoload { |
| 383 | unsigned nr; |
| 384 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 385 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 386 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 387 | struct { |
| 388 | int loaded; |
| 389 | u16 fs_sel, gs_sel, ldt_sel; |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 390 | int gs_ldt_reload_needed; |
| 391 | int fs_reload_needed; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 392 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 393 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 394 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 395 | ulong save_rflags; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 396 | struct kvm_save_segment { |
| 397 | u16 selector; |
| 398 | unsigned long base; |
| 399 | u32 limit; |
| 400 | u32 ar; |
| 401 | } tr, es, ds, fs, gs; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 402 | } rmode; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 403 | struct { |
| 404 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
| 405 | struct kvm_save_segment seg[8]; |
| 406 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 407 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 408 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 409 | |
| 410 | /* Support for vnmi-less CPUs */ |
| 411 | int soft_vnmi_blocked; |
| 412 | ktime_t entry_time; |
| 413 | s64 vnmi_blocked_time; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 414 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 415 | |
| 416 | bool rdtscp_enabled; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 417 | |
| 418 | /* Support for a guest hypervisor (nested VMX) */ |
| 419 | struct nested_vmx nested; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 420 | }; |
| 421 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 422 | enum segment_cache_field { |
| 423 | SEG_FIELD_SEL = 0, |
| 424 | SEG_FIELD_BASE = 1, |
| 425 | SEG_FIELD_LIMIT = 2, |
| 426 | SEG_FIELD_AR = 3, |
| 427 | |
| 428 | SEG_FIELD_NR = 4 |
| 429 | }; |
| 430 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 431 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 432 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 433 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 436 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
| 437 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) |
| 438 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ |
| 439 | [number##_HIGH] = VMCS12_OFFSET(name)+4 |
| 440 | |
| 441 | static unsigned short vmcs_field_to_offset_table[] = { |
| 442 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
| 443 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 444 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 445 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 446 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 447 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 448 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 449 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 450 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
| 451 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 452 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 453 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 454 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 455 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 456 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 457 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 458 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 459 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 460 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 461 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 462 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 463 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
| 464 | FIELD64(TSC_OFFSET, tsc_offset), |
| 465 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 466 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
| 467 | FIELD64(EPT_POINTER, ept_pointer), |
| 468 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 469 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 470 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 471 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 472 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 473 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 474 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 475 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 476 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 477 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
| 478 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 479 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 480 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 481 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 482 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 483 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 484 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 485 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 486 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 487 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 488 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 489 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 490 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 491 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 492 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 493 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 494 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 495 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 496 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 497 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 498 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 499 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 500 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 501 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 502 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 503 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 504 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 505 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 506 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 507 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 508 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 509 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 510 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 511 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 512 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 513 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 514 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 515 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 516 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 517 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 518 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 519 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 520 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 521 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 522 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 523 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 524 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 525 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 526 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
| 527 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 528 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 529 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 530 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 531 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 532 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 533 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 534 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 535 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 536 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 537 | FIELD(GUEST_CR0, guest_cr0), |
| 538 | FIELD(GUEST_CR3, guest_cr3), |
| 539 | FIELD(GUEST_CR4, guest_cr4), |
| 540 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 541 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 542 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 543 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 544 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 545 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 546 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 547 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 548 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 549 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 550 | FIELD(GUEST_DR7, guest_dr7), |
| 551 | FIELD(GUEST_RSP, guest_rsp), |
| 552 | FIELD(GUEST_RIP, guest_rip), |
| 553 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 554 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 555 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 556 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 557 | FIELD(HOST_CR0, host_cr0), |
| 558 | FIELD(HOST_CR3, host_cr3), |
| 559 | FIELD(HOST_CR4, host_cr4), |
| 560 | FIELD(HOST_FS_BASE, host_fs_base), |
| 561 | FIELD(HOST_GS_BASE, host_gs_base), |
| 562 | FIELD(HOST_TR_BASE, host_tr_base), |
| 563 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 564 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 565 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 566 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 567 | FIELD(HOST_RSP, host_rsp), |
| 568 | FIELD(HOST_RIP, host_rip), |
| 569 | }; |
| 570 | static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table); |
| 571 | |
| 572 | static inline short vmcs_field_to_offset(unsigned long field) |
| 573 | { |
| 574 | if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0) |
| 575 | return -1; |
| 576 | return vmcs_field_to_offset_table[field]; |
| 577 | } |
| 578 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 579 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 580 | { |
| 581 | return to_vmx(vcpu)->nested.current_vmcs12; |
| 582 | } |
| 583 | |
| 584 | static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr) |
| 585 | { |
| 586 | struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT); |
| 587 | if (is_error_page(page)) { |
| 588 | kvm_release_page_clean(page); |
| 589 | return NULL; |
| 590 | } |
| 591 | return page; |
| 592 | } |
| 593 | |
| 594 | static void nested_release_page(struct page *page) |
| 595 | { |
| 596 | kvm_release_page_dirty(page); |
| 597 | } |
| 598 | |
| 599 | static void nested_release_page_clean(struct page *page) |
| 600 | { |
| 601 | kvm_release_page_clean(page); |
| 602 | } |
| 603 | |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 604 | static u64 construct_eptp(unsigned long root_hpa); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 605 | static void kvm_cpu_vmxon(u64 addr); |
| 606 | static void kvm_cpu_vmxoff(void); |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 607 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 608 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 609 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 610 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 611 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 612 | /* |
| 613 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 614 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 615 | */ |
| 616 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 617 | static DEFINE_PER_CPU(struct desc_ptr, host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 618 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 619 | static unsigned long *vmx_io_bitmap_a; |
| 620 | static unsigned long *vmx_io_bitmap_b; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 621 | static unsigned long *vmx_msr_bitmap_legacy; |
| 622 | static unsigned long *vmx_msr_bitmap_longmode; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 623 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 624 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame^] | 625 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 626 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 627 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 628 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 629 | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 630 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 631 | int size; |
| 632 | int order; |
| 633 | u32 revision_id; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 634 | u32 pin_based_exec_ctrl; |
| 635 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 636 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 637 | u32 vmexit_ctrl; |
| 638 | u32 vmentry_ctrl; |
| 639 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 640 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 641 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 642 | u32 ept; |
| 643 | u32 vpid; |
| 644 | } vmx_capability; |
| 645 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 646 | #define VMX_SEGMENT_FIELD(seg) \ |
| 647 | [VCPU_SREG_##seg] = { \ |
| 648 | .selector = GUEST_##seg##_SELECTOR, \ |
| 649 | .base = GUEST_##seg##_BASE, \ |
| 650 | .limit = GUEST_##seg##_LIMIT, \ |
| 651 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 652 | } |
| 653 | |
| 654 | static struct kvm_vmx_segment_field { |
| 655 | unsigned selector; |
| 656 | unsigned base; |
| 657 | unsigned limit; |
| 658 | unsigned ar_bytes; |
| 659 | } kvm_vmx_segment_fields[] = { |
| 660 | VMX_SEGMENT_FIELD(CS), |
| 661 | VMX_SEGMENT_FIELD(DS), |
| 662 | VMX_SEGMENT_FIELD(ES), |
| 663 | VMX_SEGMENT_FIELD(FS), |
| 664 | VMX_SEGMENT_FIELD(GS), |
| 665 | VMX_SEGMENT_FIELD(SS), |
| 666 | VMX_SEGMENT_FIELD(TR), |
| 667 | VMX_SEGMENT_FIELD(LDTR), |
| 668 | }; |
| 669 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 670 | static u64 host_efer; |
| 671 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 672 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 673 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 674 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 675 | * 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] | 676 | * away by decrementing the array size. |
| 677 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 678 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 679 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 680 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 681 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 682 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 683 | }; |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 684 | #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 685 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 686 | static inline bool is_page_fault(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 687 | { |
| 688 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 689 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 690 | (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 691 | } |
| 692 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 693 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 694 | { |
| 695 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 696 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 697 | (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 698 | } |
| 699 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 700 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 701 | { |
| 702 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 703 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 704 | (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 705 | } |
| 706 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 707 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 708 | { |
| 709 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 710 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 711 | } |
| 712 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 713 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 714 | { |
| 715 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 716 | INTR_INFO_VALID_MASK)) == |
| 717 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 718 | } |
| 719 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 720 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 721 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 722 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 723 | } |
| 724 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 725 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 726 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 727 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 728 | } |
| 729 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 730 | static inline bool vm_need_tpr_shadow(struct kvm *kvm) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 731 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 732 | return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 733 | } |
| 734 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 735 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 736 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 737 | return vmcs_config.cpu_based_exec_ctrl & |
| 738 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 739 | } |
| 740 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 741 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 742 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 743 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 744 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 745 | } |
| 746 | |
| 747 | static inline bool cpu_has_vmx_flexpriority(void) |
| 748 | { |
| 749 | return cpu_has_vmx_tpr_shadow() && |
| 750 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 751 | } |
| 752 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 753 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 754 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 755 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | static inline bool cpu_has_vmx_eptp_uncacheable(void) |
| 759 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 760 | return vmx_capability.ept & VMX_EPTP_UC_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | static inline bool cpu_has_vmx_eptp_writeback(void) |
| 764 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 765 | return vmx_capability.ept & VMX_EPTP_WB_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 769 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 770 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 771 | } |
| 772 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 773 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 774 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 775 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 776 | } |
| 777 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 778 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 779 | { |
| 780 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 781 | } |
| 782 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 783 | static inline bool cpu_has_vmx_invept_individual_addr(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 784 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 785 | return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 786 | } |
| 787 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 788 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 789 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 790 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 791 | } |
| 792 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 793 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 794 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 795 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 796 | } |
| 797 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 798 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 799 | { |
| 800 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 801 | } |
| 802 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 803 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 804 | { |
| 805 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 806 | } |
| 807 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 808 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 809 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 810 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 811 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 812 | } |
| 813 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 814 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 815 | { |
| 816 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 817 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 818 | } |
| 819 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 820 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 821 | { |
| 822 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 823 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 824 | } |
| 825 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 826 | static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 827 | { |
Gui Jianfeng | 6d3e435 | 2010-01-29 15:36:59 +0800 | [diff] [blame] | 828 | return flexpriority_enabled && irqchip_in_kernel(kvm); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 829 | } |
| 830 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 831 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 832 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 833 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 834 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 835 | } |
| 836 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 837 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 838 | { |
| 839 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 840 | SECONDARY_EXEC_RDTSCP; |
| 841 | } |
| 842 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 843 | static inline bool cpu_has_virtual_nmis(void) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 844 | { |
| 845 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 846 | } |
| 847 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 848 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 849 | { |
| 850 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 851 | SECONDARY_EXEC_WBINVD_EXITING; |
| 852 | } |
| 853 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 854 | static inline bool report_flexpriority(void) |
| 855 | { |
| 856 | return flexpriority_enabled; |
| 857 | } |
| 858 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 859 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 860 | { |
| 861 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 862 | } |
| 863 | |
| 864 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 865 | { |
| 866 | return (vmcs12->cpu_based_vm_exec_control & |
| 867 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 868 | (vmcs12->secondary_vm_exec_control & bit); |
| 869 | } |
| 870 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 871 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12, |
| 872 | struct kvm_vcpu *vcpu) |
| 873 | { |
| 874 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 875 | } |
| 876 | |
| 877 | static inline bool is_exception(u32 intr_info) |
| 878 | { |
| 879 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 880 | == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK); |
| 881 | } |
| 882 | |
| 883 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 884 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 885 | struct vmcs12 *vmcs12, |
| 886 | u32 reason, unsigned long qualification); |
| 887 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 888 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 889 | { |
| 890 | int i; |
| 891 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 892 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 893 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 894 | return i; |
| 895 | return -1; |
| 896 | } |
| 897 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 898 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 899 | { |
| 900 | struct { |
| 901 | u64 vpid : 16; |
| 902 | u64 rsvd : 48; |
| 903 | u64 gva; |
| 904 | } operand = { vpid, 0, gva }; |
| 905 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 906 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 907 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 908 | "; ja 1f ; ud2 ; 1:" |
| 909 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 910 | } |
| 911 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 912 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 913 | { |
| 914 | struct { |
| 915 | u64 eptp, gpa; |
| 916 | } operand = {eptp, gpa}; |
| 917 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 918 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 919 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 920 | "; ja 1f ; ud2 ; 1:\n" |
| 921 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 922 | } |
| 923 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 924 | 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] | 925 | { |
| 926 | int i; |
| 927 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 928 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 929 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 930 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 931 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 932 | } |
| 933 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 934 | static void vmcs_clear(struct vmcs *vmcs) |
| 935 | { |
| 936 | u64 phys_addr = __pa(vmcs); |
| 937 | u8 error; |
| 938 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 939 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 940 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 941 | : "cc", "memory"); |
| 942 | if (error) |
| 943 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 944 | vmcs, phys_addr); |
| 945 | } |
| 946 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 947 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 948 | { |
| 949 | vmcs_clear(loaded_vmcs->vmcs); |
| 950 | loaded_vmcs->cpu = -1; |
| 951 | loaded_vmcs->launched = 0; |
| 952 | } |
| 953 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 954 | static void vmcs_load(struct vmcs *vmcs) |
| 955 | { |
| 956 | u64 phys_addr = __pa(vmcs); |
| 957 | u8 error; |
| 958 | |
| 959 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 960 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 961 | : "cc", "memory"); |
| 962 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 963 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 964 | vmcs, phys_addr); |
| 965 | } |
| 966 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 967 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 968 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 969 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 970 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 971 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 972 | if (loaded_vmcs->cpu != cpu) |
| 973 | return; /* vcpu migration can race with cpu offline */ |
| 974 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 975 | per_cpu(current_vmcs, cpu) = NULL; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 976 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
| 977 | loaded_vmcs_init(loaded_vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 978 | } |
| 979 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 980 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 981 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 982 | if (loaded_vmcs->cpu != -1) |
| 983 | smp_call_function_single( |
| 984 | loaded_vmcs->cpu, __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 985 | } |
| 986 | |
Gui Jianfeng | 1760dd4 | 2010-06-07 10:33:27 +0800 | [diff] [blame] | 987 | static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 988 | { |
| 989 | if (vmx->vpid == 0) |
| 990 | return; |
| 991 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 992 | if (cpu_has_vmx_invvpid_single()) |
| 993 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 994 | } |
| 995 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 996 | static inline void vpid_sync_vcpu_global(void) |
| 997 | { |
| 998 | if (cpu_has_vmx_invvpid_global()) |
| 999 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 1000 | } |
| 1001 | |
| 1002 | static inline void vpid_sync_context(struct vcpu_vmx *vmx) |
| 1003 | { |
| 1004 | if (cpu_has_vmx_invvpid_single()) |
Gui Jianfeng | 1760dd4 | 2010-06-07 10:33:27 +0800 | [diff] [blame] | 1005 | vpid_sync_vcpu_single(vmx); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1006 | else |
| 1007 | vpid_sync_vcpu_global(); |
| 1008 | } |
| 1009 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1010 | static inline void ept_sync_global(void) |
| 1011 | { |
| 1012 | if (cpu_has_vmx_invept_global()) |
| 1013 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
| 1014 | } |
| 1015 | |
| 1016 | static inline void ept_sync_context(u64 eptp) |
| 1017 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1018 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1019 | if (cpu_has_vmx_invept_context()) |
| 1020 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 1021 | else |
| 1022 | ept_sync_global(); |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa) |
| 1027 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1028 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1029 | if (cpu_has_vmx_invept_individual_addr()) |
| 1030 | __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR, |
| 1031 | eptp, gpa); |
| 1032 | else |
| 1033 | ept_sync_context(eptp); |
| 1034 | } |
| 1035 | } |
| 1036 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1037 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1038 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1039 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1040 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1041 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 1042 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1043 | return value; |
| 1044 | } |
| 1045 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1046 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1047 | { |
| 1048 | return vmcs_readl(field); |
| 1049 | } |
| 1050 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1051 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1052 | { |
| 1053 | return vmcs_readl(field); |
| 1054 | } |
| 1055 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1056 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1057 | { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1058 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1059 | return vmcs_readl(field); |
| 1060 | #else |
| 1061 | return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32); |
| 1062 | #endif |
| 1063 | } |
| 1064 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1065 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 1066 | { |
| 1067 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 1068 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 1069 | dump_stack(); |
| 1070 | } |
| 1071 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1072 | static void vmcs_writel(unsigned long field, unsigned long value) |
| 1073 | { |
| 1074 | u8 error; |
| 1075 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1076 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1077 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1078 | if (unlikely(error)) |
| 1079 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | static void vmcs_write16(unsigned long field, u16 value) |
| 1083 | { |
| 1084 | vmcs_writel(field, value); |
| 1085 | } |
| 1086 | |
| 1087 | static void vmcs_write32(unsigned long field, u32 value) |
| 1088 | { |
| 1089 | vmcs_writel(field, value); |
| 1090 | } |
| 1091 | |
| 1092 | static void vmcs_write64(unsigned long field, u64 value) |
| 1093 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1094 | vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 1095 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1096 | asm volatile (""); |
| 1097 | vmcs_writel(field+1, value >> 32); |
| 1098 | #endif |
| 1099 | } |
| 1100 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1101 | static void vmcs_clear_bits(unsigned long field, u32 mask) |
| 1102 | { |
| 1103 | vmcs_writel(field, vmcs_readl(field) & ~mask); |
| 1104 | } |
| 1105 | |
| 1106 | static void vmcs_set_bits(unsigned long field, u32 mask) |
| 1107 | { |
| 1108 | vmcs_writel(field, vmcs_readl(field) | mask); |
| 1109 | } |
| 1110 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1111 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 1112 | { |
| 1113 | vmx->segment_cache.bitmask = 0; |
| 1114 | } |
| 1115 | |
| 1116 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 1117 | unsigned field) |
| 1118 | { |
| 1119 | bool ret; |
| 1120 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 1121 | |
| 1122 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 1123 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 1124 | vmx->segment_cache.bitmask = 0; |
| 1125 | } |
| 1126 | ret = vmx->segment_cache.bitmask & mask; |
| 1127 | vmx->segment_cache.bitmask |= mask; |
| 1128 | return ret; |
| 1129 | } |
| 1130 | |
| 1131 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 1132 | { |
| 1133 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 1134 | |
| 1135 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 1136 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 1137 | return *p; |
| 1138 | } |
| 1139 | |
| 1140 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 1141 | { |
| 1142 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 1143 | |
| 1144 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 1145 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 1146 | return *p; |
| 1147 | } |
| 1148 | |
| 1149 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 1150 | { |
| 1151 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 1152 | |
| 1153 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 1154 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 1155 | return *p; |
| 1156 | } |
| 1157 | |
| 1158 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 1159 | { |
| 1160 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 1161 | |
| 1162 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 1163 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 1164 | return *p; |
| 1165 | } |
| 1166 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1167 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 1168 | { |
| 1169 | u32 eb; |
| 1170 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1171 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
| 1172 | (1u << NM_VECTOR) | (1u << DB_VECTOR); |
| 1173 | if ((vcpu->guest_debug & |
| 1174 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 1175 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 1176 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1177 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1178 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1179 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1180 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1181 | if (vcpu->fpu_active) |
| 1182 | eb &= ~(1u << NM_VECTOR); |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1183 | |
| 1184 | /* When we are running a nested L2 guest and L1 specified for it a |
| 1185 | * certain exception bitmap, we must trap the same exceptions and pass |
| 1186 | * them to L1. When running L2, we will only handle the exceptions |
| 1187 | * specified above if L1 did not want them. |
| 1188 | */ |
| 1189 | if (is_guest_mode(vcpu)) |
| 1190 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 1191 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1192 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 1193 | } |
| 1194 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame^] | 1195 | static void clear_atomic_switch_msr_special(unsigned long entry, |
| 1196 | unsigned long exit) |
| 1197 | { |
| 1198 | vmcs_clear_bits(VM_ENTRY_CONTROLS, entry); |
| 1199 | vmcs_clear_bits(VM_EXIT_CONTROLS, exit); |
| 1200 | } |
| 1201 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1202 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 1203 | { |
| 1204 | unsigned i; |
| 1205 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1206 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame^] | 1207 | switch (msr) { |
| 1208 | case MSR_EFER: |
| 1209 | if (cpu_has_load_ia32_efer) { |
| 1210 | clear_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER, |
| 1211 | VM_EXIT_LOAD_IA32_EFER); |
| 1212 | return; |
| 1213 | } |
| 1214 | break; |
| 1215 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1216 | if (cpu_has_load_perf_global_ctrl) { |
| 1217 | clear_atomic_switch_msr_special( |
| 1218 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1219 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 1220 | return; |
| 1221 | } |
| 1222 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1223 | } |
| 1224 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1225 | for (i = 0; i < m->nr; ++i) |
| 1226 | if (m->guest[i].index == msr) |
| 1227 | break; |
| 1228 | |
| 1229 | if (i == m->nr) |
| 1230 | return; |
| 1231 | --m->nr; |
| 1232 | m->guest[i] = m->guest[m->nr]; |
| 1233 | m->host[i] = m->host[m->nr]; |
| 1234 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1235 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1236 | } |
| 1237 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame^] | 1238 | static void add_atomic_switch_msr_special(unsigned long entry, |
| 1239 | unsigned long exit, unsigned long guest_val_vmcs, |
| 1240 | unsigned long host_val_vmcs, u64 guest_val, u64 host_val) |
| 1241 | { |
| 1242 | vmcs_write64(guest_val_vmcs, guest_val); |
| 1243 | vmcs_write64(host_val_vmcs, host_val); |
| 1244 | vmcs_set_bits(VM_ENTRY_CONTROLS, entry); |
| 1245 | vmcs_set_bits(VM_EXIT_CONTROLS, exit); |
| 1246 | } |
| 1247 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1248 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 1249 | u64 guest_val, u64 host_val) |
| 1250 | { |
| 1251 | unsigned i; |
| 1252 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1253 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame^] | 1254 | switch (msr) { |
| 1255 | case MSR_EFER: |
| 1256 | if (cpu_has_load_ia32_efer) { |
| 1257 | add_atomic_switch_msr_special(VM_ENTRY_LOAD_IA32_EFER, |
| 1258 | VM_EXIT_LOAD_IA32_EFER, |
| 1259 | GUEST_IA32_EFER, |
| 1260 | HOST_IA32_EFER, |
| 1261 | guest_val, host_val); |
| 1262 | return; |
| 1263 | } |
| 1264 | break; |
| 1265 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1266 | if (cpu_has_load_perf_global_ctrl) { |
| 1267 | add_atomic_switch_msr_special( |
| 1268 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1269 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1270 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 1271 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 1272 | guest_val, host_val); |
| 1273 | return; |
| 1274 | } |
| 1275 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1276 | } |
| 1277 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1278 | for (i = 0; i < m->nr; ++i) |
| 1279 | if (m->guest[i].index == msr) |
| 1280 | break; |
| 1281 | |
| 1282 | if (i == m->nr) { |
| 1283 | ++m->nr; |
| 1284 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1285 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1286 | } |
| 1287 | |
| 1288 | m->guest[i].index = msr; |
| 1289 | m->guest[i].value = guest_val; |
| 1290 | m->host[i].index = msr; |
| 1291 | m->host[i].value = host_val; |
| 1292 | } |
| 1293 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1294 | static void reload_tss(void) |
| 1295 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1296 | /* |
| 1297 | * VT restores TR but not its size. Useless. |
| 1298 | */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1299 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 1300 | struct desc_struct *descs; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1301 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1302 | descs = (void *)gdt->address; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1303 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 1304 | load_TR_desc(); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1305 | } |
| 1306 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1307 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1308 | { |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 1309 | u64 guest_efer; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1310 | u64 ignore_bits; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1311 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1312 | guest_efer = vmx->vcpu.arch.efer; |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 1313 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1314 | /* |
| 1315 | * NX is emulated; LMA and LME handled by hardware; SCE meaninless |
| 1316 | * outside long mode |
| 1317 | */ |
| 1318 | ignore_bits = EFER_NX | EFER_SCE; |
| 1319 | #ifdef CONFIG_X86_64 |
| 1320 | ignore_bits |= EFER_LMA | EFER_LME; |
| 1321 | /* SCE is meaningful only in long mode on Intel */ |
| 1322 | if (guest_efer & EFER_LMA) |
| 1323 | ignore_bits &= ~(u64)EFER_SCE; |
| 1324 | #endif |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1325 | guest_efer &= ~ignore_bits; |
| 1326 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1327 | vmx->guest_msrs[efer_offset].data = guest_efer; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 1328 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 1329 | |
| 1330 | clear_atomic_switch_msr(vmx, MSR_EFER); |
| 1331 | /* On ept, can't emulate nx, and must switch nx atomically */ |
| 1332 | if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) { |
| 1333 | guest_efer = vmx->vcpu.arch.efer; |
| 1334 | if (!(guest_efer & EFER_LMA)) |
| 1335 | guest_efer &= ~EFER_LME; |
| 1336 | add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer); |
| 1337 | return false; |
| 1338 | } |
| 1339 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1340 | return true; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1341 | } |
| 1342 | |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1343 | static unsigned long segment_base(u16 selector) |
| 1344 | { |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1345 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1346 | struct desc_struct *d; |
| 1347 | unsigned long table_base; |
| 1348 | unsigned long v; |
| 1349 | |
| 1350 | if (!(selector & ~3)) |
| 1351 | return 0; |
| 1352 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1353 | table_base = gdt->address; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1354 | |
| 1355 | if (selector & 4) { /* from ldt */ |
| 1356 | u16 ldt_selector = kvm_read_ldt(); |
| 1357 | |
| 1358 | if (!(ldt_selector & ~3)) |
| 1359 | return 0; |
| 1360 | |
| 1361 | table_base = segment_base(ldt_selector); |
| 1362 | } |
| 1363 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
| 1364 | v = get_desc_base(d); |
| 1365 | #ifdef CONFIG_X86_64 |
| 1366 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 1367 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; |
| 1368 | #endif |
| 1369 | return v; |
| 1370 | } |
| 1371 | |
| 1372 | static inline unsigned long kvm_read_tr_base(void) |
| 1373 | { |
| 1374 | u16 tr; |
| 1375 | asm("str %0" : "=g"(tr)); |
| 1376 | return segment_base(tr); |
| 1377 | } |
| 1378 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1379 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1380 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1381 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1382 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1383 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1384 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1385 | return; |
| 1386 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1387 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1388 | /* |
| 1389 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 1390 | * allow segment selectors with cpl > 0 or ti == 1. |
| 1391 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1392 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1393 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1394 | savesegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1395 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1396 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1397 | vmx->host_state.fs_reload_needed = 0; |
| 1398 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1399 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1400 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1401 | } |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1402 | savesegment(gs, vmx->host_state.gs_sel); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1403 | if (!(vmx->host_state.gs_sel & 7)) |
| 1404 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1405 | else { |
| 1406 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1407 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | #ifdef CONFIG_X86_64 |
| 1411 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 1412 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 1413 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1414 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 1415 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1416 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 1417 | |
| 1418 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1419 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
| 1420 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1421 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 1422 | #endif |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1423 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 1424 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 1425 | vmx->guest_msrs[i].data, |
| 1426 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1427 | } |
| 1428 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1429 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1430 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1431 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1432 | return; |
| 1433 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 1434 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1435 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1436 | #ifdef CONFIG_X86_64 |
| 1437 | if (is_long_mode(&vmx->vcpu)) |
| 1438 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 1439 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1440 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1441 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1442 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1443 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1444 | #else |
| 1445 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1446 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1447 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 1448 | if (vmx->host_state.fs_reload_needed) |
| 1449 | loadsegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1450 | reload_tss(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1451 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1452 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1453 | #endif |
Avi Kivity | 1c11e71 | 2010-05-03 16:05:44 +0300 | [diff] [blame] | 1454 | if (current_thread_info()->status & TS_USEDFPU) |
| 1455 | clts(); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 1456 | load_gdt(&__get_cpu_var(host_gdt)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1457 | } |
| 1458 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1459 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 1460 | { |
| 1461 | preempt_disable(); |
| 1462 | __vmx_load_host_state(vmx); |
| 1463 | preempt_enable(); |
| 1464 | } |
| 1465 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1466 | /* |
| 1467 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 1468 | * vcpu mutex is already taken. |
| 1469 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1470 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1471 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1472 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1473 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1474 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1475 | if (!vmm_exclusive) |
| 1476 | kvm_cpu_vmxon(phys_addr); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1477 | else if (vmx->loaded_vmcs->cpu != cpu) |
| 1478 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1479 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1480 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 1481 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 1482 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1483 | } |
| 1484 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1485 | if (vmx->loaded_vmcs->cpu != cpu) { |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1486 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1487 | unsigned long sysenter_esp; |
| 1488 | |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 1489 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1490 | local_irq_disable(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1491 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 1492 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1493 | local_irq_enable(); |
| 1494 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1495 | /* |
| 1496 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 1497 | * processors. |
| 1498 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1499 | vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1500 | vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1501 | |
| 1502 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 1503 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1504 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1505 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 1509 | { |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1510 | __vmx_load_host_state(to_vmx(vcpu)); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1511 | if (!vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1512 | __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs); |
| 1513 | vcpu->cpu = -1; |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1514 | kvm_cpu_vmxoff(); |
| 1515 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1516 | } |
| 1517 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1518 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 1519 | { |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1520 | ulong cr0; |
| 1521 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1522 | if (vcpu->fpu_active) |
| 1523 | return; |
| 1524 | vcpu->fpu_active = 1; |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1525 | cr0 = vmcs_readl(GUEST_CR0); |
| 1526 | cr0 &= ~(X86_CR0_TS | X86_CR0_MP); |
| 1527 | cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP); |
| 1528 | vmcs_writel(GUEST_CR0, cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1529 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1530 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1531 | if (is_guest_mode(vcpu)) |
| 1532 | vcpu->arch.cr0_guest_owned_bits &= |
| 1533 | ~get_vmcs12(vcpu)->cr0_guest_host_mask; |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1534 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1535 | } |
| 1536 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1537 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 1538 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1539 | /* |
| 1540 | * Return the cr0 value that a nested guest would read. This is a combination |
| 1541 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 1542 | * its hypervisor (cr0_read_shadow). |
| 1543 | */ |
| 1544 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 1545 | { |
| 1546 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 1547 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 1548 | } |
| 1549 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 1550 | { |
| 1551 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 1552 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 1553 | } |
| 1554 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1555 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 1556 | { |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1557 | /* Note that there is no vcpu->fpu_active = 0 here. The caller must |
| 1558 | * set this *before* calling this function. |
| 1559 | */ |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1560 | vmx_decache_cr0_guest_bits(vcpu); |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1561 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1562 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1563 | vcpu->arch.cr0_guest_owned_bits = 0; |
| 1564 | 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] | 1565 | if (is_guest_mode(vcpu)) { |
| 1566 | /* |
| 1567 | * L1's specified read shadow might not contain the TS bit, |
| 1568 | * so now that we turned on shadowing of this bit, we need to |
| 1569 | * set this bit of the shadow. Like in nested_vmx_run we need |
| 1570 | * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet |
| 1571 | * up-to-date here because we just decached cr0.TS (and we'll |
| 1572 | * only update vmcs12->guest_cr0 on nested exit). |
| 1573 | */ |
| 1574 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1575 | vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) | |
| 1576 | (vcpu->arch.cr0 & X86_CR0_TS); |
| 1577 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 1578 | } else |
| 1579 | vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1580 | } |
| 1581 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1582 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 1583 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1584 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 1585 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1586 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 1587 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 1588 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 1589 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1590 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1591 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 1592 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1593 | } |
| 1594 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1595 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1596 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 1600 | { |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1601 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 1602 | __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1603 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1604 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1605 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1606 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1607 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1608 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 1609 | } |
| 1610 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1611 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 1612 | { |
| 1613 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 1614 | int ret = 0; |
| 1615 | |
| 1616 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1617 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1618 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1619 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1620 | |
| 1621 | return ret & mask; |
| 1622 | } |
| 1623 | |
| 1624 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 1625 | { |
| 1626 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 1627 | u32 interruptibility = interruptibility_old; |
| 1628 | |
| 1629 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 1630 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1631 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1632 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1633 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1634 | interruptibility |= GUEST_INTR_STATE_STI; |
| 1635 | |
| 1636 | if ((interruptibility != interruptibility_old)) |
| 1637 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 1638 | } |
| 1639 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1640 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 1641 | { |
| 1642 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1643 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1644 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1645 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1646 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1647 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1648 | /* skipping an emulated instruction also counts */ |
| 1649 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1650 | } |
| 1651 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 1652 | static void vmx_clear_hlt(struct kvm_vcpu *vcpu) |
| 1653 | { |
| 1654 | /* Ensure that we clear the HLT state in the VMCS. We don't need to |
| 1655 | * explicitly skip the instruction because if the HLT state is set, then |
| 1656 | * the instruction is already executing and RIP has already been |
| 1657 | * advanced. */ |
| 1658 | if (!yield_on_hlt && |
| 1659 | vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) |
| 1660 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
| 1661 | } |
| 1662 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 1663 | /* |
| 1664 | * KVM wants to inject page-faults which it got to the guest. This function |
| 1665 | * checks whether in a nested guest, we need to inject them to L1 or L2. |
| 1666 | * This function assumes it is called with the exit reason in vmcs02 being |
| 1667 | * a #PF exception (this is the only case in which KVM injects a #PF when L2 |
| 1668 | * is running). |
| 1669 | */ |
| 1670 | static int nested_pf_handled(struct kvm_vcpu *vcpu) |
| 1671 | { |
| 1672 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1673 | |
| 1674 | /* TODO: also check PFEC_MATCH/MASK, not just EB.PF. */ |
| 1675 | if (!(vmcs12->exception_bitmap & PF_VECTOR)) |
| 1676 | return 0; |
| 1677 | |
| 1678 | nested_vmx_vmexit(vcpu); |
| 1679 | return 1; |
| 1680 | } |
| 1681 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1682 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 1683 | bool has_error_code, u32 error_code, |
| 1684 | bool reinject) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1685 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1686 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1687 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1688 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 1689 | if (nr == PF_VECTOR && is_guest_mode(vcpu) && |
| 1690 | nested_pf_handled(vcpu)) |
| 1691 | return; |
| 1692 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1693 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1694 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1695 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 1696 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1697 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1698 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 1699 | int inc_eip = 0; |
| 1700 | if (kvm_exception_is_soft(nr)) |
| 1701 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 1702 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 1703 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1704 | return; |
| 1705 | } |
| 1706 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 1707 | if (kvm_exception_is_soft(nr)) { |
| 1708 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 1709 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1710 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 1711 | } else |
| 1712 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 1713 | |
| 1714 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 1715 | vmx_clear_hlt(vcpu); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1716 | } |
| 1717 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1718 | static bool vmx_rdtscp_supported(void) |
| 1719 | { |
| 1720 | return cpu_has_vmx_rdtscp(); |
| 1721 | } |
| 1722 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1723 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1724 | * Swap MSR entry in host/guest MSR entry array. |
| 1725 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1726 | 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] | 1727 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1728 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1729 | |
| 1730 | tmp = vmx->guest_msrs[to]; |
| 1731 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 1732 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1736 | * Set up the vmcs to automatically save and restore system |
| 1737 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 1738 | * mode, as fiddling with msrs is very expensive. |
| 1739 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1740 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1741 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1742 | int save_nmsrs, index; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 1743 | unsigned long *msr_bitmap; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1744 | |
Avi Kivity | 33f9c50 | 2008-02-27 16:06:57 +0200 | [diff] [blame] | 1745 | vmx_load_host_state(vmx); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1746 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1747 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1748 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1749 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1750 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1751 | move_msr_up(vmx, index, save_nmsrs++); |
| 1752 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1753 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1754 | move_msr_up(vmx, index, save_nmsrs++); |
| 1755 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1756 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1757 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1758 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
| 1759 | if (index >= 0 && vmx->rdtscp_enabled) |
| 1760 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1761 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1762 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1763 | * if efer.sce is enabled. |
| 1764 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1765 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1766 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1767 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1768 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1769 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1770 | index = __find_msr_index(vmx, MSR_EFER); |
| 1771 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1772 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1773 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1774 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 1775 | |
| 1776 | if (cpu_has_vmx_msr_bitmap()) { |
| 1777 | if (is_long_mode(&vmx->vcpu)) |
| 1778 | msr_bitmap = vmx_msr_bitmap_longmode; |
| 1779 | else |
| 1780 | msr_bitmap = vmx_msr_bitmap_legacy; |
| 1781 | |
| 1782 | vmcs_write64(MSR_BITMAP, __pa(msr_bitmap)); |
| 1783 | } |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1787 | * reads and returns guest's timestamp counter "register" |
| 1788 | * guest_tsc = host_tsc + tsc_offset -- 21.3 |
| 1789 | */ |
| 1790 | static u64 guest_read_tsc(void) |
| 1791 | { |
| 1792 | u64 host_tsc, tsc_offset; |
| 1793 | |
| 1794 | rdtscll(host_tsc); |
| 1795 | tsc_offset = vmcs_read64(TSC_OFFSET); |
| 1796 | return host_tsc + tsc_offset; |
| 1797 | } |
| 1798 | |
| 1799 | /* |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 1800 | * Like guest_read_tsc, but always returns L1's notion of the timestamp |
| 1801 | * counter, even if a nested guest (L2) is currently running. |
| 1802 | */ |
| 1803 | u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu) |
| 1804 | { |
| 1805 | u64 host_tsc, tsc_offset; |
| 1806 | |
| 1807 | rdtscll(host_tsc); |
| 1808 | tsc_offset = is_guest_mode(vcpu) ? |
| 1809 | to_vmx(vcpu)->nested.vmcs01_tsc_offset : |
| 1810 | vmcs_read64(TSC_OFFSET); |
| 1811 | return host_tsc + tsc_offset; |
| 1812 | } |
| 1813 | |
| 1814 | /* |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 1815 | * Empty call-back. Needs to be implemented when VMX enables the SET_TSC_KHZ |
| 1816 | * ioctl. In this case the call-back should update internal vmx state to make |
| 1817 | * the changes effective. |
| 1818 | */ |
| 1819 | static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz) |
| 1820 | { |
| 1821 | /* Nothing to do here */ |
| 1822 | } |
| 1823 | |
| 1824 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1825 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1826 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1827 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1828 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 1829 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 1830 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 1831 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 1832 | * to the spec, this should set L1's TSC; The offset that L1 |
| 1833 | * set for L2 remains unchanged, and still needs to be added |
| 1834 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 1835 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 1836 | struct vmcs12 *vmcs12; |
| 1837 | to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset; |
| 1838 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 1839 | vmcs12 = get_vmcs12(vcpu); |
| 1840 | vmcs_write64(TSC_OFFSET, offset + |
| 1841 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 1842 | vmcs12->tsc_offset : 0)); |
| 1843 | } else { |
| 1844 | vmcs_write64(TSC_OFFSET, offset); |
| 1845 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1846 | } |
| 1847 | |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 1848 | static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment) |
| 1849 | { |
| 1850 | u64 offset = vmcs_read64(TSC_OFFSET); |
| 1851 | vmcs_write64(TSC_OFFSET, offset + adjustment); |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 1852 | if (is_guest_mode(vcpu)) { |
| 1853 | /* Even when running L2, the adjustment needs to apply to L1 */ |
| 1854 | to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment; |
| 1855 | } |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 1856 | } |
| 1857 | |
Joerg Roedel | 857e409 | 2011-03-25 09:44:50 +0100 | [diff] [blame] | 1858 | static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) |
| 1859 | { |
| 1860 | return target_tsc - native_read_tsc(); |
| 1861 | } |
| 1862 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 1863 | static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu) |
| 1864 | { |
| 1865 | struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 1866 | return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31))); |
| 1867 | } |
| 1868 | |
| 1869 | /* |
| 1870 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 1871 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 1872 | * all guests if the "nested" module option is off, and can also be disabled |
| 1873 | * for a single guest by disabling its VMX cpuid bit. |
| 1874 | */ |
| 1875 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 1876 | { |
| 1877 | return nested && guest_cpuid_has_vmx(vcpu); |
| 1878 | } |
| 1879 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1880 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 1881 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 1882 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 1883 | * The same values should also be used to verify that vmcs12 control fields are |
| 1884 | * valid during nested entry from L1 to L2. |
| 1885 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 1886 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 1887 | * bit in the high half is on if the corresponding bit in the control field |
| 1888 | * may be on. See also vmx_control_verify(). |
| 1889 | * TODO: allow these variables to be modified (downgraded) by module options |
| 1890 | * or other means. |
| 1891 | */ |
| 1892 | static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high; |
| 1893 | static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high; |
| 1894 | static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high; |
| 1895 | static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high; |
| 1896 | static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high; |
| 1897 | static __init void nested_vmx_setup_ctls_msrs(void) |
| 1898 | { |
| 1899 | /* |
| 1900 | * Note that as a general rule, the high half of the MSRs (bits in |
| 1901 | * the control fields which may be 1) should be initialized by the |
| 1902 | * intersection of the underlying hardware's MSR (i.e., features which |
| 1903 | * can be supported) and the list of features we want to expose - |
| 1904 | * because they are known to be properly supported in our code. |
| 1905 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 1906 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 1907 | * reason is that if one of these bits is necessary, it will appear |
| 1908 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 1909 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
| 1910 | * nested_vmx_exit_handled() will not pass related exits to L1. |
| 1911 | * These rules have exceptions below. |
| 1912 | */ |
| 1913 | |
| 1914 | /* pin-based controls */ |
| 1915 | /* |
| 1916 | * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is |
| 1917 | * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR. |
| 1918 | */ |
| 1919 | nested_vmx_pinbased_ctls_low = 0x16 ; |
| 1920 | nested_vmx_pinbased_ctls_high = 0x16 | |
| 1921 | PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING | |
| 1922 | PIN_BASED_VIRTUAL_NMIS; |
| 1923 | |
| 1924 | /* exit controls */ |
| 1925 | nested_vmx_exit_ctls_low = 0; |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 1926 | /* 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] | 1927 | #ifdef CONFIG_X86_64 |
| 1928 | nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 1929 | #else |
| 1930 | nested_vmx_exit_ctls_high = 0; |
| 1931 | #endif |
| 1932 | |
| 1933 | /* entry controls */ |
| 1934 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
| 1935 | nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high); |
| 1936 | nested_vmx_entry_ctls_low = 0; |
| 1937 | nested_vmx_entry_ctls_high &= |
| 1938 | VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE; |
| 1939 | |
| 1940 | /* cpu-based controls */ |
| 1941 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
| 1942 | nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high); |
| 1943 | nested_vmx_procbased_ctls_low = 0; |
| 1944 | nested_vmx_procbased_ctls_high &= |
| 1945 | CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
| 1946 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 1947 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 1948 | CPU_BASED_CR3_STORE_EXITING | |
| 1949 | #ifdef CONFIG_X86_64 |
| 1950 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 1951 | #endif |
| 1952 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
| 1953 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING | |
| 1954 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
| 1955 | /* |
| 1956 | * We can allow some features even when not supported by the |
| 1957 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 1958 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 1959 | * without MSR bitmaps. |
| 1960 | */ |
| 1961 | nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS; |
| 1962 | |
| 1963 | /* secondary cpu-based controls */ |
| 1964 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
| 1965 | nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high); |
| 1966 | nested_vmx_secondary_ctls_low = 0; |
| 1967 | nested_vmx_secondary_ctls_high &= |
| 1968 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1969 | } |
| 1970 | |
| 1971 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 1972 | { |
| 1973 | /* |
| 1974 | * Bits 0 in high must be 0, and bits 1 in low must be 1. |
| 1975 | */ |
| 1976 | return ((control & high) | low) == control; |
| 1977 | } |
| 1978 | |
| 1979 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 1980 | { |
| 1981 | return low | ((u64)high << 32); |
| 1982 | } |
| 1983 | |
| 1984 | /* |
| 1985 | * If we allow our guest to use VMX instructions (i.e., nested VMX), we should |
| 1986 | * also let it use VMX-specific MSRs. |
| 1987 | * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a |
| 1988 | * VMX-specific MSR, or 0 when we haven't (and the caller should handle it |
| 1989 | * like all other MSRs). |
| 1990 | */ |
| 1991 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 1992 | { |
| 1993 | if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC && |
| 1994 | msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) { |
| 1995 | /* |
| 1996 | * According to the spec, processors which do not support VMX |
| 1997 | * should throw a #GP(0) when VMX capability MSRs are read. |
| 1998 | */ |
| 1999 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 2000 | return 1; |
| 2001 | } |
| 2002 | |
| 2003 | switch (msr_index) { |
| 2004 | case MSR_IA32_FEATURE_CONTROL: |
| 2005 | *pdata = 0; |
| 2006 | break; |
| 2007 | case MSR_IA32_VMX_BASIC: |
| 2008 | /* |
| 2009 | * This MSR reports some information about VMX support. We |
| 2010 | * should return information about the VMX we emulate for the |
| 2011 | * guest, and the VMCS structure we give it - not about the |
| 2012 | * VMX support of the underlying hardware. |
| 2013 | */ |
| 2014 | *pdata = VMCS12_REVISION | |
| 2015 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 2016 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 2017 | break; |
| 2018 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 2019 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 2020 | *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low, |
| 2021 | nested_vmx_pinbased_ctls_high); |
| 2022 | break; |
| 2023 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 2024 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 2025 | *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low, |
| 2026 | nested_vmx_procbased_ctls_high); |
| 2027 | break; |
| 2028 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 2029 | case MSR_IA32_VMX_EXIT_CTLS: |
| 2030 | *pdata = vmx_control_msr(nested_vmx_exit_ctls_low, |
| 2031 | nested_vmx_exit_ctls_high); |
| 2032 | break; |
| 2033 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 2034 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 2035 | *pdata = vmx_control_msr(nested_vmx_entry_ctls_low, |
| 2036 | nested_vmx_entry_ctls_high); |
| 2037 | break; |
| 2038 | case MSR_IA32_VMX_MISC: |
| 2039 | *pdata = 0; |
| 2040 | break; |
| 2041 | /* |
| 2042 | * These MSRs specify bits which the guest must keep fixed (on or off) |
| 2043 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 2044 | * We picked the standard core2 setting. |
| 2045 | */ |
| 2046 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 2047 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 2048 | case MSR_IA32_VMX_CR0_FIXED0: |
| 2049 | *pdata = VMXON_CR0_ALWAYSON; |
| 2050 | break; |
| 2051 | case MSR_IA32_VMX_CR0_FIXED1: |
| 2052 | *pdata = -1ULL; |
| 2053 | break; |
| 2054 | case MSR_IA32_VMX_CR4_FIXED0: |
| 2055 | *pdata = VMXON_CR4_ALWAYSON; |
| 2056 | break; |
| 2057 | case MSR_IA32_VMX_CR4_FIXED1: |
| 2058 | *pdata = -1ULL; |
| 2059 | break; |
| 2060 | case MSR_IA32_VMX_VMCS_ENUM: |
| 2061 | *pdata = 0x1f; |
| 2062 | break; |
| 2063 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 2064 | *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low, |
| 2065 | nested_vmx_secondary_ctls_high); |
| 2066 | break; |
| 2067 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 2068 | /* Currently, no nested ept or nested vpid */ |
| 2069 | *pdata = 0; |
| 2070 | break; |
| 2071 | default: |
| 2072 | return 0; |
| 2073 | } |
| 2074 | |
| 2075 | return 1; |
| 2076 | } |
| 2077 | |
| 2078 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 2079 | { |
| 2080 | if (!nested_vmx_allowed(vcpu)) |
| 2081 | return 0; |
| 2082 | |
| 2083 | if (msr_index == MSR_IA32_FEATURE_CONTROL) |
| 2084 | /* TODO: the right thing. */ |
| 2085 | return 1; |
| 2086 | /* |
| 2087 | * No need to treat VMX capability MSRs specially: If we don't handle |
| 2088 | * them, handle_wrmsr will #GP(0), which is correct (they are readonly) |
| 2089 | */ |
| 2090 | return 0; |
| 2091 | } |
| 2092 | |
| 2093 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2094 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 2095 | * Returns 0 on success, non-0 otherwise. |
| 2096 | * Assumes vcpu_load() was already called. |
| 2097 | */ |
| 2098 | static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 2099 | { |
| 2100 | u64 data; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2101 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2102 | |
| 2103 | if (!pdata) { |
| 2104 | printk(KERN_ERR "BUG: get_msr called with NULL pdata\n"); |
| 2105 | return -EINVAL; |
| 2106 | } |
| 2107 | |
| 2108 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2109 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2110 | case MSR_FS_BASE: |
| 2111 | data = vmcs_readl(GUEST_FS_BASE); |
| 2112 | break; |
| 2113 | case MSR_GS_BASE: |
| 2114 | data = vmcs_readl(GUEST_GS_BASE); |
| 2115 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2116 | case MSR_KERNEL_GS_BASE: |
| 2117 | vmx_load_host_state(to_vmx(vcpu)); |
| 2118 | data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
| 2119 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2120 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2121 | case MSR_EFER: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2122 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2123 | case MSR_IA32_TSC: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2124 | data = guest_read_tsc(); |
| 2125 | break; |
| 2126 | case MSR_IA32_SYSENTER_CS: |
| 2127 | data = vmcs_read32(GUEST_SYSENTER_CS); |
| 2128 | break; |
| 2129 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2130 | data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2131 | break; |
| 2132 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2133 | data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2134 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2135 | case MSR_TSC_AUX: |
| 2136 | if (!to_vmx(vcpu)->rdtscp_enabled) |
| 2137 | return 1; |
| 2138 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2139 | default: |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2140 | vmx_load_host_state(to_vmx(vcpu)); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2141 | if (vmx_get_vmx_msr(vcpu, msr_index, pdata)) |
| 2142 | return 0; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2143 | msr = find_msr_entry(to_vmx(vcpu), msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2144 | if (msr) { |
Gleb Natapov | 542423b | 2009-08-27 15:07:30 +0300 | [diff] [blame] | 2145 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2146 | data = msr->data; |
| 2147 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2148 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2149 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | *pdata = data; |
| 2153 | return 0; |
| 2154 | } |
| 2155 | |
| 2156 | /* |
| 2157 | * Writes msr value into into the appropriate "register". |
| 2158 | * Returns 0 on success, non-0 otherwise. |
| 2159 | * Assumes vcpu_load() was already called. |
| 2160 | */ |
| 2161 | static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 2162 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2163 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2164 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2165 | int ret = 0; |
| 2166 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2167 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2168 | case MSR_EFER: |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2169 | vmx_load_host_state(vmx); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2170 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2171 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 2172 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2173 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2174 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2175 | vmcs_writel(GUEST_FS_BASE, data); |
| 2176 | break; |
| 2177 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2178 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2179 | vmcs_writel(GUEST_GS_BASE, data); |
| 2180 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2181 | case MSR_KERNEL_GS_BASE: |
| 2182 | vmx_load_host_state(vmx); |
| 2183 | vmx->msr_guest_kernel_gs_base = data; |
| 2184 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2185 | #endif |
| 2186 | case MSR_IA32_SYSENTER_CS: |
| 2187 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 2188 | break; |
| 2189 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2190 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2191 | break; |
| 2192 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2193 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2194 | break; |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2195 | case MSR_IA32_TSC: |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2196 | kvm_write_tsc(vcpu, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2197 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2198 | case MSR_IA32_CR_PAT: |
| 2199 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 2200 | vmcs_write64(GUEST_IA32_PAT, data); |
| 2201 | vcpu->arch.pat = data; |
| 2202 | break; |
| 2203 | } |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2204 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
| 2205 | break; |
| 2206 | case MSR_TSC_AUX: |
| 2207 | if (!vmx->rdtscp_enabled) |
| 2208 | return 1; |
| 2209 | /* Check reserved bit, higher 32 bits should be zero */ |
| 2210 | if ((data >> 32) != 0) |
| 2211 | return 1; |
| 2212 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2213 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2214 | if (vmx_set_vmx_msr(vcpu, msr_index, data)) |
| 2215 | break; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2216 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2217 | if (msr) { |
Gleb Natapov | 542423b | 2009-08-27 15:07:30 +0300 | [diff] [blame] | 2218 | vmx_load_host_state(vmx); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2219 | msr->data = data; |
| 2220 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2221 | } |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2222 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2223 | } |
| 2224 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2225 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2226 | } |
| 2227 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2228 | 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] | 2229 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2230 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 2231 | switch (reg) { |
| 2232 | case VCPU_REGS_RSP: |
| 2233 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 2234 | break; |
| 2235 | case VCPU_REGS_RIP: |
| 2236 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 2237 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2238 | case VCPU_EXREG_PDPTR: |
| 2239 | if (enable_ept) |
| 2240 | ept_save_pdptrs(vcpu); |
| 2241 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2242 | default: |
| 2243 | break; |
| 2244 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2245 | } |
| 2246 | |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 2247 | 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] | 2248 | { |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2249 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 2250 | vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]); |
| 2251 | else |
| 2252 | vmcs_writel(GUEST_DR7, vcpu->arch.dr7); |
| 2253 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2254 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | static __init int cpu_has_kvm_support(void) |
| 2258 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 2259 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | static __init int vmx_disabled_by_bios(void) |
| 2263 | { |
| 2264 | u64 msr; |
| 2265 | |
| 2266 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2267 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2268 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2269 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 2270 | && tboot_enabled()) |
| 2271 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2272 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2273 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2274 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 2275 | && !tboot_enabled()) { |
| 2276 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2277 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2278 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 2279 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2280 | /* launched w/o TXT and VMX disabled */ |
| 2281 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 2282 | && !tboot_enabled()) |
| 2283 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2287 | } |
| 2288 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2289 | static void kvm_cpu_vmxon(u64 addr) |
| 2290 | { |
| 2291 | asm volatile (ASM_VMX_VMXON_RAX |
| 2292 | : : "a"(&addr), "m"(addr) |
| 2293 | : "memory", "cc"); |
| 2294 | } |
| 2295 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2296 | static int hardware_enable(void *garbage) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2297 | { |
| 2298 | int cpu = raw_smp_processor_id(); |
| 2299 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2300 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2301 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2302 | if (read_cr4() & X86_CR4_VMXE) |
| 2303 | return -EBUSY; |
| 2304 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2305 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2306 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2307 | |
| 2308 | test_bits = FEATURE_CONTROL_LOCKED; |
| 2309 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 2310 | if (tboot_enabled()) |
| 2311 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 2312 | |
| 2313 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2314 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2315 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 2316 | } |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2317 | write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */ |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2318 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2319 | if (vmm_exclusive) { |
| 2320 | kvm_cpu_vmxon(phys_addr); |
| 2321 | ept_sync_global(); |
| 2322 | } |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2323 | |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 2324 | store_gdt(&__get_cpu_var(host_gdt)); |
| 2325 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2326 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2327 | } |
| 2328 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2329 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2330 | { |
| 2331 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2332 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2333 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2334 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 2335 | loaded_vmcss_on_cpu_link) |
| 2336 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2337 | } |
| 2338 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 2339 | |
| 2340 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 2341 | * tricks. |
| 2342 | */ |
| 2343 | static void kvm_cpu_vmxoff(void) |
| 2344 | { |
| 2345 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 2346 | } |
| 2347 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2348 | static void hardware_disable(void *garbage) |
| 2349 | { |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2350 | if (vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2351 | vmclear_local_loaded_vmcss(); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2352 | kvm_cpu_vmxoff(); |
| 2353 | } |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2354 | write_cr4(read_cr4() & ~X86_CR4_VMXE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2355 | } |
| 2356 | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2357 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2358 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2359 | { |
| 2360 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2361 | u32 ctl = ctl_min | ctl_opt; |
| 2362 | |
| 2363 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 2364 | |
| 2365 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 2366 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 2367 | |
| 2368 | /* Ensure minimum (required) set of control bits are supported. */ |
| 2369 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2370 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2371 | |
| 2372 | *result = ctl; |
| 2373 | return 0; |
| 2374 | } |
| 2375 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2376 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 2377 | { |
| 2378 | u32 vmx_msr_low, vmx_msr_high; |
| 2379 | |
| 2380 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 2381 | return vmx_msr_high & ctl; |
| 2382 | } |
| 2383 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2384 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2385 | { |
| 2386 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2387 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2388 | u32 _pin_based_exec_control = 0; |
| 2389 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2390 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2391 | u32 _vmexit_control = 0; |
| 2392 | u32 _vmentry_control = 0; |
| 2393 | |
| 2394 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2395 | opt = PIN_BASED_VIRTUAL_NMIS; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2396 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 2397 | &_pin_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2398 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2399 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 2400 | min = |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2401 | #ifdef CONFIG_X86_64 |
| 2402 | CPU_BASED_CR8_LOAD_EXITING | |
| 2403 | CPU_BASED_CR8_STORE_EXITING | |
| 2404 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2405 | CPU_BASED_CR3_LOAD_EXITING | |
| 2406 | CPU_BASED_CR3_STORE_EXITING | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2407 | CPU_BASED_USE_IO_BITMAPS | |
| 2408 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2409 | CPU_BASED_USE_TSC_OFFSETING | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 2410 | CPU_BASED_MWAIT_EXITING | |
| 2411 | CPU_BASED_MONITOR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2412 | CPU_BASED_INVLPG_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 2413 | |
| 2414 | if (yield_on_hlt) |
| 2415 | min |= CPU_BASED_HLT_EXITING; |
| 2416 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2417 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2418 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2419 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2420 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 2421 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2422 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2423 | #ifdef CONFIG_X86_64 |
| 2424 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 2425 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 2426 | ~CPU_BASED_CR8_STORE_EXITING; |
| 2427 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2428 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2429 | min2 = 0; |
| 2430 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2431 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2432 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2433 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2434 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2435 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
| 2436 | SECONDARY_EXEC_RDTSCP; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2437 | if (adjust_vmx_controls(min2, opt2, |
| 2438 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2439 | &_cpu_based_2nd_exec_control) < 0) |
| 2440 | return -EIO; |
| 2441 | } |
| 2442 | #ifndef CONFIG_X86_64 |
| 2443 | if (!(_cpu_based_2nd_exec_control & |
| 2444 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 2445 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2446 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2447 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2448 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 2449 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 2450 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 2451 | CPU_BASED_CR3_STORE_EXITING | |
| 2452 | CPU_BASED_INVLPG_EXITING); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2453 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
| 2454 | vmx_capability.ept, vmx_capability.vpid); |
| 2455 | } |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2456 | |
| 2457 | min = 0; |
| 2458 | #ifdef CONFIG_X86_64 |
| 2459 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 2460 | #endif |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2461 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2462 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 2463 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2464 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2465 | |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2466 | min = 0; |
| 2467 | opt = VM_ENTRY_LOAD_IA32_PAT; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2468 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 2469 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2470 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2471 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 2472 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2473 | |
| 2474 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 2475 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2476 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2477 | |
| 2478 | #ifdef CONFIG_X86_64 |
| 2479 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 2480 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2481 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2482 | #endif |
| 2483 | |
| 2484 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 2485 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2486 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2487 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2488 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
| 2489 | vmcs_conf->order = get_order(vmcs_config.size); |
| 2490 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2491 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2492 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 2493 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2494 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2495 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 2496 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2497 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2498 | cpu_has_load_ia32_efer = |
| 2499 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 2500 | VM_ENTRY_LOAD_IA32_EFER) |
| 2501 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 2502 | VM_EXIT_LOAD_IA32_EFER); |
| 2503 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame^] | 2504 | cpu_has_load_perf_global_ctrl = |
| 2505 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 2506 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 2507 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 2508 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 2509 | |
| 2510 | /* |
| 2511 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
| 2512 | * but due to arrata below it can't be used. Workaround is to use |
| 2513 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 2514 | * |
| 2515 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 2516 | * |
| 2517 | * AAK155 (model 26) |
| 2518 | * AAP115 (model 30) |
| 2519 | * AAT100 (model 37) |
| 2520 | * BC86,AAY89,BD102 (model 44) |
| 2521 | * BA97 (model 46) |
| 2522 | * |
| 2523 | */ |
| 2524 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 2525 | switch (boot_cpu_data.x86_model) { |
| 2526 | case 26: |
| 2527 | case 30: |
| 2528 | case 37: |
| 2529 | case 44: |
| 2530 | case 46: |
| 2531 | cpu_has_load_perf_global_ctrl = false; |
| 2532 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 2533 | "does not work properly. Using workaround\n"); |
| 2534 | break; |
| 2535 | default: |
| 2536 | break; |
| 2537 | } |
| 2538 | } |
| 2539 | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2540 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 2541 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2542 | |
| 2543 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 2544 | { |
| 2545 | int node = cpu_to_node(cpu); |
| 2546 | struct page *pages; |
| 2547 | struct vmcs *vmcs; |
| 2548 | |
Mel Gorman | 6484eb3 | 2009-06-16 15:31:54 -0700 | [diff] [blame] | 2549 | pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2550 | if (!pages) |
| 2551 | return NULL; |
| 2552 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2553 | memset(vmcs, 0, vmcs_config.size); |
| 2554 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2555 | return vmcs; |
| 2556 | } |
| 2557 | |
| 2558 | static struct vmcs *alloc_vmcs(void) |
| 2559 | { |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2560 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | static void free_vmcs(struct vmcs *vmcs) |
| 2564 | { |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2565 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2566 | } |
| 2567 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2568 | /* |
| 2569 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 2570 | */ |
| 2571 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 2572 | { |
| 2573 | if (!loaded_vmcs->vmcs) |
| 2574 | return; |
| 2575 | loaded_vmcs_clear(loaded_vmcs); |
| 2576 | free_vmcs(loaded_vmcs->vmcs); |
| 2577 | loaded_vmcs->vmcs = NULL; |
| 2578 | } |
| 2579 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 2580 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2581 | { |
| 2582 | int cpu; |
| 2583 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2584 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2585 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2586 | per_cpu(vmxarea, cpu) = NULL; |
| 2587 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2588 | } |
| 2589 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2590 | static __init int alloc_kvm_area(void) |
| 2591 | { |
| 2592 | int cpu; |
| 2593 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2594 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2595 | struct vmcs *vmcs; |
| 2596 | |
| 2597 | vmcs = alloc_vmcs_cpu(cpu); |
| 2598 | if (!vmcs) { |
| 2599 | free_kvm_area(); |
| 2600 | return -ENOMEM; |
| 2601 | } |
| 2602 | |
| 2603 | per_cpu(vmxarea, cpu) = vmcs; |
| 2604 | } |
| 2605 | return 0; |
| 2606 | } |
| 2607 | |
| 2608 | static __init int hardware_setup(void) |
| 2609 | { |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2610 | if (setup_vmcs_config(&vmcs_config) < 0) |
| 2611 | return -EIO; |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 2612 | |
| 2613 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 2614 | kvm_enable_efer_bits(EFER_NX); |
| 2615 | |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 2616 | if (!cpu_has_vmx_vpid()) |
| 2617 | enable_vpid = 0; |
| 2618 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 2619 | if (!cpu_has_vmx_ept() || |
| 2620 | !cpu_has_vmx_ept_4levels()) { |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 2621 | enable_ept = 0; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2622 | enable_unrestricted_guest = 0; |
| 2623 | } |
| 2624 | |
| 2625 | if (!cpu_has_vmx_unrestricted_guest()) |
| 2626 | enable_unrestricted_guest = 0; |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 2627 | |
| 2628 | if (!cpu_has_vmx_flexpriority()) |
| 2629 | flexpriority_enabled = 0; |
| 2630 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2631 | if (!cpu_has_vmx_tpr_shadow()) |
| 2632 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 2633 | |
Marcelo Tosatti | 54dee99 | 2009-06-11 12:07:44 -0300 | [diff] [blame] | 2634 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 2635 | kvm_disable_largepages(); |
| 2636 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2637 | if (!cpu_has_vmx_ple()) |
| 2638 | ple_gap = 0; |
| 2639 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2640 | if (nested) |
| 2641 | nested_vmx_setup_ctls_msrs(); |
| 2642 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2643 | return alloc_kvm_area(); |
| 2644 | } |
| 2645 | |
| 2646 | static __exit void hardware_unsetup(void) |
| 2647 | { |
| 2648 | free_kvm_area(); |
| 2649 | } |
| 2650 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2651 | static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save) |
| 2652 | { |
| 2653 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 2654 | |
Avi Kivity | 6af11b9 | 2007-03-19 13:18:10 +0200 | [diff] [blame] | 2655 | if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2656 | vmcs_write16(sf->selector, save->selector); |
| 2657 | vmcs_writel(sf->base, save->base); |
| 2658 | vmcs_write32(sf->limit, save->limit); |
| 2659 | vmcs_write32(sf->ar_bytes, save->ar); |
| 2660 | } else { |
| 2661 | u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK) |
| 2662 | << AR_DPL_SHIFT; |
| 2663 | vmcs_write32(sf->ar_bytes, 0x93 | dpl); |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 2668 | { |
| 2669 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2670 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2671 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2672 | vmx->emulation_required = 1; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2673 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2674 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2675 | vmx_segment_cache_clear(vmx); |
| 2676 | |
Avi Kivity | d0ba64f | 2011-01-03 14:28:51 +0200 | [diff] [blame] | 2677 | vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector); |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2678 | vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base); |
| 2679 | vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit); |
| 2680 | vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2681 | |
| 2682 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2683 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2684 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2685 | vmcs_writel(GUEST_RFLAGS, flags); |
| 2686 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2687 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 2688 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2689 | |
| 2690 | update_exception_bitmap(vcpu); |
| 2691 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2692 | if (emulate_invalid_guest_state) |
| 2693 | return; |
| 2694 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2695 | fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es); |
| 2696 | fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds); |
| 2697 | fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs); |
| 2698 | fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2699 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2700 | vmx_segment_cache_clear(vmx); |
| 2701 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2702 | vmcs_write16(GUEST_SS_SELECTOR, 0); |
| 2703 | vmcs_write32(GUEST_SS_AR_BYTES, 0x93); |
| 2704 | |
| 2705 | vmcs_write16(GUEST_CS_SELECTOR, |
| 2706 | vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK); |
| 2707 | vmcs_write32(GUEST_CS_AR_BYTES, 0x9b); |
| 2708 | } |
| 2709 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2710 | static gva_t rmode_tss_base(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2711 | { |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2712 | if (!kvm->arch.tss_addr) { |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 2713 | struct kvm_memslots *slots; |
| 2714 | gfn_t base_gfn; |
| 2715 | |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 2716 | slots = kvm_memslots(kvm); |
Avi Kivity | f495c6e | 2010-06-10 17:21:29 +0300 | [diff] [blame] | 2717 | base_gfn = slots->memslots[0].base_gfn + |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 2718 | kvm->memslots->memslots[0].npages - 3; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2719 | return base_gfn << PAGE_SHIFT; |
| 2720 | } |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2721 | return kvm->arch.tss_addr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | static void fix_rmode_seg(int seg, struct kvm_save_segment *save) |
| 2725 | { |
| 2726 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 2727 | |
| 2728 | save->selector = vmcs_read16(sf->selector); |
| 2729 | save->base = vmcs_readl(sf->base); |
| 2730 | save->limit = vmcs_read32(sf->limit); |
| 2731 | save->ar = vmcs_read32(sf->ar_bytes); |
Jan Kiszka | 15b00f3 | 2007-11-19 10:21:45 +0100 | [diff] [blame] | 2732 | vmcs_write16(sf->selector, save->base >> 4); |
Gleb Natapov | 444e863 | 2010-12-27 17:25:04 +0200 | [diff] [blame] | 2733 | vmcs_write32(sf->base, save->base & 0xffff0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2734 | vmcs_write32(sf->limit, 0xffff); |
| 2735 | vmcs_write32(sf->ar_bytes, 0xf3); |
Gleb Natapov | 444e863 | 2010-12-27 17:25:04 +0200 | [diff] [blame] | 2736 | if (save->base & 0xf) |
| 2737 | printk_once(KERN_WARNING "kvm: segment base is not paragraph" |
| 2738 | " aligned when entering protected mode (seg=%d)", |
| 2739 | seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 2743 | { |
| 2744 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2745 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2746 | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2747 | if (enable_unrestricted_guest) |
| 2748 | return; |
| 2749 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2750 | vmx->emulation_required = 1; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2751 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2752 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 2753 | /* |
| 2754 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
| 2755 | * vcpu. Call it here with phys address pointing 16M below 4G. |
| 2756 | */ |
| 2757 | if (!vcpu->kvm->arch.tss_addr) { |
| 2758 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 2759 | "called before entering vcpu\n"); |
| 2760 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
| 2761 | vmx_set_tss_addr(vcpu->kvm, 0xfeffd000); |
| 2762 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 2763 | } |
| 2764 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2765 | vmx_segment_cache_clear(vmx); |
| 2766 | |
Avi Kivity | d0ba64f | 2011-01-03 14:28:51 +0200 | [diff] [blame] | 2767 | vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR); |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2768 | vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2769 | vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm)); |
| 2770 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2771 | vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2772 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
| 2773 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2774 | vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2775 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 2776 | |
| 2777 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2778 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2779 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2780 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2781 | |
| 2782 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2783 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2784 | update_exception_bitmap(vcpu); |
| 2785 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2786 | if (emulate_invalid_guest_state) |
| 2787 | goto continue_rmode; |
| 2788 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2789 | vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4); |
| 2790 | vmcs_write32(GUEST_SS_LIMIT, 0xffff); |
| 2791 | vmcs_write32(GUEST_SS_AR_BYTES, 0xf3); |
| 2792 | |
| 2793 | vmcs_write32(GUEST_CS_AR_BYTES, 0xf3); |
Michael Riepe | abacf8d | 2006-12-22 01:05:45 -0800 | [diff] [blame] | 2794 | vmcs_write32(GUEST_CS_LIMIT, 0xffff); |
Avi Kivity | 8cb5b03 | 2007-03-20 18:40:40 +0200 | [diff] [blame] | 2795 | if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000) |
| 2796 | vmcs_writel(GUEST_CS_BASE, 0xf0000); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2797 | vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4); |
| 2798 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2799 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es); |
| 2800 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds); |
| 2801 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs); |
| 2802 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 2803 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2804 | continue_rmode: |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 2805 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2806 | } |
| 2807 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2808 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 2809 | { |
| 2810 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2811 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 2812 | |
| 2813 | if (!msr) |
| 2814 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2815 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2816 | /* |
| 2817 | * Force kernel_gs_base reloading before EFER changes, as control |
| 2818 | * of this msr depends on is_long_mode(). |
| 2819 | */ |
| 2820 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2821 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2822 | if (efer & EFER_LMA) { |
| 2823 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 2824 | vmcs_read32(VM_ENTRY_CONTROLS) | |
| 2825 | VM_ENTRY_IA32E_MODE); |
| 2826 | msr->data = efer; |
| 2827 | } else { |
| 2828 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 2829 | vmcs_read32(VM_ENTRY_CONTROLS) & |
| 2830 | ~VM_ENTRY_IA32E_MODE); |
| 2831 | |
| 2832 | msr->data = efer & ~EFER_LME; |
| 2833 | } |
| 2834 | setup_msrs(vmx); |
| 2835 | } |
| 2836 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2837 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2838 | |
| 2839 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 2840 | { |
| 2841 | u32 guest_tr_ar; |
| 2842 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2843 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 2844 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2845 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
| 2846 | if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 2847 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 2848 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2849 | vmcs_write32(GUEST_TR_AR_BYTES, |
| 2850 | (guest_tr_ar & ~AR_TYPE_MASK) |
| 2851 | | AR_TYPE_BUSY_64_TSS); |
| 2852 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 2853 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 2857 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2858 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 2859 | vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 2860 | & ~VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 2861 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | #endif |
| 2865 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2866 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 2867 | { |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2868 | vpid_sync_context(to_vmx(vcpu)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 2869 | if (enable_ept) { |
| 2870 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 2871 | return; |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 2872 | ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 2873 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2874 | } |
| 2875 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 2876 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 2877 | { |
| 2878 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 2879 | |
| 2880 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 2881 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 2882 | } |
| 2883 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 2884 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 2885 | { |
| 2886 | if (enable_ept && is_paging(vcpu)) |
| 2887 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 2888 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 2889 | } |
| 2890 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2891 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2892 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2893 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 2894 | |
| 2895 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 2896 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2897 | } |
| 2898 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2899 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 2900 | { |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2901 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 2902 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 2903 | return; |
| 2904 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2905 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 2906 | vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]); |
| 2907 | vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]); |
| 2908 | vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]); |
| 2909 | vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2910 | } |
| 2911 | } |
| 2912 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2913 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 2914 | { |
| 2915 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 2916 | vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 2917 | vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 2918 | vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 2919 | vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2920 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2921 | |
| 2922 | __set_bit(VCPU_EXREG_PDPTR, |
| 2923 | (unsigned long *)&vcpu->arch.regs_avail); |
| 2924 | __set_bit(VCPU_EXREG_PDPTR, |
| 2925 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2926 | } |
| 2927 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2928 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2929 | |
| 2930 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 2931 | unsigned long cr0, |
| 2932 | struct kvm_vcpu *vcpu) |
| 2933 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 2934 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 2935 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2936 | if (!(cr0 & X86_CR0_PG)) { |
| 2937 | /* From paging/starting to nonpaging */ |
| 2938 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 2939 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2940 | (CPU_BASED_CR3_LOAD_EXITING | |
| 2941 | CPU_BASED_CR3_STORE_EXITING)); |
| 2942 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2943 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2944 | } else if (!is_paging(vcpu)) { |
| 2945 | /* From nonpaging to paging */ |
| 2946 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 2947 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2948 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 2949 | CPU_BASED_CR3_STORE_EXITING)); |
| 2950 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2951 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2952 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 2953 | |
| 2954 | if (!(cr0 & X86_CR0_WP)) |
| 2955 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2956 | } |
| 2957 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2958 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 2959 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2960 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2961 | unsigned long hw_cr0; |
| 2962 | |
| 2963 | if (enable_unrestricted_guest) |
| 2964 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST) |
| 2965 | | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
| 2966 | else |
| 2967 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2968 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2969 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2970 | enter_pmode(vcpu); |
| 2971 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2972 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2973 | enter_rmode(vcpu); |
| 2974 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2975 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2976 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 2977 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2978 | enter_lmode(vcpu); |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 2979 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2980 | exit_lmode(vcpu); |
| 2981 | } |
| 2982 | #endif |
| 2983 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2984 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2985 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 2986 | |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 2987 | if (!vcpu->fpu_active) |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2988 | hw_cr0 |= X86_CR0_TS | X86_CR0_MP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 2989 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2990 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2991 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2992 | vcpu->arch.cr0 = cr0; |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 2993 | __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2994 | } |
| 2995 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2996 | static u64 construct_eptp(unsigned long root_hpa) |
| 2997 | { |
| 2998 | u64 eptp; |
| 2999 | |
| 3000 | /* TODO write the value reading from MSR */ |
| 3001 | eptp = VMX_EPT_DEFAULT_MT | |
| 3002 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; |
| 3003 | eptp |= (root_hpa & PAGE_MASK); |
| 3004 | |
| 3005 | return eptp; |
| 3006 | } |
| 3007 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3008 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 3009 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3010 | unsigned long guest_cr3; |
| 3011 | u64 eptp; |
| 3012 | |
| 3013 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3014 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3015 | eptp = construct_eptp(cr3); |
| 3016 | vmcs_write64(EPT_POINTER, eptp); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 3017 | guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) : |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3018 | vcpu->kvm->arch.ept_identity_map_addr; |
Marcelo Tosatti | 7c93be4 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 3019 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3020 | } |
| 3021 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3022 | vmx_flush_tlb(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3023 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3024 | } |
| 3025 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3026 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3027 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3028 | unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ? |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3029 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); |
| 3030 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3031 | if (cr4 & X86_CR4_VMXE) { |
| 3032 | /* |
| 3033 | * To use VMXON (and later other VMX instructions), a guest |
| 3034 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 3035 | * So basically the check on whether to allow nested VMX |
| 3036 | * is here. |
| 3037 | */ |
| 3038 | if (!nested_vmx_allowed(vcpu)) |
| 3039 | return 1; |
| 3040 | } else if (to_vmx(vcpu)->nested.vmxon) |
| 3041 | return 1; |
| 3042 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3043 | vcpu->arch.cr4 = cr4; |
Avi Kivity | bc23008 | 2009-12-08 12:14:42 +0200 | [diff] [blame] | 3044 | if (enable_ept) { |
| 3045 | if (!is_paging(vcpu)) { |
| 3046 | hw_cr4 &= ~X86_CR4_PAE; |
| 3047 | hw_cr4 |= X86_CR4_PSE; |
| 3048 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 3049 | hw_cr4 &= ~X86_CR4_PAE; |
| 3050 | } |
| 3051 | } |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3052 | |
| 3053 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 3054 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3055 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3056 | } |
| 3057 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3058 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 3059 | struct kvm_segment *var, int seg) |
| 3060 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3061 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3062 | struct kvm_save_segment *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3063 | u32 ar; |
| 3064 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3065 | if (vmx->rmode.vm86_active |
| 3066 | && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES |
| 3067 | || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS |
| 3068 | || seg == VCPU_SREG_GS) |
| 3069 | && !emulate_invalid_guest_state) { |
| 3070 | switch (seg) { |
| 3071 | case VCPU_SREG_TR: save = &vmx->rmode.tr; break; |
| 3072 | case VCPU_SREG_ES: save = &vmx->rmode.es; break; |
| 3073 | case VCPU_SREG_DS: save = &vmx->rmode.ds; break; |
| 3074 | case VCPU_SREG_FS: save = &vmx->rmode.fs; break; |
| 3075 | case VCPU_SREG_GS: save = &vmx->rmode.gs; break; |
| 3076 | default: BUG(); |
| 3077 | } |
| 3078 | var->selector = save->selector; |
| 3079 | var->base = save->base; |
| 3080 | var->limit = save->limit; |
| 3081 | ar = save->ar; |
| 3082 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3083 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3084 | goto use_saved_rmode_seg; |
| 3085 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3086 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 3087 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 3088 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 3089 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3090 | use_saved_rmode_seg: |
Avi Kivity | 9fd4a3b | 2009-01-04 23:43:42 +0200 | [diff] [blame] | 3091 | if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3092 | ar = 0; |
| 3093 | var->type = ar & 15; |
| 3094 | var->s = (ar >> 4) & 1; |
| 3095 | var->dpl = (ar >> 5) & 3; |
| 3096 | var->present = (ar >> 7) & 1; |
| 3097 | var->avl = (ar >> 12) & 1; |
| 3098 | var->l = (ar >> 13) & 1; |
| 3099 | var->db = (ar >> 14) & 1; |
| 3100 | var->g = (ar >> 15) & 1; |
| 3101 | var->unusable = (ar >> 16) & 1; |
| 3102 | } |
| 3103 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3104 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 3105 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3106 | struct kvm_segment s; |
| 3107 | |
| 3108 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 3109 | vmx_get_segment(vcpu, &s, seg); |
| 3110 | return s.base; |
| 3111 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3112 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3113 | } |
| 3114 | |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3115 | static int __vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3116 | { |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 3117 | if (!is_protmode(vcpu)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3118 | return 0; |
| 3119 | |
Avi Kivity | f4c63e5 | 2011-03-07 14:54:28 +0200 | [diff] [blame] | 3120 | if (!is_long_mode(vcpu) |
| 3121 | && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */ |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3122 | return 3; |
| 3123 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3124 | 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] | 3125 | } |
| 3126 | |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3127 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
| 3128 | { |
| 3129 | if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) { |
| 3130 | __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
| 3131 | to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu); |
| 3132 | } |
| 3133 | return to_vmx(vcpu)->cpl; |
| 3134 | } |
| 3135 | |
| 3136 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3137 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3138 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3139 | u32 ar; |
| 3140 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3141 | if (var->unusable) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3142 | ar = 1 << 16; |
| 3143 | else { |
| 3144 | ar = var->type & 15; |
| 3145 | ar |= (var->s & 1) << 4; |
| 3146 | ar |= (var->dpl & 3) << 5; |
| 3147 | ar |= (var->present & 1) << 7; |
| 3148 | ar |= (var->avl & 1) << 12; |
| 3149 | ar |= (var->l & 1) << 13; |
| 3150 | ar |= (var->db & 1) << 14; |
| 3151 | ar |= (var->g & 1) << 15; |
| 3152 | } |
Uri Lublin | f7fbf1f | 2006-12-13 00:34:00 -0800 | [diff] [blame] | 3153 | if (ar == 0) /* a 0 value means unusable */ |
| 3154 | ar = AR_UNUSABLE_MASK; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3155 | |
| 3156 | return ar; |
| 3157 | } |
| 3158 | |
| 3159 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 3160 | struct kvm_segment *var, int seg) |
| 3161 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3162 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3163 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 3164 | u32 ar; |
| 3165 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3166 | vmx_segment_cache_clear(vmx); |
| 3167 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3168 | if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) { |
Gleb Natapov | a8ba6c2 | 2011-02-21 12:07:58 +0200 | [diff] [blame] | 3169 | vmcs_write16(sf->selector, var->selector); |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3170 | vmx->rmode.tr.selector = var->selector; |
| 3171 | vmx->rmode.tr.base = var->base; |
| 3172 | vmx->rmode.tr.limit = var->limit; |
| 3173 | vmx->rmode.tr.ar = vmx_segment_access_rights(var); |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3174 | return; |
| 3175 | } |
| 3176 | vmcs_writel(sf->base, var->base); |
| 3177 | vmcs_write32(sf->limit, var->limit); |
| 3178 | vmcs_write16(sf->selector, var->selector); |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3179 | if (vmx->rmode.vm86_active && var->s) { |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3180 | /* |
| 3181 | * Hack real-mode segments into vm86 compatibility. |
| 3182 | */ |
| 3183 | if (var->base == 0xffff0000 && var->selector == 0xf000) |
| 3184 | vmcs_writel(sf->base, 0xf0000); |
| 3185 | ar = 0xf3; |
| 3186 | } else |
| 3187 | ar = vmx_segment_access_rights(var); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3188 | |
| 3189 | /* |
| 3190 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 3191 | * qemu binaries. |
| 3192 | * IA32 arch specifies that at the time of processor reset the |
| 3193 | * "Accessed" bit in the AR field of segment registers is 1. And qemu |
| 3194 | * is setting it to 0 in the usedland code. This causes invalid guest |
| 3195 | * state vmexit when "unrestricted guest" mode is turned on. |
| 3196 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 3197 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 3198 | * kvm hack. |
| 3199 | */ |
| 3200 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
| 3201 | ar |= 0x1; /* Accessed */ |
| 3202 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3203 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3204 | __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3205 | } |
| 3206 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3207 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 3208 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3209 | 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] | 3210 | |
| 3211 | *db = (ar >> 14) & 1; |
| 3212 | *l = (ar >> 13) & 1; |
| 3213 | } |
| 3214 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3215 | 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] | 3216 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3217 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 3218 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3219 | } |
| 3220 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3221 | 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] | 3222 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3223 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 3224 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3225 | } |
| 3226 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3227 | 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] | 3228 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3229 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 3230 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3231 | } |
| 3232 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3233 | 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] | 3234 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3235 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 3236 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3237 | } |
| 3238 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3239 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 3240 | { |
| 3241 | struct kvm_segment var; |
| 3242 | u32 ar; |
| 3243 | |
| 3244 | vmx_get_segment(vcpu, &var, seg); |
| 3245 | ar = vmx_segment_access_rights(&var); |
| 3246 | |
| 3247 | if (var.base != (var.selector << 4)) |
| 3248 | return false; |
| 3249 | if (var.limit != 0xffff) |
| 3250 | return false; |
| 3251 | if (ar != 0xf3) |
| 3252 | return false; |
| 3253 | |
| 3254 | return true; |
| 3255 | } |
| 3256 | |
| 3257 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 3258 | { |
| 3259 | struct kvm_segment cs; |
| 3260 | unsigned int cs_rpl; |
| 3261 | |
| 3262 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 3263 | cs_rpl = cs.selector & SELECTOR_RPL_MASK; |
| 3264 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3265 | if (cs.unusable) |
| 3266 | return false; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3267 | if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK)) |
| 3268 | return false; |
| 3269 | if (!cs.s) |
| 3270 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3271 | if (cs.type & AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3272 | if (cs.dpl > cs_rpl) |
| 3273 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3274 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3275 | if (cs.dpl != cs_rpl) |
| 3276 | return false; |
| 3277 | } |
| 3278 | if (!cs.present) |
| 3279 | return false; |
| 3280 | |
| 3281 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 3282 | return true; |
| 3283 | } |
| 3284 | |
| 3285 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 3286 | { |
| 3287 | struct kvm_segment ss; |
| 3288 | unsigned int ss_rpl; |
| 3289 | |
| 3290 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 3291 | ss_rpl = ss.selector & SELECTOR_RPL_MASK; |
| 3292 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3293 | if (ss.unusable) |
| 3294 | return true; |
| 3295 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3296 | return false; |
| 3297 | if (!ss.s) |
| 3298 | return false; |
| 3299 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 3300 | return false; |
| 3301 | if (!ss.present) |
| 3302 | return false; |
| 3303 | |
| 3304 | return true; |
| 3305 | } |
| 3306 | |
| 3307 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 3308 | { |
| 3309 | struct kvm_segment var; |
| 3310 | unsigned int rpl; |
| 3311 | |
| 3312 | vmx_get_segment(vcpu, &var, seg); |
| 3313 | rpl = var.selector & SELECTOR_RPL_MASK; |
| 3314 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3315 | if (var.unusable) |
| 3316 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3317 | if (!var.s) |
| 3318 | return false; |
| 3319 | if (!var.present) |
| 3320 | return false; |
| 3321 | if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) { |
| 3322 | if (var.dpl < rpl) /* DPL < RPL */ |
| 3323 | return false; |
| 3324 | } |
| 3325 | |
| 3326 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 3327 | * rights flags |
| 3328 | */ |
| 3329 | return true; |
| 3330 | } |
| 3331 | |
| 3332 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 3333 | { |
| 3334 | struct kvm_segment tr; |
| 3335 | |
| 3336 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 3337 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3338 | if (tr.unusable) |
| 3339 | return false; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3340 | if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 3341 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3342 | 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] | 3343 | return false; |
| 3344 | if (!tr.present) |
| 3345 | return false; |
| 3346 | |
| 3347 | return true; |
| 3348 | } |
| 3349 | |
| 3350 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 3351 | { |
| 3352 | struct kvm_segment ldtr; |
| 3353 | |
| 3354 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 3355 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3356 | if (ldtr.unusable) |
| 3357 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3358 | if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 3359 | return false; |
| 3360 | if (ldtr.type != 2) |
| 3361 | return false; |
| 3362 | if (!ldtr.present) |
| 3363 | return false; |
| 3364 | |
| 3365 | return true; |
| 3366 | } |
| 3367 | |
| 3368 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 3369 | { |
| 3370 | struct kvm_segment cs, ss; |
| 3371 | |
| 3372 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 3373 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 3374 | |
| 3375 | return ((cs.selector & SELECTOR_RPL_MASK) == |
| 3376 | (ss.selector & SELECTOR_RPL_MASK)); |
| 3377 | } |
| 3378 | |
| 3379 | /* |
| 3380 | * Check if guest state is valid. Returns true if valid, false if |
| 3381 | * not. |
| 3382 | * We assume that registers are always usable |
| 3383 | */ |
| 3384 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 3385 | { |
| 3386 | /* real mode guest state checks */ |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 3387 | if (!is_protmode(vcpu)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3388 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 3389 | return false; |
| 3390 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 3391 | return false; |
| 3392 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 3393 | return false; |
| 3394 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 3395 | return false; |
| 3396 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 3397 | return false; |
| 3398 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 3399 | return false; |
| 3400 | } else { |
| 3401 | /* protected mode guest state checks */ |
| 3402 | if (!cs_ss_rpl_check(vcpu)) |
| 3403 | return false; |
| 3404 | if (!code_segment_valid(vcpu)) |
| 3405 | return false; |
| 3406 | if (!stack_segment_valid(vcpu)) |
| 3407 | return false; |
| 3408 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 3409 | return false; |
| 3410 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 3411 | return false; |
| 3412 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 3413 | return false; |
| 3414 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 3415 | return false; |
| 3416 | if (!tr_valid(vcpu)) |
| 3417 | return false; |
| 3418 | if (!ldtr_valid(vcpu)) |
| 3419 | return false; |
| 3420 | } |
| 3421 | /* TODO: |
| 3422 | * - Add checks on RIP |
| 3423 | * - Add checks on RFLAGS |
| 3424 | */ |
| 3425 | |
| 3426 | return true; |
| 3427 | } |
| 3428 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3429 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3430 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3431 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3432 | u16 data = 0; |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3433 | int r, idx, ret = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3434 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3435 | idx = srcu_read_lock(&kvm->srcu); |
| 3436 | fn = rmode_tss_base(kvm) >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3437 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3438 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3439 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3440 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 3441 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 3442 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3443 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3444 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3445 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 3446 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3447 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3448 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3449 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3450 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3451 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3452 | r = kvm_write_guest_page(kvm, fn, &data, |
| 3453 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 3454 | sizeof(u8)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3455 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3456 | goto out; |
| 3457 | |
| 3458 | ret = 1; |
| 3459 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3460 | srcu_read_unlock(&kvm->srcu, idx); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3461 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3462 | } |
| 3463 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3464 | static int init_rmode_identity_map(struct kvm *kvm) |
| 3465 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3466 | int i, idx, r, ret; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3467 | pfn_t identity_map_pfn; |
| 3468 | u32 tmp; |
| 3469 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3470 | if (!enable_ept) |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3471 | return 1; |
| 3472 | if (unlikely(!kvm->arch.ept_identity_pagetable)) { |
| 3473 | printk(KERN_ERR "EPT: identity-mapping pagetable " |
| 3474 | "haven't been allocated!\n"); |
| 3475 | return 0; |
| 3476 | } |
| 3477 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
| 3478 | return 1; |
| 3479 | ret = 0; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3480 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3481 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3482 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 3483 | if (r < 0) |
| 3484 | goto out; |
| 3485 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 3486 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 3487 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 3488 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 3489 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 3490 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 3491 | if (r < 0) |
| 3492 | goto out; |
| 3493 | } |
| 3494 | kvm->arch.ept_identity_pagetable_done = true; |
| 3495 | ret = 1; |
| 3496 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3497 | srcu_read_unlock(&kvm->srcu, idx); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3498 | return ret; |
| 3499 | } |
| 3500 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3501 | static void seg_setup(int seg) |
| 3502 | { |
| 3503 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3504 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3505 | |
| 3506 | vmcs_write16(sf->selector, 0); |
| 3507 | vmcs_writel(sf->base, 0); |
| 3508 | vmcs_write32(sf->limit, 0xffff); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3509 | if (enable_unrestricted_guest) { |
| 3510 | ar = 0x93; |
| 3511 | if (seg == VCPU_SREG_CS) |
| 3512 | ar |= 0x08; /* code segment */ |
| 3513 | } else |
| 3514 | ar = 0xf3; |
| 3515 | |
| 3516 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3517 | } |
| 3518 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3519 | static int alloc_apic_access_page(struct kvm *kvm) |
| 3520 | { |
| 3521 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 3522 | int r = 0; |
| 3523 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3524 | mutex_lock(&kvm->slots_lock); |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 3525 | if (kvm->arch.apic_access_page) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3526 | goto out; |
| 3527 | kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT; |
| 3528 | kvm_userspace_mem.flags = 0; |
| 3529 | kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL; |
| 3530 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
| 3531 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 3532 | if (r) |
| 3533 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3534 | |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 3535 | kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3536 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3537 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3538 | return r; |
| 3539 | } |
| 3540 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3541 | static int alloc_identity_pagetable(struct kvm *kvm) |
| 3542 | { |
| 3543 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 3544 | int r = 0; |
| 3545 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3546 | mutex_lock(&kvm->slots_lock); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3547 | if (kvm->arch.ept_identity_pagetable) |
| 3548 | goto out; |
| 3549 | kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT; |
| 3550 | kvm_userspace_mem.flags = 0; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3551 | kvm_userspace_mem.guest_phys_addr = |
| 3552 | kvm->arch.ept_identity_map_addr; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3553 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
| 3554 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 3555 | if (r) |
| 3556 | goto out; |
| 3557 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3558 | kvm->arch.ept_identity_pagetable = gfn_to_page(kvm, |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3559 | kvm->arch.ept_identity_map_addr >> PAGE_SHIFT); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3560 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3561 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3562 | return r; |
| 3563 | } |
| 3564 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3565 | static void allocate_vpid(struct vcpu_vmx *vmx) |
| 3566 | { |
| 3567 | int vpid; |
| 3568 | |
| 3569 | vmx->vpid = 0; |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 3570 | if (!enable_vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3571 | return; |
| 3572 | spin_lock(&vmx_vpid_lock); |
| 3573 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 3574 | if (vpid < VMX_NR_VPIDS) { |
| 3575 | vmx->vpid = vpid; |
| 3576 | __set_bit(vpid, vmx_vpid_bitmap); |
| 3577 | } |
| 3578 | spin_unlock(&vmx_vpid_lock); |
| 3579 | } |
| 3580 | |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 3581 | static void free_vpid(struct vcpu_vmx *vmx) |
| 3582 | { |
| 3583 | if (!enable_vpid) |
| 3584 | return; |
| 3585 | spin_lock(&vmx_vpid_lock); |
| 3586 | if (vmx->vpid != 0) |
| 3587 | __clear_bit(vmx->vpid, vmx_vpid_bitmap); |
| 3588 | spin_unlock(&vmx_vpid_lock); |
| 3589 | } |
| 3590 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3591 | 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] | 3592 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3593 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3594 | |
| 3595 | if (!cpu_has_vmx_msr_bitmap()) |
| 3596 | return; |
| 3597 | |
| 3598 | /* |
| 3599 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 3600 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 3601 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 3602 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3603 | if (msr <= 0x1fff) { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3604 | __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */ |
| 3605 | __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3606 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 3607 | msr &= 0x1fff; |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3608 | __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */ |
| 3609 | __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3610 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3611 | } |
| 3612 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3613 | static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only) |
| 3614 | { |
| 3615 | if (!longmode_only) |
| 3616 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr); |
| 3617 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr); |
| 3618 | } |
| 3619 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3620 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3621 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 3622 | * will not change in the lifetime of the guest. |
| 3623 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 3624 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 3625 | */ |
| 3626 | static void vmx_set_constant_host_state(void) |
| 3627 | { |
| 3628 | u32 low32, high32; |
| 3629 | unsigned long tmpl; |
| 3630 | struct desc_ptr dt; |
| 3631 | |
| 3632 | vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */ |
| 3633 | vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */ |
| 3634 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 3635 | |
| 3636 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
| 3637 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3638 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3639 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3640 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 3641 | |
| 3642 | native_store_idt(&dt); |
| 3643 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
| 3644 | |
| 3645 | asm("mov $.Lkvm_vmx_return, %0" : "=r"(tmpl)); |
| 3646 | vmcs_writel(HOST_RIP, tmpl); /* 22.2.5 */ |
| 3647 | |
| 3648 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 3649 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 3650 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 3651 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 3652 | |
| 3653 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 3654 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 3655 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 3656 | } |
| 3657 | } |
| 3658 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3659 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 3660 | { |
| 3661 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 3662 | if (enable_ept) |
| 3663 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 3664 | if (is_guest_mode(&vmx->vcpu)) |
| 3665 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 3666 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3667 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 3668 | } |
| 3669 | |
| 3670 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 3671 | { |
| 3672 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
| 3673 | if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) { |
| 3674 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 3675 | #ifdef CONFIG_X86_64 |
| 3676 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 3677 | CPU_BASED_CR8_LOAD_EXITING; |
| 3678 | #endif |
| 3679 | } |
| 3680 | if (!enable_ept) |
| 3681 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 3682 | CPU_BASED_CR3_LOAD_EXITING | |
| 3683 | CPU_BASED_INVLPG_EXITING; |
| 3684 | return exec_control; |
| 3685 | } |
| 3686 | |
| 3687 | static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) |
| 3688 | { |
| 3689 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
| 3690 | if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 3691 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 3692 | if (vmx->vpid == 0) |
| 3693 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 3694 | if (!enable_ept) { |
| 3695 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 3696 | enable_unrestricted_guest = 0; |
| 3697 | } |
| 3698 | if (!enable_unrestricted_guest) |
| 3699 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 3700 | if (!ple_gap) |
| 3701 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 3702 | return exec_control; |
| 3703 | } |
| 3704 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3705 | static void ept_set_mmio_spte_mask(void) |
| 3706 | { |
| 3707 | /* |
| 3708 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 3709 | * of an EPT paging-structure entry is 110b (write/execute). |
| 3710 | * Also, magic bits (0xffull << 49) is set to quickly identify mmio |
| 3711 | * spte. |
| 3712 | */ |
| 3713 | kvm_mmu_set_mmio_spte_mask(0xffull << 49 | 0x6ull); |
| 3714 | } |
| 3715 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3716 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3717 | * Sets up the vmcs for emulated real mode. |
| 3718 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3719 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3720 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 3721 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3722 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 3723 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3724 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3725 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3726 | /* I/O */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3727 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
| 3728 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3729 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3730 | if (cpu_has_vmx_msr_bitmap()) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3731 | vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3732 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3733 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 3734 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3735 | /* Control */ |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3736 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, |
| 3737 | vmcs_config.pin_based_exec_ctrl); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3738 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3739 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3740 | |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 3741 | if (cpu_has_secondary_exec_ctrls()) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3742 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 3743 | vmx_secondary_exec_control(vmx)); |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 3744 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3745 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3746 | if (ple_gap) { |
| 3747 | vmcs_write32(PLE_GAP, ple_gap); |
| 3748 | vmcs_write32(PLE_WINDOW, ple_window); |
| 3749 | } |
| 3750 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 3751 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 3752 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3753 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 3754 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 3755 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 3756 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3757 | vmx_set_constant_host_state(); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3758 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3759 | rdmsrl(MSR_FS_BASE, a); |
| 3760 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 3761 | rdmsrl(MSR_GS_BASE, a); |
| 3762 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 3763 | #else |
| 3764 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 3765 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 3766 | #endif |
| 3767 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3768 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 3769 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 3770 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3771 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 3772 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3773 | |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3774 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3775 | u32 msr_low, msr_high; |
| 3776 | u64 host_pat; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3777 | rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high); |
| 3778 | host_pat = msr_low | ((u64) msr_high << 32); |
| 3779 | /* Write the default value follow host pat */ |
| 3780 | vmcs_write64(GUEST_IA32_PAT, host_pat); |
| 3781 | /* Keep arch.pat sync with GUEST_IA32_PAT */ |
| 3782 | vmx->vcpu.arch.pat = host_pat; |
| 3783 | } |
| 3784 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3785 | for (i = 0; i < NR_VMX_MSR; ++i) { |
| 3786 | u32 index = vmx_msr_index[i]; |
| 3787 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3788 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3789 | |
| 3790 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 3791 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 3792 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 3793 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3794 | vmx->guest_msrs[j].index = i; |
| 3795 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 3796 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3797 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3798 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3799 | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3800 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3801 | |
| 3802 | /* 22.2.1, 20.8.1 */ |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3803 | vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl); |
| 3804 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3805 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3806 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3807 | |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 3808 | kvm_write_tsc(&vmx->vcpu, 0); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3809 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3810 | return 0; |
| 3811 | } |
| 3812 | |
| 3813 | static int vmx_vcpu_reset(struct kvm_vcpu *vcpu) |
| 3814 | { |
| 3815 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3816 | u64 msr; |
Xiao Guangrong | 4b9d3a0 | 2010-06-08 10:15:51 +0800 | [diff] [blame] | 3817 | int ret; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3818 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3819 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3820 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3821 | vmx->rmode.vm86_active = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3822 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3823 | vmx->soft_vnmi_blocked = 0; |
| 3824 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3825 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 3826 | kvm_set_cr8(&vmx->vcpu, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3827 | msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 3828 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3829 | msr |= MSR_IA32_APICBASE_BSP; |
| 3830 | kvm_set_apic_base(&vmx->vcpu, msr); |
| 3831 | |
Jan Kiszka | 10ab25c | 2010-05-25 16:01:50 +0200 | [diff] [blame] | 3832 | ret = fx_init(&vmx->vcpu); |
| 3833 | if (ret != 0) |
| 3834 | goto out; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3835 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3836 | vmx_segment_cache_clear(vmx); |
| 3837 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 3838 | seg_setup(VCPU_SREG_CS); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3839 | /* |
| 3840 | * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode |
| 3841 | * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh. |
| 3842 | */ |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 3843 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) { |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3844 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
| 3845 | vmcs_writel(GUEST_CS_BASE, 0x000f0000); |
| 3846 | } else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3847 | vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8); |
| 3848 | vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3849 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3850 | |
| 3851 | seg_setup(VCPU_SREG_DS); |
| 3852 | seg_setup(VCPU_SREG_ES); |
| 3853 | seg_setup(VCPU_SREG_FS); |
| 3854 | seg_setup(VCPU_SREG_GS); |
| 3855 | seg_setup(VCPU_SREG_SS); |
| 3856 | |
| 3857 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 3858 | vmcs_writel(GUEST_TR_BASE, 0); |
| 3859 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 3860 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 3861 | |
| 3862 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 3863 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 3864 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 3865 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 3866 | |
| 3867 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 3868 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 3869 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 3870 | |
| 3871 | vmcs_writel(GUEST_RFLAGS, 0x02); |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 3872 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3873 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3874 | else |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3875 | kvm_rip_write(vcpu, 0); |
| 3876 | kvm_register_write(vcpu, VCPU_REGS_RSP, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3877 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3878 | vmcs_writel(GUEST_DR7, 0x400); |
| 3879 | |
| 3880 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 3881 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 3882 | |
| 3883 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 3884 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 3885 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 3886 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3887 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
| 3888 | vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
| 3889 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3890 | /* Special registers */ |
| 3891 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 3892 | |
| 3893 | setup_msrs(vmx); |
| 3894 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3895 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 3896 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3897 | if (cpu_has_vmx_tpr_shadow()) { |
| 3898 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
| 3899 | if (vm_need_tpr_shadow(vmx->vcpu.kvm)) |
| 3900 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Takuya Yoshikawa | afc2018 | 2011-03-05 12:40:20 +0900 | [diff] [blame] | 3901 | __pa(vmx->vcpu.arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3902 | vmcs_write32(TPR_THRESHOLD, 0); |
| 3903 | } |
| 3904 | |
| 3905 | if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 3906 | vmcs_write64(APIC_ACCESS_ADDR, |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 3907 | page_to_phys(vmx->vcpu.kvm->arch.apic_access_page)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3908 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3909 | if (vmx->vpid != 0) |
| 3910 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 3911 | |
Eduardo Habkost | fa40052 | 2009-10-24 02:49:58 -0200 | [diff] [blame] | 3912 | vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 3913 | vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3914 | vmx_set_cr4(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3915 | vmx_set_efer(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3916 | vmx_fpu_activate(&vmx->vcpu); |
| 3917 | update_exception_bitmap(&vmx->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3918 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 3919 | vpid_sync_context(vmx); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3920 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3921 | ret = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3922 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3923 | /* HACK: Don't enable emulation on guest boot/reset */ |
| 3924 | vmx->emulation_required = 0; |
| 3925 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3926 | out: |
| 3927 | return ret; |
| 3928 | } |
| 3929 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 3930 | /* |
| 3931 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 3932 | * For most existing hypervisors, this will always return true. |
| 3933 | */ |
| 3934 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 3935 | { |
| 3936 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 3937 | PIN_BASED_EXT_INTR_MASK; |
| 3938 | } |
| 3939 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3940 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
| 3941 | { |
| 3942 | u32 cpu_based_vm_exec_control; |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 3943 | if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) |
| 3944 | /* We can get here when nested_run_pending caused |
| 3945 | * vmx_interrupt_allowed() to return false. In this case, do |
| 3946 | * nothing - the interrupt will be injected later. |
| 3947 | */ |
| 3948 | return; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3949 | |
| 3950 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 3951 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 3952 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 3953 | } |
| 3954 | |
| 3955 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
| 3956 | { |
| 3957 | u32 cpu_based_vm_exec_control; |
| 3958 | |
| 3959 | if (!cpu_has_virtual_nmis()) { |
| 3960 | enable_irq_window(vcpu); |
| 3961 | return; |
| 3962 | } |
| 3963 | |
Avi Kivity | 30bd0c4 | 2010-11-01 23:20:48 +0200 | [diff] [blame] | 3964 | if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
| 3965 | enable_irq_window(vcpu); |
| 3966 | return; |
| 3967 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3968 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 3969 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING; |
| 3970 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 3971 | } |
| 3972 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3973 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3974 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3975 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3976 | uint32_t intr; |
| 3977 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3978 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3979 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3980 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 3981 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3982 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 3983 | int inc_eip = 0; |
| 3984 | if (vcpu->arch.interrupt.soft) |
| 3985 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 3986 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 3987 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3988 | return; |
| 3989 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3990 | intr = irq | INTR_INFO_VALID_MASK; |
| 3991 | if (vcpu->arch.interrupt.soft) { |
| 3992 | intr |= INTR_TYPE_SOFT_INTR; |
| 3993 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 3994 | vmx->vcpu.arch.event_exit_inst_len); |
| 3995 | } else |
| 3996 | intr |= INTR_TYPE_EXT_INTR; |
| 3997 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 3998 | vmx_clear_hlt(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3999 | } |
| 4000 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4001 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 4002 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 4003 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4004 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 4005 | if (is_guest_mode(vcpu)) |
| 4006 | return; |
| 4007 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4008 | if (!cpu_has_virtual_nmis()) { |
| 4009 | /* |
| 4010 | * Tracking the NMI-blocked state in software is built upon |
| 4011 | * finding the next open IRQ window. This, in turn, depends on |
| 4012 | * well-behaving guests: They have to keep IRQs disabled at |
| 4013 | * least as long as the NMI handler runs. Otherwise we may |
| 4014 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 4015 | * highly unlikely, we can live with the residual risk. |
| 4016 | */ |
| 4017 | vmx->soft_vnmi_blocked = 1; |
| 4018 | vmx->vnmi_blocked_time = 0; |
| 4019 | } |
| 4020 | |
Jan Kiszka | 487b391 | 2008-09-26 09:30:56 +0200 | [diff] [blame] | 4021 | ++vcpu->stat.nmi_injections; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 4022 | vmx->nmi_known_unmasked = false; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4023 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 4024 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 4025 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 4026 | return; |
| 4027 | } |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4028 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 4029 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 4030 | vmx_clear_hlt(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4031 | } |
| 4032 | |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4033 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 4034 | { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4035 | if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked) |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4036 | return 0; |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 4037 | |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4038 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
Avi Kivity | 30bd0c4 | 2010-11-01 23:20:48 +0200 | [diff] [blame] | 4039 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 4040 | | GUEST_INTR_STATE_NMI)); |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 4041 | } |
| 4042 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4043 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 4044 | { |
| 4045 | if (!cpu_has_virtual_nmis()) |
| 4046 | return to_vmx(vcpu)->soft_vnmi_blocked; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 4047 | if (to_vmx(vcpu)->nmi_known_unmasked) |
| 4048 | return false; |
Avi Kivity | c332c83 | 2010-05-04 12:24:12 +0300 | [diff] [blame] | 4049 | return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4050 | } |
| 4051 | |
| 4052 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 4053 | { |
| 4054 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4055 | |
| 4056 | if (!cpu_has_virtual_nmis()) { |
| 4057 | if (vmx->soft_vnmi_blocked != masked) { |
| 4058 | vmx->soft_vnmi_blocked = masked; |
| 4059 | vmx->vnmi_blocked_time = 0; |
| 4060 | } |
| 4061 | } else { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 4062 | vmx->nmi_known_unmasked = !masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4063 | if (masked) |
| 4064 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 4065 | GUEST_INTR_STATE_NMI); |
| 4066 | else |
| 4067 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 4068 | GUEST_INTR_STATE_NMI); |
| 4069 | } |
| 4070 | } |
| 4071 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4072 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 4073 | { |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 4074 | if (is_guest_mode(vcpu) && nested_exit_on_intr(vcpu)) { |
| 4075 | struct vmcs12 *vmcs12; |
| 4076 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 4077 | return 0; |
| 4078 | nested_vmx_vmexit(vcpu); |
| 4079 | vmcs12 = get_vmcs12(vcpu); |
| 4080 | vmcs12->vm_exit_reason = EXIT_REASON_EXTERNAL_INTERRUPT; |
| 4081 | vmcs12->vm_exit_intr_info = 0; |
| 4082 | /* fall through to normal code, but now in L1, not L2 */ |
| 4083 | } |
| 4084 | |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4085 | return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
| 4086 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 4087 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4088 | } |
| 4089 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4090 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 4091 | { |
| 4092 | int ret; |
| 4093 | struct kvm_userspace_memory_region tss_mem = { |
Sheng Yang | 6fe6397 | 2008-10-16 17:30:58 +0800 | [diff] [blame] | 4094 | .slot = TSS_PRIVATE_MEMSLOT, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4095 | .guest_phys_addr = addr, |
| 4096 | .memory_size = PAGE_SIZE * 3, |
| 4097 | .flags = 0, |
| 4098 | }; |
| 4099 | |
| 4100 | ret = kvm_set_memory_region(kvm, &tss_mem, 0); |
| 4101 | if (ret) |
| 4102 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 4103 | kvm->arch.tss_addr = addr; |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 4104 | if (!init_rmode_tss(kvm)) |
| 4105 | return -ENOMEM; |
| 4106 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4107 | return 0; |
| 4108 | } |
| 4109 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4110 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 4111 | int vec, u32 err_code) |
| 4112 | { |
Nitin A Kamble | b3f3770 | 2007-05-17 15:50:34 +0300 | [diff] [blame] | 4113 | /* |
| 4114 | * Instruction with address size override prefix opcode 0x67 |
| 4115 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 4116 | */ |
| 4117 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4118 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4119 | return 1; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4120 | /* |
| 4121 | * Forward all other exceptions that are valid in real mode. |
| 4122 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 4123 | * the required debugging infrastructure rework. |
| 4124 | */ |
| 4125 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4126 | case DB_VECTOR: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4127 | if (vcpu->guest_debug & |
| 4128 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 4129 | return 0; |
| 4130 | kvm_queue_exception(vcpu, vec); |
| 4131 | return 1; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4132 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 4133 | /* |
| 4134 | * Update instruction length as we may reinject the exception |
| 4135 | * from user space while in guest debugging mode. |
| 4136 | */ |
| 4137 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 4138 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4139 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 4140 | return 0; |
| 4141 | /* fall through */ |
| 4142 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4143 | case OF_VECTOR: |
| 4144 | case BR_VECTOR: |
| 4145 | case UD_VECTOR: |
| 4146 | case DF_VECTOR: |
| 4147 | case SS_VECTOR: |
| 4148 | case GP_VECTOR: |
| 4149 | case MF_VECTOR: |
| 4150 | kvm_queue_exception(vcpu, vec); |
| 4151 | return 1; |
| 4152 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4153 | return 0; |
| 4154 | } |
| 4155 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4156 | /* |
| 4157 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 4158 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 4159 | * We pass a fake environment to the machine check handler because we want |
| 4160 | * the guest to be always treated like user space, no matter what context |
| 4161 | * it used internally. |
| 4162 | */ |
| 4163 | static void kvm_machine_check(void) |
| 4164 | { |
| 4165 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 4166 | struct pt_regs regs = { |
| 4167 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 4168 | .flags = X86_EFLAGS_IF, |
| 4169 | }; |
| 4170 | |
| 4171 | do_machine_check(®s, 0); |
| 4172 | #endif |
| 4173 | } |
| 4174 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4175 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4176 | { |
| 4177 | /* already handled by vcpu_run */ |
| 4178 | return 1; |
| 4179 | } |
| 4180 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4181 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4182 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 4183 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4184 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4185 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4186 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4187 | u32 vect_info; |
| 4188 | enum emulation_result er; |
| 4189 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 4190 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 4191 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4192 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4193 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4194 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4195 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4196 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
Avi Kivity | 65ac726 | 2009-11-04 11:59:01 +0200 | [diff] [blame] | 4197 | !is_page_fault(intr_info)) { |
| 4198 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4199 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
| 4200 | vcpu->run->internal.ndata = 2; |
| 4201 | vcpu->run->internal.data[0] = vect_info; |
| 4202 | vcpu->run->internal.data[1] = intr_info; |
| 4203 | return 0; |
| 4204 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4205 | |
Jan Kiszka | e4a4188 | 2008-09-26 09:30:46 +0200 | [diff] [blame] | 4206 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 4207 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 4208 | |
| 4209 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 4210 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 4211 | return 1; |
| 4212 | } |
| 4213 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4214 | if (is_invalid_opcode(intr_info)) { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4215 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4216 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 4217 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4218 | return 1; |
| 4219 | } |
| 4220 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4221 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 4222 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4223 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 4224 | if (is_page_fault(intr_info)) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4225 | /* EPT won't cause page fault directly */ |
Julia Lawall | cf3ace7 | 2011-08-02 12:34:57 +0200 | [diff] [blame] | 4226 | BUG_ON(enable_ept); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4227 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4228 | trace_kvm_page_fault(cr2, error_code); |
| 4229 | |
Gleb Natapov | 3298b75 | 2009-05-11 13:35:46 +0300 | [diff] [blame] | 4230 | if (kvm_event_needs_reinjection(vcpu)) |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 4231 | kvm_mmu_unprotect_page_virt(vcpu, cr2); |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4232 | return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4233 | } |
| 4234 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4235 | if (vmx->rmode.vm86_active && |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4236 | handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK, |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 4237 | error_code)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4238 | if (vcpu->arch.halt_request) { |
| 4239 | vcpu->arch.halt_request = 0; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 4240 | return kvm_emulate_halt(vcpu); |
| 4241 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4242 | return 1; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 4243 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4244 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4245 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4246 | switch (ex_no) { |
| 4247 | case DB_VECTOR: |
| 4248 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 4249 | if (!(vcpu->guest_debug & |
| 4250 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
| 4251 | vcpu->arch.dr6 = dr6 | DR6_FIXED_1; |
| 4252 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 4253 | return 1; |
| 4254 | } |
| 4255 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 4256 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 4257 | /* fall through */ |
| 4258 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 4259 | /* |
| 4260 | * Update instruction length as we may reinject #BP from |
| 4261 | * user space while in guest debugging mode. Reading it for |
| 4262 | * #DB as well causes no harm, it is not used in that case. |
| 4263 | */ |
| 4264 | vmx->vcpu.arch.event_exit_inst_len = |
| 4265 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4266 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 4267 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4268 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 4269 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4270 | break; |
| 4271 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4272 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 4273 | kvm_run->ex.exception = ex_no; |
| 4274 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4275 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4276 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4277 | return 0; |
| 4278 | } |
| 4279 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4280 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4281 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 4282 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4283 | return 1; |
| 4284 | } |
| 4285 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4286 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4287 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4288 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4289 | return 0; |
| 4290 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4291 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4292 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4293 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4294 | unsigned long exit_qualification; |
Jan Kiszka | 34c33d1 | 2009-02-08 13:28:15 +0100 | [diff] [blame] | 4295 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4296 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4297 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4298 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4299 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 4300 | in = (exit_qualification & 8) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 4301 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4302 | ++vcpu->stat.io_exits; |
| 4303 | |
| 4304 | if (string || in) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4305 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4306 | |
| 4307 | port = exit_qualification >> 16; |
| 4308 | size = (exit_qualification & 7) + 1; |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 4309 | skip_emulated_instruction(vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4310 | |
| 4311 | return kvm_fast_pio_out(vcpu, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4312 | } |
| 4313 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4314 | static void |
| 4315 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 4316 | { |
| 4317 | /* |
| 4318 | * Patch in the VMCALL instruction: |
| 4319 | */ |
| 4320 | hypercall[0] = 0x0f; |
| 4321 | hypercall[1] = 0x01; |
| 4322 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4323 | } |
| 4324 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4325 | /* called to set cr0 as approriate for a mov-to-cr0 exit. */ |
| 4326 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 4327 | { |
| 4328 | if (to_vmx(vcpu)->nested.vmxon && |
| 4329 | ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON)) |
| 4330 | return 1; |
| 4331 | |
| 4332 | if (is_guest_mode(vcpu)) { |
| 4333 | /* |
| 4334 | * We get here when L2 changed cr0 in a way that did not change |
| 4335 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
| 4336 | * but did change L0 shadowed bits. This can currently happen |
| 4337 | * with the TS bit: L0 may want to leave TS on (for lazy fpu |
| 4338 | * loading) while pretending to allow the guest to change it. |
| 4339 | */ |
| 4340 | if (kvm_set_cr0(vcpu, (val & vcpu->arch.cr0_guest_owned_bits) | |
| 4341 | (vcpu->arch.cr0 & ~vcpu->arch.cr0_guest_owned_bits))) |
| 4342 | return 1; |
| 4343 | vmcs_writel(CR0_READ_SHADOW, val); |
| 4344 | return 0; |
| 4345 | } else |
| 4346 | return kvm_set_cr0(vcpu, val); |
| 4347 | } |
| 4348 | |
| 4349 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 4350 | { |
| 4351 | if (is_guest_mode(vcpu)) { |
| 4352 | if (kvm_set_cr4(vcpu, (val & vcpu->arch.cr4_guest_owned_bits) | |
| 4353 | (vcpu->arch.cr4 & ~vcpu->arch.cr4_guest_owned_bits))) |
| 4354 | return 1; |
| 4355 | vmcs_writel(CR4_READ_SHADOW, val); |
| 4356 | return 0; |
| 4357 | } else |
| 4358 | return kvm_set_cr4(vcpu, val); |
| 4359 | } |
| 4360 | |
| 4361 | /* called to set cr0 as approriate for clts instruction exit. */ |
| 4362 | static void handle_clts(struct kvm_vcpu *vcpu) |
| 4363 | { |
| 4364 | if (is_guest_mode(vcpu)) { |
| 4365 | /* |
| 4366 | * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS |
| 4367 | * but we did (!fpu_active). We need to keep GUEST_CR0.TS on, |
| 4368 | * just pretend it's off (also in arch.cr0 for fpu_activate). |
| 4369 | */ |
| 4370 | vmcs_writel(CR0_READ_SHADOW, |
| 4371 | vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS); |
| 4372 | vcpu->arch.cr0 &= ~X86_CR0_TS; |
| 4373 | } else |
| 4374 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
| 4375 | } |
| 4376 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4377 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4378 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4379 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4380 | int cr; |
| 4381 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 4382 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4383 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4384 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4385 | cr = exit_qualification & 15; |
| 4386 | reg = (exit_qualification >> 8) & 15; |
| 4387 | switch ((exit_qualification >> 4) & 3) { |
| 4388 | case 0: /* mov to cr */ |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4389 | val = kvm_register_read(vcpu, reg); |
| 4390 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4391 | switch (cr) { |
| 4392 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4393 | err = handle_set_cr0(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4394 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4395 | return 1; |
| 4396 | case 3: |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 4397 | err = kvm_set_cr3(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4398 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4399 | return 1; |
| 4400 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4401 | err = handle_set_cr4(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4402 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4403 | return 1; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4404 | case 8: { |
| 4405 | u8 cr8_prev = kvm_get_cr8(vcpu); |
| 4406 | u8 cr8 = kvm_register_read(vcpu, reg); |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 4407 | err = kvm_set_cr8(vcpu, cr8); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4408 | kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4409 | if (irqchip_in_kernel(vcpu->kvm)) |
| 4410 | return 1; |
| 4411 | if (cr8_prev <= cr8) |
| 4412 | return 1; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4413 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4414 | return 0; |
| 4415 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4416 | }; |
| 4417 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4418 | case 2: /* clts */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4419 | handle_clts(vcpu); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4420 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4421 | skip_emulated_instruction(vcpu); |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 4422 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4423 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4424 | case 1: /*mov from cr*/ |
| 4425 | switch (cr) { |
| 4426 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 4427 | val = kvm_read_cr3(vcpu); |
| 4428 | kvm_register_write(vcpu, reg, val); |
| 4429 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4430 | skip_emulated_instruction(vcpu); |
| 4431 | return 1; |
| 4432 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4433 | val = kvm_get_cr8(vcpu); |
| 4434 | kvm_register_write(vcpu, reg, val); |
| 4435 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4436 | skip_emulated_instruction(vcpu); |
| 4437 | return 1; |
| 4438 | } |
| 4439 | break; |
| 4440 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 4441 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4442 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 4443 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4444 | |
| 4445 | skip_emulated_instruction(vcpu); |
| 4446 | return 1; |
| 4447 | default: |
| 4448 | break; |
| 4449 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4450 | vcpu->run->exit_reason = 0; |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 4451 | pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4452 | (int)(exit_qualification >> 4) & 3, cr); |
| 4453 | return 0; |
| 4454 | } |
| 4455 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4456 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4457 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4458 | unsigned long exit_qualification; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4459 | int dr, reg; |
| 4460 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 4461 | /* 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] | 4462 | if (!kvm_require_cpl(vcpu, 0)) |
| 4463 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4464 | dr = vmcs_readl(GUEST_DR7); |
| 4465 | if (dr & DR7_GD) { |
| 4466 | /* |
| 4467 | * As the vm-exit takes precedence over the debug trap, we |
| 4468 | * need to emulate the latter, either for the host or the |
| 4469 | * guest debugging itself. |
| 4470 | */ |
| 4471 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4472 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
| 4473 | vcpu->run->debug.arch.dr7 = dr; |
| 4474 | vcpu->run->debug.arch.pc = |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4475 | vmcs_readl(GUEST_CS_BASE) + |
| 4476 | vmcs_readl(GUEST_RIP); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4477 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 4478 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4479 | return 0; |
| 4480 | } else { |
| 4481 | vcpu->arch.dr7 &= ~DR7_GD; |
| 4482 | vcpu->arch.dr6 |= DR6_BD; |
| 4483 | vmcs_writel(GUEST_DR7, vcpu->arch.dr7); |
| 4484 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 4485 | return 1; |
| 4486 | } |
| 4487 | } |
| 4488 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4489 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4490 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 4491 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 4492 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4493 | unsigned long val; |
| 4494 | if (!kvm_get_dr(vcpu, dr, &val)) |
| 4495 | kvm_register_write(vcpu, reg, val); |
| 4496 | } else |
| 4497 | kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4498 | skip_emulated_instruction(vcpu); |
| 4499 | return 1; |
| 4500 | } |
| 4501 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4502 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 4503 | { |
| 4504 | vmcs_writel(GUEST_DR7, val); |
| 4505 | } |
| 4506 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4507 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4508 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 4509 | kvm_emulate_cpuid(vcpu); |
| 4510 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4511 | } |
| 4512 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4513 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4514 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4515 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4516 | u64 data; |
| 4517 | |
| 4518 | if (vmx_get_msr(vcpu, ecx, &data)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4519 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4520 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4521 | return 1; |
| 4522 | } |
| 4523 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4524 | trace_kvm_msr_read(ecx, data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4525 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4526 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4527 | vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u; |
| 4528 | vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4529 | skip_emulated_instruction(vcpu); |
| 4530 | return 1; |
| 4531 | } |
| 4532 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4533 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4534 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4535 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 4536 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 4537 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4538 | |
| 4539 | if (vmx_set_msr(vcpu, ecx, data) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4540 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4541 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4542 | return 1; |
| 4543 | } |
| 4544 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4545 | trace_kvm_msr_write(ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4546 | skip_emulated_instruction(vcpu); |
| 4547 | return 1; |
| 4548 | } |
| 4549 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4550 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4551 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4552 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4553 | return 1; |
| 4554 | } |
| 4555 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4556 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4557 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4558 | u32 cpu_based_vm_exec_control; |
| 4559 | |
| 4560 | /* clear pending irq */ |
| 4561 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4562 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 4563 | 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] | 4564 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4565 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4566 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 4567 | ++vcpu->stat.irq_window_exits; |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4568 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4569 | /* |
| 4570 | * If the user space waits to inject interrupts, exit as soon as |
| 4571 | * possible |
| 4572 | */ |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 4573 | if (!irqchip_in_kernel(vcpu->kvm) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4574 | vcpu->run->request_interrupt_window && |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 4575 | !kvm_cpu_has_interrupt(vcpu)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4576 | vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4577 | return 0; |
| 4578 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4579 | return 1; |
| 4580 | } |
| 4581 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4582 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4583 | { |
| 4584 | skip_emulated_instruction(vcpu); |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 4585 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4586 | } |
| 4587 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4588 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4589 | { |
Dor Laor | 510043d | 2007-02-19 18:25:43 +0200 | [diff] [blame] | 4590 | skip_emulated_instruction(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4591 | kvm_emulate_hypercall(vcpu); |
| 4592 | return 1; |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4593 | } |
| 4594 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 4595 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 4596 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4597 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 4598 | } |
| 4599 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4600 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4601 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4602 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4603 | |
| 4604 | kvm_mmu_invlpg(vcpu, exit_qualification); |
| 4605 | skip_emulated_instruction(vcpu); |
| 4606 | return 1; |
| 4607 | } |
| 4608 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4609 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 4610 | { |
| 4611 | skip_emulated_instruction(vcpu); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 4612 | kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 4613 | return 1; |
| 4614 | } |
| 4615 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 4616 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 4617 | { |
| 4618 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 4619 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4620 | |
| 4621 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
| 4622 | skip_emulated_instruction(vcpu); |
| 4623 | return 1; |
| 4624 | } |
| 4625 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4626 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4627 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 4628 | if (likely(fasteoi)) { |
| 4629 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4630 | int access_type, offset; |
| 4631 | |
| 4632 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 4633 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 4634 | /* |
| 4635 | * Sane guest uses MOV to write EOI, with written value |
| 4636 | * not cared. So make a short-circuit here by avoiding |
| 4637 | * heavy instruction emulation. |
| 4638 | */ |
| 4639 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 4640 | (offset == APIC_EOI)) { |
| 4641 | kvm_lapic_set_eoi(vcpu); |
| 4642 | skip_emulated_instruction(vcpu); |
| 4643 | return 1; |
| 4644 | } |
| 4645 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4646 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4647 | } |
| 4648 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4649 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4650 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 4651 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4652 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 4653 | bool has_error_code = false; |
| 4654 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4655 | u16 tss_selector; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4656 | int reason, type, idt_v; |
| 4657 | |
| 4658 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
| 4659 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4660 | |
| 4661 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4662 | |
| 4663 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4664 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 4665 | switch (type) { |
| 4666 | case INTR_TYPE_NMI_INTR: |
| 4667 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 4668 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4669 | break; |
| 4670 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4671 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4672 | kvm_clear_interrupt_queue(vcpu); |
| 4673 | break; |
| 4674 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 4675 | if (vmx->idt_vectoring_info & |
| 4676 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 4677 | has_error_code = true; |
| 4678 | error_code = |
| 4679 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 4680 | } |
| 4681 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4682 | case INTR_TYPE_SOFT_EXCEPTION: |
| 4683 | kvm_clear_exception_queue(vcpu); |
| 4684 | break; |
| 4685 | default: |
| 4686 | break; |
| 4687 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 4688 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4689 | tss_selector = exit_qualification; |
| 4690 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4691 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 4692 | type != INTR_TYPE_EXT_INTR && |
| 4693 | type != INTR_TYPE_NMI_INTR)) |
| 4694 | skip_emulated_instruction(vcpu); |
| 4695 | |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 4696 | if (kvm_task_switch(vcpu, tss_selector, reason, |
| 4697 | has_error_code, error_code) == EMULATE_FAIL) { |
| 4698 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4699 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 4700 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4701 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 4702 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4703 | |
| 4704 | /* clear all local breakpoint enable flags */ |
| 4705 | vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55); |
| 4706 | |
| 4707 | /* |
| 4708 | * TODO: What about debug traps on tss switch? |
| 4709 | * Are we supposed to inject them and update dr6? |
| 4710 | */ |
| 4711 | |
| 4712 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4713 | } |
| 4714 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4715 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4716 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4717 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4718 | gpa_t gpa; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4719 | int gla_validity; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4720 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4721 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4722 | |
| 4723 | if (exit_qualification & (1 << 6)) { |
| 4724 | printk(KERN_ERR "EPT: GPA exceeds GAW!\n"); |
Jan Kiszka | 7f582ab | 2009-07-22 23:53:01 +0200 | [diff] [blame] | 4725 | return -EINVAL; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4726 | } |
| 4727 | |
| 4728 | gla_validity = (exit_qualification >> 7) & 0x3; |
| 4729 | if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) { |
| 4730 | printk(KERN_ERR "EPT: Handling EPT violation failed!\n"); |
| 4731 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 4732 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4733 | vmcs_readl(GUEST_LINEAR_ADDRESS)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4734 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 4735 | (long unsigned int)exit_qualification); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4736 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 4737 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION; |
Avi Kivity | 596ae89 | 2009-06-03 14:12:10 +0300 | [diff] [blame] | 4738 | return 0; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4742 | trace_kvm_page_fault(gpa, exit_qualification); |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4743 | return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4744 | } |
| 4745 | |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4746 | static u64 ept_rsvd_mask(u64 spte, int level) |
| 4747 | { |
| 4748 | int i; |
| 4749 | u64 mask = 0; |
| 4750 | |
| 4751 | for (i = 51; i > boot_cpu_data.x86_phys_bits; i--) |
| 4752 | mask |= (1ULL << i); |
| 4753 | |
| 4754 | if (level > 2) |
| 4755 | /* bits 7:3 reserved */ |
| 4756 | mask |= 0xf8; |
| 4757 | else if (level == 2) { |
| 4758 | if (spte & (1ULL << 7)) |
| 4759 | /* 2MB ref, bits 20:12 reserved */ |
| 4760 | mask |= 0x1ff000; |
| 4761 | else |
| 4762 | /* bits 6:3 reserved */ |
| 4763 | mask |= 0x78; |
| 4764 | } |
| 4765 | |
| 4766 | return mask; |
| 4767 | } |
| 4768 | |
| 4769 | static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte, |
| 4770 | int level) |
| 4771 | { |
| 4772 | printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level); |
| 4773 | |
| 4774 | /* 010b (write-only) */ |
| 4775 | WARN_ON((spte & 0x7) == 0x2); |
| 4776 | |
| 4777 | /* 110b (write/execute) */ |
| 4778 | WARN_ON((spte & 0x7) == 0x6); |
| 4779 | |
| 4780 | /* 100b (execute-only) and value not supported by logical processor */ |
| 4781 | if (!cpu_has_vmx_ept_execute_only()) |
| 4782 | WARN_ON((spte & 0x7) == 0x4); |
| 4783 | |
| 4784 | /* not 000b */ |
| 4785 | if ((spte & 0x7)) { |
| 4786 | u64 rsvd_bits = spte & ept_rsvd_mask(spte, level); |
| 4787 | |
| 4788 | if (rsvd_bits != 0) { |
| 4789 | printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n", |
| 4790 | __func__, rsvd_bits); |
| 4791 | WARN_ON(1); |
| 4792 | } |
| 4793 | |
| 4794 | if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) { |
| 4795 | u64 ept_mem_type = (spte & 0x38) >> 3; |
| 4796 | |
| 4797 | if (ept_mem_type == 2 || ept_mem_type == 3 || |
| 4798 | ept_mem_type == 7) { |
| 4799 | printk(KERN_ERR "%s: ept_mem_type=0x%llx\n", |
| 4800 | __func__, ept_mem_type); |
| 4801 | WARN_ON(1); |
| 4802 | } |
| 4803 | } |
| 4804 | } |
| 4805 | } |
| 4806 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4807 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4808 | { |
| 4809 | u64 sptes[4]; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 4810 | int nr_sptes, i, ret; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4811 | gpa_t gpa; |
| 4812 | |
| 4813 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 4814 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 4815 | ret = handle_mmio_page_fault_common(vcpu, gpa, true); |
| 4816 | if (likely(ret == 1)) |
| 4817 | return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) == |
| 4818 | EMULATE_DONE; |
| 4819 | if (unlikely(!ret)) |
| 4820 | return 1; |
| 4821 | |
| 4822 | /* It is the real ept misconfig */ |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4823 | printk(KERN_ERR "EPT: Misconfiguration.\n"); |
| 4824 | printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa); |
| 4825 | |
| 4826 | nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes); |
| 4827 | |
| 4828 | for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i) |
| 4829 | ept_misconfig_inspect_spte(vcpu, sptes[i-1], i); |
| 4830 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4831 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 4832 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4833 | |
| 4834 | return 0; |
| 4835 | } |
| 4836 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4837 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4838 | { |
| 4839 | u32 cpu_based_vm_exec_control; |
| 4840 | |
| 4841 | /* clear pending NMI */ |
| 4842 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4843 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 4844 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 4845 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4846 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4847 | |
| 4848 | return 1; |
| 4849 | } |
| 4850 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4851 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4852 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 4853 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4854 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4855 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 4856 | u32 cpu_exec_ctrl; |
| 4857 | bool intr_window_requested; |
| 4858 | |
| 4859 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4860 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4861 | |
| 4862 | while (!guest_state_valid(vcpu)) { |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 4863 | if (intr_window_requested |
| 4864 | && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF)) |
| 4865 | return handle_interrupt_window(&vmx->vcpu); |
| 4866 | |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4867 | err = emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4868 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4869 | if (err == EMULATE_DO_MMIO) { |
| 4870 | ret = 0; |
| 4871 | goto out; |
| 4872 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 4873 | |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 4874 | if (err != EMULATE_DONE) |
| 4875 | return 0; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4876 | |
| 4877 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4878 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4879 | if (need_resched()) |
| 4880 | schedule(); |
| 4881 | } |
| 4882 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4883 | vmx->emulation_required = 0; |
| 4884 | out: |
| 4885 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4886 | } |
| 4887 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4888 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4889 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 4890 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 4891 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 4892 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4893 | { |
| 4894 | skip_emulated_instruction(vcpu); |
| 4895 | kvm_vcpu_on_spin(vcpu); |
| 4896 | |
| 4897 | return 1; |
| 4898 | } |
| 4899 | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 4900 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 4901 | { |
| 4902 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4903 | return 1; |
| 4904 | } |
| 4905 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4906 | /* |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 4907 | * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12. |
| 4908 | * We could reuse a single VMCS for all the L2 guests, but we also want the |
| 4909 | * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this |
| 4910 | * allows keeping them loaded on the processor, and in the future will allow |
| 4911 | * optimizations where prepare_vmcs02 doesn't need to set all the fields on |
| 4912 | * every entry if they never change. |
| 4913 | * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE |
| 4914 | * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first. |
| 4915 | * |
| 4916 | * The following functions allocate and free a vmcs02 in this pool. |
| 4917 | */ |
| 4918 | |
| 4919 | /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */ |
| 4920 | static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx) |
| 4921 | { |
| 4922 | struct vmcs02_list *item; |
| 4923 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 4924 | if (item->vmptr == vmx->nested.current_vmptr) { |
| 4925 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 4926 | return &item->vmcs02; |
| 4927 | } |
| 4928 | |
| 4929 | if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) { |
| 4930 | /* Recycle the least recently used VMCS. */ |
| 4931 | item = list_entry(vmx->nested.vmcs02_pool.prev, |
| 4932 | struct vmcs02_list, list); |
| 4933 | item->vmptr = vmx->nested.current_vmptr; |
| 4934 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 4935 | return &item->vmcs02; |
| 4936 | } |
| 4937 | |
| 4938 | /* Create a new VMCS */ |
| 4939 | item = (struct vmcs02_list *) |
| 4940 | kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL); |
| 4941 | if (!item) |
| 4942 | return NULL; |
| 4943 | item->vmcs02.vmcs = alloc_vmcs(); |
| 4944 | if (!item->vmcs02.vmcs) { |
| 4945 | kfree(item); |
| 4946 | return NULL; |
| 4947 | } |
| 4948 | loaded_vmcs_init(&item->vmcs02); |
| 4949 | item->vmptr = vmx->nested.current_vmptr; |
| 4950 | list_add(&(item->list), &(vmx->nested.vmcs02_pool)); |
| 4951 | vmx->nested.vmcs02_num++; |
| 4952 | return &item->vmcs02; |
| 4953 | } |
| 4954 | |
| 4955 | /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */ |
| 4956 | static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 4957 | { |
| 4958 | struct vmcs02_list *item; |
| 4959 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 4960 | if (item->vmptr == vmptr) { |
| 4961 | free_loaded_vmcs(&item->vmcs02); |
| 4962 | list_del(&item->list); |
| 4963 | kfree(item); |
| 4964 | vmx->nested.vmcs02_num--; |
| 4965 | return; |
| 4966 | } |
| 4967 | } |
| 4968 | |
| 4969 | /* |
| 4970 | * Free all VMCSs saved for this vcpu, except the one pointed by |
| 4971 | * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one |
| 4972 | * currently used, if running L2), and vmcs01 when running L2. |
| 4973 | */ |
| 4974 | static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx) |
| 4975 | { |
| 4976 | struct vmcs02_list *item, *n; |
| 4977 | list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) { |
| 4978 | if (vmx->loaded_vmcs != &item->vmcs02) |
| 4979 | free_loaded_vmcs(&item->vmcs02); |
| 4980 | list_del(&item->list); |
| 4981 | kfree(item); |
| 4982 | } |
| 4983 | vmx->nested.vmcs02_num = 0; |
| 4984 | |
| 4985 | if (vmx->loaded_vmcs != &vmx->vmcs01) |
| 4986 | free_loaded_vmcs(&vmx->vmcs01); |
| 4987 | } |
| 4988 | |
| 4989 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 4990 | * Emulate the VMXON instruction. |
| 4991 | * Currently, we just remember that VMX is active, and do not save or even |
| 4992 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 4993 | * do not currently need to store anything in that guest-allocated memory |
| 4994 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 4995 | * argument is different from the VMXON pointer (which the spec says they do). |
| 4996 | */ |
| 4997 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 4998 | { |
| 4999 | struct kvm_segment cs; |
| 5000 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5001 | |
| 5002 | /* The Intel VMX Instruction Reference lists a bunch of bits that |
| 5003 | * are prerequisite to running VMXON, most notably cr4.VMXE must be |
| 5004 | * set to 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 5005 | * Otherwise, we should fail with #UD. We test these now: |
| 5006 | */ |
| 5007 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) || |
| 5008 | !kvm_read_cr0_bits(vcpu, X86_CR0_PE) || |
| 5009 | (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
| 5010 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5011 | return 1; |
| 5012 | } |
| 5013 | |
| 5014 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 5015 | if (is_long_mode(vcpu) && !cs.l) { |
| 5016 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5017 | return 1; |
| 5018 | } |
| 5019 | |
| 5020 | if (vmx_get_cpl(vcpu)) { |
| 5021 | kvm_inject_gp(vcpu, 0); |
| 5022 | return 1; |
| 5023 | } |
| 5024 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 5025 | INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool)); |
| 5026 | vmx->nested.vmcs02_num = 0; |
| 5027 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 5028 | vmx->nested.vmxon = true; |
| 5029 | |
| 5030 | skip_emulated_instruction(vcpu); |
| 5031 | return 1; |
| 5032 | } |
| 5033 | |
| 5034 | /* |
| 5035 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 5036 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 5037 | * slightly different). It also specifies what exception to inject otherwise. |
| 5038 | */ |
| 5039 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 5040 | { |
| 5041 | struct kvm_segment cs; |
| 5042 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5043 | |
| 5044 | if (!vmx->nested.vmxon) { |
| 5045 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5046 | return 0; |
| 5047 | } |
| 5048 | |
| 5049 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 5050 | if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) || |
| 5051 | (is_long_mode(vcpu) && !cs.l)) { |
| 5052 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5053 | return 0; |
| 5054 | } |
| 5055 | |
| 5056 | if (vmx_get_cpl(vcpu)) { |
| 5057 | kvm_inject_gp(vcpu, 0); |
| 5058 | return 0; |
| 5059 | } |
| 5060 | |
| 5061 | return 1; |
| 5062 | } |
| 5063 | |
| 5064 | /* |
| 5065 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 5066 | * just stops using VMX. |
| 5067 | */ |
| 5068 | static void free_nested(struct vcpu_vmx *vmx) |
| 5069 | { |
| 5070 | if (!vmx->nested.vmxon) |
| 5071 | return; |
| 5072 | vmx->nested.vmxon = false; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 5073 | if (vmx->nested.current_vmptr != -1ull) { |
| 5074 | kunmap(vmx->nested.current_vmcs12_page); |
| 5075 | nested_release_page(vmx->nested.current_vmcs12_page); |
| 5076 | vmx->nested.current_vmptr = -1ull; |
| 5077 | vmx->nested.current_vmcs12 = NULL; |
| 5078 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 5079 | /* Unpin physical memory we referred to in current vmcs02 */ |
| 5080 | if (vmx->nested.apic_access_page) { |
| 5081 | nested_release_page(vmx->nested.apic_access_page); |
| 5082 | vmx->nested.apic_access_page = 0; |
| 5083 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 5084 | |
| 5085 | nested_free_all_saved_vmcss(vmx); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 5086 | } |
| 5087 | |
| 5088 | /* Emulate the VMXOFF instruction */ |
| 5089 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 5090 | { |
| 5091 | if (!nested_vmx_check_permission(vcpu)) |
| 5092 | return 1; |
| 5093 | free_nested(to_vmx(vcpu)); |
| 5094 | skip_emulated_instruction(vcpu); |
| 5095 | return 1; |
| 5096 | } |
| 5097 | |
| 5098 | /* |
Nadav Har'El | 064aea7 | 2011-05-25 23:04:56 +0300 | [diff] [blame] | 5099 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 5100 | * exit caused by such an instruction (run by a guest hypervisor). |
| 5101 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 5102 | * #UD or #GP. |
| 5103 | */ |
| 5104 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 5105 | unsigned long exit_qualification, |
| 5106 | u32 vmx_instruction_info, gva_t *ret) |
| 5107 | { |
| 5108 | /* |
| 5109 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 5110 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 5111 | * addressing components of the operand. Only the displacement part |
| 5112 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 5113 | * For how an actual address is calculated from all these components, |
| 5114 | * refer to Vol. 1, "Operand Addressing". |
| 5115 | */ |
| 5116 | int scaling = vmx_instruction_info & 3; |
| 5117 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 5118 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 5119 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 5120 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 5121 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 5122 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 5123 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 5124 | |
| 5125 | if (is_reg) { |
| 5126 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5127 | return 1; |
| 5128 | } |
| 5129 | |
| 5130 | /* Addr = segment_base + offset */ |
| 5131 | /* offset = base + [index * scale] + displacement */ |
| 5132 | *ret = vmx_get_segment_base(vcpu, seg_reg); |
| 5133 | if (base_is_valid) |
| 5134 | *ret += kvm_register_read(vcpu, base_reg); |
| 5135 | if (index_is_valid) |
| 5136 | *ret += kvm_register_read(vcpu, index_reg)<<scaling; |
| 5137 | *ret += exit_qualification; /* holds the displacement */ |
| 5138 | |
| 5139 | if (addr_size == 1) /* 32 bit */ |
| 5140 | *ret &= 0xffffffff; |
| 5141 | |
| 5142 | /* |
| 5143 | * TODO: throw #GP (and return 1) in various cases that the VM* |
| 5144 | * instructions require it - e.g., offset beyond segment limit, |
| 5145 | * unusable or unreadable/unwritable segment, non-canonical 64-bit |
| 5146 | * address, and so on. Currently these are not checked. |
| 5147 | */ |
| 5148 | return 0; |
| 5149 | } |
| 5150 | |
| 5151 | /* |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 5152 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 5153 | * set the success or error code of an emulated VMX instruction, as specified |
| 5154 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 5155 | */ |
| 5156 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 5157 | { |
| 5158 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 5159 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 5160 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 5161 | } |
| 5162 | |
| 5163 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 5164 | { |
| 5165 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 5166 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 5167 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 5168 | | X86_EFLAGS_CF); |
| 5169 | } |
| 5170 | |
| 5171 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
| 5172 | u32 vm_instruction_error) |
| 5173 | { |
| 5174 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 5175 | /* |
| 5176 | * failValid writes the error number to the current VMCS, which |
| 5177 | * can't be done there isn't a current VMCS. |
| 5178 | */ |
| 5179 | nested_vmx_failInvalid(vcpu); |
| 5180 | return; |
| 5181 | } |
| 5182 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 5183 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 5184 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 5185 | | X86_EFLAGS_ZF); |
| 5186 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 5187 | } |
| 5188 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 5189 | /* Emulate the VMCLEAR instruction */ |
| 5190 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 5191 | { |
| 5192 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5193 | gva_t gva; |
| 5194 | gpa_t vmptr; |
| 5195 | struct vmcs12 *vmcs12; |
| 5196 | struct page *page; |
| 5197 | struct x86_exception e; |
| 5198 | |
| 5199 | if (!nested_vmx_check_permission(vcpu)) |
| 5200 | return 1; |
| 5201 | |
| 5202 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 5203 | vmcs_read32(VMX_INSTRUCTION_INFO), &gva)) |
| 5204 | return 1; |
| 5205 | |
| 5206 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr, |
| 5207 | sizeof(vmptr), &e)) { |
| 5208 | kvm_inject_page_fault(vcpu, &e); |
| 5209 | return 1; |
| 5210 | } |
| 5211 | |
| 5212 | if (!IS_ALIGNED(vmptr, PAGE_SIZE)) { |
| 5213 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 5214 | skip_emulated_instruction(vcpu); |
| 5215 | return 1; |
| 5216 | } |
| 5217 | |
| 5218 | if (vmptr == vmx->nested.current_vmptr) { |
| 5219 | kunmap(vmx->nested.current_vmcs12_page); |
| 5220 | nested_release_page(vmx->nested.current_vmcs12_page); |
| 5221 | vmx->nested.current_vmptr = -1ull; |
| 5222 | vmx->nested.current_vmcs12 = NULL; |
| 5223 | } |
| 5224 | |
| 5225 | page = nested_get_page(vcpu, vmptr); |
| 5226 | if (page == NULL) { |
| 5227 | /* |
| 5228 | * For accurate processor emulation, VMCLEAR beyond available |
| 5229 | * physical memory should do nothing at all. However, it is |
| 5230 | * possible that a nested vmx bug, not a guest hypervisor bug, |
| 5231 | * resulted in this case, so let's shut down before doing any |
| 5232 | * more damage: |
| 5233 | */ |
| 5234 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 5235 | return 1; |
| 5236 | } |
| 5237 | vmcs12 = kmap(page); |
| 5238 | vmcs12->launch_state = 0; |
| 5239 | kunmap(page); |
| 5240 | nested_release_page(page); |
| 5241 | |
| 5242 | nested_free_vmcs02(vmx, vmptr); |
| 5243 | |
| 5244 | skip_emulated_instruction(vcpu); |
| 5245 | nested_vmx_succeed(vcpu); |
| 5246 | return 1; |
| 5247 | } |
| 5248 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 5249 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 5250 | |
| 5251 | /* Emulate the VMLAUNCH instruction */ |
| 5252 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 5253 | { |
| 5254 | return nested_vmx_run(vcpu, true); |
| 5255 | } |
| 5256 | |
| 5257 | /* Emulate the VMRESUME instruction */ |
| 5258 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 5259 | { |
| 5260 | |
| 5261 | return nested_vmx_run(vcpu, false); |
| 5262 | } |
| 5263 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 5264 | enum vmcs_field_type { |
| 5265 | VMCS_FIELD_TYPE_U16 = 0, |
| 5266 | VMCS_FIELD_TYPE_U64 = 1, |
| 5267 | VMCS_FIELD_TYPE_U32 = 2, |
| 5268 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 |
| 5269 | }; |
| 5270 | |
| 5271 | static inline int vmcs_field_type(unsigned long field) |
| 5272 | { |
| 5273 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
| 5274 | return VMCS_FIELD_TYPE_U32; |
| 5275 | return (field >> 13) & 0x3 ; |
| 5276 | } |
| 5277 | |
| 5278 | static inline int vmcs_field_readonly(unsigned long field) |
| 5279 | { |
| 5280 | return (((field >> 10) & 0x3) == 1); |
| 5281 | } |
| 5282 | |
| 5283 | /* |
| 5284 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 5285 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 5286 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 5287 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 5288 | * 64-bit fields are to be returned). |
| 5289 | */ |
| 5290 | static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu, |
| 5291 | unsigned long field, u64 *ret) |
| 5292 | { |
| 5293 | short offset = vmcs_field_to_offset(field); |
| 5294 | char *p; |
| 5295 | |
| 5296 | if (offset < 0) |
| 5297 | return 0; |
| 5298 | |
| 5299 | p = ((char *)(get_vmcs12(vcpu))) + offset; |
| 5300 | |
| 5301 | switch (vmcs_field_type(field)) { |
| 5302 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 5303 | *ret = *((natural_width *)p); |
| 5304 | return 1; |
| 5305 | case VMCS_FIELD_TYPE_U16: |
| 5306 | *ret = *((u16 *)p); |
| 5307 | return 1; |
| 5308 | case VMCS_FIELD_TYPE_U32: |
| 5309 | *ret = *((u32 *)p); |
| 5310 | return 1; |
| 5311 | case VMCS_FIELD_TYPE_U64: |
| 5312 | *ret = *((u64 *)p); |
| 5313 | return 1; |
| 5314 | default: |
| 5315 | return 0; /* can never happen. */ |
| 5316 | } |
| 5317 | } |
| 5318 | |
| 5319 | /* |
| 5320 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 5321 | * used before) all generate the same failure when it is missing. |
| 5322 | */ |
| 5323 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 5324 | { |
| 5325 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5326 | if (vmx->nested.current_vmptr == -1ull) { |
| 5327 | nested_vmx_failInvalid(vcpu); |
| 5328 | skip_emulated_instruction(vcpu); |
| 5329 | return 0; |
| 5330 | } |
| 5331 | return 1; |
| 5332 | } |
| 5333 | |
| 5334 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 5335 | { |
| 5336 | unsigned long field; |
| 5337 | u64 field_value; |
| 5338 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5339 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5340 | gva_t gva = 0; |
| 5341 | |
| 5342 | if (!nested_vmx_check_permission(vcpu) || |
| 5343 | !nested_vmx_check_vmcs12(vcpu)) |
| 5344 | return 1; |
| 5345 | |
| 5346 | /* Decode instruction info and find the field to read */ |
| 5347 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5348 | /* Read the field, zero-extended to a u64 field_value */ |
| 5349 | if (!vmcs12_read_any(vcpu, field, &field_value)) { |
| 5350 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 5351 | skip_emulated_instruction(vcpu); |
| 5352 | return 1; |
| 5353 | } |
| 5354 | /* |
| 5355 | * Now copy part of this value to register or memory, as requested. |
| 5356 | * Note that the number of bits actually copied is 32 or 64 depending |
| 5357 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 5358 | */ |
| 5359 | if (vmx_instruction_info & (1u << 10)) { |
| 5360 | kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
| 5361 | field_value); |
| 5362 | } else { |
| 5363 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 5364 | vmx_instruction_info, &gva)) |
| 5365 | return 1; |
| 5366 | /* _system ok, as nested_vmx_check_permission verified cpl=0 */ |
| 5367 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
| 5368 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); |
| 5369 | } |
| 5370 | |
| 5371 | nested_vmx_succeed(vcpu); |
| 5372 | skip_emulated_instruction(vcpu); |
| 5373 | return 1; |
| 5374 | } |
| 5375 | |
| 5376 | |
| 5377 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 5378 | { |
| 5379 | unsigned long field; |
| 5380 | gva_t gva; |
| 5381 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5382 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5383 | char *p; |
| 5384 | short offset; |
| 5385 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 5386 | * mode, and eventually we need to write that into a field of several |
| 5387 | * possible lengths. The code below first zero-extends the value to 64 |
| 5388 | * bit (field_value), and then copies only the approriate number of |
| 5389 | * bits into the vmcs12 field. |
| 5390 | */ |
| 5391 | u64 field_value = 0; |
| 5392 | struct x86_exception e; |
| 5393 | |
| 5394 | if (!nested_vmx_check_permission(vcpu) || |
| 5395 | !nested_vmx_check_vmcs12(vcpu)) |
| 5396 | return 1; |
| 5397 | |
| 5398 | if (vmx_instruction_info & (1u << 10)) |
| 5399 | field_value = kvm_register_read(vcpu, |
| 5400 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 5401 | else { |
| 5402 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 5403 | vmx_instruction_info, &gva)) |
| 5404 | return 1; |
| 5405 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, |
| 5406 | &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) { |
| 5407 | kvm_inject_page_fault(vcpu, &e); |
| 5408 | return 1; |
| 5409 | } |
| 5410 | } |
| 5411 | |
| 5412 | |
| 5413 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5414 | if (vmcs_field_readonly(field)) { |
| 5415 | nested_vmx_failValid(vcpu, |
| 5416 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
| 5417 | skip_emulated_instruction(vcpu); |
| 5418 | return 1; |
| 5419 | } |
| 5420 | |
| 5421 | offset = vmcs_field_to_offset(field); |
| 5422 | if (offset < 0) { |
| 5423 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 5424 | skip_emulated_instruction(vcpu); |
| 5425 | return 1; |
| 5426 | } |
| 5427 | p = ((char *) get_vmcs12(vcpu)) + offset; |
| 5428 | |
| 5429 | switch (vmcs_field_type(field)) { |
| 5430 | case VMCS_FIELD_TYPE_U16: |
| 5431 | *(u16 *)p = field_value; |
| 5432 | break; |
| 5433 | case VMCS_FIELD_TYPE_U32: |
| 5434 | *(u32 *)p = field_value; |
| 5435 | break; |
| 5436 | case VMCS_FIELD_TYPE_U64: |
| 5437 | *(u64 *)p = field_value; |
| 5438 | break; |
| 5439 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 5440 | *(natural_width *)p = field_value; |
| 5441 | break; |
| 5442 | default: |
| 5443 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 5444 | skip_emulated_instruction(vcpu); |
| 5445 | return 1; |
| 5446 | } |
| 5447 | |
| 5448 | nested_vmx_succeed(vcpu); |
| 5449 | skip_emulated_instruction(vcpu); |
| 5450 | return 1; |
| 5451 | } |
| 5452 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 5453 | /* Emulate the VMPTRLD instruction */ |
| 5454 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 5455 | { |
| 5456 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5457 | gva_t gva; |
| 5458 | gpa_t vmptr; |
| 5459 | struct x86_exception e; |
| 5460 | |
| 5461 | if (!nested_vmx_check_permission(vcpu)) |
| 5462 | return 1; |
| 5463 | |
| 5464 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 5465 | vmcs_read32(VMX_INSTRUCTION_INFO), &gva)) |
| 5466 | return 1; |
| 5467 | |
| 5468 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr, |
| 5469 | sizeof(vmptr), &e)) { |
| 5470 | kvm_inject_page_fault(vcpu, &e); |
| 5471 | return 1; |
| 5472 | } |
| 5473 | |
| 5474 | if (!IS_ALIGNED(vmptr, PAGE_SIZE)) { |
| 5475 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 5476 | skip_emulated_instruction(vcpu); |
| 5477 | return 1; |
| 5478 | } |
| 5479 | |
| 5480 | if (vmx->nested.current_vmptr != vmptr) { |
| 5481 | struct vmcs12 *new_vmcs12; |
| 5482 | struct page *page; |
| 5483 | page = nested_get_page(vcpu, vmptr); |
| 5484 | if (page == NULL) { |
| 5485 | nested_vmx_failInvalid(vcpu); |
| 5486 | skip_emulated_instruction(vcpu); |
| 5487 | return 1; |
| 5488 | } |
| 5489 | new_vmcs12 = kmap(page); |
| 5490 | if (new_vmcs12->revision_id != VMCS12_REVISION) { |
| 5491 | kunmap(page); |
| 5492 | nested_release_page_clean(page); |
| 5493 | nested_vmx_failValid(vcpu, |
| 5494 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5495 | skip_emulated_instruction(vcpu); |
| 5496 | return 1; |
| 5497 | } |
| 5498 | if (vmx->nested.current_vmptr != -1ull) { |
| 5499 | kunmap(vmx->nested.current_vmcs12_page); |
| 5500 | nested_release_page(vmx->nested.current_vmcs12_page); |
| 5501 | } |
| 5502 | |
| 5503 | vmx->nested.current_vmptr = vmptr; |
| 5504 | vmx->nested.current_vmcs12 = new_vmcs12; |
| 5505 | vmx->nested.current_vmcs12_page = page; |
| 5506 | } |
| 5507 | |
| 5508 | nested_vmx_succeed(vcpu); |
| 5509 | skip_emulated_instruction(vcpu); |
| 5510 | return 1; |
| 5511 | } |
| 5512 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 5513 | /* Emulate the VMPTRST instruction */ |
| 5514 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 5515 | { |
| 5516 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5517 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5518 | gva_t vmcs_gva; |
| 5519 | struct x86_exception e; |
| 5520 | |
| 5521 | if (!nested_vmx_check_permission(vcpu)) |
| 5522 | return 1; |
| 5523 | |
| 5524 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 5525 | vmx_instruction_info, &vmcs_gva)) |
| 5526 | return 1; |
| 5527 | /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */ |
| 5528 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
| 5529 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 5530 | sizeof(u64), &e)) { |
| 5531 | kvm_inject_page_fault(vcpu, &e); |
| 5532 | return 1; |
| 5533 | } |
| 5534 | nested_vmx_succeed(vcpu); |
| 5535 | skip_emulated_instruction(vcpu); |
| 5536 | return 1; |
| 5537 | } |
| 5538 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 5539 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5540 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 5541 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 5542 | * to be done to userspace and return 0. |
| 5543 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5544 | static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5545 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 5546 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5547 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5548 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5549 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5550 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 5551 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 5552 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 5553 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 5554 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 5555 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 5556 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 5557 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5558 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5559 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 5560 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 5561 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 5562 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 5563 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 5564 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 5565 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 5566 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 5567 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 5568 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5569 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 5570 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 5571 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 5572 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5573 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5574 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5575 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 5576 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5577 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 5578 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op, |
| 5579 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5580 | }; |
| 5581 | |
| 5582 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 5583 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5584 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 5585 | /* |
| 5586 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 5587 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 5588 | * disinterest in the current event (read or write a specific MSR) by using an |
| 5589 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 5590 | */ |
| 5591 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 5592 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 5593 | { |
| 5594 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 5595 | gpa_t bitmap; |
| 5596 | |
| 5597 | if (!nested_cpu_has(get_vmcs12(vcpu), CPU_BASED_USE_MSR_BITMAPS)) |
| 5598 | return 1; |
| 5599 | |
| 5600 | /* |
| 5601 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 5602 | * for the four combinations of read/write and low/high MSR numbers. |
| 5603 | * First we need to figure out which of the four to use: |
| 5604 | */ |
| 5605 | bitmap = vmcs12->msr_bitmap; |
| 5606 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 5607 | bitmap += 2048; |
| 5608 | if (msr_index >= 0xc0000000) { |
| 5609 | msr_index -= 0xc0000000; |
| 5610 | bitmap += 1024; |
| 5611 | } |
| 5612 | |
| 5613 | /* Then read the msr_index'th bit from this bitmap: */ |
| 5614 | if (msr_index < 1024*8) { |
| 5615 | unsigned char b; |
| 5616 | kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1); |
| 5617 | return 1 & (b >> (msr_index & 7)); |
| 5618 | } else |
| 5619 | return 1; /* let L1 handle the wrong parameter */ |
| 5620 | } |
| 5621 | |
| 5622 | /* |
| 5623 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 5624 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 5625 | * intercept (via guest_host_mask etc.) the current event. |
| 5626 | */ |
| 5627 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 5628 | struct vmcs12 *vmcs12) |
| 5629 | { |
| 5630 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5631 | int cr = exit_qualification & 15; |
| 5632 | int reg = (exit_qualification >> 8) & 15; |
| 5633 | unsigned long val = kvm_register_read(vcpu, reg); |
| 5634 | |
| 5635 | switch ((exit_qualification >> 4) & 3) { |
| 5636 | case 0: /* mov to cr */ |
| 5637 | switch (cr) { |
| 5638 | case 0: |
| 5639 | if (vmcs12->cr0_guest_host_mask & |
| 5640 | (val ^ vmcs12->cr0_read_shadow)) |
| 5641 | return 1; |
| 5642 | break; |
| 5643 | case 3: |
| 5644 | if ((vmcs12->cr3_target_count >= 1 && |
| 5645 | vmcs12->cr3_target_value0 == val) || |
| 5646 | (vmcs12->cr3_target_count >= 2 && |
| 5647 | vmcs12->cr3_target_value1 == val) || |
| 5648 | (vmcs12->cr3_target_count >= 3 && |
| 5649 | vmcs12->cr3_target_value2 == val) || |
| 5650 | (vmcs12->cr3_target_count >= 4 && |
| 5651 | vmcs12->cr3_target_value3 == val)) |
| 5652 | return 0; |
| 5653 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
| 5654 | return 1; |
| 5655 | break; |
| 5656 | case 4: |
| 5657 | if (vmcs12->cr4_guest_host_mask & |
| 5658 | (vmcs12->cr4_read_shadow ^ val)) |
| 5659 | return 1; |
| 5660 | break; |
| 5661 | case 8: |
| 5662 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
| 5663 | return 1; |
| 5664 | break; |
| 5665 | } |
| 5666 | break; |
| 5667 | case 2: /* clts */ |
| 5668 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 5669 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
| 5670 | return 1; |
| 5671 | break; |
| 5672 | case 1: /* mov from cr */ |
| 5673 | switch (cr) { |
| 5674 | case 3: |
| 5675 | if (vmcs12->cpu_based_vm_exec_control & |
| 5676 | CPU_BASED_CR3_STORE_EXITING) |
| 5677 | return 1; |
| 5678 | break; |
| 5679 | case 8: |
| 5680 | if (vmcs12->cpu_based_vm_exec_control & |
| 5681 | CPU_BASED_CR8_STORE_EXITING) |
| 5682 | return 1; |
| 5683 | break; |
| 5684 | } |
| 5685 | break; |
| 5686 | case 3: /* lmsw */ |
| 5687 | /* |
| 5688 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 5689 | * cr0. Other attempted changes are ignored, with no exit. |
| 5690 | */ |
| 5691 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 5692 | (val ^ vmcs12->cr0_read_shadow)) |
| 5693 | return 1; |
| 5694 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 5695 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 5696 | (val & 0x1)) |
| 5697 | return 1; |
| 5698 | break; |
| 5699 | } |
| 5700 | return 0; |
| 5701 | } |
| 5702 | |
| 5703 | /* |
| 5704 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 5705 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 5706 | * when in is_guest_mode (L2). |
| 5707 | */ |
| 5708 | static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu) |
| 5709 | { |
| 5710 | u32 exit_reason = vmcs_read32(VM_EXIT_REASON); |
| 5711 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 5712 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5713 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5714 | |
| 5715 | if (vmx->nested.nested_run_pending) |
| 5716 | return 0; |
| 5717 | |
| 5718 | if (unlikely(vmx->fail)) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 5719 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 5720 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 5721 | return 1; |
| 5722 | } |
| 5723 | |
| 5724 | switch (exit_reason) { |
| 5725 | case EXIT_REASON_EXCEPTION_NMI: |
| 5726 | if (!is_exception(intr_info)) |
| 5727 | return 0; |
| 5728 | else if (is_page_fault(intr_info)) |
| 5729 | return enable_ept; |
| 5730 | return vmcs12->exception_bitmap & |
| 5731 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 5732 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
| 5733 | return 0; |
| 5734 | case EXIT_REASON_TRIPLE_FAULT: |
| 5735 | return 1; |
| 5736 | case EXIT_REASON_PENDING_INTERRUPT: |
| 5737 | case EXIT_REASON_NMI_WINDOW: |
| 5738 | /* |
| 5739 | * prepare_vmcs02() set the CPU_BASED_VIRTUAL_INTR_PENDING bit |
| 5740 | * (aka Interrupt Window Exiting) only when L1 turned it on, |
| 5741 | * so if we got a PENDING_INTERRUPT exit, this must be for L1. |
| 5742 | * Same for NMI Window Exiting. |
| 5743 | */ |
| 5744 | return 1; |
| 5745 | case EXIT_REASON_TASK_SWITCH: |
| 5746 | return 1; |
| 5747 | case EXIT_REASON_CPUID: |
| 5748 | return 1; |
| 5749 | case EXIT_REASON_HLT: |
| 5750 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 5751 | case EXIT_REASON_INVD: |
| 5752 | return 1; |
| 5753 | case EXIT_REASON_INVLPG: |
| 5754 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 5755 | case EXIT_REASON_RDPMC: |
| 5756 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
| 5757 | case EXIT_REASON_RDTSC: |
| 5758 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 5759 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 5760 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 5761 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: |
| 5762 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: |
| 5763 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
| 5764 | /* |
| 5765 | * VMX instructions trap unconditionally. This allows L1 to |
| 5766 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 5767 | */ |
| 5768 | return 1; |
| 5769 | case EXIT_REASON_CR_ACCESS: |
| 5770 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 5771 | case EXIT_REASON_DR_ACCESS: |
| 5772 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 5773 | case EXIT_REASON_IO_INSTRUCTION: |
| 5774 | /* TODO: support IO bitmaps */ |
| 5775 | return 1; |
| 5776 | case EXIT_REASON_MSR_READ: |
| 5777 | case EXIT_REASON_MSR_WRITE: |
| 5778 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 5779 | case EXIT_REASON_INVALID_STATE: |
| 5780 | return 1; |
| 5781 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 5782 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
| 5783 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 5784 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 5785 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 5786 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 5787 | nested_cpu_has2(vmcs12, |
| 5788 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 5789 | case EXIT_REASON_MCE_DURING_VMENTRY: |
| 5790 | return 0; |
| 5791 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
| 5792 | return 1; |
| 5793 | case EXIT_REASON_APIC_ACCESS: |
| 5794 | return nested_cpu_has2(vmcs12, |
| 5795 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 5796 | case EXIT_REASON_EPT_VIOLATION: |
| 5797 | case EXIT_REASON_EPT_MISCONFIG: |
| 5798 | return 0; |
| 5799 | case EXIT_REASON_WBINVD: |
| 5800 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 5801 | case EXIT_REASON_XSETBV: |
| 5802 | return 1; |
| 5803 | default: |
| 5804 | return 1; |
| 5805 | } |
| 5806 | } |
| 5807 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 5808 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 5809 | { |
| 5810 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 5811 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 5812 | } |
| 5813 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5814 | /* |
| 5815 | * The guest has exited. See if we can fix it or if we need userspace |
| 5816 | * assistance. |
| 5817 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5818 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5819 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5820 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5821 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5822 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5823 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5824 | /* If guest state is invalid, start emulating */ |
| 5825 | if (vmx->emulation_required && emulate_invalid_guest_state) |
| 5826 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 5827 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 5828 | /* |
| 5829 | * the KVM_REQ_EVENT optimization bit is only on for one entry, and if |
| 5830 | * we did not inject a still-pending event to L1 now because of |
| 5831 | * nested_run_pending, we need to re-enable this bit. |
| 5832 | */ |
| 5833 | if (vmx->nested.nested_run_pending) |
| 5834 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 5835 | |
Nadav Har'El | 509c75e | 2011-06-02 11:54:52 +0300 | [diff] [blame] | 5836 | if (!is_guest_mode(vcpu) && (exit_reason == EXIT_REASON_VMLAUNCH || |
| 5837 | exit_reason == EXIT_REASON_VMRESUME)) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 5838 | vmx->nested.nested_run_pending = 1; |
| 5839 | else |
| 5840 | vmx->nested.nested_run_pending = 0; |
| 5841 | |
| 5842 | if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) { |
| 5843 | nested_vmx_vmexit(vcpu); |
| 5844 | return 1; |
| 5845 | } |
| 5846 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 5847 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
| 5848 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 5849 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 5850 | = exit_reason; |
| 5851 | return 0; |
| 5852 | } |
| 5853 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5854 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5855 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 5856 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5857 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 5858 | return 0; |
| 5859 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5860 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 5861 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5862 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 5863 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
| 5864 | exit_reason != EXIT_REASON_TASK_SWITCH)) |
| 5865 | printk(KERN_WARNING "%s: unexpected, valid vectoring info " |
| 5866 | "(0x%x) and exit reason is 0x%x\n", |
| 5867 | __func__, vectoring_info, exit_reason); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5868 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 5869 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked && |
| 5870 | !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis( |
| 5871 | get_vmcs12(vcpu), vcpu)))) { |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 5872 | if (vmx_interrupt_allowed(vcpu)) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5873 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5874 | } else if (vmx->vnmi_blocked_time > 1000000000LL && |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 5875 | vcpu->arch.nmi_pending) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5876 | /* |
| 5877 | * This CPU don't support us in finding the end of an |
| 5878 | * NMI-blocked window if the guest runs with IRQs |
| 5879 | * disabled. So we pull the trigger after 1 s of |
| 5880 | * futile waiting, but inform the user about this. |
| 5881 | */ |
| 5882 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 5883 | "state on VCPU %d after 1 s timeout\n", |
| 5884 | __func__, vcpu->vcpu_id); |
| 5885 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5886 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5887 | } |
| 5888 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5889 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 5890 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5891 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5892 | else { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5893 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 5894 | vcpu->run->hw.hardware_exit_reason = exit_reason; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5895 | } |
| 5896 | return 0; |
| 5897 | } |
| 5898 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5899 | 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] | 5900 | { |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5901 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5902 | vmcs_write32(TPR_THRESHOLD, 0); |
| 5903 | return; |
| 5904 | } |
| 5905 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5906 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5907 | } |
| 5908 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5909 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5910 | { |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5911 | u32 exit_intr_info; |
| 5912 | |
| 5913 | if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 5914 | || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
| 5915 | return; |
| 5916 | |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 5917 | vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5918 | exit_intr_info = vmx->exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5919 | |
| 5920 | /* Handle machine checks before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5921 | if (is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5922 | kvm_machine_check(); |
| 5923 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5924 | /* We need to handle NMIs before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5925 | 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] | 5926 | (exit_intr_info & INTR_INFO_VALID_MASK)) { |
| 5927 | kvm_before_handle_nmi(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5928 | asm("int $2"); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 5929 | kvm_after_handle_nmi(&vmx->vcpu); |
| 5930 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5931 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5932 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5933 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 5934 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 5935 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5936 | bool unblock_nmi; |
| 5937 | u8 vector; |
| 5938 | bool idtv_info_valid; |
| 5939 | |
| 5940 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5941 | |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5942 | if (cpu_has_virtual_nmis()) { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 5943 | if (vmx->nmi_known_unmasked) |
| 5944 | return; |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 5945 | /* |
| 5946 | * Can't use vmx->exit_intr_info since we're not sure what |
| 5947 | * the exit reason is. |
| 5948 | */ |
| 5949 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5950 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 5951 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 5952 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 5953 | * SDM 3: 27.7.1.2 (September 2008) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5954 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 5955 | * a guest IRET fault. |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 5956 | * SDM 3: 23.2.2 (September 2008) |
| 5957 | * Bit 12 is undefined in any of the following cases: |
| 5958 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 5959 | * information field. |
| 5960 | * If the VM exit is due to a double fault. |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5961 | */ |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 5962 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 5963 | vector != DF_VECTOR && !idtv_info_valid) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5964 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5965 | GUEST_INTR_STATE_NMI); |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 5966 | else |
| 5967 | vmx->nmi_known_unmasked = |
| 5968 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 5969 | & GUEST_INTR_STATE_NMI); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5970 | } else if (unlikely(vmx->soft_vnmi_blocked)) |
| 5971 | vmx->vnmi_blocked_time += |
| 5972 | ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5973 | } |
| 5974 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 5975 | static void __vmx_complete_interrupts(struct vcpu_vmx *vmx, |
| 5976 | u32 idt_vectoring_info, |
| 5977 | int instr_len_field, |
| 5978 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5979 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5980 | u8 vector; |
| 5981 | int type; |
| 5982 | bool idtv_info_valid; |
| 5983 | |
| 5984 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 5985 | |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5986 | vmx->vcpu.arch.nmi_injected = false; |
| 5987 | kvm_clear_exception_queue(&vmx->vcpu); |
| 5988 | kvm_clear_interrupt_queue(&vmx->vcpu); |
| 5989 | |
| 5990 | if (!idtv_info_valid) |
| 5991 | return; |
| 5992 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5993 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 5994 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 5995 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 5996 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5997 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5998 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5999 | case INTR_TYPE_NMI_INTR: |
| 6000 | vmx->vcpu.arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6001 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 6002 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6003 | * Clear bit "block by NMI" before VM entry if a NMI |
| 6004 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6005 | */ |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 6006 | vmx_set_nmi_mask(&vmx->vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6007 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6008 | case INTR_TYPE_SOFT_EXCEPTION: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6009 | vmx->vcpu.arch.event_exit_inst_len = |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6010 | vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6011 | /* fall through */ |
| 6012 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 6013 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6014 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6015 | kvm_queue_exception_e(&vmx->vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 6016 | } else |
| 6017 | kvm_queue_exception(&vmx->vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6018 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6019 | case INTR_TYPE_SOFT_INTR: |
| 6020 | vmx->vcpu.arch.event_exit_inst_len = |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6021 | vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6022 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6023 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6024 | kvm_queue_interrupt(&vmx->vcpu, vector, |
| 6025 | type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6026 | break; |
| 6027 | default: |
| 6028 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 6029 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6030 | } |
| 6031 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6032 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 6033 | { |
Nadav Har'El | 66c78ae | 2011-05-25 23:14:07 +0300 | [diff] [blame] | 6034 | if (is_guest_mode(&vmx->vcpu)) |
| 6035 | return; |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6036 | __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info, |
| 6037 | VM_EXIT_INSTRUCTION_LEN, |
| 6038 | IDT_VECTORING_ERROR_CODE); |
| 6039 | } |
| 6040 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 6041 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 6042 | { |
Nadav Har'El | 66c78ae | 2011-05-25 23:14:07 +0300 | [diff] [blame] | 6043 | if (is_guest_mode(vcpu)) |
| 6044 | return; |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 6045 | __vmx_complete_interrupts(to_vmx(vcpu), |
| 6046 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 6047 | VM_ENTRY_INSTRUCTION_LEN, |
| 6048 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 6049 | |
| 6050 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 6051 | } |
| 6052 | |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6053 | #ifdef CONFIG_X86_64 |
| 6054 | #define R "r" |
| 6055 | #define Q "q" |
| 6056 | #else |
| 6057 | #define R "e" |
| 6058 | #define Q "l" |
| 6059 | #endif |
| 6060 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 6061 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6062 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6063 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6064 | |
Nadav Har'El | 66c78ae | 2011-05-25 23:14:07 +0300 | [diff] [blame] | 6065 | if (is_guest_mode(vcpu) && !vmx->nested.nested_run_pending) { |
| 6066 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6067 | if (vmcs12->idt_vectoring_info_field & |
| 6068 | VECTORING_INFO_VALID_MASK) { |
| 6069 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 6070 | vmcs12->idt_vectoring_info_field); |
| 6071 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 6072 | vmcs12->vm_exit_instruction_len); |
| 6073 | if (vmcs12->idt_vectoring_info_field & |
| 6074 | VECTORING_INFO_DELIVER_CODE_MASK) |
| 6075 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 6076 | vmcs12->idt_vectoring_error_code); |
| 6077 | } |
| 6078 | } |
| 6079 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6080 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
| 6081 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) |
| 6082 | vmx->entry_time = ktime_get(); |
| 6083 | |
| 6084 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 6085 | start emulation until we arrive back to a valid state */ |
| 6086 | if (vmx->emulation_required && emulate_invalid_guest_state) |
| 6087 | return; |
| 6088 | |
| 6089 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 6090 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 6091 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 6092 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 6093 | |
| 6094 | /* When single-stepping over STI and MOV SS, we must clear the |
| 6095 | * corresponding interruptibility bits in the guest state. Otherwise |
| 6096 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 6097 | * exceptions being set, but that's not correct for the guest debugging |
| 6098 | * case. */ |
| 6099 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 6100 | vmx_set_interrupt_shadow(vcpu, 0); |
| 6101 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6102 | vmx->__launched = vmx->loaded_vmcs->launched; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6103 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6104 | /* Store host registers */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6105 | "push %%"R"dx; push %%"R"bp;" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 6106 | "push %%"R"cx \n\t" /* placeholder for guest rcx */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6107 | "push %%"R"cx \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6108 | "cmp %%"R"sp, %c[host_rsp](%0) \n\t" |
| 6109 | "je 1f \n\t" |
| 6110 | "mov %%"R"sp, %c[host_rsp](%0) \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 6111 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6112 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 6113 | /* Reload cr2 if changed */ |
| 6114 | "mov %c[cr2](%0), %%"R"ax \n\t" |
| 6115 | "mov %%cr2, %%"R"dx \n\t" |
| 6116 | "cmp %%"R"ax, %%"R"dx \n\t" |
| 6117 | "je 2f \n\t" |
| 6118 | "mov %%"R"ax, %%cr2 \n\t" |
| 6119 | "2: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6120 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6121 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6122 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6123 | "mov %c[rax](%0), %%"R"ax \n\t" |
| 6124 | "mov %c[rbx](%0), %%"R"bx \n\t" |
| 6125 | "mov %c[rdx](%0), %%"R"dx \n\t" |
| 6126 | "mov %c[rsi](%0), %%"R"si \n\t" |
| 6127 | "mov %c[rdi](%0), %%"R"di \n\t" |
| 6128 | "mov %c[rbp](%0), %%"R"bp \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6129 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6130 | "mov %c[r8](%0), %%r8 \n\t" |
| 6131 | "mov %c[r9](%0), %%r9 \n\t" |
| 6132 | "mov %c[r10](%0), %%r10 \n\t" |
| 6133 | "mov %c[r11](%0), %%r11 \n\t" |
| 6134 | "mov %c[r12](%0), %%r12 \n\t" |
| 6135 | "mov %c[r13](%0), %%r13 \n\t" |
| 6136 | "mov %c[r14](%0), %%r14 \n\t" |
| 6137 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6138 | #endif |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6139 | "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */ |
| 6140 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6141 | /* Enter guest mode */ |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 6142 | "jne .Llaunched \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 6143 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 6144 | "jmp .Lkvm_vmx_return \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 6145 | ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 6146 | ".Lkvm_vmx_return: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6147 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 6148 | "mov %0, %c[wordsize](%%"R"sp) \n\t" |
| 6149 | "pop %0 \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6150 | "mov %%"R"ax, %c[rax](%0) \n\t" |
| 6151 | "mov %%"R"bx, %c[rbx](%0) \n\t" |
Avi Kivity | 1c696d0 | 2011-01-06 18:09:11 +0200 | [diff] [blame] | 6152 | "pop"Q" %c[rcx](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6153 | "mov %%"R"dx, %c[rdx](%0) \n\t" |
| 6154 | "mov %%"R"si, %c[rsi](%0) \n\t" |
| 6155 | "mov %%"R"di, %c[rdi](%0) \n\t" |
| 6156 | "mov %%"R"bp, %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6157 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6158 | "mov %%r8, %c[r8](%0) \n\t" |
| 6159 | "mov %%r9, %c[r9](%0) \n\t" |
| 6160 | "mov %%r10, %c[r10](%0) \n\t" |
| 6161 | "mov %%r11, %c[r11](%0) \n\t" |
| 6162 | "mov %%r12, %c[r12](%0) \n\t" |
| 6163 | "mov %%r13, %c[r13](%0) \n\t" |
| 6164 | "mov %%r14, %c[r14](%0) \n\t" |
| 6165 | "mov %%r15, %c[r15](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6166 | #endif |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6167 | "mov %%cr2, %%"R"ax \n\t" |
| 6168 | "mov %%"R"ax, %c[cr2](%0) \n\t" |
| 6169 | |
Avi Kivity | 1c696d0 | 2011-01-06 18:09:11 +0200 | [diff] [blame] | 6170 | "pop %%"R"bp; pop %%"R"dx \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6171 | "setbe %c[fail](%0) \n\t" |
| 6172 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6173 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6174 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6175 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6176 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 6177 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 6178 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 6179 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 6180 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 6181 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 6182 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6183 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6184 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 6185 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 6186 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 6187 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 6188 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 6189 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 6190 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 6191 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6192 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 6193 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 6194 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6195 | : "cc", "memory" |
Jan Kiszka | 07d6f55 | 2010-09-28 16:37:42 +0200 | [diff] [blame] | 6196 | , R"ax", R"bx", R"di", R"si" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6197 | #ifdef CONFIG_X86_64 |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6198 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
| 6199 | #endif |
| 6200 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6201 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 6202 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 6203 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 6204 | | (1 << VCPU_EXREG_CPL) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 6205 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 6206 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 6207 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 6208 | vcpu->arch.regs_dirty = 0; |
| 6209 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6210 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 6211 | |
Nadav Har'El | 66c78ae | 2011-05-25 23:14:07 +0300 | [diff] [blame] | 6212 | if (is_guest_mode(vcpu)) { |
| 6213 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6214 | vmcs12->idt_vectoring_info_field = vmx->idt_vectoring_info; |
| 6215 | if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) { |
| 6216 | vmcs12->idt_vectoring_error_code = |
| 6217 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 6218 | vmcs12->vm_exit_instruction_len = |
| 6219 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 6220 | } |
| 6221 | } |
| 6222 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 6223 | asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS)); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6224 | vmx->loaded_vmcs->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 6225 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6226 | vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); |
Jan Kiszka | 1e2b1dd | 2011-09-12 10:52:24 +0200 | [diff] [blame] | 6227 | trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6228 | |
| 6229 | vmx_complete_atomic_exit(vmx); |
| 6230 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6231 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6232 | } |
| 6233 | |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6234 | #undef R |
| 6235 | #undef Q |
| 6236 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6237 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 6238 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6239 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6240 | |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 6241 | free_vpid(vmx); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6242 | free_nested(vmx); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6243 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6244 | kfree(vmx->guest_msrs); |
| 6245 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 6246 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6247 | } |
| 6248 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6249 | 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] | 6250 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6251 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 6252 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6253 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6254 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6255 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6256 | return ERR_PTR(-ENOMEM); |
| 6257 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 6258 | allocate_vpid(vmx); |
| 6259 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6260 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 6261 | if (err) |
| 6262 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6263 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6264 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 6265 | err = -ENOMEM; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6266 | if (!vmx->guest_msrs) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6267 | goto uninit_vcpu; |
| 6268 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6269 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6270 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 6271 | vmx->loaded_vmcs->vmcs = alloc_vmcs(); |
| 6272 | if (!vmx->loaded_vmcs->vmcs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6273 | goto free_msrs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6274 | if (!vmm_exclusive) |
| 6275 | kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id()))); |
| 6276 | loaded_vmcs_init(vmx->loaded_vmcs); |
| 6277 | if (!vmm_exclusive) |
| 6278 | kvm_cpu_vmxoff(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6279 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6280 | cpu = get_cpu(); |
| 6281 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 6282 | vmx->vcpu.cpu = cpu; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 6283 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6284 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6285 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6286 | if (err) |
| 6287 | goto free_vmcs; |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 6288 | if (vm_need_virtualize_apic_accesses(kvm)) |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 6289 | err = alloc_apic_access_page(kvm); |
| 6290 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 6291 | goto free_vmcs; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6292 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 6293 | if (enable_ept) { |
| 6294 | if (!kvm->arch.ept_identity_map_addr) |
| 6295 | kvm->arch.ept_identity_map_addr = |
| 6296 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 6297 | err = -ENOMEM; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 6298 | if (alloc_identity_pagetable(kvm) != 0) |
| 6299 | goto free_vmcs; |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 6300 | if (!init_rmode_identity_map(kvm)) |
| 6301 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 6302 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 6303 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 6304 | vmx->nested.current_vmptr = -1ull; |
| 6305 | vmx->nested.current_vmcs12 = NULL; |
| 6306 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6307 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6308 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6309 | free_vmcs: |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6310 | free_vmcs(vmx->loaded_vmcs->vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6311 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6312 | kfree(vmx->guest_msrs); |
| 6313 | uninit_vcpu: |
| 6314 | kvm_vcpu_uninit(&vmx->vcpu); |
| 6315 | free_vcpu: |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 6316 | free_vpid(vmx); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 6317 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6318 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6319 | } |
| 6320 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6321 | static void __init vmx_check_processor_compat(void *rtn) |
| 6322 | { |
| 6323 | struct vmcs_config vmcs_conf; |
| 6324 | |
| 6325 | *(int *)rtn = 0; |
| 6326 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 6327 | *(int *)rtn = -EIO; |
| 6328 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 6329 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 6330 | smp_processor_id()); |
| 6331 | *(int *)rtn = -EIO; |
| 6332 | } |
| 6333 | } |
| 6334 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 6335 | static int get_ept_level(void) |
| 6336 | { |
| 6337 | return VMX_EPT_DEFAULT_GAW + 1; |
| 6338 | } |
| 6339 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6340 | 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] | 6341 | { |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6342 | u64 ret; |
| 6343 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6344 | /* For VT-d and EPT combination |
| 6345 | * 1. MMIO: always map as UC |
| 6346 | * 2. EPT with VT-d: |
| 6347 | * a. VT-d without snooping control feature: can't guarantee the |
| 6348 | * result, try to trust guest. |
| 6349 | * b. VT-d with snooping control feature: snooping control feature of |
| 6350 | * VT-d engine can guarantee the cache correctness. Just set it |
| 6351 | * 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] | 6352 | * 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] | 6353 | * consistent with host MTRR |
| 6354 | */ |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6355 | if (is_mmio) |
| 6356 | ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT; |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6357 | else if (vcpu->kvm->arch.iommu_domain && |
| 6358 | !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)) |
| 6359 | ret = kvm_get_guest_memory_type(vcpu, gfn) << |
| 6360 | VMX_EPT_MT_EPTE_SHIFT; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6361 | else |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6362 | ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT) |
Sheng Yang | a19a6d1 | 2010-02-09 16:41:53 +0800 | [diff] [blame] | 6363 | | VMX_EPT_IPAT_BIT; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6364 | |
| 6365 | return ret; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 6366 | } |
| 6367 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 6368 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 6369 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 6370 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 6371 | return PT_DIRECTORY_LEVEL; |
| 6372 | else |
| 6373 | /* For shadow and EPT supported 1GB page */ |
| 6374 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 6375 | } |
| 6376 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 6377 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 6378 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 6379 | struct kvm_cpuid_entry2 *best; |
| 6380 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6381 | u32 exec_control; |
| 6382 | |
| 6383 | vmx->rdtscp_enabled = false; |
| 6384 | if (vmx_rdtscp_supported()) { |
| 6385 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 6386 | if (exec_control & SECONDARY_EXEC_RDTSCP) { |
| 6387 | best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 6388 | if (best && (best->edx & bit(X86_FEATURE_RDTSCP))) |
| 6389 | vmx->rdtscp_enabled = true; |
| 6390 | else { |
| 6391 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 6392 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 6393 | exec_control); |
| 6394 | } |
| 6395 | } |
| 6396 | } |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 6397 | } |
| 6398 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 6399 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 6400 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 6401 | if (func == 1 && nested) |
| 6402 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 6403 | } |
| 6404 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 6405 | /* |
| 6406 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 6407 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
| 6408 | * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2 |
| 6409 | * guest in a way that will both be appropriate to L1's requests, and our |
| 6410 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 6411 | * function also has additional necessary side-effects, like setting various |
| 6412 | * vcpu->arch fields. |
| 6413 | */ |
| 6414 | static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 6415 | { |
| 6416 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6417 | u32 exec_control; |
| 6418 | |
| 6419 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 6420 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 6421 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 6422 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 6423 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 6424 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 6425 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 6426 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 6427 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 6428 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 6429 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 6430 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 6431 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 6432 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 6433 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 6434 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 6435 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 6436 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 6437 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 6438 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 6439 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 6440 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 6441 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 6442 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 6443 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 6444 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 6445 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 6446 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 6447 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 6448 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 6449 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 6450 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 6451 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 6452 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 6453 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 6454 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
| 6455 | |
| 6456 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 6457 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 6458 | vmcs12->vm_entry_intr_info_field); |
| 6459 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 6460 | vmcs12->vm_entry_exception_error_code); |
| 6461 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 6462 | vmcs12->vm_entry_instruction_len); |
| 6463 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 6464 | vmcs12->guest_interruptibility_info); |
| 6465 | vmcs_write32(GUEST_ACTIVITY_STATE, vmcs12->guest_activity_state); |
| 6466 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 6467 | vmcs_writel(GUEST_DR7, vmcs12->guest_dr7); |
| 6468 | vmcs_writel(GUEST_RFLAGS, vmcs12->guest_rflags); |
| 6469 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 6470 | vmcs12->guest_pending_dbg_exceptions); |
| 6471 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 6472 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 6473 | |
| 6474 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 6475 | |
| 6476 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, |
| 6477 | (vmcs_config.pin_based_exec_ctrl | |
| 6478 | vmcs12->pin_based_vm_exec_control)); |
| 6479 | |
| 6480 | /* |
| 6481 | * Whether page-faults are trapped is determined by a combination of |
| 6482 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 6483 | * If enable_ept, L0 doesn't care about page faults and we should |
| 6484 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 6485 | * care about (at least some) page faults, and because it is not easy |
| 6486 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 6487 | * to exit on each and every L2 page fault. This is done by setting |
| 6488 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 6489 | * Note that below we don't need special code to set EB.PF beyond the |
| 6490 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 6491 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 6492 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 6493 | * |
| 6494 | * A problem with this approach (when !enable_ept) is that L1 may be |
| 6495 | * injected with more page faults than it asked for. This could have |
| 6496 | * caused problems, but in practice existing hypervisors don't care. |
| 6497 | * To fix this, we will need to emulate the PFEC checking (on the L1 |
| 6498 | * page tables), using walk_addr(), when injecting PFs to L1. |
| 6499 | */ |
| 6500 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 6501 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 6502 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 6503 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 6504 | |
| 6505 | if (cpu_has_secondary_exec_ctrls()) { |
| 6506 | u32 exec_control = vmx_secondary_exec_control(vmx); |
| 6507 | if (!vmx->rdtscp_enabled) |
| 6508 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 6509 | /* Take the following fields only from vmcs12 */ |
| 6510 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6511 | if (nested_cpu_has(vmcs12, |
| 6512 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 6513 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 6514 | |
| 6515 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) { |
| 6516 | /* |
| 6517 | * Translate L1 physical address to host physical |
| 6518 | * address for vmcs02. Keep the page pinned, so this |
| 6519 | * physical address remains valid. We keep a reference |
| 6520 | * to it so we can release it later. |
| 6521 | */ |
| 6522 | if (vmx->nested.apic_access_page) /* shouldn't happen */ |
| 6523 | nested_release_page(vmx->nested.apic_access_page); |
| 6524 | vmx->nested.apic_access_page = |
| 6525 | nested_get_page(vcpu, vmcs12->apic_access_addr); |
| 6526 | /* |
| 6527 | * If translation failed, no matter: This feature asks |
| 6528 | * to exit when accessing the given address, and if it |
| 6529 | * can never be accessed, this feature won't do |
| 6530 | * anything anyway. |
| 6531 | */ |
| 6532 | if (!vmx->nested.apic_access_page) |
| 6533 | exec_control &= |
| 6534 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6535 | else |
| 6536 | vmcs_write64(APIC_ACCESS_ADDR, |
| 6537 | page_to_phys(vmx->nested.apic_access_page)); |
| 6538 | } |
| 6539 | |
| 6540 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 6541 | } |
| 6542 | |
| 6543 | |
| 6544 | /* |
| 6545 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 6546 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 6547 | * Other fields are different per CPU, and will be set later when |
| 6548 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 6549 | */ |
| 6550 | vmx_set_constant_host_state(); |
| 6551 | |
| 6552 | /* |
| 6553 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 6554 | * entry, but only if the current (host) sp changed from the value |
| 6555 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 6556 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 6557 | * here we just force the write to happen on entry. |
| 6558 | */ |
| 6559 | vmx->host_rsp = 0; |
| 6560 | |
| 6561 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 6562 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 6563 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 6564 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 6565 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
| 6566 | /* |
| 6567 | * Merging of IO and MSR bitmaps not currently supported. |
| 6568 | * Rather, exit every time. |
| 6569 | */ |
| 6570 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 6571 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 6572 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 6573 | |
| 6574 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 6575 | |
| 6576 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 6577 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 6578 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 6579 | */ |
| 6580 | update_exception_bitmap(vcpu); |
| 6581 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 6582 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 6583 | |
| 6584 | /* Note: IA32_MODE, LOAD_IA32_EFER are modified by vmx_set_efer below */ |
| 6585 | vmcs_write32(VM_EXIT_CONTROLS, |
| 6586 | vmcs12->vm_exit_controls | vmcs_config.vmexit_ctrl); |
| 6587 | vmcs_write32(VM_ENTRY_CONTROLS, vmcs12->vm_entry_controls | |
| 6588 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 6589 | |
| 6590 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) |
| 6591 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
| 6592 | else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 6593 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 6594 | |
| 6595 | |
| 6596 | set_cr4_guest_host_mask(vmx); |
| 6597 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 6598 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 6599 | vmcs_write64(TSC_OFFSET, |
| 6600 | vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset); |
| 6601 | else |
| 6602 | vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 6603 | |
| 6604 | if (enable_vpid) { |
| 6605 | /* |
| 6606 | * Trivially support vpid by letting L2s share their parent |
| 6607 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 6608 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 6609 | */ |
| 6610 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 6611 | vmx_flush_tlb(vcpu); |
| 6612 | } |
| 6613 | |
| 6614 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) |
| 6615 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
| 6616 | if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 6617 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 6618 | else |
| 6619 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 6620 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 6621 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 6622 | |
| 6623 | /* |
| 6624 | * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified |
| 6625 | * TS bit (for lazy fpu) and bits which we consider mandatory enabled. |
| 6626 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 6627 | * the specifications by L1; It's not enough to take |
| 6628 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 6629 | * have more bits than L1 expected. |
| 6630 | */ |
| 6631 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 6632 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 6633 | |
| 6634 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 6635 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 6636 | |
| 6637 | /* shadow page tables on either EPT or shadow page tables */ |
| 6638 | kvm_set_cr3(vcpu, vmcs12->guest_cr3); |
| 6639 | kvm_mmu_reset_context(vcpu); |
| 6640 | |
| 6641 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 6642 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
| 6643 | } |
| 6644 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 6645 | /* |
| 6646 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 6647 | * for running an L2 nested guest. |
| 6648 | */ |
| 6649 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 6650 | { |
| 6651 | struct vmcs12 *vmcs12; |
| 6652 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6653 | int cpu; |
| 6654 | struct loaded_vmcs *vmcs02; |
| 6655 | |
| 6656 | if (!nested_vmx_check_permission(vcpu) || |
| 6657 | !nested_vmx_check_vmcs12(vcpu)) |
| 6658 | return 1; |
| 6659 | |
| 6660 | skip_emulated_instruction(vcpu); |
| 6661 | vmcs12 = get_vmcs12(vcpu); |
| 6662 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 6663 | /* |
| 6664 | * The nested entry process starts with enforcing various prerequisites |
| 6665 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 6666 | * they fail: As the SDM explains, some conditions should cause the |
| 6667 | * instruction to fail, while others will cause the instruction to seem |
| 6668 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 6669 | * To speed up the normal (success) code path, we should avoid checking |
| 6670 | * for misconfigurations which will anyway be caught by the processor |
| 6671 | * when using the merged vmcs02. |
| 6672 | */ |
| 6673 | if (vmcs12->launch_state == launch) { |
| 6674 | nested_vmx_failValid(vcpu, |
| 6675 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 6676 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 6677 | return 1; |
| 6678 | } |
| 6679 | |
| 6680 | if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) && |
| 6681 | !IS_ALIGNED(vmcs12->msr_bitmap, PAGE_SIZE)) { |
| 6682 | /*TODO: Also verify bits beyond physical address width are 0*/ |
| 6683 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 6684 | return 1; |
| 6685 | } |
| 6686 | |
| 6687 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
| 6688 | !IS_ALIGNED(vmcs12->apic_access_addr, PAGE_SIZE)) { |
| 6689 | /*TODO: Also verify bits beyond physical address width are 0*/ |
| 6690 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 6691 | return 1; |
| 6692 | } |
| 6693 | |
| 6694 | if (vmcs12->vm_entry_msr_load_count > 0 || |
| 6695 | vmcs12->vm_exit_msr_load_count > 0 || |
| 6696 | vmcs12->vm_exit_msr_store_count > 0) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 6697 | pr_warn_ratelimited("%s: VMCS MSR_{LOAD,STORE} unsupported\n", |
| 6698 | __func__); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 6699 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 6700 | return 1; |
| 6701 | } |
| 6702 | |
| 6703 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
| 6704 | nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high) || |
| 6705 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
| 6706 | nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) || |
| 6707 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
| 6708 | nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) || |
| 6709 | !vmx_control_verify(vmcs12->vm_exit_controls, |
| 6710 | nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high) || |
| 6711 | !vmx_control_verify(vmcs12->vm_entry_controls, |
| 6712 | nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high)) |
| 6713 | { |
| 6714 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 6715 | return 1; |
| 6716 | } |
| 6717 | |
| 6718 | if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) || |
| 6719 | ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 6720 | nested_vmx_failValid(vcpu, |
| 6721 | VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
| 6722 | return 1; |
| 6723 | } |
| 6724 | |
| 6725 | if (((vmcs12->guest_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) || |
| 6726 | ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 6727 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 6728 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 6729 | return 1; |
| 6730 | } |
| 6731 | if (vmcs12->vmcs_link_pointer != -1ull) { |
| 6732 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 6733 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR); |
| 6734 | return 1; |
| 6735 | } |
| 6736 | |
| 6737 | /* |
| 6738 | * We're finally done with prerequisite checking, and can start with |
| 6739 | * the nested entry. |
| 6740 | */ |
| 6741 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 6742 | vmcs02 = nested_get_current_vmcs02(vmx); |
| 6743 | if (!vmcs02) |
| 6744 | return -ENOMEM; |
| 6745 | |
| 6746 | enter_guest_mode(vcpu); |
| 6747 | |
| 6748 | vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET); |
| 6749 | |
| 6750 | cpu = get_cpu(); |
| 6751 | vmx->loaded_vmcs = vmcs02; |
| 6752 | vmx_vcpu_put(vcpu); |
| 6753 | vmx_vcpu_load(vcpu, cpu); |
| 6754 | vcpu->cpu = cpu; |
| 6755 | put_cpu(); |
| 6756 | |
| 6757 | vmcs12->launch_state = 1; |
| 6758 | |
| 6759 | prepare_vmcs02(vcpu, vmcs12); |
| 6760 | |
| 6761 | /* |
| 6762 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 6763 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 6764 | * returned as far as L1 is concerned. It will only return (and set |
| 6765 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 6766 | */ |
| 6767 | return 1; |
| 6768 | } |
| 6769 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 6770 | /* |
| 6771 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 6772 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 6773 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 6774 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 6775 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 6776 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 6777 | * didn't trap the bit, because if L1 did, so would L0). |
| 6778 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 6779 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 6780 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 6781 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 6782 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 6783 | * changed these bits, and therefore they need to be updated, but L0 |
| 6784 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 6785 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 6786 | */ |
| 6787 | static inline unsigned long |
| 6788 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 6789 | { |
| 6790 | return |
| 6791 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 6792 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 6793 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 6794 | vcpu->arch.cr0_guest_owned_bits)); |
| 6795 | } |
| 6796 | |
| 6797 | static inline unsigned long |
| 6798 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 6799 | { |
| 6800 | return |
| 6801 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 6802 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 6803 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 6804 | vcpu->arch.cr4_guest_owned_bits)); |
| 6805 | } |
| 6806 | |
| 6807 | /* |
| 6808 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 6809 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 6810 | * and this function updates it to reflect the changes to the guest state while |
| 6811 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 6812 | * without going back to L1), and to reflect the exit reason. |
| 6813 | * Note that we do not have to copy here all VMCS fields, just those that |
| 6814 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 6815 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 6816 | * which already writes to vmcs12 directly. |
| 6817 | */ |
| 6818 | void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 6819 | { |
| 6820 | /* update guest state fields: */ |
| 6821 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 6822 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 6823 | |
| 6824 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 6825 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 6826 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 6827 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 6828 | |
| 6829 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 6830 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 6831 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 6832 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 6833 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 6834 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 6835 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 6836 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 6837 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 6838 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 6839 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 6840 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 6841 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 6842 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 6843 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 6844 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 6845 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 6846 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 6847 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 6848 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 6849 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 6850 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 6851 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 6852 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 6853 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 6854 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 6855 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 6856 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 6857 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 6858 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 6859 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 6860 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 6861 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 6862 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 6863 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 6864 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 6865 | |
| 6866 | vmcs12->guest_activity_state = vmcs_read32(GUEST_ACTIVITY_STATE); |
| 6867 | vmcs12->guest_interruptibility_info = |
| 6868 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 6869 | vmcs12->guest_pending_dbg_exceptions = |
| 6870 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
| 6871 | |
| 6872 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 6873 | * the relevant bit asks not to trap the change */ |
| 6874 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 6875 | if (vmcs12->vm_entry_controls & VM_EXIT_SAVE_IA32_PAT) |
| 6876 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
| 6877 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 6878 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 6879 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
| 6880 | |
| 6881 | /* update exit information fields: */ |
| 6882 | |
| 6883 | vmcs12->vm_exit_reason = vmcs_read32(VM_EXIT_REASON); |
| 6884 | vmcs12->exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6885 | |
| 6886 | vmcs12->vm_exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 6887 | vmcs12->vm_exit_intr_error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 6888 | vmcs12->idt_vectoring_info_field = |
| 6889 | vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 6890 | vmcs12->idt_vectoring_error_code = |
| 6891 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 6892 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 6893 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 6894 | |
| 6895 | /* clear vm-entry fields which are to be cleared on exit */ |
| 6896 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 6897 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
| 6898 | } |
| 6899 | |
| 6900 | /* |
| 6901 | * A part of what we need to when the nested L2 guest exits and we want to |
| 6902 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 6903 | * in vmcs12. |
| 6904 | * This function is to be called not only on normal nested exit, but also on |
| 6905 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 6906 | * Failures During or After Loading Guest State"). |
| 6907 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 6908 | */ |
| 6909 | void load_vmcs12_host_state(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 6910 | { |
| 6911 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 6912 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
| 6913 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 6914 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 6915 | else |
| 6916 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 6917 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 6918 | |
| 6919 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 6920 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
| 6921 | /* |
| 6922 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 6923 | * actually changed, because it depends on the current state of |
| 6924 | * fpu_active (which may have changed). |
| 6925 | * Note that vmx_set_cr0 refers to efer set above. |
| 6926 | */ |
| 6927 | kvm_set_cr0(vcpu, vmcs12->host_cr0); |
| 6928 | /* |
| 6929 | * If we did fpu_activate()/fpu_deactivate() during L2's run, we need |
| 6930 | * to apply the same changes to L1's vmcs. We just set cr0 correctly, |
| 6931 | * but we also need to update cr0_guest_host_mask and exception_bitmap. |
| 6932 | */ |
| 6933 | update_exception_bitmap(vcpu); |
| 6934 | vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0); |
| 6935 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 6936 | |
| 6937 | /* |
| 6938 | * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01 |
| 6939 | * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask(); |
| 6940 | */ |
| 6941 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 6942 | kvm_set_cr4(vcpu, vmcs12->host_cr4); |
| 6943 | |
| 6944 | /* shadow page tables on either EPT or shadow page tables */ |
| 6945 | kvm_set_cr3(vcpu, vmcs12->host_cr3); |
| 6946 | kvm_mmu_reset_context(vcpu); |
| 6947 | |
| 6948 | if (enable_vpid) { |
| 6949 | /* |
| 6950 | * Trivially support vpid by letting L2s share their parent |
| 6951 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 6952 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 6953 | */ |
| 6954 | vmx_flush_tlb(vcpu); |
| 6955 | } |
| 6956 | |
| 6957 | |
| 6958 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 6959 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 6960 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 6961 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 6962 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
| 6963 | vmcs_writel(GUEST_TR_BASE, vmcs12->host_tr_base); |
| 6964 | vmcs_writel(GUEST_GS_BASE, vmcs12->host_gs_base); |
| 6965 | vmcs_writel(GUEST_FS_BASE, vmcs12->host_fs_base); |
| 6966 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->host_es_selector); |
| 6967 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->host_cs_selector); |
| 6968 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->host_ss_selector); |
| 6969 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->host_ds_selector); |
| 6970 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->host_fs_selector); |
| 6971 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->host_gs_selector); |
| 6972 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->host_tr_selector); |
| 6973 | |
| 6974 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) |
| 6975 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
| 6976 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 6977 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 6978 | vmcs12->host_ia32_perf_global_ctrl); |
| 6979 | } |
| 6980 | |
| 6981 | /* |
| 6982 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 6983 | * and modify vmcs12 to make it see what it would expect to see there if |
| 6984 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 6985 | */ |
| 6986 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu) |
| 6987 | { |
| 6988 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6989 | int cpu; |
| 6990 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6991 | |
| 6992 | leave_guest_mode(vcpu); |
| 6993 | prepare_vmcs12(vcpu, vmcs12); |
| 6994 | |
| 6995 | cpu = get_cpu(); |
| 6996 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 6997 | vmx_vcpu_put(vcpu); |
| 6998 | vmx_vcpu_load(vcpu, cpu); |
| 6999 | vcpu->cpu = cpu; |
| 7000 | put_cpu(); |
| 7001 | |
| 7002 | /* if no vmcs02 cache requested, remove the one we used */ |
| 7003 | if (VMCS02_POOL_SIZE == 0) |
| 7004 | nested_free_vmcs02(vmx, vmx->nested.current_vmptr); |
| 7005 | |
| 7006 | load_vmcs12_host_state(vcpu, vmcs12); |
| 7007 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 7008 | /* Update TSC_OFFSET if TSC was changed while L2 ran */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 7009 | vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset); |
| 7010 | |
| 7011 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 7012 | vmx->host_rsp = 0; |
| 7013 | |
| 7014 | /* Unpin physical memory we referred to in vmcs02 */ |
| 7015 | if (vmx->nested.apic_access_page) { |
| 7016 | nested_release_page(vmx->nested.apic_access_page); |
| 7017 | vmx->nested.apic_access_page = 0; |
| 7018 | } |
| 7019 | |
| 7020 | /* |
| 7021 | * Exiting from L2 to L1, we're now back to L1 which thinks it just |
| 7022 | * finished a VMLAUNCH or VMRESUME instruction, so we need to set the |
| 7023 | * success or failure flag accordingly. |
| 7024 | */ |
| 7025 | if (unlikely(vmx->fail)) { |
| 7026 | vmx->fail = 0; |
| 7027 | nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 7028 | } else |
| 7029 | nested_vmx_succeed(vcpu); |
| 7030 | } |
| 7031 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 7032 | /* |
| 7033 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 7034 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 7035 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 7036 | * It should only be called before L2 actually succeeded to run, and when |
| 7037 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 7038 | */ |
| 7039 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 7040 | struct vmcs12 *vmcs12, |
| 7041 | u32 reason, unsigned long qualification) |
| 7042 | { |
| 7043 | load_vmcs12_host_state(vcpu, vmcs12); |
| 7044 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 7045 | vmcs12->exit_qualification = qualification; |
| 7046 | nested_vmx_succeed(vcpu); |
| 7047 | } |
| 7048 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 7049 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 7050 | struct x86_instruction_info *info, |
| 7051 | enum x86_intercept_stage stage) |
| 7052 | { |
| 7053 | return X86EMUL_CONTINUE; |
| 7054 | } |
| 7055 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 7056 | static struct kvm_x86_ops vmx_x86_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7057 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 7058 | .disabled_by_bios = vmx_disabled_by_bios, |
| 7059 | .hardware_setup = hardware_setup, |
| 7060 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 7061 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7062 | .hardware_enable = hardware_enable, |
| 7063 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 7064 | .cpu_has_accelerated_tpr = report_flexpriority, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7065 | |
| 7066 | .vcpu_create = vmx_create_vcpu, |
| 7067 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 7068 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7069 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 7070 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7071 | .vcpu_load = vmx_vcpu_load, |
| 7072 | .vcpu_put = vmx_vcpu_put, |
| 7073 | |
| 7074 | .set_guest_debug = set_guest_debug, |
| 7075 | .get_msr = vmx_get_msr, |
| 7076 | .set_msr = vmx_set_msr, |
| 7077 | .get_segment_base = vmx_get_segment_base, |
| 7078 | .get_segment = vmx_get_segment, |
| 7079 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 7080 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7081 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 7082 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 7083 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 7084 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7085 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7086 | .set_cr3 = vmx_set_cr3, |
| 7087 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7088 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7089 | .get_idt = vmx_get_idt, |
| 7090 | .set_idt = vmx_set_idt, |
| 7091 | .get_gdt = vmx_get_gdt, |
| 7092 | .set_gdt = vmx_set_gdt, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7093 | .set_dr7 = vmx_set_dr7, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 7094 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7095 | .get_rflags = vmx_get_rflags, |
| 7096 | .set_rflags = vmx_set_rflags, |
Avi Kivity | ebcbab4 | 2010-02-07 11:56:52 +0200 | [diff] [blame] | 7097 | .fpu_activate = vmx_fpu_activate, |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 7098 | .fpu_deactivate = vmx_fpu_deactivate, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7099 | |
| 7100 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7101 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7102 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 7103 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7104 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 7105 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 7106 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7107 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 7108 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7109 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 7110 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 7111 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 7112 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7113 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 7114 | .get_nmi_mask = vmx_get_nmi_mask, |
| 7115 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7116 | .enable_nmi_window = enable_nmi_window, |
| 7117 | .enable_irq_window = enable_irq_window, |
| 7118 | .update_cr8_intercept = update_cr8_intercept, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7119 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 7120 | .set_tss_addr = vmx_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 7121 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 7122 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7123 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 7124 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 7125 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 7126 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 7127 | |
| 7128 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 7129 | |
| 7130 | .rdtscp_supported = vmx_rdtscp_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 7131 | |
| 7132 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 7133 | |
| 7134 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 7135 | |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 7136 | .set_tsc_khz = vmx_set_tsc_khz, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 7137 | .write_tsc_offset = vmx_write_tsc_offset, |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 7138 | .adjust_tsc_offset = vmx_adjust_tsc_offset, |
Joerg Roedel | 857e409 | 2011-03-25 09:44:50 +0100 | [diff] [blame] | 7139 | .compute_tsc_offset = vmx_compute_tsc_offset, |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 7140 | .read_l1_tsc = vmx_read_l1_tsc, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 7141 | |
| 7142 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 7143 | |
| 7144 | .check_intercept = vmx_check_intercept, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7145 | }; |
| 7146 | |
| 7147 | static int __init vmx_init(void) |
| 7148 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 7149 | int r, i; |
| 7150 | |
| 7151 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 7152 | |
| 7153 | for (i = 0; i < NR_VMX_MSR; ++i) |
| 7154 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7155 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7156 | vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7157 | if (!vmx_io_bitmap_a) |
| 7158 | return -ENOMEM; |
| 7159 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7160 | vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7161 | if (!vmx_io_bitmap_b) { |
| 7162 | r = -ENOMEM; |
| 7163 | goto out; |
| 7164 | } |
| 7165 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7166 | vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7167 | if (!vmx_msr_bitmap_legacy) { |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7168 | r = -ENOMEM; |
| 7169 | goto out1; |
| 7170 | } |
| 7171 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7172 | vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7173 | if (!vmx_msr_bitmap_longmode) { |
| 7174 | r = -ENOMEM; |
| 7175 | goto out2; |
| 7176 | } |
| 7177 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7178 | /* |
| 7179 | * Allow direct access to the PC debug port (it is often used for I/O |
| 7180 | * delays, but the vmexits simply slow things down). |
| 7181 | */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7182 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); |
| 7183 | clear_bit(0x80, vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7184 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7185 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7186 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7187 | memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE); |
| 7188 | memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7189 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 7190 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 7191 | |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 7192 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
| 7193 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7194 | if (r) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7195 | goto out3; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7196 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7197 | vmx_disable_intercept_for_msr(MSR_FS_BASE, false); |
| 7198 | vmx_disable_intercept_for_msr(MSR_GS_BASE, false); |
| 7199 | vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true); |
| 7200 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false); |
| 7201 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false); |
| 7202 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7203 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 7204 | if (enable_ept) { |
Sheng Yang | 534e38b | 2008-09-08 15:12:30 +0800 | [diff] [blame] | 7205 | kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 7206 | VMX_EPT_EXECUTABLE_MASK); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 7207 | ept_set_mmio_spte_mask(); |
Sheng Yang | 5fdbcb9 | 2008-07-16 09:25:40 +0800 | [diff] [blame] | 7208 | kvm_enable_tdp(); |
| 7209 | } else |
| 7210 | kvm_disable_tdp(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7211 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7212 | return 0; |
| 7213 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7214 | out3: |
| 7215 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7216 | out2: |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7217 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7218 | out1: |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7219 | free_page((unsigned long)vmx_io_bitmap_b); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7220 | out: |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7221 | free_page((unsigned long)vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7222 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7223 | } |
| 7224 | |
| 7225 | static void __exit vmx_exit(void) |
| 7226 | { |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 7227 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
| 7228 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 7229 | free_page((unsigned long)vmx_io_bitmap_b); |
| 7230 | free_page((unsigned long)vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7231 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 7232 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7233 | } |
| 7234 | |
| 7235 | module_init(vmx_init) |
| 7236 | module_exit(vmx_exit) |