Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * This module enables machines with Intel VT-x extensions to run virtual |
| 5 | * machines without emulation or binary translation. |
| 6 | * |
| 7 | * Copyright (C) 2006 Qumranet, Inc. |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 8 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9 | * |
| 10 | * Authors: |
| 11 | * Avi Kivity <avi@qumranet.com> |
| 12 | * Yaniv Kamay <yaniv@qumranet.com> |
| 13 | * |
| 14 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 15 | * the COPYING file in the top-level directory. |
| 16 | * |
| 17 | */ |
| 18 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 19 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 20 | #include "mmu.h" |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 21 | #include "cpuid.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 22 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 23 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 24 | #include <linux/module.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 25 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 26 | #include <linux/mm.h> |
| 27 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 28 | #include <linux/sched.h> |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 29 | #include <linux/moduleparam.h> |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 30 | #include <linux/mod_devicetable.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 31 | #include <linux/ftrace_event.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 33 | #include <linux/tboot.h> |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 34 | #include <linux/hrtimer.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 35 | #include "kvm_cache_regs.h" |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 36 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 37 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 38 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 39 | #include <asm/desc.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 40 | #include <asm/vmx.h> |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 41 | #include <asm/virtext.h> |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 42 | #include <asm/mce.h> |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 43 | #include <asm/i387.h> |
| 44 | #include <asm/xcr.h> |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 45 | #include <asm/perf_event.h> |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 46 | #include <asm/debugreg.h> |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 47 | #include <asm/kexec.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 48 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 49 | #include "trace.h" |
| 50 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 51 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 52 | #define __ex_clear(x, reg) \ |
| 53 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 54 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 55 | MODULE_AUTHOR("Qumranet"); |
| 56 | MODULE_LICENSE("GPL"); |
| 57 | |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 58 | static const struct x86_cpu_id vmx_cpu_id[] = { |
| 59 | X86_FEATURE_MATCH(X86_FEATURE_VMX), |
| 60 | {} |
| 61 | }; |
| 62 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); |
| 63 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 64 | static bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 65 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 66 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 67 | static bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 68 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 69 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 70 | static bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 71 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 72 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 73 | static bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 74 | module_param_named(unrestricted_guest, |
| 75 | enable_unrestricted_guest, bool, S_IRUGO); |
| 76 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 77 | static bool __read_mostly enable_ept_ad_bits = 1; |
| 78 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 79 | |
Avi Kivity | a27685c | 2012-06-12 20:30:18 +0300 | [diff] [blame] | 80 | static bool __read_mostly emulate_invalid_guest_state = true; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 81 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 82 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 83 | static bool __read_mostly vmm_exclusive = 1; |
Dongxiao Xu | b923e62 | 2010-05-11 18:29:45 +0800 | [diff] [blame] | 84 | module_param(vmm_exclusive, bool, S_IRUGO); |
| 85 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 86 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 87 | module_param(fasteoi, bool, S_IRUGO); |
| 88 | |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 89 | static bool __read_mostly enable_apicv = 1; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 90 | module_param(enable_apicv, bool, S_IRUGO); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 91 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 92 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 93 | module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 94 | /* |
| 95 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 96 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 97 | * use VMX instructions. |
| 98 | */ |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 99 | static bool __read_mostly nested = 0; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 100 | module_param(nested, bool, S_IRUGO); |
| 101 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 102 | static u64 __read_mostly host_xss; |
| 103 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 104 | static bool __read_mostly enable_pml = 1; |
| 105 | module_param_named(pml, enable_pml, bool, S_IRUGO); |
| 106 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 107 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
| 108 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST (X86_CR0_WP | X86_CR0_NE) |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 109 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 110 | (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] | 111 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 112 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Andy Lutomirski | 52ce3c2 | 2014-10-07 17:16:21 -0700 | [diff] [blame] | 113 | | X86_CR4_OSXMMEXCPT | X86_CR4_TSD) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 114 | |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 115 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 116 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 117 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 118 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 119 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 120 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 121 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 122 | /* |
| 123 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 124 | * ple_gap: upper bound on the amount of time between two successive |
| 125 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 126 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 127 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 128 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 129 | * less than 2^12 cycles |
| 130 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 131 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 132 | */ |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 133 | #define KVM_VMX_DEFAULT_PLE_GAP 128 |
| 134 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 |
| 135 | #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2 |
| 136 | #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0 |
| 137 | #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \ |
| 138 | INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW |
| 139 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 140 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
| 141 | module_param(ple_gap, int, S_IRUGO); |
| 142 | |
| 143 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 144 | module_param(ple_window, int, S_IRUGO); |
| 145 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 146 | /* Default doubles per-vcpu window every exit. */ |
| 147 | static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW; |
| 148 | module_param(ple_window_grow, int, S_IRUGO); |
| 149 | |
| 150 | /* Default resets per-vcpu window every exit to ple_window. */ |
| 151 | static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK; |
| 152 | module_param(ple_window_shrink, int, S_IRUGO); |
| 153 | |
| 154 | /* Default is to compute the maximum so we can never overflow. */ |
| 155 | static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 156 | static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 157 | module_param(ple_window_max, int, S_IRUGO); |
| 158 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 159 | extern const ulong vmx_return; |
| 160 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 161 | #define NR_AUTOLOAD_MSRS 8 |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 162 | #define VMCS02_POOL_SIZE 1 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 163 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 164 | struct vmcs { |
| 165 | u32 revision_id; |
| 166 | u32 abort; |
| 167 | char data[0]; |
| 168 | }; |
| 169 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 170 | /* |
| 171 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 172 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 173 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 174 | */ |
| 175 | struct loaded_vmcs { |
| 176 | struct vmcs *vmcs; |
| 177 | int cpu; |
| 178 | int launched; |
| 179 | struct list_head loaded_vmcss_on_cpu_link; |
| 180 | }; |
| 181 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 182 | struct shared_msr_entry { |
| 183 | unsigned index; |
| 184 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 185 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 186 | }; |
| 187 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 188 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 189 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 190 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 191 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 192 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 193 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 194 | * More than one of these structures may exist, if L1 runs multiple L2 guests. |
| 195 | * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the |
| 196 | * underlying hardware which will be used to run L2. |
| 197 | * This structure is packed to ensure that its layout is identical across |
| 198 | * machines (necessary for live migration). |
| 199 | * If there are changes in this struct, VMCS12_REVISION must be changed. |
| 200 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 201 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 202 | struct __packed vmcs12 { |
| 203 | /* According to the Intel spec, a VMCS region must start with the |
| 204 | * following two fields. Then follow implementation-specific data. |
| 205 | */ |
| 206 | u32 revision_id; |
| 207 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 208 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 209 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 210 | u32 padding[7]; /* room for future expansion */ |
| 211 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 212 | u64 io_bitmap_a; |
| 213 | u64 io_bitmap_b; |
| 214 | u64 msr_bitmap; |
| 215 | u64 vm_exit_msr_store_addr; |
| 216 | u64 vm_exit_msr_load_addr; |
| 217 | u64 vm_entry_msr_load_addr; |
| 218 | u64 tsc_offset; |
| 219 | u64 virtual_apic_page_addr; |
| 220 | u64 apic_access_addr; |
| 221 | u64 ept_pointer; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 222 | u64 xss_exit_bitmap; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 223 | u64 guest_physical_address; |
| 224 | u64 vmcs_link_pointer; |
| 225 | u64 guest_ia32_debugctl; |
| 226 | u64 guest_ia32_pat; |
| 227 | u64 guest_ia32_efer; |
| 228 | u64 guest_ia32_perf_global_ctrl; |
| 229 | u64 guest_pdptr0; |
| 230 | u64 guest_pdptr1; |
| 231 | u64 guest_pdptr2; |
| 232 | u64 guest_pdptr3; |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 233 | u64 guest_bndcfgs; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 234 | u64 host_ia32_pat; |
| 235 | u64 host_ia32_efer; |
| 236 | u64 host_ia32_perf_global_ctrl; |
| 237 | u64 padding64[8]; /* room for future expansion */ |
| 238 | /* |
| 239 | * To allow migration of L1 (complete with its L2 guests) between |
| 240 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 241 | * unsigned long fields with no explict size. We use u64 (aliased |
| 242 | * natural_width) instead. Luckily, x86 is little-endian. |
| 243 | */ |
| 244 | natural_width cr0_guest_host_mask; |
| 245 | natural_width cr4_guest_host_mask; |
| 246 | natural_width cr0_read_shadow; |
| 247 | natural_width cr4_read_shadow; |
| 248 | natural_width cr3_target_value0; |
| 249 | natural_width cr3_target_value1; |
| 250 | natural_width cr3_target_value2; |
| 251 | natural_width cr3_target_value3; |
| 252 | natural_width exit_qualification; |
| 253 | natural_width guest_linear_address; |
| 254 | natural_width guest_cr0; |
| 255 | natural_width guest_cr3; |
| 256 | natural_width guest_cr4; |
| 257 | natural_width guest_es_base; |
| 258 | natural_width guest_cs_base; |
| 259 | natural_width guest_ss_base; |
| 260 | natural_width guest_ds_base; |
| 261 | natural_width guest_fs_base; |
| 262 | natural_width guest_gs_base; |
| 263 | natural_width guest_ldtr_base; |
| 264 | natural_width guest_tr_base; |
| 265 | natural_width guest_gdtr_base; |
| 266 | natural_width guest_idtr_base; |
| 267 | natural_width guest_dr7; |
| 268 | natural_width guest_rsp; |
| 269 | natural_width guest_rip; |
| 270 | natural_width guest_rflags; |
| 271 | natural_width guest_pending_dbg_exceptions; |
| 272 | natural_width guest_sysenter_esp; |
| 273 | natural_width guest_sysenter_eip; |
| 274 | natural_width host_cr0; |
| 275 | natural_width host_cr3; |
| 276 | natural_width host_cr4; |
| 277 | natural_width host_fs_base; |
| 278 | natural_width host_gs_base; |
| 279 | natural_width host_tr_base; |
| 280 | natural_width host_gdtr_base; |
| 281 | natural_width host_idtr_base; |
| 282 | natural_width host_ia32_sysenter_esp; |
| 283 | natural_width host_ia32_sysenter_eip; |
| 284 | natural_width host_rsp; |
| 285 | natural_width host_rip; |
| 286 | natural_width paddingl[8]; /* room for future expansion */ |
| 287 | u32 pin_based_vm_exec_control; |
| 288 | u32 cpu_based_vm_exec_control; |
| 289 | u32 exception_bitmap; |
| 290 | u32 page_fault_error_code_mask; |
| 291 | u32 page_fault_error_code_match; |
| 292 | u32 cr3_target_count; |
| 293 | u32 vm_exit_controls; |
| 294 | u32 vm_exit_msr_store_count; |
| 295 | u32 vm_exit_msr_load_count; |
| 296 | u32 vm_entry_controls; |
| 297 | u32 vm_entry_msr_load_count; |
| 298 | u32 vm_entry_intr_info_field; |
| 299 | u32 vm_entry_exception_error_code; |
| 300 | u32 vm_entry_instruction_len; |
| 301 | u32 tpr_threshold; |
| 302 | u32 secondary_vm_exec_control; |
| 303 | u32 vm_instruction_error; |
| 304 | u32 vm_exit_reason; |
| 305 | u32 vm_exit_intr_info; |
| 306 | u32 vm_exit_intr_error_code; |
| 307 | u32 idt_vectoring_info_field; |
| 308 | u32 idt_vectoring_error_code; |
| 309 | u32 vm_exit_instruction_len; |
| 310 | u32 vmx_instruction_info; |
| 311 | u32 guest_es_limit; |
| 312 | u32 guest_cs_limit; |
| 313 | u32 guest_ss_limit; |
| 314 | u32 guest_ds_limit; |
| 315 | u32 guest_fs_limit; |
| 316 | u32 guest_gs_limit; |
| 317 | u32 guest_ldtr_limit; |
| 318 | u32 guest_tr_limit; |
| 319 | u32 guest_gdtr_limit; |
| 320 | u32 guest_idtr_limit; |
| 321 | u32 guest_es_ar_bytes; |
| 322 | u32 guest_cs_ar_bytes; |
| 323 | u32 guest_ss_ar_bytes; |
| 324 | u32 guest_ds_ar_bytes; |
| 325 | u32 guest_fs_ar_bytes; |
| 326 | u32 guest_gs_ar_bytes; |
| 327 | u32 guest_ldtr_ar_bytes; |
| 328 | u32 guest_tr_ar_bytes; |
| 329 | u32 guest_interruptibility_info; |
| 330 | u32 guest_activity_state; |
| 331 | u32 guest_sysenter_cs; |
| 332 | u32 host_ia32_sysenter_cs; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 333 | u32 vmx_preemption_timer_value; |
| 334 | u32 padding32[7]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 335 | u16 virtual_processor_id; |
| 336 | u16 guest_es_selector; |
| 337 | u16 guest_cs_selector; |
| 338 | u16 guest_ss_selector; |
| 339 | u16 guest_ds_selector; |
| 340 | u16 guest_fs_selector; |
| 341 | u16 guest_gs_selector; |
| 342 | u16 guest_ldtr_selector; |
| 343 | u16 guest_tr_selector; |
| 344 | u16 host_es_selector; |
| 345 | u16 host_cs_selector; |
| 346 | u16 host_ss_selector; |
| 347 | u16 host_ds_selector; |
| 348 | u16 host_fs_selector; |
| 349 | u16 host_gs_selector; |
| 350 | u16 host_tr_selector; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | /* |
| 354 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 355 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 356 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. |
| 357 | */ |
| 358 | #define VMCS12_REVISION 0x11e57ed0 |
| 359 | |
| 360 | /* |
| 361 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 362 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 363 | * current implementation, 4K are reserved to avoid future complications. |
| 364 | */ |
| 365 | #define VMCS12_SIZE 0x1000 |
| 366 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 367 | /* Used to remember the last vmcs02 used for some recently used vmcs12s */ |
| 368 | struct vmcs02_list { |
| 369 | struct list_head list; |
| 370 | gpa_t vmptr; |
| 371 | struct loaded_vmcs vmcs02; |
| 372 | }; |
| 373 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 374 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 375 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 376 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 377 | */ |
| 378 | struct nested_vmx { |
| 379 | /* Has the level1 guest done vmxon? */ |
| 380 | bool vmxon; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 381 | gpa_t vmxon_ptr; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 382 | |
| 383 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 384 | gpa_t current_vmptr; |
| 385 | /* The host-usable pointer to the above */ |
| 386 | struct page *current_vmcs12_page; |
| 387 | struct vmcs12 *current_vmcs12; |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 388 | struct vmcs *current_shadow_vmcs; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 389 | /* |
| 390 | * Indicates if the shadow vmcs must be updated with the |
| 391 | * data hold by vmcs12 |
| 392 | */ |
| 393 | bool sync_shadow_vmcs; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 394 | |
| 395 | /* vmcs02_list cache of VMCSs recently used to run L2 guests */ |
| 396 | struct list_head vmcs02_pool; |
| 397 | int vmcs02_num; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 398 | u64 vmcs01_tsc_offset; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 399 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 400 | bool nested_run_pending; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 401 | /* |
| 402 | * Guest pages referred to in vmcs02 with host-physical pointers, so |
| 403 | * we must keep them pinned while L2 runs. |
| 404 | */ |
| 405 | struct page *apic_access_page; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 406 | struct page *virtual_apic_page; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 407 | u64 msr_ia32_feature_control; |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 408 | |
| 409 | struct hrtimer preemption_timer; |
| 410 | bool preemption_timer_expired; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 411 | |
| 412 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ |
| 413 | u64 vmcs01_debugctl; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 414 | }; |
| 415 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 416 | #define POSTED_INTR_ON 0 |
| 417 | /* Posted-Interrupt Descriptor */ |
| 418 | struct pi_desc { |
| 419 | u32 pir[8]; /* Posted interrupt requested */ |
| 420 | u32 control; /* bit 0 of control is outstanding notification bit */ |
| 421 | u32 rsvd[7]; |
| 422 | } __aligned(64); |
| 423 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 424 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
| 425 | { |
| 426 | return test_and_set_bit(POSTED_INTR_ON, |
| 427 | (unsigned long *)&pi_desc->control); |
| 428 | } |
| 429 | |
| 430 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) |
| 431 | { |
| 432 | return test_and_clear_bit(POSTED_INTR_ON, |
| 433 | (unsigned long *)&pi_desc->control); |
| 434 | } |
| 435 | |
| 436 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) |
| 437 | { |
| 438 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); |
| 439 | } |
| 440 | |
Marcelo Tosatti | 7c6a98d | 2014-12-16 09:08:14 -0500 | [diff] [blame] | 441 | static int pi_test_pir(int vector, struct pi_desc *pi_desc) |
| 442 | { |
| 443 | return test_bit(vector, (unsigned long *)pi_desc->pir); |
| 444 | } |
| 445 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 446 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 447 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 448 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 449 | u8 fail; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 450 | bool nmi_known_unmasked; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 451 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 452 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 453 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 454 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 455 | int nmsrs; |
| 456 | int save_nmsrs; |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 457 | unsigned long host_idt_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 458 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 459 | u64 msr_host_kernel_gs_base; |
| 460 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 461 | #endif |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 462 | u32 vm_entry_controls_shadow; |
| 463 | u32 vm_exit_controls_shadow; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 464 | /* |
| 465 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 466 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 467 | * guest (L2), it points to a different VMCS. |
| 468 | */ |
| 469 | struct loaded_vmcs vmcs01; |
| 470 | struct loaded_vmcs *loaded_vmcs; |
| 471 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 472 | struct msr_autoload { |
| 473 | unsigned nr; |
| 474 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 475 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 476 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 477 | struct { |
| 478 | int loaded; |
| 479 | u16 fs_sel, gs_sel, ldt_sel; |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 480 | #ifdef CONFIG_X86_64 |
| 481 | u16 ds_sel, es_sel; |
| 482 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 483 | int gs_ldt_reload_needed; |
| 484 | int fs_reload_needed; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 485 | u64 msr_host_bndcfgs; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 486 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 487 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 488 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 489 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 490 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 491 | struct kvm_segment segs[8]; |
| 492 | } rmode; |
| 493 | struct { |
| 494 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 495 | struct kvm_save_segment { |
| 496 | u16 selector; |
| 497 | unsigned long base; |
| 498 | u32 limit; |
| 499 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 500 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 501 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 502 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 503 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 504 | |
| 505 | /* Support for vnmi-less CPUs */ |
| 506 | int soft_vnmi_blocked; |
| 507 | ktime_t entry_time; |
| 508 | s64 vnmi_blocked_time; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 509 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 510 | |
| 511 | bool rdtscp_enabled; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 512 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 513 | /* Posted interrupt descriptor */ |
| 514 | struct pi_desc pi_desc; |
| 515 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 516 | /* Support for a guest hypervisor (nested VMX) */ |
| 517 | struct nested_vmx nested; |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 518 | |
| 519 | /* Dynamic PLE window. */ |
| 520 | int ple_window; |
| 521 | bool ple_window_dirty; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 522 | |
| 523 | /* Support for PML */ |
| 524 | #define PML_ENTITY_NUM 512 |
| 525 | struct page *pml_pg; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 526 | }; |
| 527 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 528 | enum segment_cache_field { |
| 529 | SEG_FIELD_SEL = 0, |
| 530 | SEG_FIELD_BASE = 1, |
| 531 | SEG_FIELD_LIMIT = 2, |
| 532 | SEG_FIELD_AR = 3, |
| 533 | |
| 534 | SEG_FIELD_NR = 4 |
| 535 | }; |
| 536 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 537 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 538 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 539 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 540 | } |
| 541 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 542 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
| 543 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) |
| 544 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ |
| 545 | [number##_HIGH] = VMCS12_OFFSET(name)+4 |
| 546 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 547 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 548 | static unsigned long shadow_read_only_fields[] = { |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 549 | /* |
| 550 | * We do NOT shadow fields that are modified when L0 |
| 551 | * traps and emulates any vmx instruction (e.g. VMPTRLD, |
| 552 | * VMXON...) executed by L1. |
| 553 | * For example, VM_INSTRUCTION_ERROR is read |
| 554 | * by L1 if a vmx instruction fails (part of the error path). |
| 555 | * Note the code assumes this logic. If for some reason |
| 556 | * we start shadowing these fields then we need to |
| 557 | * force a shadow sync when L0 emulates vmx instructions |
| 558 | * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified |
| 559 | * by nested_vmx_failValid) |
| 560 | */ |
| 561 | VM_EXIT_REASON, |
| 562 | VM_EXIT_INTR_INFO, |
| 563 | VM_EXIT_INSTRUCTION_LEN, |
| 564 | IDT_VECTORING_INFO_FIELD, |
| 565 | IDT_VECTORING_ERROR_CODE, |
| 566 | VM_EXIT_INTR_ERROR_CODE, |
| 567 | EXIT_QUALIFICATION, |
| 568 | GUEST_LINEAR_ADDRESS, |
| 569 | GUEST_PHYSICAL_ADDRESS |
| 570 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 571 | static int max_shadow_read_only_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 572 | ARRAY_SIZE(shadow_read_only_fields); |
| 573 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 574 | static unsigned long shadow_read_write_fields[] = { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 575 | TPR_THRESHOLD, |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 576 | GUEST_RIP, |
| 577 | GUEST_RSP, |
| 578 | GUEST_CR0, |
| 579 | GUEST_CR3, |
| 580 | GUEST_CR4, |
| 581 | GUEST_INTERRUPTIBILITY_INFO, |
| 582 | GUEST_RFLAGS, |
| 583 | GUEST_CS_SELECTOR, |
| 584 | GUEST_CS_AR_BYTES, |
| 585 | GUEST_CS_LIMIT, |
| 586 | GUEST_CS_BASE, |
| 587 | GUEST_ES_BASE, |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 588 | GUEST_BNDCFGS, |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 589 | CR0_GUEST_HOST_MASK, |
| 590 | CR0_READ_SHADOW, |
| 591 | CR4_READ_SHADOW, |
| 592 | TSC_OFFSET, |
| 593 | EXCEPTION_BITMAP, |
| 594 | CPU_BASED_VM_EXEC_CONTROL, |
| 595 | VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 596 | VM_ENTRY_INTR_INFO_FIELD, |
| 597 | VM_ENTRY_INSTRUCTION_LEN, |
| 598 | VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 599 | HOST_FS_BASE, |
| 600 | HOST_GS_BASE, |
| 601 | HOST_FS_SELECTOR, |
| 602 | HOST_GS_SELECTOR |
| 603 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 604 | static int max_shadow_read_write_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 605 | ARRAY_SIZE(shadow_read_write_fields); |
| 606 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 607 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 608 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
| 609 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 610 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 611 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 612 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 613 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 614 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 615 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 616 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
| 617 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 618 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 619 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 620 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 621 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 622 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 623 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 624 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 625 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 626 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 627 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 628 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 629 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
| 630 | FIELD64(TSC_OFFSET, tsc_offset), |
| 631 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 632 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
| 633 | FIELD64(EPT_POINTER, ept_pointer), |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 634 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 635 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 636 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 637 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 638 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 639 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 640 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 641 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 642 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 643 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 644 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 645 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 646 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 647 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 648 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 649 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 650 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 651 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 652 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 653 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 654 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 655 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 656 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 657 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 658 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 659 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 660 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 661 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 662 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 663 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 664 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 665 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 666 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 667 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 668 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 669 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 670 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 671 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 672 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 673 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 674 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 675 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 676 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 677 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 678 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 679 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 680 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 681 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 682 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 683 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 684 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 685 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 686 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 687 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 688 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 689 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 690 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 691 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 692 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 693 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 694 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 695 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 696 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 697 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 698 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 699 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 700 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 701 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 702 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 703 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 704 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 705 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 706 | FIELD(GUEST_CR0, guest_cr0), |
| 707 | FIELD(GUEST_CR3, guest_cr3), |
| 708 | FIELD(GUEST_CR4, guest_cr4), |
| 709 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 710 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 711 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 712 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 713 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 714 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 715 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 716 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 717 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 718 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 719 | FIELD(GUEST_DR7, guest_dr7), |
| 720 | FIELD(GUEST_RSP, guest_rsp), |
| 721 | FIELD(GUEST_RIP, guest_rip), |
| 722 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 723 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 724 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 725 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 726 | FIELD(HOST_CR0, host_cr0), |
| 727 | FIELD(HOST_CR3, host_cr3), |
| 728 | FIELD(HOST_CR4, host_cr4), |
| 729 | FIELD(HOST_FS_BASE, host_fs_base), |
| 730 | FIELD(HOST_GS_BASE, host_gs_base), |
| 731 | FIELD(HOST_TR_BASE, host_tr_base), |
| 732 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 733 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 734 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 735 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 736 | FIELD(HOST_RSP, host_rsp), |
| 737 | FIELD(HOST_RIP, host_rip), |
| 738 | }; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 739 | |
| 740 | static inline short vmcs_field_to_offset(unsigned long field) |
| 741 | { |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 742 | BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX); |
| 743 | |
| 744 | if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) || |
| 745 | vmcs_field_to_offset_table[field] == 0) |
| 746 | return -ENOENT; |
| 747 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 748 | return vmcs_field_to_offset_table[field]; |
| 749 | } |
| 750 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 751 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 752 | { |
| 753 | return to_vmx(vcpu)->nested.current_vmcs12; |
| 754 | } |
| 755 | |
| 756 | static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr) |
| 757 | { |
| 758 | struct page *page = gfn_to_page(vcpu->kvm, addr >> PAGE_SHIFT); |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 759 | if (is_error_page(page)) |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 760 | return NULL; |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 761 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 762 | return page; |
| 763 | } |
| 764 | |
| 765 | static void nested_release_page(struct page *page) |
| 766 | { |
| 767 | kvm_release_page_dirty(page); |
| 768 | } |
| 769 | |
| 770 | static void nested_release_page_clean(struct page *page) |
| 771 | { |
| 772 | kvm_release_page_clean(page); |
| 773 | } |
| 774 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 775 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 776 | static u64 construct_eptp(unsigned long root_hpa); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 777 | static void kvm_cpu_vmxon(u64 addr); |
| 778 | static void kvm_cpu_vmxoff(void); |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 779 | static bool vmx_mpx_supported(void); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 780 | static bool vmx_xsaves_supported(void); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 781 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 782 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 783 | struct kvm_segment *var, int seg); |
| 784 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 785 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 786 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 787 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 788 | static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu); |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 789 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 790 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 791 | static int alloc_identity_pagetable(struct kvm *kvm); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 792 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 793 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 794 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 795 | /* |
| 796 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 797 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 798 | */ |
| 799 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 800 | static DEFINE_PER_CPU(struct desc_ptr, host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 801 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 802 | static unsigned long *vmx_io_bitmap_a; |
| 803 | static unsigned long *vmx_io_bitmap_b; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 804 | static unsigned long *vmx_msr_bitmap_legacy; |
| 805 | static unsigned long *vmx_msr_bitmap_longmode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 806 | static unsigned long *vmx_msr_bitmap_legacy_x2apic; |
| 807 | static unsigned long *vmx_msr_bitmap_longmode_x2apic; |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 808 | static unsigned long *vmx_vmread_bitmap; |
| 809 | static unsigned long *vmx_vmwrite_bitmap; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 810 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 811 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 812 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 813 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 814 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 815 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 816 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 817 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 818 | int size; |
| 819 | int order; |
| 820 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 821 | u32 pin_based_exec_ctrl; |
| 822 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 823 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 824 | u32 vmexit_ctrl; |
| 825 | u32 vmentry_ctrl; |
| 826 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 827 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 828 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 829 | u32 ept; |
| 830 | u32 vpid; |
| 831 | } vmx_capability; |
| 832 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 833 | #define VMX_SEGMENT_FIELD(seg) \ |
| 834 | [VCPU_SREG_##seg] = { \ |
| 835 | .selector = GUEST_##seg##_SELECTOR, \ |
| 836 | .base = GUEST_##seg##_BASE, \ |
| 837 | .limit = GUEST_##seg##_LIMIT, \ |
| 838 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 839 | } |
| 840 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 841 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 842 | unsigned selector; |
| 843 | unsigned base; |
| 844 | unsigned limit; |
| 845 | unsigned ar_bytes; |
| 846 | } kvm_vmx_segment_fields[] = { |
| 847 | VMX_SEGMENT_FIELD(CS), |
| 848 | VMX_SEGMENT_FIELD(DS), |
| 849 | VMX_SEGMENT_FIELD(ES), |
| 850 | VMX_SEGMENT_FIELD(FS), |
| 851 | VMX_SEGMENT_FIELD(GS), |
| 852 | VMX_SEGMENT_FIELD(SS), |
| 853 | VMX_SEGMENT_FIELD(TR), |
| 854 | VMX_SEGMENT_FIELD(LDTR), |
| 855 | }; |
| 856 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 857 | static u64 host_efer; |
| 858 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 859 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 860 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 861 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 862 | * 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] | 863 | * away by decrementing the array size. |
| 864 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 865 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 866 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 867 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 868 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 869 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 870 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 871 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 872 | static inline bool is_page_fault(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 873 | { |
| 874 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 875 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 876 | (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 877 | } |
| 878 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 879 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 880 | { |
| 881 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 882 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 883 | (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 884 | } |
| 885 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 886 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 887 | { |
| 888 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 889 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 890 | (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 891 | } |
| 892 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 893 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 894 | { |
| 895 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 896 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 897 | } |
| 898 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 899 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 900 | { |
| 901 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 902 | INTR_INFO_VALID_MASK)) == |
| 903 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 904 | } |
| 905 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 906 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 907 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 908 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 909 | } |
| 910 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 911 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 912 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 913 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 914 | } |
| 915 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 916 | static inline bool vm_need_tpr_shadow(struct kvm *kvm) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 917 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 918 | return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 919 | } |
| 920 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 921 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 922 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 923 | return vmcs_config.cpu_based_exec_ctrl & |
| 924 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 925 | } |
| 926 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 927 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 928 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 929 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 930 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 931 | } |
| 932 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 933 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 934 | { |
| 935 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 936 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 937 | } |
| 938 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 939 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 940 | { |
| 941 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 942 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 943 | } |
| 944 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 945 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 946 | { |
| 947 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 948 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 949 | } |
| 950 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 951 | static inline bool cpu_has_vmx_posted_intr(void) |
| 952 | { |
| 953 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; |
| 954 | } |
| 955 | |
| 956 | static inline bool cpu_has_vmx_apicv(void) |
| 957 | { |
| 958 | return cpu_has_vmx_apic_register_virt() && |
| 959 | cpu_has_vmx_virtual_intr_delivery() && |
| 960 | cpu_has_vmx_posted_intr(); |
| 961 | } |
| 962 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 963 | static inline bool cpu_has_vmx_flexpriority(void) |
| 964 | { |
| 965 | return cpu_has_vmx_tpr_shadow() && |
| 966 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 967 | } |
| 968 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 969 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 970 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 971 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 972 | } |
| 973 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 974 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 975 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 976 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 977 | } |
| 978 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 979 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 980 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 981 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 982 | } |
| 983 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 984 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 985 | { |
| 986 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 987 | } |
| 988 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 989 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 990 | { |
| 991 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 992 | } |
| 993 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 994 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 995 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 996 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 997 | } |
| 998 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 999 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1000 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1001 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1002 | } |
| 1003 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1004 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 1005 | { |
| 1006 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 1007 | } |
| 1008 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1009 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 1010 | { |
| 1011 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 1012 | } |
| 1013 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1014 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1015 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1016 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1017 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1018 | } |
| 1019 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1020 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1021 | { |
| 1022 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1023 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 1024 | } |
| 1025 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1026 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1027 | { |
| 1028 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1029 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1030 | } |
| 1031 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1032 | static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1033 | { |
Gui Jianfeng | 6d3e435 | 2010-01-29 15:36:59 +0800 | [diff] [blame] | 1034 | return flexpriority_enabled && irqchip_in_kernel(kvm); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1035 | } |
| 1036 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1037 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1038 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1039 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1040 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1041 | } |
| 1042 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1043 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1044 | { |
| 1045 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1046 | SECONDARY_EXEC_RDTSCP; |
| 1047 | } |
| 1048 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1049 | static inline bool cpu_has_vmx_invpcid(void) |
| 1050 | { |
| 1051 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1052 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 1053 | } |
| 1054 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1055 | static inline bool cpu_has_virtual_nmis(void) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 1056 | { |
| 1057 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 1058 | } |
| 1059 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1060 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 1061 | { |
| 1062 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1063 | SECONDARY_EXEC_WBINVD_EXITING; |
| 1064 | } |
| 1065 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1066 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
| 1067 | { |
| 1068 | u64 vmx_msr; |
| 1069 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 1070 | /* check if the cpu supports writing r/o exit information fields */ |
| 1071 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) |
| 1072 | return false; |
| 1073 | |
| 1074 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1075 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1076 | } |
| 1077 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 1078 | static inline bool cpu_has_vmx_pml(void) |
| 1079 | { |
| 1080 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; |
| 1081 | } |
| 1082 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1083 | static inline bool report_flexpriority(void) |
| 1084 | { |
| 1085 | return flexpriority_enabled; |
| 1086 | } |
| 1087 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1088 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 1089 | { |
| 1090 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 1091 | } |
| 1092 | |
| 1093 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 1094 | { |
| 1095 | return (vmcs12->cpu_based_vm_exec_control & |
| 1096 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 1097 | (vmcs12->secondary_vm_exec_control & bit); |
| 1098 | } |
| 1099 | |
Nadav Har'El | f5c4368 | 2013-08-05 11:07:20 +0300 | [diff] [blame] | 1100 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1101 | { |
| 1102 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 1103 | } |
| 1104 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 1105 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
| 1106 | { |
| 1107 | return vmcs12->pin_based_vm_exec_control & |
| 1108 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1109 | } |
| 1110 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 1111 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
| 1112 | { |
| 1113 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); |
| 1114 | } |
| 1115 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1116 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
| 1117 | { |
| 1118 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) && |
| 1119 | vmx_xsaves_supported(); |
| 1120 | } |
| 1121 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1122 | static inline bool is_exception(u32 intr_info) |
| 1123 | { |
| 1124 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1125 | == (INTR_TYPE_HARD_EXCEPTION | INTR_INFO_VALID_MASK); |
| 1126 | } |
| 1127 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 1128 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 1129 | u32 exit_intr_info, |
| 1130 | unsigned long exit_qualification); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 1131 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 1132 | struct vmcs12 *vmcs12, |
| 1133 | u32 reason, unsigned long qualification); |
| 1134 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1135 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1136 | { |
| 1137 | int i; |
| 1138 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1139 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1140 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1141 | return i; |
| 1142 | return -1; |
| 1143 | } |
| 1144 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1145 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 1146 | { |
| 1147 | struct { |
| 1148 | u64 vpid : 16; |
| 1149 | u64 rsvd : 48; |
| 1150 | u64 gva; |
| 1151 | } operand = { vpid, 0, gva }; |
| 1152 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1153 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1154 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1155 | "; ja 1f ; ud2 ; 1:" |
| 1156 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 1157 | } |
| 1158 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1159 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 1160 | { |
| 1161 | struct { |
| 1162 | u64 eptp, gpa; |
| 1163 | } operand = {eptp, gpa}; |
| 1164 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1165 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1166 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1167 | "; ja 1f ; ud2 ; 1:\n" |
| 1168 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 1169 | } |
| 1170 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1171 | 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] | 1172 | { |
| 1173 | int i; |
| 1174 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1175 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1176 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1177 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1178 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1181 | static void vmcs_clear(struct vmcs *vmcs) |
| 1182 | { |
| 1183 | u64 phys_addr = __pa(vmcs); |
| 1184 | u8 error; |
| 1185 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1186 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1187 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1188 | : "cc", "memory"); |
| 1189 | if (error) |
| 1190 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 1191 | vmcs, phys_addr); |
| 1192 | } |
| 1193 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1194 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 1195 | { |
| 1196 | vmcs_clear(loaded_vmcs->vmcs); |
| 1197 | loaded_vmcs->cpu = -1; |
| 1198 | loaded_vmcs->launched = 0; |
| 1199 | } |
| 1200 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1201 | static void vmcs_load(struct vmcs *vmcs) |
| 1202 | { |
| 1203 | u64 phys_addr = __pa(vmcs); |
| 1204 | u8 error; |
| 1205 | |
| 1206 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1207 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1208 | : "cc", "memory"); |
| 1209 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 1210 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1211 | vmcs, phys_addr); |
| 1212 | } |
| 1213 | |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1214 | #ifdef CONFIG_KEXEC |
| 1215 | /* |
| 1216 | * This bitmap is used to indicate whether the vmclear |
| 1217 | * operation is enabled on all cpus. All disabled by |
| 1218 | * default. |
| 1219 | */ |
| 1220 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 1221 | |
| 1222 | static inline void crash_enable_local_vmclear(int cpu) |
| 1223 | { |
| 1224 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1225 | } |
| 1226 | |
| 1227 | static inline void crash_disable_local_vmclear(int cpu) |
| 1228 | { |
| 1229 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1230 | } |
| 1231 | |
| 1232 | static inline int crash_local_vmclear_enabled(int cpu) |
| 1233 | { |
| 1234 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1235 | } |
| 1236 | |
| 1237 | static void crash_vmclear_local_loaded_vmcss(void) |
| 1238 | { |
| 1239 | int cpu = raw_smp_processor_id(); |
| 1240 | struct loaded_vmcs *v; |
| 1241 | |
| 1242 | if (!crash_local_vmclear_enabled(cpu)) |
| 1243 | return; |
| 1244 | |
| 1245 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 1246 | loaded_vmcss_on_cpu_link) |
| 1247 | vmcs_clear(v->vmcs); |
| 1248 | } |
| 1249 | #else |
| 1250 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 1251 | static inline void crash_disable_local_vmclear(int cpu) { } |
| 1252 | #endif /* CONFIG_KEXEC */ |
| 1253 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1254 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1255 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1256 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1257 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1258 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1259 | if (loaded_vmcs->cpu != cpu) |
| 1260 | return; /* vcpu migration can race with cpu offline */ |
| 1261 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1262 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1263 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1264 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1265 | |
| 1266 | /* |
| 1267 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 1268 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 1269 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 1270 | * then adds the vmcs into percpu list before it is deleted. |
| 1271 | */ |
| 1272 | smp_wmb(); |
| 1273 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1274 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1275 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1276 | } |
| 1277 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1278 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1279 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 1280 | int cpu = loaded_vmcs->cpu; |
| 1281 | |
| 1282 | if (cpu != -1) |
| 1283 | smp_call_function_single(cpu, |
| 1284 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
Gui Jianfeng | 1760dd4 | 2010-06-07 10:33:27 +0800 | [diff] [blame] | 1287 | static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1288 | { |
| 1289 | if (vmx->vpid == 0) |
| 1290 | return; |
| 1291 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1292 | if (cpu_has_vmx_invvpid_single()) |
| 1293 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1294 | } |
| 1295 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1296 | static inline void vpid_sync_vcpu_global(void) |
| 1297 | { |
| 1298 | if (cpu_has_vmx_invvpid_global()) |
| 1299 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 1300 | } |
| 1301 | |
| 1302 | static inline void vpid_sync_context(struct vcpu_vmx *vmx) |
| 1303 | { |
| 1304 | if (cpu_has_vmx_invvpid_single()) |
Gui Jianfeng | 1760dd4 | 2010-06-07 10:33:27 +0800 | [diff] [blame] | 1305 | vpid_sync_vcpu_single(vmx); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1306 | else |
| 1307 | vpid_sync_vcpu_global(); |
| 1308 | } |
| 1309 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1310 | static inline void ept_sync_global(void) |
| 1311 | { |
| 1312 | if (cpu_has_vmx_invept_global()) |
| 1313 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
| 1314 | } |
| 1315 | |
| 1316 | static inline void ept_sync_context(u64 eptp) |
| 1317 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1318 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1319 | if (cpu_has_vmx_invept_context()) |
| 1320 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 1321 | else |
| 1322 | ept_sync_global(); |
| 1323 | } |
| 1324 | } |
| 1325 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1326 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1327 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1328 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1329 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1330 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 1331 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1332 | return value; |
| 1333 | } |
| 1334 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1335 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1336 | { |
| 1337 | return vmcs_readl(field); |
| 1338 | } |
| 1339 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1340 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1341 | { |
| 1342 | return vmcs_readl(field); |
| 1343 | } |
| 1344 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1345 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1346 | { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1347 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1348 | return vmcs_readl(field); |
| 1349 | #else |
| 1350 | return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32); |
| 1351 | #endif |
| 1352 | } |
| 1353 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1354 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 1355 | { |
| 1356 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 1357 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 1358 | dump_stack(); |
| 1359 | } |
| 1360 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1361 | static void vmcs_writel(unsigned long field, unsigned long value) |
| 1362 | { |
| 1363 | u8 error; |
| 1364 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1365 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1366 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1367 | if (unlikely(error)) |
| 1368 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | static void vmcs_write16(unsigned long field, u16 value) |
| 1372 | { |
| 1373 | vmcs_writel(field, value); |
| 1374 | } |
| 1375 | |
| 1376 | static void vmcs_write32(unsigned long field, u32 value) |
| 1377 | { |
| 1378 | vmcs_writel(field, value); |
| 1379 | } |
| 1380 | |
| 1381 | static void vmcs_write64(unsigned long field, u64 value) |
| 1382 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1383 | vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 1384 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1385 | asm volatile (""); |
| 1386 | vmcs_writel(field+1, value >> 32); |
| 1387 | #endif |
| 1388 | } |
| 1389 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1390 | static void vmcs_clear_bits(unsigned long field, u32 mask) |
| 1391 | { |
| 1392 | vmcs_writel(field, vmcs_readl(field) & ~mask); |
| 1393 | } |
| 1394 | |
| 1395 | static void vmcs_set_bits(unsigned long field, u32 mask) |
| 1396 | { |
| 1397 | vmcs_writel(field, vmcs_readl(field) | mask); |
| 1398 | } |
| 1399 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1400 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1401 | { |
| 1402 | vmcs_write32(VM_ENTRY_CONTROLS, val); |
| 1403 | vmx->vm_entry_controls_shadow = val; |
| 1404 | } |
| 1405 | |
| 1406 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1407 | { |
| 1408 | if (vmx->vm_entry_controls_shadow != val) |
| 1409 | vm_entry_controls_init(vmx, val); |
| 1410 | } |
| 1411 | |
| 1412 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) |
| 1413 | { |
| 1414 | return vmx->vm_entry_controls_shadow; |
| 1415 | } |
| 1416 | |
| 1417 | |
| 1418 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 1419 | { |
| 1420 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); |
| 1421 | } |
| 1422 | |
| 1423 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 1424 | { |
| 1425 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); |
| 1426 | } |
| 1427 | |
| 1428 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1429 | { |
| 1430 | vmcs_write32(VM_EXIT_CONTROLS, val); |
| 1431 | vmx->vm_exit_controls_shadow = val; |
| 1432 | } |
| 1433 | |
| 1434 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1435 | { |
| 1436 | if (vmx->vm_exit_controls_shadow != val) |
| 1437 | vm_exit_controls_init(vmx, val); |
| 1438 | } |
| 1439 | |
| 1440 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) |
| 1441 | { |
| 1442 | return vmx->vm_exit_controls_shadow; |
| 1443 | } |
| 1444 | |
| 1445 | |
| 1446 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 1447 | { |
| 1448 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); |
| 1449 | } |
| 1450 | |
| 1451 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 1452 | { |
| 1453 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); |
| 1454 | } |
| 1455 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1456 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 1457 | { |
| 1458 | vmx->segment_cache.bitmask = 0; |
| 1459 | } |
| 1460 | |
| 1461 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 1462 | unsigned field) |
| 1463 | { |
| 1464 | bool ret; |
| 1465 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 1466 | |
| 1467 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 1468 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 1469 | vmx->segment_cache.bitmask = 0; |
| 1470 | } |
| 1471 | ret = vmx->segment_cache.bitmask & mask; |
| 1472 | vmx->segment_cache.bitmask |= mask; |
| 1473 | return ret; |
| 1474 | } |
| 1475 | |
| 1476 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 1477 | { |
| 1478 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 1479 | |
| 1480 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 1481 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 1482 | return *p; |
| 1483 | } |
| 1484 | |
| 1485 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 1486 | { |
| 1487 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 1488 | |
| 1489 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 1490 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 1491 | return *p; |
| 1492 | } |
| 1493 | |
| 1494 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 1495 | { |
| 1496 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 1497 | |
| 1498 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 1499 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 1500 | return *p; |
| 1501 | } |
| 1502 | |
| 1503 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 1504 | { |
| 1505 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 1506 | |
| 1507 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 1508 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 1509 | return *p; |
| 1510 | } |
| 1511 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1512 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 1513 | { |
| 1514 | u32 eb; |
| 1515 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1516 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
| 1517 | (1u << NM_VECTOR) | (1u << DB_VECTOR); |
| 1518 | if ((vcpu->guest_debug & |
| 1519 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 1520 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 1521 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1522 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1523 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1524 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1525 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1526 | if (vcpu->fpu_active) |
| 1527 | eb &= ~(1u << NM_VECTOR); |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1528 | |
| 1529 | /* When we are running a nested L2 guest and L1 specified for it a |
| 1530 | * certain exception bitmap, we must trap the same exceptions and pass |
| 1531 | * them to L1. When running L2, we will only handle the exceptions |
| 1532 | * specified above if L1 did not want them. |
| 1533 | */ |
| 1534 | if (is_guest_mode(vcpu)) |
| 1535 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 1536 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1537 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 1538 | } |
| 1539 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1540 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 1541 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1542 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1543 | vm_entry_controls_clearbit(vmx, entry); |
| 1544 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1545 | } |
| 1546 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1547 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 1548 | { |
| 1549 | unsigned i; |
| 1550 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1551 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1552 | switch (msr) { |
| 1553 | case MSR_EFER: |
| 1554 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1555 | clear_atomic_switch_msr_special(vmx, |
| 1556 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1557 | VM_EXIT_LOAD_IA32_EFER); |
| 1558 | return; |
| 1559 | } |
| 1560 | break; |
| 1561 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1562 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1563 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1564 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1565 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 1566 | return; |
| 1567 | } |
| 1568 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1569 | } |
| 1570 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1571 | for (i = 0; i < m->nr; ++i) |
| 1572 | if (m->guest[i].index == msr) |
| 1573 | break; |
| 1574 | |
| 1575 | if (i == m->nr) |
| 1576 | return; |
| 1577 | --m->nr; |
| 1578 | m->guest[i] = m->guest[m->nr]; |
| 1579 | m->host[i] = m->host[m->nr]; |
| 1580 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1581 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1582 | } |
| 1583 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1584 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 1585 | unsigned long entry, unsigned long exit, |
| 1586 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 1587 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1588 | { |
| 1589 | vmcs_write64(guest_val_vmcs, guest_val); |
| 1590 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1591 | vm_entry_controls_setbit(vmx, entry); |
| 1592 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1593 | } |
| 1594 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1595 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 1596 | u64 guest_val, u64 host_val) |
| 1597 | { |
| 1598 | unsigned i; |
| 1599 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1600 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1601 | switch (msr) { |
| 1602 | case MSR_EFER: |
| 1603 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1604 | add_atomic_switch_msr_special(vmx, |
| 1605 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1606 | VM_EXIT_LOAD_IA32_EFER, |
| 1607 | GUEST_IA32_EFER, |
| 1608 | HOST_IA32_EFER, |
| 1609 | guest_val, host_val); |
| 1610 | return; |
| 1611 | } |
| 1612 | break; |
| 1613 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1614 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1615 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1616 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1617 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1618 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 1619 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 1620 | guest_val, host_val); |
| 1621 | return; |
| 1622 | } |
| 1623 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1624 | } |
| 1625 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1626 | for (i = 0; i < m->nr; ++i) |
| 1627 | if (m->guest[i].index == msr) |
| 1628 | break; |
| 1629 | |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 1630 | if (i == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 1631 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 1632 | "Can't add msr %x\n", msr); |
| 1633 | return; |
| 1634 | } else if (i == m->nr) { |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1635 | ++m->nr; |
| 1636 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1637 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1638 | } |
| 1639 | |
| 1640 | m->guest[i].index = msr; |
| 1641 | m->guest[i].value = guest_val; |
| 1642 | m->host[i].index = msr; |
| 1643 | m->host[i].value = host_val; |
| 1644 | } |
| 1645 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1646 | static void reload_tss(void) |
| 1647 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1648 | /* |
| 1649 | * VT restores TR but not its size. Useless. |
| 1650 | */ |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1651 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 1652 | struct desc_struct *descs; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1653 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1654 | descs = (void *)gdt->address; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1655 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 1656 | load_TR_desc(); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1657 | } |
| 1658 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1659 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1660 | { |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 1661 | u64 guest_efer; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1662 | u64 ignore_bits; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1663 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1664 | guest_efer = vmx->vcpu.arch.efer; |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 1665 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1666 | /* |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 1667 | * NX is emulated; LMA and LME handled by hardware; SCE meaningless |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1668 | * outside long mode |
| 1669 | */ |
| 1670 | ignore_bits = EFER_NX | EFER_SCE; |
| 1671 | #ifdef CONFIG_X86_64 |
| 1672 | ignore_bits |= EFER_LMA | EFER_LME; |
| 1673 | /* SCE is meaningful only in long mode on Intel */ |
| 1674 | if (guest_efer & EFER_LMA) |
| 1675 | ignore_bits &= ~(u64)EFER_SCE; |
| 1676 | #endif |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1677 | guest_efer &= ~ignore_bits; |
| 1678 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1679 | vmx->guest_msrs[efer_offset].data = guest_efer; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 1680 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 1681 | |
| 1682 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 1683 | |
| 1684 | /* |
| 1685 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 1686 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 1687 | * atomically, since it's faster than switching it manually. |
| 1688 | */ |
| 1689 | if (cpu_has_load_ia32_efer || |
| 1690 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 1691 | guest_efer = vmx->vcpu.arch.efer; |
| 1692 | if (!(guest_efer & EFER_LMA)) |
| 1693 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 1694 | if (guest_efer != host_efer) |
| 1695 | add_atomic_switch_msr(vmx, MSR_EFER, |
| 1696 | guest_efer, host_efer); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 1697 | return false; |
| 1698 | } |
| 1699 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1700 | return true; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1701 | } |
| 1702 | |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1703 | static unsigned long segment_base(u16 selector) |
| 1704 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1705 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1706 | struct desc_struct *d; |
| 1707 | unsigned long table_base; |
| 1708 | unsigned long v; |
| 1709 | |
| 1710 | if (!(selector & ~3)) |
| 1711 | return 0; |
| 1712 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1713 | table_base = gdt->address; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 1714 | |
| 1715 | if (selector & 4) { /* from ldt */ |
| 1716 | u16 ldt_selector = kvm_read_ldt(); |
| 1717 | |
| 1718 | if (!(ldt_selector & ~3)) |
| 1719 | return 0; |
| 1720 | |
| 1721 | table_base = segment_base(ldt_selector); |
| 1722 | } |
| 1723 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
| 1724 | v = get_desc_base(d); |
| 1725 | #ifdef CONFIG_X86_64 |
| 1726 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 1727 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; |
| 1728 | #endif |
| 1729 | return v; |
| 1730 | } |
| 1731 | |
| 1732 | static inline unsigned long kvm_read_tr_base(void) |
| 1733 | { |
| 1734 | u16 tr; |
| 1735 | asm("str %0" : "=g"(tr)); |
| 1736 | return segment_base(tr); |
| 1737 | } |
| 1738 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1739 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1740 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1741 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1742 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1743 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1744 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1745 | return; |
| 1746 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1747 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1748 | /* |
| 1749 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 1750 | * allow segment selectors with cpl > 0 or ti == 1. |
| 1751 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1752 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1753 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1754 | savesegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1755 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1756 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1757 | vmx->host_state.fs_reload_needed = 0; |
| 1758 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1759 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1760 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1761 | } |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1762 | savesegment(gs, vmx->host_state.gs_sel); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1763 | if (!(vmx->host_state.gs_sel & 7)) |
| 1764 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1765 | else { |
| 1766 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1767 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | #ifdef CONFIG_X86_64 |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1771 | savesegment(ds, vmx->host_state.ds_sel); |
| 1772 | savesegment(es, vmx->host_state.es_sel); |
| 1773 | #endif |
| 1774 | |
| 1775 | #ifdef CONFIG_X86_64 |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1776 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 1777 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 1778 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1779 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 1780 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1781 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 1782 | |
| 1783 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1784 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
| 1785 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1786 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 1787 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 1788 | if (boot_cpu_has(X86_FEATURE_MPX)) |
| 1789 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1790 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 1791 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 1792 | vmx->guest_msrs[i].data, |
| 1793 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1794 | } |
| 1795 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1796 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1797 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1798 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1799 | return; |
| 1800 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 1801 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1802 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1803 | #ifdef CONFIG_X86_64 |
| 1804 | if (is_long_mode(&vmx->vcpu)) |
| 1805 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 1806 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1807 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1808 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1809 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1810 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1811 | #else |
| 1812 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1813 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1814 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 1815 | if (vmx->host_state.fs_reload_needed) |
| 1816 | loadsegment(fs, vmx->host_state.fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1817 | #ifdef CONFIG_X86_64 |
| 1818 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { |
| 1819 | loadsegment(ds, vmx->host_state.ds_sel); |
| 1820 | loadsegment(es, vmx->host_state.es_sel); |
| 1821 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1822 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 1823 | reload_tss(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1824 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1825 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1826 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 1827 | if (vmx->host_state.msr_host_bndcfgs) |
| 1828 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 1829 | /* |
| 1830 | * If the FPU is not active (through the host task or |
| 1831 | * the guest vcpu), then restore the cr0.TS bit. |
| 1832 | */ |
| 1833 | if (!user_has_fpu() && !vmx->vcpu.guest_fpu_loaded) |
| 1834 | stts(); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1835 | load_gdt(this_cpu_ptr(&host_gdt)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1836 | } |
| 1837 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1838 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 1839 | { |
| 1840 | preempt_disable(); |
| 1841 | __vmx_load_host_state(vmx); |
| 1842 | preempt_enable(); |
| 1843 | } |
| 1844 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1845 | /* |
| 1846 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 1847 | * vcpu mutex is already taken. |
| 1848 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 1849 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1850 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1851 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1852 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1853 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1854 | if (!vmm_exclusive) |
| 1855 | kvm_cpu_vmxon(phys_addr); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1856 | else if (vmx->loaded_vmcs->cpu != cpu) |
| 1857 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1858 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1859 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 1860 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 1861 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1862 | } |
| 1863 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1864 | if (vmx->loaded_vmcs->cpu != cpu) { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1865 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1866 | unsigned long sysenter_esp; |
| 1867 | |
Avi Kivity | a8eeb04 | 2010-05-10 12:34:53 +0300 | [diff] [blame] | 1868 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1869 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1870 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1871 | |
| 1872 | /* |
| 1873 | * Read loaded_vmcs->cpu should be before fetching |
| 1874 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 1875 | * See the comments in __loaded_vmcs_clear(). |
| 1876 | */ |
| 1877 | smp_rmb(); |
| 1878 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1879 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 1880 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1881 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1882 | local_irq_enable(); |
| 1883 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1884 | /* |
| 1885 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 1886 | * processors. |
| 1887 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 1888 | vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1889 | vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1890 | |
| 1891 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 1892 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1893 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1894 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 1898 | { |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1899 | __vmx_load_host_state(to_vmx(vcpu)); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1900 | if (!vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1901 | __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs); |
| 1902 | vcpu->cpu = -1; |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 1903 | kvm_cpu_vmxoff(); |
| 1904 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1905 | } |
| 1906 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1907 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 1908 | { |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1909 | ulong cr0; |
| 1910 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1911 | if (vcpu->fpu_active) |
| 1912 | return; |
| 1913 | vcpu->fpu_active = 1; |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1914 | cr0 = vmcs_readl(GUEST_CR0); |
| 1915 | cr0 &= ~(X86_CR0_TS | X86_CR0_MP); |
| 1916 | cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP); |
| 1917 | vmcs_writel(GUEST_CR0, cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1918 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1919 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1920 | if (is_guest_mode(vcpu)) |
| 1921 | vcpu->arch.cr0_guest_owned_bits &= |
| 1922 | ~get_vmcs12(vcpu)->cr0_guest_host_mask; |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1923 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1924 | } |
| 1925 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1926 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 1927 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1928 | /* |
| 1929 | * Return the cr0 value that a nested guest would read. This is a combination |
| 1930 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 1931 | * its hypervisor (cr0_read_shadow). |
| 1932 | */ |
| 1933 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 1934 | { |
| 1935 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 1936 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 1937 | } |
| 1938 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 1939 | { |
| 1940 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 1941 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 1942 | } |
| 1943 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1944 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 1945 | { |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1946 | /* Note that there is no vcpu->fpu_active = 0 here. The caller must |
| 1947 | * set this *before* calling this function. |
| 1948 | */ |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1949 | vmx_decache_cr0_guest_bits(vcpu); |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 1950 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1951 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1952 | vcpu->arch.cr0_guest_owned_bits = 0; |
| 1953 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1954 | if (is_guest_mode(vcpu)) { |
| 1955 | /* |
| 1956 | * L1's specified read shadow might not contain the TS bit, |
| 1957 | * so now that we turned on shadowing of this bit, we need to |
| 1958 | * set this bit of the shadow. Like in nested_vmx_run we need |
| 1959 | * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet |
| 1960 | * up-to-date here because we just decached cr0.TS (and we'll |
| 1961 | * only update vmcs12->guest_cr0 on nested exit). |
| 1962 | */ |
| 1963 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1964 | vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) | |
| 1965 | (vcpu->arch.cr0 & X86_CR0_TS); |
| 1966 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 1967 | } else |
| 1968 | vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1969 | } |
| 1970 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1971 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 1972 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1973 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 1974 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1975 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 1976 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 1977 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 1978 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1979 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1980 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 1981 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1982 | } |
| 1983 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1984 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1985 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 1989 | { |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1990 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 1991 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1992 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1993 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1994 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1995 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1996 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 1997 | } |
| 1998 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 1999 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2000 | { |
| 2001 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2002 | int ret = 0; |
| 2003 | |
| 2004 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2005 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2006 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2007 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2008 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2009 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 2013 | { |
| 2014 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2015 | u32 interruptibility = interruptibility_old; |
| 2016 | |
| 2017 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 2018 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2019 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2020 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2021 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2022 | interruptibility |= GUEST_INTR_STATE_STI; |
| 2023 | |
| 2024 | if ((interruptibility != interruptibility_old)) |
| 2025 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 2026 | } |
| 2027 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2028 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 2029 | { |
| 2030 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2031 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2032 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2033 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2034 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2035 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2036 | /* skipping an emulated instruction also counts */ |
| 2037 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2038 | } |
| 2039 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2040 | /* |
| 2041 | * KVM wants to inject page-faults which it got to the guest. This function |
| 2042 | * checks whether in a nested guest, we need to inject them to L1 or L2. |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2043 | */ |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2044 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned nr) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2045 | { |
| 2046 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2047 | |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2048 | if (!(vmcs12->exception_bitmap & (1u << nr))) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2049 | return 0; |
| 2050 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 2051 | nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason, |
| 2052 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 2053 | vmcs_readl(EXIT_QUALIFICATION)); |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2054 | return 1; |
| 2055 | } |
| 2056 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2057 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 2058 | bool has_error_code, u32 error_code, |
| 2059 | bool reinject) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2060 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2061 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2062 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2063 | |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2064 | if (!reinject && is_guest_mode(vcpu) && |
| 2065 | nested_vmx_check_exception(vcpu, nr)) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2066 | return; |
| 2067 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2068 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2069 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2070 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 2071 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2072 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2073 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 2074 | int inc_eip = 0; |
| 2075 | if (kvm_exception_is_soft(nr)) |
| 2076 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 2077 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 2078 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2079 | return; |
| 2080 | } |
| 2081 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2082 | if (kvm_exception_is_soft(nr)) { |
| 2083 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2084 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2085 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 2086 | } else |
| 2087 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 2088 | |
| 2089 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2090 | } |
| 2091 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2092 | static bool vmx_rdtscp_supported(void) |
| 2093 | { |
| 2094 | return cpu_has_vmx_rdtscp(); |
| 2095 | } |
| 2096 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 2097 | static bool vmx_invpcid_supported(void) |
| 2098 | { |
| 2099 | return cpu_has_vmx_invpcid() && enable_ept; |
| 2100 | } |
| 2101 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2102 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2103 | * Swap MSR entry in host/guest MSR entry array. |
| 2104 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2105 | 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] | 2106 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2107 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2108 | |
| 2109 | tmp = vmx->guest_msrs[to]; |
| 2110 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 2111 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2112 | } |
| 2113 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2114 | static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu) |
| 2115 | { |
| 2116 | unsigned long *msr_bitmap; |
| 2117 | |
| 2118 | if (irqchip_in_kernel(vcpu->kvm) && apic_x2apic_mode(vcpu->arch.apic)) { |
| 2119 | if (is_long_mode(vcpu)) |
| 2120 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic; |
| 2121 | else |
| 2122 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic; |
| 2123 | } else { |
| 2124 | if (is_long_mode(vcpu)) |
| 2125 | msr_bitmap = vmx_msr_bitmap_longmode; |
| 2126 | else |
| 2127 | msr_bitmap = vmx_msr_bitmap_legacy; |
| 2128 | } |
| 2129 | |
| 2130 | vmcs_write64(MSR_BITMAP, __pa(msr_bitmap)); |
| 2131 | } |
| 2132 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2133 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2134 | * Set up the vmcs to automatically save and restore system |
| 2135 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 2136 | * mode, as fiddling with msrs is very expensive. |
| 2137 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2138 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2139 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2140 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2141 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2142 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2143 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2144 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2145 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2146 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2147 | move_msr_up(vmx, index, save_nmsrs++); |
| 2148 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2149 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2150 | move_msr_up(vmx, index, save_nmsrs++); |
| 2151 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2152 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2153 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2154 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
| 2155 | if (index >= 0 && vmx->rdtscp_enabled) |
| 2156 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2157 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2158 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2159 | * if efer.sce is enabled. |
| 2160 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2161 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2162 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2163 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2164 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2165 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2166 | index = __find_msr_index(vmx, MSR_EFER); |
| 2167 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2168 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2169 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2170 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 2171 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2172 | if (cpu_has_vmx_msr_bitmap()) |
| 2173 | vmx_set_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2177 | * reads and returns guest's timestamp counter "register" |
| 2178 | * guest_tsc = host_tsc + tsc_offset -- 21.3 |
| 2179 | */ |
| 2180 | static u64 guest_read_tsc(void) |
| 2181 | { |
| 2182 | u64 host_tsc, tsc_offset; |
| 2183 | |
| 2184 | rdtscll(host_tsc); |
| 2185 | tsc_offset = vmcs_read64(TSC_OFFSET); |
| 2186 | return host_tsc + tsc_offset; |
| 2187 | } |
| 2188 | |
| 2189 | /* |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 2190 | * Like guest_read_tsc, but always returns L1's notion of the timestamp |
| 2191 | * counter, even if a nested guest (L2) is currently running. |
| 2192 | */ |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 2193 | static u64 vmx_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 2194 | { |
Marcelo Tosatti | 886b470 | 2012-11-27 23:28:58 -0200 | [diff] [blame] | 2195 | u64 tsc_offset; |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 2196 | |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 2197 | tsc_offset = is_guest_mode(vcpu) ? |
| 2198 | to_vmx(vcpu)->nested.vmcs01_tsc_offset : |
| 2199 | vmcs_read64(TSC_OFFSET); |
| 2200 | return host_tsc + tsc_offset; |
| 2201 | } |
| 2202 | |
| 2203 | /* |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 2204 | * Engage any workarounds for mis-matched TSC rates. Currently limited to |
| 2205 | * software catchup for faster rates on slower CPUs. |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 2206 | */ |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 2207 | static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale) |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 2208 | { |
Zachary Amsden | cc57828 | 2012-02-03 15:43:50 -0200 | [diff] [blame] | 2209 | if (!scale) |
| 2210 | return; |
| 2211 | |
| 2212 | if (user_tsc_khz > tsc_khz) { |
| 2213 | vcpu->arch.tsc_catchup = 1; |
| 2214 | vcpu->arch.tsc_always_catchup = 1; |
| 2215 | } else |
| 2216 | WARN(1, "user requested TSC rate below hardware speed\n"); |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 2217 | } |
| 2218 | |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 2219 | static u64 vmx_read_tsc_offset(struct kvm_vcpu *vcpu) |
| 2220 | { |
| 2221 | return vmcs_read64(TSC_OFFSET); |
| 2222 | } |
| 2223 | |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 2224 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2225 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2226 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2227 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2228 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2229 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2230 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2231 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 2232 | * to the spec, this should set L1's TSC; The offset that L1 |
| 2233 | * set for L2 remains unchanged, and still needs to be added |
| 2234 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2235 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2236 | struct vmcs12 *vmcs12; |
| 2237 | to_vmx(vcpu)->nested.vmcs01_tsc_offset = offset; |
| 2238 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 2239 | vmcs12 = get_vmcs12(vcpu); |
| 2240 | vmcs_write64(TSC_OFFSET, offset + |
| 2241 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 2242 | vmcs12->tsc_offset : 0)); |
| 2243 | } else { |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 2244 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 2245 | vmcs_read64(TSC_OFFSET), offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2246 | vmcs_write64(TSC_OFFSET, offset); |
| 2247 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2248 | } |
| 2249 | |
Marcelo Tosatti | f1e2b26 | 2012-02-03 15:43:55 -0200 | [diff] [blame] | 2250 | static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host) |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 2251 | { |
| 2252 | u64 offset = vmcs_read64(TSC_OFFSET); |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 2253 | |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 2254 | vmcs_write64(TSC_OFFSET, offset + adjustment); |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2255 | if (is_guest_mode(vcpu)) { |
| 2256 | /* Even when running L2, the adjustment needs to apply to L1 */ |
| 2257 | to_vmx(vcpu)->nested.vmcs01_tsc_offset += adjustment; |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 2258 | } else |
| 2259 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, offset, |
| 2260 | offset + adjustment); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 2261 | } |
| 2262 | |
Joerg Roedel | 857e409 | 2011-03-25 09:44:50 +0100 | [diff] [blame] | 2263 | static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) |
| 2264 | { |
| 2265 | return target_tsc - native_read_tsc(); |
| 2266 | } |
| 2267 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 2268 | static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu) |
| 2269 | { |
| 2270 | struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 2271 | return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31))); |
| 2272 | } |
| 2273 | |
| 2274 | /* |
| 2275 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 2276 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 2277 | * all guests if the "nested" module option is off, and can also be disabled |
| 2278 | * for a single guest by disabling its VMX cpuid bit. |
| 2279 | */ |
| 2280 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 2281 | { |
| 2282 | return nested && guest_cpuid_has_vmx(vcpu); |
| 2283 | } |
| 2284 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2285 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2286 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 2287 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 2288 | * The same values should also be used to verify that vmcs12 control fields are |
| 2289 | * valid during nested entry from L1 to L2. |
| 2290 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 2291 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 2292 | * bit in the high half is on if the corresponding bit in the control field |
| 2293 | * may be on. See also vmx_control_verify(). |
| 2294 | * TODO: allow these variables to be modified (downgraded) by module options |
| 2295 | * or other means. |
| 2296 | */ |
| 2297 | static u32 nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high; |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2298 | static u32 nested_vmx_true_procbased_ctls_low; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2299 | static u32 nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high; |
| 2300 | static u32 nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high; |
| 2301 | static u32 nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2302 | static u32 nested_vmx_true_exit_ctls_low; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2303 | static u32 nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2304 | static u32 nested_vmx_true_entry_ctls_low; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2305 | static u32 nested_vmx_misc_low, nested_vmx_misc_high; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 2306 | static u32 nested_vmx_ept_caps; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2307 | static __init void nested_vmx_setup_ctls_msrs(void) |
| 2308 | { |
| 2309 | /* |
| 2310 | * Note that as a general rule, the high half of the MSRs (bits in |
| 2311 | * the control fields which may be 1) should be initialized by the |
| 2312 | * intersection of the underlying hardware's MSR (i.e., features which |
| 2313 | * can be supported) and the list of features we want to expose - |
| 2314 | * because they are known to be properly supported in our code. |
| 2315 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 2316 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 2317 | * reason is that if one of these bits is necessary, it will appear |
| 2318 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 2319 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
| 2320 | * nested_vmx_exit_handled() will not pass related exits to L1. |
| 2321 | * These rules have exceptions below. |
| 2322 | */ |
| 2323 | |
| 2324 | /* pin-based controls */ |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 2325 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
| 2326 | nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high); |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 2327 | nested_vmx_pinbased_ctls_low |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2328 | nested_vmx_pinbased_ctls_high &= PIN_BASED_EXT_INTR_MASK | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2329 | PIN_BASED_NMI_EXITING | PIN_BASED_VIRTUAL_NMIS; |
| 2330 | nested_vmx_pinbased_ctls_high |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 2331 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2332 | |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 2333 | /* exit controls */ |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2334 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
| 2335 | nested_vmx_exit_ctls_low, nested_vmx_exit_ctls_high); |
Jan Kiszka | 33fb20c | 2013-03-06 15:44:03 +0100 | [diff] [blame] | 2336 | nested_vmx_exit_ctls_low = VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2337 | |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2338 | nested_vmx_exit_ctls_high &= |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2339 | #ifdef CONFIG_X86_64 |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2340 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2341 | #endif |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2342 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
| 2343 | nested_vmx_exit_ctls_high |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
| 2344 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2345 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 2346 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 2347 | if (vmx_mpx_supported()) |
| 2348 | nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2349 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2350 | /* We support free control of debug control saving. */ |
| 2351 | nested_vmx_true_exit_ctls_low = nested_vmx_exit_ctls_low & |
| 2352 | ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
| 2353 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2354 | /* entry controls */ |
| 2355 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
| 2356 | nested_vmx_entry_ctls_low, nested_vmx_entry_ctls_high); |
Jan Kiszka | 33fb20c | 2013-03-06 15:44:03 +0100 | [diff] [blame] | 2357 | nested_vmx_entry_ctls_low = VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2358 | nested_vmx_entry_ctls_high &= |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 2359 | #ifdef CONFIG_X86_64 |
| 2360 | VM_ENTRY_IA32E_MODE | |
| 2361 | #endif |
| 2362 | VM_ENTRY_LOAD_IA32_PAT; |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 2363 | nested_vmx_entry_ctls_high |= (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | |
| 2364 | VM_ENTRY_LOAD_IA32_EFER); |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 2365 | if (vmx_mpx_supported()) |
| 2366 | nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 2367 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2368 | /* We support free control of debug control loading. */ |
| 2369 | nested_vmx_true_entry_ctls_low = nested_vmx_entry_ctls_low & |
| 2370 | ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
| 2371 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2372 | /* cpu-based controls */ |
| 2373 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
| 2374 | nested_vmx_procbased_ctls_low, nested_vmx_procbased_ctls_high); |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 2375 | nested_vmx_procbased_ctls_low = CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2376 | nested_vmx_procbased_ctls_high &= |
Jan Kiszka | a294c9b | 2013-10-23 17:43:09 +0100 | [diff] [blame] | 2377 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 2378 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2379 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 2380 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 2381 | CPU_BASED_CR3_STORE_EXITING | |
| 2382 | #ifdef CONFIG_X86_64 |
| 2383 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 2384 | #endif |
| 2385 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
| 2386 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | dbcb4e7 | 2012-08-13 15:38:22 +0300 | [diff] [blame] | 2387 | CPU_BASED_RDPMC_EXITING | CPU_BASED_RDTSC_EXITING | |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 2388 | CPU_BASED_PAUSE_EXITING | CPU_BASED_TPR_SHADOW | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2389 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
| 2390 | /* |
| 2391 | * We can allow some features even when not supported by the |
| 2392 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 2393 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 2394 | * without MSR bitmaps. |
| 2395 | */ |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 2396 | nested_vmx_procbased_ctls_high |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
| 2397 | CPU_BASED_USE_MSR_BITMAPS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2398 | |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2399 | /* We support free control of CR3 access interception. */ |
| 2400 | nested_vmx_true_procbased_ctls_low = nested_vmx_procbased_ctls_low & |
| 2401 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 2402 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2403 | /* secondary cpu-based controls */ |
| 2404 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
| 2405 | nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high); |
| 2406 | nested_vmx_secondary_ctls_low = 0; |
| 2407 | nested_vmx_secondary_ctls_high &= |
Jan Kiszka | d6851fb | 2013-02-23 22:34:39 +0100 | [diff] [blame] | 2408 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 2409 | SECONDARY_EXEC_WBINVD_EXITING | |
| 2410 | SECONDARY_EXEC_XSAVES; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2411 | |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2412 | if (enable_ept) { |
| 2413 | /* nested EPT: emulate EPT also to L1 */ |
Bandan Das | 78051e3 | 2014-12-06 20:32:16 +0530 | [diff] [blame] | 2414 | nested_vmx_secondary_ctls_high |= SECONDARY_EXEC_ENABLE_EPT | |
| 2415 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
Jan Kiszka | ca72d97 | 2013-08-06 10:39:55 +0200 | [diff] [blame] | 2416 | nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
Jan Kiszka | d3134db | 2013-10-23 14:40:31 +0100 | [diff] [blame] | 2417 | VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 2418 | VMX_EPT_INVEPT_BIT; |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2419 | nested_vmx_ept_caps &= vmx_capability.ept; |
| 2420 | /* |
Bandan Das | 4b85507 | 2014-04-19 18:17:44 -0400 | [diff] [blame] | 2421 | * For nested guests, we don't do anything specific |
| 2422 | * for single context invalidation. Hence, only advertise |
| 2423 | * support for global context invalidation. |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2424 | */ |
Bandan Das | 4b85507 | 2014-04-19 18:17:44 -0400 | [diff] [blame] | 2425 | nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT; |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2426 | } else |
| 2427 | nested_vmx_ept_caps = 0; |
| 2428 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2429 | /* miscellaneous data */ |
| 2430 | rdmsr(MSR_IA32_VMX_MISC, nested_vmx_misc_low, nested_vmx_misc_high); |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2431 | nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 2432 | nested_vmx_misc_low |= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
| 2433 | VMX_MISC_ACTIVITY_HLT; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2434 | nested_vmx_misc_high = 0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 2438 | { |
| 2439 | /* |
| 2440 | * Bits 0 in high must be 0, and bits 1 in low must be 1. |
| 2441 | */ |
| 2442 | return ((control & high) | low) == control; |
| 2443 | } |
| 2444 | |
| 2445 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 2446 | { |
| 2447 | return low | ((u64)high << 32); |
| 2448 | } |
| 2449 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 2450 | /* Returns 0 on success, non-0 otherwise. */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2451 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 2452 | { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2453 | switch (msr_index) { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2454 | case MSR_IA32_VMX_BASIC: |
| 2455 | /* |
| 2456 | * This MSR reports some information about VMX support. We |
| 2457 | * should return information about the VMX we emulate for the |
| 2458 | * guest, and the VMCS structure we give it - not about the |
| 2459 | * VMX support of the underlying hardware. |
| 2460 | */ |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 2461 | *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2462 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 2463 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 2464 | break; |
| 2465 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 2466 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 2467 | *pdata = vmx_control_msr(nested_vmx_pinbased_ctls_low, |
| 2468 | nested_vmx_pinbased_ctls_high); |
| 2469 | break; |
| 2470 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2471 | *pdata = vmx_control_msr(nested_vmx_true_procbased_ctls_low, |
| 2472 | nested_vmx_procbased_ctls_high); |
| 2473 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2474 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 2475 | *pdata = vmx_control_msr(nested_vmx_procbased_ctls_low, |
| 2476 | nested_vmx_procbased_ctls_high); |
| 2477 | break; |
| 2478 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2479 | *pdata = vmx_control_msr(nested_vmx_true_exit_ctls_low, |
| 2480 | nested_vmx_exit_ctls_high); |
| 2481 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2482 | case MSR_IA32_VMX_EXIT_CTLS: |
| 2483 | *pdata = vmx_control_msr(nested_vmx_exit_ctls_low, |
| 2484 | nested_vmx_exit_ctls_high); |
| 2485 | break; |
| 2486 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2487 | *pdata = vmx_control_msr(nested_vmx_true_entry_ctls_low, |
| 2488 | nested_vmx_entry_ctls_high); |
| 2489 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2490 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 2491 | *pdata = vmx_control_msr(nested_vmx_entry_ctls_low, |
| 2492 | nested_vmx_entry_ctls_high); |
| 2493 | break; |
| 2494 | case MSR_IA32_VMX_MISC: |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2495 | *pdata = vmx_control_msr(nested_vmx_misc_low, |
| 2496 | nested_vmx_misc_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2497 | break; |
| 2498 | /* |
| 2499 | * These MSRs specify bits which the guest must keep fixed (on or off) |
| 2500 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 2501 | * We picked the standard core2 setting. |
| 2502 | */ |
| 2503 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 2504 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 2505 | case MSR_IA32_VMX_CR0_FIXED0: |
| 2506 | *pdata = VMXON_CR0_ALWAYSON; |
| 2507 | break; |
| 2508 | case MSR_IA32_VMX_CR0_FIXED1: |
| 2509 | *pdata = -1ULL; |
| 2510 | break; |
| 2511 | case MSR_IA32_VMX_CR4_FIXED0: |
| 2512 | *pdata = VMXON_CR4_ALWAYSON; |
| 2513 | break; |
| 2514 | case MSR_IA32_VMX_CR4_FIXED1: |
| 2515 | *pdata = -1ULL; |
| 2516 | break; |
| 2517 | case MSR_IA32_VMX_VMCS_ENUM: |
Jan Kiszka | 5381417 | 2014-06-16 13:59:44 +0200 | [diff] [blame] | 2518 | *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2519 | break; |
| 2520 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 2521 | *pdata = vmx_control_msr(nested_vmx_secondary_ctls_low, |
| 2522 | nested_vmx_secondary_ctls_high); |
| 2523 | break; |
| 2524 | case MSR_IA32_VMX_EPT_VPID_CAP: |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2525 | /* Currently, no nested vpid support */ |
| 2526 | *pdata = nested_vmx_ept_caps; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2527 | break; |
| 2528 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2529 | return 1; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 2530 | } |
| 2531 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2532 | return 0; |
| 2533 | } |
| 2534 | |
| 2535 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2536 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 2537 | * Returns 0 on success, non-0 otherwise. |
| 2538 | * Assumes vcpu_load() was already called. |
| 2539 | */ |
| 2540 | static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 2541 | { |
| 2542 | u64 data; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2543 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2544 | |
| 2545 | if (!pdata) { |
| 2546 | printk(KERN_ERR "BUG: get_msr called with NULL pdata\n"); |
| 2547 | return -EINVAL; |
| 2548 | } |
| 2549 | |
| 2550 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2551 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2552 | case MSR_FS_BASE: |
| 2553 | data = vmcs_readl(GUEST_FS_BASE); |
| 2554 | break; |
| 2555 | case MSR_GS_BASE: |
| 2556 | data = vmcs_readl(GUEST_GS_BASE); |
| 2557 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2558 | case MSR_KERNEL_GS_BASE: |
| 2559 | vmx_load_host_state(to_vmx(vcpu)); |
| 2560 | data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
| 2561 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2562 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2563 | case MSR_EFER: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2564 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2565 | case MSR_IA32_TSC: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2566 | data = guest_read_tsc(); |
| 2567 | break; |
| 2568 | case MSR_IA32_SYSENTER_CS: |
| 2569 | data = vmcs_read32(GUEST_SYSENTER_CS); |
| 2570 | break; |
| 2571 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2572 | data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2573 | break; |
| 2574 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2575 | data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2576 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 2577 | case MSR_IA32_BNDCFGS: |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 2578 | if (!vmx_mpx_supported()) |
| 2579 | return 1; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 2580 | data = vmcs_read64(GUEST_BNDCFGS); |
| 2581 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 2582 | case MSR_IA32_FEATURE_CONTROL: |
| 2583 | if (!nested_vmx_allowed(vcpu)) |
| 2584 | return 1; |
| 2585 | data = to_vmx(vcpu)->nested.msr_ia32_feature_control; |
| 2586 | break; |
| 2587 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 2588 | if (!nested_vmx_allowed(vcpu)) |
| 2589 | return 1; |
| 2590 | return vmx_get_vmx_msr(vcpu, msr_index, pdata); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 2591 | case MSR_IA32_XSS: |
| 2592 | if (!vmx_xsaves_supported()) |
| 2593 | return 1; |
| 2594 | data = vcpu->arch.ia32_xss; |
| 2595 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2596 | case MSR_TSC_AUX: |
| 2597 | if (!to_vmx(vcpu)->rdtscp_enabled) |
| 2598 | return 1; |
| 2599 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2600 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2601 | msr = find_msr_entry(to_vmx(vcpu), msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2602 | if (msr) { |
| 2603 | data = msr->data; |
| 2604 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2605 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2606 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | *pdata = data; |
| 2610 | return 0; |
| 2611 | } |
| 2612 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 2613 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
| 2614 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2615 | /* |
| 2616 | * Writes msr value into into the appropriate "register". |
| 2617 | * Returns 0 on success, non-0 otherwise. |
| 2618 | * Assumes vcpu_load() was already called. |
| 2619 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2620 | static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2621 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2622 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2623 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2624 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2625 | u32 msr_index = msr_info->index; |
| 2626 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2627 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2628 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2629 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2630 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2631 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 2632 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2633 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2634 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2635 | vmcs_writel(GUEST_FS_BASE, data); |
| 2636 | break; |
| 2637 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2638 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2639 | vmcs_writel(GUEST_GS_BASE, data); |
| 2640 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2641 | case MSR_KERNEL_GS_BASE: |
| 2642 | vmx_load_host_state(vmx); |
| 2643 | vmx->msr_guest_kernel_gs_base = data; |
| 2644 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2645 | #endif |
| 2646 | case MSR_IA32_SYSENTER_CS: |
| 2647 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 2648 | break; |
| 2649 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2650 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2651 | break; |
| 2652 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 2653 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2654 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 2655 | case MSR_IA32_BNDCFGS: |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 2656 | if (!vmx_mpx_supported()) |
| 2657 | return 1; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 2658 | vmcs_write64(GUEST_BNDCFGS, data); |
| 2659 | break; |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2660 | case MSR_IA32_TSC: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2661 | kvm_write_tsc(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2662 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2663 | case MSR_IA32_CR_PAT: |
| 2664 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 2665 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 2666 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 2667 | vmcs_write64(GUEST_IA32_PAT, data); |
| 2668 | vcpu->arch.pat = data; |
| 2669 | break; |
| 2670 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2671 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2672 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 2673 | case MSR_IA32_TSC_ADJUST: |
| 2674 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2675 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 2676 | case MSR_IA32_FEATURE_CONTROL: |
| 2677 | if (!nested_vmx_allowed(vcpu) || |
| 2678 | (to_vmx(vcpu)->nested.msr_ia32_feature_control & |
| 2679 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 2680 | return 1; |
| 2681 | vmx->nested.msr_ia32_feature_control = data; |
| 2682 | if (msr_info->host_initiated && data == 0) |
| 2683 | vmx_leave_nested(vcpu); |
| 2684 | break; |
| 2685 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 2686 | return 1; /* they are read-only */ |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 2687 | case MSR_IA32_XSS: |
| 2688 | if (!vmx_xsaves_supported()) |
| 2689 | return 1; |
| 2690 | /* |
| 2691 | * The only supported bit as of Skylake is bit 8, but |
| 2692 | * it is not supported on KVM. |
| 2693 | */ |
| 2694 | if (data != 0) |
| 2695 | return 1; |
| 2696 | vcpu->arch.ia32_xss = data; |
| 2697 | if (vcpu->arch.ia32_xss != host_xss) |
| 2698 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
| 2699 | vcpu->arch.ia32_xss, host_xss); |
| 2700 | else |
| 2701 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 2702 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2703 | case MSR_TSC_AUX: |
| 2704 | if (!vmx->rdtscp_enabled) |
| 2705 | return 1; |
| 2706 | /* Check reserved bit, higher 32 bits should be zero */ |
| 2707 | if ((data >> 32) != 0) |
| 2708 | return 1; |
| 2709 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2710 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2711 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2712 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 2713 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2714 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 2715 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 2716 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 2717 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 2718 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 2719 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 2720 | if (ret) |
| 2721 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 2722 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2723 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2724 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2725 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2726 | } |
| 2727 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2728 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2729 | } |
| 2730 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2731 | 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] | 2732 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2733 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 2734 | switch (reg) { |
| 2735 | case VCPU_REGS_RSP: |
| 2736 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 2737 | break; |
| 2738 | case VCPU_REGS_RIP: |
| 2739 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 2740 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2741 | case VCPU_EXREG_PDPTR: |
| 2742 | if (enable_ept) |
| 2743 | ept_save_pdptrs(vcpu); |
| 2744 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2745 | default: |
| 2746 | break; |
| 2747 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2748 | } |
| 2749 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2750 | static __init int cpu_has_kvm_support(void) |
| 2751 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 2752 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | static __init int vmx_disabled_by_bios(void) |
| 2756 | { |
| 2757 | u64 msr; |
| 2758 | |
| 2759 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2760 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2761 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2762 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 2763 | && tboot_enabled()) |
| 2764 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2765 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2766 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2767 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 2768 | && !tboot_enabled()) { |
| 2769 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2770 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2771 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 2772 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2773 | /* launched w/o TXT and VMX disabled */ |
| 2774 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 2775 | && !tboot_enabled()) |
| 2776 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2780 | } |
| 2781 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2782 | static void kvm_cpu_vmxon(u64 addr) |
| 2783 | { |
| 2784 | asm volatile (ASM_VMX_VMXON_RAX |
| 2785 | : : "a"(&addr), "m"(addr) |
| 2786 | : "memory", "cc"); |
| 2787 | } |
| 2788 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 2789 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2790 | { |
| 2791 | int cpu = raw_smp_processor_id(); |
| 2792 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2793 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2794 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2795 | if (read_cr4() & X86_CR4_VMXE) |
| 2796 | return -EBUSY; |
| 2797 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2798 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2799 | |
| 2800 | /* |
| 2801 | * Now we can enable the vmclear operation in kdump |
| 2802 | * since the loaded_vmcss_on_cpu list on this cpu |
| 2803 | * has been initialized. |
| 2804 | * |
| 2805 | * Though the cpu is not in VMX operation now, there |
| 2806 | * is no problem to enable the vmclear operation |
| 2807 | * for the loaded_vmcss_on_cpu list is empty! |
| 2808 | */ |
| 2809 | crash_enable_local_vmclear(cpu); |
| 2810 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2811 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2812 | |
| 2813 | test_bits = FEATURE_CONTROL_LOCKED; |
| 2814 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 2815 | if (tboot_enabled()) |
| 2816 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 2817 | |
| 2818 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2819 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2820 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 2821 | } |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2822 | write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */ |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2823 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2824 | if (vmm_exclusive) { |
| 2825 | kvm_cpu_vmxon(phys_addr); |
| 2826 | ept_sync_global(); |
| 2827 | } |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2828 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 2829 | native_store_gdt(this_cpu_ptr(&host_gdt)); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 2830 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2831 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2832 | } |
| 2833 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2834 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2835 | { |
| 2836 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2837 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2838 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2839 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 2840 | loaded_vmcss_on_cpu_link) |
| 2841 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2842 | } |
| 2843 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 2844 | |
| 2845 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 2846 | * tricks. |
| 2847 | */ |
| 2848 | static void kvm_cpu_vmxoff(void) |
| 2849 | { |
| 2850 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 2851 | } |
| 2852 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 2853 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2854 | { |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2855 | if (vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2856 | vmclear_local_loaded_vmcss(); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2857 | kvm_cpu_vmxoff(); |
| 2858 | } |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2859 | write_cr4(read_cr4() & ~X86_CR4_VMXE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2860 | } |
| 2861 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2862 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2863 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2864 | { |
| 2865 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2866 | u32 ctl = ctl_min | ctl_opt; |
| 2867 | |
| 2868 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 2869 | |
| 2870 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 2871 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 2872 | |
| 2873 | /* Ensure minimum (required) set of control bits are supported. */ |
| 2874 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2875 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2876 | |
| 2877 | *result = ctl; |
| 2878 | return 0; |
| 2879 | } |
| 2880 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2881 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 2882 | { |
| 2883 | u32 vmx_msr_low, vmx_msr_high; |
| 2884 | |
| 2885 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 2886 | return vmx_msr_high & ctl; |
| 2887 | } |
| 2888 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2889 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2890 | { |
| 2891 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2892 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2893 | u32 _pin_based_exec_control = 0; |
| 2894 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2895 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2896 | u32 _vmexit_control = 0; |
| 2897 | u32 _vmentry_control = 0; |
| 2898 | |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 2899 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2900 | #ifdef CONFIG_X86_64 |
| 2901 | CPU_BASED_CR8_LOAD_EXITING | |
| 2902 | CPU_BASED_CR8_STORE_EXITING | |
| 2903 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2904 | CPU_BASED_CR3_LOAD_EXITING | |
| 2905 | CPU_BASED_CR3_STORE_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2906 | CPU_BASED_USE_IO_BITMAPS | |
| 2907 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2908 | CPU_BASED_USE_TSC_OFFSETING | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 2909 | CPU_BASED_MWAIT_EXITING | |
| 2910 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 2911 | CPU_BASED_INVLPG_EXITING | |
| 2912 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 2913 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2914 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2915 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2916 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2917 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 2918 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2919 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2920 | #ifdef CONFIG_X86_64 |
| 2921 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 2922 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 2923 | ~CPU_BASED_CR8_STORE_EXITING; |
| 2924 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2925 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2926 | min2 = 0; |
| 2927 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2928 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2929 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2930 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2931 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2932 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2933 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 2934 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 2935 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 2936 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 2937 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 2938 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 2939 | SECONDARY_EXEC_XSAVES | |
| 2940 | SECONDARY_EXEC_ENABLE_PML; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2941 | if (adjust_vmx_controls(min2, opt2, |
| 2942 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2943 | &_cpu_based_2nd_exec_control) < 0) |
| 2944 | return -EIO; |
| 2945 | } |
| 2946 | #ifndef CONFIG_X86_64 |
| 2947 | if (!(_cpu_based_2nd_exec_control & |
| 2948 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 2949 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2950 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 2951 | |
| 2952 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 2953 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2954 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 2955 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 2956 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 2957 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2958 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2959 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 2960 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 2961 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 2962 | CPU_BASED_CR3_STORE_EXITING | |
| 2963 | CPU_BASED_INVLPG_EXITING); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2964 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
| 2965 | vmx_capability.ept, vmx_capability.vpid); |
| 2966 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2967 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 2968 | min = VM_EXIT_SAVE_DEBUG_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2969 | #ifdef CONFIG_X86_64 |
| 2970 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 2971 | #endif |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 2972 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2973 | VM_EXIT_ACK_INTR_ON_EXIT | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2974 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 2975 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2976 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2977 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 2978 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
| 2979 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR; |
| 2980 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 2981 | &_pin_based_exec_control) < 0) |
| 2982 | return -EIO; |
| 2983 | |
| 2984 | if (!(_cpu_based_2nd_exec_control & |
| 2985 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) || |
| 2986 | !(_vmexit_control & VM_EXIT_ACK_INTR_ON_EXIT)) |
| 2987 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 2988 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 2989 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2990 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2991 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 2992 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2993 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2994 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 2995 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2996 | |
| 2997 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 2998 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2999 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3000 | |
| 3001 | #ifdef CONFIG_X86_64 |
| 3002 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 3003 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3004 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3005 | #endif |
| 3006 | |
| 3007 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 3008 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3009 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3010 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3011 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
| 3012 | vmcs_conf->order = get_order(vmcs_config.size); |
| 3013 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3014 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3015 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 3016 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3017 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3018 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 3019 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3020 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 3021 | cpu_has_load_ia32_efer = |
| 3022 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 3023 | VM_ENTRY_LOAD_IA32_EFER) |
| 3024 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 3025 | VM_EXIT_LOAD_IA32_EFER); |
| 3026 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 3027 | cpu_has_load_perf_global_ctrl = |
| 3028 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 3029 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 3030 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 3031 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 3032 | |
| 3033 | /* |
| 3034 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
| 3035 | * but due to arrata below it can't be used. Workaround is to use |
| 3036 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 3037 | * |
| 3038 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 3039 | * |
| 3040 | * AAK155 (model 26) |
| 3041 | * AAP115 (model 30) |
| 3042 | * AAT100 (model 37) |
| 3043 | * BC86,AAY89,BD102 (model 44) |
| 3044 | * BA97 (model 46) |
| 3045 | * |
| 3046 | */ |
| 3047 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 3048 | switch (boot_cpu_data.x86_model) { |
| 3049 | case 26: |
| 3050 | case 30: |
| 3051 | case 37: |
| 3052 | case 44: |
| 3053 | case 46: |
| 3054 | cpu_has_load_perf_global_ctrl = false; |
| 3055 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 3056 | "does not work properly. Using workaround\n"); |
| 3057 | break; |
| 3058 | default: |
| 3059 | break; |
| 3060 | } |
| 3061 | } |
| 3062 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3063 | if (cpu_has_xsaves) |
| 3064 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 3065 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3066 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 3067 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3068 | |
| 3069 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 3070 | { |
| 3071 | int node = cpu_to_node(cpu); |
| 3072 | struct page *pages; |
| 3073 | struct vmcs *vmcs; |
| 3074 | |
Mel Gorman | 6484eb3 | 2009-06-16 15:31:54 -0700 | [diff] [blame] | 3075 | pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3076 | if (!pages) |
| 3077 | return NULL; |
| 3078 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3079 | memset(vmcs, 0, vmcs_config.size); |
| 3080 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3081 | return vmcs; |
| 3082 | } |
| 3083 | |
| 3084 | static struct vmcs *alloc_vmcs(void) |
| 3085 | { |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3086 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | static void free_vmcs(struct vmcs *vmcs) |
| 3090 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3091 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3092 | } |
| 3093 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3094 | /* |
| 3095 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 3096 | */ |
| 3097 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 3098 | { |
| 3099 | if (!loaded_vmcs->vmcs) |
| 3100 | return; |
| 3101 | loaded_vmcs_clear(loaded_vmcs); |
| 3102 | free_vmcs(loaded_vmcs->vmcs); |
| 3103 | loaded_vmcs->vmcs = NULL; |
| 3104 | } |
| 3105 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 3106 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3107 | { |
| 3108 | int cpu; |
| 3109 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3110 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3111 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3112 | per_cpu(vmxarea, cpu) = NULL; |
| 3113 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3114 | } |
| 3115 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 3116 | static void init_vmcs_shadow_fields(void) |
| 3117 | { |
| 3118 | int i, j; |
| 3119 | |
| 3120 | /* No checks for read only fields yet */ |
| 3121 | |
| 3122 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
| 3123 | switch (shadow_read_write_fields[i]) { |
| 3124 | case GUEST_BNDCFGS: |
| 3125 | if (!vmx_mpx_supported()) |
| 3126 | continue; |
| 3127 | break; |
| 3128 | default: |
| 3129 | break; |
| 3130 | } |
| 3131 | |
| 3132 | if (j < i) |
| 3133 | shadow_read_write_fields[j] = |
| 3134 | shadow_read_write_fields[i]; |
| 3135 | j++; |
| 3136 | } |
| 3137 | max_shadow_read_write_fields = j; |
| 3138 | |
| 3139 | /* shadowed fields guest access without vmexit */ |
| 3140 | for (i = 0; i < max_shadow_read_write_fields; i++) { |
| 3141 | clear_bit(shadow_read_write_fields[i], |
| 3142 | vmx_vmwrite_bitmap); |
| 3143 | clear_bit(shadow_read_write_fields[i], |
| 3144 | vmx_vmread_bitmap); |
| 3145 | } |
| 3146 | for (i = 0; i < max_shadow_read_only_fields; i++) |
| 3147 | clear_bit(shadow_read_only_fields[i], |
| 3148 | vmx_vmread_bitmap); |
| 3149 | } |
| 3150 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3151 | static __init int alloc_kvm_area(void) |
| 3152 | { |
| 3153 | int cpu; |
| 3154 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3155 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3156 | struct vmcs *vmcs; |
| 3157 | |
| 3158 | vmcs = alloc_vmcs_cpu(cpu); |
| 3159 | if (!vmcs) { |
| 3160 | free_kvm_area(); |
| 3161 | return -ENOMEM; |
| 3162 | } |
| 3163 | |
| 3164 | per_cpu(vmxarea, cpu) = vmcs; |
| 3165 | } |
| 3166 | return 0; |
| 3167 | } |
| 3168 | |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 3169 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 3170 | { |
| 3171 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 3172 | } |
| 3173 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 3174 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3175 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3176 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3177 | if (!emulate_invalid_guest_state) { |
| 3178 | /* |
| 3179 | * CS and SS RPL should be equal during guest entry according |
| 3180 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 3181 | * is in the middle of the transition from real mode to |
| 3182 | * protected mode it is safe to assume that RPL 0 is a good |
| 3183 | * default value. |
| 3184 | */ |
| 3185 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
| 3186 | save->selector &= ~SELECTOR_RPL_MASK; |
| 3187 | save->dpl = save->selector & SELECTOR_RPL_MASK; |
| 3188 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3189 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3190 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 3194 | { |
| 3195 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3196 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3197 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3198 | /* |
| 3199 | * Update real mode segment cache. It may be not up-to-date if sement |
| 3200 | * register was written while vcpu was in a guest mode. |
| 3201 | */ |
| 3202 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 3203 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 3204 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 3205 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 3206 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 3207 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 3208 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3209 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3210 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3211 | vmx_segment_cache_clear(vmx); |
| 3212 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3213 | vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3214 | |
| 3215 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3216 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 3217 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3218 | vmcs_writel(GUEST_RFLAGS, flags); |
| 3219 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 3220 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 3221 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3222 | |
| 3223 | update_exception_bitmap(vcpu); |
| 3224 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 3225 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 3226 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 3227 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 3228 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 3229 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 3230 | fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3231 | } |
| 3232 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3233 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3234 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 3235 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3236 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3237 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3238 | var.dpl = 0x3; |
| 3239 | if (seg == VCPU_SREG_CS) |
| 3240 | var.type = 0x3; |
| 3241 | |
| 3242 | if (!emulate_invalid_guest_state) { |
| 3243 | var.selector = var.base >> 4; |
| 3244 | var.base = var.base & 0xffff0; |
| 3245 | var.limit = 0xffff; |
| 3246 | var.g = 0; |
| 3247 | var.db = 0; |
| 3248 | var.present = 1; |
| 3249 | var.s = 1; |
| 3250 | var.l = 0; |
| 3251 | var.unusable = 0; |
| 3252 | var.type = 0x3; |
| 3253 | var.avl = 0; |
| 3254 | if (save->base & 0xf) |
| 3255 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 3256 | "paragraph aligned when entering " |
| 3257 | "protected mode (seg=%d)", seg); |
| 3258 | } |
| 3259 | |
| 3260 | vmcs_write16(sf->selector, var.selector); |
| 3261 | vmcs_write32(sf->base, var.base); |
| 3262 | vmcs_write32(sf->limit, var.limit); |
| 3263 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 3267 | { |
| 3268 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3269 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3270 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3271 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 3272 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 3273 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 3274 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 3275 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 3276 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 3277 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3278 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3279 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3280 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3281 | /* |
| 3282 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3283 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3284 | */ |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3285 | if (!vcpu->kvm->arch.tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3286 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 3287 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3288 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3289 | vmx_segment_cache_clear(vmx); |
| 3290 | |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3291 | vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3292 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3293 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 3294 | |
| 3295 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3296 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3297 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 3298 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3299 | |
| 3300 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 3301 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3302 | update_exception_bitmap(vcpu); |
| 3303 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3304 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 3305 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 3306 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 3307 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 3308 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 3309 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3310 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 3311 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3312 | } |
| 3313 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3314 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 3315 | { |
| 3316 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3317 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 3318 | |
| 3319 | if (!msr) |
| 3320 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3321 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3322 | /* |
| 3323 | * Force kernel_gs_base reloading before EFER changes, as control |
| 3324 | * of this msr depends on is_long_mode(). |
| 3325 | */ |
| 3326 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3327 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3328 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3329 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3330 | msr->data = efer; |
| 3331 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3332 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3333 | |
| 3334 | msr->data = efer & ~EFER_LME; |
| 3335 | } |
| 3336 | setup_msrs(vmx); |
| 3337 | } |
| 3338 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3339 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3340 | |
| 3341 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 3342 | { |
| 3343 | u32 guest_tr_ar; |
| 3344 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3345 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 3346 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3347 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
| 3348 | if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 3349 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 3350 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3351 | vmcs_write32(GUEST_TR_AR_BYTES, |
| 3352 | (guest_tr_ar & ~AR_TYPE_MASK) |
| 3353 | | AR_TYPE_BUSY_64_TSS); |
| 3354 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 3355 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 3359 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3360 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 3361 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | #endif |
| 3365 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3366 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 3367 | { |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 3368 | vpid_sync_context(to_vmx(vcpu)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 3369 | if (enable_ept) { |
| 3370 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 3371 | return; |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 3372 | ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 3373 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3374 | } |
| 3375 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 3376 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 3377 | { |
| 3378 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 3379 | |
| 3380 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 3381 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 3382 | } |
| 3383 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 3384 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 3385 | { |
| 3386 | if (enable_ept && is_paging(vcpu)) |
| 3387 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 3388 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 3389 | } |
| 3390 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 3391 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 3392 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3393 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 3394 | |
| 3395 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 3396 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 3397 | } |
| 3398 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3399 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 3400 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3401 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 3402 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3403 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 3404 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 3405 | return; |
| 3406 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3407 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3408 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 3409 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 3410 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 3411 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3412 | } |
| 3413 | } |
| 3414 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3415 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 3416 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3417 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 3418 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3419 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3420 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 3421 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 3422 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 3423 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3424 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3425 | |
| 3426 | __set_bit(VCPU_EXREG_PDPTR, |
| 3427 | (unsigned long *)&vcpu->arch.regs_avail); |
| 3428 | __set_bit(VCPU_EXREG_PDPTR, |
| 3429 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3430 | } |
| 3431 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3432 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3433 | |
| 3434 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 3435 | unsigned long cr0, |
| 3436 | struct kvm_vcpu *vcpu) |
| 3437 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 3438 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 3439 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3440 | if (!(cr0 & X86_CR0_PG)) { |
| 3441 | /* From paging/starting to nonpaging */ |
| 3442 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 3443 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3444 | (CPU_BASED_CR3_LOAD_EXITING | |
| 3445 | CPU_BASED_CR3_STORE_EXITING)); |
| 3446 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3447 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3448 | } else if (!is_paging(vcpu)) { |
| 3449 | /* From nonpaging to paging */ |
| 3450 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 3451 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3452 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 3453 | CPU_BASED_CR3_STORE_EXITING)); |
| 3454 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3455 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3456 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 3457 | |
| 3458 | if (!(cr0 & X86_CR0_WP)) |
| 3459 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3460 | } |
| 3461 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3462 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 3463 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3464 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3465 | unsigned long hw_cr0; |
| 3466 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 3467 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3468 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 3469 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 3470 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 3471 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3472 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 3473 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 3474 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3475 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 3476 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 3477 | enter_rmode(vcpu); |
| 3478 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3479 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3480 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3481 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 3482 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3483 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 3484 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3485 | exit_lmode(vcpu); |
| 3486 | } |
| 3487 | #endif |
| 3488 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3489 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3490 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 3491 | |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3492 | if (!vcpu->fpu_active) |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 3493 | hw_cr0 |= X86_CR0_TS | X86_CR0_MP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3494 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3495 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3496 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3497 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 3498 | |
| 3499 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 3500 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3501 | } |
| 3502 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3503 | static u64 construct_eptp(unsigned long root_hpa) |
| 3504 | { |
| 3505 | u64 eptp; |
| 3506 | |
| 3507 | /* TODO write the value reading from MSR */ |
| 3508 | eptp = VMX_EPT_DEFAULT_MT | |
| 3509 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; |
Xudong Hao | b38f993 | 2012-05-28 19:33:36 +0800 | [diff] [blame] | 3510 | if (enable_ept_ad_bits) |
| 3511 | eptp |= VMX_EPT_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3512 | eptp |= (root_hpa & PAGE_MASK); |
| 3513 | |
| 3514 | return eptp; |
| 3515 | } |
| 3516 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3517 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 3518 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3519 | unsigned long guest_cr3; |
| 3520 | u64 eptp; |
| 3521 | |
| 3522 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3523 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3524 | eptp = construct_eptp(cr3); |
| 3525 | vmcs_write64(EPT_POINTER, eptp); |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 3526 | if (is_paging(vcpu) || is_guest_mode(vcpu)) |
| 3527 | guest_cr3 = kvm_read_cr3(vcpu); |
| 3528 | else |
| 3529 | guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr; |
Marcelo Tosatti | 7c93be4 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 3530 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3531 | } |
| 3532 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3533 | vmx_flush_tlb(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3534 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3535 | } |
| 3536 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3537 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3538 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3539 | unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ? |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3540 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); |
| 3541 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3542 | if (cr4 & X86_CR4_VMXE) { |
| 3543 | /* |
| 3544 | * To use VMXON (and later other VMX instructions), a guest |
| 3545 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 3546 | * So basically the check on whether to allow nested VMX |
| 3547 | * is here. |
| 3548 | */ |
| 3549 | if (!nested_vmx_allowed(vcpu)) |
| 3550 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 3551 | } |
| 3552 | if (to_vmx(vcpu)->nested.vmxon && |
| 3553 | ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3554 | return 1; |
| 3555 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3556 | vcpu->arch.cr4 = cr4; |
Avi Kivity | bc23008 | 2009-12-08 12:14:42 +0200 | [diff] [blame] | 3557 | if (enable_ept) { |
| 3558 | if (!is_paging(vcpu)) { |
| 3559 | hw_cr4 &= ~X86_CR4_PAE; |
| 3560 | hw_cr4 |= X86_CR4_PSE; |
Dongxiao Xu | c08800a | 2013-02-04 11:50:43 +0800 | [diff] [blame] | 3561 | /* |
Feng Wu | e1e746b | 2014-04-01 17:46:35 +0800 | [diff] [blame] | 3562 | * SMEP/SMAP is disabled if CPU is in non-paging mode |
| 3563 | * in hardware. However KVM always uses paging mode to |
Dongxiao Xu | c08800a | 2013-02-04 11:50:43 +0800 | [diff] [blame] | 3564 | * emulate guest non-paging mode with TDP. |
Feng Wu | e1e746b | 2014-04-01 17:46:35 +0800 | [diff] [blame] | 3565 | * To emulate this behavior, SMEP/SMAP needs to be |
| 3566 | * manually disabled when guest switches to non-paging |
| 3567 | * mode. |
Dongxiao Xu | c08800a | 2013-02-04 11:50:43 +0800 | [diff] [blame] | 3568 | */ |
Feng Wu | e1e746b | 2014-04-01 17:46:35 +0800 | [diff] [blame] | 3569 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP); |
Avi Kivity | bc23008 | 2009-12-08 12:14:42 +0200 | [diff] [blame] | 3570 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 3571 | hw_cr4 &= ~X86_CR4_PAE; |
| 3572 | } |
| 3573 | } |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3574 | |
| 3575 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 3576 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3577 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3578 | } |
| 3579 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3580 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 3581 | struct kvm_segment *var, int seg) |
| 3582 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3583 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3584 | u32 ar; |
| 3585 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 3586 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3587 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3588 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3589 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3590 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 3591 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 3592 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 3593 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3594 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3595 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 3596 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 3597 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 3598 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 3599 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3600 | var->type = ar & 15; |
| 3601 | var->s = (ar >> 4) & 1; |
| 3602 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 3603 | /* |
| 3604 | * Some userspaces do not preserve unusable property. Since usable |
| 3605 | * segment has to be present according to VMX spec we can use present |
| 3606 | * property to amend userspace bug by making unusable segment always |
| 3607 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 3608 | * segment as unusable. |
| 3609 | */ |
| 3610 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3611 | var->avl = (ar >> 12) & 1; |
| 3612 | var->l = (ar >> 13) & 1; |
| 3613 | var->db = (ar >> 14) & 1; |
| 3614 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3615 | } |
| 3616 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3617 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 3618 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3619 | struct kvm_segment s; |
| 3620 | |
| 3621 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 3622 | vmx_get_segment(vcpu, &s, seg); |
| 3623 | return s.base; |
| 3624 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3625 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3626 | } |
| 3627 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 3628 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3629 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 3630 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3631 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 3632 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3633 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 3634 | else { |
| 3635 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
| 3636 | return AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3637 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3638 | } |
| 3639 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3640 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3641 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3642 | u32 ar; |
| 3643 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 3644 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3645 | ar = 1 << 16; |
| 3646 | else { |
| 3647 | ar = var->type & 15; |
| 3648 | ar |= (var->s & 1) << 4; |
| 3649 | ar |= (var->dpl & 3) << 5; |
| 3650 | ar |= (var->present & 1) << 7; |
| 3651 | ar |= (var->avl & 1) << 12; |
| 3652 | ar |= (var->l & 1) << 13; |
| 3653 | ar |= (var->db & 1) << 14; |
| 3654 | ar |= (var->g & 1) << 15; |
| 3655 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3656 | |
| 3657 | return ar; |
| 3658 | } |
| 3659 | |
| 3660 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 3661 | struct kvm_segment *var, int seg) |
| 3662 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3663 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 3664 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3665 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3666 | vmx_segment_cache_clear(vmx); |
| 3667 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 3668 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 3669 | vmx->rmode.segs[seg] = *var; |
| 3670 | if (seg == VCPU_SREG_TR) |
| 3671 | vmcs_write16(sf->selector, var->selector); |
| 3672 | else if (var->s) |
| 3673 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3674 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3675 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 3676 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3677 | vmcs_writel(sf->base, var->base); |
| 3678 | vmcs_write32(sf->limit, var->limit); |
| 3679 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3680 | |
| 3681 | /* |
| 3682 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 3683 | * qemu binaries. |
| 3684 | * IA32 arch specifies that at the time of processor reset the |
| 3685 | * "Accessed" bit in the AR field of segment registers is 1. And qemu |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 3686 | * is setting it to 0 in the userland code. This causes invalid guest |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3687 | * state vmexit when "unrestricted guest" mode is turned on. |
| 3688 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 3689 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 3690 | * kvm hack. |
| 3691 | */ |
| 3692 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 3693 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3694 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 3695 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3696 | |
| 3697 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 3698 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3699 | } |
| 3700 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3701 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 3702 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3703 | 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] | 3704 | |
| 3705 | *db = (ar >> 14) & 1; |
| 3706 | *l = (ar >> 13) & 1; |
| 3707 | } |
| 3708 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3709 | 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] | 3710 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3711 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 3712 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3713 | } |
| 3714 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3715 | 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] | 3716 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3717 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 3718 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3719 | } |
| 3720 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3721 | 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] | 3722 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3723 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 3724 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3725 | } |
| 3726 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3727 | 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] | 3728 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3729 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 3730 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3731 | } |
| 3732 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3733 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 3734 | { |
| 3735 | struct kvm_segment var; |
| 3736 | u32 ar; |
| 3737 | |
| 3738 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 3739 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 3740 | if (seg == VCPU_SREG_CS) |
| 3741 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3742 | ar = vmx_segment_access_rights(&var); |
| 3743 | |
| 3744 | if (var.base != (var.selector << 4)) |
| 3745 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 3746 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3747 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 3748 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3749 | return false; |
| 3750 | |
| 3751 | return true; |
| 3752 | } |
| 3753 | |
| 3754 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 3755 | { |
| 3756 | struct kvm_segment cs; |
| 3757 | unsigned int cs_rpl; |
| 3758 | |
| 3759 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 3760 | cs_rpl = cs.selector & SELECTOR_RPL_MASK; |
| 3761 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3762 | if (cs.unusable) |
| 3763 | return false; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3764 | if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK)) |
| 3765 | return false; |
| 3766 | if (!cs.s) |
| 3767 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3768 | if (cs.type & AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3769 | if (cs.dpl > cs_rpl) |
| 3770 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3771 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3772 | if (cs.dpl != cs_rpl) |
| 3773 | return false; |
| 3774 | } |
| 3775 | if (!cs.present) |
| 3776 | return false; |
| 3777 | |
| 3778 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 3779 | return true; |
| 3780 | } |
| 3781 | |
| 3782 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 3783 | { |
| 3784 | struct kvm_segment ss; |
| 3785 | unsigned int ss_rpl; |
| 3786 | |
| 3787 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 3788 | ss_rpl = ss.selector & SELECTOR_RPL_MASK; |
| 3789 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3790 | if (ss.unusable) |
| 3791 | return true; |
| 3792 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3793 | return false; |
| 3794 | if (!ss.s) |
| 3795 | return false; |
| 3796 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 3797 | return false; |
| 3798 | if (!ss.present) |
| 3799 | return false; |
| 3800 | |
| 3801 | return true; |
| 3802 | } |
| 3803 | |
| 3804 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 3805 | { |
| 3806 | struct kvm_segment var; |
| 3807 | unsigned int rpl; |
| 3808 | |
| 3809 | vmx_get_segment(vcpu, &var, seg); |
| 3810 | rpl = var.selector & SELECTOR_RPL_MASK; |
| 3811 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3812 | if (var.unusable) |
| 3813 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3814 | if (!var.s) |
| 3815 | return false; |
| 3816 | if (!var.present) |
| 3817 | return false; |
| 3818 | if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) { |
| 3819 | if (var.dpl < rpl) /* DPL < RPL */ |
| 3820 | return false; |
| 3821 | } |
| 3822 | |
| 3823 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 3824 | * rights flags |
| 3825 | */ |
| 3826 | return true; |
| 3827 | } |
| 3828 | |
| 3829 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 3830 | { |
| 3831 | struct kvm_segment tr; |
| 3832 | |
| 3833 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 3834 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3835 | if (tr.unusable) |
| 3836 | return false; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3837 | if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 3838 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3839 | 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] | 3840 | return false; |
| 3841 | if (!tr.present) |
| 3842 | return false; |
| 3843 | |
| 3844 | return true; |
| 3845 | } |
| 3846 | |
| 3847 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 3848 | { |
| 3849 | struct kvm_segment ldtr; |
| 3850 | |
| 3851 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 3852 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3853 | if (ldtr.unusable) |
| 3854 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3855 | if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */ |
| 3856 | return false; |
| 3857 | if (ldtr.type != 2) |
| 3858 | return false; |
| 3859 | if (!ldtr.present) |
| 3860 | return false; |
| 3861 | |
| 3862 | return true; |
| 3863 | } |
| 3864 | |
| 3865 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 3866 | { |
| 3867 | struct kvm_segment cs, ss; |
| 3868 | |
| 3869 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 3870 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 3871 | |
| 3872 | return ((cs.selector & SELECTOR_RPL_MASK) == |
| 3873 | (ss.selector & SELECTOR_RPL_MASK)); |
| 3874 | } |
| 3875 | |
| 3876 | /* |
| 3877 | * Check if guest state is valid. Returns true if valid, false if |
| 3878 | * not. |
| 3879 | * We assume that registers are always usable |
| 3880 | */ |
| 3881 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 3882 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 3883 | if (enable_unrestricted_guest) |
| 3884 | return true; |
| 3885 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3886 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 3887 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3888 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 3889 | return false; |
| 3890 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 3891 | return false; |
| 3892 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 3893 | return false; |
| 3894 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 3895 | return false; |
| 3896 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 3897 | return false; |
| 3898 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 3899 | return false; |
| 3900 | } else { |
| 3901 | /* protected mode guest state checks */ |
| 3902 | if (!cs_ss_rpl_check(vcpu)) |
| 3903 | return false; |
| 3904 | if (!code_segment_valid(vcpu)) |
| 3905 | return false; |
| 3906 | if (!stack_segment_valid(vcpu)) |
| 3907 | return false; |
| 3908 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 3909 | return false; |
| 3910 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 3911 | return false; |
| 3912 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 3913 | return false; |
| 3914 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 3915 | return false; |
| 3916 | if (!tr_valid(vcpu)) |
| 3917 | return false; |
| 3918 | if (!ldtr_valid(vcpu)) |
| 3919 | return false; |
| 3920 | } |
| 3921 | /* TODO: |
| 3922 | * - Add checks on RIP |
| 3923 | * - Add checks on RFLAGS |
| 3924 | */ |
| 3925 | |
| 3926 | return true; |
| 3927 | } |
| 3928 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3929 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3930 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3931 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3932 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 3933 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3934 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3935 | idx = srcu_read_lock(&kvm->srcu); |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3936 | fn = kvm->arch.tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3937 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3938 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3939 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3940 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 3941 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 3942 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3943 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3944 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3945 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 3946 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3947 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3948 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3949 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3950 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3951 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3952 | r = kvm_write_guest_page(kvm, fn, &data, |
| 3953 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 3954 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3955 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3956 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 3957 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3958 | } |
| 3959 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3960 | static int init_rmode_identity_map(struct kvm *kvm) |
| 3961 | { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 3962 | int i, idx, r = 0; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3963 | pfn_t identity_map_pfn; |
| 3964 | u32 tmp; |
| 3965 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3966 | if (!enable_ept) |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 3967 | return 0; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3968 | |
| 3969 | /* Protect kvm->arch.ept_identity_pagetable_done. */ |
| 3970 | mutex_lock(&kvm->slots_lock); |
| 3971 | |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 3972 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3973 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3974 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 3975 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3976 | |
| 3977 | r = alloc_identity_pagetable(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 3978 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3979 | goto out2; |
| 3980 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3981 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3982 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 3983 | if (r < 0) |
| 3984 | goto out; |
| 3985 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 3986 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 3987 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 3988 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 3989 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 3990 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 3991 | if (r < 0) |
| 3992 | goto out; |
| 3993 | } |
| 3994 | kvm->arch.ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 3995 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3996 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3997 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3998 | |
| 3999 | out2: |
| 4000 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4001 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4002 | } |
| 4003 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4004 | static void seg_setup(int seg) |
| 4005 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4006 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4007 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4008 | |
| 4009 | vmcs_write16(sf->selector, 0); |
| 4010 | vmcs_writel(sf->base, 0); |
| 4011 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 4012 | ar = 0x93; |
| 4013 | if (seg == VCPU_SREG_CS) |
| 4014 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4015 | |
| 4016 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4017 | } |
| 4018 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4019 | static int alloc_apic_access_page(struct kvm *kvm) |
| 4020 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 4021 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4022 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 4023 | int r = 0; |
| 4024 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4025 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 4026 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4027 | goto out; |
| 4028 | kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT; |
| 4029 | kvm_userspace_mem.flags = 0; |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 4030 | kvm_userspace_mem.guest_phys_addr = APIC_DEFAULT_PHYS_BASE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4031 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
Takuya Yoshikawa | 47ae31e | 2013-02-27 19:43:00 +0900 | [diff] [blame] | 4032 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4033 | if (r) |
| 4034 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4035 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 4036 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 4037 | if (is_error_page(page)) { |
| 4038 | r = -EFAULT; |
| 4039 | goto out; |
| 4040 | } |
| 4041 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 4042 | /* |
| 4043 | * Do not pin the page in memory, so that memory hot-unplug |
| 4044 | * is able to migrate it. |
| 4045 | */ |
| 4046 | put_page(page); |
| 4047 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4048 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4049 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4050 | return r; |
| 4051 | } |
| 4052 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4053 | static int alloc_identity_pagetable(struct kvm *kvm) |
| 4054 | { |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4055 | /* Called with kvm->slots_lock held. */ |
| 4056 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4057 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 4058 | int r = 0; |
| 4059 | |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4060 | BUG_ON(kvm->arch.ept_identity_pagetable_done); |
| 4061 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4062 | kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT; |
| 4063 | kvm_userspace_mem.flags = 0; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4064 | kvm_userspace_mem.guest_phys_addr = |
| 4065 | kvm->arch.ept_identity_map_addr; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4066 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
Takuya Yoshikawa | 47ae31e | 2013-02-27 19:43:00 +0900 | [diff] [blame] | 4067 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4068 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4069 | return r; |
| 4070 | } |
| 4071 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4072 | static void allocate_vpid(struct vcpu_vmx *vmx) |
| 4073 | { |
| 4074 | int vpid; |
| 4075 | |
| 4076 | vmx->vpid = 0; |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 4077 | if (!enable_vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4078 | return; |
| 4079 | spin_lock(&vmx_vpid_lock); |
| 4080 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 4081 | if (vpid < VMX_NR_VPIDS) { |
| 4082 | vmx->vpid = vpid; |
| 4083 | __set_bit(vpid, vmx_vpid_bitmap); |
| 4084 | } |
| 4085 | spin_unlock(&vmx_vpid_lock); |
| 4086 | } |
| 4087 | |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4088 | static void free_vpid(struct vcpu_vmx *vmx) |
| 4089 | { |
| 4090 | if (!enable_vpid) |
| 4091 | return; |
| 4092 | spin_lock(&vmx_vpid_lock); |
| 4093 | if (vmx->vpid != 0) |
| 4094 | __clear_bit(vmx->vpid, vmx_vpid_bitmap); |
| 4095 | spin_unlock(&vmx_vpid_lock); |
| 4096 | } |
| 4097 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4098 | #define MSR_TYPE_R 1 |
| 4099 | #define MSR_TYPE_W 2 |
| 4100 | static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 4101 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4102 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4103 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4104 | |
| 4105 | if (!cpu_has_vmx_msr_bitmap()) |
| 4106 | return; |
| 4107 | |
| 4108 | /* |
| 4109 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4110 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4111 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4112 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4113 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4114 | if (type & MSR_TYPE_R) |
| 4115 | /* read-low */ |
| 4116 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 4117 | |
| 4118 | if (type & MSR_TYPE_W) |
| 4119 | /* write-low */ |
| 4120 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 4121 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4122 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4123 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4124 | if (type & MSR_TYPE_R) |
| 4125 | /* read-high */ |
| 4126 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 4127 | |
| 4128 | if (type & MSR_TYPE_W) |
| 4129 | /* write-high */ |
| 4130 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 4131 | |
| 4132 | } |
| 4133 | } |
| 4134 | |
| 4135 | static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
| 4136 | u32 msr, int type) |
| 4137 | { |
| 4138 | int f = sizeof(unsigned long); |
| 4139 | |
| 4140 | if (!cpu_has_vmx_msr_bitmap()) |
| 4141 | return; |
| 4142 | |
| 4143 | /* |
| 4144 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4145 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4146 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4147 | */ |
| 4148 | if (msr <= 0x1fff) { |
| 4149 | if (type & MSR_TYPE_R) |
| 4150 | /* read-low */ |
| 4151 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 4152 | |
| 4153 | if (type & MSR_TYPE_W) |
| 4154 | /* write-low */ |
| 4155 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 4156 | |
| 4157 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4158 | msr &= 0x1fff; |
| 4159 | if (type & MSR_TYPE_R) |
| 4160 | /* read-high */ |
| 4161 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 4162 | |
| 4163 | if (type & MSR_TYPE_W) |
| 4164 | /* write-high */ |
| 4165 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 4166 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4167 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4168 | } |
| 4169 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4170 | static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only) |
| 4171 | { |
| 4172 | if (!longmode_only) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4173 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, |
| 4174 | msr, MSR_TYPE_R | MSR_TYPE_W); |
| 4175 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, |
| 4176 | msr, MSR_TYPE_R | MSR_TYPE_W); |
| 4177 | } |
| 4178 | |
| 4179 | static void vmx_enable_intercept_msr_read_x2apic(u32 msr) |
| 4180 | { |
| 4181 | __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
| 4182 | msr, MSR_TYPE_R); |
| 4183 | __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
| 4184 | msr, MSR_TYPE_R); |
| 4185 | } |
| 4186 | |
| 4187 | static void vmx_disable_intercept_msr_read_x2apic(u32 msr) |
| 4188 | { |
| 4189 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
| 4190 | msr, MSR_TYPE_R); |
| 4191 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
| 4192 | msr, MSR_TYPE_R); |
| 4193 | } |
| 4194 | |
| 4195 | static void vmx_disable_intercept_msr_write_x2apic(u32 msr) |
| 4196 | { |
| 4197 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
| 4198 | msr, MSR_TYPE_W); |
| 4199 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
| 4200 | msr, MSR_TYPE_W); |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4201 | } |
| 4202 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4203 | static int vmx_vm_has_apicv(struct kvm *kvm) |
| 4204 | { |
| 4205 | return enable_apicv && irqchip_in_kernel(kvm); |
| 4206 | } |
| 4207 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4208 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 4209 | * Send interrupt to vcpu via posted interrupt way. |
| 4210 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 4211 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 4212 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 4213 | * interrupt from PIR in next vmentry. |
| 4214 | */ |
| 4215 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 4216 | { |
| 4217 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4218 | int r; |
| 4219 | |
| 4220 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 4221 | return; |
| 4222 | |
| 4223 | r = pi_test_and_set_on(&vmx->pi_desc); |
| 4224 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Zhang, Yang Z | 6ffbbbb | 2013-04-17 23:11:54 -0300 | [diff] [blame] | 4225 | #ifdef CONFIG_SMP |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 4226 | if (!r && (vcpu->mode == IN_GUEST_MODE)) |
| 4227 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), |
| 4228 | POSTED_INTR_VECTOR); |
| 4229 | else |
Zhang, Yang Z | 6ffbbbb | 2013-04-17 23:11:54 -0300 | [diff] [blame] | 4230 | #endif |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 4231 | kvm_vcpu_kick(vcpu); |
| 4232 | } |
| 4233 | |
| 4234 | static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
| 4235 | { |
| 4236 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4237 | |
| 4238 | if (!pi_test_and_clear_on(&vmx->pi_desc)) |
| 4239 | return; |
| 4240 | |
| 4241 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir); |
| 4242 | } |
| 4243 | |
| 4244 | static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu) |
| 4245 | { |
| 4246 | return; |
| 4247 | } |
| 4248 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4249 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4250 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 4251 | * will not change in the lifetime of the guest. |
| 4252 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 4253 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 4254 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4255 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4256 | { |
| 4257 | u32 low32, high32; |
| 4258 | unsigned long tmpl; |
| 4259 | struct desc_ptr dt; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 4260 | unsigned long cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4261 | |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 4262 | vmcs_writel(HOST_CR0, read_cr0() & ~X86_CR0_TS); /* 22.2.3 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4263 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 4264 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 4265 | /* Save the most likely value for this task's CR4 in the VMCS. */ |
| 4266 | cr4 = read_cr4(); |
| 4267 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
| 4268 | vmx->host_state.vmcs_host_cr4 = cr4; |
| 4269 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4270 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 4271 | #ifdef CONFIG_X86_64 |
| 4272 | /* |
| 4273 | * Load null selectors, so we can avoid reloading them in |
| 4274 | * __vmx_load_host_state(), in case userspace uses the null selectors |
| 4275 | * too (the expected case). |
| 4276 | */ |
| 4277 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 4278 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 4279 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4280 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 4281 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 4282 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4283 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 4284 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 4285 | |
| 4286 | native_store_idt(&dt); |
| 4287 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4288 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4289 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 4290 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4291 | |
| 4292 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 4293 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 4294 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 4295 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 4296 | |
| 4297 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 4298 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 4299 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 4300 | } |
| 4301 | } |
| 4302 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4303 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 4304 | { |
| 4305 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 4306 | if (enable_ept) |
| 4307 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 4308 | if (is_guest_mode(&vmx->vcpu)) |
| 4309 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 4310 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4311 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 4312 | } |
| 4313 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4314 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 4315 | { |
| 4316 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 4317 | |
| 4318 | if (!vmx_vm_has_apicv(vmx->vcpu.kvm)) |
| 4319 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
| 4320 | return pin_based_exec_ctrl; |
| 4321 | } |
| 4322 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4323 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 4324 | { |
| 4325 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
Paolo Bonzini | d16c293 | 2014-02-21 10:36:37 +0100 | [diff] [blame] | 4326 | |
| 4327 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 4328 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 4329 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4330 | if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) { |
| 4331 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 4332 | #ifdef CONFIG_X86_64 |
| 4333 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 4334 | CPU_BASED_CR8_LOAD_EXITING; |
| 4335 | #endif |
| 4336 | } |
| 4337 | if (!enable_ept) |
| 4338 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 4339 | CPU_BASED_CR3_LOAD_EXITING | |
| 4340 | CPU_BASED_INVLPG_EXITING; |
| 4341 | return exec_control; |
| 4342 | } |
| 4343 | |
| 4344 | static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) |
| 4345 | { |
| 4346 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
| 4347 | if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 4348 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 4349 | if (vmx->vpid == 0) |
| 4350 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 4351 | if (!enable_ept) { |
| 4352 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 4353 | enable_unrestricted_guest = 0; |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 4354 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
| 4355 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4356 | } |
| 4357 | if (!enable_unrestricted_guest) |
| 4358 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 4359 | if (!ple_gap) |
| 4360 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4361 | if (!vmx_vm_has_apicv(vmx->vcpu.kvm)) |
| 4362 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 4363 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4364 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 4365 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 4366 | (handle_vmptrld). |
| 4367 | We can NOT enable shadow_vmcs here because we don't have yet |
| 4368 | a current VMCS12 |
| 4369 | */ |
| 4370 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 4371 | /* PML is enabled/disabled in creating/destorying vcpu */ |
| 4372 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
| 4373 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4374 | return exec_control; |
| 4375 | } |
| 4376 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 4377 | static void ept_set_mmio_spte_mask(void) |
| 4378 | { |
| 4379 | /* |
| 4380 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 4381 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | 885032b | 2013-06-07 16:51:23 +0800 | [diff] [blame] | 4382 | * Also, magic bits (0x3ull << 62) is set to quickly identify mmio |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 4383 | * spte. |
| 4384 | */ |
Xiao Guangrong | 885032b | 2013-06-07 16:51:23 +0800 | [diff] [blame] | 4385 | kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 4386 | } |
| 4387 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 4388 | #define VMX_XSS_EXIT_BITMAP 0 |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4389 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4390 | * Sets up the vmcs for emulated real mode. |
| 4391 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 4392 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4393 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 4394 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4395 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 4396 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4397 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4398 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4399 | /* I/O */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4400 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
| 4401 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4402 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 4403 | if (enable_shadow_vmcs) { |
| 4404 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
| 4405 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 4406 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4407 | if (cpu_has_vmx_msr_bitmap()) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4408 | vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4409 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4410 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 4411 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4412 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4413 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx)); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4414 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4415 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4416 | |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 4417 | if (cpu_has_secondary_exec_ctrls()) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4418 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 4419 | vmx_secondary_exec_control(vmx)); |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 4420 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4421 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4422 | if (vmx_vm_has_apicv(vmx->vcpu.kvm)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4423 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 4424 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 4425 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 4426 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 4427 | |
| 4428 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4429 | |
| 4430 | vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
| 4431 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4432 | } |
| 4433 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4434 | if (ple_gap) { |
| 4435 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 4436 | vmx->ple_window = ple_window; |
| 4437 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4438 | } |
| 4439 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 4440 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 4441 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4442 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 4443 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 4444 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 4445 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4446 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4447 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4448 | rdmsrl(MSR_FS_BASE, a); |
| 4449 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 4450 | rdmsrl(MSR_GS_BASE, a); |
| 4451 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 4452 | #else |
| 4453 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 4454 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 4455 | #endif |
| 4456 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4457 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 4458 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 4459 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4460 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 4461 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4462 | |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 4463 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4464 | u32 msr_low, msr_high; |
| 4465 | u64 host_pat; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 4466 | rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high); |
| 4467 | host_pat = msr_low | ((u64) msr_high << 32); |
| 4468 | /* Write the default value follow host pat */ |
| 4469 | vmcs_write64(GUEST_IA32_PAT, host_pat); |
| 4470 | /* Keep arch.pat sync with GUEST_IA32_PAT */ |
| 4471 | vmx->vcpu.arch.pat = host_pat; |
| 4472 | } |
| 4473 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 4474 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4475 | u32 index = vmx_msr_index[i]; |
| 4476 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4477 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4478 | |
| 4479 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 4480 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 4481 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 4482 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4483 | vmx->guest_msrs[j].index = i; |
| 4484 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 4485 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4486 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4487 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4488 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4489 | |
| 4490 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4491 | |
| 4492 | /* 22.2.1, 20.8.1 */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4493 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4494 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4495 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4496 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4497 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 4498 | if (vmx_xsaves_supported()) |
| 4499 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 4500 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4501 | return 0; |
| 4502 | } |
| 4503 | |
Jan Kiszka | 57f252f | 2013-03-12 10:20:24 +0100 | [diff] [blame] | 4504 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4505 | { |
| 4506 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 4507 | struct msr_data apic_base_msr; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4508 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4509 | vmx->rmode.vm86_active = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4510 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4511 | vmx->soft_vnmi_blocked = 0; |
| 4512 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4513 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 4514 | kvm_set_cr8(&vmx->vcpu, 0); |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 4515 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE; |
Gleb Natapov | c5af89b | 2009-06-09 15:56:26 +0300 | [diff] [blame] | 4516 | if (kvm_vcpu_is_bsp(&vmx->vcpu)) |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 4517 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 4518 | apic_base_msr.host_initiated = true; |
| 4519 | kvm_set_apic_base(&vmx->vcpu, &apic_base_msr); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4520 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4521 | vmx_segment_cache_clear(vmx); |
| 4522 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 4523 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 4524 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | 04b6683 | 2013-03-19 16:30:26 +0100 | [diff] [blame] | 4525 | vmcs_write32(GUEST_CS_BASE, 0xffff0000); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4526 | |
| 4527 | seg_setup(VCPU_SREG_DS); |
| 4528 | seg_setup(VCPU_SREG_ES); |
| 4529 | seg_setup(VCPU_SREG_FS); |
| 4530 | seg_setup(VCPU_SREG_GS); |
| 4531 | seg_setup(VCPU_SREG_SS); |
| 4532 | |
| 4533 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 4534 | vmcs_writel(GUEST_TR_BASE, 0); |
| 4535 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 4536 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 4537 | |
| 4538 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 4539 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 4540 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 4541 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 4542 | |
| 4543 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 4544 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 4545 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 4546 | |
| 4547 | vmcs_writel(GUEST_RFLAGS, 0x02); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 4548 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4549 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4550 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 4551 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 4552 | |
| 4553 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 4554 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 4555 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 4556 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4557 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
| 4558 | vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
| 4559 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4560 | /* Special registers */ |
| 4561 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 4562 | |
| 4563 | setup_msrs(vmx); |
| 4564 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4565 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 4566 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4567 | if (cpu_has_vmx_tpr_shadow()) { |
| 4568 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
| 4569 | if (vm_need_tpr_shadow(vmx->vcpu.kvm)) |
| 4570 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Takuya Yoshikawa | afc2018 | 2011-03-05 12:40:20 +0900 | [diff] [blame] | 4571 | __pa(vmx->vcpu.arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4572 | vmcs_write32(TPR_THRESHOLD, 0); |
| 4573 | } |
| 4574 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 4575 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4576 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4577 | if (vmx_vm_has_apicv(vcpu->kvm)) |
| 4578 | memset(&vmx->pi_desc, 0, sizeof(struct pi_desc)); |
| 4579 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4580 | if (vmx->vpid != 0) |
| 4581 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 4582 | |
Eduardo Habkost | fa40052 | 2009-10-24 02:49:58 -0200 | [diff] [blame] | 4583 | 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] | 4584 | vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 4585 | vmx_set_cr4(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 4586 | vmx_set_efer(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 4587 | vmx_fpu_activate(&vmx->vcpu); |
| 4588 | update_exception_bitmap(&vmx->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4589 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 4590 | vpid_sync_context(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4591 | } |
| 4592 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 4593 | /* |
| 4594 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 4595 | * For most existing hypervisors, this will always return true. |
| 4596 | */ |
| 4597 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 4598 | { |
| 4599 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 4600 | PIN_BASED_EXT_INTR_MASK; |
| 4601 | } |
| 4602 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 4603 | /* |
| 4604 | * In nested virtualization, check if L1 has set |
| 4605 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 4606 | */ |
| 4607 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 4608 | { |
| 4609 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 4610 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 4611 | } |
| 4612 | |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 4613 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 4614 | { |
| 4615 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 4616 | PIN_BASED_NMI_EXITING; |
| 4617 | } |
| 4618 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 4619 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4620 | { |
| 4621 | u32 cpu_based_vm_exec_control; |
Jan Kiszka | 730dca4 | 2013-04-28 10:50:52 +0200 | [diff] [blame] | 4622 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4623 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4624 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 4625 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 4626 | } |
| 4627 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 4628 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4629 | { |
| 4630 | u32 cpu_based_vm_exec_control; |
| 4631 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 4632 | if (!cpu_has_virtual_nmis() || |
| 4633 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
| 4634 | enable_irq_window(vcpu); |
| 4635 | return; |
| 4636 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 4637 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4638 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4639 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING; |
| 4640 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 4641 | } |
| 4642 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4643 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4644 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 4645 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4646 | uint32_t intr; |
| 4647 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 4648 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4649 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4650 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 4651 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4652 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 4653 | int inc_eip = 0; |
| 4654 | if (vcpu->arch.interrupt.soft) |
| 4655 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 4656 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 4657 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4658 | return; |
| 4659 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4660 | intr = irq | INTR_INFO_VALID_MASK; |
| 4661 | if (vcpu->arch.interrupt.soft) { |
| 4662 | intr |= INTR_TYPE_SOFT_INTR; |
| 4663 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 4664 | vmx->vcpu.arch.event_exit_inst_len); |
| 4665 | } else |
| 4666 | intr |= INTR_TYPE_EXT_INTR; |
| 4667 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4668 | } |
| 4669 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4670 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 4671 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 4672 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4673 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 4674 | if (is_guest_mode(vcpu)) |
| 4675 | return; |
| 4676 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4677 | if (!cpu_has_virtual_nmis()) { |
| 4678 | /* |
| 4679 | * Tracking the NMI-blocked state in software is built upon |
| 4680 | * finding the next open IRQ window. This, in turn, depends on |
| 4681 | * well-behaving guests: They have to keep IRQs disabled at |
| 4682 | * least as long as the NMI handler runs. Otherwise we may |
| 4683 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 4684 | * highly unlikely, we can live with the residual risk. |
| 4685 | */ |
| 4686 | vmx->soft_vnmi_blocked = 1; |
| 4687 | vmx->vnmi_blocked_time = 0; |
| 4688 | } |
| 4689 | |
Jan Kiszka | 487b391 | 2008-09-26 09:30:56 +0200 | [diff] [blame] | 4690 | ++vcpu->stat.nmi_injections; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 4691 | vmx->nmi_known_unmasked = false; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4692 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 4693 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 4694 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 4695 | return; |
| 4696 | } |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4697 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 4698 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4699 | } |
| 4700 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4701 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 4702 | { |
| 4703 | if (!cpu_has_virtual_nmis()) |
| 4704 | return to_vmx(vcpu)->soft_vnmi_blocked; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 4705 | if (to_vmx(vcpu)->nmi_known_unmasked) |
| 4706 | return false; |
Avi Kivity | c332c83 | 2010-05-04 12:24:12 +0300 | [diff] [blame] | 4707 | return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 4711 | { |
| 4712 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4713 | |
| 4714 | if (!cpu_has_virtual_nmis()) { |
| 4715 | if (vmx->soft_vnmi_blocked != masked) { |
| 4716 | vmx->soft_vnmi_blocked = masked; |
| 4717 | vmx->vnmi_blocked_time = 0; |
| 4718 | } |
| 4719 | } else { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 4720 | vmx->nmi_known_unmasked = !masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4721 | if (masked) |
| 4722 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 4723 | GUEST_INTR_STATE_NMI); |
| 4724 | else |
| 4725 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 4726 | GUEST_INTR_STATE_NMI); |
| 4727 | } |
| 4728 | } |
| 4729 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 4730 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 4731 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 4732 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 4733 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 4734 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 4735 | if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked) |
| 4736 | return 0; |
| 4737 | |
| 4738 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 4739 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 4740 | | GUEST_INTR_STATE_NMI)); |
| 4741 | } |
| 4742 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4743 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 4744 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 4745 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 4746 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4747 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 4748 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4749 | } |
| 4750 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4751 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 4752 | { |
| 4753 | int ret; |
| 4754 | struct kvm_userspace_memory_region tss_mem = { |
Sheng Yang | 6fe6397 | 2008-10-16 17:30:58 +0800 | [diff] [blame] | 4755 | .slot = TSS_PRIVATE_MEMSLOT, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4756 | .guest_phys_addr = addr, |
| 4757 | .memory_size = PAGE_SIZE * 3, |
| 4758 | .flags = 0, |
| 4759 | }; |
| 4760 | |
Takuya Yoshikawa | 47ae31e | 2013-02-27 19:43:00 +0900 | [diff] [blame] | 4761 | ret = kvm_set_memory_region(kvm, &tss_mem); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4762 | if (ret) |
| 4763 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 4764 | kvm->arch.tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 4765 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4766 | } |
| 4767 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4768 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4769 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4770 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4771 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 4772 | /* |
| 4773 | * Update instruction length as we may reinject the exception |
| 4774 | * from user space while in guest debugging mode. |
| 4775 | */ |
| 4776 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 4777 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4778 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4779 | return false; |
| 4780 | /* fall through */ |
| 4781 | case DB_VECTOR: |
| 4782 | if (vcpu->guest_debug & |
| 4783 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 4784 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4785 | /* fall through */ |
| 4786 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4787 | case OF_VECTOR: |
| 4788 | case BR_VECTOR: |
| 4789 | case UD_VECTOR: |
| 4790 | case DF_VECTOR: |
| 4791 | case SS_VECTOR: |
| 4792 | case GP_VECTOR: |
| 4793 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4794 | return true; |
| 4795 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4796 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4797 | return false; |
| 4798 | } |
| 4799 | |
| 4800 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 4801 | int vec, u32 err_code) |
| 4802 | { |
| 4803 | /* |
| 4804 | * Instruction with address size override prefix opcode 0x67 |
| 4805 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 4806 | */ |
| 4807 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
| 4808 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
| 4809 | if (vcpu->arch.halt_request) { |
| 4810 | vcpu->arch.halt_request = 0; |
| 4811 | return kvm_emulate_halt(vcpu); |
| 4812 | } |
| 4813 | return 1; |
| 4814 | } |
| 4815 | return 0; |
| 4816 | } |
| 4817 | |
| 4818 | /* |
| 4819 | * Forward all other exceptions that are valid in real mode. |
| 4820 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 4821 | * the required debugging infrastructure rework. |
| 4822 | */ |
| 4823 | kvm_queue_exception(vcpu, vec); |
| 4824 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4825 | } |
| 4826 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4827 | /* |
| 4828 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 4829 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 4830 | * We pass a fake environment to the machine check handler because we want |
| 4831 | * the guest to be always treated like user space, no matter what context |
| 4832 | * it used internally. |
| 4833 | */ |
| 4834 | static void kvm_machine_check(void) |
| 4835 | { |
| 4836 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 4837 | struct pt_regs regs = { |
| 4838 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 4839 | .flags = X86_EFLAGS_IF, |
| 4840 | }; |
| 4841 | |
| 4842 | do_machine_check(®s, 0); |
| 4843 | #endif |
| 4844 | } |
| 4845 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4846 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4847 | { |
| 4848 | /* already handled by vcpu_run */ |
| 4849 | return 1; |
| 4850 | } |
| 4851 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4852 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4853 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 4854 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4855 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4856 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4857 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4858 | u32 vect_info; |
| 4859 | enum emulation_result er; |
| 4860 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 4861 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 4862 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4863 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4864 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4865 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4866 | |
Jan Kiszka | e4a4188 | 2008-09-26 09:30:46 +0200 | [diff] [blame] | 4867 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 4868 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 4869 | |
| 4870 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 4871 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 4872 | return 1; |
| 4873 | } |
| 4874 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4875 | if (is_invalid_opcode(intr_info)) { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4876 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4877 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 4878 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4879 | return 1; |
| 4880 | } |
| 4881 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4882 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 4883 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4884 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 4885 | |
| 4886 | /* |
| 4887 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 4888 | * MMIO, it is better to report an internal error. |
| 4889 | * See the comments in vmx_handle_exit. |
| 4890 | */ |
| 4891 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 4892 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 4893 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4894 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
| 4895 | vcpu->run->internal.ndata = 2; |
| 4896 | vcpu->run->internal.data[0] = vect_info; |
| 4897 | vcpu->run->internal.data[1] = intr_info; |
| 4898 | return 0; |
| 4899 | } |
| 4900 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4901 | if (is_page_fault(intr_info)) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4902 | /* EPT won't cause page fault directly */ |
Julia Lawall | cf3ace7 | 2011-08-02 12:34:57 +0200 | [diff] [blame] | 4903 | BUG_ON(enable_ept); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4904 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4905 | trace_kvm_page_fault(cr2, error_code); |
| 4906 | |
Gleb Natapov | 3298b75 | 2009-05-11 13:35:46 +0300 | [diff] [blame] | 4907 | if (kvm_event_needs_reinjection(vcpu)) |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 4908 | kvm_mmu_unprotect_page_virt(vcpu, cr2); |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 4909 | return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4910 | } |
| 4911 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4912 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4913 | |
| 4914 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 4915 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 4916 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4917 | switch (ex_no) { |
| 4918 | case DB_VECTOR: |
| 4919 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 4920 | if (!(vcpu->guest_debug & |
| 4921 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 4922 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 4923 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 4924 | if (!(dr6 & ~DR6_RESERVED)) /* icebp */ |
| 4925 | skip_emulated_instruction(vcpu); |
| 4926 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4927 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 4928 | return 1; |
| 4929 | } |
| 4930 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 4931 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 4932 | /* fall through */ |
| 4933 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 4934 | /* |
| 4935 | * Update instruction length as we may reinject #BP from |
| 4936 | * user space while in guest debugging mode. Reading it for |
| 4937 | * #DB as well causes no harm, it is not used in that case. |
| 4938 | */ |
| 4939 | vmx->vcpu.arch.event_exit_inst_len = |
| 4940 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4941 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 4942 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4943 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 4944 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4945 | break; |
| 4946 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4947 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 4948 | kvm_run->ex.exception = ex_no; |
| 4949 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4950 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4951 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4952 | return 0; |
| 4953 | } |
| 4954 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4955 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4956 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 4957 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4958 | return 1; |
| 4959 | } |
| 4960 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4961 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4962 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4963 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4964 | return 0; |
| 4965 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4966 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4967 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4968 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4969 | unsigned long exit_qualification; |
Jan Kiszka | 34c33d1 | 2009-02-08 13:28:15 +0100 | [diff] [blame] | 4970 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4971 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4972 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4973 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4974 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 4975 | in = (exit_qualification & 8) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 4976 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4977 | ++vcpu->stat.io_exits; |
| 4978 | |
| 4979 | if (string || in) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 4980 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4981 | |
| 4982 | port = exit_qualification >> 16; |
| 4983 | size = (exit_qualification & 7) + 1; |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 4984 | skip_emulated_instruction(vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4985 | |
| 4986 | return kvm_fast_pio_out(vcpu, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4987 | } |
| 4988 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4989 | static void |
| 4990 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 4991 | { |
| 4992 | /* |
| 4993 | * Patch in the VMCALL instruction: |
| 4994 | */ |
| 4995 | hypercall[0] = 0x0f; |
| 4996 | hypercall[1] = 0x01; |
| 4997 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4998 | } |
| 4999 | |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5000 | static bool nested_cr0_valid(struct vmcs12 *vmcs12, unsigned long val) |
| 5001 | { |
| 5002 | unsigned long always_on = VMXON_CR0_ALWAYSON; |
| 5003 | |
| 5004 | if (nested_vmx_secondary_ctls_high & |
| 5005 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
| 5006 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 5007 | always_on &= ~(X86_CR0_PE | X86_CR0_PG); |
| 5008 | return (val & always_on) == always_on; |
| 5009 | } |
| 5010 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 5011 | /* called to set cr0 as appropriate for a mov-to-cr0 exit. */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5012 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 5013 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5014 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5015 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5016 | unsigned long orig_val = val; |
| 5017 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5018 | /* |
| 5019 | * We get here when L2 changed cr0 in a way that did not change |
| 5020 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5021 | * but did change L0 shadowed bits. So we first calculate the |
| 5022 | * effective cr0 value that L1 would like to write into the |
| 5023 | * hardware. It consists of the L2-owned bits from the new |
| 5024 | * value combined with the L1-owned bits from L1's guest_cr0. |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5025 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5026 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 5027 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 5028 | |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5029 | if (!nested_cr0_valid(vmcs12, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5030 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5031 | |
| 5032 | if (kvm_set_cr0(vcpu, val)) |
| 5033 | return 1; |
| 5034 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5035 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5036 | } else { |
| 5037 | if (to_vmx(vcpu)->nested.vmxon && |
| 5038 | ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON)) |
| 5039 | return 1; |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5040 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5041 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5042 | } |
| 5043 | |
| 5044 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 5045 | { |
| 5046 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5047 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5048 | unsigned long orig_val = val; |
| 5049 | |
| 5050 | /* analogously to handle_set_cr0 */ |
| 5051 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 5052 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 5053 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5054 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5055 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5056 | return 0; |
| 5057 | } else |
| 5058 | return kvm_set_cr4(vcpu, val); |
| 5059 | } |
| 5060 | |
| 5061 | /* called to set cr0 as approriate for clts instruction exit. */ |
| 5062 | static void handle_clts(struct kvm_vcpu *vcpu) |
| 5063 | { |
| 5064 | if (is_guest_mode(vcpu)) { |
| 5065 | /* |
| 5066 | * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS |
| 5067 | * but we did (!fpu_active). We need to keep GUEST_CR0.TS on, |
| 5068 | * just pretend it's off (also in arch.cr0 for fpu_activate). |
| 5069 | */ |
| 5070 | vmcs_writel(CR0_READ_SHADOW, |
| 5071 | vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS); |
| 5072 | vcpu->arch.cr0 &= ~X86_CR0_TS; |
| 5073 | } else |
| 5074 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
| 5075 | } |
| 5076 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5077 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5078 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5079 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5080 | int cr; |
| 5081 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 5082 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5083 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5084 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5085 | cr = exit_qualification & 15; |
| 5086 | reg = (exit_qualification >> 8) & 15; |
| 5087 | switch ((exit_qualification >> 4) & 3) { |
| 5088 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 5089 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5090 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5091 | switch (cr) { |
| 5092 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5093 | err = handle_set_cr0(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5094 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5095 | return 1; |
| 5096 | case 3: |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 5097 | err = kvm_set_cr3(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5098 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5099 | return 1; |
| 5100 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5101 | err = handle_set_cr4(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5102 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5103 | return 1; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5104 | case 8: { |
| 5105 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 5106 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 5107 | err = kvm_set_cr8(vcpu, cr8); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5108 | kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5109 | if (irqchip_in_kernel(vcpu->kvm)) |
| 5110 | return 1; |
| 5111 | if (cr8_prev <= cr8) |
| 5112 | return 1; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5113 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5114 | return 0; |
| 5115 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 5116 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5117 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5118 | case 2: /* clts */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5119 | handle_clts(vcpu); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5120 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5121 | skip_emulated_instruction(vcpu); |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 5122 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5123 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5124 | case 1: /*mov from cr*/ |
| 5125 | switch (cr) { |
| 5126 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 5127 | val = kvm_read_cr3(vcpu); |
| 5128 | kvm_register_write(vcpu, reg, val); |
| 5129 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5130 | skip_emulated_instruction(vcpu); |
| 5131 | return 1; |
| 5132 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5133 | val = kvm_get_cr8(vcpu); |
| 5134 | kvm_register_write(vcpu, reg, val); |
| 5135 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5136 | skip_emulated_instruction(vcpu); |
| 5137 | return 1; |
| 5138 | } |
| 5139 | break; |
| 5140 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 5141 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5142 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 5143 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5144 | |
| 5145 | skip_emulated_instruction(vcpu); |
| 5146 | return 1; |
| 5147 | default: |
| 5148 | break; |
| 5149 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5150 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 5151 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5152 | (int)(exit_qualification >> 4) & 3, cr); |
| 5153 | return 0; |
| 5154 | } |
| 5155 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5156 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5157 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5158 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 5159 | int dr, dr7, reg; |
| 5160 | |
| 5161 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5162 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 5163 | |
| 5164 | /* First, if DR does not exist, trigger UD */ |
| 5165 | if (!kvm_require_dr(vcpu, dr)) |
| 5166 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5167 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 5168 | /* 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] | 5169 | if (!kvm_require_cpl(vcpu, 0)) |
| 5170 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 5171 | dr7 = vmcs_readl(GUEST_DR7); |
| 5172 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5173 | /* |
| 5174 | * As the vm-exit takes precedence over the debug trap, we |
| 5175 | * need to emulate the latter, either for the host or the |
| 5176 | * guest debugging itself. |
| 5177 | */ |
| 5178 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5179 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 5180 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 5181 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5182 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 5183 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5184 | return 0; |
| 5185 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 5186 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 5187 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5188 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 5189 | return 1; |
| 5190 | } |
| 5191 | } |
| 5192 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 5193 | if (vcpu->guest_debug == 0) { |
| 5194 | u32 cpu_based_vm_exec_control; |
| 5195 | |
| 5196 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5197 | cpu_based_vm_exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 5198 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 5199 | |
| 5200 | /* |
| 5201 | * No more DR vmexits; force a reload of the debug registers |
| 5202 | * and reenter on this instruction. The next vmexit will |
| 5203 | * retrieve the full state of the debug registers. |
| 5204 | */ |
| 5205 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 5206 | return 1; |
| 5207 | } |
| 5208 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5209 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 5210 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 5211 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 5212 | |
| 5213 | if (kvm_get_dr(vcpu, dr, &val)) |
| 5214 | return 1; |
| 5215 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 5216 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 5217 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 5218 | return 1; |
| 5219 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5220 | skip_emulated_instruction(vcpu); |
| 5221 | return 1; |
| 5222 | } |
| 5223 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 5224 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 5225 | { |
| 5226 | return vcpu->arch.dr6; |
| 5227 | } |
| 5228 | |
| 5229 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 5230 | { |
| 5231 | } |
| 5232 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 5233 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 5234 | { |
| 5235 | u32 cpu_based_vm_exec_control; |
| 5236 | |
| 5237 | get_debugreg(vcpu->arch.db[0], 0); |
| 5238 | get_debugreg(vcpu->arch.db[1], 1); |
| 5239 | get_debugreg(vcpu->arch.db[2], 2); |
| 5240 | get_debugreg(vcpu->arch.db[3], 3); |
| 5241 | get_debugreg(vcpu->arch.dr6, 6); |
| 5242 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 5243 | |
| 5244 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
| 5245 | |
| 5246 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5247 | cpu_based_vm_exec_control |= CPU_BASED_MOV_DR_EXITING; |
| 5248 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 5249 | } |
| 5250 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 5251 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 5252 | { |
| 5253 | vmcs_writel(GUEST_DR7, val); |
| 5254 | } |
| 5255 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5256 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5257 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 5258 | kvm_emulate_cpuid(vcpu); |
| 5259 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5260 | } |
| 5261 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5262 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5263 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5264 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5265 | u64 data; |
| 5266 | |
| 5267 | if (vmx_get_msr(vcpu, ecx, &data)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 5268 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 5269 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5270 | return 1; |
| 5271 | } |
| 5272 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5273 | trace_kvm_msr_read(ecx, data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 5274 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5275 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5276 | vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u; |
| 5277 | vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5278 | skip_emulated_instruction(vcpu); |
| 5279 | return 1; |
| 5280 | } |
| 5281 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5282 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5283 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 5284 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5285 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 5286 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 5287 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5288 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 5289 | msr.data = data; |
| 5290 | msr.index = ecx; |
| 5291 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 5292 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 5293 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 5294 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5295 | return 1; |
| 5296 | } |
| 5297 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 5298 | trace_kvm_msr_write(ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5299 | skip_emulated_instruction(vcpu); |
| 5300 | return 1; |
| 5301 | } |
| 5302 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5303 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5304 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5305 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5306 | return 1; |
| 5307 | } |
| 5308 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5309 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5310 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5311 | u32 cpu_based_vm_exec_control; |
| 5312 | |
| 5313 | /* clear pending irq */ |
| 5314 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5315 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 5316 | 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] | 5317 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5318 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 5319 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 5320 | ++vcpu->stat.irq_window_exits; |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 5321 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 5322 | /* |
| 5323 | * If the user space waits to inject interrupts, exit as soon as |
| 5324 | * possible |
| 5325 | */ |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 5326 | if (!irqchip_in_kernel(vcpu->kvm) && |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5327 | vcpu->run->request_interrupt_window && |
Gleb Natapov | 8061823 | 2009-04-21 17:44:56 +0300 | [diff] [blame] | 5328 | !kvm_cpu_has_interrupt(vcpu)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5329 | vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 5330 | return 0; |
| 5331 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5332 | return 1; |
| 5333 | } |
| 5334 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5335 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5336 | { |
| 5337 | skip_emulated_instruction(vcpu); |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 5338 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5339 | } |
| 5340 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5341 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5342 | { |
Dor Laor | 510043d | 2007-02-19 18:25:43 +0200 | [diff] [blame] | 5343 | skip_emulated_instruction(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5344 | kvm_emulate_hypercall(vcpu); |
| 5345 | return 1; |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5346 | } |
| 5347 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 5348 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 5349 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5350 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 5351 | } |
| 5352 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5353 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5354 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 5355 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5356 | |
| 5357 | kvm_mmu_invlpg(vcpu, exit_qualification); |
| 5358 | skip_emulated_instruction(vcpu); |
| 5359 | return 1; |
| 5360 | } |
| 5361 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 5362 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 5363 | { |
| 5364 | int err; |
| 5365 | |
| 5366 | err = kvm_rdpmc(vcpu); |
| 5367 | kvm_complete_insn_gp(vcpu, err); |
| 5368 | |
| 5369 | return 1; |
| 5370 | } |
| 5371 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5372 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 5373 | { |
| 5374 | skip_emulated_instruction(vcpu); |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5375 | kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 5376 | return 1; |
| 5377 | } |
| 5378 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 5379 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 5380 | { |
| 5381 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 5382 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 5383 | |
| 5384 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
| 5385 | skip_emulated_instruction(vcpu); |
| 5386 | return 1; |
| 5387 | } |
| 5388 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5389 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 5390 | { |
| 5391 | skip_emulated_instruction(vcpu); |
| 5392 | WARN(1, "this should never happen\n"); |
| 5393 | return 1; |
| 5394 | } |
| 5395 | |
| 5396 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 5397 | { |
| 5398 | skip_emulated_instruction(vcpu); |
| 5399 | WARN(1, "this should never happen\n"); |
| 5400 | return 1; |
| 5401 | } |
| 5402 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5403 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5404 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 5405 | if (likely(fasteoi)) { |
| 5406 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5407 | int access_type, offset; |
| 5408 | |
| 5409 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 5410 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 5411 | /* |
| 5412 | * Sane guest uses MOV to write EOI, with written value |
| 5413 | * not cared. So make a short-circuit here by avoiding |
| 5414 | * heavy instruction emulation. |
| 5415 | */ |
| 5416 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 5417 | (offset == APIC_EOI)) { |
| 5418 | kvm_lapic_set_eoi(vcpu); |
| 5419 | skip_emulated_instruction(vcpu); |
| 5420 | return 1; |
| 5421 | } |
| 5422 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5423 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5424 | } |
| 5425 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5426 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 5427 | { |
| 5428 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5429 | int vector = exit_qualification & 0xff; |
| 5430 | |
| 5431 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 5432 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 5433 | return 1; |
| 5434 | } |
| 5435 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 5436 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 5437 | { |
| 5438 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5439 | u32 offset = exit_qualification & 0xfff; |
| 5440 | |
| 5441 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 5442 | kvm_apic_write_nodecode(vcpu, offset); |
| 5443 | return 1; |
| 5444 | } |
| 5445 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5446 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5447 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 5448 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5449 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 5450 | bool has_error_code = false; |
| 5451 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5452 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 5453 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5454 | |
| 5455 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 5456 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5457 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5458 | |
| 5459 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5460 | |
| 5461 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5462 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 5463 | switch (type) { |
| 5464 | case INTR_TYPE_NMI_INTR: |
| 5465 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 5466 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5467 | break; |
| 5468 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5469 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5470 | kvm_clear_interrupt_queue(vcpu); |
| 5471 | break; |
| 5472 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 5473 | if (vmx->idt_vectoring_info & |
| 5474 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 5475 | has_error_code = true; |
| 5476 | error_code = |
| 5477 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 5478 | } |
| 5479 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5480 | case INTR_TYPE_SOFT_EXCEPTION: |
| 5481 | kvm_clear_exception_queue(vcpu); |
| 5482 | break; |
| 5483 | default: |
| 5484 | break; |
| 5485 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 5486 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5487 | tss_selector = exit_qualification; |
| 5488 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5489 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 5490 | type != INTR_TYPE_EXT_INTR && |
| 5491 | type != INTR_TYPE_NMI_INTR)) |
| 5492 | skip_emulated_instruction(vcpu); |
| 5493 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 5494 | if (kvm_task_switch(vcpu, tss_selector, |
| 5495 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 5496 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 5497 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 5498 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 5499 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5500 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 5501 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5502 | |
| 5503 | /* clear all local breakpoint enable flags */ |
Nadav Amit | 0e8a0996 | 2014-10-03 01:10:02 +0300 | [diff] [blame] | 5504 | vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~0x155); |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5505 | |
| 5506 | /* |
| 5507 | * TODO: What about debug traps on tss switch? |
| 5508 | * Are we supposed to inject them and update dr6? |
| 5509 | */ |
| 5510 | |
| 5511 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5512 | } |
| 5513 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5514 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5515 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 5516 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5517 | gpa_t gpa; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 5518 | u32 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5519 | int gla_validity; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5520 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 5521 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5522 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5523 | gla_validity = (exit_qualification >> 7) & 0x3; |
| 5524 | if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) { |
| 5525 | printk(KERN_ERR "EPT: Handling EPT violation failed!\n"); |
| 5526 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 5527 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 5528 | vmcs_readl(GUEST_LINEAR_ADDRESS)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5529 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 5530 | (long unsigned int)exit_qualification); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5531 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 5532 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION; |
Avi Kivity | 596ae89 | 2009-06-03 14:12:10 +0300 | [diff] [blame] | 5533 | return 0; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5534 | } |
| 5535 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 5536 | /* |
| 5537 | * EPT violation happened while executing iret from NMI, |
| 5538 | * "blocked by NMI" bit has to be set before next VM entry. |
| 5539 | * There are errata that may cause this bit to not be set: |
| 5540 | * AAK134, BY25. |
| 5541 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 5542 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
| 5543 | cpu_has_virtual_nmis() && |
| 5544 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 5545 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 5546 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5547 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5548 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 5549 | |
| 5550 | /* It is a write fault? */ |
Tiejun Chen | 81ed33e | 2014-11-18 17:12:56 +0800 | [diff] [blame] | 5551 | error_code = exit_qualification & PFERR_WRITE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 5552 | /* It is a fetch fault? */ |
Tiejun Chen | 81ed33e | 2014-11-18 17:12:56 +0800 | [diff] [blame] | 5553 | error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 5554 | /* ept page table is present? */ |
Tiejun Chen | 81ed33e | 2014-11-18 17:12:56 +0800 | [diff] [blame] | 5555 | error_code |= (exit_qualification >> 3) & PFERR_PRESENT_MASK; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 5556 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 5557 | vcpu->arch.exit_qualification = exit_qualification; |
| 5558 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 5559 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5560 | } |
| 5561 | |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5562 | static u64 ept_rsvd_mask(u64 spte, int level) |
| 5563 | { |
| 5564 | int i; |
| 5565 | u64 mask = 0; |
| 5566 | |
| 5567 | for (i = 51; i > boot_cpu_data.x86_phys_bits; i--) |
| 5568 | mask |= (1ULL << i); |
| 5569 | |
Wanpeng Li | a32e845 | 2014-08-20 15:31:53 +0800 | [diff] [blame] | 5570 | if (level == 4) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5571 | /* bits 7:3 reserved */ |
| 5572 | mask |= 0xf8; |
Wanpeng Li | a32e845 | 2014-08-20 15:31:53 +0800 | [diff] [blame] | 5573 | else if (spte & (1ULL << 7)) |
| 5574 | /* |
| 5575 | * 1GB/2MB page, bits 29:12 or 20:12 reserved respectively, |
| 5576 | * level == 1 if the hypervisor is using the ignored bit 7. |
| 5577 | */ |
| 5578 | mask |= (PAGE_SIZE << ((level - 1) * 9)) - PAGE_SIZE; |
| 5579 | else if (level > 1) |
| 5580 | /* bits 6:3 reserved */ |
| 5581 | mask |= 0x78; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5582 | |
| 5583 | return mask; |
| 5584 | } |
| 5585 | |
| 5586 | static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte, |
| 5587 | int level) |
| 5588 | { |
| 5589 | printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level); |
| 5590 | |
| 5591 | /* 010b (write-only) */ |
| 5592 | WARN_ON((spte & 0x7) == 0x2); |
| 5593 | |
| 5594 | /* 110b (write/execute) */ |
| 5595 | WARN_ON((spte & 0x7) == 0x6); |
| 5596 | |
| 5597 | /* 100b (execute-only) and value not supported by logical processor */ |
| 5598 | if (!cpu_has_vmx_ept_execute_only()) |
| 5599 | WARN_ON((spte & 0x7) == 0x4); |
| 5600 | |
| 5601 | /* not 000b */ |
| 5602 | if ((spte & 0x7)) { |
| 5603 | u64 rsvd_bits = spte & ept_rsvd_mask(spte, level); |
| 5604 | |
| 5605 | if (rsvd_bits != 0) { |
| 5606 | printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n", |
| 5607 | __func__, rsvd_bits); |
| 5608 | WARN_ON(1); |
| 5609 | } |
| 5610 | |
Wanpeng Li | a32e845 | 2014-08-20 15:31:53 +0800 | [diff] [blame] | 5611 | /* bits 5:3 are _not_ reserved for large page or leaf page */ |
| 5612 | if ((rsvd_bits & 0x38) == 0) { |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5613 | u64 ept_mem_type = (spte & 0x38) >> 3; |
| 5614 | |
| 5615 | if (ept_mem_type == 2 || ept_mem_type == 3 || |
| 5616 | ept_mem_type == 7) { |
| 5617 | printk(KERN_ERR "%s: ept_mem_type=0x%llx\n", |
| 5618 | __func__, ept_mem_type); |
| 5619 | WARN_ON(1); |
| 5620 | } |
| 5621 | } |
| 5622 | } |
| 5623 | } |
| 5624 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5625 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5626 | { |
| 5627 | u64 sptes[4]; |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5628 | int nr_sptes, i, ret; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5629 | gpa_t gpa; |
| 5630 | |
| 5631 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 5632 | if (!kvm_io_bus_write(vcpu->kvm, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
| 5633 | skip_emulated_instruction(vcpu); |
| 5634 | return 1; |
| 5635 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5636 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5637 | ret = handle_mmio_page_fault_common(vcpu, gpa, true); |
Xiao Guangrong | b37fbea | 2013-06-07 16:51:25 +0800 | [diff] [blame] | 5638 | if (likely(ret == RET_MMIO_PF_EMULATE)) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5639 | return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) == |
| 5640 | EMULATE_DONE; |
Xiao Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 5641 | |
| 5642 | if (unlikely(ret == RET_MMIO_PF_INVALID)) |
| 5643 | return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0); |
| 5644 | |
Xiao Guangrong | b37fbea | 2013-06-07 16:51:25 +0800 | [diff] [blame] | 5645 | if (unlikely(ret == RET_MMIO_PF_RETRY)) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5646 | return 1; |
| 5647 | |
| 5648 | /* It is the real ept misconfig */ |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5649 | printk(KERN_ERR "EPT: Misconfiguration.\n"); |
| 5650 | printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa); |
| 5651 | |
| 5652 | nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes); |
| 5653 | |
| 5654 | for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i) |
| 5655 | ept_misconfig_inspect_spte(vcpu, sptes[i-1], i); |
| 5656 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5657 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 5658 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5659 | |
| 5660 | return 0; |
| 5661 | } |
| 5662 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5663 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5664 | { |
| 5665 | u32 cpu_based_vm_exec_control; |
| 5666 | |
| 5667 | /* clear pending NMI */ |
| 5668 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5669 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 5670 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 5671 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5672 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5673 | |
| 5674 | return 1; |
| 5675 | } |
| 5676 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5677 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5678 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 5679 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5680 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5681 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 5682 | u32 cpu_exec_ctrl; |
| 5683 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 5684 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 5685 | |
| 5686 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5687 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5688 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 5689 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 5690 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 5691 | return handle_interrupt_window(&vmx->vcpu); |
| 5692 | |
Avi Kivity | de87dcdd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 5693 | if (test_bit(KVM_REQ_EVENT, &vcpu->requests)) |
| 5694 | return 1; |
| 5695 | |
Gleb Natapov | 991eebf | 2013-04-11 12:10:51 +0300 | [diff] [blame] | 5696 | err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5697 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 5698 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 5699 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5700 | ret = 0; |
| 5701 | goto out; |
| 5702 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 5703 | |
Avi Kivity | de5f70e | 2012-06-12 20:22:28 +0300 | [diff] [blame] | 5704 | if (err != EMULATE_DONE) { |
| 5705 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 5706 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 5707 | vcpu->run->internal.ndata = 0; |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 5708 | return 0; |
Avi Kivity | de5f70e | 2012-06-12 20:22:28 +0300 | [diff] [blame] | 5709 | } |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5710 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 5711 | if (vcpu->arch.halt_request) { |
| 5712 | vcpu->arch.halt_request = 0; |
| 5713 | ret = kvm_emulate_halt(vcpu); |
| 5714 | goto out; |
| 5715 | } |
| 5716 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5717 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5718 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5719 | if (need_resched()) |
| 5720 | schedule(); |
| 5721 | } |
| 5722 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5723 | out: |
| 5724 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5725 | } |
| 5726 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 5727 | static int __grow_ple_window(int val) |
| 5728 | { |
| 5729 | if (ple_window_grow < 1) |
| 5730 | return ple_window; |
| 5731 | |
| 5732 | val = min(val, ple_window_actual_max); |
| 5733 | |
| 5734 | if (ple_window_grow < ple_window) |
| 5735 | val *= ple_window_grow; |
| 5736 | else |
| 5737 | val += ple_window_grow; |
| 5738 | |
| 5739 | return val; |
| 5740 | } |
| 5741 | |
| 5742 | static int __shrink_ple_window(int val, int modifier, int minimum) |
| 5743 | { |
| 5744 | if (modifier < 1) |
| 5745 | return ple_window; |
| 5746 | |
| 5747 | if (modifier < ple_window) |
| 5748 | val /= modifier; |
| 5749 | else |
| 5750 | val -= modifier; |
| 5751 | |
| 5752 | return max(val, minimum); |
| 5753 | } |
| 5754 | |
| 5755 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 5756 | { |
| 5757 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5758 | int old = vmx->ple_window; |
| 5759 | |
| 5760 | vmx->ple_window = __grow_ple_window(old); |
| 5761 | |
| 5762 | if (vmx->ple_window != old) |
| 5763 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 5764 | |
| 5765 | trace_kvm_ple_window_grow(vcpu->vcpu_id, vmx->ple_window, old); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 5769 | { |
| 5770 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5771 | int old = vmx->ple_window; |
| 5772 | |
| 5773 | vmx->ple_window = __shrink_ple_window(old, |
| 5774 | ple_window_shrink, ple_window); |
| 5775 | |
| 5776 | if (vmx->ple_window != old) |
| 5777 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 5778 | |
| 5779 | trace_kvm_ple_window_shrink(vcpu->vcpu_id, vmx->ple_window, old); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 5780 | } |
| 5781 | |
| 5782 | /* |
| 5783 | * ple_window_actual_max is computed to be one grow_ple_window() below |
| 5784 | * ple_window_max. (See __grow_ple_window for the reason.) |
| 5785 | * This prevents overflows, because ple_window_max is int. |
| 5786 | * ple_window_max effectively rounded down to a multiple of ple_window_grow in |
| 5787 | * this process. |
| 5788 | * ple_window_max is also prevented from setting vmx->ple_window < ple_window. |
| 5789 | */ |
| 5790 | static void update_ple_window_actual_max(void) |
| 5791 | { |
| 5792 | ple_window_actual_max = |
| 5793 | __shrink_ple_window(max(ple_window_max, ple_window), |
| 5794 | ple_window_grow, INT_MIN); |
| 5795 | } |
| 5796 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 5797 | static __init int hardware_setup(void) |
| 5798 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 5799 | int r = -ENOMEM, i, msr; |
| 5800 | |
| 5801 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 5802 | |
| 5803 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 5804 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 5805 | |
| 5806 | vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 5807 | if (!vmx_io_bitmap_a) |
| 5808 | return r; |
| 5809 | |
| 5810 | vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 5811 | if (!vmx_io_bitmap_b) |
| 5812 | goto out; |
| 5813 | |
| 5814 | vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 5815 | if (!vmx_msr_bitmap_legacy) |
| 5816 | goto out1; |
| 5817 | |
| 5818 | vmx_msr_bitmap_legacy_x2apic = |
| 5819 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 5820 | if (!vmx_msr_bitmap_legacy_x2apic) |
| 5821 | goto out2; |
| 5822 | |
| 5823 | vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 5824 | if (!vmx_msr_bitmap_longmode) |
| 5825 | goto out3; |
| 5826 | |
| 5827 | vmx_msr_bitmap_longmode_x2apic = |
| 5828 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 5829 | if (!vmx_msr_bitmap_longmode_x2apic) |
| 5830 | goto out4; |
| 5831 | vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 5832 | if (!vmx_vmread_bitmap) |
| 5833 | goto out5; |
| 5834 | |
| 5835 | vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 5836 | if (!vmx_vmwrite_bitmap) |
| 5837 | goto out6; |
| 5838 | |
| 5839 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 5840 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 5841 | |
| 5842 | /* |
| 5843 | * Allow direct access to the PC debug port (it is often used for I/O |
| 5844 | * delays, but the vmexits simply slow things down). |
| 5845 | */ |
| 5846 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); |
| 5847 | clear_bit(0x80, vmx_io_bitmap_a); |
| 5848 | |
| 5849 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); |
| 5850 | |
| 5851 | memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE); |
| 5852 | memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE); |
| 5853 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 5854 | if (setup_vmcs_config(&vmcs_config) < 0) { |
| 5855 | r = -EIO; |
| 5856 | goto out7; |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 5857 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 5858 | |
| 5859 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 5860 | kvm_enable_efer_bits(EFER_NX); |
| 5861 | |
| 5862 | if (!cpu_has_vmx_vpid()) |
| 5863 | enable_vpid = 0; |
| 5864 | if (!cpu_has_vmx_shadow_vmcs()) |
| 5865 | enable_shadow_vmcs = 0; |
| 5866 | if (enable_shadow_vmcs) |
| 5867 | init_vmcs_shadow_fields(); |
| 5868 | |
| 5869 | if (!cpu_has_vmx_ept() || |
| 5870 | !cpu_has_vmx_ept_4levels()) { |
| 5871 | enable_ept = 0; |
| 5872 | enable_unrestricted_guest = 0; |
| 5873 | enable_ept_ad_bits = 0; |
| 5874 | } |
| 5875 | |
| 5876 | if (!cpu_has_vmx_ept_ad_bits()) |
| 5877 | enable_ept_ad_bits = 0; |
| 5878 | |
| 5879 | if (!cpu_has_vmx_unrestricted_guest()) |
| 5880 | enable_unrestricted_guest = 0; |
| 5881 | |
| 5882 | if (!cpu_has_vmx_flexpriority()) { |
| 5883 | flexpriority_enabled = 0; |
| 5884 | |
| 5885 | /* |
| 5886 | * set_apic_access_page_addr() is used to reload apic access |
| 5887 | * page upon invalidation. No need to do anything if the |
| 5888 | * processor does not have the APIC_ACCESS_ADDR VMCS field. |
| 5889 | */ |
| 5890 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
| 5891 | } |
| 5892 | |
| 5893 | if (!cpu_has_vmx_tpr_shadow()) |
| 5894 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 5895 | |
| 5896 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 5897 | kvm_disable_largepages(); |
| 5898 | |
| 5899 | if (!cpu_has_vmx_ple()) |
| 5900 | ple_gap = 0; |
| 5901 | |
| 5902 | if (!cpu_has_vmx_apicv()) |
| 5903 | enable_apicv = 0; |
| 5904 | |
| 5905 | if (enable_apicv) |
| 5906 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 5907 | else { |
| 5908 | kvm_x86_ops->hwapic_irr_update = NULL; |
Tiejun Chen | b4eef9b | 2014-12-22 10:32:57 +0100 | [diff] [blame] | 5909 | kvm_x86_ops->hwapic_isr_update = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 5910 | kvm_x86_ops->deliver_posted_interrupt = NULL; |
Marcelo Tosatti | 7c6a98d | 2014-12-16 09:08:14 -0500 | [diff] [blame] | 5911 | kvm_x86_ops->test_posted_interrupt = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 5912 | kvm_x86_ops->sync_pir_to_irr = vmx_sync_pir_to_irr_dummy; |
| 5913 | } |
| 5914 | |
| 5915 | if (nested) |
| 5916 | nested_vmx_setup_ctls_msrs(); |
| 5917 | |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 5918 | vmx_disable_intercept_for_msr(MSR_FS_BASE, false); |
| 5919 | vmx_disable_intercept_for_msr(MSR_GS_BASE, false); |
| 5920 | vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true); |
| 5921 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false); |
| 5922 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false); |
| 5923 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false); |
| 5924 | vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true); |
| 5925 | |
| 5926 | memcpy(vmx_msr_bitmap_legacy_x2apic, |
| 5927 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
| 5928 | memcpy(vmx_msr_bitmap_longmode_x2apic, |
| 5929 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
| 5930 | |
| 5931 | if (enable_apicv) { |
| 5932 | for (msr = 0x800; msr <= 0x8ff; msr++) |
| 5933 | vmx_disable_intercept_msr_read_x2apic(msr); |
| 5934 | |
| 5935 | /* According SDM, in x2apic mode, the whole id reg is used. |
| 5936 | * But in KVM, it only use the highest eight bits. Need to |
| 5937 | * intercept it */ |
| 5938 | vmx_enable_intercept_msr_read_x2apic(0x802); |
| 5939 | /* TMCCT */ |
| 5940 | vmx_enable_intercept_msr_read_x2apic(0x839); |
| 5941 | /* TPR */ |
| 5942 | vmx_disable_intercept_msr_write_x2apic(0x808); |
| 5943 | /* EOI */ |
| 5944 | vmx_disable_intercept_msr_write_x2apic(0x80b); |
| 5945 | /* SELF-IPI */ |
| 5946 | vmx_disable_intercept_msr_write_x2apic(0x83f); |
| 5947 | } |
| 5948 | |
| 5949 | if (enable_ept) { |
| 5950 | kvm_mmu_set_mask_ptes(0ull, |
| 5951 | (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull, |
| 5952 | (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull, |
| 5953 | 0ull, VMX_EPT_EXECUTABLE_MASK); |
| 5954 | ept_set_mmio_spte_mask(); |
| 5955 | kvm_enable_tdp(); |
| 5956 | } else |
| 5957 | kvm_disable_tdp(); |
| 5958 | |
| 5959 | update_ple_window_actual_max(); |
| 5960 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 5961 | /* |
| 5962 | * Only enable PML when hardware supports PML feature, and both EPT |
| 5963 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 5964 | */ |
| 5965 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 5966 | enable_pml = 0; |
| 5967 | |
| 5968 | if (!enable_pml) { |
| 5969 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 5970 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 5971 | kvm_x86_ops->flush_log_dirty = NULL; |
| 5972 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 5973 | } |
| 5974 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 5975 | return alloc_kvm_area(); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 5976 | |
| 5977 | out7: |
| 5978 | free_page((unsigned long)vmx_vmwrite_bitmap); |
| 5979 | out6: |
| 5980 | free_page((unsigned long)vmx_vmread_bitmap); |
| 5981 | out5: |
| 5982 | free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic); |
| 5983 | out4: |
| 5984 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
| 5985 | out3: |
| 5986 | free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic); |
| 5987 | out2: |
| 5988 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
| 5989 | out1: |
| 5990 | free_page((unsigned long)vmx_io_bitmap_b); |
| 5991 | out: |
| 5992 | free_page((unsigned long)vmx_io_bitmap_a); |
| 5993 | |
| 5994 | return r; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 5995 | } |
| 5996 | |
| 5997 | static __exit void hardware_unsetup(void) |
| 5998 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 5999 | free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic); |
| 6000 | free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic); |
| 6001 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
| 6002 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
| 6003 | free_page((unsigned long)vmx_io_bitmap_b); |
| 6004 | free_page((unsigned long)vmx_io_bitmap_a); |
| 6005 | free_page((unsigned long)vmx_vmwrite_bitmap); |
| 6006 | free_page((unsigned long)vmx_vmread_bitmap); |
| 6007 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6008 | free_kvm_area(); |
| 6009 | } |
| 6010 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6011 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6012 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 6013 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 6014 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 6015 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6016 | { |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 6017 | if (ple_gap) |
| 6018 | grow_ple_window(vcpu); |
| 6019 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6020 | skip_emulated_instruction(vcpu); |
| 6021 | kvm_vcpu_on_spin(vcpu); |
| 6022 | |
| 6023 | return 1; |
| 6024 | } |
| 6025 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6026 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 6027 | { |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6028 | skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 6029 | return 1; |
| 6030 | } |
| 6031 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6032 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 6033 | { |
| 6034 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 6035 | return handle_nop(vcpu); |
| 6036 | } |
| 6037 | |
| 6038 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 6039 | { |
| 6040 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 6041 | return handle_nop(vcpu); |
| 6042 | } |
| 6043 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6044 | /* |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6045 | * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12. |
| 6046 | * We could reuse a single VMCS for all the L2 guests, but we also want the |
| 6047 | * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this |
| 6048 | * allows keeping them loaded on the processor, and in the future will allow |
| 6049 | * optimizations where prepare_vmcs02 doesn't need to set all the fields on |
| 6050 | * every entry if they never change. |
| 6051 | * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE |
| 6052 | * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first. |
| 6053 | * |
| 6054 | * The following functions allocate and free a vmcs02 in this pool. |
| 6055 | */ |
| 6056 | |
| 6057 | /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */ |
| 6058 | static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx) |
| 6059 | { |
| 6060 | struct vmcs02_list *item; |
| 6061 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 6062 | if (item->vmptr == vmx->nested.current_vmptr) { |
| 6063 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 6064 | return &item->vmcs02; |
| 6065 | } |
| 6066 | |
| 6067 | if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) { |
| 6068 | /* Recycle the least recently used VMCS. */ |
| 6069 | item = list_entry(vmx->nested.vmcs02_pool.prev, |
| 6070 | struct vmcs02_list, list); |
| 6071 | item->vmptr = vmx->nested.current_vmptr; |
| 6072 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 6073 | return &item->vmcs02; |
| 6074 | } |
| 6075 | |
| 6076 | /* Create a new VMCS */ |
Ioan Orghici | 0fa24ce | 2013-03-10 15:46:00 +0200 | [diff] [blame] | 6077 | item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6078 | if (!item) |
| 6079 | return NULL; |
| 6080 | item->vmcs02.vmcs = alloc_vmcs(); |
| 6081 | if (!item->vmcs02.vmcs) { |
| 6082 | kfree(item); |
| 6083 | return NULL; |
| 6084 | } |
| 6085 | loaded_vmcs_init(&item->vmcs02); |
| 6086 | item->vmptr = vmx->nested.current_vmptr; |
| 6087 | list_add(&(item->list), &(vmx->nested.vmcs02_pool)); |
| 6088 | vmx->nested.vmcs02_num++; |
| 6089 | return &item->vmcs02; |
| 6090 | } |
| 6091 | |
| 6092 | /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */ |
| 6093 | static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 6094 | { |
| 6095 | struct vmcs02_list *item; |
| 6096 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 6097 | if (item->vmptr == vmptr) { |
| 6098 | free_loaded_vmcs(&item->vmcs02); |
| 6099 | list_del(&item->list); |
| 6100 | kfree(item); |
| 6101 | vmx->nested.vmcs02_num--; |
| 6102 | return; |
| 6103 | } |
| 6104 | } |
| 6105 | |
| 6106 | /* |
| 6107 | * Free all VMCSs saved for this vcpu, except the one pointed by |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6108 | * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs |
| 6109 | * must be &vmx->vmcs01. |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6110 | */ |
| 6111 | static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx) |
| 6112 | { |
| 6113 | struct vmcs02_list *item, *n; |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6114 | |
| 6115 | WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6116 | list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) { |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6117 | /* |
| 6118 | * Something will leak if the above WARN triggers. Better than |
| 6119 | * a use-after-free. |
| 6120 | */ |
| 6121 | if (vmx->loaded_vmcs == &item->vmcs02) |
| 6122 | continue; |
| 6123 | |
| 6124 | free_loaded_vmcs(&item->vmcs02); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6125 | list_del(&item->list); |
| 6126 | kfree(item); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6127 | vmx->nested.vmcs02_num--; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6128 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6129 | } |
| 6130 | |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 6131 | /* |
| 6132 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 6133 | * set the success or error code of an emulated VMX instruction, as specified |
| 6134 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 6135 | */ |
| 6136 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 6137 | { |
| 6138 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 6139 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 6140 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 6141 | } |
| 6142 | |
| 6143 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 6144 | { |
| 6145 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 6146 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 6147 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 6148 | | X86_EFLAGS_CF); |
| 6149 | } |
| 6150 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 6151 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 6152 | u32 vm_instruction_error) |
| 6153 | { |
| 6154 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 6155 | /* |
| 6156 | * failValid writes the error number to the current VMCS, which |
| 6157 | * can't be done there isn't a current VMCS. |
| 6158 | */ |
| 6159 | nested_vmx_failInvalid(vcpu); |
| 6160 | return; |
| 6161 | } |
| 6162 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 6163 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 6164 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 6165 | | X86_EFLAGS_ZF); |
| 6166 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 6167 | /* |
| 6168 | * We don't need to force a shadow sync because |
| 6169 | * VM_INSTRUCTION_ERROR is not shadowed |
| 6170 | */ |
| 6171 | } |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 6172 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 6173 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 6174 | { |
| 6175 | /* TODO: not to reset guest simply here. */ |
| 6176 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 6177 | pr_warn("kvm: nested vmx abort, indicator %d\n", indicator); |
| 6178 | } |
| 6179 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 6180 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 6181 | { |
| 6182 | struct vcpu_vmx *vmx = |
| 6183 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 6184 | |
| 6185 | vmx->nested.preemption_timer_expired = true; |
| 6186 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 6187 | kvm_vcpu_kick(&vmx->vcpu); |
| 6188 | |
| 6189 | return HRTIMER_NORESTART; |
| 6190 | } |
| 6191 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6192 | /* |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6193 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 6194 | * exit caused by such an instruction (run by a guest hypervisor). |
| 6195 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 6196 | * #UD or #GP. |
| 6197 | */ |
| 6198 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 6199 | unsigned long exit_qualification, |
| 6200 | u32 vmx_instruction_info, gva_t *ret) |
| 6201 | { |
| 6202 | /* |
| 6203 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 6204 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 6205 | * addressing components of the operand. Only the displacement part |
| 6206 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 6207 | * For how an actual address is calculated from all these components, |
| 6208 | * refer to Vol. 1, "Operand Addressing". |
| 6209 | */ |
| 6210 | int scaling = vmx_instruction_info & 3; |
| 6211 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 6212 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 6213 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 6214 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 6215 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 6216 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 6217 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 6218 | |
| 6219 | if (is_reg) { |
| 6220 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6221 | return 1; |
| 6222 | } |
| 6223 | |
| 6224 | /* Addr = segment_base + offset */ |
| 6225 | /* offset = base + [index * scale] + displacement */ |
| 6226 | *ret = vmx_get_segment_base(vcpu, seg_reg); |
| 6227 | if (base_is_valid) |
| 6228 | *ret += kvm_register_read(vcpu, base_reg); |
| 6229 | if (index_is_valid) |
| 6230 | *ret += kvm_register_read(vcpu, index_reg)<<scaling; |
| 6231 | *ret += exit_qualification; /* holds the displacement */ |
| 6232 | |
| 6233 | if (addr_size == 1) /* 32 bit */ |
| 6234 | *ret &= 0xffffffff; |
| 6235 | |
| 6236 | /* |
| 6237 | * TODO: throw #GP (and return 1) in various cases that the VM* |
| 6238 | * instructions require it - e.g., offset beyond segment limit, |
| 6239 | * unusable or unreadable/unwritable segment, non-canonical 64-bit |
| 6240 | * address, and so on. Currently these are not checked. |
| 6241 | */ |
| 6242 | return 0; |
| 6243 | } |
| 6244 | |
| 6245 | /* |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6246 | * This function performs the various checks including |
| 6247 | * - if it's 4KB aligned |
| 6248 | * - No bits beyond the physical address width are set |
| 6249 | * - Returns 0 on success or else 1 |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6250 | * (Intel SDM Section 30.3) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6251 | */ |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6252 | static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason, |
| 6253 | gpa_t *vmpointer) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6254 | { |
| 6255 | gva_t gva; |
| 6256 | gpa_t vmptr; |
| 6257 | struct x86_exception e; |
| 6258 | struct page *page; |
| 6259 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6260 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 6261 | |
| 6262 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 6263 | vmcs_read32(VMX_INSTRUCTION_INFO), &gva)) |
| 6264 | return 1; |
| 6265 | |
| 6266 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr, |
| 6267 | sizeof(vmptr), &e)) { |
| 6268 | kvm_inject_page_fault(vcpu, &e); |
| 6269 | return 1; |
| 6270 | } |
| 6271 | |
| 6272 | switch (exit_reason) { |
| 6273 | case EXIT_REASON_VMON: |
| 6274 | /* |
| 6275 | * SDM 3: 24.11.5 |
| 6276 | * The first 4 bytes of VMXON region contain the supported |
| 6277 | * VMCS revision identifier |
| 6278 | * |
| 6279 | * Note - IA32_VMX_BASIC[48] will never be 1 |
| 6280 | * for the nested case; |
| 6281 | * which replaces physical address width with 32 |
| 6282 | * |
| 6283 | */ |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 6284 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6285 | nested_vmx_failInvalid(vcpu); |
| 6286 | skip_emulated_instruction(vcpu); |
| 6287 | return 1; |
| 6288 | } |
| 6289 | |
| 6290 | page = nested_get_page(vcpu, vmptr); |
| 6291 | if (page == NULL || |
| 6292 | *(u32 *)kmap(page) != VMCS12_REVISION) { |
| 6293 | nested_vmx_failInvalid(vcpu); |
| 6294 | kunmap(page); |
| 6295 | skip_emulated_instruction(vcpu); |
| 6296 | return 1; |
| 6297 | } |
| 6298 | kunmap(page); |
| 6299 | vmx->nested.vmxon_ptr = vmptr; |
| 6300 | break; |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6301 | case EXIT_REASON_VMCLEAR: |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 6302 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6303 | nested_vmx_failValid(vcpu, |
| 6304 | VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 6305 | skip_emulated_instruction(vcpu); |
| 6306 | return 1; |
| 6307 | } |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6308 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6309 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 6310 | nested_vmx_failValid(vcpu, |
| 6311 | VMXERR_VMCLEAR_VMXON_POINTER); |
| 6312 | skip_emulated_instruction(vcpu); |
| 6313 | return 1; |
| 6314 | } |
| 6315 | break; |
| 6316 | case EXIT_REASON_VMPTRLD: |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 6317 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6318 | nested_vmx_failValid(vcpu, |
| 6319 | VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 6320 | skip_emulated_instruction(vcpu); |
| 6321 | return 1; |
| 6322 | } |
| 6323 | |
| 6324 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 6325 | nested_vmx_failValid(vcpu, |
| 6326 | VMXERR_VMCLEAR_VMXON_POINTER); |
| 6327 | skip_emulated_instruction(vcpu); |
| 6328 | return 1; |
| 6329 | } |
| 6330 | break; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6331 | default: |
| 6332 | return 1; /* shouldn't happen */ |
| 6333 | } |
| 6334 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6335 | if (vmpointer) |
| 6336 | *vmpointer = vmptr; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6337 | return 0; |
| 6338 | } |
| 6339 | |
| 6340 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6341 | * Emulate the VMXON instruction. |
| 6342 | * Currently, we just remember that VMX is active, and do not save or even |
| 6343 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 6344 | * do not currently need to store anything in that guest-allocated memory |
| 6345 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 6346 | * argument is different from the VMXON pointer (which the spec says they do). |
| 6347 | */ |
| 6348 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 6349 | { |
| 6350 | struct kvm_segment cs; |
| 6351 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 6352 | struct vmcs *shadow_vmcs; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 6353 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 6354 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6355 | |
| 6356 | /* The Intel VMX Instruction Reference lists a bunch of bits that |
| 6357 | * are prerequisite to running VMXON, most notably cr4.VMXE must be |
| 6358 | * set to 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 6359 | * Otherwise, we should fail with #UD. We test these now: |
| 6360 | */ |
| 6361 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) || |
| 6362 | !kvm_read_cr0_bits(vcpu, X86_CR0_PE) || |
| 6363 | (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
| 6364 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6365 | return 1; |
| 6366 | } |
| 6367 | |
| 6368 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 6369 | if (is_long_mode(vcpu) && !cs.l) { |
| 6370 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6371 | return 1; |
| 6372 | } |
| 6373 | |
| 6374 | if (vmx_get_cpl(vcpu)) { |
| 6375 | kvm_inject_gp(vcpu, 0); |
| 6376 | return 1; |
| 6377 | } |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6378 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6379 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6380 | return 1; |
| 6381 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 6382 | if (vmx->nested.vmxon) { |
| 6383 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
| 6384 | skip_emulated_instruction(vcpu); |
| 6385 | return 1; |
| 6386 | } |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 6387 | |
| 6388 | if ((vmx->nested.msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
| 6389 | != VMXON_NEEDED_FEATURES) { |
| 6390 | kvm_inject_gp(vcpu, 0); |
| 6391 | return 1; |
| 6392 | } |
| 6393 | |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 6394 | if (enable_shadow_vmcs) { |
| 6395 | shadow_vmcs = alloc_vmcs(); |
| 6396 | if (!shadow_vmcs) |
| 6397 | return -ENOMEM; |
| 6398 | /* mark vmcs as shadow */ |
| 6399 | shadow_vmcs->revision_id |= (1u << 31); |
| 6400 | /* init shadow vmcs */ |
| 6401 | vmcs_clear(shadow_vmcs); |
| 6402 | vmx->nested.current_shadow_vmcs = shadow_vmcs; |
| 6403 | } |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6404 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6405 | INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool)); |
| 6406 | vmx->nested.vmcs02_num = 0; |
| 6407 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 6408 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
| 6409 | HRTIMER_MODE_REL); |
| 6410 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 6411 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6412 | vmx->nested.vmxon = true; |
| 6413 | |
| 6414 | skip_emulated_instruction(vcpu); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 6415 | nested_vmx_succeed(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6416 | return 1; |
| 6417 | } |
| 6418 | |
| 6419 | /* |
| 6420 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 6421 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 6422 | * slightly different). It also specifies what exception to inject otherwise. |
| 6423 | */ |
| 6424 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 6425 | { |
| 6426 | struct kvm_segment cs; |
| 6427 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6428 | |
| 6429 | if (!vmx->nested.vmxon) { |
| 6430 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6431 | return 0; |
| 6432 | } |
| 6433 | |
| 6434 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 6435 | if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) || |
| 6436 | (is_long_mode(vcpu) && !cs.l)) { |
| 6437 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6438 | return 0; |
| 6439 | } |
| 6440 | |
| 6441 | if (vmx_get_cpl(vcpu)) { |
| 6442 | kvm_inject_gp(vcpu, 0); |
| 6443 | return 0; |
| 6444 | } |
| 6445 | |
| 6446 | return 1; |
| 6447 | } |
| 6448 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 6449 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
| 6450 | { |
Abel Gordon | 8a1b9dd | 2013-04-18 14:39:55 +0300 | [diff] [blame] | 6451 | u32 exec_control; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 6452 | if (vmx->nested.current_vmptr == -1ull) |
| 6453 | return; |
| 6454 | |
| 6455 | /* current_vmptr and current_vmcs12 are always set/reset together */ |
| 6456 | if (WARN_ON(vmx->nested.current_vmcs12 == NULL)) |
| 6457 | return; |
| 6458 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 6459 | if (enable_shadow_vmcs) { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 6460 | /* copy to memory all shadowed fields in case |
| 6461 | they were modified */ |
| 6462 | copy_shadow_to_vmcs12(vmx); |
| 6463 | vmx->nested.sync_shadow_vmcs = false; |
| 6464 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 6465 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 6466 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 6467 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 6468 | } |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 6469 | kunmap(vmx->nested.current_vmcs12_page); |
| 6470 | nested_release_page(vmx->nested.current_vmcs12_page); |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 6471 | vmx->nested.current_vmptr = -1ull; |
| 6472 | vmx->nested.current_vmcs12 = NULL; |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 6473 | } |
| 6474 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6475 | /* |
| 6476 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 6477 | * just stops using VMX. |
| 6478 | */ |
| 6479 | static void free_nested(struct vcpu_vmx *vmx) |
| 6480 | { |
| 6481 | if (!vmx->nested.vmxon) |
| 6482 | return; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 6483 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6484 | vmx->nested.vmxon = false; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 6485 | nested_release_vmcs12(vmx); |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 6486 | if (enable_shadow_vmcs) |
| 6487 | free_vmcs(vmx->nested.current_shadow_vmcs); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 6488 | /* Unpin physical memory we referred to in current vmcs02 */ |
| 6489 | if (vmx->nested.apic_access_page) { |
| 6490 | nested_release_page(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 6491 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 6492 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 6493 | if (vmx->nested.virtual_apic_page) { |
| 6494 | nested_release_page(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 6495 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 6496 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6497 | |
| 6498 | nested_free_all_saved_vmcss(vmx); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | /* Emulate the VMXOFF instruction */ |
| 6502 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 6503 | { |
| 6504 | if (!nested_vmx_check_permission(vcpu)) |
| 6505 | return 1; |
| 6506 | free_nested(to_vmx(vcpu)); |
| 6507 | skip_emulated_instruction(vcpu); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 6508 | nested_vmx_succeed(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6509 | return 1; |
| 6510 | } |
| 6511 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 6512 | /* Emulate the VMCLEAR instruction */ |
| 6513 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 6514 | { |
| 6515 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 6516 | gpa_t vmptr; |
| 6517 | struct vmcs12 *vmcs12; |
| 6518 | struct page *page; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 6519 | |
| 6520 | if (!nested_vmx_check_permission(vcpu)) |
| 6521 | return 1; |
| 6522 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6523 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr)) |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 6524 | return 1; |
| 6525 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 6526 | if (vmptr == vmx->nested.current_vmptr) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 6527 | nested_release_vmcs12(vmx); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 6528 | |
| 6529 | page = nested_get_page(vcpu, vmptr); |
| 6530 | if (page == NULL) { |
| 6531 | /* |
| 6532 | * For accurate processor emulation, VMCLEAR beyond available |
| 6533 | * physical memory should do nothing at all. However, it is |
| 6534 | * possible that a nested vmx bug, not a guest hypervisor bug, |
| 6535 | * resulted in this case, so let's shut down before doing any |
| 6536 | * more damage: |
| 6537 | */ |
| 6538 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 6539 | return 1; |
| 6540 | } |
| 6541 | vmcs12 = kmap(page); |
| 6542 | vmcs12->launch_state = 0; |
| 6543 | kunmap(page); |
| 6544 | nested_release_page(page); |
| 6545 | |
| 6546 | nested_free_vmcs02(vmx, vmptr); |
| 6547 | |
| 6548 | skip_emulated_instruction(vcpu); |
| 6549 | nested_vmx_succeed(vcpu); |
| 6550 | return 1; |
| 6551 | } |
| 6552 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 6553 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 6554 | |
| 6555 | /* Emulate the VMLAUNCH instruction */ |
| 6556 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 6557 | { |
| 6558 | return nested_vmx_run(vcpu, true); |
| 6559 | } |
| 6560 | |
| 6561 | /* Emulate the VMRESUME instruction */ |
| 6562 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 6563 | { |
| 6564 | |
| 6565 | return nested_vmx_run(vcpu, false); |
| 6566 | } |
| 6567 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6568 | enum vmcs_field_type { |
| 6569 | VMCS_FIELD_TYPE_U16 = 0, |
| 6570 | VMCS_FIELD_TYPE_U64 = 1, |
| 6571 | VMCS_FIELD_TYPE_U32 = 2, |
| 6572 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 |
| 6573 | }; |
| 6574 | |
| 6575 | static inline int vmcs_field_type(unsigned long field) |
| 6576 | { |
| 6577 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
| 6578 | return VMCS_FIELD_TYPE_U32; |
| 6579 | return (field >> 13) & 0x3 ; |
| 6580 | } |
| 6581 | |
| 6582 | static inline int vmcs_field_readonly(unsigned long field) |
| 6583 | { |
| 6584 | return (((field >> 10) & 0x3) == 1); |
| 6585 | } |
| 6586 | |
| 6587 | /* |
| 6588 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 6589 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 6590 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 6591 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 6592 | * 64-bit fields are to be returned). |
| 6593 | */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6594 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
| 6595 | unsigned long field, u64 *ret) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6596 | { |
| 6597 | short offset = vmcs_field_to_offset(field); |
| 6598 | char *p; |
| 6599 | |
| 6600 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6601 | return offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6602 | |
| 6603 | p = ((char *)(get_vmcs12(vcpu))) + offset; |
| 6604 | |
| 6605 | switch (vmcs_field_type(field)) { |
| 6606 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 6607 | *ret = *((natural_width *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6608 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6609 | case VMCS_FIELD_TYPE_U16: |
| 6610 | *ret = *((u16 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6611 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6612 | case VMCS_FIELD_TYPE_U32: |
| 6613 | *ret = *((u32 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6614 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6615 | case VMCS_FIELD_TYPE_U64: |
| 6616 | *ret = *((u64 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6617 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6618 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6619 | WARN_ON(1); |
| 6620 | return -ENOENT; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6621 | } |
| 6622 | } |
| 6623 | |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 6624 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6625 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
| 6626 | unsigned long field, u64 field_value){ |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 6627 | short offset = vmcs_field_to_offset(field); |
| 6628 | char *p = ((char *) get_vmcs12(vcpu)) + offset; |
| 6629 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6630 | return offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 6631 | |
| 6632 | switch (vmcs_field_type(field)) { |
| 6633 | case VMCS_FIELD_TYPE_U16: |
| 6634 | *(u16 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6635 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 6636 | case VMCS_FIELD_TYPE_U32: |
| 6637 | *(u32 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6638 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 6639 | case VMCS_FIELD_TYPE_U64: |
| 6640 | *(u64 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6641 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 6642 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 6643 | *(natural_width *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6644 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 6645 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6646 | WARN_ON(1); |
| 6647 | return -ENOENT; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 6648 | } |
| 6649 | |
| 6650 | } |
| 6651 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 6652 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 6653 | { |
| 6654 | int i; |
| 6655 | unsigned long field; |
| 6656 | u64 field_value; |
| 6657 | struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 6658 | const unsigned long *fields = shadow_read_write_fields; |
| 6659 | const int num_fields = max_shadow_read_write_fields; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 6660 | |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 6661 | preempt_disable(); |
| 6662 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 6663 | vmcs_load(shadow_vmcs); |
| 6664 | |
| 6665 | for (i = 0; i < num_fields; i++) { |
| 6666 | field = fields[i]; |
| 6667 | switch (vmcs_field_type(field)) { |
| 6668 | case VMCS_FIELD_TYPE_U16: |
| 6669 | field_value = vmcs_read16(field); |
| 6670 | break; |
| 6671 | case VMCS_FIELD_TYPE_U32: |
| 6672 | field_value = vmcs_read32(field); |
| 6673 | break; |
| 6674 | case VMCS_FIELD_TYPE_U64: |
| 6675 | field_value = vmcs_read64(field); |
| 6676 | break; |
| 6677 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 6678 | field_value = vmcs_readl(field); |
| 6679 | break; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6680 | default: |
| 6681 | WARN_ON(1); |
| 6682 | continue; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 6683 | } |
| 6684 | vmcs12_write_any(&vmx->vcpu, field, field_value); |
| 6685 | } |
| 6686 | |
| 6687 | vmcs_clear(shadow_vmcs); |
| 6688 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 6689 | |
| 6690 | preempt_enable(); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 6691 | } |
| 6692 | |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 6693 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 6694 | { |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 6695 | const unsigned long *fields[] = { |
| 6696 | shadow_read_write_fields, |
| 6697 | shadow_read_only_fields |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 6698 | }; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 6699 | const int max_fields[] = { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 6700 | max_shadow_read_write_fields, |
| 6701 | max_shadow_read_only_fields |
| 6702 | }; |
| 6703 | int i, q; |
| 6704 | unsigned long field; |
| 6705 | u64 field_value = 0; |
| 6706 | struct vmcs *shadow_vmcs = vmx->nested.current_shadow_vmcs; |
| 6707 | |
| 6708 | vmcs_load(shadow_vmcs); |
| 6709 | |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 6710 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 6711 | for (i = 0; i < max_fields[q]; i++) { |
| 6712 | field = fields[q][i]; |
| 6713 | vmcs12_read_any(&vmx->vcpu, field, &field_value); |
| 6714 | |
| 6715 | switch (vmcs_field_type(field)) { |
| 6716 | case VMCS_FIELD_TYPE_U16: |
| 6717 | vmcs_write16(field, (u16)field_value); |
| 6718 | break; |
| 6719 | case VMCS_FIELD_TYPE_U32: |
| 6720 | vmcs_write32(field, (u32)field_value); |
| 6721 | break; |
| 6722 | case VMCS_FIELD_TYPE_U64: |
| 6723 | vmcs_write64(field, (u64)field_value); |
| 6724 | break; |
| 6725 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 6726 | vmcs_writel(field, (long)field_value); |
| 6727 | break; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6728 | default: |
| 6729 | WARN_ON(1); |
| 6730 | break; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 6731 | } |
| 6732 | } |
| 6733 | } |
| 6734 | |
| 6735 | vmcs_clear(shadow_vmcs); |
| 6736 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 6737 | } |
| 6738 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6739 | /* |
| 6740 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 6741 | * used before) all generate the same failure when it is missing. |
| 6742 | */ |
| 6743 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 6744 | { |
| 6745 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6746 | if (vmx->nested.current_vmptr == -1ull) { |
| 6747 | nested_vmx_failInvalid(vcpu); |
| 6748 | skip_emulated_instruction(vcpu); |
| 6749 | return 0; |
| 6750 | } |
| 6751 | return 1; |
| 6752 | } |
| 6753 | |
| 6754 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 6755 | { |
| 6756 | unsigned long field; |
| 6757 | u64 field_value; |
| 6758 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6759 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 6760 | gva_t gva = 0; |
| 6761 | |
| 6762 | if (!nested_vmx_check_permission(vcpu) || |
| 6763 | !nested_vmx_check_vmcs12(vcpu)) |
| 6764 | return 1; |
| 6765 | |
| 6766 | /* Decode instruction info and find the field to read */ |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 6767 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6768 | /* Read the field, zero-extended to a u64 field_value */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6769 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6770 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 6771 | skip_emulated_instruction(vcpu); |
| 6772 | return 1; |
| 6773 | } |
| 6774 | /* |
| 6775 | * Now copy part of this value to register or memory, as requested. |
| 6776 | * Note that the number of bits actually copied is 32 or 64 depending |
| 6777 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 6778 | */ |
| 6779 | if (vmx_instruction_info & (1u << 10)) { |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 6780 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6781 | field_value); |
| 6782 | } else { |
| 6783 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 6784 | vmx_instruction_info, &gva)) |
| 6785 | return 1; |
| 6786 | /* _system ok, as nested_vmx_check_permission verified cpl=0 */ |
| 6787 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
| 6788 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); |
| 6789 | } |
| 6790 | |
| 6791 | nested_vmx_succeed(vcpu); |
| 6792 | skip_emulated_instruction(vcpu); |
| 6793 | return 1; |
| 6794 | } |
| 6795 | |
| 6796 | |
| 6797 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 6798 | { |
| 6799 | unsigned long field; |
| 6800 | gva_t gva; |
| 6801 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6802 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6803 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 6804 | * mode, and eventually we need to write that into a field of several |
| 6805 | * possible lengths. The code below first zero-extends the value to 64 |
| 6806 | * bit (field_value), and then copies only the approriate number of |
| 6807 | * bits into the vmcs12 field. |
| 6808 | */ |
| 6809 | u64 field_value = 0; |
| 6810 | struct x86_exception e; |
| 6811 | |
| 6812 | if (!nested_vmx_check_permission(vcpu) || |
| 6813 | !nested_vmx_check_vmcs12(vcpu)) |
| 6814 | return 1; |
| 6815 | |
| 6816 | if (vmx_instruction_info & (1u << 10)) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 6817 | field_value = kvm_register_readl(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6818 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 6819 | else { |
| 6820 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 6821 | vmx_instruction_info, &gva)) |
| 6822 | return 1; |
| 6823 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 6824 | &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6825 | kvm_inject_page_fault(vcpu, &e); |
| 6826 | return 1; |
| 6827 | } |
| 6828 | } |
| 6829 | |
| 6830 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 6831 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6832 | if (vmcs_field_readonly(field)) { |
| 6833 | nested_vmx_failValid(vcpu, |
| 6834 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
| 6835 | skip_emulated_instruction(vcpu); |
| 6836 | return 1; |
| 6837 | } |
| 6838 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 6839 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 6840 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 6841 | skip_emulated_instruction(vcpu); |
| 6842 | return 1; |
| 6843 | } |
| 6844 | |
| 6845 | nested_vmx_succeed(vcpu); |
| 6846 | skip_emulated_instruction(vcpu); |
| 6847 | return 1; |
| 6848 | } |
| 6849 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 6850 | /* Emulate the VMPTRLD instruction */ |
| 6851 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 6852 | { |
| 6853 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 6854 | gpa_t vmptr; |
Abel Gordon | 8a1b9dd | 2013-04-18 14:39:55 +0300 | [diff] [blame] | 6855 | u32 exec_control; |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 6856 | |
| 6857 | if (!nested_vmx_check_permission(vcpu)) |
| 6858 | return 1; |
| 6859 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6860 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr)) |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 6861 | return 1; |
| 6862 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 6863 | if (vmx->nested.current_vmptr != vmptr) { |
| 6864 | struct vmcs12 *new_vmcs12; |
| 6865 | struct page *page; |
| 6866 | page = nested_get_page(vcpu, vmptr); |
| 6867 | if (page == NULL) { |
| 6868 | nested_vmx_failInvalid(vcpu); |
| 6869 | skip_emulated_instruction(vcpu); |
| 6870 | return 1; |
| 6871 | } |
| 6872 | new_vmcs12 = kmap(page); |
| 6873 | if (new_vmcs12->revision_id != VMCS12_REVISION) { |
| 6874 | kunmap(page); |
| 6875 | nested_release_page_clean(page); |
| 6876 | nested_vmx_failValid(vcpu, |
| 6877 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 6878 | skip_emulated_instruction(vcpu); |
| 6879 | return 1; |
| 6880 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 6881 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 6882 | nested_release_vmcs12(vmx); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 6883 | vmx->nested.current_vmptr = vmptr; |
| 6884 | vmx->nested.current_vmcs12 = new_vmcs12; |
| 6885 | vmx->nested.current_vmcs12_page = page; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 6886 | if (enable_shadow_vmcs) { |
Abel Gordon | 8a1b9dd | 2013-04-18 14:39:55 +0300 | [diff] [blame] | 6887 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 6888 | exec_control |= SECONDARY_EXEC_SHADOW_VMCS; |
| 6889 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 6890 | vmcs_write64(VMCS_LINK_POINTER, |
| 6891 | __pa(vmx->nested.current_shadow_vmcs)); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 6892 | vmx->nested.sync_shadow_vmcs = true; |
| 6893 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 6894 | } |
| 6895 | |
| 6896 | nested_vmx_succeed(vcpu); |
| 6897 | skip_emulated_instruction(vcpu); |
| 6898 | return 1; |
| 6899 | } |
| 6900 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 6901 | /* Emulate the VMPTRST instruction */ |
| 6902 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 6903 | { |
| 6904 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6905 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 6906 | gva_t vmcs_gva; |
| 6907 | struct x86_exception e; |
| 6908 | |
| 6909 | if (!nested_vmx_check_permission(vcpu)) |
| 6910 | return 1; |
| 6911 | |
| 6912 | if (get_vmx_mem_address(vcpu, exit_qualification, |
| 6913 | vmx_instruction_info, &vmcs_gva)) |
| 6914 | return 1; |
| 6915 | /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */ |
| 6916 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
| 6917 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 6918 | sizeof(u64), &e)) { |
| 6919 | kvm_inject_page_fault(vcpu, &e); |
| 6920 | return 1; |
| 6921 | } |
| 6922 | nested_vmx_succeed(vcpu); |
| 6923 | skip_emulated_instruction(vcpu); |
| 6924 | return 1; |
| 6925 | } |
| 6926 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 6927 | /* Emulate the INVEPT instruction */ |
| 6928 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 6929 | { |
| 6930 | u32 vmx_instruction_info, types; |
| 6931 | unsigned long type; |
| 6932 | gva_t gva; |
| 6933 | struct x86_exception e; |
| 6934 | struct { |
| 6935 | u64 eptp, gpa; |
| 6936 | } operand; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 6937 | |
| 6938 | if (!(nested_vmx_secondary_ctls_high & SECONDARY_EXEC_ENABLE_EPT) || |
| 6939 | !(nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) { |
| 6940 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6941 | return 1; |
| 6942 | } |
| 6943 | |
| 6944 | if (!nested_vmx_check_permission(vcpu)) |
| 6945 | return 1; |
| 6946 | |
| 6947 | if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) { |
| 6948 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6949 | return 1; |
| 6950 | } |
| 6951 | |
| 6952 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 6953 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 6954 | |
| 6955 | types = (nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
| 6956 | |
| 6957 | if (!(types & (1UL << type))) { |
| 6958 | nested_vmx_failValid(vcpu, |
| 6959 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 6960 | return 1; |
| 6961 | } |
| 6962 | |
| 6963 | /* According to the Intel VMX instruction reference, the memory |
| 6964 | * operand is read even if it isn't needed (e.g., for type==global) |
| 6965 | */ |
| 6966 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 6967 | vmx_instruction_info, &gva)) |
| 6968 | return 1; |
| 6969 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, |
| 6970 | sizeof(operand), &e)) { |
| 6971 | kvm_inject_page_fault(vcpu, &e); |
| 6972 | return 1; |
| 6973 | } |
| 6974 | |
| 6975 | switch (type) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 6976 | case VMX_EPT_EXTENT_GLOBAL: |
| 6977 | kvm_mmu_sync_roots(vcpu); |
Liang Chen | 77c3913 | 2014-09-18 12:38:37 -0400 | [diff] [blame] | 6978 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 6979 | nested_vmx_succeed(vcpu); |
| 6980 | break; |
| 6981 | default: |
Bandan Das | 4b85507 | 2014-04-19 18:17:44 -0400 | [diff] [blame] | 6982 | /* Trap single context invalidation invept calls */ |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 6983 | BUG_ON(1); |
| 6984 | break; |
| 6985 | } |
| 6986 | |
| 6987 | skip_emulated_instruction(vcpu); |
| 6988 | return 1; |
| 6989 | } |
| 6990 | |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 6991 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 6992 | { |
| 6993 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6994 | return 1; |
| 6995 | } |
| 6996 | |
Marcelo Tosatti | 7c6a98d | 2014-12-16 09:08:14 -0500 | [diff] [blame] | 6997 | static bool vmx_test_pir(struct kvm_vcpu *vcpu, int vector) |
| 6998 | { |
| 6999 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7000 | |
| 7001 | return pi_test_pir(vector, &vmx->pi_desc); |
| 7002 | } |
| 7003 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 7004 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 7005 | { |
| 7006 | unsigned long exit_qualification; |
| 7007 | |
| 7008 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 7009 | |
| 7010 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7011 | |
| 7012 | /* |
| 7013 | * PML buffer FULL happened while executing iret from NMI, |
| 7014 | * "blocked by NMI" bit has to be set before next VM entry. |
| 7015 | */ |
| 7016 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
| 7017 | cpu_has_virtual_nmis() && |
| 7018 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 7019 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 7020 | GUEST_INTR_STATE_NMI); |
| 7021 | |
| 7022 | /* |
| 7023 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 7024 | * here.., and there's no userspace involvement needed for PML. |
| 7025 | */ |
| 7026 | return 1; |
| 7027 | } |
| 7028 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 7029 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7030 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 7031 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 7032 | * to be done to userspace and return 0. |
| 7033 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 7034 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7035 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 7036 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 7037 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7038 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7039 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7040 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 7041 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 7042 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 7043 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 7044 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 7045 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 7046 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7047 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7048 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7049 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7050 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7051 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7052 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7053 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7054 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7055 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7056 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7057 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7058 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 7059 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7060 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 7061 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 7062 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7063 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7064 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7065 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7066 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 7067 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7068 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 7069 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7070 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7071 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
| 7072 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7073 | [EXIT_REASON_INVEPT] = handle_invept, |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7074 | [EXIT_REASON_INVVPID] = handle_invvpid, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7075 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 7076 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 7077 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7078 | }; |
| 7079 | |
| 7080 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 7081 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7082 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7083 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 7084 | struct vmcs12 *vmcs12) |
| 7085 | { |
| 7086 | unsigned long exit_qualification; |
| 7087 | gpa_t bitmap, last_bitmap; |
| 7088 | unsigned int port; |
| 7089 | int size; |
| 7090 | u8 b; |
| 7091 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7092 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
Zhihui Zhang | 2f0a639 | 2013-12-30 15:56:29 -0500 | [diff] [blame] | 7093 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7094 | |
| 7095 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7096 | |
| 7097 | port = exit_qualification >> 16; |
| 7098 | size = (exit_qualification & 7) + 1; |
| 7099 | |
| 7100 | last_bitmap = (gpa_t)-1; |
| 7101 | b = -1; |
| 7102 | |
| 7103 | while (size > 0) { |
| 7104 | if (port < 0x8000) |
| 7105 | bitmap = vmcs12->io_bitmap_a; |
| 7106 | else if (port < 0x10000) |
| 7107 | bitmap = vmcs12->io_bitmap_b; |
| 7108 | else |
| 7109 | return 1; |
| 7110 | bitmap += (port & 0x7fff) / 8; |
| 7111 | |
| 7112 | if (last_bitmap != bitmap) |
| 7113 | if (kvm_read_guest(vcpu->kvm, bitmap, &b, 1)) |
| 7114 | return 1; |
| 7115 | if (b & (1 << (port & 7))) |
| 7116 | return 1; |
| 7117 | |
| 7118 | port++; |
| 7119 | size--; |
| 7120 | last_bitmap = bitmap; |
| 7121 | } |
| 7122 | |
| 7123 | return 0; |
| 7124 | } |
| 7125 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7126 | /* |
| 7127 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 7128 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 7129 | * disinterest in the current event (read or write a specific MSR) by using an |
| 7130 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 7131 | */ |
| 7132 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 7133 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 7134 | { |
| 7135 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 7136 | gpa_t bitmap; |
| 7137 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 7138 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7139 | return 1; |
| 7140 | |
| 7141 | /* |
| 7142 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 7143 | * for the four combinations of read/write and low/high MSR numbers. |
| 7144 | * First we need to figure out which of the four to use: |
| 7145 | */ |
| 7146 | bitmap = vmcs12->msr_bitmap; |
| 7147 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 7148 | bitmap += 2048; |
| 7149 | if (msr_index >= 0xc0000000) { |
| 7150 | msr_index -= 0xc0000000; |
| 7151 | bitmap += 1024; |
| 7152 | } |
| 7153 | |
| 7154 | /* Then read the msr_index'th bit from this bitmap: */ |
| 7155 | if (msr_index < 1024*8) { |
| 7156 | unsigned char b; |
Jan Kiszka | bd31a7f | 2013-02-14 19:46:27 +0100 | [diff] [blame] | 7157 | if (kvm_read_guest(vcpu->kvm, bitmap + msr_index/8, &b, 1)) |
| 7158 | return 1; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7159 | return 1 & (b >> (msr_index & 7)); |
| 7160 | } else |
| 7161 | return 1; /* let L1 handle the wrong parameter */ |
| 7162 | } |
| 7163 | |
| 7164 | /* |
| 7165 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 7166 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 7167 | * intercept (via guest_host_mask etc.) the current event. |
| 7168 | */ |
| 7169 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 7170 | struct vmcs12 *vmcs12) |
| 7171 | { |
| 7172 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7173 | int cr = exit_qualification & 15; |
| 7174 | int reg = (exit_qualification >> 8) & 15; |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 7175 | unsigned long val = kvm_register_readl(vcpu, reg); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7176 | |
| 7177 | switch ((exit_qualification >> 4) & 3) { |
| 7178 | case 0: /* mov to cr */ |
| 7179 | switch (cr) { |
| 7180 | case 0: |
| 7181 | if (vmcs12->cr0_guest_host_mask & |
| 7182 | (val ^ vmcs12->cr0_read_shadow)) |
| 7183 | return 1; |
| 7184 | break; |
| 7185 | case 3: |
| 7186 | if ((vmcs12->cr3_target_count >= 1 && |
| 7187 | vmcs12->cr3_target_value0 == val) || |
| 7188 | (vmcs12->cr3_target_count >= 2 && |
| 7189 | vmcs12->cr3_target_value1 == val) || |
| 7190 | (vmcs12->cr3_target_count >= 3 && |
| 7191 | vmcs12->cr3_target_value2 == val) || |
| 7192 | (vmcs12->cr3_target_count >= 4 && |
| 7193 | vmcs12->cr3_target_value3 == val)) |
| 7194 | return 0; |
| 7195 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
| 7196 | return 1; |
| 7197 | break; |
| 7198 | case 4: |
| 7199 | if (vmcs12->cr4_guest_host_mask & |
| 7200 | (vmcs12->cr4_read_shadow ^ val)) |
| 7201 | return 1; |
| 7202 | break; |
| 7203 | case 8: |
| 7204 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
| 7205 | return 1; |
| 7206 | break; |
| 7207 | } |
| 7208 | break; |
| 7209 | case 2: /* clts */ |
| 7210 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 7211 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
| 7212 | return 1; |
| 7213 | break; |
| 7214 | case 1: /* mov from cr */ |
| 7215 | switch (cr) { |
| 7216 | case 3: |
| 7217 | if (vmcs12->cpu_based_vm_exec_control & |
| 7218 | CPU_BASED_CR3_STORE_EXITING) |
| 7219 | return 1; |
| 7220 | break; |
| 7221 | case 8: |
| 7222 | if (vmcs12->cpu_based_vm_exec_control & |
| 7223 | CPU_BASED_CR8_STORE_EXITING) |
| 7224 | return 1; |
| 7225 | break; |
| 7226 | } |
| 7227 | break; |
| 7228 | case 3: /* lmsw */ |
| 7229 | /* |
| 7230 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 7231 | * cr0. Other attempted changes are ignored, with no exit. |
| 7232 | */ |
| 7233 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 7234 | (val ^ vmcs12->cr0_read_shadow)) |
| 7235 | return 1; |
| 7236 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 7237 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 7238 | (val & 0x1)) |
| 7239 | return 1; |
| 7240 | break; |
| 7241 | } |
| 7242 | return 0; |
| 7243 | } |
| 7244 | |
| 7245 | /* |
| 7246 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 7247 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 7248 | * when in is_guest_mode (L2). |
| 7249 | */ |
| 7250 | static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu) |
| 7251 | { |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7252 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 7253 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7254 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Jan Kiszka | 957c897 | 2013-02-24 14:11:34 +0100 | [diff] [blame] | 7255 | u32 exit_reason = vmx->exit_reason; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7256 | |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 7257 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 7258 | vmcs_readl(EXIT_QUALIFICATION), |
| 7259 | vmx->idt_vectoring_info, |
| 7260 | intr_info, |
| 7261 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 7262 | KVM_ISA_VMX); |
| 7263 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7264 | if (vmx->nested.nested_run_pending) |
| 7265 | return 0; |
| 7266 | |
| 7267 | if (unlikely(vmx->fail)) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 7268 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 7269 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7270 | return 1; |
| 7271 | } |
| 7272 | |
| 7273 | switch (exit_reason) { |
| 7274 | case EXIT_REASON_EXCEPTION_NMI: |
| 7275 | if (!is_exception(intr_info)) |
| 7276 | return 0; |
| 7277 | else if (is_page_fault(intr_info)) |
| 7278 | return enable_ept; |
Anthoine Bourgeois | e504c90 | 2013-11-13 11:45:37 +0100 | [diff] [blame] | 7279 | else if (is_no_device(intr_info) && |
Paolo Bonzini | ccf9844 | 2014-02-27 22:54:11 +0100 | [diff] [blame] | 7280 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
Anthoine Bourgeois | e504c90 | 2013-11-13 11:45:37 +0100 | [diff] [blame] | 7281 | return 0; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7282 | return vmcs12->exception_bitmap & |
| 7283 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 7284 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
| 7285 | return 0; |
| 7286 | case EXIT_REASON_TRIPLE_FAULT: |
| 7287 | return 1; |
| 7288 | case EXIT_REASON_PENDING_INTERRUPT: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 7289 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7290 | case EXIT_REASON_NMI_WINDOW: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 7291 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7292 | case EXIT_REASON_TASK_SWITCH: |
| 7293 | return 1; |
| 7294 | case EXIT_REASON_CPUID: |
Marcelo Tosatti | bc61349 | 2014-09-18 18:24:57 -0300 | [diff] [blame] | 7295 | if (kvm_register_read(vcpu, VCPU_REGS_RAX) == 0xa) |
| 7296 | return 0; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7297 | return 1; |
| 7298 | case EXIT_REASON_HLT: |
| 7299 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 7300 | case EXIT_REASON_INVD: |
| 7301 | return 1; |
| 7302 | case EXIT_REASON_INVLPG: |
| 7303 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 7304 | case EXIT_REASON_RDPMC: |
| 7305 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
| 7306 | case EXIT_REASON_RDTSC: |
| 7307 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 7308 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 7309 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 7310 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: |
| 7311 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: |
| 7312 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7313 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7314 | /* |
| 7315 | * VMX instructions trap unconditionally. This allows L1 to |
| 7316 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 7317 | */ |
| 7318 | return 1; |
| 7319 | case EXIT_REASON_CR_ACCESS: |
| 7320 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 7321 | case EXIT_REASON_DR_ACCESS: |
| 7322 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 7323 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7324 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7325 | case EXIT_REASON_MSR_READ: |
| 7326 | case EXIT_REASON_MSR_WRITE: |
| 7327 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 7328 | case EXIT_REASON_INVALID_STATE: |
| 7329 | return 1; |
| 7330 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 7331 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
| 7332 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 7333 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 7334 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 7335 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 7336 | nested_cpu_has2(vmcs12, |
| 7337 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 7338 | case EXIT_REASON_MCE_DURING_VMENTRY: |
| 7339 | return 0; |
| 7340 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7341 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7342 | case EXIT_REASON_APIC_ACCESS: |
| 7343 | return nested_cpu_has2(vmcs12, |
| 7344 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 7345 | case EXIT_REASON_EPT_VIOLATION: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 7346 | /* |
| 7347 | * L0 always deals with the EPT violation. If nested EPT is |
| 7348 | * used, and the nested mmu code discovers that the address is |
| 7349 | * missing in the guest EPT table (EPT12), the EPT violation |
| 7350 | * will be injected with nested_ept_inject_page_fault() |
| 7351 | */ |
| 7352 | return 0; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7353 | case EXIT_REASON_EPT_MISCONFIG: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 7354 | /* |
| 7355 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 7356 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 7357 | * (EPT on EPT). So any problems with the structure of the |
| 7358 | * table is L0's fault. |
| 7359 | */ |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7360 | return 0; |
| 7361 | case EXIT_REASON_WBINVD: |
| 7362 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 7363 | case EXIT_REASON_XSETBV: |
| 7364 | return 1; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 7365 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 7366 | /* |
| 7367 | * This should never happen, since it is not possible to |
| 7368 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 7369 | * If if it were, XSS would have to be checked against |
| 7370 | * the XSS exit bitmap in vmcs12. |
| 7371 | */ |
| 7372 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7373 | default: |
| 7374 | return 1; |
| 7375 | } |
| 7376 | } |
| 7377 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 7378 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 7379 | { |
| 7380 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 7381 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 7382 | } |
| 7383 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 7384 | static int vmx_enable_pml(struct vcpu_vmx *vmx) |
| 7385 | { |
| 7386 | struct page *pml_pg; |
| 7387 | u32 exec_control; |
| 7388 | |
| 7389 | pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 7390 | if (!pml_pg) |
| 7391 | return -ENOMEM; |
| 7392 | |
| 7393 | vmx->pml_pg = pml_pg; |
| 7394 | |
| 7395 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 7396 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 7397 | |
| 7398 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 7399 | exec_control |= SECONDARY_EXEC_ENABLE_PML; |
| 7400 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 7401 | |
| 7402 | return 0; |
| 7403 | } |
| 7404 | |
| 7405 | static void vmx_disable_pml(struct vcpu_vmx *vmx) |
| 7406 | { |
| 7407 | u32 exec_control; |
| 7408 | |
| 7409 | ASSERT(vmx->pml_pg); |
| 7410 | __free_page(vmx->pml_pg); |
| 7411 | vmx->pml_pg = NULL; |
| 7412 | |
| 7413 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 7414 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
| 7415 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 7416 | } |
| 7417 | |
| 7418 | static void vmx_flush_pml_buffer(struct vcpu_vmx *vmx) |
| 7419 | { |
| 7420 | struct kvm *kvm = vmx->vcpu.kvm; |
| 7421 | u64 *pml_buf; |
| 7422 | u16 pml_idx; |
| 7423 | |
| 7424 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 7425 | |
| 7426 | /* Do nothing if PML buffer is empty */ |
| 7427 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 7428 | return; |
| 7429 | |
| 7430 | /* PML index always points to next available PML buffer entity */ |
| 7431 | if (pml_idx >= PML_ENTITY_NUM) |
| 7432 | pml_idx = 0; |
| 7433 | else |
| 7434 | pml_idx++; |
| 7435 | |
| 7436 | pml_buf = page_address(vmx->pml_pg); |
| 7437 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 7438 | u64 gpa; |
| 7439 | |
| 7440 | gpa = pml_buf[pml_idx]; |
| 7441 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
| 7442 | mark_page_dirty(kvm, gpa >> PAGE_SHIFT); |
| 7443 | } |
| 7444 | |
| 7445 | /* reset PML index */ |
| 7446 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 7447 | } |
| 7448 | |
| 7449 | /* |
| 7450 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 7451 | * Called before reporting dirty_bitmap to userspace. |
| 7452 | */ |
| 7453 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 7454 | { |
| 7455 | int i; |
| 7456 | struct kvm_vcpu *vcpu; |
| 7457 | /* |
| 7458 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 7459 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 7460 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 7461 | * buffer. |
| 7462 | */ |
| 7463 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 7464 | kvm_vcpu_kick(vcpu); |
| 7465 | } |
| 7466 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7467 | /* |
| 7468 | * The guest has exited. See if we can fix it or if we need userspace |
| 7469 | * assistance. |
| 7470 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7471 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7472 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 7473 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 7474 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 7475 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 7476 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 7477 | /* |
| 7478 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 7479 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 7480 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 7481 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 7482 | * flushed already. |
| 7483 | */ |
| 7484 | if (enable_pml) |
| 7485 | vmx_flush_pml_buffer(vmx); |
| 7486 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7487 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 7488 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7489 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 7490 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7491 | if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 7492 | nested_vmx_vmexit(vcpu, exit_reason, |
| 7493 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 7494 | vmcs_readl(EXIT_QUALIFICATION)); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7495 | return 1; |
| 7496 | } |
| 7497 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 7498 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
| 7499 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 7500 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 7501 | = exit_reason; |
| 7502 | return 0; |
| 7503 | } |
| 7504 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 7505 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7506 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 7507 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 7508 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 7509 | return 0; |
| 7510 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7511 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 7512 | /* |
| 7513 | * Note: |
| 7514 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 7515 | * delivery event since it indicates guest is accessing MMIO. |
| 7516 | * The vm-exit can be triggered again after return to guest that |
| 7517 | * will cause infinite loop. |
| 7518 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 7519 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7520 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7521 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 7522 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 7523 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7524 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
| 7525 | vcpu->run->internal.ndata = 2; |
| 7526 | vcpu->run->internal.data[0] = vectoring_info; |
| 7527 | vcpu->run->internal.data[1] = exit_reason; |
| 7528 | return 0; |
| 7529 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 7530 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7531 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked && |
| 7532 | !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis( |
Nadav Har'El | f5c4368 | 2013-08-05 11:07:20 +0300 | [diff] [blame] | 7533 | get_vmcs12(vcpu))))) { |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 7534 | if (vmx_interrupt_allowed(vcpu)) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 7535 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 7536 | } else if (vmx->vnmi_blocked_time > 1000000000LL && |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 7537 | vcpu->arch.nmi_pending) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 7538 | /* |
| 7539 | * This CPU don't support us in finding the end of an |
| 7540 | * NMI-blocked window if the guest runs with IRQs |
| 7541 | * disabled. So we pull the trigger after 1 s of |
| 7542 | * futile waiting, but inform the user about this. |
| 7543 | */ |
| 7544 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 7545 | "state on VCPU %d after 1 s timeout\n", |
| 7546 | __func__, vcpu->vcpu_id); |
| 7547 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 7548 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 7549 | } |
| 7550 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7551 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 7552 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7553 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7554 | else { |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 7555 | WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason); |
| 7556 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7557 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7558 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7559 | } |
| 7560 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7561 | 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] | 7562 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7563 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 7564 | |
| 7565 | if (is_guest_mode(vcpu) && |
| 7566 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 7567 | return; |
| 7568 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7569 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7570 | vmcs_write32(TPR_THRESHOLD, 0); |
| 7571 | return; |
| 7572 | } |
| 7573 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7574 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7575 | } |
| 7576 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 7577 | static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
| 7578 | { |
| 7579 | u32 sec_exec_control; |
| 7580 | |
| 7581 | /* |
| 7582 | * There is not point to enable virtualize x2apic without enable |
| 7583 | * apicv |
| 7584 | */ |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7585 | if (!cpu_has_vmx_virtualize_x2apic_mode() || |
| 7586 | !vmx_vm_has_apicv(vcpu->kvm)) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 7587 | return; |
| 7588 | |
| 7589 | if (!vm_need_tpr_shadow(vcpu->kvm)) |
| 7590 | return; |
| 7591 | |
| 7592 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 7593 | |
| 7594 | if (set) { |
| 7595 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 7596 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 7597 | } else { |
| 7598 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 7599 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 7600 | } |
| 7601 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 7602 | |
| 7603 | vmx_set_msr_bitmap(vcpu); |
| 7604 | } |
| 7605 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 7606 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 7607 | { |
| 7608 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7609 | |
| 7610 | /* |
| 7611 | * Currently we do not handle the nested case where L2 has an |
| 7612 | * APIC access page of its own; that page is still pinned. |
| 7613 | * Hence, we skip the case where the VCPU is in guest mode _and_ |
| 7614 | * L1 prepared an APIC access page for L2. |
| 7615 | * |
| 7616 | * For the case where L1 and L2 share the same APIC access page |
| 7617 | * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear |
| 7618 | * in the vmcs12), this function will only update either the vmcs01 |
| 7619 | * or the vmcs02. If the former, the vmcs02 will be updated by |
| 7620 | * prepare_vmcs02. If the latter, the vmcs01 will be updated in |
| 7621 | * the next L2->L1 exit. |
| 7622 | */ |
| 7623 | if (!is_guest_mode(vcpu) || |
| 7624 | !nested_cpu_has2(vmx->nested.current_vmcs12, |
| 7625 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 7626 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 7627 | } |
| 7628 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7629 | static void vmx_hwapic_isr_update(struct kvm *kvm, int isr) |
| 7630 | { |
| 7631 | u16 status; |
| 7632 | u8 old; |
| 7633 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7634 | if (isr == -1) |
| 7635 | isr = 0; |
| 7636 | |
| 7637 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 7638 | old = status >> 8; |
| 7639 | if (isr != old) { |
| 7640 | status &= 0xff; |
| 7641 | status |= isr << 8; |
| 7642 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 7643 | } |
| 7644 | } |
| 7645 | |
| 7646 | static void vmx_set_rvi(int vector) |
| 7647 | { |
| 7648 | u16 status; |
| 7649 | u8 old; |
| 7650 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 7651 | if (vector == -1) |
| 7652 | vector = 0; |
| 7653 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7654 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 7655 | old = (u8)status & 0xff; |
| 7656 | if ((u8)vector != old) { |
| 7657 | status &= ~0xff; |
| 7658 | status |= (u8)vector; |
| 7659 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 7660 | } |
| 7661 | } |
| 7662 | |
| 7663 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 7664 | { |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 7665 | if (!is_guest_mode(vcpu)) { |
| 7666 | vmx_set_rvi(max_irr); |
| 7667 | return; |
| 7668 | } |
| 7669 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 7670 | if (max_irr == -1) |
| 7671 | return; |
| 7672 | |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 7673 | /* |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 7674 | * In guest mode. If a vmexit is needed, vmx_check_nested_events |
| 7675 | * handles it. |
| 7676 | */ |
| 7677 | if (nested_exit_on_intr(vcpu)) |
| 7678 | return; |
| 7679 | |
| 7680 | /* |
| 7681 | * Else, fall back to pre-APICv interrupt injection since L2 |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 7682 | * is run without virtual interrupt delivery. |
| 7683 | */ |
| 7684 | if (!kvm_event_needs_reinjection(vcpu) && |
| 7685 | vmx_interrupt_allowed(vcpu)) { |
| 7686 | kvm_queue_interrupt(vcpu, max_irr, false); |
| 7687 | vmx_inject_irq(vcpu); |
| 7688 | } |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7689 | } |
| 7690 | |
| 7691 | static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) |
| 7692 | { |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 7693 | if (!vmx_vm_has_apicv(vcpu->kvm)) |
| 7694 | return; |
| 7695 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7696 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 7697 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 7698 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 7699 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 7700 | } |
| 7701 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 7702 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 7703 | { |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 7704 | u32 exit_intr_info; |
| 7705 | |
| 7706 | if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 7707 | || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
| 7708 | return; |
| 7709 | |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 7710 | vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 7711 | exit_intr_info = vmx->exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 7712 | |
| 7713 | /* Handle machine checks before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 7714 | if (is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 7715 | kvm_machine_check(); |
| 7716 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 7717 | /* We need to handle NMIs before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 7718 | 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] | 7719 | (exit_intr_info & INTR_INFO_VALID_MASK)) { |
| 7720 | kvm_before_handle_nmi(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 7721 | asm("int $2"); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 7722 | kvm_after_handle_nmi(&vmx->vcpu); |
| 7723 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 7724 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 7725 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 7726 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 7727 | { |
| 7728 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 7729 | |
| 7730 | /* |
| 7731 | * If external interrupt exists, IF bit is set in rflags/eflags on the |
| 7732 | * interrupt stack frame, and interrupt will be enabled on a return |
| 7733 | * from interrupt handler. |
| 7734 | */ |
| 7735 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 7736 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 7737 | unsigned int vector; |
| 7738 | unsigned long entry; |
| 7739 | gate_desc *desc; |
| 7740 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7741 | #ifdef CONFIG_X86_64 |
| 7742 | unsigned long tmp; |
| 7743 | #endif |
| 7744 | |
| 7745 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 7746 | desc = (gate_desc *)vmx->host_idt_base + vector; |
| 7747 | entry = gate_offset(*desc); |
| 7748 | asm volatile( |
| 7749 | #ifdef CONFIG_X86_64 |
| 7750 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 7751 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 7752 | "push $%c[ss]\n\t" |
| 7753 | "push %[sp]\n\t" |
| 7754 | #endif |
| 7755 | "pushf\n\t" |
| 7756 | "orl $0x200, (%%" _ASM_SP ")\n\t" |
| 7757 | __ASM_SIZE(push) " $%c[cs]\n\t" |
| 7758 | "call *%[entry]\n\t" |
| 7759 | : |
| 7760 | #ifdef CONFIG_X86_64 |
| 7761 | [sp]"=&r"(tmp) |
| 7762 | #endif |
| 7763 | : |
| 7764 | [entry]"r"(entry), |
| 7765 | [ss]"i"(__KERNEL_DS), |
| 7766 | [cs]"i"(__KERNEL_CS) |
| 7767 | ); |
| 7768 | } else |
| 7769 | local_irq_enable(); |
| 7770 | } |
| 7771 | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 7772 | static bool vmx_mpx_supported(void) |
| 7773 | { |
| 7774 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && |
| 7775 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); |
| 7776 | } |
| 7777 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 7778 | static bool vmx_xsaves_supported(void) |
| 7779 | { |
| 7780 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 7781 | SECONDARY_EXEC_XSAVES; |
| 7782 | } |
| 7783 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 7784 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 7785 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 7786 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 7787 | bool unblock_nmi; |
| 7788 | u8 vector; |
| 7789 | bool idtv_info_valid; |
| 7790 | |
| 7791 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 7792 | |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 7793 | if (cpu_has_virtual_nmis()) { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 7794 | if (vmx->nmi_known_unmasked) |
| 7795 | return; |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 7796 | /* |
| 7797 | * Can't use vmx->exit_intr_info since we're not sure what |
| 7798 | * the exit reason is. |
| 7799 | */ |
| 7800 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 7801 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 7802 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 7803 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 7804 | * SDM 3: 27.7.1.2 (September 2008) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 7805 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 7806 | * a guest IRET fault. |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 7807 | * SDM 3: 23.2.2 (September 2008) |
| 7808 | * Bit 12 is undefined in any of the following cases: |
| 7809 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 7810 | * information field. |
| 7811 | * If the VM exit is due to a double fault. |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 7812 | */ |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 7813 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 7814 | vector != DF_VECTOR && !idtv_info_valid) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 7815 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 7816 | GUEST_INTR_STATE_NMI); |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 7817 | else |
| 7818 | vmx->nmi_known_unmasked = |
| 7819 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 7820 | & GUEST_INTR_STATE_NMI); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 7821 | } else if (unlikely(vmx->soft_vnmi_blocked)) |
| 7822 | vmx->vnmi_blocked_time += |
| 7823 | ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 7824 | } |
| 7825 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 7826 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 7827 | u32 idt_vectoring_info, |
| 7828 | int instr_len_field, |
| 7829 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 7830 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 7831 | u8 vector; |
| 7832 | int type; |
| 7833 | bool idtv_info_valid; |
| 7834 | |
| 7835 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 7836 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 7837 | vcpu->arch.nmi_injected = false; |
| 7838 | kvm_clear_exception_queue(vcpu); |
| 7839 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 7840 | |
| 7841 | if (!idtv_info_valid) |
| 7842 | return; |
| 7843 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 7844 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7845 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 7846 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 7847 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 7848 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7849 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 7850 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 7851 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 7852 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 7853 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 7854 | * Clear bit "block by NMI" before VM entry if a NMI |
| 7855 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 7856 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 7857 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 7858 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 7859 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 7860 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 7861 | /* fall through */ |
| 7862 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 7863 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 7864 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 7865 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 7866 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 7867 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 7868 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 7869 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 7870 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 7871 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 7872 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 7873 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 7874 | break; |
| 7875 | default: |
| 7876 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 7877 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 7878 | } |
| 7879 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 7880 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 7881 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 7882 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 7883 | VM_EXIT_INSTRUCTION_LEN, |
| 7884 | IDT_VECTORING_ERROR_CODE); |
| 7885 | } |
| 7886 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 7887 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 7888 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 7889 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 7890 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 7891 | VM_ENTRY_INSTRUCTION_LEN, |
| 7892 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 7893 | |
| 7894 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 7895 | } |
| 7896 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 7897 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 7898 | { |
| 7899 | int i, nr_msrs; |
| 7900 | struct perf_guest_switch_msr *msrs; |
| 7901 | |
| 7902 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 7903 | |
| 7904 | if (!msrs) |
| 7905 | return; |
| 7906 | |
| 7907 | for (i = 0; i < nr_msrs; i++) |
| 7908 | if (msrs[i].host == msrs[i].guest) |
| 7909 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 7910 | else |
| 7911 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
| 7912 | msrs[i].host); |
| 7913 | } |
| 7914 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 7915 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7916 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 7917 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 7918 | unsigned long debugctlmsr, cr4; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 7919 | |
| 7920 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
| 7921 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) |
| 7922 | vmx->entry_time = ktime_get(); |
| 7923 | |
| 7924 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 7925 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 7926 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 7927 | return; |
| 7928 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 7929 | if (vmx->ple_window_dirty) { |
| 7930 | vmx->ple_window_dirty = false; |
| 7931 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 7932 | } |
| 7933 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7934 | if (vmx->nested.sync_shadow_vmcs) { |
| 7935 | copy_vmcs12_to_shadow(vmx); |
| 7936 | vmx->nested.sync_shadow_vmcs = false; |
| 7937 | } |
| 7938 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 7939 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 7940 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 7941 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 7942 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 7943 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 7944 | cr4 = read_cr4(); |
| 7945 | if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) { |
| 7946 | vmcs_writel(HOST_CR4, cr4); |
| 7947 | vmx->host_state.vmcs_host_cr4 = cr4; |
| 7948 | } |
| 7949 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 7950 | /* When single-stepping over STI and MOV SS, we must clear the |
| 7951 | * corresponding interruptibility bits in the guest state. Otherwise |
| 7952 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 7953 | * exceptions being set, but that's not correct for the guest debugging |
| 7954 | * case. */ |
| 7955 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 7956 | vmx_set_interrupt_shadow(vcpu, 0); |
| 7957 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 7958 | atomic_switch_perf_msrs(vmx); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 7959 | debugctlmsr = get_debugctlmsr(); |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 7960 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 7961 | vmx->__launched = vmx->loaded_vmcs->launched; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 7962 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7963 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 7964 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 7965 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 7966 | "push %%" _ASM_CX " \n\t" |
| 7967 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 7968 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 7969 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 7970 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 7971 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 7972 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 7973 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 7974 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 7975 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 7976 | "je 2f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 7977 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 7978 | "2: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7979 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 7980 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7981 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 7982 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 7983 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 7984 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 7985 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 7986 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 7987 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 7988 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 7989 | "mov %c[r8](%0), %%r8 \n\t" |
| 7990 | "mov %c[r9](%0), %%r9 \n\t" |
| 7991 | "mov %c[r10](%0), %%r10 \n\t" |
| 7992 | "mov %c[r11](%0), %%r11 \n\t" |
| 7993 | "mov %c[r12](%0), %%r12 \n\t" |
| 7994 | "mov %c[r13](%0), %%r13 \n\t" |
| 7995 | "mov %c[r14](%0), %%r14 \n\t" |
| 7996 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7997 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 7998 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 7999 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8000 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 8001 | "jne 1f \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 8002 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 8003 | "jmp 2f \n\t" |
| 8004 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" |
| 8005 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8006 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8007 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 8008 | "pop %0 \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8009 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 8010 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 8011 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 8012 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 8013 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 8014 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 8015 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 8016 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8017 | "mov %%r8, %c[r8](%0) \n\t" |
| 8018 | "mov %%r9, %c[r9](%0) \n\t" |
| 8019 | "mov %%r10, %c[r10](%0) \n\t" |
| 8020 | "mov %%r11, %c[r11](%0) \n\t" |
| 8021 | "mov %%r12, %c[r12](%0) \n\t" |
| 8022 | "mov %%r13, %c[r13](%0) \n\t" |
| 8023 | "mov %%r14, %c[r14](%0) \n\t" |
| 8024 | "mov %%r15, %c[r15](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8025 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8026 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 8027 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 8028 | |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8029 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8030 | "setbe %c[fail](%0) \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 8031 | ".pushsection .rodata \n\t" |
| 8032 | ".global vmx_return \n\t" |
| 8033 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 8034 | ".popsection" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8035 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 8036 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8037 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 8038 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 8039 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 8040 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 8041 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 8042 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 8043 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 8044 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 8045 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 8046 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 8047 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 8048 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 8049 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 8050 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 8051 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 8052 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 8053 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 8054 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8055 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 8056 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 8057 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 8058 | : "cc", "memory" |
| 8059 | #ifdef CONFIG_X86_64 |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8060 | , "rax", "rbx", "rdi", "rsi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 8061 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8062 | #else |
| 8063 | , "eax", "ebx", "edi", "esi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 8064 | #endif |
| 8065 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8066 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 8067 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
| 8068 | if (debugctlmsr) |
| 8069 | update_debugctlmsr(debugctlmsr); |
| 8070 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 8071 | #ifndef CONFIG_X86_64 |
| 8072 | /* |
| 8073 | * The sysexit path does not restore ds/es, so we must set them to |
| 8074 | * a reasonable value ourselves. |
| 8075 | * |
| 8076 | * We can't defer this to vmx_load_host_state() since that function |
| 8077 | * may be executed in interrupt context, which saves and restore segments |
| 8078 | * around it, nullifying its effect. |
| 8079 | */ |
| 8080 | loadsegment(ds, __USER_DS); |
| 8081 | loadsegment(es, __USER_DS); |
| 8082 | #endif |
| 8083 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 8084 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 8085 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 8086 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 8087 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 8088 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 8089 | vcpu->arch.regs_dirty = 0; |
| 8090 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 8091 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 8092 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 8093 | vmx->loaded_vmcs->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 8094 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8095 | vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); |
Jan Kiszka | 1e2b1dd | 2011-09-12 10:52:24 +0200 | [diff] [blame] | 8096 | trace_kvm_exit(vmx->exit_reason, vcpu, KVM_ISA_VMX); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8097 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 8098 | /* |
| 8099 | * the KVM_REQ_EVENT optimization bit is only on for one entry, and if |
| 8100 | * we did not inject a still-pending event to L1 now because of |
| 8101 | * nested_run_pending, we need to re-enable this bit. |
| 8102 | */ |
| 8103 | if (vmx->nested.nested_run_pending) |
| 8104 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 8105 | |
| 8106 | vmx->nested.nested_run_pending = 0; |
| 8107 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8108 | vmx_complete_atomic_exit(vmx); |
| 8109 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8110 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8111 | } |
| 8112 | |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 8113 | static void vmx_load_vmcs01(struct kvm_vcpu *vcpu) |
| 8114 | { |
| 8115 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8116 | int cpu; |
| 8117 | |
| 8118 | if (vmx->loaded_vmcs == &vmx->vmcs01) |
| 8119 | return; |
| 8120 | |
| 8121 | cpu = get_cpu(); |
| 8122 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 8123 | vmx_vcpu_put(vcpu); |
| 8124 | vmx_vcpu_load(vcpu, cpu); |
| 8125 | vcpu->cpu = cpu; |
| 8126 | put_cpu(); |
| 8127 | } |
| 8128 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8129 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 8130 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8131 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8132 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 8133 | if (enable_pml) |
| 8134 | vmx_disable_pml(vmx); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 8135 | free_vpid(vmx); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 8136 | leave_guest_mode(vcpu); |
| 8137 | vmx_load_vmcs01(vcpu); |
Marcelo Tosatti | 26a865f | 2014-01-03 17:00:51 -0200 | [diff] [blame] | 8138 | free_nested(vmx); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 8139 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8140 | kfree(vmx->guest_msrs); |
| 8141 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 8142 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8143 | } |
| 8144 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8145 | 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] | 8146 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8147 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 8148 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 8149 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8150 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 8151 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8152 | return ERR_PTR(-ENOMEM); |
| 8153 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 8154 | allocate_vpid(vmx); |
| 8155 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8156 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 8157 | if (err) |
| 8158 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 8159 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 8160 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 8161 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 8162 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 8163 | |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 8164 | err = -ENOMEM; |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8165 | if (!vmx->guest_msrs) { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8166 | goto uninit_vcpu; |
| 8167 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 8168 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 8169 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 8170 | vmx->loaded_vmcs->vmcs = alloc_vmcs(); |
| 8171 | if (!vmx->loaded_vmcs->vmcs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8172 | goto free_msrs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 8173 | if (!vmm_exclusive) |
| 8174 | kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id()))); |
| 8175 | loaded_vmcs_init(vmx->loaded_vmcs); |
| 8176 | if (!vmm_exclusive) |
| 8177 | kvm_cpu_vmxoff(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 8178 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 8179 | cpu = get_cpu(); |
| 8180 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 8181 | vmx->vcpu.cpu = cpu; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 8182 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8183 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 8184 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8185 | if (err) |
| 8186 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 8187 | if (vm_need_virtualize_apic_accesses(kvm)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 8188 | err = alloc_apic_access_page(kvm); |
| 8189 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 8190 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 8191 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 8192 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 8193 | if (enable_ept) { |
| 8194 | if (!kvm->arch.ept_identity_map_addr) |
| 8195 | kvm->arch.ept_identity_map_addr = |
| 8196 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 8197 | err = init_rmode_identity_map(kvm); |
| 8198 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 8199 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 8200 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 8201 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 8202 | vmx->nested.current_vmptr = -1ull; |
| 8203 | vmx->nested.current_vmcs12 = NULL; |
| 8204 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 8205 | /* |
| 8206 | * If PML is turned on, failure on enabling PML just results in failure |
| 8207 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 8208 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 8209 | * for the guest, etc. |
| 8210 | */ |
| 8211 | if (enable_pml) { |
| 8212 | err = vmx_enable_pml(vmx); |
| 8213 | if (err) |
| 8214 | goto free_vmcs; |
| 8215 | } |
| 8216 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8217 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 8218 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8219 | free_vmcs: |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 8220 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8221 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8222 | kfree(vmx->guest_msrs); |
| 8223 | uninit_vcpu: |
| 8224 | kvm_vcpu_uninit(&vmx->vcpu); |
| 8225 | free_vcpu: |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 8226 | free_vpid(vmx); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 8227 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 8228 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8229 | } |
| 8230 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 8231 | static void __init vmx_check_processor_compat(void *rtn) |
| 8232 | { |
| 8233 | struct vmcs_config vmcs_conf; |
| 8234 | |
| 8235 | *(int *)rtn = 0; |
| 8236 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 8237 | *(int *)rtn = -EIO; |
| 8238 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 8239 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 8240 | smp_processor_id()); |
| 8241 | *(int *)rtn = -EIO; |
| 8242 | } |
| 8243 | } |
| 8244 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 8245 | static int get_ept_level(void) |
| 8246 | { |
| 8247 | return VMX_EPT_DEFAULT_GAW + 1; |
| 8248 | } |
| 8249 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 8250 | 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] | 8251 | { |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 8252 | u64 ret; |
| 8253 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 8254 | /* For VT-d and EPT combination |
| 8255 | * 1. MMIO: always map as UC |
| 8256 | * 2. EPT with VT-d: |
| 8257 | * a. VT-d without snooping control feature: can't guarantee the |
| 8258 | * result, try to trust guest. |
| 8259 | * b. VT-d with snooping control feature: snooping control feature of |
| 8260 | * VT-d engine can guarantee the cache correctness. Just set it |
| 8261 | * 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] | 8262 | * 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] | 8263 | * consistent with host MTRR |
| 8264 | */ |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 8265 | if (is_mmio) |
| 8266 | ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT; |
Alex Williamson | e0f0bbc | 2013-10-30 11:02:30 -0600 | [diff] [blame] | 8267 | else if (kvm_arch_has_noncoherent_dma(vcpu->kvm)) |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 8268 | ret = kvm_get_guest_memory_type(vcpu, gfn) << |
| 8269 | VMX_EPT_MT_EPTE_SHIFT; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 8270 | else |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 8271 | ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT) |
Sheng Yang | a19a6d1 | 2010-02-09 16:41:53 +0800 | [diff] [blame] | 8272 | | VMX_EPT_IPAT_BIT; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 8273 | |
| 8274 | return ret; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 8275 | } |
| 8276 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 8277 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 8278 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 8279 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 8280 | return PT_DIRECTORY_LEVEL; |
| 8281 | else |
| 8282 | /* For shadow and EPT supported 1GB page */ |
| 8283 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 8284 | } |
| 8285 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 8286 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 8287 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 8288 | struct kvm_cpuid_entry2 *best; |
| 8289 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8290 | u32 exec_control; |
| 8291 | |
| 8292 | vmx->rdtscp_enabled = false; |
| 8293 | if (vmx_rdtscp_supported()) { |
| 8294 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 8295 | if (exec_control & SECONDARY_EXEC_RDTSCP) { |
| 8296 | best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0); |
| 8297 | if (best && (best->edx & bit(X86_FEATURE_RDTSCP))) |
| 8298 | vmx->rdtscp_enabled = true; |
| 8299 | else { |
| 8300 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 8301 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 8302 | exec_control); |
| 8303 | } |
| 8304 | } |
| 8305 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 8306 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 8307 | /* Exposing INVPCID only when PCID is exposed */ |
| 8308 | best = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 8309 | if (vmx_invpcid_supported() && |
Ren, Yongjie | 4f97704 | 2012-09-07 07:36:59 +0000 | [diff] [blame] | 8310 | best && (best->ebx & bit(X86_FEATURE_INVPCID)) && |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 8311 | guest_cpuid_has_pcid(vcpu)) { |
Takashi Iwai | 29282fd | 2012-11-09 15:20:17 +0100 | [diff] [blame] | 8312 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 8313 | exec_control |= SECONDARY_EXEC_ENABLE_INVPCID; |
| 8314 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 8315 | exec_control); |
| 8316 | } else { |
Takashi Iwai | 29282fd | 2012-11-09 15:20:17 +0100 | [diff] [blame] | 8317 | if (cpu_has_secondary_exec_ctrls()) { |
| 8318 | exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 8319 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 8320 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 8321 | exec_control); |
| 8322 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 8323 | if (best) |
Ren, Yongjie | 4f97704 | 2012-09-07 07:36:59 +0000 | [diff] [blame] | 8324 | best->ebx &= ~bit(X86_FEATURE_INVPCID); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 8325 | } |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 8326 | } |
| 8327 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 8328 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 8329 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 8330 | if (func == 1 && nested) |
| 8331 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 8332 | } |
| 8333 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 8334 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 8335 | struct x86_exception *fault) |
| 8336 | { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 8337 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 8338 | u32 exit_reason; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 8339 | |
| 8340 | if (fault->error_code & PFERR_RSVD_MASK) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 8341 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 8342 | else |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 8343 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
| 8344 | nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 8345 | vmcs12->guest_physical_address = fault->address; |
| 8346 | } |
| 8347 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 8348 | /* Callbacks for nested_ept_init_mmu_context: */ |
| 8349 | |
| 8350 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) |
| 8351 | { |
| 8352 | /* return the page table to be shadowed - in our case, EPT12 */ |
| 8353 | return get_vmcs12(vcpu)->ept_pointer; |
| 8354 | } |
| 8355 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 8356 | static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 8357 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 8358 | WARN_ON(mmu_is_nested(vcpu)); |
| 8359 | kvm_init_shadow_ept_mmu(vcpu, |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 8360 | nested_vmx_ept_caps & VMX_EPT_EXECUTE_ONLY_BIT); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 8361 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
| 8362 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; |
| 8363 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; |
| 8364 | |
| 8365 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 8366 | } |
| 8367 | |
| 8368 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 8369 | { |
| 8370 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 8371 | } |
| 8372 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 8373 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 8374 | u16 error_code) |
| 8375 | { |
| 8376 | bool inequality, bit; |
| 8377 | |
| 8378 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 8379 | inequality = |
| 8380 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 8381 | vmcs12->page_fault_error_code_match; |
| 8382 | return inequality ^ bit; |
| 8383 | } |
| 8384 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 8385 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 8386 | struct x86_exception *fault) |
| 8387 | { |
| 8388 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 8389 | |
| 8390 | WARN_ON(!is_guest_mode(vcpu)); |
| 8391 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 8392 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code)) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 8393 | nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason, |
| 8394 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8395 | vmcs_readl(EXIT_QUALIFICATION)); |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 8396 | else |
| 8397 | kvm_inject_page_fault(vcpu, fault); |
| 8398 | } |
| 8399 | |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 8400 | static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, |
| 8401 | struct vmcs12 *vmcs12) |
| 8402 | { |
| 8403 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8404 | |
| 8405 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8406 | /* TODO: Also verify bits beyond physical address width are 0 */ |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 8407 | if (!PAGE_ALIGNED(vmcs12->apic_access_addr)) |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 8408 | return false; |
| 8409 | |
| 8410 | /* |
| 8411 | * Translate L1 physical address to host physical |
| 8412 | * address for vmcs02. Keep the page pinned, so this |
| 8413 | * physical address remains valid. We keep a reference |
| 8414 | * to it so we can release it later. |
| 8415 | */ |
| 8416 | if (vmx->nested.apic_access_page) /* shouldn't happen */ |
| 8417 | nested_release_page(vmx->nested.apic_access_page); |
| 8418 | vmx->nested.apic_access_page = |
| 8419 | nested_get_page(vcpu, vmcs12->apic_access_addr); |
| 8420 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8421 | |
| 8422 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 8423 | /* TODO: Also verify bits beyond physical address width are 0 */ |
| 8424 | if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr)) |
| 8425 | return false; |
| 8426 | |
| 8427 | if (vmx->nested.virtual_apic_page) /* shouldn't happen */ |
| 8428 | nested_release_page(vmx->nested.virtual_apic_page); |
| 8429 | vmx->nested.virtual_apic_page = |
| 8430 | nested_get_page(vcpu, vmcs12->virtual_apic_page_addr); |
| 8431 | |
| 8432 | /* |
| 8433 | * Failing the vm entry is _not_ what the processor does |
| 8434 | * but it's basically the only possibility we have. |
| 8435 | * We could still enter the guest if CR8 load exits are |
| 8436 | * enabled, CR8 store exits are enabled, and virtualize APIC |
| 8437 | * access is disabled; in this case the processor would never |
| 8438 | * use the TPR shadow and we could simply clear the bit from |
| 8439 | * the execution control. But such a configuration is useless, |
| 8440 | * so let's keep the code simple. |
| 8441 | */ |
| 8442 | if (!vmx->nested.virtual_apic_page) |
| 8443 | return false; |
| 8444 | } |
| 8445 | |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 8446 | return true; |
| 8447 | } |
| 8448 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 8449 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 8450 | { |
| 8451 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 8452 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8453 | |
| 8454 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 8455 | return; |
| 8456 | |
| 8457 | /* Make sure short timeouts reliably trigger an immediate vmexit. |
| 8458 | * hrtimer_start does not guarantee this. */ |
| 8459 | if (preemption_timeout <= 1) { |
| 8460 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 8461 | return; |
| 8462 | } |
| 8463 | |
| 8464 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 8465 | preemption_timeout *= 1000000; |
| 8466 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 8467 | hrtimer_start(&vmx->nested.preemption_timer, |
| 8468 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 8469 | } |
| 8470 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 8471 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
| 8472 | unsigned long count_field, |
| 8473 | unsigned long addr_field, |
| 8474 | int maxphyaddr) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8475 | { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 8476 | u64 count, addr; |
| 8477 | |
| 8478 | if (vmcs12_read_any(vcpu, count_field, &count) || |
| 8479 | vmcs12_read_any(vcpu, addr_field, &addr)) { |
| 8480 | WARN_ON(1); |
| 8481 | return -EINVAL; |
| 8482 | } |
| 8483 | if (count == 0) |
| 8484 | return 0; |
| 8485 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
| 8486 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { |
| 8487 | pr_warn_ratelimited( |
| 8488 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
| 8489 | addr_field, maxphyaddr, count, addr); |
| 8490 | return -EINVAL; |
| 8491 | } |
| 8492 | return 0; |
| 8493 | } |
| 8494 | |
| 8495 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 8496 | struct vmcs12 *vmcs12) |
| 8497 | { |
| 8498 | int maxphyaddr; |
| 8499 | |
| 8500 | if (vmcs12->vm_exit_msr_load_count == 0 && |
| 8501 | vmcs12->vm_exit_msr_store_count == 0 && |
| 8502 | vmcs12->vm_entry_msr_load_count == 0) |
| 8503 | return 0; /* Fast path */ |
| 8504 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 8505 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
| 8506 | VM_EXIT_MSR_LOAD_ADDR, maxphyaddr) || |
| 8507 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
| 8508 | VM_EXIT_MSR_STORE_ADDR, maxphyaddr) || |
| 8509 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
| 8510 | VM_ENTRY_MSR_LOAD_ADDR, maxphyaddr)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8511 | return -EINVAL; |
| 8512 | return 0; |
| 8513 | } |
| 8514 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 8515 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 8516 | struct vmx_msr_entry *e) |
| 8517 | { |
| 8518 | /* x2APIC MSR accesses are not allowed */ |
| 8519 | if (apic_x2apic_mode(vcpu->arch.apic) && e->index >> 8 == 0x8) |
| 8520 | return -EINVAL; |
| 8521 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 8522 | e->index == MSR_IA32_UCODE_REV) |
| 8523 | return -EINVAL; |
| 8524 | if (e->reserved != 0) |
| 8525 | return -EINVAL; |
| 8526 | return 0; |
| 8527 | } |
| 8528 | |
| 8529 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 8530 | struct vmx_msr_entry *e) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8531 | { |
| 8532 | if (e->index == MSR_FS_BASE || |
| 8533 | e->index == MSR_GS_BASE || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 8534 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 8535 | nested_vmx_msr_check_common(vcpu, e)) |
| 8536 | return -EINVAL; |
| 8537 | return 0; |
| 8538 | } |
| 8539 | |
| 8540 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 8541 | struct vmx_msr_entry *e) |
| 8542 | { |
| 8543 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 8544 | nested_vmx_msr_check_common(vcpu, e)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8545 | return -EINVAL; |
| 8546 | return 0; |
| 8547 | } |
| 8548 | |
| 8549 | /* |
| 8550 | * Load guest's/host's msr at nested entry/exit. |
| 8551 | * return 0 for success, entry index for failure. |
| 8552 | */ |
| 8553 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 8554 | { |
| 8555 | u32 i; |
| 8556 | struct vmx_msr_entry e; |
| 8557 | struct msr_data msr; |
| 8558 | |
| 8559 | msr.host_initiated = false; |
| 8560 | for (i = 0; i < count; i++) { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 8561 | if (kvm_read_guest(vcpu->kvm, gpa + i * sizeof(e), |
| 8562 | &e, sizeof(e))) { |
| 8563 | pr_warn_ratelimited( |
| 8564 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 8565 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8566 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 8567 | } |
| 8568 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
| 8569 | pr_warn_ratelimited( |
| 8570 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 8571 | __func__, i, e.index, e.reserved); |
| 8572 | goto fail; |
| 8573 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8574 | msr.index = e.index; |
| 8575 | msr.data = e.value; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 8576 | if (kvm_set_msr(vcpu, &msr)) { |
| 8577 | pr_warn_ratelimited( |
| 8578 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 8579 | __func__, i, e.index, e.value); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8580 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 8581 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8582 | } |
| 8583 | return 0; |
| 8584 | fail: |
| 8585 | return i + 1; |
| 8586 | } |
| 8587 | |
| 8588 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 8589 | { |
| 8590 | u32 i; |
| 8591 | struct vmx_msr_entry e; |
| 8592 | |
| 8593 | for (i = 0; i < count; i++) { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 8594 | if (kvm_read_guest(vcpu->kvm, |
| 8595 | gpa + i * sizeof(e), |
| 8596 | &e, 2 * sizeof(u32))) { |
| 8597 | pr_warn_ratelimited( |
| 8598 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 8599 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8600 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 8601 | } |
| 8602 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
| 8603 | pr_warn_ratelimited( |
| 8604 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 8605 | __func__, i, e.index, e.reserved); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8606 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 8607 | } |
| 8608 | if (kvm_get_msr(vcpu, e.index, &e.value)) { |
| 8609 | pr_warn_ratelimited( |
| 8610 | "%s cannot read MSR (%u, 0x%x)\n", |
| 8611 | __func__, i, e.index); |
| 8612 | return -EINVAL; |
| 8613 | } |
| 8614 | if (kvm_write_guest(vcpu->kvm, |
| 8615 | gpa + i * sizeof(e) + |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8616 | offsetof(struct vmx_msr_entry, value), |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 8617 | &e.value, sizeof(e.value))) { |
| 8618 | pr_warn_ratelimited( |
| 8619 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 8620 | __func__, i, e.index, e.value); |
| 8621 | return -EINVAL; |
| 8622 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8623 | } |
| 8624 | return 0; |
| 8625 | } |
| 8626 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8627 | /* |
| 8628 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 8629 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
Tiejun Chen | b461966 | 2014-09-22 10:31:38 +0800 | [diff] [blame] | 8630 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8631 | * guest in a way that will both be appropriate to L1's requests, and our |
| 8632 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 8633 | * function also has additional necessary side-effects, like setting various |
| 8634 | * vcpu->arch fields. |
| 8635 | */ |
| 8636 | static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 8637 | { |
| 8638 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8639 | u32 exec_control; |
| 8640 | |
| 8641 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 8642 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 8643 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 8644 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 8645 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 8646 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 8647 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 8648 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 8649 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 8650 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 8651 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 8652 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 8653 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 8654 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 8655 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 8656 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 8657 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 8658 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 8659 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 8660 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 8661 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 8662 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 8663 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 8664 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 8665 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 8666 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 8667 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 8668 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 8669 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 8670 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 8671 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 8672 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 8673 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 8674 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 8675 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 8676 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
| 8677 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 8678 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 8679 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 8680 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 8681 | } else { |
| 8682 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 8683 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 8684 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8685 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 8686 | vmcs12->vm_entry_intr_info_field); |
| 8687 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 8688 | vmcs12->vm_entry_exception_error_code); |
| 8689 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 8690 | vmcs12->vm_entry_instruction_len); |
| 8691 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 8692 | vmcs12->guest_interruptibility_info); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8693 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
Gleb Natapov | 63fbf59 | 2013-07-28 18:31:06 +0300 | [diff] [blame] | 8694 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8695 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 8696 | vmcs12->guest_pending_dbg_exceptions); |
| 8697 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 8698 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 8699 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 8700 | if (nested_cpu_has_xsaves(vmcs12)) |
| 8701 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8702 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 8703 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 8704 | exec_control = vmcs12->pin_based_vm_exec_control; |
| 8705 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 8706 | exec_control &= ~(PIN_BASED_VMX_PREEMPTION_TIMER | |
| 8707 | PIN_BASED_POSTED_INTR); |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 8708 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8709 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 8710 | vmx->nested.preemption_timer_expired = false; |
| 8711 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 8712 | vmx_start_preemption_timer(vcpu); |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 8713 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8714 | /* |
| 8715 | * Whether page-faults are trapped is determined by a combination of |
| 8716 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 8717 | * If enable_ept, L0 doesn't care about page faults and we should |
| 8718 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 8719 | * care about (at least some) page faults, and because it is not easy |
| 8720 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 8721 | * to exit on each and every L2 page fault. This is done by setting |
| 8722 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 8723 | * Note that below we don't need special code to set EB.PF beyond the |
| 8724 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 8725 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 8726 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 8727 | * |
| 8728 | * A problem with this approach (when !enable_ept) is that L1 may be |
| 8729 | * injected with more page faults than it asked for. This could have |
| 8730 | * caused problems, but in practice existing hypervisors don't care. |
| 8731 | * To fix this, we will need to emulate the PFEC checking (on the L1 |
| 8732 | * page tables), using walk_addr(), when injecting PFs to L1. |
| 8733 | */ |
| 8734 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 8735 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 8736 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 8737 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 8738 | |
| 8739 | if (cpu_has_secondary_exec_ctrls()) { |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 8740 | exec_control = vmx_secondary_exec_control(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8741 | if (!vmx->rdtscp_enabled) |
| 8742 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 8743 | /* Take the following fields only from vmcs12 */ |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 8744 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 8745 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
| 8746 | SECONDARY_EXEC_APIC_REGISTER_VIRT); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8747 | if (nested_cpu_has(vmcs12, |
| 8748 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 8749 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 8750 | |
| 8751 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) { |
| 8752 | /* |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8753 | * If translation failed, no matter: This feature asks |
| 8754 | * to exit when accessing the given address, and if it |
| 8755 | * can never be accessed, this feature won't do |
| 8756 | * anything anyway. |
| 8757 | */ |
| 8758 | if (!vmx->nested.apic_access_page) |
| 8759 | exec_control &= |
| 8760 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 8761 | else |
| 8762 | vmcs_write64(APIC_ACCESS_ADDR, |
| 8763 | page_to_phys(vmx->nested.apic_access_page)); |
Jan Kiszka | ca3f257 | 2013-12-16 12:55:46 +0100 | [diff] [blame] | 8764 | } else if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) { |
| 8765 | exec_control |= |
| 8766 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8767 | kvm_vcpu_reload_apic_access_page(vcpu); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8768 | } |
| 8769 | |
| 8770 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 8771 | } |
| 8772 | |
| 8773 | |
| 8774 | /* |
| 8775 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 8776 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 8777 | * Other fields are different per CPU, and will be set later when |
| 8778 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 8779 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8780 | vmx_set_constant_host_state(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8781 | |
| 8782 | /* |
| 8783 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 8784 | * entry, but only if the current (host) sp changed from the value |
| 8785 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 8786 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 8787 | * here we just force the write to happen on entry. |
| 8788 | */ |
| 8789 | vmx->host_rsp = 0; |
| 8790 | |
| 8791 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 8792 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 8793 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 8794 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 8795 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8796 | |
| 8797 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
| 8798 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
| 8799 | page_to_phys(vmx->nested.virtual_apic_page)); |
| 8800 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
| 8801 | } |
| 8802 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8803 | /* |
| 8804 | * Merging of IO and MSR bitmaps not currently supported. |
| 8805 | * Rather, exit every time. |
| 8806 | */ |
| 8807 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 8808 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 8809 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 8810 | |
| 8811 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 8812 | |
| 8813 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 8814 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 8815 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 8816 | */ |
| 8817 | update_exception_bitmap(vcpu); |
| 8818 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 8819 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 8820 | |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 8821 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 8822 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 8823 | * bits are further modified by vmx_set_efer() below. |
| 8824 | */ |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 8825 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 8826 | |
| 8827 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are |
| 8828 | * emulated by vmx_set_efer(), below. |
| 8829 | */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 8830 | vm_entry_controls_init(vmx, |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 8831 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
| 8832 | ~VM_ENTRY_IA32E_MODE) | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8833 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 8834 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 8835 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8836 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 8837 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
| 8838 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8839 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 8840 | |
| 8841 | |
| 8842 | set_cr4_guest_host_mask(vmx); |
| 8843 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 8844 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) |
| 8845 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 8846 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 8847 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 8848 | vmcs_write64(TSC_OFFSET, |
| 8849 | vmx->nested.vmcs01_tsc_offset + vmcs12->tsc_offset); |
| 8850 | else |
| 8851 | vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8852 | |
| 8853 | if (enable_vpid) { |
| 8854 | /* |
| 8855 | * Trivially support vpid by letting L2s share their parent |
| 8856 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 8857 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 8858 | */ |
| 8859 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 8860 | vmx_flush_tlb(vcpu); |
| 8861 | } |
| 8862 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 8863 | if (nested_cpu_has_ept(vmcs12)) { |
| 8864 | kvm_mmu_unload(vcpu); |
| 8865 | nested_ept_init_mmu_context(vcpu); |
| 8866 | } |
| 8867 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8868 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) |
| 8869 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 8870 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8871 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 8872 | else |
| 8873 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 8874 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 8875 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 8876 | |
| 8877 | /* |
| 8878 | * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified |
| 8879 | * TS bit (for lazy fpu) and bits which we consider mandatory enabled. |
| 8880 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 8881 | * the specifications by L1; It's not enough to take |
| 8882 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 8883 | * have more bits than L1 expected. |
| 8884 | */ |
| 8885 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 8886 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 8887 | |
| 8888 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 8889 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 8890 | |
| 8891 | /* shadow page tables on either EPT or shadow page tables */ |
| 8892 | kvm_set_cr3(vcpu, vmcs12->guest_cr3); |
| 8893 | kvm_mmu_reset_context(vcpu); |
| 8894 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 8895 | if (!enable_ept) |
| 8896 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 8897 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 8898 | /* |
| 8899 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 |
| 8900 | */ |
| 8901 | if (enable_ept) { |
| 8902 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 8903 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 8904 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 8905 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 8906 | } |
| 8907 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8908 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 8909 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
| 8910 | } |
| 8911 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8912 | /* |
| 8913 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 8914 | * for running an L2 nested guest. |
| 8915 | */ |
| 8916 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 8917 | { |
| 8918 | struct vmcs12 *vmcs12; |
| 8919 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8920 | int cpu; |
| 8921 | struct loaded_vmcs *vmcs02; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 8922 | bool ia32e; |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8923 | u32 msr_entry_idx; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8924 | |
| 8925 | if (!nested_vmx_check_permission(vcpu) || |
| 8926 | !nested_vmx_check_vmcs12(vcpu)) |
| 8927 | return 1; |
| 8928 | |
| 8929 | skip_emulated_instruction(vcpu); |
| 8930 | vmcs12 = get_vmcs12(vcpu); |
| 8931 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8932 | if (enable_shadow_vmcs) |
| 8933 | copy_shadow_to_vmcs12(vmx); |
| 8934 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 8935 | /* |
| 8936 | * The nested entry process starts with enforcing various prerequisites |
| 8937 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 8938 | * they fail: As the SDM explains, some conditions should cause the |
| 8939 | * instruction to fail, while others will cause the instruction to seem |
| 8940 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 8941 | * To speed up the normal (success) code path, we should avoid checking |
| 8942 | * for misconfigurations which will anyway be caught by the processor |
| 8943 | * when using the merged vmcs02. |
| 8944 | */ |
| 8945 | if (vmcs12->launch_state == launch) { |
| 8946 | nested_vmx_failValid(vcpu, |
| 8947 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 8948 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 8949 | return 1; |
| 8950 | } |
| 8951 | |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 8952 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 8953 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) { |
Paolo Bonzini | 26539bd | 2013-04-15 15:00:27 +0200 | [diff] [blame] | 8954 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 8955 | return 1; |
| 8956 | } |
| 8957 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 8958 | if ((vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_MSR_BITMAPS) && |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 8959 | !PAGE_ALIGNED(vmcs12->msr_bitmap)) { |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 8960 | /*TODO: Also verify bits beyond physical address width are 0*/ |
| 8961 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 8962 | return 1; |
| 8963 | } |
| 8964 | |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 8965 | if (!nested_get_vmcs12_pages(vcpu, vmcs12)) { |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 8966 | /*TODO: Also verify bits beyond physical address width are 0*/ |
| 8967 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 8968 | return 1; |
| 8969 | } |
| 8970 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 8971 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) { |
| 8972 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 8973 | return 1; |
| 8974 | } |
| 8975 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 8976 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 8977 | nested_vmx_true_procbased_ctls_low, |
| 8978 | nested_vmx_procbased_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 8979 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
| 8980 | nested_vmx_secondary_ctls_low, nested_vmx_secondary_ctls_high) || |
| 8981 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
| 8982 | nested_vmx_pinbased_ctls_low, nested_vmx_pinbased_ctls_high) || |
| 8983 | !vmx_control_verify(vmcs12->vm_exit_controls, |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 8984 | nested_vmx_true_exit_ctls_low, |
| 8985 | nested_vmx_exit_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 8986 | !vmx_control_verify(vmcs12->vm_entry_controls, |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 8987 | nested_vmx_true_entry_ctls_low, |
| 8988 | nested_vmx_entry_ctls_high)) |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 8989 | { |
| 8990 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 8991 | return 1; |
| 8992 | } |
| 8993 | |
| 8994 | if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) || |
| 8995 | ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 8996 | nested_vmx_failValid(vcpu, |
| 8997 | VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
| 8998 | return 1; |
| 8999 | } |
| 9000 | |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 9001 | if (!nested_cr0_valid(vmcs12, vmcs12->guest_cr0) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 9002 | ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 9003 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 9004 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 9005 | return 1; |
| 9006 | } |
| 9007 | if (vmcs12->vmcs_link_pointer != -1ull) { |
| 9008 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 9009 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR); |
| 9010 | return 1; |
| 9011 | } |
| 9012 | |
| 9013 | /* |
Jan Kiszka | cb0c8cda | 2013-04-27 12:58:00 +0200 | [diff] [blame] | 9014 | * If the load IA32_EFER VM-entry control is 1, the following checks |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 9015 | * are performed on the field for the IA32_EFER MSR: |
| 9016 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 9017 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 9018 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 9019 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 9020 | * CR0.PG) is 1. |
| 9021 | */ |
| 9022 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) { |
| 9023 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 9024 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 9025 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 9026 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 9027 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) { |
| 9028 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 9029 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 9030 | return 1; |
| 9031 | } |
| 9032 | } |
| 9033 | |
| 9034 | /* |
| 9035 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 9036 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 9037 | * the values of the LMA and LME bits in the field must each be that of |
| 9038 | * the host address-space size VM-exit control. |
| 9039 | */ |
| 9040 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 9041 | ia32e = (vmcs12->vm_exit_controls & |
| 9042 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 9043 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 9044 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 9045 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) { |
| 9046 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 9047 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 9048 | return 1; |
| 9049 | } |
| 9050 | } |
| 9051 | |
| 9052 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 9053 | * We're finally done with prerequisite checking, and can start with |
| 9054 | * the nested entry. |
| 9055 | */ |
| 9056 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 9057 | vmcs02 = nested_get_current_vmcs02(vmx); |
| 9058 | if (!vmcs02) |
| 9059 | return -ENOMEM; |
| 9060 | |
| 9061 | enter_guest_mode(vcpu); |
| 9062 | |
| 9063 | vmx->nested.vmcs01_tsc_offset = vmcs_read64(TSC_OFFSET); |
| 9064 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 9065 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 9066 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 9067 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 9068 | cpu = get_cpu(); |
| 9069 | vmx->loaded_vmcs = vmcs02; |
| 9070 | vmx_vcpu_put(vcpu); |
| 9071 | vmx_vcpu_load(vcpu, cpu); |
| 9072 | vcpu->cpu = cpu; |
| 9073 | put_cpu(); |
| 9074 | |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 9075 | vmx_segment_cache_clear(vmx); |
| 9076 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 9077 | prepare_vmcs02(vcpu, vmcs12); |
| 9078 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9079 | msr_entry_idx = nested_vmx_load_msr(vcpu, |
| 9080 | vmcs12->vm_entry_msr_load_addr, |
| 9081 | vmcs12->vm_entry_msr_load_count); |
| 9082 | if (msr_entry_idx) { |
| 9083 | leave_guest_mode(vcpu); |
| 9084 | vmx_load_vmcs01(vcpu); |
| 9085 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 9086 | EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx); |
| 9087 | return 1; |
| 9088 | } |
| 9089 | |
| 9090 | vmcs12->launch_state = 1; |
| 9091 | |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 9092 | if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) |
| 9093 | return kvm_emulate_halt(vcpu); |
| 9094 | |
Jan Kiszka | 7af40ad3 | 2014-01-04 18:47:23 +0100 | [diff] [blame] | 9095 | vmx->nested.nested_run_pending = 1; |
| 9096 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 9097 | /* |
| 9098 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 9099 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 9100 | * returned as far as L1 is concerned. It will only return (and set |
| 9101 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 9102 | */ |
| 9103 | return 1; |
| 9104 | } |
| 9105 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9106 | /* |
| 9107 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 9108 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 9109 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 9110 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 9111 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 9112 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 9113 | * didn't trap the bit, because if L1 did, so would L0). |
| 9114 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 9115 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 9116 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 9117 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 9118 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 9119 | * changed these bits, and therefore they need to be updated, but L0 |
| 9120 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 9121 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 9122 | */ |
| 9123 | static inline unsigned long |
| 9124 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 9125 | { |
| 9126 | return |
| 9127 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 9128 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 9129 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 9130 | vcpu->arch.cr0_guest_owned_bits)); |
| 9131 | } |
| 9132 | |
| 9133 | static inline unsigned long |
| 9134 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 9135 | { |
| 9136 | return |
| 9137 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 9138 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 9139 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 9140 | vcpu->arch.cr4_guest_owned_bits)); |
| 9141 | } |
| 9142 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 9143 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 9144 | struct vmcs12 *vmcs12) |
| 9145 | { |
| 9146 | u32 idt_vectoring; |
| 9147 | unsigned int nr; |
| 9148 | |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 9149 | if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 9150 | nr = vcpu->arch.exception.nr; |
| 9151 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 9152 | |
| 9153 | if (kvm_exception_is_soft(nr)) { |
| 9154 | vmcs12->vm_exit_instruction_len = |
| 9155 | vcpu->arch.event_exit_inst_len; |
| 9156 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 9157 | } else |
| 9158 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 9159 | |
| 9160 | if (vcpu->arch.exception.has_error_code) { |
| 9161 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 9162 | vmcs12->idt_vectoring_error_code = |
| 9163 | vcpu->arch.exception.error_code; |
| 9164 | } |
| 9165 | |
| 9166 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
Jan Kiszka | cd2633c | 2013-10-23 17:42:15 +0100 | [diff] [blame] | 9167 | } else if (vcpu->arch.nmi_injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 9168 | vmcs12->idt_vectoring_info_field = |
| 9169 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 9170 | } else if (vcpu->arch.interrupt.pending) { |
| 9171 | nr = vcpu->arch.interrupt.nr; |
| 9172 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 9173 | |
| 9174 | if (vcpu->arch.interrupt.soft) { |
| 9175 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 9176 | vmcs12->vm_entry_instruction_len = |
| 9177 | vcpu->arch.event_exit_inst_len; |
| 9178 | } else |
| 9179 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 9180 | |
| 9181 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 9182 | } |
| 9183 | } |
| 9184 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 9185 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 9186 | { |
| 9187 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9188 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9189 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 9190 | vmx->nested.preemption_timer_expired) { |
| 9191 | if (vmx->nested.nested_run_pending) |
| 9192 | return -EBUSY; |
| 9193 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 9194 | return 0; |
| 9195 | } |
| 9196 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 9197 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
Jan Kiszka | 220c567 | 2014-03-07 20:03:14 +0100 | [diff] [blame] | 9198 | if (vmx->nested.nested_run_pending || |
| 9199 | vcpu->arch.interrupt.pending) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 9200 | return -EBUSY; |
| 9201 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 9202 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 9203 | INTR_INFO_VALID_MASK, 0); |
| 9204 | /* |
| 9205 | * The NMI-triggered VM exit counts as injection: |
| 9206 | * clear this one and block further NMIs. |
| 9207 | */ |
| 9208 | vcpu->arch.nmi_pending = 0; |
| 9209 | vmx_set_nmi_mask(vcpu, true); |
| 9210 | return 0; |
| 9211 | } |
| 9212 | |
| 9213 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 9214 | nested_exit_on_intr(vcpu)) { |
| 9215 | if (vmx->nested.nested_run_pending) |
| 9216 | return -EBUSY; |
| 9217 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
| 9218 | } |
| 9219 | |
| 9220 | return 0; |
| 9221 | } |
| 9222 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9223 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 9224 | { |
| 9225 | ktime_t remaining = |
| 9226 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 9227 | u64 value; |
| 9228 | |
| 9229 | if (ktime_to_ns(remaining) <= 0) |
| 9230 | return 0; |
| 9231 | |
| 9232 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 9233 | do_div(value, 1000000); |
| 9234 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 9235 | } |
| 9236 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9237 | /* |
| 9238 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 9239 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 9240 | * and this function updates it to reflect the changes to the guest state while |
| 9241 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 9242 | * without going back to L1), and to reflect the exit reason. |
| 9243 | * Note that we do not have to copy here all VMCS fields, just those that |
| 9244 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 9245 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 9246 | * which already writes to vmcs12 directly. |
| 9247 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9248 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 9249 | u32 exit_reason, u32 exit_intr_info, |
| 9250 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9251 | { |
| 9252 | /* update guest state fields: */ |
| 9253 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 9254 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 9255 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9256 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 9257 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 9258 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 9259 | |
| 9260 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 9261 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 9262 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 9263 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 9264 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 9265 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 9266 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 9267 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 9268 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 9269 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 9270 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 9271 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 9272 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 9273 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 9274 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 9275 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 9276 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 9277 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 9278 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 9279 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 9280 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 9281 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 9282 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 9283 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 9284 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 9285 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 9286 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 9287 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 9288 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 9289 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 9290 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 9291 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 9292 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 9293 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 9294 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 9295 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 9296 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9297 | vmcs12->guest_interruptibility_info = |
| 9298 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 9299 | vmcs12->guest_pending_dbg_exceptions = |
| 9300 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
Jan Kiszka | 3edf1e6 | 2014-01-04 18:47:24 +0100 | [diff] [blame] | 9301 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 9302 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 9303 | else |
| 9304 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9305 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9306 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 9307 | if (vmcs12->vm_exit_controls & |
| 9308 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 9309 | vmcs12->vmx_preemption_timer_value = |
| 9310 | vmx_get_preemption_timer_value(vcpu); |
| 9311 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 9312 | } |
Arthur Chunqi Li | 7854cbc | 2013-09-16 16:11:44 +0800 | [diff] [blame] | 9313 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 9314 | /* |
| 9315 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 9316 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 9317 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 9318 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 9319 | * |
| 9320 | * Additionally, restore L2's PDPTR to vmcs12. |
| 9321 | */ |
| 9322 | if (enable_ept) { |
| 9323 | vmcs12->guest_cr3 = vmcs_read64(GUEST_CR3); |
| 9324 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 9325 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 9326 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 9327 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 9328 | } |
| 9329 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 9330 | vmcs12->vm_entry_controls = |
| 9331 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 9332 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 9333 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 9334 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 9335 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 9336 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 9337 | } |
| 9338 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9339 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 9340 | * the relevant bit asks not to trap the change */ |
Jan Kiszka | b8c07d5 | 2013-04-06 13:51:21 +0200 | [diff] [blame] | 9341 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9342 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
Jan Kiszka | 10ba54a | 2013-08-08 16:26:31 +0200 | [diff] [blame] | 9343 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 9344 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9345 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 9346 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 9347 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 9348 | if (vmx_mpx_supported()) |
| 9349 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 9350 | if (nested_cpu_has_xsaves(vmcs12)) |
| 9351 | vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9352 | |
| 9353 | /* update exit information fields: */ |
| 9354 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9355 | vmcs12->vm_exit_reason = exit_reason; |
| 9356 | vmcs12->exit_qualification = exit_qualification; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9357 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9358 | vmcs12->vm_exit_intr_info = exit_intr_info; |
Jan Kiszka | c0d1c77 | 2013-04-14 12:12:50 +0200 | [diff] [blame] | 9359 | if ((vmcs12->vm_exit_intr_info & |
| 9360 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == |
| 9361 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) |
| 9362 | vmcs12->vm_exit_intr_error_code = |
| 9363 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 9364 | vmcs12->idt_vectoring_info_field = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9365 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 9366 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 9367 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 9368 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
| 9369 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 9370 | * instead of reading the real value. */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9371 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 9372 | |
| 9373 | /* |
| 9374 | * Transfer the event that L0 or L1 may wanted to inject into |
| 9375 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 9376 | */ |
| 9377 | vmcs12_save_pending_event(vcpu, vmcs12); |
| 9378 | } |
| 9379 | |
| 9380 | /* |
| 9381 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 9382 | * preserved above and would only end up incorrectly in L1. |
| 9383 | */ |
| 9384 | vcpu->arch.nmi_injected = false; |
| 9385 | kvm_clear_exception_queue(vcpu); |
| 9386 | kvm_clear_interrupt_queue(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9387 | } |
| 9388 | |
| 9389 | /* |
| 9390 | * A part of what we need to when the nested L2 guest exits and we want to |
| 9391 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 9392 | * in vmcs12. |
| 9393 | * This function is to be called not only on normal nested exit, but also on |
| 9394 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 9395 | * Failures During or After Loading Guest State"). |
| 9396 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 9397 | */ |
Jan Kiszka | 733568f | 2013-02-23 15:07:47 +0100 | [diff] [blame] | 9398 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 9399 | struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9400 | { |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 9401 | struct kvm_segment seg; |
| 9402 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9403 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 9404 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 9405 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9406 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 9407 | else |
| 9408 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 9409 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 9410 | |
| 9411 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 9412 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
H. Peter Anvin | 1adfa76 | 2013-04-27 16:10:11 -0700 | [diff] [blame] | 9413 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9414 | /* |
| 9415 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 9416 | * actually changed, because it depends on the current state of |
| 9417 | * fpu_active (which may have changed). |
| 9418 | * Note that vmx_set_cr0 refers to efer set above. |
| 9419 | */ |
Jan Kiszka | 9e3e4db | 2013-09-03 21:11:45 +0200 | [diff] [blame] | 9420 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9421 | /* |
| 9422 | * If we did fpu_activate()/fpu_deactivate() during L2's run, we need |
| 9423 | * to apply the same changes to L1's vmcs. We just set cr0 correctly, |
| 9424 | * but we also need to update cr0_guest_host_mask and exception_bitmap. |
| 9425 | */ |
| 9426 | update_exception_bitmap(vcpu); |
| 9427 | vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0); |
| 9428 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 9429 | |
| 9430 | /* |
| 9431 | * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01 |
| 9432 | * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask(); |
| 9433 | */ |
| 9434 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 9435 | kvm_set_cr4(vcpu, vmcs12->host_cr4); |
| 9436 | |
Jan Kiszka | 29bf08f | 2013-12-28 16:31:52 +0100 | [diff] [blame] | 9437 | nested_ept_uninit_mmu_context(vcpu); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9438 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9439 | kvm_set_cr3(vcpu, vmcs12->host_cr3); |
| 9440 | kvm_mmu_reset_context(vcpu); |
| 9441 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 9442 | if (!enable_ept) |
| 9443 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
| 9444 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9445 | if (enable_vpid) { |
| 9446 | /* |
| 9447 | * Trivially support vpid by letting L2s share their parent |
| 9448 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 9449 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 9450 | */ |
| 9451 | vmx_flush_tlb(vcpu); |
| 9452 | } |
| 9453 | |
| 9454 | |
| 9455 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 9456 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 9457 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 9458 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 9459 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9460 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 9461 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 9462 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 9463 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 9464 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 9465 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9466 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 9467 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 9468 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9469 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 9470 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 9471 | vmcs12->host_ia32_perf_global_ctrl); |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 9472 | |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 9473 | /* Set L1 segment info according to Intel SDM |
| 9474 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 9475 | seg = (struct kvm_segment) { |
| 9476 | .base = 0, |
| 9477 | .limit = 0xFFFFFFFF, |
| 9478 | .selector = vmcs12->host_cs_selector, |
| 9479 | .type = 11, |
| 9480 | .present = 1, |
| 9481 | .s = 1, |
| 9482 | .g = 1 |
| 9483 | }; |
| 9484 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 9485 | seg.l = 1; |
| 9486 | else |
| 9487 | seg.db = 1; |
| 9488 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 9489 | seg = (struct kvm_segment) { |
| 9490 | .base = 0, |
| 9491 | .limit = 0xFFFFFFFF, |
| 9492 | .type = 3, |
| 9493 | .present = 1, |
| 9494 | .s = 1, |
| 9495 | .db = 1, |
| 9496 | .g = 1 |
| 9497 | }; |
| 9498 | seg.selector = vmcs12->host_ds_selector; |
| 9499 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 9500 | seg.selector = vmcs12->host_es_selector; |
| 9501 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 9502 | seg.selector = vmcs12->host_ss_selector; |
| 9503 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 9504 | seg.selector = vmcs12->host_fs_selector; |
| 9505 | seg.base = vmcs12->host_fs_base; |
| 9506 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 9507 | seg.selector = vmcs12->host_gs_selector; |
| 9508 | seg.base = vmcs12->host_gs_base; |
| 9509 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 9510 | seg = (struct kvm_segment) { |
Gleb Natapov | 205befd | 2013-08-04 15:08:06 +0300 | [diff] [blame] | 9511 | .base = vmcs12->host_tr_base, |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 9512 | .limit = 0x67, |
| 9513 | .selector = vmcs12->host_tr_selector, |
| 9514 | .type = 11, |
| 9515 | .present = 1 |
| 9516 | }; |
| 9517 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 9518 | |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 9519 | kvm_set_dr(vcpu, 7, 0x400); |
| 9520 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9521 | |
| 9522 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 9523 | vmcs12->vm_exit_msr_load_count)) |
| 9524 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9525 | } |
| 9526 | |
| 9527 | /* |
| 9528 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 9529 | * and modify vmcs12 to make it see what it would expect to see there if |
| 9530 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 9531 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9532 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 9533 | u32 exit_intr_info, |
| 9534 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9535 | { |
| 9536 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9537 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9538 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 9539 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 9540 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 9541 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9542 | leave_guest_mode(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9543 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 9544 | exit_qualification); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9545 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9546 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
| 9547 | vmcs12->vm_exit_msr_store_count)) |
| 9548 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
| 9549 | |
Wanpeng Li | f3380ca | 2014-08-05 12:42:23 +0800 | [diff] [blame] | 9550 | vmx_load_vmcs01(vcpu); |
| 9551 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 9552 | if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT) |
| 9553 | && nested_exit_intr_ack_set(vcpu)) { |
| 9554 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 9555 | WARN_ON(irq < 0); |
| 9556 | vmcs12->vm_exit_intr_info = irq | |
| 9557 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 9558 | } |
| 9559 | |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 9560 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 9561 | vmcs12->exit_qualification, |
| 9562 | vmcs12->idt_vectoring_info_field, |
| 9563 | vmcs12->vm_exit_intr_info, |
| 9564 | vmcs12->vm_exit_intr_error_code, |
| 9565 | KVM_ISA_VMX); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9566 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 9567 | vm_entry_controls_init(vmx, vmcs_read32(VM_ENTRY_CONTROLS)); |
| 9568 | vm_exit_controls_init(vmx, vmcs_read32(VM_EXIT_CONTROLS)); |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 9569 | vmx_segment_cache_clear(vmx); |
| 9570 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9571 | /* if no vmcs02 cache requested, remove the one we used */ |
| 9572 | if (VMCS02_POOL_SIZE == 0) |
| 9573 | nested_free_vmcs02(vmx, vmx->nested.current_vmptr); |
| 9574 | |
| 9575 | load_vmcs12_host_state(vcpu, vmcs12); |
| 9576 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 9577 | /* Update TSC_OFFSET if TSC was changed while L2 ran */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9578 | vmcs_write64(TSC_OFFSET, vmx->nested.vmcs01_tsc_offset); |
| 9579 | |
| 9580 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 9581 | vmx->host_rsp = 0; |
| 9582 | |
| 9583 | /* Unpin physical memory we referred to in vmcs02 */ |
| 9584 | if (vmx->nested.apic_access_page) { |
| 9585 | nested_release_page(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 9586 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9587 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9588 | if (vmx->nested.virtual_apic_page) { |
| 9589 | nested_release_page(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 9590 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9591 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9592 | |
| 9593 | /* |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9594 | * We are now running in L2, mmu_notifier will force to reload the |
| 9595 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 9596 | */ |
| 9597 | kvm_vcpu_reload_apic_access_page(vcpu); |
| 9598 | |
| 9599 | /* |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9600 | * Exiting from L2 to L1, we're now back to L1 which thinks it just |
| 9601 | * finished a VMLAUNCH or VMRESUME instruction, so we need to set the |
| 9602 | * success or failure flag accordingly. |
| 9603 | */ |
| 9604 | if (unlikely(vmx->fail)) { |
| 9605 | vmx->fail = 0; |
| 9606 | nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 9607 | } else |
| 9608 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 9609 | if (enable_shadow_vmcs) |
| 9610 | vmx->nested.sync_shadow_vmcs = true; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 9611 | |
| 9612 | /* in case we halted in L2 */ |
| 9613 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 9614 | } |
| 9615 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 9616 | /* |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 9617 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 9618 | */ |
| 9619 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 9620 | { |
| 9621 | if (is_guest_mode(vcpu)) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9622 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 9623 | free_nested(to_vmx(vcpu)); |
| 9624 | } |
| 9625 | |
| 9626 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 9627 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 9628 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 9629 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 9630 | * It should only be called before L2 actually succeeded to run, and when |
| 9631 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 9632 | */ |
| 9633 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 9634 | struct vmcs12 *vmcs12, |
| 9635 | u32 reason, unsigned long qualification) |
| 9636 | { |
| 9637 | load_vmcs12_host_state(vcpu, vmcs12); |
| 9638 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 9639 | vmcs12->exit_qualification = qualification; |
| 9640 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 9641 | if (enable_shadow_vmcs) |
| 9642 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 9643 | } |
| 9644 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 9645 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 9646 | struct x86_instruction_info *info, |
| 9647 | enum x86_intercept_stage stage) |
| 9648 | { |
| 9649 | return X86EMUL_CONTINUE; |
| 9650 | } |
| 9651 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 9652 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 9653 | { |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 9654 | if (ple_gap) |
| 9655 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 9656 | } |
| 9657 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 9658 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 9659 | struct kvm_memory_slot *slot) |
| 9660 | { |
| 9661 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 9662 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 9663 | } |
| 9664 | |
| 9665 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 9666 | struct kvm_memory_slot *slot) |
| 9667 | { |
| 9668 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 9669 | } |
| 9670 | |
| 9671 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 9672 | { |
| 9673 | kvm_flush_pml_buffers(kvm); |
| 9674 | } |
| 9675 | |
| 9676 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 9677 | struct kvm_memory_slot *memslot, |
| 9678 | gfn_t offset, unsigned long mask) |
| 9679 | { |
| 9680 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 9681 | } |
| 9682 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 9683 | static struct kvm_x86_ops vmx_x86_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9684 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 9685 | .disabled_by_bios = vmx_disabled_by_bios, |
| 9686 | .hardware_setup = hardware_setup, |
| 9687 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 9688 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9689 | .hardware_enable = hardware_enable, |
| 9690 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 9691 | .cpu_has_accelerated_tpr = report_flexpriority, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9692 | |
| 9693 | .vcpu_create = vmx_create_vcpu, |
| 9694 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 9695 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9696 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 9697 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9698 | .vcpu_load = vmx_vcpu_load, |
| 9699 | .vcpu_put = vmx_vcpu_put, |
| 9700 | |
Jan Kiszka | c863901 | 2012-09-21 05:42:55 +0200 | [diff] [blame] | 9701 | .update_db_bp_intercept = update_exception_bitmap, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9702 | .get_msr = vmx_get_msr, |
| 9703 | .set_msr = vmx_set_msr, |
| 9704 | .get_segment_base = vmx_get_segment_base, |
| 9705 | .get_segment = vmx_get_segment, |
| 9706 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 9707 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9708 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 9709 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 9710 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 9711 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9712 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9713 | .set_cr3 = vmx_set_cr3, |
| 9714 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9715 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9716 | .get_idt = vmx_get_idt, |
| 9717 | .set_idt = vmx_set_idt, |
| 9718 | .get_gdt = vmx_get_gdt, |
| 9719 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 9720 | .get_dr6 = vmx_get_dr6, |
| 9721 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 9722 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 9723 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 9724 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9725 | .get_rflags = vmx_get_rflags, |
| 9726 | .set_rflags = vmx_set_rflags, |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 9727 | .fpu_deactivate = vmx_fpu_deactivate, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9728 | |
| 9729 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9730 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9731 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 9732 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9733 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 9734 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 9735 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 9736 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 9737 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9738 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 9739 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 9740 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 9741 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9742 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 9743 | .get_nmi_mask = vmx_get_nmi_mask, |
| 9744 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9745 | .enable_nmi_window = enable_nmi_window, |
| 9746 | .enable_irq_window = enable_irq_window, |
| 9747 | .update_cr8_intercept = update_cr8_intercept, |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9748 | .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9749 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9750 | .vm_has_apicv = vmx_vm_has_apicv, |
| 9751 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
| 9752 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 9753 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 9754 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 9755 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Marcelo Tosatti | 7c6a98d | 2014-12-16 09:08:14 -0500 | [diff] [blame] | 9756 | .test_posted_interrupt = vmx_test_pir, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9757 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 9758 | .set_tss_addr = vmx_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 9759 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 9760 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 9761 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 9762 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 9763 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 9764 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 9765 | |
| 9766 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9767 | |
| 9768 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9769 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 9770 | |
| 9771 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 9772 | |
| 9773 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 9774 | |
Joerg Roedel | 4051b18 | 2011-03-25 09:44:49 +0100 | [diff] [blame] | 9775 | .set_tsc_khz = vmx_set_tsc_khz, |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 9776 | .read_tsc_offset = vmx_read_tsc_offset, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 9777 | .write_tsc_offset = vmx_write_tsc_offset, |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 9778 | .adjust_tsc_offset = vmx_adjust_tsc_offset, |
Joerg Roedel | 857e409 | 2011-03-25 09:44:50 +0100 | [diff] [blame] | 9779 | .compute_tsc_offset = vmx_compute_tsc_offset, |
Nadav Har'El | d5c1785 | 2011-08-02 15:54:20 +0300 | [diff] [blame] | 9780 | .read_l1_tsc = vmx_read_l1_tsc, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 9781 | |
| 9782 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 9783 | |
| 9784 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9785 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 9786 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 9787 | .xsaves_supported = vmx_xsaves_supported, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 9788 | |
| 9789 | .check_nested_events = vmx_check_nested_events, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 9790 | |
| 9791 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame^] | 9792 | |
| 9793 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 9794 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 9795 | .flush_log_dirty = vmx_flush_log_dirty, |
| 9796 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9797 | }; |
| 9798 | |
| 9799 | static int __init vmx_init(void) |
| 9800 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 9801 | int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
| 9802 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 9803 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 9804 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 9805 | |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 9806 | #ifdef CONFIG_KEXEC |
| 9807 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 9808 | crash_vmclear_local_loaded_vmcss); |
| 9809 | #endif |
| 9810 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 9811 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9812 | } |
| 9813 | |
| 9814 | static void __exit vmx_exit(void) |
| 9815 | { |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 9816 | #ifdef CONFIG_KEXEC |
Monam Agarwal | 3b63a43 | 2014-03-22 12:28:10 +0530 | [diff] [blame] | 9817 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 9818 | synchronize_rcu(); |
| 9819 | #endif |
| 9820 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 9821 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9822 | } |
| 9823 | |
| 9824 | module_init(vmx_init) |
| 9825 | module_exit(vmx_exit) |