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 bypass_guest_pf = 1; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 53 | module_param(bypass_guest_pf, bool, S_IRUGO); |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 54 | |
Avi Kivity | 4462d21 | 2009-03-23 17:53:37 +0200 | [diff] [blame] | 55 | static int __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 56 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 57 | |
Avi Kivity | 4462d21 | 2009-03-23 17:53:37 +0200 | [diff] [blame] | 58 | static int __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 59 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 60 | |
Avi Kivity | 4462d21 | 2009-03-23 17:53:37 +0200 | [diff] [blame] | 61 | static int __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 62 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 63 | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 64 | static int __read_mostly enable_unrestricted_guest = 1; |
| 65 | module_param_named(unrestricted_guest, |
| 66 | enable_unrestricted_guest, bool, S_IRUGO); |
| 67 | |
Avi Kivity | 4462d21 | 2009-03-23 17:53:37 +0200 | [diff] [blame] | 68 | static int __read_mostly emulate_invalid_guest_state = 0; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 69 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 70 | |
Dongxiao Xu | b923e62 | 2010-05-11 18:29:45 +0800 | [diff] [blame] | 71 | static int __read_mostly vmm_exclusive = 1; |
| 72 | module_param(vmm_exclusive, bool, S_IRUGO); |
| 73 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 74 | static int __read_mostly yield_on_hlt = 1; |
| 75 | module_param(yield_on_hlt, 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 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 121 | #define NR_AUTOLOAD_MSRS 1 |
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; |
| 349 | /* |
| 350 | * Guest pages referred to in vmcs02 with host-physical pointers, so |
| 351 | * we must keep them pinned while L2 runs. |
| 352 | */ |
| 353 | struct page *apic_access_page; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 354 | }; |
| 355 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 356 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 357 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 358 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 359 | u8 fail; |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 360 | u8 cpl; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 361 | bool nmi_known_unmasked; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 362 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 363 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 364 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 365 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 366 | int nmsrs; |
| 367 | int save_nmsrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 368 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 369 | u64 msr_host_kernel_gs_base; |
| 370 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 371 | #endif |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 372 | /* |
| 373 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 374 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 375 | * guest (L2), it points to a different VMCS. |
| 376 | */ |
| 377 | struct loaded_vmcs vmcs01; |
| 378 | struct loaded_vmcs *loaded_vmcs; |
| 379 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 380 | struct msr_autoload { |
| 381 | unsigned nr; |
| 382 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 383 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 384 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 385 | struct { |
| 386 | int loaded; |
| 387 | u16 fs_sel, gs_sel, ldt_sel; |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 388 | int gs_ldt_reload_needed; |
| 389 | int fs_reload_needed; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 390 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 391 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 392 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 393 | ulong save_rflags; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 394 | struct kvm_save_segment { |
| 395 | u16 selector; |
| 396 | unsigned long base; |
| 397 | u32 limit; |
| 398 | u32 ar; |
| 399 | } tr, es, ds, fs, gs; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 400 | } rmode; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 401 | struct { |
| 402 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
| 403 | struct kvm_save_segment seg[8]; |
| 404 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 405 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 406 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 407 | |
| 408 | /* Support for vnmi-less CPUs */ |
| 409 | int soft_vnmi_blocked; |
| 410 | ktime_t entry_time; |
| 411 | s64 vnmi_blocked_time; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 412 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 413 | |
| 414 | bool rdtscp_enabled; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 415 | |
| 416 | /* Support for a guest hypervisor (nested VMX) */ |
| 417 | struct nested_vmx nested; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 418 | }; |
| 419 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 420 | enum segment_cache_field { |
| 421 | SEG_FIELD_SEL = 0, |
| 422 | SEG_FIELD_BASE = 1, |
| 423 | SEG_FIELD_LIMIT = 2, |
| 424 | SEG_FIELD_AR = 3, |
| 425 | |
| 426 | SEG_FIELD_NR = 4 |
| 427 | }; |
| 428 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 429 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 430 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 431 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 432 | } |
| 433 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 434 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
| 435 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) |
| 436 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ |
| 437 | [number##_HIGH] = VMCS12_OFFSET(name)+4 |
| 438 | |
| 439 | static unsigned short vmcs_field_to_offset_table[] = { |
| 440 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
| 441 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 442 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 443 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 444 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 445 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 446 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 447 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 448 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
| 449 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 450 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 451 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 452 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 453 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 454 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 455 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 456 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 457 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 458 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 459 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 460 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 461 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
| 462 | FIELD64(TSC_OFFSET, tsc_offset), |
| 463 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 464 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
| 465 | FIELD64(EPT_POINTER, ept_pointer), |
| 466 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 467 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 468 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 469 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 470 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 471 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 472 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 473 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 474 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 475 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
| 476 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 477 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 478 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 479 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 480 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 481 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 482 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 483 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 484 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 485 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 486 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 487 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 488 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 489 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 490 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 491 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 492 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 493 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 494 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 495 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 496 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 497 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 498 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 499 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 500 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 501 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 502 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 503 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 504 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 505 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 506 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 507 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 508 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 509 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 510 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 511 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 512 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 513 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 514 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 515 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 516 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 517 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 518 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 519 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 520 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 521 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 522 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 523 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 524 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
| 525 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 526 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 527 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 528 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 529 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 530 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 531 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 532 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 533 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 534 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 535 | FIELD(GUEST_CR0, guest_cr0), |
| 536 | FIELD(GUEST_CR3, guest_cr3), |
| 537 | FIELD(GUEST_CR4, guest_cr4), |
| 538 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 539 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 540 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 541 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 542 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 543 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 544 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 545 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 546 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 547 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 548 | FIELD(GUEST_DR7, guest_dr7), |
| 549 | FIELD(GUEST_RSP, guest_rsp), |
| 550 | FIELD(GUEST_RIP, guest_rip), |
| 551 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 552 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 553 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 554 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 555 | FIELD(HOST_CR0, host_cr0), |
| 556 | FIELD(HOST_CR3, host_cr3), |
| 557 | FIELD(HOST_CR4, host_cr4), |
| 558 | FIELD(HOST_FS_BASE, host_fs_base), |
| 559 | FIELD(HOST_GS_BASE, host_gs_base), |
| 560 | FIELD(HOST_TR_BASE, host_tr_base), |
| 561 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 562 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 563 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 564 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 565 | FIELD(HOST_RSP, host_rsp), |
| 566 | FIELD(HOST_RIP, host_rip), |
| 567 | }; |
| 568 | static const int max_vmcs_field = ARRAY_SIZE(vmcs_field_to_offset_table); |
| 569 | |
| 570 | static inline short vmcs_field_to_offset(unsigned long field) |
| 571 | { |
| 572 | if (field >= max_vmcs_field || vmcs_field_to_offset_table[field] == 0) |
| 573 | return -1; |
| 574 | return vmcs_field_to_offset_table[field]; |
| 575 | } |
| 576 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 577 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 578 | { |
| 579 | return to_vmx(vcpu)->nested.current_vmcs12; |
| 580 | } |
| 581 | |
| 582 | static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr) |
| 583 | { |
| 584 | struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT); |
| 585 | if (is_error_page(page)) { |
| 586 | kvm_release_page_clean(page); |
| 587 | return NULL; |
| 588 | } |
| 589 | return page; |
| 590 | } |
| 591 | |
| 592 | static void nested_release_page(struct page *page) |
| 593 | { |
| 594 | kvm_release_page_dirty(page); |
| 595 | } |
| 596 | |
| 597 | static void nested_release_page_clean(struct page *page) |
| 598 | { |
| 599 | kvm_release_page_clean(page); |
| 600 | } |
| 601 | |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 602 | static u64 construct_eptp(unsigned long root_hpa); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 603 | static void kvm_cpu_vmxon(u64 addr); |
| 604 | static void kvm_cpu_vmxoff(void); |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 605 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 606 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 607 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 608 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 609 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 610 | /* |
| 611 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 612 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 613 | */ |
| 614 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 615 | static DEFINE_PER_CPU(struct desc_ptr, host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 616 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 617 | static unsigned long *vmx_io_bitmap_a; |
| 618 | static unsigned long *vmx_io_bitmap_b; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 619 | static unsigned long *vmx_msr_bitmap_legacy; |
| 620 | static unsigned long *vmx_msr_bitmap_longmode; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 621 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 622 | static bool cpu_has_load_ia32_efer; |
| 623 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 624 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 625 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 626 | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 627 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 628 | int size; |
| 629 | int order; |
| 630 | u32 revision_id; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 631 | u32 pin_based_exec_ctrl; |
| 632 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 633 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 634 | u32 vmexit_ctrl; |
| 635 | u32 vmentry_ctrl; |
| 636 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 637 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 638 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 639 | u32 ept; |
| 640 | u32 vpid; |
| 641 | } vmx_capability; |
| 642 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 643 | #define VMX_SEGMENT_FIELD(seg) \ |
| 644 | [VCPU_SREG_##seg] = { \ |
| 645 | .selector = GUEST_##seg##_SELECTOR, \ |
| 646 | .base = GUEST_##seg##_BASE, \ |
| 647 | .limit = GUEST_##seg##_LIMIT, \ |
| 648 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 649 | } |
| 650 | |
| 651 | static struct kvm_vmx_segment_field { |
| 652 | unsigned selector; |
| 653 | unsigned base; |
| 654 | unsigned limit; |
| 655 | unsigned ar_bytes; |
| 656 | } kvm_vmx_segment_fields[] = { |
| 657 | VMX_SEGMENT_FIELD(CS), |
| 658 | VMX_SEGMENT_FIELD(DS), |
| 659 | VMX_SEGMENT_FIELD(ES), |
| 660 | VMX_SEGMENT_FIELD(FS), |
| 661 | VMX_SEGMENT_FIELD(GS), |
| 662 | VMX_SEGMENT_FIELD(SS), |
| 663 | VMX_SEGMENT_FIELD(TR), |
| 664 | VMX_SEGMENT_FIELD(LDTR), |
| 665 | }; |
| 666 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 667 | static u64 host_efer; |
| 668 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 669 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 670 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 671 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 672 | * 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] | 673 | * away by decrementing the array size. |
| 674 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 675 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 676 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 677 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 678 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 679 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 680 | }; |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 681 | #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 682 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 683 | static inline bool is_page_fault(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 684 | { |
| 685 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 686 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 687 | (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 690 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 691 | { |
| 692 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 693 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 694 | (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 695 | } |
| 696 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 697 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 698 | { |
| 699 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 700 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 701 | (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 702 | } |
| 703 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 704 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 705 | { |
| 706 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 707 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 708 | } |
| 709 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 710 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 711 | { |
| 712 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 713 | INTR_INFO_VALID_MASK)) == |
| 714 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 715 | } |
| 716 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 717 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 718 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 719 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 720 | } |
| 721 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 722 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 723 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 724 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 725 | } |
| 726 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 727 | static inline bool vm_need_tpr_shadow(struct kvm *kvm) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 728 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 729 | return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 730 | } |
| 731 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 732 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 733 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 734 | return vmcs_config.cpu_based_exec_ctrl & |
| 735 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 736 | } |
| 737 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 738 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 739 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 740 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 741 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 742 | } |
| 743 | |
| 744 | static inline bool cpu_has_vmx_flexpriority(void) |
| 745 | { |
| 746 | return cpu_has_vmx_tpr_shadow() && |
| 747 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 748 | } |
| 749 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 750 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 751 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 752 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | static inline bool cpu_has_vmx_eptp_uncacheable(void) |
| 756 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 757 | return vmx_capability.ept & VMX_EPTP_UC_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | static inline bool cpu_has_vmx_eptp_writeback(void) |
| 761 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 762 | return vmx_capability.ept & VMX_EPTP_WB_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 766 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 767 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 768 | } |
| 769 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 770 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 771 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 772 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 773 | } |
| 774 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 775 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 776 | { |
| 777 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 778 | } |
| 779 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 780 | static inline bool cpu_has_vmx_invept_individual_addr(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 781 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 782 | return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 783 | } |
| 784 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 785 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 786 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 787 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 788 | } |
| 789 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 790 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 791 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 792 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 793 | } |
| 794 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 795 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 796 | { |
| 797 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 798 | } |
| 799 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 800 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 801 | { |
| 802 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 803 | } |
| 804 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 805 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 806 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 807 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 808 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 809 | } |
| 810 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 811 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 812 | { |
| 813 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 814 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 815 | } |
| 816 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 817 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 818 | { |
| 819 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 820 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 821 | } |
| 822 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 823 | static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 824 | { |
Gui Jianfeng | 6d3e435 | 2010-01-29 15:36:59 +0800 | [diff] [blame] | 825 | return flexpriority_enabled && irqchip_in_kernel(kvm); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 826 | } |
| 827 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 828 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 829 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 830 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 831 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 832 | } |
| 833 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 834 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 835 | { |
| 836 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 837 | SECONDARY_EXEC_RDTSCP; |
| 838 | } |
| 839 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 840 | static inline bool cpu_has_virtual_nmis(void) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 841 | { |
| 842 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 843 | } |
| 844 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 845 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 846 | { |
| 847 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 848 | SECONDARY_EXEC_WBINVD_EXITING; |
| 849 | } |
| 850 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 851 | static inline bool report_flexpriority(void) |
| 852 | { |
| 853 | return flexpriority_enabled; |
| 854 | } |
| 855 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 856 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 857 | { |
| 858 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 859 | } |
| 860 | |
| 861 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 862 | { |
| 863 | return (vmcs12->cpu_based_vm_exec_control & |
| 864 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 865 | (vmcs12->secondary_vm_exec_control & bit); |
| 866 | } |
| 867 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 868 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 869 | { |
| 870 | int i; |
| 871 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 872 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 873 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 874 | return i; |
| 875 | return -1; |
| 876 | } |
| 877 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 878 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 879 | { |
| 880 | struct { |
| 881 | u64 vpid : 16; |
| 882 | u64 rsvd : 48; |
| 883 | u64 gva; |
| 884 | } operand = { vpid, 0, gva }; |
| 885 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 886 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 887 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 888 | "; ja 1f ; ud2 ; 1:" |
| 889 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 890 | } |
| 891 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 892 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 893 | { |
| 894 | struct { |
| 895 | u64 eptp, gpa; |
| 896 | } operand = {eptp, gpa}; |
| 897 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 898 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 899 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 900 | "; ja 1f ; ud2 ; 1:\n" |
| 901 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 902 | } |
| 903 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 904 | 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] | 905 | { |
| 906 | int i; |
| 907 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 908 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 909 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 910 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 911 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 912 | } |
| 913 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 914 | static void vmcs_clear(struct vmcs *vmcs) |
| 915 | { |
| 916 | u64 phys_addr = __pa(vmcs); |
| 917 | u8 error; |
| 918 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 919 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 920 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 921 | : "cc", "memory"); |
| 922 | if (error) |
| 923 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 924 | vmcs, phys_addr); |
| 925 | } |
| 926 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 927 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 928 | { |
| 929 | vmcs_clear(loaded_vmcs->vmcs); |
| 930 | loaded_vmcs->cpu = -1; |
| 931 | loaded_vmcs->launched = 0; |
| 932 | } |
| 933 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 934 | static void vmcs_load(struct vmcs *vmcs) |
| 935 | { |
| 936 | u64 phys_addr = __pa(vmcs); |
| 937 | u8 error; |
| 938 | |
| 939 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 940 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 941 | : "cc", "memory"); |
| 942 | if (error) |
| 943 | printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n", |
| 944 | vmcs, phys_addr); |
| 945 | } |
| 946 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 947 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 948 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 949 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 950 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 951 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 952 | if (loaded_vmcs->cpu != cpu) |
| 953 | return; /* vcpu migration can race with cpu offline */ |
| 954 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 955 | per_cpu(current_vmcs, cpu) = NULL; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 956 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
| 957 | loaded_vmcs_init(loaded_vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 958 | } |
| 959 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 960 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 961 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 962 | if (loaded_vmcs->cpu != -1) |
| 963 | smp_call_function_single( |
| 964 | loaded_vmcs->cpu, __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 965 | } |
| 966 | |
Gui Jianfeng | 1760dd4 | 2010-06-07 10:33:27 +0800 | [diff] [blame] | 967 | static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 968 | { |
| 969 | if (vmx->vpid == 0) |
| 970 | return; |
| 971 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 972 | if (cpu_has_vmx_invvpid_single()) |
| 973 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 974 | } |
| 975 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 976 | static inline void vpid_sync_vcpu_global(void) |
| 977 | { |
| 978 | if (cpu_has_vmx_invvpid_global()) |
| 979 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 980 | } |
| 981 | |
| 982 | static inline void vpid_sync_context(struct vcpu_vmx *vmx) |
| 983 | { |
| 984 | if (cpu_has_vmx_invvpid_single()) |
Gui Jianfeng | 1760dd4 | 2010-06-07 10:33:27 +0800 | [diff] [blame] | 985 | vpid_sync_vcpu_single(vmx); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 986 | else |
| 987 | vpid_sync_vcpu_global(); |
| 988 | } |
| 989 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 990 | static inline void ept_sync_global(void) |
| 991 | { |
| 992 | if (cpu_has_vmx_invept_global()) |
| 993 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
| 994 | } |
| 995 | |
| 996 | static inline void ept_sync_context(u64 eptp) |
| 997 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 998 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 999 | if (cpu_has_vmx_invept_context()) |
| 1000 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 1001 | else |
| 1002 | ept_sync_global(); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa) |
| 1007 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1008 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1009 | if (cpu_has_vmx_invept_individual_addr()) |
| 1010 | __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR, |
| 1011 | eptp, gpa); |
| 1012 | else |
| 1013 | ept_sync_context(eptp); |
| 1014 | } |
| 1015 | } |
| 1016 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1017 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1018 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1019 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1020 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1021 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 1022 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1023 | return value; |
| 1024 | } |
| 1025 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1026 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1027 | { |
| 1028 | return vmcs_readl(field); |
| 1029 | } |
| 1030 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1031 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1032 | { |
| 1033 | return vmcs_readl(field); |
| 1034 | } |
| 1035 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1036 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1037 | { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1038 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1039 | return vmcs_readl(field); |
| 1040 | #else |
| 1041 | return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32); |
| 1042 | #endif |
| 1043 | } |
| 1044 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1045 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 1046 | { |
| 1047 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 1048 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 1049 | dump_stack(); |
| 1050 | } |
| 1051 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1052 | static void vmcs_writel(unsigned long field, unsigned long value) |
| 1053 | { |
| 1054 | u8 error; |
| 1055 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1056 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1057 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1058 | if (unlikely(error)) |
| 1059 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | static void vmcs_write16(unsigned long field, u16 value) |
| 1063 | { |
| 1064 | vmcs_writel(field, value); |
| 1065 | } |
| 1066 | |
| 1067 | static void vmcs_write32(unsigned long field, u32 value) |
| 1068 | { |
| 1069 | vmcs_writel(field, value); |
| 1070 | } |
| 1071 | |
| 1072 | static void vmcs_write64(unsigned long field, u64 value) |
| 1073 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1074 | vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 1075 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1076 | asm volatile (""); |
| 1077 | vmcs_writel(field+1, value >> 32); |
| 1078 | #endif |
| 1079 | } |
| 1080 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1081 | static void vmcs_clear_bits(unsigned long field, u32 mask) |
| 1082 | { |
| 1083 | vmcs_writel(field, vmcs_readl(field) & ~mask); |
| 1084 | } |
| 1085 | |
| 1086 | static void vmcs_set_bits(unsigned long field, u32 mask) |
| 1087 | { |
| 1088 | vmcs_writel(field, vmcs_readl(field) | mask); |
| 1089 | } |
| 1090 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1091 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 1092 | { |
| 1093 | vmx->segment_cache.bitmask = 0; |
| 1094 | } |
| 1095 | |
| 1096 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 1097 | unsigned field) |
| 1098 | { |
| 1099 | bool ret; |
| 1100 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 1101 | |
| 1102 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 1103 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 1104 | vmx->segment_cache.bitmask = 0; |
| 1105 | } |
| 1106 | ret = vmx->segment_cache.bitmask & mask; |
| 1107 | vmx->segment_cache.bitmask |= mask; |
| 1108 | return ret; |
| 1109 | } |
| 1110 | |
| 1111 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 1112 | { |
| 1113 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 1114 | |
| 1115 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 1116 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 1117 | return *p; |
| 1118 | } |
| 1119 | |
| 1120 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 1121 | { |
| 1122 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 1123 | |
| 1124 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 1125 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 1126 | return *p; |
| 1127 | } |
| 1128 | |
| 1129 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 1130 | { |
| 1131 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 1132 | |
| 1133 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 1134 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 1135 | return *p; |
| 1136 | } |
| 1137 | |
| 1138 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 1139 | { |
| 1140 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 1141 | |
| 1142 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 1143 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 1144 | return *p; |
| 1145 | } |
| 1146 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1147 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 1148 | { |
| 1149 | u32 eb; |
| 1150 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1151 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
| 1152 | (1u << NM_VECTOR) | (1u << DB_VECTOR); |
| 1153 | if ((vcpu->guest_debug & |
| 1154 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 1155 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 1156 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1157 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1158 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1159 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1160 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1161 | if (vcpu->fpu_active) |
| 1162 | eb &= ~(1u << NM_VECTOR); |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1163 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 1164 | } |
| 1165 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1166 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 1167 | { |
| 1168 | unsigned i; |
| 1169 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1170 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1171 | if (msr == MSR_EFER && cpu_has_load_ia32_efer) { |
| 1172 | vmcs_clear_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER); |
| 1173 | vmcs_clear_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER); |
| 1174 | return; |
| 1175 | } |
| 1176 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1177 | for (i = 0; i < m->nr; ++i) |
| 1178 | if (m->guest[i].index == msr) |
| 1179 | break; |
| 1180 | |
| 1181 | if (i == m->nr) |
| 1182 | return; |
| 1183 | --m->nr; |
| 1184 | m->guest[i] = m->guest[m->nr]; |
| 1185 | m->host[i] = m->host[m->nr]; |
| 1186 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1187 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1188 | } |
| 1189 | |
| 1190 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 1191 | u64 guest_val, u64 host_val) |
| 1192 | { |
| 1193 | unsigned i; |
| 1194 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1195 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1196 | if (msr == MSR_EFER && cpu_has_load_ia32_efer) { |
| 1197 | vmcs_write64(GUEST_IA32_EFER, guest_val); |
| 1198 | vmcs_write64(HOST_IA32_EFER, host_val); |
| 1199 | vmcs_set_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER); |
| 1200 | vmcs_set_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER); |
| 1201 | return; |
| 1202 | } |
| 1203 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1204 | for (i = 0; i < m->nr; ++i) |
| 1205 | if (m->guest[i].index == msr) |
| 1206 | break; |
| 1207 | |
| 1208 | if (i == m->nr) { |
| 1209 | ++m->nr; |
| 1210 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1211 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1212 | } |
| 1213 | |
| 1214 | m->guest[i].index = msr; |
| 1215 | m->guest[i].value = guest_val; |
| 1216 | m->host[i].index = msr; |
| 1217 | m->host[i].value = host_val; |
| 1218 | } |
| 1219 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1220 | static void reload_tss(void) |
| 1221 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1222 | /* |
| 1223 | * VT restores TR but not its size. Useless. |
| 1224 | */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1225 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 1226 | struct desc_struct *descs; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1227 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1228 | descs = (void *)gdt->address; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1229 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 1230 | load_TR_desc(); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1231 | } |
| 1232 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1233 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1234 | { |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 1235 | u64 guest_efer; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1236 | u64 ignore_bits; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1237 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1238 | guest_efer = vmx->vcpu.arch.efer; |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 1239 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1240 | /* |
| 1241 | * NX is emulated; LMA and LME handled by hardware; SCE meaninless |
| 1242 | * outside long mode |
| 1243 | */ |
| 1244 | ignore_bits = EFER_NX | EFER_SCE; |
| 1245 | #ifdef CONFIG_X86_64 |
| 1246 | ignore_bits |= EFER_LMA | EFER_LME; |
| 1247 | /* SCE is meaningful only in long mode on Intel */ |
| 1248 | if (guest_efer & EFER_LMA) |
| 1249 | ignore_bits &= ~(u64)EFER_SCE; |
| 1250 | #endif |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1251 | guest_efer &= ~ignore_bits; |
| 1252 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1253 | vmx->guest_msrs[efer_offset].data = guest_efer; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 1254 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 1255 | |
| 1256 | clear_atomic_switch_msr(vmx, MSR_EFER); |
| 1257 | /* On ept, can't emulate nx, and must switch nx atomically */ |
| 1258 | if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) { |
| 1259 | guest_efer = vmx->vcpu.arch.efer; |
| 1260 | if (!(guest_efer & EFER_LMA)) |
| 1261 | guest_efer &= ~EFER_LME; |
| 1262 | add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer); |
| 1263 | return false; |
| 1264 | } |
| 1265 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1266 | return true; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1267 | } |
| 1268 | |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1269 | static unsigned long segment_base(u16 selector) |
| 1270 | { |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1271 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1272 | struct desc_struct *d; |
| 1273 | unsigned long table_base; |
| 1274 | unsigned long v; |
| 1275 | |
| 1276 | if (!(selector & ~3)) |
| 1277 | return 0; |
| 1278 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1279 | table_base = gdt->address; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1280 | |
| 1281 | if (selector & 4) { /* from ldt */ |
| 1282 | u16 ldt_selector = kvm_read_ldt(); |
| 1283 | |
| 1284 | if (!(ldt_selector & ~3)) |
| 1285 | return 0; |
| 1286 | |
| 1287 | table_base = segment_base(ldt_selector); |
| 1288 | } |
| 1289 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
| 1290 | v = get_desc_base(d); |
| 1291 | #ifdef CONFIG_X86_64 |
| 1292 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 1293 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; |
| 1294 | #endif |
| 1295 | return v; |
| 1296 | } |
| 1297 | |
| 1298 | static inline unsigned long kvm_read_tr_base(void) |
| 1299 | { |
| 1300 | u16 tr; |
| 1301 | asm("str %0" : "=g"(tr)); |
| 1302 | return segment_base(tr); |
| 1303 | } |
| 1304 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1305 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1306 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1307 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1308 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1309 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1310 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1311 | return; |
| 1312 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1313 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1314 | /* |
| 1315 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 1316 | * allow segment selectors with cpl > 0 or ti == 1. |
| 1317 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1318 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1319 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1320 | savesegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1321 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1322 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1323 | vmx->host_state.fs_reload_needed = 0; |
| 1324 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1325 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1326 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1327 | } |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1328 | savesegment(gs, vmx->host_state.gs_sel); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1329 | if (!(vmx->host_state.gs_sel & 7)) |
| 1330 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1331 | else { |
| 1332 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1333 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | #ifdef CONFIG_X86_64 |
| 1337 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 1338 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 1339 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1340 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 1341 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1342 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 1343 | |
| 1344 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1345 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
| 1346 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1347 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 1348 | #endif |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1349 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 1350 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 1351 | vmx->guest_msrs[i].data, |
| 1352 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1353 | } |
| 1354 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1355 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1356 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1357 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1358 | return; |
| 1359 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 1360 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1361 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1362 | #ifdef CONFIG_X86_64 |
| 1363 | if (is_long_mode(&vmx->vcpu)) |
| 1364 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 1365 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1366 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1367 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1368 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1369 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1370 | #else |
| 1371 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1372 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1373 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 1374 | if (vmx->host_state.fs_reload_needed) |
| 1375 | loadsegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1376 | reload_tss(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1377 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1378 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1379 | #endif |
Avi Kivity | 1c11e71 | 2010-05-03 16:05:44 +0300 | [diff] [blame] | 1380 | if (current_thread_info()->status & TS_USEDFPU) |
| 1381 | clts(); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 1382 | load_gdt(&__get_cpu_var(host_gdt)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1383 | } |
| 1384 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1385 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 1386 | { |
| 1387 | preempt_disable(); |
| 1388 | __vmx_load_host_state(vmx); |
| 1389 | preempt_enable(); |
| 1390 | } |
| 1391 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1392 | /* |
| 1393 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 1394 | * vcpu mutex is already taken. |
| 1395 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1396 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1397 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1398 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1399 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1400 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1401 | if (!vmm_exclusive) |
| 1402 | kvm_cpu_vmxon(phys_addr); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1403 | else if (vmx->loaded_vmcs->cpu != cpu) |
| 1404 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1405 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1406 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 1407 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 1408 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1409 | } |
| 1410 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1411 | if (vmx->loaded_vmcs->cpu != cpu) { |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1412 | struct desc_ptr *gdt = &__get_cpu_var(host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1413 | unsigned long sysenter_esp; |
| 1414 | |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 1415 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1416 | local_irq_disable(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1417 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 1418 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1419 | local_irq_enable(); |
| 1420 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1421 | /* |
| 1422 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 1423 | * processors. |
| 1424 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1425 | vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1426 | vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1427 | |
| 1428 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 1429 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1430 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1431 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 1435 | { |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1436 | __vmx_load_host_state(to_vmx(vcpu)); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1437 | if (!vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1438 | __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs); |
| 1439 | vcpu->cpu = -1; |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1440 | kvm_cpu_vmxoff(); |
| 1441 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1442 | } |
| 1443 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1444 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 1445 | { |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1446 | ulong cr0; |
| 1447 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1448 | if (vcpu->fpu_active) |
| 1449 | return; |
| 1450 | vcpu->fpu_active = 1; |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1451 | cr0 = vmcs_readl(GUEST_CR0); |
| 1452 | cr0 &= ~(X86_CR0_TS | X86_CR0_MP); |
| 1453 | cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP); |
| 1454 | vmcs_writel(GUEST_CR0, cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1455 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1456 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 1457 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1458 | } |
| 1459 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1460 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 1461 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1462 | /* |
| 1463 | * Return the cr0 value that a nested guest would read. This is a combination |
| 1464 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 1465 | * its hypervisor (cr0_read_shadow). |
| 1466 | */ |
| 1467 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 1468 | { |
| 1469 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 1470 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 1471 | } |
| 1472 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 1473 | { |
| 1474 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 1475 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 1476 | } |
| 1477 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1478 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 1479 | { |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1480 | vmx_decache_cr0_guest_bits(vcpu); |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1481 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1482 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1483 | vcpu->arch.cr0_guest_owned_bits = 0; |
| 1484 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 1485 | vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1486 | } |
| 1487 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1488 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 1489 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1490 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 1491 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1492 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 1493 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 1494 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 1495 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1496 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1497 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 1498 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1499 | } |
| 1500 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1501 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1502 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 1506 | { |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1507 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 1508 | __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1509 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1510 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1511 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1512 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1513 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1514 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 1515 | } |
| 1516 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1517 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 1518 | { |
| 1519 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 1520 | int ret = 0; |
| 1521 | |
| 1522 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1523 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1524 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1525 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1526 | |
| 1527 | return ret & mask; |
| 1528 | } |
| 1529 | |
| 1530 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 1531 | { |
| 1532 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 1533 | u32 interruptibility = interruptibility_old; |
| 1534 | |
| 1535 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 1536 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1537 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1538 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1539 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1540 | interruptibility |= GUEST_INTR_STATE_STI; |
| 1541 | |
| 1542 | if ((interruptibility != interruptibility_old)) |
| 1543 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 1544 | } |
| 1545 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1546 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 1547 | { |
| 1548 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1549 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1550 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1551 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1552 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1553 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1554 | /* skipping an emulated instruction also counts */ |
| 1555 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1556 | } |
| 1557 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 1558 | static void vmx_clear_hlt(struct kvm_vcpu *vcpu) |
| 1559 | { |
| 1560 | /* Ensure that we clear the HLT state in the VMCS. We don't need to |
| 1561 | * explicitly skip the instruction because if the HLT state is set, then |
| 1562 | * the instruction is already executing and RIP has already been |
| 1563 | * advanced. */ |
| 1564 | if (!yield_on_hlt && |
| 1565 | vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) |
| 1566 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
| 1567 | } |
| 1568 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1569 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 1570 | bool has_error_code, u32 error_code, |
| 1571 | bool reinject) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1572 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1573 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1574 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1575 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1576 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1577 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1578 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 1579 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1580 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1581 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 1582 | int inc_eip = 0; |
| 1583 | if (kvm_exception_is_soft(nr)) |
| 1584 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 1585 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 1586 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1587 | return; |
| 1588 | } |
| 1589 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 1590 | if (kvm_exception_is_soft(nr)) { |
| 1591 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 1592 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1593 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 1594 | } else |
| 1595 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 1596 | |
| 1597 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 1598 | vmx_clear_hlt(vcpu); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1599 | } |
| 1600 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1601 | static bool vmx_rdtscp_supported(void) |
| 1602 | { |
| 1603 | return cpu_has_vmx_rdtscp(); |
| 1604 | } |
| 1605 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1606 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1607 | * Swap MSR entry in host/guest MSR entry array. |
| 1608 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1609 | 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] | 1610 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1611 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1612 | |
| 1613 | tmp = vmx->guest_msrs[to]; |
| 1614 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 1615 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1619 | * Set up the vmcs to automatically save and restore system |
| 1620 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 1621 | * mode, as fiddling with msrs is very expensive. |
| 1622 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1623 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1624 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1625 | int save_nmsrs, index; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 1626 | unsigned long *msr_bitmap; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1627 | |
Avi Kivity | 33f9c50 | 2008-02-27 16:06:57 +0200 | [diff] [blame] | 1628 | vmx_load_host_state(vmx); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1629 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1630 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1631 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1632 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1633 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1634 | move_msr_up(vmx, index, save_nmsrs++); |
| 1635 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1636 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1637 | move_msr_up(vmx, index, save_nmsrs++); |
| 1638 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1639 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1640 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1641 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
| 1642 | if (index >= 0 && vmx->rdtscp_enabled) |
| 1643 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1644 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1645 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1646 | * if efer.sce is enabled. |
| 1647 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1648 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1649 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1650 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1651 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1652 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1653 | index = __find_msr_index(vmx, MSR_EFER); |
| 1654 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1655 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1656 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1657 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 1658 | |
| 1659 | if (cpu_has_vmx_msr_bitmap()) { |
| 1660 | if (is_long_mode(&vmx->vcpu)) |
| 1661 | msr_bitmap = vmx_msr_bitmap_longmode; |
| 1662 | else |
| 1663 | msr_bitmap = vmx_msr_bitmap_legacy; |
| 1664 | |
| 1665 | vmcs_write64(MSR_BITMAP, __pa(msr_bitmap)); |
| 1666 | } |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1670 | * reads and returns guest's timestamp counter "register" |
| 1671 | * guest_tsc = host_tsc + tsc_offset -- 21.3 |
| 1672 | */ |
| 1673 | static u64 guest_read_tsc(void) |
| 1674 | { |
| 1675 | u64 host_tsc, tsc_offset; |
| 1676 | |
| 1677 | rdtscll(host_tsc); |
| 1678 | tsc_offset = vmcs_read64(TSC_OFFSET); |
| 1679 | return host_tsc + tsc_offset; |
| 1680 | } |
| 1681 | |
| 1682 | /* |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 1683 | * Empty call-back. Needs to be implemented when VMX enables the SET_TSC_KHZ |
| 1684 | * ioctl. In this case the call-back should update internal vmx state to make |
| 1685 | * the changes effective. |
| 1686 | */ |
| 1687 | static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz) |
| 1688 | { |
| 1689 | /* Nothing to do here */ |
| 1690 | } |
| 1691 | |
| 1692 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1693 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1694 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 1695 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1696 | { |
Zachary Amsden | f4e1b3c | 2010-08-19 22:07:16 -1000 | [diff] [blame] | 1697 | vmcs_write64(TSC_OFFSET, offset); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1698 | } |
| 1699 | |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 1700 | static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment) |
| 1701 | { |
| 1702 | u64 offset = vmcs_read64(TSC_OFFSET); |
| 1703 | vmcs_write64(TSC_OFFSET, offset + adjustment); |
| 1704 | } |
| 1705 | |
Joerg Roedel | 857e409 | 2011-03-25 09:44:50 +0100 | [diff] [blame] | 1706 | static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) |
| 1707 | { |
| 1708 | return target_tsc - native_read_tsc(); |
| 1709 | } |
| 1710 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 1711 | static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu) |
| 1712 | { |
| 1713 | struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 1714 | return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31))); |
| 1715 | } |
| 1716 | |
| 1717 | /* |
| 1718 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 1719 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 1720 | * all guests if the "nested" module option is off, and can also be disabled |
| 1721 | * for a single guest by disabling its VMX cpuid bit. |
| 1722 | */ |
| 1723 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 1724 | { |
| 1725 | return nested && guest_cpuid_has_vmx(vcpu); |
| 1726 | } |
| 1727 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1728 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 1729 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 1730 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 1731 | * The same values should also be used to verify that vmcs12 control fields are |
| 1732 | * valid during nested entry from L1 to L2. |
| 1733 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 1734 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 1735 | * bit in the high half is on if the corresponding bit in the control field |
| 1736 | * may be on. See also vmx_control_verify(). |
| 1737 | * TODO: allow these variables to be modified (downgraded) by module options |
| 1738 | * or other means. |
| 1739 | */ |
| 1740 | static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high; |
| 1741 | static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high; |
| 1742 | static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high; |
| 1743 | static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high; |
| 1744 | static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high; |
| 1745 | static __init void nested_vmx_setup_ctls_msrs(void) |
| 1746 | { |
| 1747 | /* |
| 1748 | * Note that as a general rule, the high half of the MSRs (bits in |
| 1749 | * the control fields which may be 1) should be initialized by the |
| 1750 | * intersection of the underlying hardware's MSR (i.e., features which |
| 1751 | * can be supported) and the list of features we want to expose - |
| 1752 | * because they are known to be properly supported in our code. |
| 1753 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 1754 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 1755 | * reason is that if one of these bits is necessary, it will appear |
| 1756 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 1757 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
| 1758 | * nested_vmx_exit_handled() will not pass related exits to L1. |
| 1759 | * These rules have exceptions below. |
| 1760 | */ |
| 1761 | |
| 1762 | /* pin-based controls */ |
| 1763 | /* |
| 1764 | * According to the Intel spec, if bit 55 of VMX_BASIC is off (as it is |
| 1765 | * in our case), bits 1, 2 and 4 (i.e., 0x16) must be 1 in this MSR. |
| 1766 | */ |
| 1767 | nested_vmx_pinbased_ctls_low = 0x16 ; |
| 1768 | nested_vmx_pinbased_ctls_high = 0x16 | |
| 1769 | PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING | |
| 1770 | PIN_BASED_VIRTUAL_NMIS; |
| 1771 | |
| 1772 | /* exit controls */ |
| 1773 | nested_vmx_exit_ctls_low = 0; |
| 1774 | #ifdef CONFIG_X86_64 |
| 1775 | nested_vmx_exit_ctls_high = VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 1776 | #else |
| 1777 | nested_vmx_exit_ctls_high = 0; |
| 1778 | #endif |
| 1779 | |
| 1780 | /* entry controls */ |
| 1781 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
| 1782 | nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high); |
| 1783 | nested_vmx_entry_ctls_low = 0; |
| 1784 | nested_vmx_entry_ctls_high &= |
| 1785 | VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_IA32E_MODE; |
| 1786 | |
| 1787 | /* cpu-based controls */ |
| 1788 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
| 1789 | nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high); |
| 1790 | nested_vmx_procbased_ctls_low = 0; |
| 1791 | nested_vmx_procbased_ctls_high &= |
| 1792 | CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
| 1793 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 1794 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 1795 | CPU_BASED_CR3_STORE_EXITING | |
| 1796 | #ifdef CONFIG_X86_64 |
| 1797 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 1798 | #endif |
| 1799 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
| 1800 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING | |
| 1801 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
| 1802 | /* |
| 1803 | * We can allow some features even when not supported by the |
| 1804 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 1805 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 1806 | * without MSR bitmaps. |
| 1807 | */ |
| 1808 | nested_vmx_procbased_ctls_high |= CPU_BASED_USE_MSR_BITMAPS; |
| 1809 | |
| 1810 | /* secondary cpu-based controls */ |
| 1811 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
| 1812 | nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high); |
| 1813 | nested_vmx_secondary_ctls_low = 0; |
| 1814 | nested_vmx_secondary_ctls_high &= |
| 1815 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1816 | } |
| 1817 | |
| 1818 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 1819 | { |
| 1820 | /* |
| 1821 | * Bits 0 in high must be 0, and bits 1 in low must be 1. |
| 1822 | */ |
| 1823 | return ((control & high) | low) == control; |
| 1824 | } |
| 1825 | |
| 1826 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 1827 | { |
| 1828 | return low | ((u64)high << 32); |
| 1829 | } |
| 1830 | |
| 1831 | /* |
| 1832 | * If we allow our guest to use VMX instructions (i.e., nested VMX), we should |
| 1833 | * also let it use VMX-specific MSRs. |
| 1834 | * vmx_get_vmx_msr() and vmx_set_vmx_msr() return 1 when we handled a |
| 1835 | * VMX-specific MSR, or 0 when we haven't (and the caller should handle it |
| 1836 | * like all other MSRs). |
| 1837 | */ |
| 1838 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 1839 | { |
| 1840 | if (!nested_vmx_allowed(vcpu) && msr_index >= MSR_IA32_VMX_BASIC && |
| 1841 | msr_index <= MSR_IA32_VMX_TRUE_ENTRY_CTLS) { |
| 1842 | /* |
| 1843 | * According to the spec, processors which do not support VMX |
| 1844 | * should throw a #GP(0) when VMX capability MSRs are read. |
| 1845 | */ |
| 1846 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 1847 | return 1; |
| 1848 | } |
| 1849 | |
| 1850 | switch (msr_index) { |
| 1851 | case MSR_IA32_FEATURE_CONTROL: |
| 1852 | *pdata = 0; |
| 1853 | break; |
| 1854 | case MSR_IA32_VMX_BASIC: |
| 1855 | /* |
| 1856 | * This MSR reports some information about VMX support. We |
| 1857 | * should return information about the VMX we emulate for the |
| 1858 | * guest, and the VMCS structure we give it - not about the |
| 1859 | * VMX support of the underlying hardware. |
| 1860 | */ |
| 1861 | *pdata = VMCS12_REVISION | |
| 1862 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 1863 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 1864 | break; |
| 1865 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 1866 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 1867 | *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low, |
| 1868 | nested_vmx_pinbased_ctls_high); |
| 1869 | break; |
| 1870 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 1871 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 1872 | *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low, |
| 1873 | nested_vmx_procbased_ctls_high); |
| 1874 | break; |
| 1875 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 1876 | case MSR_IA32_VMX_EXIT_CTLS: |
| 1877 | *pdata = vmx_control_msr(nested_vmx_exit_ctls_low, |
| 1878 | nested_vmx_exit_ctls_high); |
| 1879 | break; |
| 1880 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 1881 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 1882 | *pdata = vmx_control_msr(nested_vmx_entry_ctls_low, |
| 1883 | nested_vmx_entry_ctls_high); |
| 1884 | break; |
| 1885 | case MSR_IA32_VMX_MISC: |
| 1886 | *pdata = 0; |
| 1887 | break; |
| 1888 | /* |
| 1889 | * These MSRs specify bits which the guest must keep fixed (on or off) |
| 1890 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 1891 | * We picked the standard core2 setting. |
| 1892 | */ |
| 1893 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 1894 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 1895 | case MSR_IA32_VMX_CR0_FIXED0: |
| 1896 | *pdata = VMXON_CR0_ALWAYSON; |
| 1897 | break; |
| 1898 | case MSR_IA32_VMX_CR0_FIXED1: |
| 1899 | *pdata = -1ULL; |
| 1900 | break; |
| 1901 | case MSR_IA32_VMX_CR4_FIXED0: |
| 1902 | *pdata = VMXON_CR4_ALWAYSON; |
| 1903 | break; |
| 1904 | case MSR_IA32_VMX_CR4_FIXED1: |
| 1905 | *pdata = -1ULL; |
| 1906 | break; |
| 1907 | case MSR_IA32_VMX_VMCS_ENUM: |
| 1908 | *pdata = 0x1f; |
| 1909 | break; |
| 1910 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 1911 | *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low, |
| 1912 | nested_vmx_secondary_ctls_high); |
| 1913 | break; |
| 1914 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 1915 | /* Currently, no nested ept or nested vpid */ |
| 1916 | *pdata = 0; |
| 1917 | break; |
| 1918 | default: |
| 1919 | return 0; |
| 1920 | } |
| 1921 | |
| 1922 | return 1; |
| 1923 | } |
| 1924 | |
| 1925 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 1926 | { |
| 1927 | if (!nested_vmx_allowed(vcpu)) |
| 1928 | return 0; |
| 1929 | |
| 1930 | if (msr_index == MSR_IA32_FEATURE_CONTROL) |
| 1931 | /* TODO: the right thing. */ |
| 1932 | return 1; |
| 1933 | /* |
| 1934 | * No need to treat VMX capability MSRs specially: If we don't handle |
| 1935 | * them, handle_wrmsr will #GP(0), which is correct (they are readonly) |
| 1936 | */ |
| 1937 | return 0; |
| 1938 | } |
| 1939 | |
| 1940 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1941 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 1942 | * Returns 0 on success, non-0 otherwise. |
| 1943 | * Assumes vcpu_load() was already called. |
| 1944 | */ |
| 1945 | static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 1946 | { |
| 1947 | u64 data; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1948 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1949 | |
| 1950 | if (!pdata) { |
| 1951 | printk(KERN_ERR "BUG: get_msr called with NULL pdata\n"); |
| 1952 | return -EINVAL; |
| 1953 | } |
| 1954 | |
| 1955 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1956 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1957 | case MSR_FS_BASE: |
| 1958 | data = vmcs_readl(GUEST_FS_BASE); |
| 1959 | break; |
| 1960 | case MSR_GS_BASE: |
| 1961 | data = vmcs_readl(GUEST_GS_BASE); |
| 1962 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1963 | case MSR_KERNEL_GS_BASE: |
| 1964 | vmx_load_host_state(to_vmx(vcpu)); |
| 1965 | data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
| 1966 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1967 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1968 | case MSR_EFER: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1969 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 1970 | case MSR_IA32_TSC: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1971 | data = guest_read_tsc(); |
| 1972 | break; |
| 1973 | case MSR_IA32_SYSENTER_CS: |
| 1974 | data = vmcs_read32(GUEST_SYSENTER_CS); |
| 1975 | break; |
| 1976 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 1977 | data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1978 | break; |
| 1979 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 1980 | data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1981 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1982 | case MSR_TSC_AUX: |
| 1983 | if (!to_vmx(vcpu)->rdtscp_enabled) |
| 1984 | return 1; |
| 1985 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1986 | default: |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1987 | vmx_load_host_state(to_vmx(vcpu)); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 1988 | if (vmx_get_vmx_msr(vcpu, msr_index, pdata)) |
| 1989 | return 0; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1990 | msr = find_msr_entry(to_vmx(vcpu), msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1991 | if (msr) { |
Gleb Natapov | 542423b | 2009-08-27 15:07:30 +0300 | [diff] [blame] | 1992 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1993 | data = msr->data; |
| 1994 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1995 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1996 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | *pdata = data; |
| 2000 | return 0; |
| 2001 | } |
| 2002 | |
| 2003 | /* |
| 2004 | * Writes msr value into into the appropriate "register". |
| 2005 | * Returns 0 on success, non-0 otherwise. |
| 2006 | * Assumes vcpu_load() was already called. |
| 2007 | */ |
| 2008 | static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 2009 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2010 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2011 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2012 | int ret = 0; |
| 2013 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2014 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2015 | case MSR_EFER: |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2016 | vmx_load_host_state(vmx); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2017 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2018 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 2019 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2020 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2021 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2022 | vmcs_writel(GUEST_FS_BASE, data); |
| 2023 | break; |
| 2024 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2025 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2026 | vmcs_writel(GUEST_GS_BASE, data); |
| 2027 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2028 | case MSR_KERNEL_GS_BASE: |
| 2029 | vmx_load_host_state(vmx); |
| 2030 | vmx->msr_guest_kernel_gs_base = data; |
| 2031 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2032 | #endif |
| 2033 | case MSR_IA32_SYSENTER_CS: |
| 2034 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 2035 | break; |
| 2036 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2037 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2038 | break; |
| 2039 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2040 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2041 | break; |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2042 | case MSR_IA32_TSC: |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2043 | kvm_write_tsc(vcpu, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2044 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2045 | case MSR_IA32_CR_PAT: |
| 2046 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
| 2047 | vmcs_write64(GUEST_IA32_PAT, data); |
| 2048 | vcpu->arch.pat = data; |
| 2049 | break; |
| 2050 | } |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2051 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
| 2052 | break; |
| 2053 | case MSR_TSC_AUX: |
| 2054 | if (!vmx->rdtscp_enabled) |
| 2055 | return 1; |
| 2056 | /* Check reserved bit, higher 32 bits should be zero */ |
| 2057 | if ((data >> 32) != 0) |
| 2058 | return 1; |
| 2059 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2060 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2061 | if (vmx_set_vmx_msr(vcpu, msr_index, data)) |
| 2062 | break; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2063 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2064 | if (msr) { |
Gleb Natapov | 542423b | 2009-08-27 15:07:30 +0300 | [diff] [blame] | 2065 | vmx_load_host_state(vmx); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2066 | msr->data = data; |
| 2067 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2068 | } |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2069 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2070 | } |
| 2071 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2072 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2073 | } |
| 2074 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2075 | 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] | 2076 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2077 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 2078 | switch (reg) { |
| 2079 | case VCPU_REGS_RSP: |
| 2080 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 2081 | break; |
| 2082 | case VCPU_REGS_RIP: |
| 2083 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 2084 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2085 | case VCPU_EXREG_PDPTR: |
| 2086 | if (enable_ept) |
| 2087 | ept_save_pdptrs(vcpu); |
| 2088 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2089 | default: |
| 2090 | break; |
| 2091 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2092 | } |
| 2093 | |
Jan Kiszka | 355be0b | 2009-10-03 00:31:21 +0200 | [diff] [blame] | 2094 | 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] | 2095 | { |
Jan Kiszka | ae675ef | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2096 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 2097 | vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]); |
| 2098 | else |
| 2099 | vmcs_writel(GUEST_DR7, vcpu->arch.dr7); |
| 2100 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2101 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | static __init int cpu_has_kvm_support(void) |
| 2105 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 2106 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | static __init int vmx_disabled_by_bios(void) |
| 2110 | { |
| 2111 | u64 msr; |
| 2112 | |
| 2113 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2114 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2115 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2116 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 2117 | && tboot_enabled()) |
| 2118 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2119 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2120 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2121 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 2122 | && !tboot_enabled()) { |
| 2123 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2124 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2125 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 2126 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2127 | /* launched w/o TXT and VMX disabled */ |
| 2128 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 2129 | && !tboot_enabled()) |
| 2130 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2134 | } |
| 2135 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2136 | static void kvm_cpu_vmxon(u64 addr) |
| 2137 | { |
| 2138 | asm volatile (ASM_VMX_VMXON_RAX |
| 2139 | : : "a"(&addr), "m"(addr) |
| 2140 | : "memory", "cc"); |
| 2141 | } |
| 2142 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2143 | static int hardware_enable(void *garbage) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2144 | { |
| 2145 | int cpu = raw_smp_processor_id(); |
| 2146 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2147 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2148 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2149 | if (read_cr4() & X86_CR4_VMXE) |
| 2150 | return -EBUSY; |
| 2151 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2152 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2153 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2154 | |
| 2155 | test_bits = FEATURE_CONTROL_LOCKED; |
| 2156 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 2157 | if (tboot_enabled()) |
| 2158 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 2159 | |
| 2160 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2161 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2162 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 2163 | } |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2164 | write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */ |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2165 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2166 | if (vmm_exclusive) { |
| 2167 | kvm_cpu_vmxon(phys_addr); |
| 2168 | ept_sync_global(); |
| 2169 | } |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2170 | |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 2171 | store_gdt(&__get_cpu_var(host_gdt)); |
| 2172 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2173 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2174 | } |
| 2175 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2176 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2177 | { |
| 2178 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2179 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2180 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2181 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 2182 | loaded_vmcss_on_cpu_link) |
| 2183 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2184 | } |
| 2185 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 2186 | |
| 2187 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 2188 | * tricks. |
| 2189 | */ |
| 2190 | static void kvm_cpu_vmxoff(void) |
| 2191 | { |
| 2192 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 2193 | } |
| 2194 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2195 | static void hardware_disable(void *garbage) |
| 2196 | { |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2197 | if (vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2198 | vmclear_local_loaded_vmcss(); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2199 | kvm_cpu_vmxoff(); |
| 2200 | } |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2201 | write_cr4(read_cr4() & ~X86_CR4_VMXE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2202 | } |
| 2203 | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2204 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2205 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2206 | { |
| 2207 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2208 | u32 ctl = ctl_min | ctl_opt; |
| 2209 | |
| 2210 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 2211 | |
| 2212 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 2213 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 2214 | |
| 2215 | /* Ensure minimum (required) set of control bits are supported. */ |
| 2216 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2217 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2218 | |
| 2219 | *result = ctl; |
| 2220 | return 0; |
| 2221 | } |
| 2222 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2223 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 2224 | { |
| 2225 | u32 vmx_msr_low, vmx_msr_high; |
| 2226 | |
| 2227 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 2228 | return vmx_msr_high & ctl; |
| 2229 | } |
| 2230 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2231 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2232 | { |
| 2233 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2234 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2235 | u32 _pin_based_exec_control = 0; |
| 2236 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2237 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2238 | u32 _vmexit_control = 0; |
| 2239 | u32 _vmentry_control = 0; |
| 2240 | |
| 2241 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 2242 | opt = PIN_BASED_VIRTUAL_NMIS; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2243 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 2244 | &_pin_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2245 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2246 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 2247 | min = |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2248 | #ifdef CONFIG_X86_64 |
| 2249 | CPU_BASED_CR8_LOAD_EXITING | |
| 2250 | CPU_BASED_CR8_STORE_EXITING | |
| 2251 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2252 | CPU_BASED_CR3_LOAD_EXITING | |
| 2253 | CPU_BASED_CR3_STORE_EXITING | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2254 | CPU_BASED_USE_IO_BITMAPS | |
| 2255 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2256 | CPU_BASED_USE_TSC_OFFSETING | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 2257 | CPU_BASED_MWAIT_EXITING | |
| 2258 | CPU_BASED_MONITOR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2259 | CPU_BASED_INVLPG_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 2260 | |
| 2261 | if (yield_on_hlt) |
| 2262 | min |= CPU_BASED_HLT_EXITING; |
| 2263 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2264 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2265 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2266 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2267 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 2268 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2269 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2270 | #ifdef CONFIG_X86_64 |
| 2271 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 2272 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 2273 | ~CPU_BASED_CR8_STORE_EXITING; |
| 2274 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2275 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2276 | min2 = 0; |
| 2277 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2278 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2279 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2280 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2281 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2282 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
| 2283 | SECONDARY_EXEC_RDTSCP; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2284 | if (adjust_vmx_controls(min2, opt2, |
| 2285 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2286 | &_cpu_based_2nd_exec_control) < 0) |
| 2287 | return -EIO; |
| 2288 | } |
| 2289 | #ifndef CONFIG_X86_64 |
| 2290 | if (!(_cpu_based_2nd_exec_control & |
| 2291 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 2292 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2293 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2294 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2295 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 2296 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 2297 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 2298 | CPU_BASED_CR3_STORE_EXITING | |
| 2299 | CPU_BASED_INVLPG_EXITING); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2300 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
| 2301 | vmx_capability.ept, vmx_capability.vpid); |
| 2302 | } |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2303 | |
| 2304 | min = 0; |
| 2305 | #ifdef CONFIG_X86_64 |
| 2306 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 2307 | #endif |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2308 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2309 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 2310 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2311 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2312 | |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2313 | min = 0; |
| 2314 | opt = VM_ENTRY_LOAD_IA32_PAT; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2315 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 2316 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2317 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2318 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 2319 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2320 | |
| 2321 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 2322 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2323 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2324 | |
| 2325 | #ifdef CONFIG_X86_64 |
| 2326 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 2327 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2328 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2329 | #endif |
| 2330 | |
| 2331 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 2332 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2333 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2334 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2335 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
| 2336 | vmcs_conf->order = get_order(vmcs_config.size); |
| 2337 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2338 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2339 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 2340 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2341 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2342 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 2343 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2344 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2345 | cpu_has_load_ia32_efer = |
| 2346 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 2347 | VM_ENTRY_LOAD_IA32_EFER) |
| 2348 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 2349 | VM_EXIT_LOAD_IA32_EFER); |
| 2350 | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2351 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 2352 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2353 | |
| 2354 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 2355 | { |
| 2356 | int node = cpu_to_node(cpu); |
| 2357 | struct page *pages; |
| 2358 | struct vmcs *vmcs; |
| 2359 | |
Mel Gorman | 6484eb3 | 2009-06-16 15:31:54 -0700 | [diff] [blame] | 2360 | pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2361 | if (!pages) |
| 2362 | return NULL; |
| 2363 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2364 | memset(vmcs, 0, vmcs_config.size); |
| 2365 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2366 | return vmcs; |
| 2367 | } |
| 2368 | |
| 2369 | static struct vmcs *alloc_vmcs(void) |
| 2370 | { |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2371 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | static void free_vmcs(struct vmcs *vmcs) |
| 2375 | { |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2376 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2377 | } |
| 2378 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2379 | /* |
| 2380 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 2381 | */ |
| 2382 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 2383 | { |
| 2384 | if (!loaded_vmcs->vmcs) |
| 2385 | return; |
| 2386 | loaded_vmcs_clear(loaded_vmcs); |
| 2387 | free_vmcs(loaded_vmcs->vmcs); |
| 2388 | loaded_vmcs->vmcs = NULL; |
| 2389 | } |
| 2390 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 2391 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2392 | { |
| 2393 | int cpu; |
| 2394 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2395 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2396 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2397 | per_cpu(vmxarea, cpu) = NULL; |
| 2398 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2399 | } |
| 2400 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2401 | static __init int alloc_kvm_area(void) |
| 2402 | { |
| 2403 | int cpu; |
| 2404 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2405 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2406 | struct vmcs *vmcs; |
| 2407 | |
| 2408 | vmcs = alloc_vmcs_cpu(cpu); |
| 2409 | if (!vmcs) { |
| 2410 | free_kvm_area(); |
| 2411 | return -ENOMEM; |
| 2412 | } |
| 2413 | |
| 2414 | per_cpu(vmxarea, cpu) = vmcs; |
| 2415 | } |
| 2416 | return 0; |
| 2417 | } |
| 2418 | |
| 2419 | static __init int hardware_setup(void) |
| 2420 | { |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2421 | if (setup_vmcs_config(&vmcs_config) < 0) |
| 2422 | return -EIO; |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 2423 | |
| 2424 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 2425 | kvm_enable_efer_bits(EFER_NX); |
| 2426 | |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 2427 | if (!cpu_has_vmx_vpid()) |
| 2428 | enable_vpid = 0; |
| 2429 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 2430 | if (!cpu_has_vmx_ept() || |
| 2431 | !cpu_has_vmx_ept_4levels()) { |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 2432 | enable_ept = 0; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2433 | enable_unrestricted_guest = 0; |
| 2434 | } |
| 2435 | |
| 2436 | if (!cpu_has_vmx_unrestricted_guest()) |
| 2437 | enable_unrestricted_guest = 0; |
Sheng Yang | 93ba03c | 2009-04-01 15:52:32 +0800 | [diff] [blame] | 2438 | |
| 2439 | if (!cpu_has_vmx_flexpriority()) |
| 2440 | flexpriority_enabled = 0; |
| 2441 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 2442 | if (!cpu_has_vmx_tpr_shadow()) |
| 2443 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 2444 | |
Marcelo Tosatti | 54dee99 | 2009-06-11 12:07:44 -0300 | [diff] [blame] | 2445 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 2446 | kvm_disable_largepages(); |
| 2447 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2448 | if (!cpu_has_vmx_ple()) |
| 2449 | ple_gap = 0; |
| 2450 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2451 | if (nested) |
| 2452 | nested_vmx_setup_ctls_msrs(); |
| 2453 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2454 | return alloc_kvm_area(); |
| 2455 | } |
| 2456 | |
| 2457 | static __exit void hardware_unsetup(void) |
| 2458 | { |
| 2459 | free_kvm_area(); |
| 2460 | } |
| 2461 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2462 | static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save) |
| 2463 | { |
| 2464 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 2465 | |
Avi Kivity | 6af11b9 | 2007-03-19 13:18:10 +0200 | [diff] [blame] | 2466 | if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2467 | vmcs_write16(sf->selector, save->selector); |
| 2468 | vmcs_writel(sf->base, save->base); |
| 2469 | vmcs_write32(sf->limit, save->limit); |
| 2470 | vmcs_write32(sf->ar_bytes, save->ar); |
| 2471 | } else { |
| 2472 | u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK) |
| 2473 | << AR_DPL_SHIFT; |
| 2474 | vmcs_write32(sf->ar_bytes, 0x93 | dpl); |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 2479 | { |
| 2480 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2481 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2482 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2483 | vmx->emulation_required = 1; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2484 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2485 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2486 | vmx_segment_cache_clear(vmx); |
| 2487 | |
Avi Kivity | d0ba64f | 2011-01-03 14:28:51 +0200 | [diff] [blame] | 2488 | vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector); |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2489 | vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base); |
| 2490 | vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit); |
| 2491 | vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2492 | |
| 2493 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2494 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2495 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2496 | vmcs_writel(GUEST_RFLAGS, flags); |
| 2497 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2498 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 2499 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2500 | |
| 2501 | update_exception_bitmap(vcpu); |
| 2502 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2503 | if (emulate_invalid_guest_state) |
| 2504 | return; |
| 2505 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2506 | fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es); |
| 2507 | fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds); |
| 2508 | fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs); |
| 2509 | fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2510 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2511 | vmx_segment_cache_clear(vmx); |
| 2512 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2513 | vmcs_write16(GUEST_SS_SELECTOR, 0); |
| 2514 | vmcs_write32(GUEST_SS_AR_BYTES, 0x93); |
| 2515 | |
| 2516 | vmcs_write16(GUEST_CS_SELECTOR, |
| 2517 | vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK); |
| 2518 | vmcs_write32(GUEST_CS_AR_BYTES, 0x9b); |
| 2519 | } |
| 2520 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2521 | static gva_t rmode_tss_base(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2522 | { |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2523 | if (!kvm->arch.tss_addr) { |
Marcelo Tosatti | bc6678a | 2009-12-23 14:35:21 -0200 | [diff] [blame] | 2524 | struct kvm_memslots *slots; |
| 2525 | gfn_t base_gfn; |
| 2526 | |
Lai Jiangshan | 90d83dc | 2010-04-19 17:41:23 +0800 | [diff] [blame] | 2527 | slots = kvm_memslots(kvm); |
Avi Kivity | f495c6e | 2010-06-10 17:21:29 +0300 | [diff] [blame] | 2528 | base_gfn = slots->memslots[0].base_gfn + |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 2529 | kvm->memslots->memslots[0].npages - 3; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2530 | return base_gfn << PAGE_SHIFT; |
| 2531 | } |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 2532 | return kvm->arch.tss_addr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | static void fix_rmode_seg(int seg, struct kvm_save_segment *save) |
| 2536 | { |
| 2537 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 2538 | |
| 2539 | save->selector = vmcs_read16(sf->selector); |
| 2540 | save->base = vmcs_readl(sf->base); |
| 2541 | save->limit = vmcs_read32(sf->limit); |
| 2542 | save->ar = vmcs_read32(sf->ar_bytes); |
Jan Kiszka | 15b00f3 | 2007-11-19 10:21:45 +0100 | [diff] [blame] | 2543 | vmcs_write16(sf->selector, save->base >> 4); |
Gleb Natapov | 444e863 | 2010-12-27 17:25:04 +0200 | [diff] [blame] | 2544 | vmcs_write32(sf->base, save->base & 0xffff0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2545 | vmcs_write32(sf->limit, 0xffff); |
| 2546 | vmcs_write32(sf->ar_bytes, 0xf3); |
Gleb Natapov | 444e863 | 2010-12-27 17:25:04 +0200 | [diff] [blame] | 2547 | if (save->base & 0xf) |
| 2548 | printk_once(KERN_WARNING "kvm: segment base is not paragraph" |
| 2549 | " aligned when entering protected mode (seg=%d)", |
| 2550 | seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 2554 | { |
| 2555 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2556 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2557 | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2558 | if (enable_unrestricted_guest) |
| 2559 | return; |
| 2560 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2561 | vmx->emulation_required = 1; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2562 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2563 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 2564 | /* |
| 2565 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
| 2566 | * vcpu. Call it here with phys address pointing 16M below 4G. |
| 2567 | */ |
| 2568 | if (!vcpu->kvm->arch.tss_addr) { |
| 2569 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 2570 | "called before entering vcpu\n"); |
| 2571 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
| 2572 | vmx_set_tss_addr(vcpu->kvm, 0xfeffd000); |
| 2573 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 2574 | } |
| 2575 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2576 | vmx_segment_cache_clear(vmx); |
| 2577 | |
Avi Kivity | d0ba64f | 2011-01-03 14:28:51 +0200 | [diff] [blame] | 2578 | vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR); |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2579 | vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2580 | vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm)); |
| 2581 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2582 | vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2583 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
| 2584 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2585 | vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2586 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 2587 | |
| 2588 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2589 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2590 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2591 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2592 | |
| 2593 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2594 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2595 | update_exception_bitmap(vcpu); |
| 2596 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2597 | if (emulate_invalid_guest_state) |
| 2598 | goto continue_rmode; |
| 2599 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2600 | vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4); |
| 2601 | vmcs_write32(GUEST_SS_LIMIT, 0xffff); |
| 2602 | vmcs_write32(GUEST_SS_AR_BYTES, 0xf3); |
| 2603 | |
| 2604 | vmcs_write32(GUEST_CS_AR_BYTES, 0xf3); |
Michael Riepe | abacf8d | 2006-12-22 01:05:45 -0800 | [diff] [blame] | 2605 | vmcs_write32(GUEST_CS_LIMIT, 0xffff); |
Avi Kivity | 8cb5b03 | 2007-03-20 18:40:40 +0200 | [diff] [blame] | 2606 | if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000) |
| 2607 | vmcs_writel(GUEST_CS_BASE, 0xf0000); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2608 | vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4); |
| 2609 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2610 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es); |
| 2611 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds); |
| 2612 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs); |
| 2613 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 2614 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2615 | continue_rmode: |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 2616 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2617 | } |
| 2618 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2619 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 2620 | { |
| 2621 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2622 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 2623 | |
| 2624 | if (!msr) |
| 2625 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2626 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2627 | /* |
| 2628 | * Force kernel_gs_base reloading before EFER changes, as control |
| 2629 | * of this msr depends on is_long_mode(). |
| 2630 | */ |
| 2631 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2632 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2633 | if (efer & EFER_LMA) { |
| 2634 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 2635 | vmcs_read32(VM_ENTRY_CONTROLS) | |
| 2636 | VM_ENTRY_IA32E_MODE); |
| 2637 | msr->data = efer; |
| 2638 | } else { |
| 2639 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 2640 | vmcs_read32(VM_ENTRY_CONTROLS) & |
| 2641 | ~VM_ENTRY_IA32E_MODE); |
| 2642 | |
| 2643 | msr->data = efer & ~EFER_LME; |
| 2644 | } |
| 2645 | setup_msrs(vmx); |
| 2646 | } |
| 2647 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2648 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2649 | |
| 2650 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 2651 | { |
| 2652 | u32 guest_tr_ar; |
| 2653 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2654 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 2655 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2656 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
| 2657 | if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) { |
| 2658 | printk(KERN_DEBUG "%s: tss fixup for long mode. \n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2659 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2660 | vmcs_write32(GUEST_TR_AR_BYTES, |
| 2661 | (guest_tr_ar & ~AR_TYPE_MASK) |
| 2662 | | AR_TYPE_BUSY_64_TSS); |
| 2663 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 2664 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 2668 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2669 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 2670 | vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 2671 | & ~VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 2672 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | #endif |
| 2676 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2677 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 2678 | { |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2679 | vpid_sync_context(to_vmx(vcpu)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 2680 | if (enable_ept) { |
| 2681 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 2682 | return; |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 2683 | ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 2684 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2685 | } |
| 2686 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 2687 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 2688 | { |
| 2689 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 2690 | |
| 2691 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 2692 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 2693 | } |
| 2694 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 2695 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 2696 | { |
| 2697 | if (enable_ept && is_paging(vcpu)) |
| 2698 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 2699 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 2700 | } |
| 2701 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2702 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2703 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2704 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 2705 | |
| 2706 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 2707 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2708 | } |
| 2709 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2710 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 2711 | { |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2712 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 2713 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 2714 | return; |
| 2715 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2716 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 2717 | vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]); |
| 2718 | vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]); |
| 2719 | vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]); |
| 2720 | vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2721 | } |
| 2722 | } |
| 2723 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2724 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 2725 | { |
| 2726 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Joerg Roedel | ff03a07 | 2010-09-10 17:30:57 +0200 | [diff] [blame] | 2727 | vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 2728 | vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 2729 | vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 2730 | vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2731 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2732 | |
| 2733 | __set_bit(VCPU_EXREG_PDPTR, |
| 2734 | (unsigned long *)&vcpu->arch.regs_avail); |
| 2735 | __set_bit(VCPU_EXREG_PDPTR, |
| 2736 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2737 | } |
| 2738 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2739 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2740 | |
| 2741 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 2742 | unsigned long cr0, |
| 2743 | struct kvm_vcpu *vcpu) |
| 2744 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 2745 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 2746 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2747 | if (!(cr0 & X86_CR0_PG)) { |
| 2748 | /* From paging/starting to nonpaging */ |
| 2749 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 2750 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2751 | (CPU_BASED_CR3_LOAD_EXITING | |
| 2752 | CPU_BASED_CR3_STORE_EXITING)); |
| 2753 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2754 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2755 | } else if (!is_paging(vcpu)) { |
| 2756 | /* From nonpaging to paging */ |
| 2757 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 2758 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2759 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 2760 | CPU_BASED_CR3_STORE_EXITING)); |
| 2761 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2762 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2763 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 2764 | |
| 2765 | if (!(cr0 & X86_CR0_WP)) |
| 2766 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2767 | } |
| 2768 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2769 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 2770 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2771 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2772 | unsigned long hw_cr0; |
| 2773 | |
| 2774 | if (enable_unrestricted_guest) |
| 2775 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST) |
| 2776 | | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
| 2777 | else |
| 2778 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2779 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2780 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2781 | enter_pmode(vcpu); |
| 2782 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2783 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2784 | enter_rmode(vcpu); |
| 2785 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2786 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2787 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 2788 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2789 | enter_lmode(vcpu); |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 2790 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2791 | exit_lmode(vcpu); |
| 2792 | } |
| 2793 | #endif |
| 2794 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2795 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2796 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 2797 | |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 2798 | if (!vcpu->fpu_active) |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2799 | hw_cr0 |= X86_CR0_TS | X86_CR0_MP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 2800 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2801 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2802 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2803 | vcpu->arch.cr0 = cr0; |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 2804 | __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2805 | } |
| 2806 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2807 | static u64 construct_eptp(unsigned long root_hpa) |
| 2808 | { |
| 2809 | u64 eptp; |
| 2810 | |
| 2811 | /* TODO write the value reading from MSR */ |
| 2812 | eptp = VMX_EPT_DEFAULT_MT | |
| 2813 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; |
| 2814 | eptp |= (root_hpa & PAGE_MASK); |
| 2815 | |
| 2816 | return eptp; |
| 2817 | } |
| 2818 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2819 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 2820 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2821 | unsigned long guest_cr3; |
| 2822 | u64 eptp; |
| 2823 | |
| 2824 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2825 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2826 | eptp = construct_eptp(cr3); |
| 2827 | vmcs_write64(EPT_POINTER, eptp); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 2828 | guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) : |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 2829 | vcpu->kvm->arch.ept_identity_map_addr; |
Marcelo Tosatti | 7c93be4 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 2830 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2831 | } |
| 2832 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2833 | vmx_flush_tlb(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2834 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2835 | } |
| 2836 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2837 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2838 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2839 | unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ? |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2840 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); |
| 2841 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2842 | if (cr4 & X86_CR4_VMXE) { |
| 2843 | /* |
| 2844 | * To use VMXON (and later other VMX instructions), a guest |
| 2845 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 2846 | * So basically the check on whether to allow nested VMX |
| 2847 | * is here. |
| 2848 | */ |
| 2849 | if (!nested_vmx_allowed(vcpu)) |
| 2850 | return 1; |
| 2851 | } else if (to_vmx(vcpu)->nested.vmxon) |
| 2852 | return 1; |
| 2853 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2854 | vcpu->arch.cr4 = cr4; |
Avi Kivity | bc23008 | 2009-12-08 12:14:42 +0200 | [diff] [blame] | 2855 | if (enable_ept) { |
| 2856 | if (!is_paging(vcpu)) { |
| 2857 | hw_cr4 &= ~X86_CR4_PAE; |
| 2858 | hw_cr4 |= X86_CR4_PSE; |
| 2859 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 2860 | hw_cr4 &= ~X86_CR4_PAE; |
| 2861 | } |
| 2862 | } |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2863 | |
| 2864 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 2865 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2866 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2867 | } |
| 2868 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2869 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 2870 | struct kvm_segment *var, int seg) |
| 2871 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2872 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2873 | struct kvm_save_segment *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2874 | u32 ar; |
| 2875 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2876 | if (vmx->rmode.vm86_active |
| 2877 | && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES |
| 2878 | || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS |
| 2879 | || seg == VCPU_SREG_GS) |
| 2880 | && !emulate_invalid_guest_state) { |
| 2881 | switch (seg) { |
| 2882 | case VCPU_SREG_TR: save = &vmx->rmode.tr; break; |
| 2883 | case VCPU_SREG_ES: save = &vmx->rmode.es; break; |
| 2884 | case VCPU_SREG_DS: save = &vmx->rmode.ds; break; |
| 2885 | case VCPU_SREG_FS: save = &vmx->rmode.fs; break; |
| 2886 | case VCPU_SREG_GS: save = &vmx->rmode.gs; break; |
| 2887 | default: BUG(); |
| 2888 | } |
| 2889 | var->selector = save->selector; |
| 2890 | var->base = save->base; |
| 2891 | var->limit = save->limit; |
| 2892 | ar = save->ar; |
| 2893 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2894 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2895 | goto use_saved_rmode_seg; |
| 2896 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2897 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 2898 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 2899 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 2900 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2901 | use_saved_rmode_seg: |
Avi Kivity | 9fd4a3b | 2009-01-04 23:43:42 +0200 | [diff] [blame] | 2902 | if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2903 | ar = 0; |
| 2904 | var->type = ar & 15; |
| 2905 | var->s = (ar >> 4) & 1; |
| 2906 | var->dpl = (ar >> 5) & 3; |
| 2907 | var->present = (ar >> 7) & 1; |
| 2908 | var->avl = (ar >> 12) & 1; |
| 2909 | var->l = (ar >> 13) & 1; |
| 2910 | var->db = (ar >> 14) & 1; |
| 2911 | var->g = (ar >> 15) & 1; |
| 2912 | var->unusable = (ar >> 16) & 1; |
| 2913 | } |
| 2914 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2915 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 2916 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2917 | struct kvm_segment s; |
| 2918 | |
| 2919 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2920 | vmx_get_segment(vcpu, &s, seg); |
| 2921 | return s.base; |
| 2922 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2923 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2924 | } |
| 2925 | |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 2926 | static int __vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2927 | { |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 2928 | if (!is_protmode(vcpu)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2929 | return 0; |
| 2930 | |
Avi Kivity | f4c63e5 | 2011-03-07 14:54:28 +0200 | [diff] [blame] | 2931 | if (!is_long_mode(vcpu) |
| 2932 | && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */ |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2933 | return 3; |
| 2934 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2935 | 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] | 2936 | } |
| 2937 | |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 2938 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
| 2939 | { |
| 2940 | if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) { |
| 2941 | __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
| 2942 | to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu); |
| 2943 | } |
| 2944 | return to_vmx(vcpu)->cpl; |
| 2945 | } |
| 2946 | |
| 2947 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2948 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2949 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2950 | u32 ar; |
| 2951 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2952 | if (var->unusable) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2953 | ar = 1 << 16; |
| 2954 | else { |
| 2955 | ar = var->type & 15; |
| 2956 | ar |= (var->s & 1) << 4; |
| 2957 | ar |= (var->dpl & 3) << 5; |
| 2958 | ar |= (var->present & 1) << 7; |
| 2959 | ar |= (var->avl & 1) << 12; |
| 2960 | ar |= (var->l & 1) << 13; |
| 2961 | ar |= (var->db & 1) << 14; |
| 2962 | ar |= (var->g & 1) << 15; |
| 2963 | } |
Uri Lublin | f7fbf1f | 2006-12-13 00:34:00 -0800 | [diff] [blame] | 2964 | if (ar == 0) /* a 0 value means unusable */ |
| 2965 | ar = AR_UNUSABLE_MASK; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2966 | |
| 2967 | return ar; |
| 2968 | } |
| 2969 | |
| 2970 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 2971 | struct kvm_segment *var, int seg) |
| 2972 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2973 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2974 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 2975 | u32 ar; |
| 2976 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2977 | vmx_segment_cache_clear(vmx); |
| 2978 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2979 | if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) { |
Gleb Natapov | a8ba6c2 | 2011-02-21 12:07:58 +0200 | [diff] [blame] | 2980 | vmcs_write16(sf->selector, var->selector); |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2981 | vmx->rmode.tr.selector = var->selector; |
| 2982 | vmx->rmode.tr.base = var->base; |
| 2983 | vmx->rmode.tr.limit = var->limit; |
| 2984 | vmx->rmode.tr.ar = vmx_segment_access_rights(var); |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2985 | return; |
| 2986 | } |
| 2987 | vmcs_writel(sf->base, var->base); |
| 2988 | vmcs_write32(sf->limit, var->limit); |
| 2989 | vmcs_write16(sf->selector, var->selector); |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2990 | if (vmx->rmode.vm86_active && var->s) { |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2991 | /* |
| 2992 | * Hack real-mode segments into vm86 compatibility. |
| 2993 | */ |
| 2994 | if (var->base == 0xffff0000 && var->selector == 0xf000) |
| 2995 | vmcs_writel(sf->base, 0xf0000); |
| 2996 | ar = 0xf3; |
| 2997 | } else |
| 2998 | ar = vmx_segment_access_rights(var); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2999 | |
| 3000 | /* |
| 3001 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 3002 | * qemu binaries. |
| 3003 | * IA32 arch specifies that at the time of processor reset the |
| 3004 | * "Accessed" bit in the AR field of segment registers is 1. And qemu |
| 3005 | * is setting it to 0 in the usedland code. This causes invalid guest |
| 3006 | * state vmexit when "unrestricted guest" mode is turned on. |
| 3007 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 3008 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 3009 | * kvm hack. |
| 3010 | */ |
| 3011 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
| 3012 | ar |= 0x1; /* Accessed */ |
| 3013 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3014 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3015 | __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3016 | } |
| 3017 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3018 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 3019 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3020 | 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] | 3021 | |
| 3022 | *db = (ar >> 14) & 1; |
| 3023 | *l = (ar >> 13) & 1; |
| 3024 | } |
| 3025 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3026 | 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] | 3027 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3028 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 3029 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3030 | } |
| 3031 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3032 | 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] | 3033 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3034 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 3035 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3036 | } |
| 3037 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3038 | 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] | 3039 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3040 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 3041 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3042 | } |
| 3043 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3044 | 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] | 3045 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3046 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 3047 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3048 | } |
| 3049 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3050 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 3051 | { |
| 3052 | struct kvm_segment var; |
| 3053 | u32 ar; |
| 3054 | |
| 3055 | vmx_get_segment(vcpu, &var, seg); |
| 3056 | ar = vmx_segment_access_rights(&var); |
| 3057 | |
| 3058 | if (var.base != (var.selector << 4)) |
| 3059 | return false; |
| 3060 | if (var.limit != 0xffff) |
| 3061 | return false; |
| 3062 | if (ar != 0xf3) |
| 3063 | return false; |
| 3064 | |
| 3065 | return true; |
| 3066 | } |
| 3067 | |
| 3068 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 3069 | { |
| 3070 | struct kvm_segment cs; |
| 3071 | unsigned int cs_rpl; |
| 3072 | |
| 3073 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 3074 | cs_rpl = cs.selector & SELECTOR_RPL_MASK; |
| 3075 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3076 | if (cs.unusable) |
| 3077 | return false; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3078 | if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK)) |
| 3079 | return false; |
| 3080 | if (!cs.s) |
| 3081 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3082 | if (cs.type & AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3083 | if (cs.dpl > cs_rpl) |
| 3084 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3085 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3086 | if (cs.dpl != cs_rpl) |
| 3087 | return false; |
| 3088 | } |
| 3089 | if (!cs.present) |
| 3090 | return false; |
| 3091 | |
| 3092 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 3093 | return true; |
| 3094 | } |
| 3095 | |
| 3096 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 3097 | { |
| 3098 | struct kvm_segment ss; |
| 3099 | unsigned int ss_rpl; |
| 3100 | |
| 3101 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 3102 | ss_rpl = ss.selector & SELECTOR_RPL_MASK; |
| 3103 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3104 | if (ss.unusable) |
| 3105 | return true; |
| 3106 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3107 | return false; |
| 3108 | if (!ss.s) |
| 3109 | return false; |
| 3110 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 3111 | return false; |
| 3112 | if (!ss.present) |
| 3113 | return false; |
| 3114 | |
| 3115 | return true; |
| 3116 | } |
| 3117 | |
| 3118 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 3119 | { |
| 3120 | struct kvm_segment var; |
| 3121 | unsigned int rpl; |
| 3122 | |
| 3123 | vmx_get_segment(vcpu, &var, seg); |
| 3124 | rpl = var.selector & SELECTOR_RPL_MASK; |
| 3125 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3126 | if (var.unusable) |
| 3127 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3128 | if (!var.s) |
| 3129 | return false; |
| 3130 | if (!var.present) |
| 3131 | return false; |
| 3132 | if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) { |
| 3133 | if (var.dpl < rpl) /* DPL < RPL */ |
| 3134 | return false; |
| 3135 | } |
| 3136 | |
| 3137 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 3138 | * rights flags |
| 3139 | */ |
| 3140 | return true; |
| 3141 | } |
| 3142 | |
| 3143 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 3144 | { |
| 3145 | struct kvm_segment tr; |
| 3146 | |
| 3147 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 3148 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3149 | if (tr.unusable) |
| 3150 | return false; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3151 | if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 3152 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3153 | 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] | 3154 | return false; |
| 3155 | if (!tr.present) |
| 3156 | return false; |
| 3157 | |
| 3158 | return true; |
| 3159 | } |
| 3160 | |
| 3161 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 3162 | { |
| 3163 | struct kvm_segment ldtr; |
| 3164 | |
| 3165 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 3166 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3167 | if (ldtr.unusable) |
| 3168 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3169 | if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 3170 | return false; |
| 3171 | if (ldtr.type != 2) |
| 3172 | return false; |
| 3173 | if (!ldtr.present) |
| 3174 | return false; |
| 3175 | |
| 3176 | return true; |
| 3177 | } |
| 3178 | |
| 3179 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 3180 | { |
| 3181 | struct kvm_segment cs, ss; |
| 3182 | |
| 3183 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 3184 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 3185 | |
| 3186 | return ((cs.selector & SELECTOR_RPL_MASK) == |
| 3187 | (ss.selector & SELECTOR_RPL_MASK)); |
| 3188 | } |
| 3189 | |
| 3190 | /* |
| 3191 | * Check if guest state is valid. Returns true if valid, false if |
| 3192 | * not. |
| 3193 | * We assume that registers are always usable |
| 3194 | */ |
| 3195 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 3196 | { |
| 3197 | /* real mode guest state checks */ |
Avi Kivity | 3eeb328 | 2010-01-21 15:31:48 +0200 | [diff] [blame] | 3198 | if (!is_protmode(vcpu)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3199 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 3200 | return false; |
| 3201 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 3202 | return false; |
| 3203 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 3204 | return false; |
| 3205 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 3206 | return false; |
| 3207 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 3208 | return false; |
| 3209 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 3210 | return false; |
| 3211 | } else { |
| 3212 | /* protected mode guest state checks */ |
| 3213 | if (!cs_ss_rpl_check(vcpu)) |
| 3214 | return false; |
| 3215 | if (!code_segment_valid(vcpu)) |
| 3216 | return false; |
| 3217 | if (!stack_segment_valid(vcpu)) |
| 3218 | return false; |
| 3219 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 3220 | return false; |
| 3221 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 3222 | return false; |
| 3223 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 3224 | return false; |
| 3225 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 3226 | return false; |
| 3227 | if (!tr_valid(vcpu)) |
| 3228 | return false; |
| 3229 | if (!ldtr_valid(vcpu)) |
| 3230 | return false; |
| 3231 | } |
| 3232 | /* TODO: |
| 3233 | * - Add checks on RIP |
| 3234 | * - Add checks on RFLAGS |
| 3235 | */ |
| 3236 | |
| 3237 | return true; |
| 3238 | } |
| 3239 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3240 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3241 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3242 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3243 | u16 data = 0; |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3244 | int r, idx, ret = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3245 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3246 | idx = srcu_read_lock(&kvm->srcu); |
| 3247 | fn = rmode_tss_base(kvm) >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3248 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3249 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3250 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3251 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 3252 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 3253 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3254 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3255 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3256 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 3257 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3258 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3259 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3260 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3261 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3262 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3263 | r = kvm_write_guest_page(kvm, fn, &data, |
| 3264 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 3265 | sizeof(u8)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3266 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3267 | goto out; |
| 3268 | |
| 3269 | ret = 1; |
| 3270 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3271 | srcu_read_unlock(&kvm->srcu, idx); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3272 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3273 | } |
| 3274 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3275 | static int init_rmode_identity_map(struct kvm *kvm) |
| 3276 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3277 | int i, idx, r, ret; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3278 | pfn_t identity_map_pfn; |
| 3279 | u32 tmp; |
| 3280 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3281 | if (!enable_ept) |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3282 | return 1; |
| 3283 | if (unlikely(!kvm->arch.ept_identity_pagetable)) { |
| 3284 | printk(KERN_ERR "EPT: identity-mapping pagetable " |
| 3285 | "haven't been allocated!\n"); |
| 3286 | return 0; |
| 3287 | } |
| 3288 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
| 3289 | return 1; |
| 3290 | ret = 0; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3291 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3292 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3293 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 3294 | if (r < 0) |
| 3295 | goto out; |
| 3296 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 3297 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 3298 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 3299 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 3300 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 3301 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 3302 | if (r < 0) |
| 3303 | goto out; |
| 3304 | } |
| 3305 | kvm->arch.ept_identity_pagetable_done = true; |
| 3306 | ret = 1; |
| 3307 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3308 | srcu_read_unlock(&kvm->srcu, idx); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3309 | return ret; |
| 3310 | } |
| 3311 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3312 | static void seg_setup(int seg) |
| 3313 | { |
| 3314 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3315 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3316 | |
| 3317 | vmcs_write16(sf->selector, 0); |
| 3318 | vmcs_writel(sf->base, 0); |
| 3319 | vmcs_write32(sf->limit, 0xffff); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3320 | if (enable_unrestricted_guest) { |
| 3321 | ar = 0x93; |
| 3322 | if (seg == VCPU_SREG_CS) |
| 3323 | ar |= 0x08; /* code segment */ |
| 3324 | } else |
| 3325 | ar = 0xf3; |
| 3326 | |
| 3327 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3328 | } |
| 3329 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3330 | static int alloc_apic_access_page(struct kvm *kvm) |
| 3331 | { |
| 3332 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 3333 | int r = 0; |
| 3334 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3335 | mutex_lock(&kvm->slots_lock); |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 3336 | if (kvm->arch.apic_access_page) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3337 | goto out; |
| 3338 | kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT; |
| 3339 | kvm_userspace_mem.flags = 0; |
| 3340 | kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL; |
| 3341 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
| 3342 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 3343 | if (r) |
| 3344 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3345 | |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 3346 | kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3347 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3348 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3349 | return r; |
| 3350 | } |
| 3351 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3352 | static int alloc_identity_pagetable(struct kvm *kvm) |
| 3353 | { |
| 3354 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 3355 | int r = 0; |
| 3356 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3357 | mutex_lock(&kvm->slots_lock); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3358 | if (kvm->arch.ept_identity_pagetable) |
| 3359 | goto out; |
| 3360 | kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT; |
| 3361 | kvm_userspace_mem.flags = 0; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3362 | kvm_userspace_mem.guest_phys_addr = |
| 3363 | kvm->arch.ept_identity_map_addr; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3364 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
| 3365 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 3366 | if (r) |
| 3367 | goto out; |
| 3368 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3369 | kvm->arch.ept_identity_pagetable = gfn_to_page(kvm, |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3370 | kvm->arch.ept_identity_map_addr >> PAGE_SHIFT); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3371 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3372 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3373 | return r; |
| 3374 | } |
| 3375 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3376 | static void allocate_vpid(struct vcpu_vmx *vmx) |
| 3377 | { |
| 3378 | int vpid; |
| 3379 | |
| 3380 | vmx->vpid = 0; |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 3381 | if (!enable_vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3382 | return; |
| 3383 | spin_lock(&vmx_vpid_lock); |
| 3384 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 3385 | if (vpid < VMX_NR_VPIDS) { |
| 3386 | vmx->vpid = vpid; |
| 3387 | __set_bit(vpid, vmx_vpid_bitmap); |
| 3388 | } |
| 3389 | spin_unlock(&vmx_vpid_lock); |
| 3390 | } |
| 3391 | |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 3392 | static void free_vpid(struct vcpu_vmx *vmx) |
| 3393 | { |
| 3394 | if (!enable_vpid) |
| 3395 | return; |
| 3396 | spin_lock(&vmx_vpid_lock); |
| 3397 | if (vmx->vpid != 0) |
| 3398 | __clear_bit(vmx->vpid, vmx_vpid_bitmap); |
| 3399 | spin_unlock(&vmx_vpid_lock); |
| 3400 | } |
| 3401 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3402 | 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] | 3403 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3404 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3405 | |
| 3406 | if (!cpu_has_vmx_msr_bitmap()) |
| 3407 | return; |
| 3408 | |
| 3409 | /* |
| 3410 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 3411 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 3412 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 3413 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3414 | if (msr <= 0x1fff) { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3415 | __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */ |
| 3416 | __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3417 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 3418 | msr &= 0x1fff; |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3419 | __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */ |
| 3420 | __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3421 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3422 | } |
| 3423 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3424 | static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only) |
| 3425 | { |
| 3426 | if (!longmode_only) |
| 3427 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr); |
| 3428 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr); |
| 3429 | } |
| 3430 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3431 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3432 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 3433 | * will not change in the lifetime of the guest. |
| 3434 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 3435 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 3436 | */ |
| 3437 | static void vmx_set_constant_host_state(void) |
| 3438 | { |
| 3439 | u32 low32, high32; |
| 3440 | unsigned long tmpl; |
| 3441 | struct desc_ptr dt; |
| 3442 | |
| 3443 | vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */ |
| 3444 | vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */ |
| 3445 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 3446 | |
| 3447 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
| 3448 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3449 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3450 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3451 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 3452 | |
| 3453 | native_store_idt(&dt); |
| 3454 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
| 3455 | |
| 3456 | asm("mov $.Lkvm_vmx_return, %0" : "=r"(tmpl)); |
| 3457 | vmcs_writel(HOST_RIP, tmpl); /* 22.2.5 */ |
| 3458 | |
| 3459 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 3460 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 3461 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 3462 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 3463 | |
| 3464 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 3465 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 3466 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 3467 | } |
| 3468 | } |
| 3469 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3470 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 3471 | { |
| 3472 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 3473 | if (enable_ept) |
| 3474 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 3475 | if (is_guest_mode(&vmx->vcpu)) |
| 3476 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 3477 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3478 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 3479 | } |
| 3480 | |
| 3481 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 3482 | { |
| 3483 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
| 3484 | if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) { |
| 3485 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 3486 | #ifdef CONFIG_X86_64 |
| 3487 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 3488 | CPU_BASED_CR8_LOAD_EXITING; |
| 3489 | #endif |
| 3490 | } |
| 3491 | if (!enable_ept) |
| 3492 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 3493 | CPU_BASED_CR3_LOAD_EXITING | |
| 3494 | CPU_BASED_INVLPG_EXITING; |
| 3495 | return exec_control; |
| 3496 | } |
| 3497 | |
| 3498 | static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) |
| 3499 | { |
| 3500 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
| 3501 | if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 3502 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 3503 | if (vmx->vpid == 0) |
| 3504 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 3505 | if (!enable_ept) { |
| 3506 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 3507 | enable_unrestricted_guest = 0; |
| 3508 | } |
| 3509 | if (!enable_unrestricted_guest) |
| 3510 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 3511 | if (!ple_gap) |
| 3512 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 3513 | return exec_control; |
| 3514 | } |
| 3515 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3516 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3517 | * Sets up the vmcs for emulated real mode. |
| 3518 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3519 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3520 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3521 | unsigned long a; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3522 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3523 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3524 | /* I/O */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3525 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
| 3526 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3527 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3528 | if (cpu_has_vmx_msr_bitmap()) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3529 | vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3530 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3531 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 3532 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3533 | /* Control */ |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3534 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, |
| 3535 | vmcs_config.pin_based_exec_ctrl); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3536 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3537 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3538 | |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 3539 | if (cpu_has_secondary_exec_ctrls()) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3540 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 3541 | vmx_secondary_exec_control(vmx)); |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 3542 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3543 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3544 | if (ple_gap) { |
| 3545 | vmcs_write32(PLE_GAP, ple_gap); |
| 3546 | vmcs_write32(PLE_WINDOW, ple_window); |
| 3547 | } |
| 3548 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 3549 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf); |
| 3550 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3551 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 3552 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 3553 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 3554 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3555 | vmx_set_constant_host_state(); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3556 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3557 | rdmsrl(MSR_FS_BASE, a); |
| 3558 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 3559 | rdmsrl(MSR_GS_BASE, a); |
| 3560 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 3561 | #else |
| 3562 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 3563 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 3564 | #endif |
| 3565 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3566 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 3567 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 3568 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3569 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 3570 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3571 | |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3572 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3573 | u32 msr_low, msr_high; |
| 3574 | u64 host_pat; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3575 | rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high); |
| 3576 | host_pat = msr_low | ((u64) msr_high << 32); |
| 3577 | /* Write the default value follow host pat */ |
| 3578 | vmcs_write64(GUEST_IA32_PAT, host_pat); |
| 3579 | /* Keep arch.pat sync with GUEST_IA32_PAT */ |
| 3580 | vmx->vcpu.arch.pat = host_pat; |
| 3581 | } |
| 3582 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3583 | for (i = 0; i < NR_VMX_MSR; ++i) { |
| 3584 | u32 index = vmx_msr_index[i]; |
| 3585 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3586 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3587 | |
| 3588 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 3589 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 3590 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 3591 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3592 | vmx->guest_msrs[j].index = i; |
| 3593 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 3594 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3595 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3596 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3597 | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3598 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3599 | |
| 3600 | /* 22.2.1, 20.8.1 */ |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3601 | vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl); |
| 3602 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3603 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3604 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3605 | |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 3606 | kvm_write_tsc(&vmx->vcpu, 0); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3607 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3608 | return 0; |
| 3609 | } |
| 3610 | |
| 3611 | static int vmx_vcpu_reset(struct kvm_vcpu *vcpu) |
| 3612 | { |
| 3613 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3614 | u64 msr; |
Xiao Guangrong | 4b9d3a0 | 2010-06-08 10:15:51 +0800 | [diff] [blame] | 3615 | int ret; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3616 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3617 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3618 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3619 | vmx->rmode.vm86_active = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3620 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3621 | vmx->soft_vnmi_blocked = 0; |
| 3622 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3623 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 3624 | kvm_set_cr8(&vmx->vcpu, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3625 | msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 3626 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3627 | msr |= MSR_IA32_APICBASE_BSP; |
| 3628 | kvm_set_apic_base(&vmx->vcpu, msr); |
| 3629 | |
Jan Kiszka | 10ab25c | 2010-05-25 16:01:50 +0200 | [diff] [blame] | 3630 | ret = fx_init(&vmx->vcpu); |
| 3631 | if (ret != 0) |
| 3632 | goto out; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3633 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3634 | vmx_segment_cache_clear(vmx); |
| 3635 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 3636 | seg_setup(VCPU_SREG_CS); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3637 | /* |
| 3638 | * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode |
| 3639 | * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh. |
| 3640 | */ |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 3641 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) { |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3642 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
| 3643 | vmcs_writel(GUEST_CS_BASE, 0x000f0000); |
| 3644 | } else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3645 | vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8); |
| 3646 | vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3647 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3648 | |
| 3649 | seg_setup(VCPU_SREG_DS); |
| 3650 | seg_setup(VCPU_SREG_ES); |
| 3651 | seg_setup(VCPU_SREG_FS); |
| 3652 | seg_setup(VCPU_SREG_GS); |
| 3653 | seg_setup(VCPU_SREG_SS); |
| 3654 | |
| 3655 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 3656 | vmcs_writel(GUEST_TR_BASE, 0); |
| 3657 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 3658 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 3659 | |
| 3660 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 3661 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 3662 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 3663 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 3664 | |
| 3665 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 3666 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 3667 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 3668 | |
| 3669 | vmcs_writel(GUEST_RFLAGS, 0x02); |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 3670 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3671 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3672 | else |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3673 | kvm_rip_write(vcpu, 0); |
| 3674 | kvm_register_write(vcpu, VCPU_REGS_RSP, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3675 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3676 | vmcs_writel(GUEST_DR7, 0x400); |
| 3677 | |
| 3678 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 3679 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 3680 | |
| 3681 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 3682 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 3683 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 3684 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3685 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
| 3686 | vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
| 3687 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3688 | /* Special registers */ |
| 3689 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 3690 | |
| 3691 | setup_msrs(vmx); |
| 3692 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3693 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 3694 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3695 | if (cpu_has_vmx_tpr_shadow()) { |
| 3696 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
| 3697 | if (vm_need_tpr_shadow(vmx->vcpu.kvm)) |
| 3698 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Takuya Yoshikawa | afc2018 | 2011-03-05 12:40:20 +0900 | [diff] [blame] | 3699 | __pa(vmx->vcpu.arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3700 | vmcs_write32(TPR_THRESHOLD, 0); |
| 3701 | } |
| 3702 | |
| 3703 | if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 3704 | vmcs_write64(APIC_ACCESS_ADDR, |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 3705 | page_to_phys(vmx->vcpu.kvm->arch.apic_access_page)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3706 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3707 | if (vmx->vpid != 0) |
| 3708 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 3709 | |
Eduardo Habkost | fa40052 | 2009-10-24 02:49:58 -0200 | [diff] [blame] | 3710 | 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] | 3711 | vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3712 | vmx_set_cr4(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3713 | vmx_set_efer(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3714 | vmx_fpu_activate(&vmx->vcpu); |
| 3715 | update_exception_bitmap(&vmx->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3716 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 3717 | vpid_sync_context(vmx); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3718 | |
Marcelo Tosatti | 3200f40 | 2008-03-29 20:17:59 -0300 | [diff] [blame] | 3719 | ret = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3720 | |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3721 | /* HACK: Don't enable emulation on guest boot/reset */ |
| 3722 | vmx->emulation_required = 0; |
| 3723 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3724 | out: |
| 3725 | return ret; |
| 3726 | } |
| 3727 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3728 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
| 3729 | { |
| 3730 | u32 cpu_based_vm_exec_control; |
| 3731 | |
| 3732 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 3733 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 3734 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 3735 | } |
| 3736 | |
| 3737 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
| 3738 | { |
| 3739 | u32 cpu_based_vm_exec_control; |
| 3740 | |
| 3741 | if (!cpu_has_virtual_nmis()) { |
| 3742 | enable_irq_window(vcpu); |
| 3743 | return; |
| 3744 | } |
| 3745 | |
Avi Kivity | 30bd0c4 | 2010-11-01 23:20:48 +0200 | [diff] [blame] | 3746 | if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
| 3747 | enable_irq_window(vcpu); |
| 3748 | return; |
| 3749 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3750 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 3751 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING; |
| 3752 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 3753 | } |
| 3754 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3755 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3756 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3757 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3758 | uint32_t intr; |
| 3759 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3760 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3761 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3762 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 3763 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3764 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 3765 | int inc_eip = 0; |
| 3766 | if (vcpu->arch.interrupt.soft) |
| 3767 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 3768 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 3769 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3770 | return; |
| 3771 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3772 | intr = irq | INTR_INFO_VALID_MASK; |
| 3773 | if (vcpu->arch.interrupt.soft) { |
| 3774 | intr |= INTR_TYPE_SOFT_INTR; |
| 3775 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 3776 | vmx->vcpu.arch.event_exit_inst_len); |
| 3777 | } else |
| 3778 | intr |= INTR_TYPE_EXT_INTR; |
| 3779 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 3780 | vmx_clear_hlt(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3781 | } |
| 3782 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3783 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 3784 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 3785 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3786 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3787 | if (!cpu_has_virtual_nmis()) { |
| 3788 | /* |
| 3789 | * Tracking the NMI-blocked state in software is built upon |
| 3790 | * finding the next open IRQ window. This, in turn, depends on |
| 3791 | * well-behaving guests: They have to keep IRQs disabled at |
| 3792 | * least as long as the NMI handler runs. Otherwise we may |
| 3793 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 3794 | * highly unlikely, we can live with the residual risk. |
| 3795 | */ |
| 3796 | vmx->soft_vnmi_blocked = 1; |
| 3797 | vmx->vnmi_blocked_time = 0; |
| 3798 | } |
| 3799 | |
Jan Kiszka | 487b391 | 2008-09-26 09:30:56 +0200 | [diff] [blame] | 3800 | ++vcpu->stat.nmi_injections; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 3801 | vmx->nmi_known_unmasked = false; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3802 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 3803 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 3804 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 3805 | return; |
| 3806 | } |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3807 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 3808 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 3809 | vmx_clear_hlt(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3810 | } |
| 3811 | |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 3812 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 3813 | { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3814 | if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked) |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 3815 | return 0; |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 3816 | |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 3817 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
Avi Kivity | 30bd0c4 | 2010-11-01 23:20:48 +0200 | [diff] [blame] | 3818 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 3819 | | GUEST_INTR_STATE_NMI)); |
Jan Kiszka | 33f089c | 2008-09-26 09:30:49 +0200 | [diff] [blame] | 3820 | } |
| 3821 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3822 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 3823 | { |
| 3824 | if (!cpu_has_virtual_nmis()) |
| 3825 | return to_vmx(vcpu)->soft_vnmi_blocked; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 3826 | if (to_vmx(vcpu)->nmi_known_unmasked) |
| 3827 | return false; |
Avi Kivity | c332c83 | 2010-05-04 12:24:12 +0300 | [diff] [blame] | 3828 | return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 3832 | { |
| 3833 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3834 | |
| 3835 | if (!cpu_has_virtual_nmis()) { |
| 3836 | if (vmx->soft_vnmi_blocked != masked) { |
| 3837 | vmx->soft_vnmi_blocked = masked; |
| 3838 | vmx->vnmi_blocked_time = 0; |
| 3839 | } |
| 3840 | } else { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 3841 | vmx->nmi_known_unmasked = !masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3842 | if (masked) |
| 3843 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 3844 | GUEST_INTR_STATE_NMI); |
| 3845 | else |
| 3846 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 3847 | GUEST_INTR_STATE_NMI); |
| 3848 | } |
| 3849 | } |
| 3850 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 3851 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 3852 | { |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 3853 | return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
| 3854 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 3855 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 3856 | } |
| 3857 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 3858 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 3859 | { |
| 3860 | int ret; |
| 3861 | struct kvm_userspace_memory_region tss_mem = { |
Sheng Yang | 6fe6397 | 2008-10-16 17:30:58 +0800 | [diff] [blame] | 3862 | .slot = TSS_PRIVATE_MEMSLOT, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 3863 | .guest_phys_addr = addr, |
| 3864 | .memory_size = PAGE_SIZE * 3, |
| 3865 | .flags = 0, |
| 3866 | }; |
| 3867 | |
| 3868 | ret = kvm_set_memory_region(kvm, &tss_mem, 0); |
| 3869 | if (ret) |
| 3870 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 3871 | kvm->arch.tss_addr = addr; |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 3872 | if (!init_rmode_tss(kvm)) |
| 3873 | return -ENOMEM; |
| 3874 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 3875 | return 0; |
| 3876 | } |
| 3877 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3878 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 3879 | int vec, u32 err_code) |
| 3880 | { |
Nitin A Kamble | b3f3770 | 2007-05-17 15:50:34 +0300 | [diff] [blame] | 3881 | /* |
| 3882 | * Instruction with address size override prefix opcode 0x67 |
| 3883 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 3884 | */ |
| 3885 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 3886 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3887 | return 1; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3888 | /* |
| 3889 | * Forward all other exceptions that are valid in real mode. |
| 3890 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 3891 | * the required debugging infrastructure rework. |
| 3892 | */ |
| 3893 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3894 | case DB_VECTOR: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3895 | if (vcpu->guest_debug & |
| 3896 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 3897 | return 0; |
| 3898 | kvm_queue_exception(vcpu, vec); |
| 3899 | return 1; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3900 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 3901 | /* |
| 3902 | * Update instruction length as we may reinject the exception |
| 3903 | * from user space while in guest debugging mode. |
| 3904 | */ |
| 3905 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 3906 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3907 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 3908 | return 0; |
| 3909 | /* fall through */ |
| 3910 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3911 | case OF_VECTOR: |
| 3912 | case BR_VECTOR: |
| 3913 | case UD_VECTOR: |
| 3914 | case DF_VECTOR: |
| 3915 | case SS_VECTOR: |
| 3916 | case GP_VECTOR: |
| 3917 | case MF_VECTOR: |
| 3918 | kvm_queue_exception(vcpu, vec); |
| 3919 | return 1; |
| 3920 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3921 | return 0; |
| 3922 | } |
| 3923 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 3924 | /* |
| 3925 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 3926 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 3927 | * We pass a fake environment to the machine check handler because we want |
| 3928 | * the guest to be always treated like user space, no matter what context |
| 3929 | * it used internally. |
| 3930 | */ |
| 3931 | static void kvm_machine_check(void) |
| 3932 | { |
| 3933 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 3934 | struct pt_regs regs = { |
| 3935 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 3936 | .flags = X86_EFLAGS_IF, |
| 3937 | }; |
| 3938 | |
| 3939 | do_machine_check(®s, 0); |
| 3940 | #endif |
| 3941 | } |
| 3942 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3943 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 3944 | { |
| 3945 | /* already handled by vcpu_run */ |
| 3946 | return 1; |
| 3947 | } |
| 3948 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3949 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3950 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 3951 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3952 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3953 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3954 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3955 | u32 vect_info; |
| 3956 | enum emulation_result er; |
| 3957 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 3958 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 3959 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3960 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 3961 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 3962 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 3963 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3964 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
Avi Kivity | 65ac726 | 2009-11-04 11:59:01 +0200 | [diff] [blame] | 3965 | !is_page_fault(intr_info)) { |
| 3966 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 3967 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
| 3968 | vcpu->run->internal.ndata = 2; |
| 3969 | vcpu->run->internal.data[0] = vect_info; |
| 3970 | vcpu->run->internal.data[1] = intr_info; |
| 3971 | return 0; |
| 3972 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3973 | |
Jan Kiszka | e4a4188 | 2008-09-26 09:30:46 +0200 | [diff] [blame] | 3974 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 3975 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 3976 | |
| 3977 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 3978 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 3979 | return 1; |
| 3980 | } |
| 3981 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3982 | if (is_invalid_opcode(intr_info)) { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 3983 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3984 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 3985 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 3986 | return 1; |
| 3987 | } |
| 3988 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3989 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 3990 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3991 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 3992 | if (is_page_fault(intr_info)) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3993 | /* EPT won't cause page fault directly */ |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3994 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3995 | BUG(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3996 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3997 | trace_kvm_page_fault(cr2, error_code); |
| 3998 | |
Gleb Natapov | 3298b75 | 2009-05-11 13:35:46 +0300 | [diff] [blame] | 3999 | if (kvm_event_needs_reinjection(vcpu)) |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 4000 | kvm_mmu_unprotect_page_virt(vcpu, cr2); |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4001 | return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4002 | } |
| 4003 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4004 | if (vmx->rmode.vm86_active && |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4005 | handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK, |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 4006 | error_code)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4007 | if (vcpu->arch.halt_request) { |
| 4008 | vcpu->arch.halt_request = 0; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 4009 | return kvm_emulate_halt(vcpu); |
| 4010 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4011 | return 1; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 4012 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4013 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4014 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4015 | switch (ex_no) { |
| 4016 | case DB_VECTOR: |
| 4017 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 4018 | if (!(vcpu->guest_debug & |
| 4019 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
| 4020 | vcpu->arch.dr6 = dr6 | DR6_FIXED_1; |
| 4021 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 4022 | return 1; |
| 4023 | } |
| 4024 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 4025 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 4026 | /* fall through */ |
| 4027 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 4028 | /* |
| 4029 | * Update instruction length as we may reinject #BP from |
| 4030 | * user space while in guest debugging mode. Reading it for |
| 4031 | * #DB as well causes no harm, it is not used in that case. |
| 4032 | */ |
| 4033 | vmx->vcpu.arch.event_exit_inst_len = |
| 4034 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4035 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 4036 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4037 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 4038 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4039 | break; |
| 4040 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4041 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 4042 | kvm_run->ex.exception = ex_no; |
| 4043 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4044 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4045 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4046 | return 0; |
| 4047 | } |
| 4048 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4049 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4050 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 4051 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4052 | return 1; |
| 4053 | } |
| 4054 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4055 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4056 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4057 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4058 | return 0; |
| 4059 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4060 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4061 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4062 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4063 | unsigned long exit_qualification; |
Jan Kiszka | 34c33d1 | 2009-02-08 13:28:15 +0100 | [diff] [blame] | 4064 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4065 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4066 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4067 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4068 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 4069 | in = (exit_qualification & 8) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 4070 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4071 | ++vcpu->stat.io_exits; |
| 4072 | |
| 4073 | if (string || in) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4074 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4075 | |
| 4076 | port = exit_qualification >> 16; |
| 4077 | size = (exit_qualification & 7) + 1; |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 4078 | skip_emulated_instruction(vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4079 | |
| 4080 | return kvm_fast_pio_out(vcpu, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4081 | } |
| 4082 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4083 | static void |
| 4084 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 4085 | { |
| 4086 | /* |
| 4087 | * Patch in the VMCALL instruction: |
| 4088 | */ |
| 4089 | hypercall[0] = 0x0f; |
| 4090 | hypercall[1] = 0x01; |
| 4091 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4092 | } |
| 4093 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4094 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4095 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4096 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4097 | int cr; |
| 4098 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 4099 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4100 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4101 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4102 | cr = exit_qualification & 15; |
| 4103 | reg = (exit_qualification >> 8) & 15; |
| 4104 | switch ((exit_qualification >> 4) & 3) { |
| 4105 | case 0: /* mov to cr */ |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4106 | val = kvm_register_read(vcpu, reg); |
| 4107 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4108 | switch (cr) { |
| 4109 | case 0: |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 4110 | err = kvm_set_cr0(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4111 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4112 | return 1; |
| 4113 | case 3: |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 4114 | err = kvm_set_cr3(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4115 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4116 | return 1; |
| 4117 | case 4: |
Avi Kivity | a83b29c | 2010-06-10 17:02:15 +0300 | [diff] [blame] | 4118 | err = kvm_set_cr4(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4119 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4120 | return 1; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4121 | case 8: { |
| 4122 | u8 cr8_prev = kvm_get_cr8(vcpu); |
| 4123 | u8 cr8 = kvm_register_read(vcpu, reg); |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 4124 | err = kvm_set_cr8(vcpu, cr8); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 4125 | kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4126 | if (irqchip_in_kernel(vcpu->kvm)) |
| 4127 | return 1; |
| 4128 | if (cr8_prev <= cr8) |
| 4129 | return 1; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4130 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4131 | return 0; |
| 4132 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4133 | }; |
| 4134 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4135 | case 2: /* clts */ |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 4136 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4137 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4138 | skip_emulated_instruction(vcpu); |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 4139 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4140 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4141 | case 1: /*mov from cr*/ |
| 4142 | switch (cr) { |
| 4143 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 4144 | val = kvm_read_cr3(vcpu); |
| 4145 | kvm_register_write(vcpu, reg, val); |
| 4146 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4147 | skip_emulated_instruction(vcpu); |
| 4148 | return 1; |
| 4149 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4150 | val = kvm_get_cr8(vcpu); |
| 4151 | kvm_register_write(vcpu, reg, val); |
| 4152 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4153 | skip_emulated_instruction(vcpu); |
| 4154 | return 1; |
| 4155 | } |
| 4156 | break; |
| 4157 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 4158 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4159 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 4160 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4161 | |
| 4162 | skip_emulated_instruction(vcpu); |
| 4163 | return 1; |
| 4164 | default: |
| 4165 | break; |
| 4166 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4167 | vcpu->run->exit_reason = 0; |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 4168 | pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4169 | (int)(exit_qualification >> 4) & 3, cr); |
| 4170 | return 0; |
| 4171 | } |
| 4172 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4173 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4174 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4175 | unsigned long exit_qualification; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4176 | int dr, reg; |
| 4177 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 4178 | /* 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] | 4179 | if (!kvm_require_cpl(vcpu, 0)) |
| 4180 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4181 | dr = vmcs_readl(GUEST_DR7); |
| 4182 | if (dr & DR7_GD) { |
| 4183 | /* |
| 4184 | * As the vm-exit takes precedence over the debug trap, we |
| 4185 | * need to emulate the latter, either for the host or the |
| 4186 | * guest debugging itself. |
| 4187 | */ |
| 4188 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4189 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
| 4190 | vcpu->run->debug.arch.dr7 = dr; |
| 4191 | vcpu->run->debug.arch.pc = |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4192 | vmcs_readl(GUEST_CS_BASE) + |
| 4193 | vmcs_readl(GUEST_RIP); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4194 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 4195 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4196 | return 0; |
| 4197 | } else { |
| 4198 | vcpu->arch.dr7 &= ~DR7_GD; |
| 4199 | vcpu->arch.dr6 |= DR6_BD; |
| 4200 | vmcs_writel(GUEST_DR7, vcpu->arch.dr7); |
| 4201 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 4202 | return 1; |
| 4203 | } |
| 4204 | } |
| 4205 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4206 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4207 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 4208 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 4209 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4210 | unsigned long val; |
| 4211 | if (!kvm_get_dr(vcpu, dr, &val)) |
| 4212 | kvm_register_write(vcpu, reg, val); |
| 4213 | } else |
| 4214 | kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4215 | skip_emulated_instruction(vcpu); |
| 4216 | return 1; |
| 4217 | } |
| 4218 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4219 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 4220 | { |
| 4221 | vmcs_writel(GUEST_DR7, val); |
| 4222 | } |
| 4223 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4224 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4225 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 4226 | kvm_emulate_cpuid(vcpu); |
| 4227 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4228 | } |
| 4229 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4230 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4231 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4232 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4233 | u64 data; |
| 4234 | |
| 4235 | if (vmx_get_msr(vcpu, ecx, &data)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4236 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4237 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4238 | return 1; |
| 4239 | } |
| 4240 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4241 | trace_kvm_msr_read(ecx, data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4242 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4243 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4244 | vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u; |
| 4245 | vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4246 | skip_emulated_instruction(vcpu); |
| 4247 | return 1; |
| 4248 | } |
| 4249 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4250 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4251 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4252 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 4253 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 4254 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4255 | |
| 4256 | if (vmx_set_msr(vcpu, ecx, data) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4257 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4258 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4259 | return 1; |
| 4260 | } |
| 4261 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4262 | trace_kvm_msr_write(ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4263 | skip_emulated_instruction(vcpu); |
| 4264 | return 1; |
| 4265 | } |
| 4266 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4267 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4268 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4269 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4270 | return 1; |
| 4271 | } |
| 4272 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4273 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4274 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4275 | u32 cpu_based_vm_exec_control; |
| 4276 | |
| 4277 | /* clear pending irq */ |
| 4278 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4279 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 4280 | 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] | 4281 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4282 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4283 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 4284 | ++vcpu->stat.irq_window_exits; |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4285 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4286 | /* |
| 4287 | * If the user space waits to inject interrupts, exit as soon as |
| 4288 | * possible |
| 4289 | */ |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 4290 | if (!irqchip_in_kernel(vcpu->kvm) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4291 | vcpu->run->request_interrupt_window && |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 4292 | !kvm_cpu_has_interrupt(vcpu)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4293 | vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 4294 | return 0; |
| 4295 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4296 | return 1; |
| 4297 | } |
| 4298 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4299 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4300 | { |
| 4301 | skip_emulated_instruction(vcpu); |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 4302 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4303 | } |
| 4304 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4305 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4306 | { |
Dor Laor | 510043d | 2007-02-19 18:25:43 +0200 | [diff] [blame] | 4307 | skip_emulated_instruction(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4308 | kvm_emulate_hypercall(vcpu); |
| 4309 | return 1; |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4310 | } |
| 4311 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4312 | static int handle_vmx_insn(struct kvm_vcpu *vcpu) |
Avi Kivity | e3c7cb6 | 2009-06-16 14:19:52 +0300 | [diff] [blame] | 4313 | { |
| 4314 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4315 | return 1; |
| 4316 | } |
| 4317 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 4318 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 4319 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4320 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 4321 | } |
| 4322 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4323 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4324 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4325 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4326 | |
| 4327 | kvm_mmu_invlpg(vcpu, exit_qualification); |
| 4328 | skip_emulated_instruction(vcpu); |
| 4329 | return 1; |
| 4330 | } |
| 4331 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4332 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 4333 | { |
| 4334 | skip_emulated_instruction(vcpu); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 4335 | kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 4336 | return 1; |
| 4337 | } |
| 4338 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 4339 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 4340 | { |
| 4341 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 4342 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4343 | |
| 4344 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
| 4345 | skip_emulated_instruction(vcpu); |
| 4346 | return 1; |
| 4347 | } |
| 4348 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4349 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4350 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4351 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4352 | } |
| 4353 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4354 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4355 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 4356 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4357 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 4358 | bool has_error_code = false; |
| 4359 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4360 | u16 tss_selector; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4361 | int reason, type, idt_v; |
| 4362 | |
| 4363 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
| 4364 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4365 | |
| 4366 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4367 | |
| 4368 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4369 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 4370 | switch (type) { |
| 4371 | case INTR_TYPE_NMI_INTR: |
| 4372 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 4373 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4374 | break; |
| 4375 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4376 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4377 | kvm_clear_interrupt_queue(vcpu); |
| 4378 | break; |
| 4379 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 4380 | if (vmx->idt_vectoring_info & |
| 4381 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 4382 | has_error_code = true; |
| 4383 | error_code = |
| 4384 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 4385 | } |
| 4386 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4387 | case INTR_TYPE_SOFT_EXCEPTION: |
| 4388 | kvm_clear_exception_queue(vcpu); |
| 4389 | break; |
| 4390 | default: |
| 4391 | break; |
| 4392 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 4393 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4394 | tss_selector = exit_qualification; |
| 4395 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4396 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 4397 | type != INTR_TYPE_EXT_INTR && |
| 4398 | type != INTR_TYPE_NMI_INTR)) |
| 4399 | skip_emulated_instruction(vcpu); |
| 4400 | |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 4401 | if (kvm_task_switch(vcpu, tss_selector, reason, |
| 4402 | has_error_code, error_code) == EMULATE_FAIL) { |
| 4403 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4404 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 4405 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4406 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 4407 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4408 | |
| 4409 | /* clear all local breakpoint enable flags */ |
| 4410 | vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55); |
| 4411 | |
| 4412 | /* |
| 4413 | * TODO: What about debug traps on tss switch? |
| 4414 | * Are we supposed to inject them and update dr6? |
| 4415 | */ |
| 4416 | |
| 4417 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4418 | } |
| 4419 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4420 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4421 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4422 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4423 | gpa_t gpa; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4424 | int gla_validity; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4425 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4426 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4427 | |
| 4428 | if (exit_qualification & (1 << 6)) { |
| 4429 | printk(KERN_ERR "EPT: GPA exceeds GAW!\n"); |
Jan Kiszka | 7f582ab | 2009-07-22 23:53:01 +0200 | [diff] [blame] | 4430 | return -EINVAL; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4431 | } |
| 4432 | |
| 4433 | gla_validity = (exit_qualification >> 7) & 0x3; |
| 4434 | if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) { |
| 4435 | printk(KERN_ERR "EPT: Handling EPT violation failed!\n"); |
| 4436 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 4437 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4438 | vmcs_readl(GUEST_LINEAR_ADDRESS)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4439 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 4440 | (long unsigned int)exit_qualification); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4441 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 4442 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION; |
Avi Kivity | 596ae89 | 2009-06-03 14:12:10 +0300 | [diff] [blame] | 4443 | return 0; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4447 | trace_kvm_page_fault(gpa, exit_qualification); |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4448 | return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4449 | } |
| 4450 | |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4451 | static u64 ept_rsvd_mask(u64 spte, int level) |
| 4452 | { |
| 4453 | int i; |
| 4454 | u64 mask = 0; |
| 4455 | |
| 4456 | for (i = 51; i > boot_cpu_data.x86_phys_bits; i--) |
| 4457 | mask |= (1ULL << i); |
| 4458 | |
| 4459 | if (level > 2) |
| 4460 | /* bits 7:3 reserved */ |
| 4461 | mask |= 0xf8; |
| 4462 | else if (level == 2) { |
| 4463 | if (spte & (1ULL << 7)) |
| 4464 | /* 2MB ref, bits 20:12 reserved */ |
| 4465 | mask |= 0x1ff000; |
| 4466 | else |
| 4467 | /* bits 6:3 reserved */ |
| 4468 | mask |= 0x78; |
| 4469 | } |
| 4470 | |
| 4471 | return mask; |
| 4472 | } |
| 4473 | |
| 4474 | static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte, |
| 4475 | int level) |
| 4476 | { |
| 4477 | printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level); |
| 4478 | |
| 4479 | /* 010b (write-only) */ |
| 4480 | WARN_ON((spte & 0x7) == 0x2); |
| 4481 | |
| 4482 | /* 110b (write/execute) */ |
| 4483 | WARN_ON((spte & 0x7) == 0x6); |
| 4484 | |
| 4485 | /* 100b (execute-only) and value not supported by logical processor */ |
| 4486 | if (!cpu_has_vmx_ept_execute_only()) |
| 4487 | WARN_ON((spte & 0x7) == 0x4); |
| 4488 | |
| 4489 | /* not 000b */ |
| 4490 | if ((spte & 0x7)) { |
| 4491 | u64 rsvd_bits = spte & ept_rsvd_mask(spte, level); |
| 4492 | |
| 4493 | if (rsvd_bits != 0) { |
| 4494 | printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n", |
| 4495 | __func__, rsvd_bits); |
| 4496 | WARN_ON(1); |
| 4497 | } |
| 4498 | |
| 4499 | if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) { |
| 4500 | u64 ept_mem_type = (spte & 0x38) >> 3; |
| 4501 | |
| 4502 | if (ept_mem_type == 2 || ept_mem_type == 3 || |
| 4503 | ept_mem_type == 7) { |
| 4504 | printk(KERN_ERR "%s: ept_mem_type=0x%llx\n", |
| 4505 | __func__, ept_mem_type); |
| 4506 | WARN_ON(1); |
| 4507 | } |
| 4508 | } |
| 4509 | } |
| 4510 | } |
| 4511 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4512 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4513 | { |
| 4514 | u64 sptes[4]; |
| 4515 | int nr_sptes, i; |
| 4516 | gpa_t gpa; |
| 4517 | |
| 4518 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 4519 | |
| 4520 | printk(KERN_ERR "EPT: Misconfiguration.\n"); |
| 4521 | printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa); |
| 4522 | |
| 4523 | nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes); |
| 4524 | |
| 4525 | for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i) |
| 4526 | ept_misconfig_inspect_spte(vcpu, sptes[i-1], i); |
| 4527 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4528 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 4529 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4530 | |
| 4531 | return 0; |
| 4532 | } |
| 4533 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4534 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4535 | { |
| 4536 | u32 cpu_based_vm_exec_control; |
| 4537 | |
| 4538 | /* clear pending NMI */ |
| 4539 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4540 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 4541 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 4542 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4543 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4544 | |
| 4545 | return 1; |
| 4546 | } |
| 4547 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4548 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4549 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 4550 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4551 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4552 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 4553 | u32 cpu_exec_ctrl; |
| 4554 | bool intr_window_requested; |
| 4555 | |
| 4556 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4557 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4558 | |
| 4559 | while (!guest_state_valid(vcpu)) { |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 4560 | if (intr_window_requested |
| 4561 | && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF)) |
| 4562 | return handle_interrupt_window(&vmx->vcpu); |
| 4563 | |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4564 | err = emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4565 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4566 | if (err == EMULATE_DO_MMIO) { |
| 4567 | ret = 0; |
| 4568 | goto out; |
| 4569 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 4570 | |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 4571 | if (err != EMULATE_DONE) |
| 4572 | return 0; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4573 | |
| 4574 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4575 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4576 | if (need_resched()) |
| 4577 | schedule(); |
| 4578 | } |
| 4579 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4580 | vmx->emulation_required = 0; |
| 4581 | out: |
| 4582 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4583 | } |
| 4584 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4585 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4586 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 4587 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 4588 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 4589 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4590 | { |
| 4591 | skip_emulated_instruction(vcpu); |
| 4592 | kvm_vcpu_on_spin(vcpu); |
| 4593 | |
| 4594 | return 1; |
| 4595 | } |
| 4596 | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 4597 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 4598 | { |
| 4599 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4600 | return 1; |
| 4601 | } |
| 4602 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4603 | /* |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 4604 | * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12. |
| 4605 | * We could reuse a single VMCS for all the L2 guests, but we also want the |
| 4606 | * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this |
| 4607 | * allows keeping them loaded on the processor, and in the future will allow |
| 4608 | * optimizations where prepare_vmcs02 doesn't need to set all the fields on |
| 4609 | * every entry if they never change. |
| 4610 | * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE |
| 4611 | * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first. |
| 4612 | * |
| 4613 | * The following functions allocate and free a vmcs02 in this pool. |
| 4614 | */ |
| 4615 | |
| 4616 | /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */ |
| 4617 | static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx) |
| 4618 | { |
| 4619 | struct vmcs02_list *item; |
| 4620 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 4621 | if (item->vmptr == vmx->nested.current_vmptr) { |
| 4622 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 4623 | return &item->vmcs02; |
| 4624 | } |
| 4625 | |
| 4626 | if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) { |
| 4627 | /* Recycle the least recently used VMCS. */ |
| 4628 | item = list_entry(vmx->nested.vmcs02_pool.prev, |
| 4629 | struct vmcs02_list, list); |
| 4630 | item->vmptr = vmx->nested.current_vmptr; |
| 4631 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 4632 | return &item->vmcs02; |
| 4633 | } |
| 4634 | |
| 4635 | /* Create a new VMCS */ |
| 4636 | item = (struct vmcs02_list *) |
| 4637 | kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL); |
| 4638 | if (!item) |
| 4639 | return NULL; |
| 4640 | item->vmcs02.vmcs = alloc_vmcs(); |
| 4641 | if (!item->vmcs02.vmcs) { |
| 4642 | kfree(item); |
| 4643 | return NULL; |
| 4644 | } |
| 4645 | loaded_vmcs_init(&item->vmcs02); |
| 4646 | item->vmptr = vmx->nested.current_vmptr; |
| 4647 | list_add(&(item->list), &(vmx->nested.vmcs02_pool)); |
| 4648 | vmx->nested.vmcs02_num++; |
| 4649 | return &item->vmcs02; |
| 4650 | } |
| 4651 | |
| 4652 | /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */ |
| 4653 | static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 4654 | { |
| 4655 | struct vmcs02_list *item; |
| 4656 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 4657 | if (item->vmptr == vmptr) { |
| 4658 | free_loaded_vmcs(&item->vmcs02); |
| 4659 | list_del(&item->list); |
| 4660 | kfree(item); |
| 4661 | vmx->nested.vmcs02_num--; |
| 4662 | return; |
| 4663 | } |
| 4664 | } |
| 4665 | |
| 4666 | /* |
| 4667 | * Free all VMCSs saved for this vcpu, except the one pointed by |
| 4668 | * vmx->loaded_vmcs. These include the VMCSs in vmcs02_pool (except the one |
| 4669 | * currently used, if running L2), and vmcs01 when running L2. |
| 4670 | */ |
| 4671 | static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx) |
| 4672 | { |
| 4673 | struct vmcs02_list *item, *n; |
| 4674 | list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) { |
| 4675 | if (vmx->loaded_vmcs != &item->vmcs02) |
| 4676 | free_loaded_vmcs(&item->vmcs02); |
| 4677 | list_del(&item->list); |
| 4678 | kfree(item); |
| 4679 | } |
| 4680 | vmx->nested.vmcs02_num = 0; |
| 4681 | |
| 4682 | if (vmx->loaded_vmcs != &vmx->vmcs01) |
| 4683 | free_loaded_vmcs(&vmx->vmcs01); |
| 4684 | } |
| 4685 | |
| 4686 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 4687 | * Emulate the VMXON instruction. |
| 4688 | * Currently, we just remember that VMX is active, and do not save or even |
| 4689 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 4690 | * do not currently need to store anything in that guest-allocated memory |
| 4691 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 4692 | * argument is different from the VMXON pointer (which the spec says they do). |
| 4693 | */ |
| 4694 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 4695 | { |
| 4696 | struct kvm_segment cs; |
| 4697 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4698 | |
| 4699 | /* The Intel VMX Instruction Reference lists a bunch of bits that |
| 4700 | * are prerequisite to running VMXON, most notably cr4.VMXE must be |
| 4701 | * set to 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 4702 | * Otherwise, we should fail with #UD. We test these now: |
| 4703 | */ |
| 4704 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) || |
| 4705 | !kvm_read_cr0_bits(vcpu, X86_CR0_PE) || |
| 4706 | (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
| 4707 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4708 | return 1; |
| 4709 | } |
| 4710 | |
| 4711 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 4712 | if (is_long_mode(vcpu) && !cs.l) { |
| 4713 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4714 | return 1; |
| 4715 | } |
| 4716 | |
| 4717 | if (vmx_get_cpl(vcpu)) { |
| 4718 | kvm_inject_gp(vcpu, 0); |
| 4719 | return 1; |
| 4720 | } |
| 4721 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 4722 | INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool)); |
| 4723 | vmx->nested.vmcs02_num = 0; |
| 4724 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 4725 | vmx->nested.vmxon = true; |
| 4726 | |
| 4727 | skip_emulated_instruction(vcpu); |
| 4728 | return 1; |
| 4729 | } |
| 4730 | |
| 4731 | /* |
| 4732 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 4733 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 4734 | * slightly different). It also specifies what exception to inject otherwise. |
| 4735 | */ |
| 4736 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 4737 | { |
| 4738 | struct kvm_segment cs; |
| 4739 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4740 | |
| 4741 | if (!vmx->nested.vmxon) { |
| 4742 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4743 | return 0; |
| 4744 | } |
| 4745 | |
| 4746 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 4747 | if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) || |
| 4748 | (is_long_mode(vcpu) && !cs.l)) { |
| 4749 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4750 | return 0; |
| 4751 | } |
| 4752 | |
| 4753 | if (vmx_get_cpl(vcpu)) { |
| 4754 | kvm_inject_gp(vcpu, 0); |
| 4755 | return 0; |
| 4756 | } |
| 4757 | |
| 4758 | return 1; |
| 4759 | } |
| 4760 | |
| 4761 | /* |
| 4762 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 4763 | * just stops using VMX. |
| 4764 | */ |
| 4765 | static void free_nested(struct vcpu_vmx *vmx) |
| 4766 | { |
| 4767 | if (!vmx->nested.vmxon) |
| 4768 | return; |
| 4769 | vmx->nested.vmxon = false; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 4770 | if (vmx->nested.current_vmptr != -1ull) { |
| 4771 | kunmap(vmx->nested.current_vmcs12_page); |
| 4772 | nested_release_page(vmx->nested.current_vmcs12_page); |
| 4773 | vmx->nested.current_vmptr = -1ull; |
| 4774 | vmx->nested.current_vmcs12 = NULL; |
| 4775 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 4776 | /* Unpin physical memory we referred to in current vmcs02 */ |
| 4777 | if (vmx->nested.apic_access_page) { |
| 4778 | nested_release_page(vmx->nested.apic_access_page); |
| 4779 | vmx->nested.apic_access_page = 0; |
| 4780 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 4781 | |
| 4782 | nested_free_all_saved_vmcss(vmx); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | /* Emulate the VMXOFF instruction */ |
| 4786 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 4787 | { |
| 4788 | if (!nested_vmx_check_permission(vcpu)) |
| 4789 | return 1; |
| 4790 | free_nested(to_vmx(vcpu)); |
| 4791 | skip_emulated_instruction(vcpu); |
| 4792 | return 1; |
| 4793 | } |
| 4794 | |
| 4795 | /* |
Nadav Har'El | 064aea7 | 2011-05-25 23:04:56 +0300 | [diff] [blame] | 4796 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 4797 | * exit caused by such an instruction (run by a guest hypervisor). |
| 4798 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 4799 | * #UD or #GP. |
| 4800 | */ |
| 4801 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 4802 | unsigned long exit_qualification, |
| 4803 | u32 vmx_instruction_info, gva_t *ret) |
| 4804 | { |
| 4805 | /* |
| 4806 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 4807 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 4808 | * addressing components of the operand. Only the displacement part |
| 4809 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 4810 | * For how an actual address is calculated from all these components, |
| 4811 | * refer to Vol. 1, "Operand Addressing". |
| 4812 | */ |
| 4813 | int scaling = vmx_instruction_info & 3; |
| 4814 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 4815 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 4816 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 4817 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 4818 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 4819 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 4820 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 4821 | |
| 4822 | if (is_reg) { |
| 4823 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4824 | return 1; |
| 4825 | } |
| 4826 | |
| 4827 | /* Addr = segment_base + offset */ |
| 4828 | /* offset = base + [index * scale] + displacement */ |
| 4829 | *ret = vmx_get_segment_base(vcpu, seg_reg); |
| 4830 | if (base_is_valid) |
| 4831 | *ret += kvm_register_read(vcpu, base_reg); |
| 4832 | if (index_is_valid) |
| 4833 | *ret += kvm_register_read(vcpu, index_reg)<<scaling; |
| 4834 | *ret += exit_qualification; /* holds the displacement */ |
| 4835 | |
| 4836 | if (addr_size == 1) /* 32 bit */ |
| 4837 | *ret &= 0xffffffff; |
| 4838 | |
| 4839 | /* |
| 4840 | * TODO: throw #GP (and return 1) in various cases that the VM* |
| 4841 | * instructions require it - e.g., offset beyond segment limit, |
| 4842 | * unusable or unreadable/unwritable segment, non-canonical 64-bit |
| 4843 | * address, and so on. Currently these are not checked. |
| 4844 | */ |
| 4845 | return 0; |
| 4846 | } |
| 4847 | |
| 4848 | /* |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 4849 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 4850 | * set the success or error code of an emulated VMX instruction, as specified |
| 4851 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 4852 | */ |
| 4853 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 4854 | { |
| 4855 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 4856 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 4857 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 4858 | } |
| 4859 | |
| 4860 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 4861 | { |
| 4862 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 4863 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 4864 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 4865 | | X86_EFLAGS_CF); |
| 4866 | } |
| 4867 | |
| 4868 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
| 4869 | u32 vm_instruction_error) |
| 4870 | { |
| 4871 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 4872 | /* |
| 4873 | * failValid writes the error number to the current VMCS, which |
| 4874 | * can't be done there isn't a current VMCS. |
| 4875 | */ |
| 4876 | nested_vmx_failInvalid(vcpu); |
| 4877 | return; |
| 4878 | } |
| 4879 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 4880 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 4881 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 4882 | | X86_EFLAGS_ZF); |
| 4883 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 4884 | } |
| 4885 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 4886 | /* Emulate the VMCLEAR instruction */ |
| 4887 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 4888 | { |
| 4889 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4890 | gva_t gva; |
| 4891 | gpa_t vmptr; |
| 4892 | struct vmcs12 *vmcs12; |
| 4893 | struct page *page; |
| 4894 | struct x86_exception e; |
| 4895 | |
| 4896 | if (!nested_vmx_check_permission(vcpu)) |
| 4897 | return 1; |
| 4898 | |
| 4899 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 4900 | vmcs_read32(VMX_INSTRUCTION_INFO), &gva)) |
| 4901 | return 1; |
| 4902 | |
| 4903 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr, |
| 4904 | sizeof(vmptr), &e)) { |
| 4905 | kvm_inject_page_fault(vcpu, &e); |
| 4906 | return 1; |
| 4907 | } |
| 4908 | |
| 4909 | if (!IS_ALIGNED(vmptr, PAGE_SIZE)) { |
| 4910 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 4911 | skip_emulated_instruction(vcpu); |
| 4912 | return 1; |
| 4913 | } |
| 4914 | |
| 4915 | if (vmptr == vmx->nested.current_vmptr) { |
| 4916 | kunmap(vmx->nested.current_vmcs12_page); |
| 4917 | nested_release_page(vmx->nested.current_vmcs12_page); |
| 4918 | vmx->nested.current_vmptr = -1ull; |
| 4919 | vmx->nested.current_vmcs12 = NULL; |
| 4920 | } |
| 4921 | |
| 4922 | page = nested_get_page(vcpu, vmptr); |
| 4923 | if (page == NULL) { |
| 4924 | /* |
| 4925 | * For accurate processor emulation, VMCLEAR beyond available |
| 4926 | * physical memory should do nothing at all. However, it is |
| 4927 | * possible that a nested vmx bug, not a guest hypervisor bug, |
| 4928 | * resulted in this case, so let's shut down before doing any |
| 4929 | * more damage: |
| 4930 | */ |
| 4931 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 4932 | return 1; |
| 4933 | } |
| 4934 | vmcs12 = kmap(page); |
| 4935 | vmcs12->launch_state = 0; |
| 4936 | kunmap(page); |
| 4937 | nested_release_page(page); |
| 4938 | |
| 4939 | nested_free_vmcs02(vmx, vmptr); |
| 4940 | |
| 4941 | skip_emulated_instruction(vcpu); |
| 4942 | nested_vmx_succeed(vcpu); |
| 4943 | return 1; |
| 4944 | } |
| 4945 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame^] | 4946 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 4947 | |
| 4948 | /* Emulate the VMLAUNCH instruction */ |
| 4949 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 4950 | { |
| 4951 | return nested_vmx_run(vcpu, true); |
| 4952 | } |
| 4953 | |
| 4954 | /* Emulate the VMRESUME instruction */ |
| 4955 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 4956 | { |
| 4957 | |
| 4958 | return nested_vmx_run(vcpu, false); |
| 4959 | } |
| 4960 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 4961 | enum vmcs_field_type { |
| 4962 | VMCS_FIELD_TYPE_U16 = 0, |
| 4963 | VMCS_FIELD_TYPE_U64 = 1, |
| 4964 | VMCS_FIELD_TYPE_U32 = 2, |
| 4965 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 |
| 4966 | }; |
| 4967 | |
| 4968 | static inline int vmcs_field_type(unsigned long field) |
| 4969 | { |
| 4970 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
| 4971 | return VMCS_FIELD_TYPE_U32; |
| 4972 | return (field >> 13) & 0x3 ; |
| 4973 | } |
| 4974 | |
| 4975 | static inline int vmcs_field_readonly(unsigned long field) |
| 4976 | { |
| 4977 | return (((field >> 10) & 0x3) == 1); |
| 4978 | } |
| 4979 | |
| 4980 | /* |
| 4981 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 4982 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 4983 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 4984 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 4985 | * 64-bit fields are to be returned). |
| 4986 | */ |
| 4987 | static inline bool vmcs12_read_any(struct kvm_vcpu *vcpu, |
| 4988 | unsigned long field, u64 *ret) |
| 4989 | { |
| 4990 | short offset = vmcs_field_to_offset(field); |
| 4991 | char *p; |
| 4992 | |
| 4993 | if (offset < 0) |
| 4994 | return 0; |
| 4995 | |
| 4996 | p = ((char *)(get_vmcs12(vcpu))) + offset; |
| 4997 | |
| 4998 | switch (vmcs_field_type(field)) { |
| 4999 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 5000 | *ret = *((natural_width *)p); |
| 5001 | return 1; |
| 5002 | case VMCS_FIELD_TYPE_U16: |
| 5003 | *ret = *((u16 *)p); |
| 5004 | return 1; |
| 5005 | case VMCS_FIELD_TYPE_U32: |
| 5006 | *ret = *((u32 *)p); |
| 5007 | return 1; |
| 5008 | case VMCS_FIELD_TYPE_U64: |
| 5009 | *ret = *((u64 *)p); |
| 5010 | return 1; |
| 5011 | default: |
| 5012 | return 0; /* can never happen. */ |
| 5013 | } |
| 5014 | } |
| 5015 | |
| 5016 | /* |
| 5017 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 5018 | * used before) all generate the same failure when it is missing. |
| 5019 | */ |
| 5020 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 5021 | { |
| 5022 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5023 | if (vmx->nested.current_vmptr == -1ull) { |
| 5024 | nested_vmx_failInvalid(vcpu); |
| 5025 | skip_emulated_instruction(vcpu); |
| 5026 | return 0; |
| 5027 | } |
| 5028 | return 1; |
| 5029 | } |
| 5030 | |
| 5031 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 5032 | { |
| 5033 | unsigned long field; |
| 5034 | u64 field_value; |
| 5035 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5036 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5037 | gva_t gva = 0; |
| 5038 | |
| 5039 | if (!nested_vmx_check_permission(vcpu) || |
| 5040 | !nested_vmx_check_vmcs12(vcpu)) |
| 5041 | return 1; |
| 5042 | |
| 5043 | /* Decode instruction info and find the field to read */ |
| 5044 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5045 | /* Read the field, zero-extended to a u64 field_value */ |
| 5046 | if (!vmcs12_read_any(vcpu, field, &field_value)) { |
| 5047 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 5048 | skip_emulated_instruction(vcpu); |
| 5049 | return 1; |
| 5050 | } |
| 5051 | /* |
| 5052 | * Now copy part of this value to register or memory, as requested. |
| 5053 | * Note that the number of bits actually copied is 32 or 64 depending |
| 5054 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 5055 | */ |
| 5056 | if (vmx_instruction_info & (1u << 10)) { |
| 5057 | kvm_register_write(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
| 5058 | field_value); |
| 5059 | } else { |
| 5060 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 5061 | vmx_instruction_info, &gva)) |
| 5062 | return 1; |
| 5063 | /* _system ok, as nested_vmx_check_permission verified cpl=0 */ |
| 5064 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
| 5065 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); |
| 5066 | } |
| 5067 | |
| 5068 | nested_vmx_succeed(vcpu); |
| 5069 | skip_emulated_instruction(vcpu); |
| 5070 | return 1; |
| 5071 | } |
| 5072 | |
| 5073 | |
| 5074 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 5075 | { |
| 5076 | unsigned long field; |
| 5077 | gva_t gva; |
| 5078 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5079 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5080 | char *p; |
| 5081 | short offset; |
| 5082 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 5083 | * mode, and eventually we need to write that into a field of several |
| 5084 | * possible lengths. The code below first zero-extends the value to 64 |
| 5085 | * bit (field_value), and then copies only the approriate number of |
| 5086 | * bits into the vmcs12 field. |
| 5087 | */ |
| 5088 | u64 field_value = 0; |
| 5089 | struct x86_exception e; |
| 5090 | |
| 5091 | if (!nested_vmx_check_permission(vcpu) || |
| 5092 | !nested_vmx_check_vmcs12(vcpu)) |
| 5093 | return 1; |
| 5094 | |
| 5095 | if (vmx_instruction_info & (1u << 10)) |
| 5096 | field_value = kvm_register_read(vcpu, |
| 5097 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 5098 | else { |
| 5099 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 5100 | vmx_instruction_info, &gva)) |
| 5101 | return 1; |
| 5102 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, |
| 5103 | &field_value, (is_long_mode(vcpu) ? 8 : 4), &e)) { |
| 5104 | kvm_inject_page_fault(vcpu, &e); |
| 5105 | return 1; |
| 5106 | } |
| 5107 | } |
| 5108 | |
| 5109 | |
| 5110 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 5111 | if (vmcs_field_readonly(field)) { |
| 5112 | nested_vmx_failValid(vcpu, |
| 5113 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
| 5114 | skip_emulated_instruction(vcpu); |
| 5115 | return 1; |
| 5116 | } |
| 5117 | |
| 5118 | offset = vmcs_field_to_offset(field); |
| 5119 | if (offset < 0) { |
| 5120 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 5121 | skip_emulated_instruction(vcpu); |
| 5122 | return 1; |
| 5123 | } |
| 5124 | p = ((char *) get_vmcs12(vcpu)) + offset; |
| 5125 | |
| 5126 | switch (vmcs_field_type(field)) { |
| 5127 | case VMCS_FIELD_TYPE_U16: |
| 5128 | *(u16 *)p = field_value; |
| 5129 | break; |
| 5130 | case VMCS_FIELD_TYPE_U32: |
| 5131 | *(u32 *)p = field_value; |
| 5132 | break; |
| 5133 | case VMCS_FIELD_TYPE_U64: |
| 5134 | *(u64 *)p = field_value; |
| 5135 | break; |
| 5136 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 5137 | *(natural_width *)p = field_value; |
| 5138 | break; |
| 5139 | default: |
| 5140 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 5141 | skip_emulated_instruction(vcpu); |
| 5142 | return 1; |
| 5143 | } |
| 5144 | |
| 5145 | nested_vmx_succeed(vcpu); |
| 5146 | skip_emulated_instruction(vcpu); |
| 5147 | return 1; |
| 5148 | } |
| 5149 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 5150 | /* Emulate the VMPTRLD instruction */ |
| 5151 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 5152 | { |
| 5153 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5154 | gva_t gva; |
| 5155 | gpa_t vmptr; |
| 5156 | struct x86_exception e; |
| 5157 | |
| 5158 | if (!nested_vmx_check_permission(vcpu)) |
| 5159 | return 1; |
| 5160 | |
| 5161 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 5162 | vmcs_read32(VMX_INSTRUCTION_INFO), &gva)) |
| 5163 | return 1; |
| 5164 | |
| 5165 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr, |
| 5166 | sizeof(vmptr), &e)) { |
| 5167 | kvm_inject_page_fault(vcpu, &e); |
| 5168 | return 1; |
| 5169 | } |
| 5170 | |
| 5171 | if (!IS_ALIGNED(vmptr, PAGE_SIZE)) { |
| 5172 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 5173 | skip_emulated_instruction(vcpu); |
| 5174 | return 1; |
| 5175 | } |
| 5176 | |
| 5177 | if (vmx->nested.current_vmptr != vmptr) { |
| 5178 | struct vmcs12 *new_vmcs12; |
| 5179 | struct page *page; |
| 5180 | page = nested_get_page(vcpu, vmptr); |
| 5181 | if (page == NULL) { |
| 5182 | nested_vmx_failInvalid(vcpu); |
| 5183 | skip_emulated_instruction(vcpu); |
| 5184 | return 1; |
| 5185 | } |
| 5186 | new_vmcs12 = kmap(page); |
| 5187 | if (new_vmcs12->revision_id != VMCS12_REVISION) { |
| 5188 | kunmap(page); |
| 5189 | nested_release_page_clean(page); |
| 5190 | nested_vmx_failValid(vcpu, |
| 5191 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 5192 | skip_emulated_instruction(vcpu); |
| 5193 | return 1; |
| 5194 | } |
| 5195 | if (vmx->nested.current_vmptr != -1ull) { |
| 5196 | kunmap(vmx->nested.current_vmcs12_page); |
| 5197 | nested_release_page(vmx->nested.current_vmcs12_page); |
| 5198 | } |
| 5199 | |
| 5200 | vmx->nested.current_vmptr = vmptr; |
| 5201 | vmx->nested.current_vmcs12 = new_vmcs12; |
| 5202 | vmx->nested.current_vmcs12_page = page; |
| 5203 | } |
| 5204 | |
| 5205 | nested_vmx_succeed(vcpu); |
| 5206 | skip_emulated_instruction(vcpu); |
| 5207 | return 1; |
| 5208 | } |
| 5209 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 5210 | /* Emulate the VMPTRST instruction */ |
| 5211 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 5212 | { |
| 5213 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5214 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5215 | gva_t vmcs_gva; |
| 5216 | struct x86_exception e; |
| 5217 | |
| 5218 | if (!nested_vmx_check_permission(vcpu)) |
| 5219 | return 1; |
| 5220 | |
| 5221 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 5222 | vmx_instruction_info, &vmcs_gva)) |
| 5223 | return 1; |
| 5224 | /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */ |
| 5225 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
| 5226 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 5227 | sizeof(u64), &e)) { |
| 5228 | kvm_inject_page_fault(vcpu, &e); |
| 5229 | return 1; |
| 5230 | } |
| 5231 | nested_vmx_succeed(vcpu); |
| 5232 | skip_emulated_instruction(vcpu); |
| 5233 | return 1; |
| 5234 | } |
| 5235 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 5236 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5237 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 5238 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 5239 | * to be done to userspace and return 0. |
| 5240 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5241 | static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5242 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 5243 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5244 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5245 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5246 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5247 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 5248 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 5249 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 5250 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 5251 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 5252 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 5253 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 5254 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5255 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5256 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 5257 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame^] | 5258 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 5259 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 5260 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 5261 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame^] | 5262 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 5263 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 5264 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 5265 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5266 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 5267 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 5268 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 5269 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5270 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5271 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5272 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 5273 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5274 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 5275 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op, |
| 5276 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5277 | }; |
| 5278 | |
| 5279 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 5280 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5281 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 5282 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 5283 | { |
| 5284 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 5285 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 5286 | } |
| 5287 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5288 | /* |
| 5289 | * The guest has exited. See if we can fix it or if we need userspace |
| 5290 | * assistance. |
| 5291 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5292 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5293 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5294 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5295 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5296 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5297 | |
Avi Kivity | aa17911 | 2010-11-17 18:44:19 +0200 | [diff] [blame] | 5298 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 5299 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5300 | /* If guest state is invalid, start emulating */ |
| 5301 | if (vmx->emulation_required && emulate_invalid_guest_state) |
| 5302 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 5303 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 5304 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
| 5305 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 5306 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 5307 | = exit_reason; |
| 5308 | return 0; |
| 5309 | } |
| 5310 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5311 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5312 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 5313 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5314 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 5315 | return 0; |
| 5316 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5317 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 5318 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5319 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 5320 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
| 5321 | exit_reason != EXIT_REASON_TASK_SWITCH)) |
| 5322 | printk(KERN_WARNING "%s: unexpected, valid vectoring info " |
| 5323 | "(0x%x) and exit reason is 0x%x\n", |
| 5324 | __func__, vectoring_info, exit_reason); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5325 | |
| 5326 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) { |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 5327 | if (vmx_interrupt_allowed(vcpu)) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5328 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5329 | } else if (vmx->vnmi_blocked_time > 1000000000LL && |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 5330 | vcpu->arch.nmi_pending) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5331 | /* |
| 5332 | * This CPU don't support us in finding the end of an |
| 5333 | * NMI-blocked window if the guest runs with IRQs |
| 5334 | * disabled. So we pull the trigger after 1 s of |
| 5335 | * futile waiting, but inform the user about this. |
| 5336 | */ |
| 5337 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 5338 | "state on VCPU %d after 1 s timeout\n", |
| 5339 | __func__, vcpu->vcpu_id); |
| 5340 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5341 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5342 | } |
| 5343 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5344 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 5345 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5346 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5347 | else { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5348 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 5349 | vcpu->run->hw.hardware_exit_reason = exit_reason; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5350 | } |
| 5351 | return 0; |
| 5352 | } |
| 5353 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5354 | 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] | 5355 | { |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5356 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5357 | vmcs_write32(TPR_THRESHOLD, 0); |
| 5358 | return; |
| 5359 | } |
| 5360 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5361 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5362 | } |
| 5363 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5364 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5365 | { |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5366 | u32 exit_intr_info; |
| 5367 | |
| 5368 | if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 5369 | || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
| 5370 | return; |
| 5371 | |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 5372 | vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5373 | exit_intr_info = vmx->exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5374 | |
| 5375 | /* Handle machine checks before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5376 | if (is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5377 | kvm_machine_check(); |
| 5378 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5379 | /* We need to handle NMIs before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5380 | 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] | 5381 | (exit_intr_info & INTR_INFO_VALID_MASK)) { |
| 5382 | kvm_before_handle_nmi(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5383 | asm("int $2"); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 5384 | kvm_after_handle_nmi(&vmx->vcpu); |
| 5385 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5386 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5387 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5388 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 5389 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 5390 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5391 | bool unblock_nmi; |
| 5392 | u8 vector; |
| 5393 | bool idtv_info_valid; |
| 5394 | |
| 5395 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5396 | |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5397 | if (cpu_has_virtual_nmis()) { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 5398 | if (vmx->nmi_known_unmasked) |
| 5399 | return; |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 5400 | /* |
| 5401 | * Can't use vmx->exit_intr_info since we're not sure what |
| 5402 | * the exit reason is. |
| 5403 | */ |
| 5404 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5405 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 5406 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 5407 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 5408 | * SDM 3: 27.7.1.2 (September 2008) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5409 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 5410 | * a guest IRET fault. |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 5411 | * SDM 3: 23.2.2 (September 2008) |
| 5412 | * Bit 12 is undefined in any of the following cases: |
| 5413 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 5414 | * information field. |
| 5415 | * If the VM exit is due to a double fault. |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5416 | */ |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 5417 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 5418 | vector != DF_VECTOR && !idtv_info_valid) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5419 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5420 | GUEST_INTR_STATE_NMI); |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 5421 | else |
| 5422 | vmx->nmi_known_unmasked = |
| 5423 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 5424 | & GUEST_INTR_STATE_NMI); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5425 | } else if (unlikely(vmx->soft_vnmi_blocked)) |
| 5426 | vmx->vnmi_blocked_time += |
| 5427 | ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5428 | } |
| 5429 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 5430 | static void __vmx_complete_interrupts(struct vcpu_vmx *vmx, |
| 5431 | u32 idt_vectoring_info, |
| 5432 | int instr_len_field, |
| 5433 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5434 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5435 | u8 vector; |
| 5436 | int type; |
| 5437 | bool idtv_info_valid; |
| 5438 | |
| 5439 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 5440 | |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5441 | vmx->vcpu.arch.nmi_injected = false; |
| 5442 | kvm_clear_exception_queue(&vmx->vcpu); |
| 5443 | kvm_clear_interrupt_queue(&vmx->vcpu); |
| 5444 | |
| 5445 | if (!idtv_info_valid) |
| 5446 | return; |
| 5447 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5448 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 5449 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 5450 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 5451 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5452 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5453 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5454 | case INTR_TYPE_NMI_INTR: |
| 5455 | vmx->vcpu.arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 5456 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 5457 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5458 | * Clear bit "block by NMI" before VM entry if a NMI |
| 5459 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 5460 | */ |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 5461 | vmx_set_nmi_mask(&vmx->vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5462 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5463 | case INTR_TYPE_SOFT_EXCEPTION: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5464 | vmx->vcpu.arch.event_exit_inst_len = |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 5465 | vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5466 | /* fall through */ |
| 5467 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 5468 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 5469 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5470 | kvm_queue_exception_e(&vmx->vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 5471 | } else |
| 5472 | kvm_queue_exception(&vmx->vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5473 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5474 | case INTR_TYPE_SOFT_INTR: |
| 5475 | vmx->vcpu.arch.event_exit_inst_len = |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 5476 | vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5477 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5478 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5479 | kvm_queue_interrupt(&vmx->vcpu, vector, |
| 5480 | type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5481 | break; |
| 5482 | default: |
| 5483 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 5484 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5485 | } |
| 5486 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 5487 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 5488 | { |
| 5489 | __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info, |
| 5490 | VM_EXIT_INSTRUCTION_LEN, |
| 5491 | IDT_VECTORING_ERROR_CODE); |
| 5492 | } |
| 5493 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 5494 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 5495 | { |
| 5496 | __vmx_complete_interrupts(to_vmx(vcpu), |
| 5497 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 5498 | VM_ENTRY_INSTRUCTION_LEN, |
| 5499 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 5500 | |
| 5501 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 5502 | } |
| 5503 | |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 5504 | #ifdef CONFIG_X86_64 |
| 5505 | #define R "r" |
| 5506 | #define Q "q" |
| 5507 | #else |
| 5508 | #define R "e" |
| 5509 | #define Q "l" |
| 5510 | #endif |
| 5511 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 5512 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5513 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5514 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 5515 | |
| 5516 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
| 5517 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) |
| 5518 | vmx->entry_time = ktime_get(); |
| 5519 | |
| 5520 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 5521 | start emulation until we arrive back to a valid state */ |
| 5522 | if (vmx->emulation_required && emulate_invalid_guest_state) |
| 5523 | return; |
| 5524 | |
| 5525 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 5526 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 5527 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 5528 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 5529 | |
| 5530 | /* When single-stepping over STI and MOV SS, we must clear the |
| 5531 | * corresponding interruptibility bits in the guest state. Otherwise |
| 5532 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 5533 | * exceptions being set, but that's not correct for the guest debugging |
| 5534 | * case. */ |
| 5535 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 5536 | vmx_set_interrupt_shadow(vcpu, 0); |
| 5537 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 5538 | vmx->__launched = vmx->loaded_vmcs->launched; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 5539 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5540 | /* Store host registers */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 5541 | "push %%"R"dx; push %%"R"bp;" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 5542 | "push %%"R"cx \n\t" /* placeholder for guest rcx */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 5543 | "push %%"R"cx \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 5544 | "cmp %%"R"sp, %c[host_rsp](%0) \n\t" |
| 5545 | "je 1f \n\t" |
| 5546 | "mov %%"R"sp, %c[host_rsp](%0) \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 5547 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 5548 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 5549 | /* Reload cr2 if changed */ |
| 5550 | "mov %c[cr2](%0), %%"R"ax \n\t" |
| 5551 | "mov %%cr2, %%"R"dx \n\t" |
| 5552 | "cmp %%"R"ax, %%"R"dx \n\t" |
| 5553 | "je 2f \n\t" |
| 5554 | "mov %%"R"ax, %%cr2 \n\t" |
| 5555 | "2: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5556 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 5557 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5558 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 5559 | "mov %c[rax](%0), %%"R"ax \n\t" |
| 5560 | "mov %c[rbx](%0), %%"R"bx \n\t" |
| 5561 | "mov %c[rdx](%0), %%"R"dx \n\t" |
| 5562 | "mov %c[rsi](%0), %%"R"si \n\t" |
| 5563 | "mov %c[rdi](%0), %%"R"di \n\t" |
| 5564 | "mov %c[rbp](%0), %%"R"bp \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5565 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 5566 | "mov %c[r8](%0), %%r8 \n\t" |
| 5567 | "mov %c[r9](%0), %%r9 \n\t" |
| 5568 | "mov %c[r10](%0), %%r10 \n\t" |
| 5569 | "mov %c[r11](%0), %%r11 \n\t" |
| 5570 | "mov %c[r12](%0), %%r12 \n\t" |
| 5571 | "mov %c[r13](%0), %%r13 \n\t" |
| 5572 | "mov %c[r14](%0), %%r14 \n\t" |
| 5573 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5574 | #endif |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 5575 | "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */ |
| 5576 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5577 | /* Enter guest mode */ |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 5578 | "jne .Llaunched \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 5579 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 5580 | "jmp .Lkvm_vmx_return \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 5581 | ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 5582 | ".Lkvm_vmx_return: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5583 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 5584 | "mov %0, %c[wordsize](%%"R"sp) \n\t" |
| 5585 | "pop %0 \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 5586 | "mov %%"R"ax, %c[rax](%0) \n\t" |
| 5587 | "mov %%"R"bx, %c[rbx](%0) \n\t" |
Avi Kivity | 1c696d0 | 2011-01-06 18:09:11 +0200 | [diff] [blame] | 5588 | "pop"Q" %c[rcx](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 5589 | "mov %%"R"dx, %c[rdx](%0) \n\t" |
| 5590 | "mov %%"R"si, %c[rsi](%0) \n\t" |
| 5591 | "mov %%"R"di, %c[rdi](%0) \n\t" |
| 5592 | "mov %%"R"bp, %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5593 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 5594 | "mov %%r8, %c[r8](%0) \n\t" |
| 5595 | "mov %%r9, %c[r9](%0) \n\t" |
| 5596 | "mov %%r10, %c[r10](%0) \n\t" |
| 5597 | "mov %%r11, %c[r11](%0) \n\t" |
| 5598 | "mov %%r12, %c[r12](%0) \n\t" |
| 5599 | "mov %%r13, %c[r13](%0) \n\t" |
| 5600 | "mov %%r14, %c[r14](%0) \n\t" |
| 5601 | "mov %%r15, %c[r15](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5602 | #endif |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 5603 | "mov %%cr2, %%"R"ax \n\t" |
| 5604 | "mov %%"R"ax, %c[cr2](%0) \n\t" |
| 5605 | |
Avi Kivity | 1c696d0 | 2011-01-06 18:09:11 +0200 | [diff] [blame] | 5606 | "pop %%"R"bp; pop %%"R"dx \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 5607 | "setbe %c[fail](%0) \n\t" |
| 5608 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 5609 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 5610 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 5611 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5612 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 5613 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 5614 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 5615 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 5616 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 5617 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 5618 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5619 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5620 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 5621 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 5622 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 5623 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 5624 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 5625 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 5626 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 5627 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5628 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 5629 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 5630 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 5631 | : "cc", "memory" |
Jan Kiszka | 07d6f55 | 2010-09-28 16:37:42 +0200 | [diff] [blame] | 5632 | , R"ax", R"bx", R"di", R"si" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 5633 | #ifdef CONFIG_X86_64 |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 5634 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
| 5635 | #endif |
| 5636 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5637 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 5638 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 5639 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 5640 | | (1 << VCPU_EXREG_CPL) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 5641 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5642 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 5643 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 5644 | vcpu->arch.regs_dirty = 0; |
| 5645 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5646 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 5647 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 5648 | asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS)); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 5649 | vmx->loaded_vmcs->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 5650 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5651 | vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5652 | |
| 5653 | vmx_complete_atomic_exit(vmx); |
| 5654 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5655 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5656 | } |
| 5657 | |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 5658 | #undef R |
| 5659 | #undef Q |
| 5660 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5661 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 5662 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5663 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5664 | |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5665 | free_vpid(vmx); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 5666 | free_nested(vmx); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 5667 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5668 | kfree(vmx->guest_msrs); |
| 5669 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 5670 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5671 | } |
| 5672 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5673 | 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] | 5674 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5675 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 5676 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 5677 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5678 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5679 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5680 | return ERR_PTR(-ENOMEM); |
| 5681 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5682 | allocate_vpid(vmx); |
| 5683 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5684 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 5685 | if (err) |
| 5686 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 5687 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5688 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 5689 | err = -ENOMEM; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5690 | if (!vmx->guest_msrs) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5691 | goto uninit_vcpu; |
| 5692 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 5693 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 5694 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 5695 | vmx->loaded_vmcs->vmcs = alloc_vmcs(); |
| 5696 | if (!vmx->loaded_vmcs->vmcs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5697 | goto free_msrs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 5698 | if (!vmm_exclusive) |
| 5699 | kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id()))); |
| 5700 | loaded_vmcs_init(vmx->loaded_vmcs); |
| 5701 | if (!vmm_exclusive) |
| 5702 | kvm_cpu_vmxoff(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5703 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 5704 | cpu = get_cpu(); |
| 5705 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 5706 | vmx->vcpu.cpu = cpu; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 5707 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5708 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 5709 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5710 | if (err) |
| 5711 | goto free_vmcs; |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 5712 | if (vm_need_virtualize_apic_accesses(kvm)) |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 5713 | err = alloc_apic_access_page(kvm); |
| 5714 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 5715 | goto free_vmcs; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 5716 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 5717 | if (enable_ept) { |
| 5718 | if (!kvm->arch.ept_identity_map_addr) |
| 5719 | kvm->arch.ept_identity_map_addr = |
| 5720 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 5721 | err = -ENOMEM; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5722 | if (alloc_identity_pagetable(kvm) != 0) |
| 5723 | goto free_vmcs; |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 5724 | if (!init_rmode_identity_map(kvm)) |
| 5725 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 5726 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5727 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 5728 | vmx->nested.current_vmptr = -1ull; |
| 5729 | vmx->nested.current_vmcs12 = NULL; |
| 5730 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5731 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 5732 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5733 | free_vmcs: |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 5734 | free_vmcs(vmx->loaded_vmcs->vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5735 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5736 | kfree(vmx->guest_msrs); |
| 5737 | uninit_vcpu: |
| 5738 | kvm_vcpu_uninit(&vmx->vcpu); |
| 5739 | free_vcpu: |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5740 | free_vpid(vmx); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 5741 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 5742 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5743 | } |
| 5744 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 5745 | static void __init vmx_check_processor_compat(void *rtn) |
| 5746 | { |
| 5747 | struct vmcs_config vmcs_conf; |
| 5748 | |
| 5749 | *(int *)rtn = 0; |
| 5750 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 5751 | *(int *)rtn = -EIO; |
| 5752 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 5753 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 5754 | smp_processor_id()); |
| 5755 | *(int *)rtn = -EIO; |
| 5756 | } |
| 5757 | } |
| 5758 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 5759 | static int get_ept_level(void) |
| 5760 | { |
| 5761 | return VMX_EPT_DEFAULT_GAW + 1; |
| 5762 | } |
| 5763 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 5764 | 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] | 5765 | { |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 5766 | u64 ret; |
| 5767 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 5768 | /* For VT-d and EPT combination |
| 5769 | * 1. MMIO: always map as UC |
| 5770 | * 2. EPT with VT-d: |
| 5771 | * a. VT-d without snooping control feature: can't guarantee the |
| 5772 | * result, try to trust guest. |
| 5773 | * b. VT-d with snooping control feature: snooping control feature of |
| 5774 | * VT-d engine can guarantee the cache correctness. Just set it |
| 5775 | * 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] | 5776 | * 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] | 5777 | * consistent with host MTRR |
| 5778 | */ |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 5779 | if (is_mmio) |
| 5780 | ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT; |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 5781 | else if (vcpu->kvm->arch.iommu_domain && |
| 5782 | !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY)) |
| 5783 | ret = kvm_get_guest_memory_type(vcpu, gfn) << |
| 5784 | VMX_EPT_MT_EPTE_SHIFT; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 5785 | else |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 5786 | ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT) |
Sheng Yang | a19a6d1 | 2010-02-09 16:41:53 +0800 | [diff] [blame] | 5787 | | VMX_EPT_IPAT_BIT; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 5788 | |
| 5789 | return ret; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 5790 | } |
| 5791 | |
Avi Kivity | f4c9e87 | 2009-12-28 16:06:35 +0200 | [diff] [blame] | 5792 | #define _ER(x) { EXIT_REASON_##x, #x } |
| 5793 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5794 | static const struct trace_print_flags vmx_exit_reasons_str[] = { |
Avi Kivity | f4c9e87 | 2009-12-28 16:06:35 +0200 | [diff] [blame] | 5795 | _ER(EXCEPTION_NMI), |
| 5796 | _ER(EXTERNAL_INTERRUPT), |
| 5797 | _ER(TRIPLE_FAULT), |
| 5798 | _ER(PENDING_INTERRUPT), |
| 5799 | _ER(NMI_WINDOW), |
| 5800 | _ER(TASK_SWITCH), |
| 5801 | _ER(CPUID), |
| 5802 | _ER(HLT), |
| 5803 | _ER(INVLPG), |
| 5804 | _ER(RDPMC), |
| 5805 | _ER(RDTSC), |
| 5806 | _ER(VMCALL), |
| 5807 | _ER(VMCLEAR), |
| 5808 | _ER(VMLAUNCH), |
| 5809 | _ER(VMPTRLD), |
| 5810 | _ER(VMPTRST), |
| 5811 | _ER(VMREAD), |
| 5812 | _ER(VMRESUME), |
| 5813 | _ER(VMWRITE), |
| 5814 | _ER(VMOFF), |
| 5815 | _ER(VMON), |
| 5816 | _ER(CR_ACCESS), |
| 5817 | _ER(DR_ACCESS), |
| 5818 | _ER(IO_INSTRUCTION), |
| 5819 | _ER(MSR_READ), |
| 5820 | _ER(MSR_WRITE), |
| 5821 | _ER(MWAIT_INSTRUCTION), |
| 5822 | _ER(MONITOR_INSTRUCTION), |
| 5823 | _ER(PAUSE_INSTRUCTION), |
| 5824 | _ER(MCE_DURING_VMENTRY), |
| 5825 | _ER(TPR_BELOW_THRESHOLD), |
| 5826 | _ER(APIC_ACCESS), |
| 5827 | _ER(EPT_VIOLATION), |
| 5828 | _ER(EPT_MISCONFIG), |
| 5829 | _ER(WBINVD), |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5830 | { -1, NULL } |
| 5831 | }; |
| 5832 | |
Avi Kivity | f4c9e87 | 2009-12-28 16:06:35 +0200 | [diff] [blame] | 5833 | #undef _ER |
| 5834 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 5835 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 5836 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 5837 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 5838 | return PT_DIRECTORY_LEVEL; |
| 5839 | else |
| 5840 | /* For shadow and EPT supported 1GB page */ |
| 5841 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 5842 | } |
| 5843 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 5844 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 5845 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 5846 | struct kvm_cpuid_entry2 *best; |
| 5847 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5848 | u32 exec_control; |
| 5849 | |
| 5850 | vmx->rdtscp_enabled = false; |
| 5851 | if (vmx_rdtscp_supported()) { |
| 5852 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 5853 | if (exec_control & SECONDARY_EXEC_RDTSCP) { |
| 5854 | best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 5855 | if (best && (best->edx & bit(X86_FEATURE_RDTSCP))) |
| 5856 | vmx->rdtscp_enabled = true; |
| 5857 | else { |
| 5858 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 5859 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 5860 | exec_control); |
| 5861 | } |
| 5862 | } |
| 5863 | } |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 5864 | } |
| 5865 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 5866 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 5867 | { |
| 5868 | } |
| 5869 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 5870 | /* |
| 5871 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 5872 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
| 5873 | * with L0's requirements for its guest (a.k.a. vmsc01), so we can run the L2 |
| 5874 | * guest in a way that will both be appropriate to L1's requests, and our |
| 5875 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 5876 | * function also has additional necessary side-effects, like setting various |
| 5877 | * vcpu->arch fields. |
| 5878 | */ |
| 5879 | static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 5880 | { |
| 5881 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5882 | u32 exec_control; |
| 5883 | |
| 5884 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 5885 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 5886 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 5887 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 5888 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 5889 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 5890 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 5891 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 5892 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 5893 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 5894 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 5895 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 5896 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 5897 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 5898 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 5899 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 5900 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 5901 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 5902 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 5903 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 5904 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 5905 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 5906 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 5907 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 5908 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 5909 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 5910 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 5911 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 5912 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 5913 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 5914 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 5915 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 5916 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 5917 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 5918 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 5919 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
| 5920 | |
| 5921 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 5922 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 5923 | vmcs12->vm_entry_intr_info_field); |
| 5924 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 5925 | vmcs12->vm_entry_exception_error_code); |
| 5926 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 5927 | vmcs12->vm_entry_instruction_len); |
| 5928 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 5929 | vmcs12->guest_interruptibility_info); |
| 5930 | vmcs_write32(GUEST_ACTIVITY_STATE, vmcs12->guest_activity_state); |
| 5931 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 5932 | vmcs_writel(GUEST_DR7, vmcs12->guest_dr7); |
| 5933 | vmcs_writel(GUEST_RFLAGS, vmcs12->guest_rflags); |
| 5934 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 5935 | vmcs12->guest_pending_dbg_exceptions); |
| 5936 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 5937 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 5938 | |
| 5939 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 5940 | |
| 5941 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, |
| 5942 | (vmcs_config.pin_based_exec_ctrl | |
| 5943 | vmcs12->pin_based_vm_exec_control)); |
| 5944 | |
| 5945 | /* |
| 5946 | * Whether page-faults are trapped is determined by a combination of |
| 5947 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 5948 | * If enable_ept, L0 doesn't care about page faults and we should |
| 5949 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 5950 | * care about (at least some) page faults, and because it is not easy |
| 5951 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 5952 | * to exit on each and every L2 page fault. This is done by setting |
| 5953 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 5954 | * Note that below we don't need special code to set EB.PF beyond the |
| 5955 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 5956 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 5957 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 5958 | * |
| 5959 | * A problem with this approach (when !enable_ept) is that L1 may be |
| 5960 | * injected with more page faults than it asked for. This could have |
| 5961 | * caused problems, but in practice existing hypervisors don't care. |
| 5962 | * To fix this, we will need to emulate the PFEC checking (on the L1 |
| 5963 | * page tables), using walk_addr(), when injecting PFs to L1. |
| 5964 | */ |
| 5965 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 5966 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 5967 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 5968 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 5969 | |
| 5970 | if (cpu_has_secondary_exec_ctrls()) { |
| 5971 | u32 exec_control = vmx_secondary_exec_control(vmx); |
| 5972 | if (!vmx->rdtscp_enabled) |
| 5973 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 5974 | /* Take the following fields only from vmcs12 */ |
| 5975 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 5976 | if (nested_cpu_has(vmcs12, |
| 5977 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 5978 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 5979 | |
| 5980 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) { |
| 5981 | /* |
| 5982 | * Translate L1 physical address to host physical |
| 5983 | * address for vmcs02. Keep the page pinned, so this |
| 5984 | * physical address remains valid. We keep a reference |
| 5985 | * to it so we can release it later. |
| 5986 | */ |
| 5987 | if (vmx->nested.apic_access_page) /* shouldn't happen */ |
| 5988 | nested_release_page(vmx->nested.apic_access_page); |
| 5989 | vmx->nested.apic_access_page = |
| 5990 | nested_get_page(vcpu, vmcs12->apic_access_addr); |
| 5991 | /* |
| 5992 | * If translation failed, no matter: This feature asks |
| 5993 | * to exit when accessing the given address, and if it |
| 5994 | * can never be accessed, this feature won't do |
| 5995 | * anything anyway. |
| 5996 | */ |
| 5997 | if (!vmx->nested.apic_access_page) |
| 5998 | exec_control &= |
| 5999 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6000 | else |
| 6001 | vmcs_write64(APIC_ACCESS_ADDR, |
| 6002 | page_to_phys(vmx->nested.apic_access_page)); |
| 6003 | } |
| 6004 | |
| 6005 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 6006 | } |
| 6007 | |
| 6008 | |
| 6009 | /* |
| 6010 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 6011 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 6012 | * Other fields are different per CPU, and will be set later when |
| 6013 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 6014 | */ |
| 6015 | vmx_set_constant_host_state(); |
| 6016 | |
| 6017 | /* |
| 6018 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 6019 | * entry, but only if the current (host) sp changed from the value |
| 6020 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 6021 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 6022 | * here we just force the write to happen on entry. |
| 6023 | */ |
| 6024 | vmx->host_rsp = 0; |
| 6025 | |
| 6026 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 6027 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 6028 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 6029 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 6030 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
| 6031 | /* |
| 6032 | * Merging of IO and MSR bitmaps not currently supported. |
| 6033 | * Rather, exit every time. |
| 6034 | */ |
| 6035 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 6036 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 6037 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 6038 | |
| 6039 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 6040 | |
| 6041 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 6042 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 6043 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 6044 | */ |
| 6045 | update_exception_bitmap(vcpu); |
| 6046 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 6047 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 6048 | |
| 6049 | /* Note: IA32_MODE, LOAD_IA32_EFER are modified by vmx_set_efer below */ |
| 6050 | vmcs_write32(VM_EXIT_CONTROLS, |
| 6051 | vmcs12->vm_exit_controls | vmcs_config.vmexit_ctrl); |
| 6052 | vmcs_write32(VM_ENTRY_CONTROLS, vmcs12->vm_entry_controls | |
| 6053 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 6054 | |
| 6055 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) |
| 6056 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
| 6057 | else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 6058 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 6059 | |
| 6060 | |
| 6061 | set_cr4_guest_host_mask(vmx); |
| 6062 | |
| 6063 | vmcs_write64(TSC_OFFSET, |
| 6064 | vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset); |
| 6065 | |
| 6066 | if (enable_vpid) { |
| 6067 | /* |
| 6068 | * Trivially support vpid by letting L2s share their parent |
| 6069 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 6070 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 6071 | */ |
| 6072 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 6073 | vmx_flush_tlb(vcpu); |
| 6074 | } |
| 6075 | |
| 6076 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) |
| 6077 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
| 6078 | if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 6079 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 6080 | else |
| 6081 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 6082 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 6083 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 6084 | |
| 6085 | /* |
| 6086 | * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified |
| 6087 | * TS bit (for lazy fpu) and bits which we consider mandatory enabled. |
| 6088 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 6089 | * the specifications by L1; It's not enough to take |
| 6090 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 6091 | * have more bits than L1 expected. |
| 6092 | */ |
| 6093 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 6094 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 6095 | |
| 6096 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 6097 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 6098 | |
| 6099 | /* shadow page tables on either EPT or shadow page tables */ |
| 6100 | kvm_set_cr3(vcpu, vmcs12->guest_cr3); |
| 6101 | kvm_mmu_reset_context(vcpu); |
| 6102 | |
| 6103 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 6104 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
| 6105 | } |
| 6106 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame^] | 6107 | /* |
| 6108 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 6109 | * for running an L2 nested guest. |
| 6110 | */ |
| 6111 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 6112 | { |
| 6113 | struct vmcs12 *vmcs12; |
| 6114 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6115 | int cpu; |
| 6116 | struct loaded_vmcs *vmcs02; |
| 6117 | |
| 6118 | if (!nested_vmx_check_permission(vcpu) || |
| 6119 | !nested_vmx_check_vmcs12(vcpu)) |
| 6120 | return 1; |
| 6121 | |
| 6122 | skip_emulated_instruction(vcpu); |
| 6123 | vmcs12 = get_vmcs12(vcpu); |
| 6124 | |
| 6125 | vmcs02 = nested_get_current_vmcs02(vmx); |
| 6126 | if (!vmcs02) |
| 6127 | return -ENOMEM; |
| 6128 | |
| 6129 | enter_guest_mode(vcpu); |
| 6130 | |
| 6131 | vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET); |
| 6132 | |
| 6133 | cpu = get_cpu(); |
| 6134 | vmx->loaded_vmcs = vmcs02; |
| 6135 | vmx_vcpu_put(vcpu); |
| 6136 | vmx_vcpu_load(vcpu, cpu); |
| 6137 | vcpu->cpu = cpu; |
| 6138 | put_cpu(); |
| 6139 | |
| 6140 | vmcs12->launch_state = 1; |
| 6141 | |
| 6142 | prepare_vmcs02(vcpu, vmcs12); |
| 6143 | |
| 6144 | /* |
| 6145 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 6146 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 6147 | * returned as far as L1 is concerned. It will only return (and set |
| 6148 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 6149 | */ |
| 6150 | return 1; |
| 6151 | } |
| 6152 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 6153 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 6154 | struct x86_instruction_info *info, |
| 6155 | enum x86_intercept_stage stage) |
| 6156 | { |
| 6157 | return X86EMUL_CONTINUE; |
| 6158 | } |
| 6159 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 6160 | static struct kvm_x86_ops vmx_x86_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6161 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 6162 | .disabled_by_bios = vmx_disabled_by_bios, |
| 6163 | .hardware_setup = hardware_setup, |
| 6164 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6165 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6166 | .hardware_enable = hardware_enable, |
| 6167 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 6168 | .cpu_has_accelerated_tpr = report_flexpriority, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6169 | |
| 6170 | .vcpu_create = vmx_create_vcpu, |
| 6171 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 6172 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6173 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 6174 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6175 | .vcpu_load = vmx_vcpu_load, |
| 6176 | .vcpu_put = vmx_vcpu_put, |
| 6177 | |
| 6178 | .set_guest_debug = set_guest_debug, |
| 6179 | .get_msr = vmx_get_msr, |
| 6180 | .set_msr = vmx_set_msr, |
| 6181 | .get_segment_base = vmx_get_segment_base, |
| 6182 | .get_segment = vmx_get_segment, |
| 6183 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 6184 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6185 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 6186 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 6187 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 6188 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6189 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6190 | .set_cr3 = vmx_set_cr3, |
| 6191 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6192 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6193 | .get_idt = vmx_get_idt, |
| 6194 | .set_idt = vmx_set_idt, |
| 6195 | .get_gdt = vmx_get_gdt, |
| 6196 | .set_gdt = vmx_set_gdt, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6197 | .set_dr7 = vmx_set_dr7, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 6198 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6199 | .get_rflags = vmx_get_rflags, |
| 6200 | .set_rflags = vmx_set_rflags, |
Avi Kivity | ebcbab4 | 2010-02-07 11:56:52 +0200 | [diff] [blame] | 6201 | .fpu_activate = vmx_fpu_activate, |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 6202 | .fpu_deactivate = vmx_fpu_deactivate, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6203 | |
| 6204 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6205 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6206 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 6207 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6208 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 6209 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 6210 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6211 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 6212 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 6213 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 6214 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 6215 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6216 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 6217 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6218 | .get_nmi_mask = vmx_get_nmi_mask, |
| 6219 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 6220 | .enable_nmi_window = enable_nmi_window, |
| 6221 | .enable_irq_window = enable_irq_window, |
| 6222 | .update_cr8_intercept = update_cr8_intercept, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 6223 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6224 | .set_tss_addr = vmx_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 6225 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6226 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6227 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 6228 | .get_exit_info = vmx_get_exit_info, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6229 | .exit_reasons_str = vmx_exit_reasons_str, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 6230 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 6231 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 6232 | |
| 6233 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 6234 | |
| 6235 | .rdtscp_supported = vmx_rdtscp_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 6236 | |
| 6237 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 6238 | |
| 6239 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 6240 | |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 6241 | .set_tsc_khz = vmx_set_tsc_khz, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 6242 | .write_tsc_offset = vmx_write_tsc_offset, |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 6243 | .adjust_tsc_offset = vmx_adjust_tsc_offset, |
Joerg Roedel | 857e409 | 2011-03-25 09:44:50 +0100 | [diff] [blame] | 6244 | .compute_tsc_offset = vmx_compute_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 6245 | |
| 6246 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 6247 | |
| 6248 | .check_intercept = vmx_check_intercept, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6249 | }; |
| 6250 | |
| 6251 | static int __init vmx_init(void) |
| 6252 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 6253 | int r, i; |
| 6254 | |
| 6255 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 6256 | |
| 6257 | for (i = 0; i < NR_VMX_MSR; ++i) |
| 6258 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 6259 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 6260 | vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 6261 | if (!vmx_io_bitmap_a) |
| 6262 | return -ENOMEM; |
| 6263 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 6264 | vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 6265 | if (!vmx_io_bitmap_b) { |
| 6266 | r = -ENOMEM; |
| 6267 | goto out; |
| 6268 | } |
| 6269 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 6270 | vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6271 | if (!vmx_msr_bitmap_legacy) { |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6272 | r = -ENOMEM; |
| 6273 | goto out1; |
| 6274 | } |
| 6275 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 6276 | vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6277 | if (!vmx_msr_bitmap_longmode) { |
| 6278 | r = -ENOMEM; |
| 6279 | goto out2; |
| 6280 | } |
| 6281 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 6282 | /* |
| 6283 | * Allow direct access to the PC debug port (it is often used for I/O |
| 6284 | * delays, but the vmexits simply slow things down). |
| 6285 | */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 6286 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); |
| 6287 | clear_bit(0x80, vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 6288 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 6289 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 6290 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 6291 | memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE); |
| 6292 | memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6293 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 6294 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 6295 | |
Avi Kivity | 0ee75be | 2010-04-28 15:39:01 +0300 | [diff] [blame] | 6296 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
| 6297 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 6298 | if (r) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 6299 | goto out3; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6300 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 6301 | vmx_disable_intercept_for_msr(MSR_FS_BASE, false); |
| 6302 | vmx_disable_intercept_for_msr(MSR_GS_BASE, false); |
| 6303 | vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true); |
| 6304 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false); |
| 6305 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false); |
| 6306 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 6307 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 6308 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6309 | bypass_guest_pf = 0; |
Sheng Yang | 534e38b | 2008-09-08 15:12:30 +0800 | [diff] [blame] | 6310 | kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6311 | VMX_EPT_EXECUTABLE_MASK); |
Sheng Yang | 5fdbcb9 | 2008-07-16 09:25:40 +0800 | [diff] [blame] | 6312 | kvm_enable_tdp(); |
| 6313 | } else |
| 6314 | kvm_disable_tdp(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6315 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 6316 | if (bypass_guest_pf) |
| 6317 | kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull); |
| 6318 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 6319 | return 0; |
| 6320 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 6321 | out3: |
| 6322 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6323 | out2: |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 6324 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 6325 | out1: |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 6326 | free_page((unsigned long)vmx_io_bitmap_b); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 6327 | out: |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 6328 | free_page((unsigned long)vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 6329 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6330 | } |
| 6331 | |
| 6332 | static void __exit vmx_exit(void) |
| 6333 | { |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 6334 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
| 6335 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 6336 | free_page((unsigned long)vmx_io_bitmap_b); |
| 6337 | free_page((unsigned long)vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 6338 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 6339 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6340 | } |
| 6341 | |
| 6342 | module_init(vmx_init) |
| 6343 | module_exit(vmx_exit) |