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" |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 22 | #include "lapic.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 23 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 24 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/module.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 26 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 27 | #include <linux/mm.h> |
| 28 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 29 | #include <linux/sched.h> |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 30 | #include <linux/moduleparam.h> |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 31 | #include <linux/mod_devicetable.h> |
Steven Rostedt (Red Hat) | af658dc | 2015-04-29 14:36:05 -0400 | [diff] [blame] | 32 | #include <linux/trace_events.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 34 | #include <linux/tboot.h> |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 35 | #include <linux/hrtimer.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 36 | #include "kvm_cache_regs.h" |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 37 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 38 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 39 | #include <asm/cpu.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 40 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 41 | #include <asm/desc.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 42 | #include <asm/vmx.h> |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 43 | #include <asm/virtext.h> |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 44 | #include <asm/mce.h> |
Ingo Molnar | 952f07e | 2015-04-26 16:56:05 +0200 | [diff] [blame] | 45 | #include <asm/fpu/internal.h> |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 46 | #include <asm/perf_event.h> |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 47 | #include <asm/debugreg.h> |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 48 | #include <asm/kexec.h> |
Radim Krčmář | dab2087 | 2015-02-09 22:44:07 +0100 | [diff] [blame] | 49 | #include <asm/apic.h> |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 50 | #include <asm/irq_remapping.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 51 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 52 | #include "trace.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 53 | #include "pmu.h" |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 54 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 55 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 56 | #define __ex_clear(x, reg) \ |
| 57 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 58 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 59 | MODULE_AUTHOR("Qumranet"); |
| 60 | MODULE_LICENSE("GPL"); |
| 61 | |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 62 | static const struct x86_cpu_id vmx_cpu_id[] = { |
| 63 | X86_FEATURE_MATCH(X86_FEATURE_VMX), |
| 64 | {} |
| 65 | }; |
| 66 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); |
| 67 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 68 | static bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 69 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 70 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 71 | static bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 72 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 73 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 74 | static bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 75 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 76 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 77 | static bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 78 | module_param_named(unrestricted_guest, |
| 79 | enable_unrestricted_guest, bool, S_IRUGO); |
| 80 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 81 | static bool __read_mostly enable_ept_ad_bits = 1; |
| 82 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 83 | |
Avi Kivity | a27685c | 2012-06-12 20:30:18 +0300 | [diff] [blame] | 84 | static bool __read_mostly emulate_invalid_guest_state = true; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 85 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 86 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 87 | static bool __read_mostly vmm_exclusive = 1; |
Dongxiao Xu | b923e62 | 2010-05-11 18:29:45 +0800 | [diff] [blame] | 88 | module_param(vmm_exclusive, bool, S_IRUGO); |
| 89 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 90 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 91 | module_param(fasteoi, bool, S_IRUGO); |
| 92 | |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 93 | static bool __read_mostly enable_apicv = 1; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 94 | module_param(enable_apicv, bool, S_IRUGO); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 95 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 96 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 97 | 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] | 98 | /* |
| 99 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 100 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 101 | * use VMX instructions. |
| 102 | */ |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 103 | static bool __read_mostly nested = 0; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 104 | module_param(nested, bool, S_IRUGO); |
| 105 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 106 | static u64 __read_mostly host_xss; |
| 107 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 108 | static bool __read_mostly enable_pml = 1; |
| 109 | module_param_named(pml, enable_pml, bool, S_IRUGO); |
| 110 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 111 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
| 112 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 113 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
| 114 | static int __read_mostly cpu_preemption_timer_multi; |
| 115 | static bool __read_mostly enable_preemption_timer = 1; |
| 116 | #ifdef CONFIG_X86_64 |
| 117 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); |
| 118 | #endif |
| 119 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 120 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
| 121 | #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] | 122 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 123 | (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] | 124 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 125 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Andy Lutomirski | 52ce3c2 | 2014-10-07 17:16:21 -0700 | [diff] [blame] | 126 | | X86_CR4_OSXMMEXCPT | X86_CR4_TSD) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 127 | |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 128 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 129 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 130 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 131 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 132 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 133 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 134 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 135 | /* |
| 136 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 137 | * ple_gap: upper bound on the amount of time between two successive |
| 138 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 139 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 140 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 141 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 142 | * less than 2^12 cycles |
| 143 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 144 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 145 | */ |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 146 | #define KVM_VMX_DEFAULT_PLE_GAP 128 |
| 147 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 |
| 148 | #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2 |
| 149 | #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0 |
| 150 | #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \ |
| 151 | INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW |
| 152 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 153 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
| 154 | module_param(ple_gap, int, S_IRUGO); |
| 155 | |
| 156 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 157 | module_param(ple_window, int, S_IRUGO); |
| 158 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 159 | /* Default doubles per-vcpu window every exit. */ |
| 160 | static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW; |
| 161 | module_param(ple_window_grow, int, S_IRUGO); |
| 162 | |
| 163 | /* Default resets per-vcpu window every exit to ple_window. */ |
| 164 | static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK; |
| 165 | module_param(ple_window_shrink, int, S_IRUGO); |
| 166 | |
| 167 | /* Default is to compute the maximum so we can never overflow. */ |
| 168 | static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 169 | static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 170 | module_param(ple_window_max, int, S_IRUGO); |
| 171 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 172 | extern const ulong vmx_return; |
| 173 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 174 | #define NR_AUTOLOAD_MSRS 8 |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 175 | #define VMCS02_POOL_SIZE 1 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 176 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 177 | struct vmcs { |
| 178 | u32 revision_id; |
| 179 | u32 abort; |
| 180 | char data[0]; |
| 181 | }; |
| 182 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 183 | /* |
| 184 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 185 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 186 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 187 | */ |
| 188 | struct loaded_vmcs { |
| 189 | struct vmcs *vmcs; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 190 | struct vmcs *shadow_vmcs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 191 | int cpu; |
| 192 | int launched; |
| 193 | struct list_head loaded_vmcss_on_cpu_link; |
| 194 | }; |
| 195 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 196 | struct shared_msr_entry { |
| 197 | unsigned index; |
| 198 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 199 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 200 | }; |
| 201 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 202 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 203 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 204 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 205 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 206 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 207 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 208 | * More than one of these structures may exist, if L1 runs multiple L2 guests. |
| 209 | * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the |
| 210 | * underlying hardware which will be used to run L2. |
| 211 | * This structure is packed to ensure that its layout is identical across |
| 212 | * machines (necessary for live migration). |
| 213 | * If there are changes in this struct, VMCS12_REVISION must be changed. |
| 214 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 215 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 216 | struct __packed vmcs12 { |
| 217 | /* According to the Intel spec, a VMCS region must start with the |
| 218 | * following two fields. Then follow implementation-specific data. |
| 219 | */ |
| 220 | u32 revision_id; |
| 221 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 222 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 223 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 224 | u32 padding[7]; /* room for future expansion */ |
| 225 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 226 | u64 io_bitmap_a; |
| 227 | u64 io_bitmap_b; |
| 228 | u64 msr_bitmap; |
| 229 | u64 vm_exit_msr_store_addr; |
| 230 | u64 vm_exit_msr_load_addr; |
| 231 | u64 vm_entry_msr_load_addr; |
| 232 | u64 tsc_offset; |
| 233 | u64 virtual_apic_page_addr; |
| 234 | u64 apic_access_addr; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 235 | u64 posted_intr_desc_addr; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 236 | u64 ept_pointer; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 237 | u64 eoi_exit_bitmap0; |
| 238 | u64 eoi_exit_bitmap1; |
| 239 | u64 eoi_exit_bitmap2; |
| 240 | u64 eoi_exit_bitmap3; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 241 | u64 xss_exit_bitmap; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 242 | u64 guest_physical_address; |
| 243 | u64 vmcs_link_pointer; |
| 244 | u64 guest_ia32_debugctl; |
| 245 | u64 guest_ia32_pat; |
| 246 | u64 guest_ia32_efer; |
| 247 | u64 guest_ia32_perf_global_ctrl; |
| 248 | u64 guest_pdptr0; |
| 249 | u64 guest_pdptr1; |
| 250 | u64 guest_pdptr2; |
| 251 | u64 guest_pdptr3; |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 252 | u64 guest_bndcfgs; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 253 | u64 host_ia32_pat; |
| 254 | u64 host_ia32_efer; |
| 255 | u64 host_ia32_perf_global_ctrl; |
| 256 | u64 padding64[8]; /* room for future expansion */ |
| 257 | /* |
| 258 | * To allow migration of L1 (complete with its L2 guests) between |
| 259 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 260 | * unsigned long fields with no explict size. We use u64 (aliased |
| 261 | * natural_width) instead. Luckily, x86 is little-endian. |
| 262 | */ |
| 263 | natural_width cr0_guest_host_mask; |
| 264 | natural_width cr4_guest_host_mask; |
| 265 | natural_width cr0_read_shadow; |
| 266 | natural_width cr4_read_shadow; |
| 267 | natural_width cr3_target_value0; |
| 268 | natural_width cr3_target_value1; |
| 269 | natural_width cr3_target_value2; |
| 270 | natural_width cr3_target_value3; |
| 271 | natural_width exit_qualification; |
| 272 | natural_width guest_linear_address; |
| 273 | natural_width guest_cr0; |
| 274 | natural_width guest_cr3; |
| 275 | natural_width guest_cr4; |
| 276 | natural_width guest_es_base; |
| 277 | natural_width guest_cs_base; |
| 278 | natural_width guest_ss_base; |
| 279 | natural_width guest_ds_base; |
| 280 | natural_width guest_fs_base; |
| 281 | natural_width guest_gs_base; |
| 282 | natural_width guest_ldtr_base; |
| 283 | natural_width guest_tr_base; |
| 284 | natural_width guest_gdtr_base; |
| 285 | natural_width guest_idtr_base; |
| 286 | natural_width guest_dr7; |
| 287 | natural_width guest_rsp; |
| 288 | natural_width guest_rip; |
| 289 | natural_width guest_rflags; |
| 290 | natural_width guest_pending_dbg_exceptions; |
| 291 | natural_width guest_sysenter_esp; |
| 292 | natural_width guest_sysenter_eip; |
| 293 | natural_width host_cr0; |
| 294 | natural_width host_cr3; |
| 295 | natural_width host_cr4; |
| 296 | natural_width host_fs_base; |
| 297 | natural_width host_gs_base; |
| 298 | natural_width host_tr_base; |
| 299 | natural_width host_gdtr_base; |
| 300 | natural_width host_idtr_base; |
| 301 | natural_width host_ia32_sysenter_esp; |
| 302 | natural_width host_ia32_sysenter_eip; |
| 303 | natural_width host_rsp; |
| 304 | natural_width host_rip; |
| 305 | natural_width paddingl[8]; /* room for future expansion */ |
| 306 | u32 pin_based_vm_exec_control; |
| 307 | u32 cpu_based_vm_exec_control; |
| 308 | u32 exception_bitmap; |
| 309 | u32 page_fault_error_code_mask; |
| 310 | u32 page_fault_error_code_match; |
| 311 | u32 cr3_target_count; |
| 312 | u32 vm_exit_controls; |
| 313 | u32 vm_exit_msr_store_count; |
| 314 | u32 vm_exit_msr_load_count; |
| 315 | u32 vm_entry_controls; |
| 316 | u32 vm_entry_msr_load_count; |
| 317 | u32 vm_entry_intr_info_field; |
| 318 | u32 vm_entry_exception_error_code; |
| 319 | u32 vm_entry_instruction_len; |
| 320 | u32 tpr_threshold; |
| 321 | u32 secondary_vm_exec_control; |
| 322 | u32 vm_instruction_error; |
| 323 | u32 vm_exit_reason; |
| 324 | u32 vm_exit_intr_info; |
| 325 | u32 vm_exit_intr_error_code; |
| 326 | u32 idt_vectoring_info_field; |
| 327 | u32 idt_vectoring_error_code; |
| 328 | u32 vm_exit_instruction_len; |
| 329 | u32 vmx_instruction_info; |
| 330 | u32 guest_es_limit; |
| 331 | u32 guest_cs_limit; |
| 332 | u32 guest_ss_limit; |
| 333 | u32 guest_ds_limit; |
| 334 | u32 guest_fs_limit; |
| 335 | u32 guest_gs_limit; |
| 336 | u32 guest_ldtr_limit; |
| 337 | u32 guest_tr_limit; |
| 338 | u32 guest_gdtr_limit; |
| 339 | u32 guest_idtr_limit; |
| 340 | u32 guest_es_ar_bytes; |
| 341 | u32 guest_cs_ar_bytes; |
| 342 | u32 guest_ss_ar_bytes; |
| 343 | u32 guest_ds_ar_bytes; |
| 344 | u32 guest_fs_ar_bytes; |
| 345 | u32 guest_gs_ar_bytes; |
| 346 | u32 guest_ldtr_ar_bytes; |
| 347 | u32 guest_tr_ar_bytes; |
| 348 | u32 guest_interruptibility_info; |
| 349 | u32 guest_activity_state; |
| 350 | u32 guest_sysenter_cs; |
| 351 | u32 host_ia32_sysenter_cs; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 352 | u32 vmx_preemption_timer_value; |
| 353 | u32 padding32[7]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 354 | u16 virtual_processor_id; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 355 | u16 posted_intr_nv; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 356 | u16 guest_es_selector; |
| 357 | u16 guest_cs_selector; |
| 358 | u16 guest_ss_selector; |
| 359 | u16 guest_ds_selector; |
| 360 | u16 guest_fs_selector; |
| 361 | u16 guest_gs_selector; |
| 362 | u16 guest_ldtr_selector; |
| 363 | u16 guest_tr_selector; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 364 | u16 guest_intr_status; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 365 | u16 host_es_selector; |
| 366 | u16 host_cs_selector; |
| 367 | u16 host_ss_selector; |
| 368 | u16 host_ds_selector; |
| 369 | u16 host_fs_selector; |
| 370 | u16 host_gs_selector; |
| 371 | u16 host_tr_selector; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 372 | }; |
| 373 | |
| 374 | /* |
| 375 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 376 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 377 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. |
| 378 | */ |
| 379 | #define VMCS12_REVISION 0x11e57ed0 |
| 380 | |
| 381 | /* |
| 382 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 383 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 384 | * current implementation, 4K are reserved to avoid future complications. |
| 385 | */ |
| 386 | #define VMCS12_SIZE 0x1000 |
| 387 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 388 | /* Used to remember the last vmcs02 used for some recently used vmcs12s */ |
| 389 | struct vmcs02_list { |
| 390 | struct list_head list; |
| 391 | gpa_t vmptr; |
| 392 | struct loaded_vmcs vmcs02; |
| 393 | }; |
| 394 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 395 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 396 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 397 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 398 | */ |
| 399 | struct nested_vmx { |
| 400 | /* Has the level1 guest done vmxon? */ |
| 401 | bool vmxon; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 402 | gpa_t vmxon_ptr; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 403 | |
| 404 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 405 | gpa_t current_vmptr; |
| 406 | /* The host-usable pointer to the above */ |
| 407 | struct page *current_vmcs12_page; |
| 408 | struct vmcs12 *current_vmcs12; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 409 | /* |
| 410 | * Cache of the guest's VMCS, existing outside of guest memory. |
| 411 | * Loaded from guest memory during VMPTRLD. Flushed to guest |
| 412 | * memory during VMXOFF, VMCLEAR, VMPTRLD. |
| 413 | */ |
| 414 | struct vmcs12 *cached_vmcs12; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 415 | /* |
| 416 | * Indicates if the shadow vmcs must be updated with the |
| 417 | * data hold by vmcs12 |
| 418 | */ |
| 419 | bool sync_shadow_vmcs; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 420 | |
| 421 | /* vmcs02_list cache of VMCSs recently used to run L2 guests */ |
| 422 | struct list_head vmcs02_pool; |
| 423 | int vmcs02_num; |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 424 | bool change_vmcs01_virtual_x2apic_mode; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 425 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 426 | bool nested_run_pending; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 427 | /* |
| 428 | * Guest pages referred to in vmcs02 with host-physical pointers, so |
| 429 | * we must keep them pinned while L2 runs. |
| 430 | */ |
| 431 | struct page *apic_access_page; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 432 | struct page *virtual_apic_page; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 433 | struct page *pi_desc_page; |
| 434 | struct pi_desc *pi_desc; |
| 435 | bool pi_pending; |
| 436 | u16 posted_intr_nv; |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 437 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 438 | unsigned long *msr_bitmap; |
| 439 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 440 | struct hrtimer preemption_timer; |
| 441 | bool preemption_timer_expired; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 442 | |
| 443 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ |
| 444 | u64 vmcs01_debugctl; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 445 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 446 | u16 vpid02; |
| 447 | u16 last_vpid; |
| 448 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 449 | u32 nested_vmx_procbased_ctls_low; |
| 450 | u32 nested_vmx_procbased_ctls_high; |
| 451 | u32 nested_vmx_true_procbased_ctls_low; |
| 452 | u32 nested_vmx_secondary_ctls_low; |
| 453 | u32 nested_vmx_secondary_ctls_high; |
| 454 | u32 nested_vmx_pinbased_ctls_low; |
| 455 | u32 nested_vmx_pinbased_ctls_high; |
| 456 | u32 nested_vmx_exit_ctls_low; |
| 457 | u32 nested_vmx_exit_ctls_high; |
| 458 | u32 nested_vmx_true_exit_ctls_low; |
| 459 | u32 nested_vmx_entry_ctls_low; |
| 460 | u32 nested_vmx_entry_ctls_high; |
| 461 | u32 nested_vmx_true_entry_ctls_low; |
| 462 | u32 nested_vmx_misc_low; |
| 463 | u32 nested_vmx_misc_high; |
| 464 | u32 nested_vmx_ept_caps; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 465 | u32 nested_vmx_vpid_caps; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 466 | }; |
| 467 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 468 | #define POSTED_INTR_ON 0 |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 469 | #define POSTED_INTR_SN 1 |
| 470 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 471 | /* Posted-Interrupt Descriptor */ |
| 472 | struct pi_desc { |
| 473 | u32 pir[8]; /* Posted interrupt requested */ |
Feng Wu | 6ef1522 | 2015-09-18 22:29:45 +0800 | [diff] [blame] | 474 | union { |
| 475 | struct { |
| 476 | /* bit 256 - Outstanding Notification */ |
| 477 | u16 on : 1, |
| 478 | /* bit 257 - Suppress Notification */ |
| 479 | sn : 1, |
| 480 | /* bit 271:258 - Reserved */ |
| 481 | rsvd_1 : 14; |
| 482 | /* bit 279:272 - Notification Vector */ |
| 483 | u8 nv; |
| 484 | /* bit 287:280 - Reserved */ |
| 485 | u8 rsvd_2; |
| 486 | /* bit 319:288 - Notification Destination */ |
| 487 | u32 ndst; |
| 488 | }; |
| 489 | u64 control; |
| 490 | }; |
| 491 | u32 rsvd[6]; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 492 | } __aligned(64); |
| 493 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 494 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
| 495 | { |
| 496 | return test_and_set_bit(POSTED_INTR_ON, |
| 497 | (unsigned long *)&pi_desc->control); |
| 498 | } |
| 499 | |
| 500 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) |
| 501 | { |
| 502 | return test_and_clear_bit(POSTED_INTR_ON, |
| 503 | (unsigned long *)&pi_desc->control); |
| 504 | } |
| 505 | |
| 506 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) |
| 507 | { |
| 508 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); |
| 509 | } |
| 510 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 511 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
| 512 | { |
| 513 | return clear_bit(POSTED_INTR_SN, |
| 514 | (unsigned long *)&pi_desc->control); |
| 515 | } |
| 516 | |
| 517 | static inline void pi_set_sn(struct pi_desc *pi_desc) |
| 518 | { |
| 519 | return set_bit(POSTED_INTR_SN, |
| 520 | (unsigned long *)&pi_desc->control); |
| 521 | } |
| 522 | |
| 523 | static inline int pi_test_on(struct pi_desc *pi_desc) |
| 524 | { |
| 525 | return test_bit(POSTED_INTR_ON, |
| 526 | (unsigned long *)&pi_desc->control); |
| 527 | } |
| 528 | |
| 529 | static inline int pi_test_sn(struct pi_desc *pi_desc) |
| 530 | { |
| 531 | return test_bit(POSTED_INTR_SN, |
| 532 | (unsigned long *)&pi_desc->control); |
| 533 | } |
| 534 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 535 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 536 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 537 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 538 | u8 fail; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 539 | bool nmi_known_unmasked; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 540 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 541 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 542 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 543 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 544 | int nmsrs; |
| 545 | int save_nmsrs; |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 546 | unsigned long host_idt_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 547 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 548 | u64 msr_host_kernel_gs_base; |
| 549 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 550 | #endif |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 551 | u32 vm_entry_controls_shadow; |
| 552 | u32 vm_exit_controls_shadow; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 553 | /* |
| 554 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 555 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 556 | * guest (L2), it points to a different VMCS. |
| 557 | */ |
| 558 | struct loaded_vmcs vmcs01; |
| 559 | struct loaded_vmcs *loaded_vmcs; |
| 560 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 561 | struct msr_autoload { |
| 562 | unsigned nr; |
| 563 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 564 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 565 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 566 | struct { |
| 567 | int loaded; |
| 568 | u16 fs_sel, gs_sel, ldt_sel; |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 569 | #ifdef CONFIG_X86_64 |
| 570 | u16 ds_sel, es_sel; |
| 571 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 572 | int gs_ldt_reload_needed; |
| 573 | int fs_reload_needed; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 574 | u64 msr_host_bndcfgs; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 575 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 576 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 577 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 578 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 579 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 580 | struct kvm_segment segs[8]; |
| 581 | } rmode; |
| 582 | struct { |
| 583 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 584 | struct kvm_save_segment { |
| 585 | u16 selector; |
| 586 | unsigned long base; |
| 587 | u32 limit; |
| 588 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 589 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 590 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 591 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 592 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 593 | |
| 594 | /* Support for vnmi-less CPUs */ |
| 595 | int soft_vnmi_blocked; |
| 596 | ktime_t entry_time; |
| 597 | s64 vnmi_blocked_time; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 598 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 599 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 600 | /* Posted interrupt descriptor */ |
| 601 | struct pi_desc pi_desc; |
| 602 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 603 | /* Support for a guest hypervisor (nested VMX) */ |
| 604 | struct nested_vmx nested; |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 605 | |
| 606 | /* Dynamic PLE window. */ |
| 607 | int ple_window; |
| 608 | bool ple_window_dirty; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 609 | |
| 610 | /* Support for PML */ |
| 611 | #define PML_ENTITY_NUM 512 |
| 612 | struct page *pml_pg; |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 613 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 614 | /* apic deadline value in host tsc */ |
| 615 | u64 hv_deadline_tsc; |
| 616 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 617 | u64 current_tsc_ratio; |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 618 | |
| 619 | bool guest_pkru_valid; |
| 620 | u32 guest_pkru; |
| 621 | u32 host_pkru; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 622 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 623 | /* |
| 624 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in |
| 625 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included |
| 626 | * in msr_ia32_feature_control_valid_bits. |
| 627 | */ |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 628 | u64 msr_ia32_feature_control; |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 629 | u64 msr_ia32_feature_control_valid_bits; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 630 | }; |
| 631 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 632 | enum segment_cache_field { |
| 633 | SEG_FIELD_SEL = 0, |
| 634 | SEG_FIELD_BASE = 1, |
| 635 | SEG_FIELD_LIMIT = 2, |
| 636 | SEG_FIELD_AR = 3, |
| 637 | |
| 638 | SEG_FIELD_NR = 4 |
| 639 | }; |
| 640 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 641 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 642 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 643 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 644 | } |
| 645 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 646 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
| 647 | { |
| 648 | return &(to_vmx(vcpu)->pi_desc); |
| 649 | } |
| 650 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 651 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
| 652 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) |
| 653 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ |
| 654 | [number##_HIGH] = VMCS12_OFFSET(name)+4 |
| 655 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 656 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 657 | static unsigned long shadow_read_only_fields[] = { |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 658 | /* |
| 659 | * We do NOT shadow fields that are modified when L0 |
| 660 | * traps and emulates any vmx instruction (e.g. VMPTRLD, |
| 661 | * VMXON...) executed by L1. |
| 662 | * For example, VM_INSTRUCTION_ERROR is read |
| 663 | * by L1 if a vmx instruction fails (part of the error path). |
| 664 | * Note the code assumes this logic. If for some reason |
| 665 | * we start shadowing these fields then we need to |
| 666 | * force a shadow sync when L0 emulates vmx instructions |
| 667 | * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified |
| 668 | * by nested_vmx_failValid) |
| 669 | */ |
| 670 | VM_EXIT_REASON, |
| 671 | VM_EXIT_INTR_INFO, |
| 672 | VM_EXIT_INSTRUCTION_LEN, |
| 673 | IDT_VECTORING_INFO_FIELD, |
| 674 | IDT_VECTORING_ERROR_CODE, |
| 675 | VM_EXIT_INTR_ERROR_CODE, |
| 676 | EXIT_QUALIFICATION, |
| 677 | GUEST_LINEAR_ADDRESS, |
| 678 | GUEST_PHYSICAL_ADDRESS |
| 679 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 680 | static int max_shadow_read_only_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 681 | ARRAY_SIZE(shadow_read_only_fields); |
| 682 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 683 | static unsigned long shadow_read_write_fields[] = { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 684 | TPR_THRESHOLD, |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 685 | GUEST_RIP, |
| 686 | GUEST_RSP, |
| 687 | GUEST_CR0, |
| 688 | GUEST_CR3, |
| 689 | GUEST_CR4, |
| 690 | GUEST_INTERRUPTIBILITY_INFO, |
| 691 | GUEST_RFLAGS, |
| 692 | GUEST_CS_SELECTOR, |
| 693 | GUEST_CS_AR_BYTES, |
| 694 | GUEST_CS_LIMIT, |
| 695 | GUEST_CS_BASE, |
| 696 | GUEST_ES_BASE, |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 697 | GUEST_BNDCFGS, |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 698 | CR0_GUEST_HOST_MASK, |
| 699 | CR0_READ_SHADOW, |
| 700 | CR4_READ_SHADOW, |
| 701 | TSC_OFFSET, |
| 702 | EXCEPTION_BITMAP, |
| 703 | CPU_BASED_VM_EXEC_CONTROL, |
| 704 | VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 705 | VM_ENTRY_INTR_INFO_FIELD, |
| 706 | VM_ENTRY_INSTRUCTION_LEN, |
| 707 | VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 708 | HOST_FS_BASE, |
| 709 | HOST_GS_BASE, |
| 710 | HOST_FS_SELECTOR, |
| 711 | HOST_GS_SELECTOR |
| 712 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 713 | static int max_shadow_read_write_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 714 | ARRAY_SIZE(shadow_read_write_fields); |
| 715 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 716 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 717 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 718 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 719 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 720 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 721 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 722 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 723 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 724 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 725 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 726 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 727 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 728 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 729 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 730 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 731 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 732 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 733 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 734 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 735 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 736 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 737 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 738 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 739 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 740 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
| 741 | FIELD64(TSC_OFFSET, tsc_offset), |
| 742 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 743 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 744 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 745 | FIELD64(EPT_POINTER, ept_pointer), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 746 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
| 747 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), |
| 748 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), |
| 749 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 750 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 751 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 752 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 753 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 754 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 755 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 756 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 757 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 758 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 759 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 760 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 761 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 762 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 763 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 764 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 765 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 766 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 767 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 768 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 769 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 770 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 771 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 772 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 773 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 774 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 775 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 776 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 777 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 778 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 779 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 780 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 781 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 782 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 783 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 784 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 785 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 786 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 787 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 788 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 789 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 790 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 791 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 792 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 793 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 794 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 795 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 796 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 797 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 798 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 799 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 800 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 801 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 802 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 803 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 804 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 805 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 806 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 807 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 808 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 809 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 810 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 811 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 812 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 813 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 814 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 815 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 816 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 817 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 818 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 819 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 820 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 821 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 822 | FIELD(GUEST_CR0, guest_cr0), |
| 823 | FIELD(GUEST_CR3, guest_cr3), |
| 824 | FIELD(GUEST_CR4, guest_cr4), |
| 825 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 826 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 827 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 828 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 829 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 830 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 831 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 832 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 833 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 834 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 835 | FIELD(GUEST_DR7, guest_dr7), |
| 836 | FIELD(GUEST_RSP, guest_rsp), |
| 837 | FIELD(GUEST_RIP, guest_rip), |
| 838 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 839 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 840 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 841 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 842 | FIELD(HOST_CR0, host_cr0), |
| 843 | FIELD(HOST_CR3, host_cr3), |
| 844 | FIELD(HOST_CR4, host_cr4), |
| 845 | FIELD(HOST_FS_BASE, host_fs_base), |
| 846 | FIELD(HOST_GS_BASE, host_gs_base), |
| 847 | FIELD(HOST_TR_BASE, host_tr_base), |
| 848 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 849 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 850 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 851 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 852 | FIELD(HOST_RSP, host_rsp), |
| 853 | FIELD(HOST_RIP, host_rip), |
| 854 | }; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 855 | |
| 856 | static inline short vmcs_field_to_offset(unsigned long field) |
| 857 | { |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 858 | BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX); |
| 859 | |
| 860 | if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) || |
| 861 | vmcs_field_to_offset_table[field] == 0) |
| 862 | return -ENOENT; |
| 863 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 864 | return vmcs_field_to_offset_table[field]; |
| 865 | } |
| 866 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 867 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 868 | { |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 869 | return to_vmx(vcpu)->nested.cached_vmcs12; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr) |
| 873 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 874 | struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT); |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 875 | if (is_error_page(page)) |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 876 | return NULL; |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 877 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 878 | return page; |
| 879 | } |
| 880 | |
| 881 | static void nested_release_page(struct page *page) |
| 882 | { |
| 883 | kvm_release_page_dirty(page); |
| 884 | } |
| 885 | |
| 886 | static void nested_release_page_clean(struct page *page) |
| 887 | { |
| 888 | kvm_release_page_clean(page); |
| 889 | } |
| 890 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 891 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 892 | static u64 construct_eptp(unsigned long root_hpa); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 893 | static void kvm_cpu_vmxon(u64 addr); |
| 894 | static void kvm_cpu_vmxoff(void); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 895 | static bool vmx_xsaves_supported(void); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 896 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 897 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 898 | struct kvm_segment *var, int seg); |
| 899 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 900 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 901 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 902 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 903 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 904 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 905 | static int alloc_identity_pagetable(struct kvm *kvm); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 906 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 907 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 908 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 909 | /* |
| 910 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 911 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 912 | */ |
| 913 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 914 | static DEFINE_PER_CPU(struct desc_ptr, host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 915 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 916 | /* |
| 917 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 918 | * can find which vCPU should be waken up. |
| 919 | */ |
| 920 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 921 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 922 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 923 | static unsigned long *vmx_io_bitmap_a; |
| 924 | static unsigned long *vmx_io_bitmap_b; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 925 | static unsigned long *vmx_msr_bitmap_legacy; |
| 926 | static unsigned long *vmx_msr_bitmap_longmode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 927 | static unsigned long *vmx_msr_bitmap_legacy_x2apic; |
| 928 | static unsigned long *vmx_msr_bitmap_longmode_x2apic; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 929 | static unsigned long *vmx_msr_bitmap_legacy_x2apic_apicv_inactive; |
| 930 | static unsigned long *vmx_msr_bitmap_longmode_x2apic_apicv_inactive; |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 931 | static unsigned long *vmx_vmread_bitmap; |
| 932 | static unsigned long *vmx_vmwrite_bitmap; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 933 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 934 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 935 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 936 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 937 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 938 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 939 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 940 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 941 | int size; |
| 942 | int order; |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 943 | u32 basic_cap; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 944 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 945 | u32 pin_based_exec_ctrl; |
| 946 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 947 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 948 | u32 vmexit_ctrl; |
| 949 | u32 vmentry_ctrl; |
| 950 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 951 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 952 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 953 | u32 ept; |
| 954 | u32 vpid; |
| 955 | } vmx_capability; |
| 956 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 957 | #define VMX_SEGMENT_FIELD(seg) \ |
| 958 | [VCPU_SREG_##seg] = { \ |
| 959 | .selector = GUEST_##seg##_SELECTOR, \ |
| 960 | .base = GUEST_##seg##_BASE, \ |
| 961 | .limit = GUEST_##seg##_LIMIT, \ |
| 962 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 963 | } |
| 964 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 965 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 966 | unsigned selector; |
| 967 | unsigned base; |
| 968 | unsigned limit; |
| 969 | unsigned ar_bytes; |
| 970 | } kvm_vmx_segment_fields[] = { |
| 971 | VMX_SEGMENT_FIELD(CS), |
| 972 | VMX_SEGMENT_FIELD(DS), |
| 973 | VMX_SEGMENT_FIELD(ES), |
| 974 | VMX_SEGMENT_FIELD(FS), |
| 975 | VMX_SEGMENT_FIELD(GS), |
| 976 | VMX_SEGMENT_FIELD(SS), |
| 977 | VMX_SEGMENT_FIELD(TR), |
| 978 | VMX_SEGMENT_FIELD(LDTR), |
| 979 | }; |
| 980 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 981 | static u64 host_efer; |
| 982 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 983 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 984 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 985 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 986 | * 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] | 987 | * away by decrementing the array size. |
| 988 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 989 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 990 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 991 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 992 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 993 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 994 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 995 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 996 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 997 | { |
| 998 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 999 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1000 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
| 1001 | } |
| 1002 | |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 1003 | static inline bool is_debug(u32 intr_info) |
| 1004 | { |
| 1005 | return is_exception_n(intr_info, DB_VECTOR); |
| 1006 | } |
| 1007 | |
| 1008 | static inline bool is_breakpoint(u32 intr_info) |
| 1009 | { |
| 1010 | return is_exception_n(intr_info, BP_VECTOR); |
| 1011 | } |
| 1012 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1013 | static inline bool is_page_fault(u32 intr_info) |
| 1014 | { |
| 1015 | return is_exception_n(intr_info, PF_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1016 | } |
| 1017 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1018 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1019 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1020 | return is_exception_n(intr_info, NM_VECTOR); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1021 | } |
| 1022 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1023 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1024 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1025 | return is_exception_n(intr_info, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1026 | } |
| 1027 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1028 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1029 | { |
| 1030 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1031 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 1032 | } |
| 1033 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1034 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 1035 | { |
| 1036 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1037 | INTR_INFO_VALID_MASK)) == |
| 1038 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 1039 | } |
| 1040 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1041 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1042 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1043 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1044 | } |
| 1045 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1046 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1047 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1048 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1049 | } |
| 1050 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1051 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1052 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1053 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1054 | } |
| 1055 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1056 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1057 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1058 | return vmcs_config.cpu_based_exec_ctrl & |
| 1059 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1060 | } |
| 1061 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1062 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1063 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1064 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1065 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1066 | } |
| 1067 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1068 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 1069 | { |
| 1070 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1071 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 1072 | } |
| 1073 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1074 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 1075 | { |
| 1076 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1077 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1078 | } |
| 1079 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1080 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 1081 | { |
| 1082 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1083 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1084 | } |
| 1085 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1086 | /* |
| 1087 | * Comment's format: document - errata name - stepping - processor name. |
| 1088 | * Refer from |
| 1089 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 1090 | */ |
| 1091 | static u32 vmx_preemption_cpu_tfms[] = { |
| 1092 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 1093 | 0x000206E6, |
| 1094 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 1095 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 1096 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1097 | 0x00020652, |
| 1098 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1099 | 0x00020655, |
| 1100 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 1101 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 1102 | /* |
| 1103 | * 320767.pdf - AAP86 - B1 - |
| 1104 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 1105 | */ |
| 1106 | 0x000106E5, |
| 1107 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 1108 | 0x000106A0, |
| 1109 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 1110 | 0x000106A1, |
| 1111 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 1112 | 0x000106A4, |
| 1113 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 1114 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 1115 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 1116 | 0x000106A5, |
| 1117 | }; |
| 1118 | |
| 1119 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 1120 | { |
| 1121 | u32 eax = cpuid_eax(0x00000001), i; |
| 1122 | |
| 1123 | /* Clear the reserved bits */ |
| 1124 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 1125 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1126 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 1127 | return true; |
| 1128 | |
| 1129 | return false; |
| 1130 | } |
| 1131 | |
| 1132 | static inline bool cpu_has_vmx_preemption_timer(void) |
| 1133 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1134 | return vmcs_config.pin_based_exec_ctrl & |
| 1135 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1136 | } |
| 1137 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1138 | static inline bool cpu_has_vmx_posted_intr(void) |
| 1139 | { |
Paolo Bonzini | d6a858d | 2015-09-28 11:58:14 +0200 | [diff] [blame] | 1140 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
| 1141 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | static inline bool cpu_has_vmx_apicv(void) |
| 1145 | { |
| 1146 | return cpu_has_vmx_apic_register_virt() && |
| 1147 | cpu_has_vmx_virtual_intr_delivery() && |
| 1148 | cpu_has_vmx_posted_intr(); |
| 1149 | } |
| 1150 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1151 | static inline bool cpu_has_vmx_flexpriority(void) |
| 1152 | { |
| 1153 | return cpu_has_vmx_tpr_shadow() && |
| 1154 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1155 | } |
| 1156 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1157 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 1158 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1159 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1160 | } |
| 1161 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1162 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 1163 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1164 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1165 | } |
| 1166 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1167 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 1168 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1169 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 1172 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 1173 | { |
| 1174 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 1175 | } |
| 1176 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 1177 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 1178 | { |
| 1179 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 1180 | } |
| 1181 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1182 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1183 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1184 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1185 | } |
| 1186 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1187 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1188 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1189 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1190 | } |
| 1191 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1192 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 1193 | { |
| 1194 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 1195 | } |
| 1196 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1197 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 1198 | { |
| 1199 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 1200 | } |
| 1201 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1202 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1203 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1204 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1205 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1206 | } |
| 1207 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1208 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1209 | { |
| 1210 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1211 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 1212 | } |
| 1213 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1214 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1215 | { |
| 1216 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1217 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1218 | } |
| 1219 | |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1220 | static inline bool cpu_has_vmx_basic_inout(void) |
| 1221 | { |
| 1222 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); |
| 1223 | } |
| 1224 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1225 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1226 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1227 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1228 | } |
| 1229 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1230 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1231 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1232 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1233 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1234 | } |
| 1235 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1236 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1237 | { |
| 1238 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1239 | SECONDARY_EXEC_RDTSCP; |
| 1240 | } |
| 1241 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1242 | static inline bool cpu_has_vmx_invpcid(void) |
| 1243 | { |
| 1244 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1245 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 1246 | } |
| 1247 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1248 | static inline bool cpu_has_virtual_nmis(void) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 1249 | { |
| 1250 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 1251 | } |
| 1252 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1253 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 1254 | { |
| 1255 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1256 | SECONDARY_EXEC_WBINVD_EXITING; |
| 1257 | } |
| 1258 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1259 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
| 1260 | { |
| 1261 | u64 vmx_msr; |
| 1262 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 1263 | /* check if the cpu supports writing r/o exit information fields */ |
| 1264 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) |
| 1265 | return false; |
| 1266 | |
| 1267 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1268 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1269 | } |
| 1270 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1271 | static inline bool cpu_has_vmx_pml(void) |
| 1272 | { |
| 1273 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; |
| 1274 | } |
| 1275 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 1276 | static inline bool cpu_has_vmx_tsc_scaling(void) |
| 1277 | { |
| 1278 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1279 | SECONDARY_EXEC_TSC_SCALING; |
| 1280 | } |
| 1281 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1282 | static inline bool report_flexpriority(void) |
| 1283 | { |
| 1284 | return flexpriority_enabled; |
| 1285 | } |
| 1286 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1287 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 1288 | { |
| 1289 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 1290 | } |
| 1291 | |
| 1292 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 1293 | { |
| 1294 | return (vmcs12->cpu_based_vm_exec_control & |
| 1295 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 1296 | (vmcs12->secondary_vm_exec_control & bit); |
| 1297 | } |
| 1298 | |
Nadav Har'El | f5c4368 | 2013-08-05 11:07:20 +0300 | [diff] [blame] | 1299 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1300 | { |
| 1301 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 1302 | } |
| 1303 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 1304 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
| 1305 | { |
| 1306 | return vmcs12->pin_based_vm_exec_control & |
| 1307 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1308 | } |
| 1309 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 1310 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
| 1311 | { |
| 1312 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); |
| 1313 | } |
| 1314 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1315 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
| 1316 | { |
| 1317 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) && |
| 1318 | vmx_xsaves_supported(); |
| 1319 | } |
| 1320 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 1321 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
| 1322 | { |
| 1323 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
| 1324 | } |
| 1325 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 1326 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
| 1327 | { |
| 1328 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); |
| 1329 | } |
| 1330 | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 1331 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
| 1332 | { |
| 1333 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); |
| 1334 | } |
| 1335 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 1336 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
| 1337 | { |
| 1338 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 1339 | } |
| 1340 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 1341 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
| 1342 | { |
| 1343 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; |
| 1344 | } |
| 1345 | |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1346 | static inline bool is_nmi(u32 intr_info) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1347 | { |
| 1348 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1349 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1350 | } |
| 1351 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 1352 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 1353 | u32 exit_intr_info, |
| 1354 | unsigned long exit_qualification); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 1355 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 1356 | struct vmcs12 *vmcs12, |
| 1357 | u32 reason, unsigned long qualification); |
| 1358 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1359 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1360 | { |
| 1361 | int i; |
| 1362 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1363 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1364 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1365 | return i; |
| 1366 | return -1; |
| 1367 | } |
| 1368 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1369 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 1370 | { |
| 1371 | struct { |
| 1372 | u64 vpid : 16; |
| 1373 | u64 rsvd : 48; |
| 1374 | u64 gva; |
| 1375 | } operand = { vpid, 0, gva }; |
| 1376 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1377 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1378 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1379 | "; ja 1f ; ud2 ; 1:" |
| 1380 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 1381 | } |
| 1382 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1383 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 1384 | { |
| 1385 | struct { |
| 1386 | u64 eptp, gpa; |
| 1387 | } operand = {eptp, gpa}; |
| 1388 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1389 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1390 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1391 | "; ja 1f ; ud2 ; 1:\n" |
| 1392 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 1393 | } |
| 1394 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1395 | 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] | 1396 | { |
| 1397 | int i; |
| 1398 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1399 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1400 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1401 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1402 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1403 | } |
| 1404 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1405 | static void vmcs_clear(struct vmcs *vmcs) |
| 1406 | { |
| 1407 | u64 phys_addr = __pa(vmcs); |
| 1408 | u8 error; |
| 1409 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1410 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1411 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1412 | : "cc", "memory"); |
| 1413 | if (error) |
| 1414 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 1415 | vmcs, phys_addr); |
| 1416 | } |
| 1417 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1418 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 1419 | { |
| 1420 | vmcs_clear(loaded_vmcs->vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 1421 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 1422 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1423 | loaded_vmcs->cpu = -1; |
| 1424 | loaded_vmcs->launched = 0; |
| 1425 | } |
| 1426 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1427 | static void vmcs_load(struct vmcs *vmcs) |
| 1428 | { |
| 1429 | u64 phys_addr = __pa(vmcs); |
| 1430 | u8 error; |
| 1431 | |
| 1432 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1433 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1434 | : "cc", "memory"); |
| 1435 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 1436 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1437 | vmcs, phys_addr); |
| 1438 | } |
| 1439 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1440 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1441 | /* |
| 1442 | * This bitmap is used to indicate whether the vmclear |
| 1443 | * operation is enabled on all cpus. All disabled by |
| 1444 | * default. |
| 1445 | */ |
| 1446 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 1447 | |
| 1448 | static inline void crash_enable_local_vmclear(int cpu) |
| 1449 | { |
| 1450 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1451 | } |
| 1452 | |
| 1453 | static inline void crash_disable_local_vmclear(int cpu) |
| 1454 | { |
| 1455 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1456 | } |
| 1457 | |
| 1458 | static inline int crash_local_vmclear_enabled(int cpu) |
| 1459 | { |
| 1460 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1461 | } |
| 1462 | |
| 1463 | static void crash_vmclear_local_loaded_vmcss(void) |
| 1464 | { |
| 1465 | int cpu = raw_smp_processor_id(); |
| 1466 | struct loaded_vmcs *v; |
| 1467 | |
| 1468 | if (!crash_local_vmclear_enabled(cpu)) |
| 1469 | return; |
| 1470 | |
| 1471 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 1472 | loaded_vmcss_on_cpu_link) |
| 1473 | vmcs_clear(v->vmcs); |
| 1474 | } |
| 1475 | #else |
| 1476 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 1477 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1478 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1479 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1480 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1481 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1482 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1483 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1484 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1485 | if (loaded_vmcs->cpu != cpu) |
| 1486 | return; /* vcpu migration can race with cpu offline */ |
| 1487 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1488 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1489 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1490 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1491 | |
| 1492 | /* |
| 1493 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 1494 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 1495 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 1496 | * then adds the vmcs into percpu list before it is deleted. |
| 1497 | */ |
| 1498 | smp_wmb(); |
| 1499 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1500 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1501 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1502 | } |
| 1503 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1504 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1505 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 1506 | int cpu = loaded_vmcs->cpu; |
| 1507 | |
| 1508 | if (cpu != -1) |
| 1509 | smp_call_function_single(cpu, |
| 1510 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1511 | } |
| 1512 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1513 | static inline void vpid_sync_vcpu_single(int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1514 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1515 | if (vpid == 0) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1516 | return; |
| 1517 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1518 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1519 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1520 | } |
| 1521 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1522 | static inline void vpid_sync_vcpu_global(void) |
| 1523 | { |
| 1524 | if (cpu_has_vmx_invvpid_global()) |
| 1525 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 1526 | } |
| 1527 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1528 | static inline void vpid_sync_context(int vpid) |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1529 | { |
| 1530 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1531 | vpid_sync_vcpu_single(vpid); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1532 | else |
| 1533 | vpid_sync_vcpu_global(); |
| 1534 | } |
| 1535 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1536 | static inline void ept_sync_global(void) |
| 1537 | { |
| 1538 | if (cpu_has_vmx_invept_global()) |
| 1539 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
| 1540 | } |
| 1541 | |
| 1542 | static inline void ept_sync_context(u64 eptp) |
| 1543 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1544 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1545 | if (cpu_has_vmx_invept_context()) |
| 1546 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 1547 | else |
| 1548 | ept_sync_global(); |
| 1549 | } |
| 1550 | } |
| 1551 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1552 | static __always_inline void vmcs_check16(unsigned long field) |
| 1553 | { |
| 1554 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 1555 | "16-bit accessor invalid for 64-bit field"); |
| 1556 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1557 | "16-bit accessor invalid for 64-bit high field"); |
| 1558 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1559 | "16-bit accessor invalid for 32-bit high field"); |
| 1560 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1561 | "16-bit accessor invalid for natural width field"); |
| 1562 | } |
| 1563 | |
| 1564 | static __always_inline void vmcs_check32(unsigned long field) |
| 1565 | { |
| 1566 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1567 | "32-bit accessor invalid for 16-bit field"); |
| 1568 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1569 | "32-bit accessor invalid for natural width field"); |
| 1570 | } |
| 1571 | |
| 1572 | static __always_inline void vmcs_check64(unsigned long field) |
| 1573 | { |
| 1574 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1575 | "64-bit accessor invalid for 16-bit field"); |
| 1576 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1577 | "64-bit accessor invalid for 64-bit high field"); |
| 1578 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1579 | "64-bit accessor invalid for 32-bit field"); |
| 1580 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1581 | "64-bit accessor invalid for natural width field"); |
| 1582 | } |
| 1583 | |
| 1584 | static __always_inline void vmcs_checkl(unsigned long field) |
| 1585 | { |
| 1586 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1587 | "Natural width accessor invalid for 16-bit field"); |
| 1588 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 1589 | "Natural width accessor invalid for 64-bit field"); |
| 1590 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1591 | "Natural width accessor invalid for 64-bit high field"); |
| 1592 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1593 | "Natural width accessor invalid for 32-bit field"); |
| 1594 | } |
| 1595 | |
| 1596 | static __always_inline unsigned long __vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1597 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1598 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1599 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1600 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 1601 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1602 | return value; |
| 1603 | } |
| 1604 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1605 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1606 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1607 | vmcs_check16(field); |
| 1608 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1609 | } |
| 1610 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1611 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1612 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1613 | vmcs_check32(field); |
| 1614 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1615 | } |
| 1616 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1617 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1618 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1619 | vmcs_check64(field); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1620 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1621 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1622 | #else |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1623 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1624 | #endif |
| 1625 | } |
| 1626 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1627 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
| 1628 | { |
| 1629 | vmcs_checkl(field); |
| 1630 | return __vmcs_readl(field); |
| 1631 | } |
| 1632 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1633 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 1634 | { |
| 1635 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 1636 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 1637 | dump_stack(); |
| 1638 | } |
| 1639 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1640 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1641 | { |
| 1642 | u8 error; |
| 1643 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1644 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1645 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1646 | if (unlikely(error)) |
| 1647 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1648 | } |
| 1649 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1650 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1651 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1652 | vmcs_check16(field); |
| 1653 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1654 | } |
| 1655 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1656 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1657 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1658 | vmcs_check32(field); |
| 1659 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1660 | } |
| 1661 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1662 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1663 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1664 | vmcs_check64(field); |
| 1665 | __vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 1666 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1667 | asm volatile (""); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1668 | __vmcs_writel(field+1, value >> 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1669 | #endif |
| 1670 | } |
| 1671 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1672 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1673 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1674 | vmcs_checkl(field); |
| 1675 | __vmcs_writel(field, value); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1676 | } |
| 1677 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1678 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1679 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1680 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 1681 | "vmcs_clear_bits does not support 64-bit fields"); |
| 1682 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); |
| 1683 | } |
| 1684 | |
| 1685 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
| 1686 | { |
| 1687 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 1688 | "vmcs_set_bits does not support 64-bit fields"); |
| 1689 | __vmcs_writel(field, __vmcs_readl(field) | mask); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1690 | } |
| 1691 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 1692 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 1693 | { |
| 1694 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); |
| 1695 | } |
| 1696 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1697 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1698 | { |
| 1699 | vmcs_write32(VM_ENTRY_CONTROLS, val); |
| 1700 | vmx->vm_entry_controls_shadow = val; |
| 1701 | } |
| 1702 | |
| 1703 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1704 | { |
| 1705 | if (vmx->vm_entry_controls_shadow != val) |
| 1706 | vm_entry_controls_init(vmx, val); |
| 1707 | } |
| 1708 | |
| 1709 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) |
| 1710 | { |
| 1711 | return vmx->vm_entry_controls_shadow; |
| 1712 | } |
| 1713 | |
| 1714 | |
| 1715 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 1716 | { |
| 1717 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); |
| 1718 | } |
| 1719 | |
| 1720 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 1721 | { |
| 1722 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); |
| 1723 | } |
| 1724 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 1725 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 1726 | { |
| 1727 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); |
| 1728 | } |
| 1729 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1730 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1731 | { |
| 1732 | vmcs_write32(VM_EXIT_CONTROLS, val); |
| 1733 | vmx->vm_exit_controls_shadow = val; |
| 1734 | } |
| 1735 | |
| 1736 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1737 | { |
| 1738 | if (vmx->vm_exit_controls_shadow != val) |
| 1739 | vm_exit_controls_init(vmx, val); |
| 1740 | } |
| 1741 | |
| 1742 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) |
| 1743 | { |
| 1744 | return vmx->vm_exit_controls_shadow; |
| 1745 | } |
| 1746 | |
| 1747 | |
| 1748 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 1749 | { |
| 1750 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); |
| 1751 | } |
| 1752 | |
| 1753 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 1754 | { |
| 1755 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); |
| 1756 | } |
| 1757 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1758 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 1759 | { |
| 1760 | vmx->segment_cache.bitmask = 0; |
| 1761 | } |
| 1762 | |
| 1763 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 1764 | unsigned field) |
| 1765 | { |
| 1766 | bool ret; |
| 1767 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 1768 | |
| 1769 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 1770 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 1771 | vmx->segment_cache.bitmask = 0; |
| 1772 | } |
| 1773 | ret = vmx->segment_cache.bitmask & mask; |
| 1774 | vmx->segment_cache.bitmask |= mask; |
| 1775 | return ret; |
| 1776 | } |
| 1777 | |
| 1778 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 1779 | { |
| 1780 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 1781 | |
| 1782 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 1783 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 1784 | return *p; |
| 1785 | } |
| 1786 | |
| 1787 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 1788 | { |
| 1789 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 1790 | |
| 1791 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 1792 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 1793 | return *p; |
| 1794 | } |
| 1795 | |
| 1796 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 1797 | { |
| 1798 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 1799 | |
| 1800 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 1801 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 1802 | return *p; |
| 1803 | } |
| 1804 | |
| 1805 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 1806 | { |
| 1807 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 1808 | |
| 1809 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 1810 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 1811 | return *p; |
| 1812 | } |
| 1813 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1814 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 1815 | { |
| 1816 | u32 eb; |
| 1817 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1818 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 1819 | (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1820 | if ((vcpu->guest_debug & |
| 1821 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 1822 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 1823 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1824 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1825 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1826 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1827 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1828 | if (vcpu->fpu_active) |
| 1829 | eb &= ~(1u << NM_VECTOR); |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1830 | |
| 1831 | /* When we are running a nested L2 guest and L1 specified for it a |
| 1832 | * certain exception bitmap, we must trap the same exceptions and pass |
| 1833 | * them to L1. When running L2, we will only handle the exceptions |
| 1834 | * specified above if L1 did not want them. |
| 1835 | */ |
| 1836 | if (is_guest_mode(vcpu)) |
| 1837 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 1838 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1839 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 1840 | } |
| 1841 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1842 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 1843 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1844 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1845 | vm_entry_controls_clearbit(vmx, entry); |
| 1846 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1847 | } |
| 1848 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1849 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 1850 | { |
| 1851 | unsigned i; |
| 1852 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1853 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1854 | switch (msr) { |
| 1855 | case MSR_EFER: |
| 1856 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1857 | clear_atomic_switch_msr_special(vmx, |
| 1858 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1859 | VM_EXIT_LOAD_IA32_EFER); |
| 1860 | return; |
| 1861 | } |
| 1862 | break; |
| 1863 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1864 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1865 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1866 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1867 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 1868 | return; |
| 1869 | } |
| 1870 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1871 | } |
| 1872 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1873 | for (i = 0; i < m->nr; ++i) |
| 1874 | if (m->guest[i].index == msr) |
| 1875 | break; |
| 1876 | |
| 1877 | if (i == m->nr) |
| 1878 | return; |
| 1879 | --m->nr; |
| 1880 | m->guest[i] = m->guest[m->nr]; |
| 1881 | m->host[i] = m->host[m->nr]; |
| 1882 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1883 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1884 | } |
| 1885 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1886 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 1887 | unsigned long entry, unsigned long exit, |
| 1888 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 1889 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1890 | { |
| 1891 | vmcs_write64(guest_val_vmcs, guest_val); |
| 1892 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1893 | vm_entry_controls_setbit(vmx, entry); |
| 1894 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1895 | } |
| 1896 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1897 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 1898 | u64 guest_val, u64 host_val) |
| 1899 | { |
| 1900 | unsigned i; |
| 1901 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1902 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1903 | switch (msr) { |
| 1904 | case MSR_EFER: |
| 1905 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1906 | add_atomic_switch_msr_special(vmx, |
| 1907 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1908 | VM_EXIT_LOAD_IA32_EFER, |
| 1909 | GUEST_IA32_EFER, |
| 1910 | HOST_IA32_EFER, |
| 1911 | guest_val, host_val); |
| 1912 | return; |
| 1913 | } |
| 1914 | break; |
| 1915 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1916 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1917 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1918 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1919 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1920 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 1921 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 1922 | guest_val, host_val); |
| 1923 | return; |
| 1924 | } |
| 1925 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 1926 | case MSR_IA32_PEBS_ENABLE: |
| 1927 | /* PEBS needs a quiescent period after being disabled (to write |
| 1928 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 1929 | * provide that period, so a CPU could write host's record into |
| 1930 | * guest's memory. |
| 1931 | */ |
| 1932 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1933 | } |
| 1934 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1935 | for (i = 0; i < m->nr; ++i) |
| 1936 | if (m->guest[i].index == msr) |
| 1937 | break; |
| 1938 | |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 1939 | if (i == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 1940 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 1941 | "Can't add msr %x\n", msr); |
| 1942 | return; |
| 1943 | } else if (i == m->nr) { |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1944 | ++m->nr; |
| 1945 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1946 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1947 | } |
| 1948 | |
| 1949 | m->guest[i].index = msr; |
| 1950 | m->guest[i].value = guest_val; |
| 1951 | m->host[i].index = msr; |
| 1952 | m->host[i].value = host_val; |
| 1953 | } |
| 1954 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1955 | static void reload_tss(void) |
| 1956 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1957 | /* |
| 1958 | * VT restores TR but not its size. Useless. |
| 1959 | */ |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 1960 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 1961 | struct desc_struct *descs; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1962 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 1963 | descs = (void *)gdt->address; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1964 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 1965 | load_TR_desc(); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1966 | } |
| 1967 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1968 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1969 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 1970 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 1971 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1972 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 1973 | if (!enable_ept) { |
| 1974 | /* |
| 1975 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 1976 | * host CPUID is more efficient than testing guest CPUID |
| 1977 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 1978 | */ |
| 1979 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 1980 | guest_efer |= EFER_NX; |
| 1981 | else if (!(guest_efer & EFER_NX)) |
| 1982 | ignore_bits |= EFER_NX; |
| 1983 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 1984 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1985 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 1986 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1987 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 1988 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 1989 | #ifdef CONFIG_X86_64 |
| 1990 | ignore_bits |= EFER_LMA | EFER_LME; |
| 1991 | /* SCE is meaningful only in long mode on Intel */ |
| 1992 | if (guest_efer & EFER_LMA) |
| 1993 | ignore_bits &= ~(u64)EFER_SCE; |
| 1994 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 1995 | |
| 1996 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 1997 | |
| 1998 | /* |
| 1999 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 2000 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 2001 | * atomically, since it's faster than switching it manually. |
| 2002 | */ |
| 2003 | if (cpu_has_load_ia32_efer || |
| 2004 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2005 | if (!(guest_efer & EFER_LMA)) |
| 2006 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 2007 | if (guest_efer != host_efer) |
| 2008 | add_atomic_switch_msr(vmx, MSR_EFER, |
| 2009 | guest_efer, host_efer); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2010 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2011 | } else { |
| 2012 | guest_efer &= ~ignore_bits; |
| 2013 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2014 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2015 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 2016 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 2017 | |
| 2018 | return true; |
| 2019 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2020 | } |
| 2021 | |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2022 | static unsigned long segment_base(u16 selector) |
| 2023 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 2024 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2025 | struct desc_struct *d; |
| 2026 | unsigned long table_base; |
| 2027 | unsigned long v; |
| 2028 | |
| 2029 | if (!(selector & ~3)) |
| 2030 | return 0; |
| 2031 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 2032 | table_base = gdt->address; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2033 | |
| 2034 | if (selector & 4) { /* from ldt */ |
| 2035 | u16 ldt_selector = kvm_read_ldt(); |
| 2036 | |
| 2037 | if (!(ldt_selector & ~3)) |
| 2038 | return 0; |
| 2039 | |
| 2040 | table_base = segment_base(ldt_selector); |
| 2041 | } |
| 2042 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
| 2043 | v = get_desc_base(d); |
| 2044 | #ifdef CONFIG_X86_64 |
| 2045 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 2046 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; |
| 2047 | #endif |
| 2048 | return v; |
| 2049 | } |
| 2050 | |
| 2051 | static inline unsigned long kvm_read_tr_base(void) |
| 2052 | { |
| 2053 | u16 tr; |
| 2054 | asm("str %0" : "=g"(tr)); |
| 2055 | return segment_base(tr); |
| 2056 | } |
| 2057 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2058 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2059 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2060 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2061 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2062 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2063 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2064 | return; |
| 2065 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2066 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2067 | /* |
| 2068 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 2069 | * allow segment selectors with cpl > 0 or ti == 1. |
| 2070 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2071 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2072 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2073 | savesegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2074 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2075 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2076 | vmx->host_state.fs_reload_needed = 0; |
| 2077 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2078 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2079 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2080 | } |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2081 | savesegment(gs, vmx->host_state.gs_sel); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2082 | if (!(vmx->host_state.gs_sel & 7)) |
| 2083 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2084 | else { |
| 2085 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2086 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | #ifdef CONFIG_X86_64 |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2090 | savesegment(ds, vmx->host_state.ds_sel); |
| 2091 | savesegment(es, vmx->host_state.es_sel); |
| 2092 | #endif |
| 2093 | |
| 2094 | #ifdef CONFIG_X86_64 |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2095 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 2096 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 2097 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2098 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 2099 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2100 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2101 | |
| 2102 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2103 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
| 2104 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2105 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2106 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2107 | if (boot_cpu_has(X86_FEATURE_MPX)) |
| 2108 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2109 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 2110 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 2111 | vmx->guest_msrs[i].data, |
| 2112 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2113 | } |
| 2114 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2115 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2116 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2117 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2118 | return; |
| 2119 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2120 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2121 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2122 | #ifdef CONFIG_X86_64 |
| 2123 | if (is_long_mode(&vmx->vcpu)) |
| 2124 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 2125 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2126 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2127 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2128 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2129 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2130 | #else |
| 2131 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2132 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2133 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 2134 | if (vmx->host_state.fs_reload_needed) |
| 2135 | loadsegment(fs, vmx->host_state.fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2136 | #ifdef CONFIG_X86_64 |
| 2137 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { |
| 2138 | loadsegment(ds, vmx->host_state.ds_sel); |
| 2139 | loadsegment(es, vmx->host_state.es_sel); |
| 2140 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2141 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2142 | reload_tss(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2143 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2144 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2145 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2146 | if (vmx->host_state.msr_host_bndcfgs) |
| 2147 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 2148 | /* |
| 2149 | * If the FPU is not active (through the host task or |
| 2150 | * the guest vcpu), then restore the cr0.TS bit. |
| 2151 | */ |
Ingo Molnar | 3c6dffa | 2015-04-28 12:28:08 +0200 | [diff] [blame] | 2152 | if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded) |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 2153 | stts(); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 2154 | load_gdt(this_cpu_ptr(&host_gdt)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2155 | } |
| 2156 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2157 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 2158 | { |
| 2159 | preempt_disable(); |
| 2160 | __vmx_load_host_state(vmx); |
| 2161 | preempt_enable(); |
| 2162 | } |
| 2163 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2164 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 2165 | { |
| 2166 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2167 | struct pi_desc old, new; |
| 2168 | unsigned int dest; |
| 2169 | |
| 2170 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 2171 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 2172 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2173 | return; |
| 2174 | |
| 2175 | do { |
| 2176 | old.control = new.control = pi_desc->control; |
| 2177 | |
| 2178 | /* |
| 2179 | * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there |
| 2180 | * are two possible cases: |
| 2181 | * 1. After running 'pre_block', context switch |
| 2182 | * happened. For this case, 'sn' was set in |
| 2183 | * vmx_vcpu_put(), so we need to clear it here. |
| 2184 | * 2. After running 'pre_block', we were blocked, |
| 2185 | * and woken up by some other guy. For this case, |
| 2186 | * we don't need to do anything, 'pi_post_block' |
| 2187 | * will do everything for us. However, we cannot |
| 2188 | * check whether it is case #1 or case #2 here |
| 2189 | * (maybe, not needed), so we also clear sn here, |
| 2190 | * I think it is not a big deal. |
| 2191 | */ |
| 2192 | if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) { |
| 2193 | if (vcpu->cpu != cpu) { |
| 2194 | dest = cpu_physical_id(cpu); |
| 2195 | |
| 2196 | if (x2apic_enabled()) |
| 2197 | new.ndst = dest; |
| 2198 | else |
| 2199 | new.ndst = (dest << 8) & 0xFF00; |
| 2200 | } |
| 2201 | |
| 2202 | /* set 'NV' to 'notification vector' */ |
| 2203 | new.nv = POSTED_INTR_VECTOR; |
| 2204 | } |
| 2205 | |
| 2206 | /* Allow posting non-urgent interrupts */ |
| 2207 | new.sn = 0; |
| 2208 | } while (cmpxchg(&pi_desc->control, old.control, |
| 2209 | new.control) != old.control); |
| 2210 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2211 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2212 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
| 2213 | { |
| 2214 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; |
| 2215 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); |
| 2216 | } |
| 2217 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2218 | /* |
| 2219 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 2220 | * vcpu mutex is already taken. |
| 2221 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2222 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2223 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2224 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2225 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2226 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2227 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2228 | if (!vmm_exclusive) |
| 2229 | kvm_cpu_vmxon(phys_addr); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2230 | else if (!already_loaded) |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2231 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2232 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2233 | if (!already_loaded) { |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2234 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2235 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2236 | |
| 2237 | /* |
| 2238 | * Read loaded_vmcs->cpu should be before fetching |
| 2239 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 2240 | * See the comments in __loaded_vmcs_clear(). |
| 2241 | */ |
| 2242 | smp_rmb(); |
| 2243 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2244 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 2245 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2246 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2247 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 2251 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 2252 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 2253 | } |
| 2254 | |
| 2255 | if (!already_loaded) { |
| 2256 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
| 2257 | unsigned long sysenter_esp; |
| 2258 | |
| 2259 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2260 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2261 | /* |
| 2262 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 2263 | * processors. |
| 2264 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2265 | vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 2266 | vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2267 | |
| 2268 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 2269 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 2270 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2271 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2272 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2273 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2274 | /* Setup TSC multiplier */ |
| 2275 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2276 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 2277 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2278 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2279 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2280 | vmx->host_pkru = read_pkru(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 2284 | { |
| 2285 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2286 | |
| 2287 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 2288 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 2289 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2290 | return; |
| 2291 | |
| 2292 | /* Set SN when the vCPU is preempted */ |
| 2293 | if (vcpu->preempted) |
| 2294 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 2298 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2299 | vmx_vcpu_pi_put(vcpu); |
| 2300 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2301 | __vmx_load_host_state(to_vmx(vcpu)); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2302 | if (!vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2303 | __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs); |
| 2304 | vcpu->cpu = -1; |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2305 | kvm_cpu_vmxoff(); |
| 2306 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2307 | } |
| 2308 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2309 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 2310 | { |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2311 | ulong cr0; |
| 2312 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2313 | if (vcpu->fpu_active) |
| 2314 | return; |
| 2315 | vcpu->fpu_active = 1; |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2316 | cr0 = vmcs_readl(GUEST_CR0); |
| 2317 | cr0 &= ~(X86_CR0_TS | X86_CR0_MP); |
| 2318 | cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP); |
| 2319 | vmcs_writel(GUEST_CR0, cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2320 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2321 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2322 | if (is_guest_mode(vcpu)) |
| 2323 | vcpu->arch.cr0_guest_owned_bits &= |
| 2324 | ~get_vmcs12(vcpu)->cr0_guest_host_mask; |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2325 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2326 | } |
| 2327 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2328 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 2329 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 2330 | /* |
| 2331 | * Return the cr0 value that a nested guest would read. This is a combination |
| 2332 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 2333 | * its hypervisor (cr0_read_shadow). |
| 2334 | */ |
| 2335 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 2336 | { |
| 2337 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 2338 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 2339 | } |
| 2340 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 2341 | { |
| 2342 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 2343 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 2344 | } |
| 2345 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2346 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 2347 | { |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2348 | /* Note that there is no vcpu->fpu_active = 0 here. The caller must |
| 2349 | * set this *before* calling this function. |
| 2350 | */ |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2351 | vmx_decache_cr0_guest_bits(vcpu); |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2352 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2353 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2354 | vcpu->arch.cr0_guest_owned_bits = 0; |
| 2355 | 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] | 2356 | if (is_guest_mode(vcpu)) { |
| 2357 | /* |
| 2358 | * L1's specified read shadow might not contain the TS bit, |
| 2359 | * so now that we turned on shadowing of this bit, we need to |
| 2360 | * set this bit of the shadow. Like in nested_vmx_run we need |
| 2361 | * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet |
| 2362 | * up-to-date here because we just decached cr0.TS (and we'll |
| 2363 | * only update vmcs12->guest_cr0 on nested exit). |
| 2364 | */ |
| 2365 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2366 | vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) | |
| 2367 | (vcpu->arch.cr0 & X86_CR0_TS); |
| 2368 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 2369 | } else |
| 2370 | vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2371 | } |
| 2372 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2373 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 2374 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2375 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 2376 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2377 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 2378 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2379 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 2380 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2381 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2382 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 2383 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2384 | } |
| 2385 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2386 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2387 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2391 | { |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2392 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2393 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2394 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2395 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2396 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2397 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2398 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 2399 | } |
| 2400 | |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 2401 | static u32 vmx_get_pkru(struct kvm_vcpu *vcpu) |
| 2402 | { |
| 2403 | return to_vmx(vcpu)->guest_pkru; |
| 2404 | } |
| 2405 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2406 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2407 | { |
| 2408 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2409 | int ret = 0; |
| 2410 | |
| 2411 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2412 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2413 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2414 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2415 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2416 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 2420 | { |
| 2421 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2422 | u32 interruptibility = interruptibility_old; |
| 2423 | |
| 2424 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 2425 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2426 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2427 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2428 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2429 | interruptibility |= GUEST_INTR_STATE_STI; |
| 2430 | |
| 2431 | if ((interruptibility != interruptibility_old)) |
| 2432 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 2433 | } |
| 2434 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2435 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 2436 | { |
| 2437 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2438 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2439 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2440 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2441 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2442 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2443 | /* skipping an emulated instruction also counts */ |
| 2444 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2445 | } |
| 2446 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2447 | /* |
| 2448 | * KVM wants to inject page-faults which it got to the guest. This function |
| 2449 | * 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] | 2450 | */ |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2451 | 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] | 2452 | { |
| 2453 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2454 | |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2455 | if (!(vmcs12->exception_bitmap & (1u << nr))) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2456 | return 0; |
| 2457 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 2458 | nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason, |
| 2459 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 2460 | vmcs_readl(EXIT_QUALIFICATION)); |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2461 | return 1; |
| 2462 | } |
| 2463 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2464 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 2465 | bool has_error_code, u32 error_code, |
| 2466 | bool reinject) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2467 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2468 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2469 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2470 | |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2471 | if (!reinject && is_guest_mode(vcpu) && |
| 2472 | nested_vmx_check_exception(vcpu, nr)) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2473 | return; |
| 2474 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2475 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2476 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2477 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 2478 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2479 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2480 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 2481 | int inc_eip = 0; |
| 2482 | if (kvm_exception_is_soft(nr)) |
| 2483 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 2484 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 2485 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2486 | return; |
| 2487 | } |
| 2488 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2489 | if (kvm_exception_is_soft(nr)) { |
| 2490 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2491 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2492 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 2493 | } else |
| 2494 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 2495 | |
| 2496 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2497 | } |
| 2498 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2499 | static bool vmx_rdtscp_supported(void) |
| 2500 | { |
| 2501 | return cpu_has_vmx_rdtscp(); |
| 2502 | } |
| 2503 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 2504 | static bool vmx_invpcid_supported(void) |
| 2505 | { |
| 2506 | return cpu_has_vmx_invpcid() && enable_ept; |
| 2507 | } |
| 2508 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2509 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2510 | * Swap MSR entry in host/guest MSR entry array. |
| 2511 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2512 | 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] | 2513 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2514 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2515 | |
| 2516 | tmp = vmx->guest_msrs[to]; |
| 2517 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 2518 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2519 | } |
| 2520 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2521 | static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu) |
| 2522 | { |
| 2523 | unsigned long *msr_bitmap; |
| 2524 | |
Wincy Van | 670125b | 2015-03-04 14:31:56 +0800 | [diff] [blame] | 2525 | if (is_guest_mode(vcpu)) |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 2526 | msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap; |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 2527 | else if (cpu_has_secondary_exec_ctrls() && |
| 2528 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 2529 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 2530 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) { |
| 2531 | if (is_long_mode(vcpu)) |
| 2532 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic; |
| 2533 | else |
| 2534 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic; |
| 2535 | } else { |
| 2536 | if (is_long_mode(vcpu)) |
| 2537 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv_inactive; |
| 2538 | else |
| 2539 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv_inactive; |
| 2540 | } |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2541 | } else { |
| 2542 | if (is_long_mode(vcpu)) |
| 2543 | msr_bitmap = vmx_msr_bitmap_longmode; |
| 2544 | else |
| 2545 | msr_bitmap = vmx_msr_bitmap_legacy; |
| 2546 | } |
| 2547 | |
| 2548 | vmcs_write64(MSR_BITMAP, __pa(msr_bitmap)); |
| 2549 | } |
| 2550 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2551 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2552 | * Set up the vmcs to automatically save and restore system |
| 2553 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 2554 | * mode, as fiddling with msrs is very expensive. |
| 2555 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2556 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2557 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2558 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2559 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2560 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2561 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2562 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2563 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2564 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2565 | move_msr_up(vmx, index, save_nmsrs++); |
| 2566 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2567 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2568 | move_msr_up(vmx, index, save_nmsrs++); |
| 2569 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2570 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2571 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2572 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 2573 | if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2574 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2575 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2576 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2577 | * if efer.sce is enabled. |
| 2578 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2579 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2580 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2581 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2582 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2583 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2584 | index = __find_msr_index(vmx, MSR_EFER); |
| 2585 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2586 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2587 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2588 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 2589 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2590 | if (cpu_has_vmx_msr_bitmap()) |
| 2591 | vmx_set_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2595 | * reads and returns guest's timestamp counter "register" |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2596 | * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset |
| 2597 | * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2598 | */ |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2599 | static u64 guest_read_tsc(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2600 | { |
| 2601 | u64 host_tsc, tsc_offset; |
| 2602 | |
Andy Lutomirski | 4ea1636 | 2015-06-25 18:44:07 +0200 | [diff] [blame] | 2603 | host_tsc = rdtsc(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2604 | tsc_offset = vmcs_read64(TSC_OFFSET); |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2605 | return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2609 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2610 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2611 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2612 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2613 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2614 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2615 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 2616 | * to the spec, this should set L1's TSC; The offset that L1 |
| 2617 | * set for L2 remains unchanged, and still needs to be added |
| 2618 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2619 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2620 | struct vmcs12 *vmcs12; |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2621 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 2622 | vmcs12 = get_vmcs12(vcpu); |
| 2623 | vmcs_write64(TSC_OFFSET, offset + |
| 2624 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 2625 | vmcs12->tsc_offset : 0)); |
| 2626 | } else { |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 2627 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 2628 | vmcs_read64(TSC_OFFSET), offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2629 | vmcs_write64(TSC_OFFSET, offset); |
| 2630 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2631 | } |
| 2632 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 2633 | static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu) |
| 2634 | { |
| 2635 | struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 2636 | return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31))); |
| 2637 | } |
| 2638 | |
| 2639 | /* |
| 2640 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 2641 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 2642 | * all guests if the "nested" module option is off, and can also be disabled |
| 2643 | * for a single guest by disabling its VMX cpuid bit. |
| 2644 | */ |
| 2645 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 2646 | { |
| 2647 | return nested && guest_cpuid_has_vmx(vcpu); |
| 2648 | } |
| 2649 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2650 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2651 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 2652 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 2653 | * The same values should also be used to verify that vmcs12 control fields are |
| 2654 | * valid during nested entry from L1 to L2. |
| 2655 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 2656 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 2657 | * bit in the high half is on if the corresponding bit in the control field |
| 2658 | * may be on. See also vmx_control_verify(). |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2659 | */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2660 | static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx) |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2661 | { |
| 2662 | /* |
| 2663 | * Note that as a general rule, the high half of the MSRs (bits in |
| 2664 | * the control fields which may be 1) should be initialized by the |
| 2665 | * intersection of the underlying hardware's MSR (i.e., features which |
| 2666 | * can be supported) and the list of features we want to expose - |
| 2667 | * because they are known to be properly supported in our code. |
| 2668 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 2669 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 2670 | * reason is that if one of these bits is necessary, it will appear |
| 2671 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 2672 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
| 2673 | * nested_vmx_exit_handled() will not pass related exits to L1. |
| 2674 | * These rules have exceptions below. |
| 2675 | */ |
| 2676 | |
| 2677 | /* pin-based controls */ |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 2678 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2679 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 2680 | vmx->nested.nested_vmx_pinbased_ctls_high); |
| 2681 | vmx->nested.nested_vmx_pinbased_ctls_low |= |
| 2682 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2683 | vmx->nested.nested_vmx_pinbased_ctls_high &= |
| 2684 | PIN_BASED_EXT_INTR_MASK | |
| 2685 | PIN_BASED_NMI_EXITING | |
| 2686 | PIN_BASED_VIRTUAL_NMIS; |
| 2687 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
| 2688 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 2689 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 2690 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 2691 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
| 2692 | PIN_BASED_POSTED_INTR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2693 | |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 2694 | /* exit controls */ |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2695 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2696 | vmx->nested.nested_vmx_exit_ctls_low, |
| 2697 | vmx->nested.nested_vmx_exit_ctls_high); |
| 2698 | vmx->nested.nested_vmx_exit_ctls_low = |
| 2699 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2700 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2701 | vmx->nested.nested_vmx_exit_ctls_high &= |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2702 | #ifdef CONFIG_X86_64 |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2703 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2704 | #endif |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2705 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2706 | vmx->nested.nested_vmx_exit_ctls_high |= |
| 2707 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2708 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2709 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 2710 | |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 2711 | if (kvm_mpx_supported()) |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2712 | vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2713 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2714 | /* We support free control of debug control saving. */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2715 | vmx->nested.nested_vmx_true_exit_ctls_low = |
| 2716 | vmx->nested.nested_vmx_exit_ctls_low & |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2717 | ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
| 2718 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2719 | /* entry controls */ |
| 2720 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2721 | vmx->nested.nested_vmx_entry_ctls_low, |
| 2722 | vmx->nested.nested_vmx_entry_ctls_high); |
| 2723 | vmx->nested.nested_vmx_entry_ctls_low = |
| 2724 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2725 | vmx->nested.nested_vmx_entry_ctls_high &= |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 2726 | #ifdef CONFIG_X86_64 |
| 2727 | VM_ENTRY_IA32E_MODE | |
| 2728 | #endif |
| 2729 | VM_ENTRY_LOAD_IA32_PAT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2730 | vmx->nested.nested_vmx_entry_ctls_high |= |
| 2731 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 2732 | if (kvm_mpx_supported()) |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2733 | vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 2734 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2735 | /* We support free control of debug control loading. */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2736 | vmx->nested.nested_vmx_true_entry_ctls_low = |
| 2737 | vmx->nested.nested_vmx_entry_ctls_low & |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2738 | ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
| 2739 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2740 | /* cpu-based controls */ |
| 2741 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2742 | vmx->nested.nested_vmx_procbased_ctls_low, |
| 2743 | vmx->nested.nested_vmx_procbased_ctls_high); |
| 2744 | vmx->nested.nested_vmx_procbased_ctls_low = |
| 2745 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2746 | vmx->nested.nested_vmx_procbased_ctls_high &= |
Jan Kiszka | a294c9b | 2013-10-23 17:43:09 +0100 | [diff] [blame] | 2747 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 2748 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2749 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 2750 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 2751 | CPU_BASED_CR3_STORE_EXITING | |
| 2752 | #ifdef CONFIG_X86_64 |
| 2753 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 2754 | #endif |
| 2755 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 2756 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 2757 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 2758 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 2759 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2760 | /* |
| 2761 | * We can allow some features even when not supported by the |
| 2762 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 2763 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 2764 | * without MSR bitmaps. |
| 2765 | */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2766 | vmx->nested.nested_vmx_procbased_ctls_high |= |
| 2767 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 2768 | CPU_BASED_USE_MSR_BITMAPS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2769 | |
Jan Kiszka | 3dcdf3e | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2770 | /* We support free control of CR3 access interception. */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2771 | vmx->nested.nested_vmx_true_procbased_ctls_low = |
| 2772 | vmx->nested.nested_vmx_procbased_ctls_low & |
Jan Kiszka | 3dcdf3e | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2773 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 2774 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2775 | /* secondary cpu-based controls */ |
| 2776 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2777 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 2778 | vmx->nested.nested_vmx_secondary_ctls_high); |
| 2779 | vmx->nested.nested_vmx_secondary_ctls_low = 0; |
| 2780 | vmx->nested.nested_vmx_secondary_ctls_high &= |
Jan Kiszka | d6851fb | 2013-02-23 22:34:39 +0100 | [diff] [blame] | 2781 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 2782 | SECONDARY_EXEC_RDTSCP | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 2783 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 2784 | SECONDARY_EXEC_ENABLE_VPID | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 2785 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 2786 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 2787 | SECONDARY_EXEC_WBINVD_EXITING | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 2788 | SECONDARY_EXEC_XSAVES; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2789 | |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2790 | if (enable_ept) { |
| 2791 | /* nested EPT: emulate EPT also to L1 */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2792 | vmx->nested.nested_vmx_secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 2793 | SECONDARY_EXEC_ENABLE_EPT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2794 | vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
Jan Kiszka | d3134db | 2013-10-23 14:40:31 +0100 | [diff] [blame] | 2795 | VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 2796 | VMX_EPT_INVEPT_BIT; |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 2797 | if (cpu_has_vmx_ept_execute_only()) |
| 2798 | vmx->nested.nested_vmx_ept_caps |= |
| 2799 | VMX_EPT_EXECUTE_ONLY_BIT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2800 | vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept; |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 2801 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
| 2802 | VMX_EPT_EXTENT_CONTEXT_BIT; |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2803 | } else |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2804 | vmx->nested.nested_vmx_ept_caps = 0; |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2805 | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 2806 | /* |
| 2807 | * Old versions of KVM use the single-context version without |
| 2808 | * checking for support, so declare that it is supported even |
| 2809 | * though it is treated as global context. The alternative is |
| 2810 | * not failing the single-context invvpid, and it is worse. |
| 2811 | */ |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 2812 | if (enable_vpid) |
| 2813 | vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 2814 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 2815 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 2816 | else |
| 2817 | vmx->nested.nested_vmx_vpid_caps = 0; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 2818 | |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 2819 | if (enable_unrestricted_guest) |
| 2820 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 2821 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 2822 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2823 | /* miscellaneous data */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2824 | rdmsr(MSR_IA32_VMX_MISC, |
| 2825 | vmx->nested.nested_vmx_misc_low, |
| 2826 | vmx->nested.nested_vmx_misc_high); |
| 2827 | vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 2828 | vmx->nested.nested_vmx_misc_low |= |
| 2829 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2830 | VMX_MISC_ACTIVITY_HLT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2831 | vmx->nested.nested_vmx_misc_high = 0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 2835 | { |
| 2836 | /* |
| 2837 | * Bits 0 in high must be 0, and bits 1 in low must be 1. |
| 2838 | */ |
| 2839 | return ((control & high) | low) == control; |
| 2840 | } |
| 2841 | |
| 2842 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 2843 | { |
| 2844 | return low | ((u64)high << 32); |
| 2845 | } |
| 2846 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 2847 | /* Returns 0 on success, non-0 otherwise. */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2848 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 2849 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2850 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2851 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2852 | switch (msr_index) { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2853 | case MSR_IA32_VMX_BASIC: |
| 2854 | /* |
| 2855 | * This MSR reports some information about VMX support. We |
| 2856 | * should return information about the VMX we emulate for the |
| 2857 | * guest, and the VMCS structure we give it - not about the |
| 2858 | * VMX support of the underlying hardware. |
| 2859 | */ |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 2860 | *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2861 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 2862 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 2863 | if (cpu_has_vmx_basic_inout()) |
| 2864 | *pdata |= VMX_BASIC_INOUT; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2865 | break; |
| 2866 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 2867 | case MSR_IA32_VMX_PINBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2868 | *pdata = vmx_control_msr( |
| 2869 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 2870 | vmx->nested.nested_vmx_pinbased_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2871 | break; |
| 2872 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2873 | *pdata = vmx_control_msr( |
| 2874 | vmx->nested.nested_vmx_true_procbased_ctls_low, |
| 2875 | vmx->nested.nested_vmx_procbased_ctls_high); |
Jan Kiszka | 3dcdf3e | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2876 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2877 | case MSR_IA32_VMX_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2878 | *pdata = vmx_control_msr( |
| 2879 | vmx->nested.nested_vmx_procbased_ctls_low, |
| 2880 | vmx->nested.nested_vmx_procbased_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2881 | break; |
| 2882 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2883 | *pdata = vmx_control_msr( |
| 2884 | vmx->nested.nested_vmx_true_exit_ctls_low, |
| 2885 | vmx->nested.nested_vmx_exit_ctls_high); |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2886 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2887 | case MSR_IA32_VMX_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2888 | *pdata = vmx_control_msr( |
| 2889 | vmx->nested.nested_vmx_exit_ctls_low, |
| 2890 | vmx->nested.nested_vmx_exit_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2891 | break; |
| 2892 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2893 | *pdata = vmx_control_msr( |
| 2894 | vmx->nested.nested_vmx_true_entry_ctls_low, |
| 2895 | vmx->nested.nested_vmx_entry_ctls_high); |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2896 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2897 | case MSR_IA32_VMX_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2898 | *pdata = vmx_control_msr( |
| 2899 | vmx->nested.nested_vmx_entry_ctls_low, |
| 2900 | vmx->nested.nested_vmx_entry_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2901 | break; |
| 2902 | case MSR_IA32_VMX_MISC: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2903 | *pdata = vmx_control_msr( |
| 2904 | vmx->nested.nested_vmx_misc_low, |
| 2905 | vmx->nested.nested_vmx_misc_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2906 | break; |
| 2907 | /* |
| 2908 | * These MSRs specify bits which the guest must keep fixed (on or off) |
| 2909 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 2910 | * We picked the standard core2 setting. |
| 2911 | */ |
| 2912 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 2913 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 2914 | case MSR_IA32_VMX_CR0_FIXED0: |
| 2915 | *pdata = VMXON_CR0_ALWAYSON; |
| 2916 | break; |
| 2917 | case MSR_IA32_VMX_CR0_FIXED1: |
| 2918 | *pdata = -1ULL; |
| 2919 | break; |
| 2920 | case MSR_IA32_VMX_CR4_FIXED0: |
| 2921 | *pdata = VMXON_CR4_ALWAYSON; |
| 2922 | break; |
| 2923 | case MSR_IA32_VMX_CR4_FIXED1: |
| 2924 | *pdata = -1ULL; |
| 2925 | break; |
| 2926 | case MSR_IA32_VMX_VMCS_ENUM: |
Jan Kiszka | 5381417 | 2014-06-16 13:59:44 +0200 | [diff] [blame] | 2927 | *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2928 | break; |
| 2929 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2930 | *pdata = vmx_control_msr( |
| 2931 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 2932 | vmx->nested.nested_vmx_secondary_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2933 | break; |
| 2934 | case MSR_IA32_VMX_EPT_VPID_CAP: |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 2935 | *pdata = vmx->nested.nested_vmx_ept_caps | |
| 2936 | ((u64)vmx->nested.nested_vmx_vpid_caps << 32); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2937 | break; |
| 2938 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2939 | return 1; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 2940 | } |
| 2941 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2942 | return 0; |
| 2943 | } |
| 2944 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 2945 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 2946 | uint64_t val) |
| 2947 | { |
| 2948 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 2949 | |
| 2950 | return !(val & ~valid_bits); |
| 2951 | } |
| 2952 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2953 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2954 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 2955 | * Returns 0 on success, non-0 otherwise. |
| 2956 | * Assumes vcpu_load() was already called. |
| 2957 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2958 | static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2959 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2960 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2961 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2962 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2963 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2964 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2965 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2966 | break; |
| 2967 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2968 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2969 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2970 | case MSR_KERNEL_GS_BASE: |
| 2971 | vmx_load_host_state(to_vmx(vcpu)); |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2972 | msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2973 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2974 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2975 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2976 | return kvm_get_msr_common(vcpu, msr_info); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 2977 | case MSR_IA32_TSC: |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2978 | msr_info->data = guest_read_tsc(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2979 | break; |
| 2980 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2981 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2982 | break; |
| 2983 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2984 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2985 | break; |
| 2986 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2987 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2988 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 2989 | case MSR_IA32_BNDCFGS: |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 2990 | if (!kvm_mpx_supported()) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 2991 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 2992 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 2993 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 2994 | case MSR_IA32_MCG_EXT_CTL: |
| 2995 | if (!msr_info->host_initiated && |
| 2996 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 2997 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 2998 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 2999 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 3000 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3001 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3002 | msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3003 | break; |
| 3004 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3005 | if (!nested_vmx_allowed(vcpu)) |
| 3006 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3007 | return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3008 | case MSR_IA32_XSS: |
| 3009 | if (!vmx_xsaves_supported()) |
| 3010 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3011 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3012 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3013 | case MSR_TSC_AUX: |
Haozhong Zhang | 81b1b9c | 2015-12-14 23:13:38 +0800 | [diff] [blame] | 3014 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3015 | return 1; |
| 3016 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3017 | default: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3018 | msr = find_msr_entry(to_vmx(vcpu), msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3019 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3020 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3021 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3022 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3023 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3024 | } |
| 3025 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3026 | return 0; |
| 3027 | } |
| 3028 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3029 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
| 3030 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3031 | /* |
| 3032 | * Writes msr value into into the appropriate "register". |
| 3033 | * Returns 0 on success, non-0 otherwise. |
| 3034 | * Assumes vcpu_load() was already called. |
| 3035 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3036 | 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] | 3037 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3038 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3039 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3040 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3041 | u32 msr_index = msr_info->index; |
| 3042 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3043 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3044 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3045 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3046 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3047 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 3048 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3049 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3050 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3051 | vmcs_writel(GUEST_FS_BASE, data); |
| 3052 | break; |
| 3053 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3054 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3055 | vmcs_writel(GUEST_GS_BASE, data); |
| 3056 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3057 | case MSR_KERNEL_GS_BASE: |
| 3058 | vmx_load_host_state(vmx); |
| 3059 | vmx->msr_guest_kernel_gs_base = data; |
| 3060 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3061 | #endif |
| 3062 | case MSR_IA32_SYSENTER_CS: |
| 3063 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 3064 | break; |
| 3065 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3066 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3067 | break; |
| 3068 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3069 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3070 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3071 | case MSR_IA32_BNDCFGS: |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3072 | if (!kvm_mpx_supported()) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3073 | return 1; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3074 | vmcs_write64(GUEST_BNDCFGS, data); |
| 3075 | break; |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 3076 | case MSR_IA32_TSC: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3077 | kvm_write_tsc(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3078 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3079 | case MSR_IA32_CR_PAT: |
| 3080 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 3081 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 3082 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3083 | vmcs_write64(GUEST_IA32_PAT, data); |
| 3084 | vcpu->arch.pat = data; |
| 3085 | break; |
| 3086 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3087 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3088 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 3089 | case MSR_IA32_TSC_ADJUST: |
| 3090 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3091 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3092 | case MSR_IA32_MCG_EXT_CTL: |
| 3093 | if ((!msr_info->host_initiated && |
| 3094 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 3095 | FEATURE_CONTROL_LMCE)) || |
| 3096 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 3097 | return 1; |
| 3098 | vcpu->arch.mcg_ext_ctl = data; |
| 3099 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3100 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3101 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3102 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3103 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 3104 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3105 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3106 | if (msr_info->host_initiated && data == 0) |
| 3107 | vmx_leave_nested(vcpu); |
| 3108 | break; |
| 3109 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3110 | return 1; /* they are read-only */ |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3111 | case MSR_IA32_XSS: |
| 3112 | if (!vmx_xsaves_supported()) |
| 3113 | return 1; |
| 3114 | /* |
| 3115 | * The only supported bit as of Skylake is bit 8, but |
| 3116 | * it is not supported on KVM. |
| 3117 | */ |
| 3118 | if (data != 0) |
| 3119 | return 1; |
| 3120 | vcpu->arch.ia32_xss = data; |
| 3121 | if (vcpu->arch.ia32_xss != host_xss) |
| 3122 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
| 3123 | vcpu->arch.ia32_xss, host_xss); |
| 3124 | else |
| 3125 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 3126 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3127 | case MSR_TSC_AUX: |
Haozhong Zhang | 81b1b9c | 2015-12-14 23:13:38 +0800 | [diff] [blame] | 3128 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3129 | return 1; |
| 3130 | /* Check reserved bit, higher 32 bits should be zero */ |
| 3131 | if ((data >> 32) != 0) |
| 3132 | return 1; |
| 3133 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3134 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3135 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3136 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3137 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3138 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3139 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 3140 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3141 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 3142 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3143 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3144 | if (ret) |
| 3145 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3146 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3147 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3148 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3149 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3150 | } |
| 3151 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3152 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3153 | } |
| 3154 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3155 | 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] | 3156 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3157 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 3158 | switch (reg) { |
| 3159 | case VCPU_REGS_RSP: |
| 3160 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 3161 | break; |
| 3162 | case VCPU_REGS_RIP: |
| 3163 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 3164 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3165 | case VCPU_EXREG_PDPTR: |
| 3166 | if (enable_ept) |
| 3167 | ept_save_pdptrs(vcpu); |
| 3168 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3169 | default: |
| 3170 | break; |
| 3171 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3172 | } |
| 3173 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3174 | static __init int cpu_has_kvm_support(void) |
| 3175 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 3176 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | static __init int vmx_disabled_by_bios(void) |
| 3180 | { |
| 3181 | u64 msr; |
| 3182 | |
| 3183 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3184 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3185 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3186 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 3187 | && tboot_enabled()) |
| 3188 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3189 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3190 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3191 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 3192 | && !tboot_enabled()) { |
| 3193 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3194 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3195 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 3196 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3197 | /* launched w/o TXT and VMX disabled */ |
| 3198 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 3199 | && !tboot_enabled()) |
| 3200 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3204 | } |
| 3205 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 3206 | static void kvm_cpu_vmxon(u64 addr) |
| 3207 | { |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 3208 | intel_pt_handle_vmx(1); |
| 3209 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 3210 | asm volatile (ASM_VMX_VMXON_RAX |
| 3211 | : : "a"(&addr), "m"(addr) |
| 3212 | : "memory", "cc"); |
| 3213 | } |
| 3214 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 3215 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3216 | { |
| 3217 | int cpu = raw_smp_processor_id(); |
| 3218 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3219 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3220 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 3221 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3222 | return -EBUSY; |
| 3223 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3224 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 3225 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 3226 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 3227 | |
| 3228 | /* |
| 3229 | * Now we can enable the vmclear operation in kdump |
| 3230 | * since the loaded_vmcss_on_cpu list on this cpu |
| 3231 | * has been initialized. |
| 3232 | * |
| 3233 | * Though the cpu is not in VMX operation now, there |
| 3234 | * is no problem to enable the vmclear operation |
| 3235 | * for the loaded_vmcss_on_cpu list is empty! |
| 3236 | */ |
| 3237 | crash_enable_local_vmclear(cpu); |
| 3238 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3239 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3240 | |
| 3241 | test_bits = FEATURE_CONTROL_LOCKED; |
| 3242 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 3243 | if (tboot_enabled()) |
| 3244 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 3245 | |
| 3246 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3247 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3248 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 3249 | } |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 3250 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3251 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 3252 | if (vmm_exclusive) { |
| 3253 | kvm_cpu_vmxon(phys_addr); |
| 3254 | ept_sync_global(); |
| 3255 | } |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3256 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 3257 | native_store_gdt(this_cpu_ptr(&host_gdt)); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 3258 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3259 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3260 | } |
| 3261 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3262 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3263 | { |
| 3264 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3265 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3266 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3267 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 3268 | loaded_vmcss_on_cpu_link) |
| 3269 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3270 | } |
| 3271 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 3272 | |
| 3273 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 3274 | * tricks. |
| 3275 | */ |
| 3276 | static void kvm_cpu_vmxoff(void) |
| 3277 | { |
| 3278 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 3279 | |
| 3280 | intel_pt_handle_vmx(0); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 3281 | } |
| 3282 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 3283 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3284 | { |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 3285 | if (vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3286 | vmclear_local_loaded_vmcss(); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 3287 | kvm_cpu_vmxoff(); |
| 3288 | } |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 3289 | cr4_clear_bits(X86_CR4_VMXE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3290 | } |
| 3291 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3292 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3293 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3294 | { |
| 3295 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3296 | u32 ctl = ctl_min | ctl_opt; |
| 3297 | |
| 3298 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 3299 | |
| 3300 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 3301 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 3302 | |
| 3303 | /* Ensure minimum (required) set of control bits are supported. */ |
| 3304 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3305 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3306 | |
| 3307 | *result = ctl; |
| 3308 | return 0; |
| 3309 | } |
| 3310 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 3311 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 3312 | { |
| 3313 | u32 vmx_msr_low, vmx_msr_high; |
| 3314 | |
| 3315 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 3316 | return vmx_msr_high & ctl; |
| 3317 | } |
| 3318 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3319 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3320 | { |
| 3321 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3322 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3323 | u32 _pin_based_exec_control = 0; |
| 3324 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3325 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3326 | u32 _vmexit_control = 0; |
| 3327 | u32 _vmentry_control = 0; |
| 3328 | |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 3329 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3330 | #ifdef CONFIG_X86_64 |
| 3331 | CPU_BASED_CR8_LOAD_EXITING | |
| 3332 | CPU_BASED_CR8_STORE_EXITING | |
| 3333 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3334 | CPU_BASED_CR3_LOAD_EXITING | |
| 3335 | CPU_BASED_CR3_STORE_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3336 | CPU_BASED_USE_IO_BITMAPS | |
| 3337 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3338 | CPU_BASED_USE_TSC_OFFSETING | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 3339 | CPU_BASED_MWAIT_EXITING | |
| 3340 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 3341 | CPU_BASED_INVLPG_EXITING | |
| 3342 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 3343 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3344 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3345 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3346 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3347 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 3348 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3349 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3350 | #ifdef CONFIG_X86_64 |
| 3351 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 3352 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 3353 | ~CPU_BASED_CR8_STORE_EXITING; |
| 3354 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3355 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3356 | min2 = 0; |
| 3357 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3358 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3359 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3360 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3361 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3362 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3363 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3364 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3365 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3366 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 3367 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3368 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 3369 | SECONDARY_EXEC_XSAVES | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 3370 | SECONDARY_EXEC_ENABLE_PML | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 3371 | SECONDARY_EXEC_TSC_SCALING; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3372 | if (adjust_vmx_controls(min2, opt2, |
| 3373 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3374 | &_cpu_based_2nd_exec_control) < 0) |
| 3375 | return -EIO; |
| 3376 | } |
| 3377 | #ifndef CONFIG_X86_64 |
| 3378 | if (!(_cpu_based_2nd_exec_control & |
| 3379 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 3380 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 3381 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3382 | |
| 3383 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 3384 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3385 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3386 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 3387 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3388 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3389 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3390 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 3391 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 3392 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 3393 | CPU_BASED_CR3_STORE_EXITING | |
| 3394 | CPU_BASED_INVLPG_EXITING); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3395 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
| 3396 | vmx_capability.ept, vmx_capability.vpid); |
| 3397 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3398 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3399 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3400 | #ifdef CONFIG_X86_64 |
| 3401 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 3402 | #endif |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 3403 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3404 | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3405 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 3406 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3407 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3408 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3409 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 3410 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | |
| 3411 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3412 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 3413 | &_pin_based_exec_control) < 0) |
| 3414 | return -EIO; |
| 3415 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 3416 | if (cpu_has_broken_vmx_preemption_timer()) |
| 3417 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3418 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3419 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3420 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 3421 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 3422 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 3423 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3424 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 3425 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3426 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3427 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 3428 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3429 | |
| 3430 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 3431 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3432 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3433 | |
| 3434 | #ifdef CONFIG_X86_64 |
| 3435 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 3436 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3437 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3438 | #endif |
| 3439 | |
| 3440 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 3441 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3442 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3443 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3444 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 3445 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 3446 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3447 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3448 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3449 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 3450 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3451 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3452 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 3453 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3454 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 3455 | cpu_has_load_ia32_efer = |
| 3456 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 3457 | VM_ENTRY_LOAD_IA32_EFER) |
| 3458 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 3459 | VM_EXIT_LOAD_IA32_EFER); |
| 3460 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 3461 | cpu_has_load_perf_global_ctrl = |
| 3462 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 3463 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 3464 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 3465 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 3466 | |
| 3467 | /* |
| 3468 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 3469 | * but due to errata below it can't be used. Workaround is to use |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 3470 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 3471 | * |
| 3472 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 3473 | * |
| 3474 | * AAK155 (model 26) |
| 3475 | * AAP115 (model 30) |
| 3476 | * AAT100 (model 37) |
| 3477 | * BC86,AAY89,BD102 (model 44) |
| 3478 | * BA97 (model 46) |
| 3479 | * |
| 3480 | */ |
| 3481 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 3482 | switch (boot_cpu_data.x86_model) { |
| 3483 | case 26: |
| 3484 | case 30: |
| 3485 | case 37: |
| 3486 | case 44: |
| 3487 | case 46: |
| 3488 | cpu_has_load_perf_global_ctrl = false; |
| 3489 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 3490 | "does not work properly. Using workaround\n"); |
| 3491 | break; |
| 3492 | default: |
| 3493 | break; |
| 3494 | } |
| 3495 | } |
| 3496 | |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 3497 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3498 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 3499 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3500 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 3501 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3502 | |
| 3503 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 3504 | { |
| 3505 | int node = cpu_to_node(cpu); |
| 3506 | struct page *pages; |
| 3507 | struct vmcs *vmcs; |
| 3508 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 3509 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3510 | if (!pages) |
| 3511 | return NULL; |
| 3512 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3513 | memset(vmcs, 0, vmcs_config.size); |
| 3514 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3515 | return vmcs; |
| 3516 | } |
| 3517 | |
| 3518 | static struct vmcs *alloc_vmcs(void) |
| 3519 | { |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3520 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | static void free_vmcs(struct vmcs *vmcs) |
| 3524 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3525 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3526 | } |
| 3527 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3528 | /* |
| 3529 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 3530 | */ |
| 3531 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 3532 | { |
| 3533 | if (!loaded_vmcs->vmcs) |
| 3534 | return; |
| 3535 | loaded_vmcs_clear(loaded_vmcs); |
| 3536 | free_vmcs(loaded_vmcs->vmcs); |
| 3537 | loaded_vmcs->vmcs = NULL; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 3538 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3539 | } |
| 3540 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 3541 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3542 | { |
| 3543 | int cpu; |
| 3544 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3545 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3546 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3547 | per_cpu(vmxarea, cpu) = NULL; |
| 3548 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3549 | } |
| 3550 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 3551 | static void init_vmcs_shadow_fields(void) |
| 3552 | { |
| 3553 | int i, j; |
| 3554 | |
| 3555 | /* No checks for read only fields yet */ |
| 3556 | |
| 3557 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
| 3558 | switch (shadow_read_write_fields[i]) { |
| 3559 | case GUEST_BNDCFGS: |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3560 | if (!kvm_mpx_supported()) |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 3561 | continue; |
| 3562 | break; |
| 3563 | default: |
| 3564 | break; |
| 3565 | } |
| 3566 | |
| 3567 | if (j < i) |
| 3568 | shadow_read_write_fields[j] = |
| 3569 | shadow_read_write_fields[i]; |
| 3570 | j++; |
| 3571 | } |
| 3572 | max_shadow_read_write_fields = j; |
| 3573 | |
| 3574 | /* shadowed fields guest access without vmexit */ |
| 3575 | for (i = 0; i < max_shadow_read_write_fields; i++) { |
| 3576 | clear_bit(shadow_read_write_fields[i], |
| 3577 | vmx_vmwrite_bitmap); |
| 3578 | clear_bit(shadow_read_write_fields[i], |
| 3579 | vmx_vmread_bitmap); |
| 3580 | } |
| 3581 | for (i = 0; i < max_shadow_read_only_fields; i++) |
| 3582 | clear_bit(shadow_read_only_fields[i], |
| 3583 | vmx_vmread_bitmap); |
| 3584 | } |
| 3585 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3586 | static __init int alloc_kvm_area(void) |
| 3587 | { |
| 3588 | int cpu; |
| 3589 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3590 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3591 | struct vmcs *vmcs; |
| 3592 | |
| 3593 | vmcs = alloc_vmcs_cpu(cpu); |
| 3594 | if (!vmcs) { |
| 3595 | free_kvm_area(); |
| 3596 | return -ENOMEM; |
| 3597 | } |
| 3598 | |
| 3599 | per_cpu(vmxarea, cpu) = vmcs; |
| 3600 | } |
| 3601 | return 0; |
| 3602 | } |
| 3603 | |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 3604 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 3605 | { |
| 3606 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 3607 | } |
| 3608 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 3609 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3610 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3611 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3612 | if (!emulate_invalid_guest_state) { |
| 3613 | /* |
| 3614 | * CS and SS RPL should be equal during guest entry according |
| 3615 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 3616 | * is in the middle of the transition from real mode to |
| 3617 | * protected mode it is safe to assume that RPL 0 is a good |
| 3618 | * default value. |
| 3619 | */ |
| 3620 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 3621 | save->selector &= ~SEGMENT_RPL_MASK; |
| 3622 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3623 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3624 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3625 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3626 | } |
| 3627 | |
| 3628 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 3629 | { |
| 3630 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3631 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3632 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3633 | /* |
| 3634 | * Update real mode segment cache. It may be not up-to-date if sement |
| 3635 | * register was written while vcpu was in a guest mode. |
| 3636 | */ |
| 3637 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 3638 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 3639 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 3640 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 3641 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 3642 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 3643 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3644 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3645 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3646 | vmx_segment_cache_clear(vmx); |
| 3647 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3648 | 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] | 3649 | |
| 3650 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3651 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 3652 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3653 | vmcs_writel(GUEST_RFLAGS, flags); |
| 3654 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 3655 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 3656 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3657 | |
| 3658 | update_exception_bitmap(vcpu); |
| 3659 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 3660 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 3661 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 3662 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 3663 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 3664 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 3665 | 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] | 3666 | } |
| 3667 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3668 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3669 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 3670 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3671 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3672 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3673 | var.dpl = 0x3; |
| 3674 | if (seg == VCPU_SREG_CS) |
| 3675 | var.type = 0x3; |
| 3676 | |
| 3677 | if (!emulate_invalid_guest_state) { |
| 3678 | var.selector = var.base >> 4; |
| 3679 | var.base = var.base & 0xffff0; |
| 3680 | var.limit = 0xffff; |
| 3681 | var.g = 0; |
| 3682 | var.db = 0; |
| 3683 | var.present = 1; |
| 3684 | var.s = 1; |
| 3685 | var.l = 0; |
| 3686 | var.unusable = 0; |
| 3687 | var.type = 0x3; |
| 3688 | var.avl = 0; |
| 3689 | if (save->base & 0xf) |
| 3690 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 3691 | "paragraph aligned when entering " |
| 3692 | "protected mode (seg=%d)", seg); |
| 3693 | } |
| 3694 | |
| 3695 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 7c3bab1 | 2017-02-21 03:50:01 -0500 | [diff] [blame^] | 3696 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3697 | vmcs_write32(sf->limit, var.limit); |
| 3698 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 3702 | { |
| 3703 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3704 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3705 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3706 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 3707 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 3708 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 3709 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 3710 | 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] | 3711 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 3712 | 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] | 3713 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3714 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3715 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3716 | /* |
| 3717 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3718 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3719 | */ |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3720 | if (!vcpu->kvm->arch.tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3721 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 3722 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3723 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3724 | vmx_segment_cache_clear(vmx); |
| 3725 | |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3726 | vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3727 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3728 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 3729 | |
| 3730 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3731 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3732 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 3733 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3734 | |
| 3735 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 3736 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3737 | update_exception_bitmap(vcpu); |
| 3738 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3739 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 3740 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 3741 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 3742 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 3743 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 3744 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3745 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 3746 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3747 | } |
| 3748 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3749 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 3750 | { |
| 3751 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3752 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 3753 | |
| 3754 | if (!msr) |
| 3755 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3756 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3757 | /* |
| 3758 | * Force kernel_gs_base reloading before EFER changes, as control |
| 3759 | * of this msr depends on is_long_mode(). |
| 3760 | */ |
| 3761 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3762 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3763 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3764 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3765 | msr->data = efer; |
| 3766 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3767 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3768 | |
| 3769 | msr->data = efer & ~EFER_LME; |
| 3770 | } |
| 3771 | setup_msrs(vmx); |
| 3772 | } |
| 3773 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3774 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3775 | |
| 3776 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 3777 | { |
| 3778 | u32 guest_tr_ar; |
| 3779 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3780 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 3781 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3782 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 3783 | if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 3784 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 3785 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3786 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 3787 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 3788 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3789 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 3790 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3791 | } |
| 3792 | |
| 3793 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 3794 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3795 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 3796 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | #endif |
| 3800 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 3801 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3802 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 3803 | vpid_sync_context(vpid); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 3804 | if (enable_ept) { |
| 3805 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 3806 | return; |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 3807 | ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 3808 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3809 | } |
| 3810 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 3811 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 3812 | { |
| 3813 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid); |
| 3814 | } |
| 3815 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 3816 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 3817 | { |
| 3818 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 3819 | |
| 3820 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 3821 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 3822 | } |
| 3823 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 3824 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 3825 | { |
| 3826 | if (enable_ept && is_paging(vcpu)) |
| 3827 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 3828 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 3829 | } |
| 3830 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 3831 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 3832 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3833 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 3834 | |
| 3835 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 3836 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 3837 | } |
| 3838 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3839 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 3840 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3841 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 3842 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3843 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 3844 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 3845 | return; |
| 3846 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3847 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3848 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 3849 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 3850 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 3851 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3852 | } |
| 3853 | } |
| 3854 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3855 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 3856 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3857 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 3858 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3859 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 3860 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 3861 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 3862 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 3863 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3864 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3865 | |
| 3866 | __set_bit(VCPU_EXREG_PDPTR, |
| 3867 | (unsigned long *)&vcpu->arch.regs_avail); |
| 3868 | __set_bit(VCPU_EXREG_PDPTR, |
| 3869 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 3870 | } |
| 3871 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3872 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3873 | |
| 3874 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 3875 | unsigned long cr0, |
| 3876 | struct kvm_vcpu *vcpu) |
| 3877 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 3878 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 3879 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3880 | if (!(cr0 & X86_CR0_PG)) { |
| 3881 | /* From paging/starting to nonpaging */ |
| 3882 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 3883 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3884 | (CPU_BASED_CR3_LOAD_EXITING | |
| 3885 | CPU_BASED_CR3_STORE_EXITING)); |
| 3886 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3887 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3888 | } else if (!is_paging(vcpu)) { |
| 3889 | /* From nonpaging to paging */ |
| 3890 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 3891 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3892 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 3893 | CPU_BASED_CR3_STORE_EXITING)); |
| 3894 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 3895 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3896 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 3897 | |
| 3898 | if (!(cr0 & X86_CR0_WP)) |
| 3899 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3900 | } |
| 3901 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3902 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 3903 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3904 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3905 | unsigned long hw_cr0; |
| 3906 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 3907 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3908 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 3909 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 3910 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 3911 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3912 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 3913 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 3914 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3915 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 3916 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 3917 | enter_rmode(vcpu); |
| 3918 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3919 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3920 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3921 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 3922 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3923 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 3924 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3925 | exit_lmode(vcpu); |
| 3926 | } |
| 3927 | #endif |
| 3928 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3929 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3930 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 3931 | |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3932 | if (!vcpu->fpu_active) |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 3933 | hw_cr0 |= X86_CR0_TS | X86_CR0_MP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 3934 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3935 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3936 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3937 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 3938 | |
| 3939 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 3940 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3941 | } |
| 3942 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3943 | static u64 construct_eptp(unsigned long root_hpa) |
| 3944 | { |
| 3945 | u64 eptp; |
| 3946 | |
| 3947 | /* TODO write the value reading from MSR */ |
| 3948 | eptp = VMX_EPT_DEFAULT_MT | |
| 3949 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; |
Xudong Hao | b38f993 | 2012-05-28 19:33:36 +0800 | [diff] [blame] | 3950 | if (enable_ept_ad_bits) |
| 3951 | eptp |= VMX_EPT_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3952 | eptp |= (root_hpa & PAGE_MASK); |
| 3953 | |
| 3954 | return eptp; |
| 3955 | } |
| 3956 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3957 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 3958 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3959 | unsigned long guest_cr3; |
| 3960 | u64 eptp; |
| 3961 | |
| 3962 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 3963 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3964 | eptp = construct_eptp(cr3); |
| 3965 | vmcs_write64(EPT_POINTER, eptp); |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 3966 | if (is_paging(vcpu) || is_guest_mode(vcpu)) |
| 3967 | guest_cr3 = kvm_read_cr3(vcpu); |
| 3968 | else |
| 3969 | guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr; |
Marcelo Tosatti | 7c93be4 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 3970 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3971 | } |
| 3972 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3973 | vmx_flush_tlb(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3974 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3975 | } |
| 3976 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3977 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3978 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 3979 | /* |
| 3980 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 3981 | * is in force while we are in guest mode. Do not let guests control |
| 3982 | * this bit, even if host CR4.MCE == 0. |
| 3983 | */ |
| 3984 | unsigned long hw_cr4 = |
| 3985 | (cr4_read_shadow() & X86_CR4_MCE) | |
| 3986 | (cr4 & ~X86_CR4_MCE) | |
| 3987 | (to_vmx(vcpu)->rmode.vm86_active ? |
| 3988 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 3989 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 3990 | if (cr4 & X86_CR4_VMXE) { |
| 3991 | /* |
| 3992 | * To use VMXON (and later other VMX instructions), a guest |
| 3993 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 3994 | * So basically the check on whether to allow nested VMX |
| 3995 | * is here. |
| 3996 | */ |
| 3997 | if (!nested_vmx_allowed(vcpu)) |
| 3998 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 3999 | } |
| 4000 | if (to_vmx(vcpu)->nested.vmxon && |
| 4001 | ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4002 | return 1; |
| 4003 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4004 | vcpu->arch.cr4 = cr4; |
Avi Kivity | bc23008 | 2009-12-08 12:14:42 +0200 | [diff] [blame] | 4005 | if (enable_ept) { |
| 4006 | if (!is_paging(vcpu)) { |
| 4007 | hw_cr4 &= ~X86_CR4_PAE; |
| 4008 | hw_cr4 |= X86_CR4_PSE; |
| 4009 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 4010 | hw_cr4 &= ~X86_CR4_PAE; |
| 4011 | } |
| 4012 | } |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4013 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4014 | if (!enable_unrestricted_guest && !is_paging(vcpu)) |
| 4015 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 4016 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 4017 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 4018 | * to be manually disabled when guest switches to non-paging |
| 4019 | * mode. |
| 4020 | * |
| 4021 | * If !enable_unrestricted_guest, the CPU is always running |
| 4022 | * with CR0.PG=1 and CR4 needs to be modified. |
| 4023 | * If enable_unrestricted_guest, the CPU automatically |
| 4024 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4025 | */ |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 4026 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4027 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4028 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 4029 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4030 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4031 | } |
| 4032 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4033 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 4034 | struct kvm_segment *var, int seg) |
| 4035 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4036 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4037 | u32 ar; |
| 4038 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 4039 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4040 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4041 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4042 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4043 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 4044 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 4045 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 4046 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4047 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4048 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 4049 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 4050 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 4051 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 4052 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4053 | var->type = ar & 15; |
| 4054 | var->s = (ar >> 4) & 1; |
| 4055 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 4056 | /* |
| 4057 | * Some userspaces do not preserve unusable property. Since usable |
| 4058 | * segment has to be present according to VMX spec we can use present |
| 4059 | * property to amend userspace bug by making unusable segment always |
| 4060 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 4061 | * segment as unusable. |
| 4062 | */ |
| 4063 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4064 | var->avl = (ar >> 12) & 1; |
| 4065 | var->l = (ar >> 13) & 1; |
| 4066 | var->db = (ar >> 14) & 1; |
| 4067 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4068 | } |
| 4069 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4070 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 4071 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4072 | struct kvm_segment s; |
| 4073 | |
| 4074 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 4075 | vmx_get_segment(vcpu, &s, seg); |
| 4076 | return s.base; |
| 4077 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4078 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4079 | } |
| 4080 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 4081 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4082 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 4083 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4084 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 4085 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4086 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 4087 | else { |
| 4088 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4089 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 4090 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 4091 | } |
| 4092 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4093 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4094 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4095 | u32 ar; |
| 4096 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 4097 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4098 | ar = 1 << 16; |
| 4099 | else { |
| 4100 | ar = var->type & 15; |
| 4101 | ar |= (var->s & 1) << 4; |
| 4102 | ar |= (var->dpl & 3) << 5; |
| 4103 | ar |= (var->present & 1) << 7; |
| 4104 | ar |= (var->avl & 1) << 12; |
| 4105 | ar |= (var->l & 1) << 13; |
| 4106 | ar |= (var->db & 1) << 14; |
| 4107 | ar |= (var->g & 1) << 15; |
| 4108 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4109 | |
| 4110 | return ar; |
| 4111 | } |
| 4112 | |
| 4113 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 4114 | struct kvm_segment *var, int seg) |
| 4115 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4116 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4117 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4118 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4119 | vmx_segment_cache_clear(vmx); |
| 4120 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 4121 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 4122 | vmx->rmode.segs[seg] = *var; |
| 4123 | if (seg == VCPU_SREG_TR) |
| 4124 | vmcs_write16(sf->selector, var->selector); |
| 4125 | else if (var->s) |
| 4126 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4127 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4128 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 4129 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4130 | vmcs_writel(sf->base, var->base); |
| 4131 | vmcs_write32(sf->limit, var->limit); |
| 4132 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4133 | |
| 4134 | /* |
| 4135 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 4136 | * qemu binaries. |
| 4137 | * IA32 arch specifies that at the time of processor reset the |
| 4138 | * "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] | 4139 | * 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] | 4140 | * state vmexit when "unrestricted guest" mode is turned on. |
| 4141 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 4142 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 4143 | * kvm hack. |
| 4144 | */ |
| 4145 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 4146 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4147 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 4148 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4149 | |
| 4150 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 4151 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4152 | } |
| 4153 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4154 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 4155 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4156 | 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] | 4157 | |
| 4158 | *db = (ar >> 14) & 1; |
| 4159 | *l = (ar >> 13) & 1; |
| 4160 | } |
| 4161 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4162 | 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] | 4163 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4164 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 4165 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4166 | } |
| 4167 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4168 | 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] | 4169 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4170 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 4171 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4172 | } |
| 4173 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4174 | 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] | 4175 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4176 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 4177 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4178 | } |
| 4179 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4180 | 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] | 4181 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4182 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 4183 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4184 | } |
| 4185 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4186 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 4187 | { |
| 4188 | struct kvm_segment var; |
| 4189 | u32 ar; |
| 4190 | |
| 4191 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 4192 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 4193 | if (seg == VCPU_SREG_CS) |
| 4194 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4195 | ar = vmx_segment_access_rights(&var); |
| 4196 | |
| 4197 | if (var.base != (var.selector << 4)) |
| 4198 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 4199 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4200 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 4201 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4202 | return false; |
| 4203 | |
| 4204 | return true; |
| 4205 | } |
| 4206 | |
| 4207 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 4208 | { |
| 4209 | struct kvm_segment cs; |
| 4210 | unsigned int cs_rpl; |
| 4211 | |
| 4212 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4213 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4214 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4215 | if (cs.unusable) |
| 4216 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4217 | if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK)) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4218 | return false; |
| 4219 | if (!cs.s) |
| 4220 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4221 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4222 | if (cs.dpl > cs_rpl) |
| 4223 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4224 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4225 | if (cs.dpl != cs_rpl) |
| 4226 | return false; |
| 4227 | } |
| 4228 | if (!cs.present) |
| 4229 | return false; |
| 4230 | |
| 4231 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 4232 | return true; |
| 4233 | } |
| 4234 | |
| 4235 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 4236 | { |
| 4237 | struct kvm_segment ss; |
| 4238 | unsigned int ss_rpl; |
| 4239 | |
| 4240 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4241 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4242 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4243 | if (ss.unusable) |
| 4244 | return true; |
| 4245 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4246 | return false; |
| 4247 | if (!ss.s) |
| 4248 | return false; |
| 4249 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 4250 | return false; |
| 4251 | if (!ss.present) |
| 4252 | return false; |
| 4253 | |
| 4254 | return true; |
| 4255 | } |
| 4256 | |
| 4257 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 4258 | { |
| 4259 | struct kvm_segment var; |
| 4260 | unsigned int rpl; |
| 4261 | |
| 4262 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4263 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4264 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4265 | if (var.unusable) |
| 4266 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4267 | if (!var.s) |
| 4268 | return false; |
| 4269 | if (!var.present) |
| 4270 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4271 | if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4272 | if (var.dpl < rpl) /* DPL < RPL */ |
| 4273 | return false; |
| 4274 | } |
| 4275 | |
| 4276 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 4277 | * rights flags |
| 4278 | */ |
| 4279 | return true; |
| 4280 | } |
| 4281 | |
| 4282 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 4283 | { |
| 4284 | struct kvm_segment tr; |
| 4285 | |
| 4286 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 4287 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4288 | if (tr.unusable) |
| 4289 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4290 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4291 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4292 | 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] | 4293 | return false; |
| 4294 | if (!tr.present) |
| 4295 | return false; |
| 4296 | |
| 4297 | return true; |
| 4298 | } |
| 4299 | |
| 4300 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 4301 | { |
| 4302 | struct kvm_segment ldtr; |
| 4303 | |
| 4304 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 4305 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4306 | if (ldtr.unusable) |
| 4307 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4308 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4309 | return false; |
| 4310 | if (ldtr.type != 2) |
| 4311 | return false; |
| 4312 | if (!ldtr.present) |
| 4313 | return false; |
| 4314 | |
| 4315 | return true; |
| 4316 | } |
| 4317 | |
| 4318 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 4319 | { |
| 4320 | struct kvm_segment cs, ss; |
| 4321 | |
| 4322 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 4323 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 4324 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4325 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 4326 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4327 | } |
| 4328 | |
| 4329 | /* |
| 4330 | * Check if guest state is valid. Returns true if valid, false if |
| 4331 | * not. |
| 4332 | * We assume that registers are always usable |
| 4333 | */ |
| 4334 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 4335 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 4336 | if (enable_unrestricted_guest) |
| 4337 | return true; |
| 4338 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4339 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 4340 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4341 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 4342 | return false; |
| 4343 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 4344 | return false; |
| 4345 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 4346 | return false; |
| 4347 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 4348 | return false; |
| 4349 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 4350 | return false; |
| 4351 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 4352 | return false; |
| 4353 | } else { |
| 4354 | /* protected mode guest state checks */ |
| 4355 | if (!cs_ss_rpl_check(vcpu)) |
| 4356 | return false; |
| 4357 | if (!code_segment_valid(vcpu)) |
| 4358 | return false; |
| 4359 | if (!stack_segment_valid(vcpu)) |
| 4360 | return false; |
| 4361 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 4362 | return false; |
| 4363 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 4364 | return false; |
| 4365 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 4366 | return false; |
| 4367 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 4368 | return false; |
| 4369 | if (!tr_valid(vcpu)) |
| 4370 | return false; |
| 4371 | if (!ldtr_valid(vcpu)) |
| 4372 | return false; |
| 4373 | } |
| 4374 | /* TODO: |
| 4375 | * - Add checks on RIP |
| 4376 | * - Add checks on RFLAGS |
| 4377 | */ |
| 4378 | |
| 4379 | return true; |
| 4380 | } |
| 4381 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 4382 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4383 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4384 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4385 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 4386 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4387 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4388 | idx = srcu_read_lock(&kvm->srcu); |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4389 | fn = kvm->arch.tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4390 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 4391 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4392 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4393 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 4394 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 4395 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4396 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4397 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4398 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 4399 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4400 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4401 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 4402 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4403 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4404 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4405 | r = kvm_write_guest_page(kvm, fn, &data, |
| 4406 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 4407 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4408 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4409 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 4410 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4411 | } |
| 4412 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4413 | static int init_rmode_identity_map(struct kvm *kvm) |
| 4414 | { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4415 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 4416 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4417 | u32 tmp; |
| 4418 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4419 | if (!enable_ept) |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4420 | return 0; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4421 | |
| 4422 | /* Protect kvm->arch.ept_identity_pagetable_done. */ |
| 4423 | mutex_lock(&kvm->slots_lock); |
| 4424 | |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4425 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4426 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4427 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4428 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4429 | |
| 4430 | r = alloc_identity_pagetable(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4431 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4432 | goto out2; |
| 4433 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4434 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4435 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 4436 | if (r < 0) |
| 4437 | goto out; |
| 4438 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 4439 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 4440 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 4441 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 4442 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 4443 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 4444 | if (r < 0) |
| 4445 | goto out; |
| 4446 | } |
| 4447 | kvm->arch.ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4448 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4449 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4450 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4451 | |
| 4452 | out2: |
| 4453 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4454 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4455 | } |
| 4456 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4457 | static void seg_setup(int seg) |
| 4458 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4459 | 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] | 4460 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4461 | |
| 4462 | vmcs_write16(sf->selector, 0); |
| 4463 | vmcs_writel(sf->base, 0); |
| 4464 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 4465 | ar = 0x93; |
| 4466 | if (seg == VCPU_SREG_CS) |
| 4467 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4468 | |
| 4469 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4470 | } |
| 4471 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4472 | static int alloc_apic_access_page(struct kvm *kvm) |
| 4473 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 4474 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4475 | int r = 0; |
| 4476 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4477 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 4478 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4479 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 4480 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 4481 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4482 | if (r) |
| 4483 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4484 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 4485 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 4486 | if (is_error_page(page)) { |
| 4487 | r = -EFAULT; |
| 4488 | goto out; |
| 4489 | } |
| 4490 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 4491 | /* |
| 4492 | * Do not pin the page in memory, so that memory hot-unplug |
| 4493 | * is able to migrate it. |
| 4494 | */ |
| 4495 | put_page(page); |
| 4496 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4497 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4498 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4499 | return r; |
| 4500 | } |
| 4501 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4502 | static int alloc_identity_pagetable(struct kvm *kvm) |
| 4503 | { |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4504 | /* Called with kvm->slots_lock held. */ |
| 4505 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4506 | int r = 0; |
| 4507 | |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4508 | BUG_ON(kvm->arch.ept_identity_pagetable_done); |
| 4509 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 4510 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
| 4511 | kvm->arch.ept_identity_map_addr, PAGE_SIZE); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4512 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4513 | return r; |
| 4514 | } |
| 4515 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4516 | static int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4517 | { |
| 4518 | int vpid; |
| 4519 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 4520 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4521 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4522 | spin_lock(&vmx_vpid_lock); |
| 4523 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4524 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4525 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4526 | else |
| 4527 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4528 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4529 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4530 | } |
| 4531 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4532 | static void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4533 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4534 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4535 | return; |
| 4536 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4537 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4538 | spin_unlock(&vmx_vpid_lock); |
| 4539 | } |
| 4540 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4541 | #define MSR_TYPE_R 1 |
| 4542 | #define MSR_TYPE_W 2 |
| 4543 | static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 4544 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4545 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4546 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4547 | |
| 4548 | if (!cpu_has_vmx_msr_bitmap()) |
| 4549 | return; |
| 4550 | |
| 4551 | /* |
| 4552 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4553 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4554 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4555 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4556 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4557 | if (type & MSR_TYPE_R) |
| 4558 | /* read-low */ |
| 4559 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 4560 | |
| 4561 | if (type & MSR_TYPE_W) |
| 4562 | /* write-low */ |
| 4563 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 4564 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4565 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4566 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4567 | if (type & MSR_TYPE_R) |
| 4568 | /* read-high */ |
| 4569 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 4570 | |
| 4571 | if (type & MSR_TYPE_W) |
| 4572 | /* write-high */ |
| 4573 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 4574 | |
| 4575 | } |
| 4576 | } |
| 4577 | |
| 4578 | static void __vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
| 4579 | u32 msr, int type) |
| 4580 | { |
| 4581 | int f = sizeof(unsigned long); |
| 4582 | |
| 4583 | if (!cpu_has_vmx_msr_bitmap()) |
| 4584 | return; |
| 4585 | |
| 4586 | /* |
| 4587 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4588 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4589 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4590 | */ |
| 4591 | if (msr <= 0x1fff) { |
| 4592 | if (type & MSR_TYPE_R) |
| 4593 | /* read-low */ |
| 4594 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 4595 | |
| 4596 | if (type & MSR_TYPE_W) |
| 4597 | /* write-low */ |
| 4598 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 4599 | |
| 4600 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4601 | msr &= 0x1fff; |
| 4602 | if (type & MSR_TYPE_R) |
| 4603 | /* read-high */ |
| 4604 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 4605 | |
| 4606 | if (type & MSR_TYPE_W) |
| 4607 | /* write-high */ |
| 4608 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 4609 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4610 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4611 | } |
| 4612 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 4613 | /* |
| 4614 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 4615 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 4616 | */ |
| 4617 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 4618 | unsigned long *msr_bitmap_nested, |
| 4619 | u32 msr, int type) |
| 4620 | { |
| 4621 | int f = sizeof(unsigned long); |
| 4622 | |
| 4623 | if (!cpu_has_vmx_msr_bitmap()) { |
| 4624 | WARN_ON(1); |
| 4625 | return; |
| 4626 | } |
| 4627 | |
| 4628 | /* |
| 4629 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4630 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4631 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4632 | */ |
| 4633 | if (msr <= 0x1fff) { |
| 4634 | if (type & MSR_TYPE_R && |
| 4635 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 4636 | /* read-low */ |
| 4637 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 4638 | |
| 4639 | if (type & MSR_TYPE_W && |
| 4640 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 4641 | /* write-low */ |
| 4642 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 4643 | |
| 4644 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4645 | msr &= 0x1fff; |
| 4646 | if (type & MSR_TYPE_R && |
| 4647 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 4648 | /* read-high */ |
| 4649 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 4650 | |
| 4651 | if (type & MSR_TYPE_W && |
| 4652 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 4653 | /* write-high */ |
| 4654 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 4655 | |
| 4656 | } |
| 4657 | } |
| 4658 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4659 | static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only) |
| 4660 | { |
| 4661 | if (!longmode_only) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4662 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, |
| 4663 | msr, MSR_TYPE_R | MSR_TYPE_W); |
| 4664 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, |
| 4665 | msr, MSR_TYPE_R | MSR_TYPE_W); |
| 4666 | } |
| 4667 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4668 | static void vmx_enable_intercept_msr_read_x2apic(u32 msr, bool apicv_active) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4669 | { |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4670 | if (apicv_active) { |
| 4671 | __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
| 4672 | msr, MSR_TYPE_R); |
| 4673 | __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
| 4674 | msr, MSR_TYPE_R); |
| 4675 | } else { |
| 4676 | __vmx_enable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv_inactive, |
| 4677 | msr, MSR_TYPE_R); |
| 4678 | __vmx_enable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv_inactive, |
| 4679 | msr, MSR_TYPE_R); |
| 4680 | } |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4681 | } |
| 4682 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4683 | static void vmx_disable_intercept_msr_read_x2apic(u32 msr, bool apicv_active) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4684 | { |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4685 | if (apicv_active) { |
| 4686 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
| 4687 | msr, MSR_TYPE_R); |
| 4688 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
| 4689 | msr, MSR_TYPE_R); |
| 4690 | } else { |
| 4691 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv_inactive, |
| 4692 | msr, MSR_TYPE_R); |
| 4693 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv_inactive, |
| 4694 | msr, MSR_TYPE_R); |
| 4695 | } |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4696 | } |
| 4697 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4698 | static void vmx_disable_intercept_msr_write_x2apic(u32 msr, bool apicv_active) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4699 | { |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4700 | if (apicv_active) { |
| 4701 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
| 4702 | msr, MSR_TYPE_W); |
| 4703 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
| 4704 | msr, MSR_TYPE_W); |
| 4705 | } else { |
| 4706 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv_inactive, |
| 4707 | msr, MSR_TYPE_W); |
| 4708 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv_inactive, |
| 4709 | msr, MSR_TYPE_W); |
| 4710 | } |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4711 | } |
| 4712 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4713 | static bool vmx_get_enable_apicv(void) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 4714 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4715 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 4716 | } |
| 4717 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4718 | static int vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
| 4719 | { |
| 4720 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4721 | int max_irr; |
| 4722 | void *vapic_page; |
| 4723 | u16 status; |
| 4724 | |
| 4725 | if (vmx->nested.pi_desc && |
| 4726 | vmx->nested.pi_pending) { |
| 4727 | vmx->nested.pi_pending = false; |
| 4728 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 4729 | return 0; |
| 4730 | |
| 4731 | max_irr = find_last_bit( |
| 4732 | (unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 4733 | |
| 4734 | if (max_irr == 256) |
| 4735 | return 0; |
| 4736 | |
| 4737 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
| 4738 | if (!vapic_page) { |
| 4739 | WARN_ON(1); |
| 4740 | return -ENOMEM; |
| 4741 | } |
| 4742 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page); |
| 4743 | kunmap(vmx->nested.virtual_apic_page); |
| 4744 | |
| 4745 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 4746 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 4747 | status &= ~0xff; |
| 4748 | status |= (u8)max_irr; |
| 4749 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 4750 | } |
| 4751 | } |
| 4752 | return 0; |
| 4753 | } |
| 4754 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 4755 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu) |
| 4756 | { |
| 4757 | #ifdef CONFIG_SMP |
| 4758 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 4759 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4760 | |
| 4761 | /* |
| 4762 | * Currently, we don't support urgent interrupt, |
| 4763 | * all interrupts are recognized as non-urgent |
| 4764 | * interrupt, so we cannot post interrupts when |
| 4765 | * 'SN' is set. |
| 4766 | * |
| 4767 | * If the vcpu is in guest mode, it means it is |
| 4768 | * running instead of being scheduled out and |
| 4769 | * waiting in the run queue, and that's the only |
| 4770 | * case when 'SN' is set currently, warning if |
| 4771 | * 'SN' is set. |
| 4772 | */ |
| 4773 | WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc)); |
| 4774 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 4775 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), |
| 4776 | POSTED_INTR_VECTOR); |
| 4777 | return true; |
| 4778 | } |
| 4779 | #endif |
| 4780 | return false; |
| 4781 | } |
| 4782 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4783 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 4784 | int vector) |
| 4785 | { |
| 4786 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4787 | |
| 4788 | if (is_guest_mode(vcpu) && |
| 4789 | vector == vmx->nested.posted_intr_nv) { |
| 4790 | /* the PIR and ON have been set by L1. */ |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 4791 | kvm_vcpu_trigger_posted_interrupt(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4792 | /* |
| 4793 | * If a posted intr is not recognized by hardware, |
| 4794 | * we will accomplish it in the next vmentry. |
| 4795 | */ |
| 4796 | vmx->nested.pi_pending = true; |
| 4797 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4798 | return 0; |
| 4799 | } |
| 4800 | return -1; |
| 4801 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4802 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 4803 | * Send interrupt to vcpu via posted interrupt way. |
| 4804 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 4805 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 4806 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 4807 | * interrupt from PIR in next vmentry. |
| 4808 | */ |
| 4809 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 4810 | { |
| 4811 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4812 | int r; |
| 4813 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4814 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 4815 | if (!r) |
| 4816 | return; |
| 4817 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 4818 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 4819 | return; |
| 4820 | |
| 4821 | r = pi_test_and_set_on(&vmx->pi_desc); |
| 4822 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 4823 | if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 4824 | kvm_vcpu_kick(vcpu); |
| 4825 | } |
| 4826 | |
| 4827 | static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
| 4828 | { |
| 4829 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4830 | |
| 4831 | if (!pi_test_and_clear_on(&vmx->pi_desc)) |
| 4832 | return; |
| 4833 | |
| 4834 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir); |
| 4835 | } |
| 4836 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4837 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4838 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 4839 | * will not change in the lifetime of the guest. |
| 4840 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 4841 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 4842 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4843 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4844 | { |
| 4845 | u32 low32, high32; |
| 4846 | unsigned long tmpl; |
| 4847 | struct desc_ptr dt; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 4848 | unsigned long cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4849 | |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 4850 | 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] | 4851 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 4852 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 4853 | /* Save the most likely value for this task's CR4 in the VMCS. */ |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 4854 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 4855 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
| 4856 | vmx->host_state.vmcs_host_cr4 = cr4; |
| 4857 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4858 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 4859 | #ifdef CONFIG_X86_64 |
| 4860 | /* |
| 4861 | * Load null selectors, so we can avoid reloading them in |
| 4862 | * __vmx_load_host_state(), in case userspace uses the null selectors |
| 4863 | * too (the expected case). |
| 4864 | */ |
| 4865 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 4866 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 4867 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4868 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 4869 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 4870 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4871 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 4872 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 4873 | |
| 4874 | native_store_idt(&dt); |
| 4875 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4876 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4877 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 4878 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 4879 | |
| 4880 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 4881 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 4882 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 4883 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 4884 | |
| 4885 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 4886 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 4887 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 4888 | } |
| 4889 | } |
| 4890 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4891 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 4892 | { |
| 4893 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 4894 | if (enable_ept) |
| 4895 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 4896 | if (is_guest_mode(&vmx->vcpu)) |
| 4897 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 4898 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4899 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 4900 | } |
| 4901 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4902 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 4903 | { |
| 4904 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 4905 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4906 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4907 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 4908 | /* Enable the preemption timer dynamically */ |
| 4909 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4910 | return pin_based_exec_ctrl; |
| 4911 | } |
| 4912 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4913 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 4914 | { |
| 4915 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4916 | |
| 4917 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx)); |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 4918 | if (cpu_has_secondary_exec_ctrls()) { |
| 4919 | if (kvm_vcpu_apicv_active(vcpu)) |
| 4920 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 4921 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 4922 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 4923 | else |
| 4924 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 4925 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 4926 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 4927 | } |
| 4928 | |
| 4929 | if (cpu_has_vmx_msr_bitmap()) |
| 4930 | vmx_set_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4931 | } |
| 4932 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4933 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 4934 | { |
| 4935 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
Paolo Bonzini | d16c293 | 2014-02-21 10:36:37 +0100 | [diff] [blame] | 4936 | |
| 4937 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 4938 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 4939 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 4940 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4941 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 4942 | #ifdef CONFIG_X86_64 |
| 4943 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 4944 | CPU_BASED_CR8_LOAD_EXITING; |
| 4945 | #endif |
| 4946 | } |
| 4947 | if (!enable_ept) |
| 4948 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 4949 | CPU_BASED_CR3_LOAD_EXITING | |
| 4950 | CPU_BASED_INVLPG_EXITING; |
| 4951 | return exec_control; |
| 4952 | } |
| 4953 | |
| 4954 | static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) |
| 4955 | { |
| 4956 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 4957 | if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4958 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 4959 | if (vmx->vpid == 0) |
| 4960 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 4961 | if (!enable_ept) { |
| 4962 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 4963 | enable_unrestricted_guest = 0; |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 4964 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
| 4965 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4966 | } |
| 4967 | if (!enable_unrestricted_guest) |
| 4968 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 4969 | if (!ple_gap) |
| 4970 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4971 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4972 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 4973 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4974 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 4975 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 4976 | (handle_vmptrld). |
| 4977 | We can NOT enable shadow_vmcs here because we don't have yet |
| 4978 | a current VMCS12 |
| 4979 | */ |
| 4980 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 4981 | |
| 4982 | if (!enable_pml) |
| 4983 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 4984 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4985 | return exec_control; |
| 4986 | } |
| 4987 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 4988 | static void ept_set_mmio_spte_mask(void) |
| 4989 | { |
| 4990 | /* |
| 4991 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 4992 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | 885032b | 2013-06-07 16:51:23 +0800 | [diff] [blame] | 4993 | * Also, magic bits (0x3ull << 62) is set to quickly identify mmio |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 4994 | * spte. |
| 4995 | */ |
Xiao Guangrong | 885032b | 2013-06-07 16:51:23 +0800 | [diff] [blame] | 4996 | kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 4997 | } |
| 4998 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 4999 | #define VMX_XSS_EXIT_BITMAP 0 |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5000 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5001 | * Sets up the vmcs for emulated real mode. |
| 5002 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 5003 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5004 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 5005 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5006 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 5007 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5008 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5009 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5010 | /* I/O */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 5011 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
| 5012 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5013 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 5014 | if (enable_shadow_vmcs) { |
| 5015 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
| 5016 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 5017 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5018 | if (cpu_has_vmx_msr_bitmap()) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5019 | vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5020 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5021 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 5022 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5023 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5024 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, vmx_pin_based_exec_ctrl(vmx)); |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 5025 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5026 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5027 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5028 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 5029 | if (cpu_has_secondary_exec_ctrls()) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5030 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 5031 | vmx_secondary_exec_control(vmx)); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 5032 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5033 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5034 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5035 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 5036 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 5037 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 5038 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 5039 | |
| 5040 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5041 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 5042 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5043 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5044 | } |
| 5045 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5046 | if (ple_gap) { |
| 5047 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 5048 | vmx->ple_window = ple_window; |
| 5049 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5050 | } |
| 5051 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 5052 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 5053 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5054 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 5055 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 5056 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 5057 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5058 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5059 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5060 | rdmsrl(MSR_FS_BASE, a); |
| 5061 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 5062 | rdmsrl(MSR_GS_BASE, a); |
| 5063 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 5064 | #else |
| 5065 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 5066 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 5067 | #endif |
| 5068 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 5069 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 5070 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 5071 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 5072 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 5073 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5074 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 5075 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 5076 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 5077 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 5078 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5079 | u32 index = vmx_msr_index[i]; |
| 5080 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5081 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5082 | |
| 5083 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 5084 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 5085 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 5086 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 5087 | vmx->guest_msrs[j].index = i; |
| 5088 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 5089 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5090 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5091 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5092 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 5093 | |
| 5094 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5095 | |
| 5096 | /* 22.2.1, 20.8.1 */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 5097 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 5098 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5099 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5100 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5101 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5102 | if (vmx_xsaves_supported()) |
| 5103 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 5104 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 5105 | if (enable_pml) { |
| 5106 | ASSERT(vmx->pml_pg); |
| 5107 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 5108 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 5109 | } |
| 5110 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5111 | return 0; |
| 5112 | } |
| 5113 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5114 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5115 | { |
| 5116 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 5117 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5118 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5119 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5120 | vmx->rmode.vm86_active = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5121 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5122 | vmx->soft_vnmi_blocked = 0; |
| 5123 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5124 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5125 | kvm_set_cr8(vcpu, 0); |
| 5126 | |
| 5127 | if (!init_event) { |
| 5128 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 5129 | MSR_IA32_APICBASE_ENABLE; |
| 5130 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 5131 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 5132 | apic_base_msr.host_initiated = true; |
| 5133 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 5134 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5135 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5136 | vmx_segment_cache_clear(vmx); |
| 5137 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 5138 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 5139 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 5140 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5141 | |
| 5142 | seg_setup(VCPU_SREG_DS); |
| 5143 | seg_setup(VCPU_SREG_ES); |
| 5144 | seg_setup(VCPU_SREG_FS); |
| 5145 | seg_setup(VCPU_SREG_GS); |
| 5146 | seg_setup(VCPU_SREG_SS); |
| 5147 | |
| 5148 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 5149 | vmcs_writel(GUEST_TR_BASE, 0); |
| 5150 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 5151 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 5152 | |
| 5153 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 5154 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 5155 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 5156 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 5157 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5158 | if (!init_event) { |
| 5159 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 5160 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 5161 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 5162 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 5163 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5164 | |
| 5165 | vmcs_writel(GUEST_RFLAGS, 0x02); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 5166 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5167 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5168 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 5169 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 5170 | |
| 5171 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 5172 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 5173 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 5174 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5175 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 5176 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5177 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5178 | setup_msrs(vmx); |
| 5179 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5180 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 5181 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5182 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5183 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5184 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5185 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5186 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5187 | vmcs_write32(TPR_THRESHOLD, 0); |
| 5188 | } |
| 5189 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 5190 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5191 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5192 | if (kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5193 | memset(&vmx->pi_desc, 0, sizeof(struct pi_desc)); |
| 5194 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5195 | if (vmx->vpid != 0) |
| 5196 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 5197 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5198 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5199 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 5200 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5201 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 5202 | vmx_set_efer(vcpu, 0); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5203 | vmx_fpu_activate(vcpu); |
| 5204 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5205 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 5206 | vpid_sync_context(vmx->vpid); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5207 | } |
| 5208 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 5209 | /* |
| 5210 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 5211 | * For most existing hypervisors, this will always return true. |
| 5212 | */ |
| 5213 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 5214 | { |
| 5215 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 5216 | PIN_BASED_EXT_INTR_MASK; |
| 5217 | } |
| 5218 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 5219 | /* |
| 5220 | * In nested virtualization, check if L1 has set |
| 5221 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 5222 | */ |
| 5223 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 5224 | { |
| 5225 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 5226 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 5227 | } |
| 5228 | |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 5229 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 5230 | { |
| 5231 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 5232 | PIN_BASED_NMI_EXITING; |
| 5233 | } |
| 5234 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5235 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5236 | { |
| 5237 | u32 cpu_based_vm_exec_control; |
Jan Kiszka | 730dca4 | 2013-04-28 10:50:52 +0200 | [diff] [blame] | 5238 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5239 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5240 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 5241 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 5242 | } |
| 5243 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5244 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5245 | { |
| 5246 | u32 cpu_based_vm_exec_control; |
| 5247 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5248 | if (!cpu_has_virtual_nmis() || |
| 5249 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
| 5250 | enable_irq_window(vcpu); |
| 5251 | return; |
| 5252 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 5253 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5254 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5255 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING; |
| 5256 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 5257 | } |
| 5258 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5259 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5260 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 5261 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5262 | uint32_t intr; |
| 5263 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 5264 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5265 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 5266 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 5267 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5268 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 5269 | int inc_eip = 0; |
| 5270 | if (vcpu->arch.interrupt.soft) |
| 5271 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 5272 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 5273 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5274 | return; |
| 5275 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5276 | intr = irq | INTR_INFO_VALID_MASK; |
| 5277 | if (vcpu->arch.interrupt.soft) { |
| 5278 | intr |= INTR_TYPE_SOFT_INTR; |
| 5279 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 5280 | vmx->vcpu.arch.event_exit_inst_len); |
| 5281 | } else |
| 5282 | intr |= INTR_TYPE_EXT_INTR; |
| 5283 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5284 | } |
| 5285 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5286 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 5287 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 5288 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5289 | |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 5290 | if (!is_guest_mode(vcpu)) { |
| 5291 | if (!cpu_has_virtual_nmis()) { |
| 5292 | /* |
| 5293 | * Tracking the NMI-blocked state in software is built upon |
| 5294 | * finding the next open IRQ window. This, in turn, depends on |
| 5295 | * well-behaving guests: They have to keep IRQs disabled at |
| 5296 | * least as long as the NMI handler runs. Otherwise we may |
| 5297 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 5298 | * highly unlikely, we can live with the residual risk. |
| 5299 | */ |
| 5300 | vmx->soft_vnmi_blocked = 1; |
| 5301 | vmx->vnmi_blocked_time = 0; |
| 5302 | } |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 5303 | |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 5304 | ++vcpu->stat.nmi_injections; |
| 5305 | vmx->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5306 | } |
| 5307 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5308 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 5309 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 5310 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 5311 | return; |
| 5312 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 5313 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5314 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 5315 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5316 | } |
| 5317 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5318 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 5319 | { |
| 5320 | if (!cpu_has_virtual_nmis()) |
| 5321 | return to_vmx(vcpu)->soft_vnmi_blocked; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 5322 | if (to_vmx(vcpu)->nmi_known_unmasked) |
| 5323 | return false; |
Avi Kivity | c332c83 | 2010-05-04 12:24:12 +0300 | [diff] [blame] | 5324 | return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5325 | } |
| 5326 | |
| 5327 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 5328 | { |
| 5329 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5330 | |
| 5331 | if (!cpu_has_virtual_nmis()) { |
| 5332 | if (vmx->soft_vnmi_blocked != masked) { |
| 5333 | vmx->soft_vnmi_blocked = masked; |
| 5334 | vmx->vnmi_blocked_time = 0; |
| 5335 | } |
| 5336 | } else { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 5337 | vmx->nmi_known_unmasked = !masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5338 | if (masked) |
| 5339 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5340 | GUEST_INTR_STATE_NMI); |
| 5341 | else |
| 5342 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5343 | GUEST_INTR_STATE_NMI); |
| 5344 | } |
| 5345 | } |
| 5346 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 5347 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 5348 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 5349 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 5350 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 5351 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 5352 | if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked) |
| 5353 | return 0; |
| 5354 | |
| 5355 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 5356 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 5357 | | GUEST_INTR_STATE_NMI)); |
| 5358 | } |
| 5359 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5360 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 5361 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 5362 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 5363 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 5364 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 5365 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5366 | } |
| 5367 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5368 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 5369 | { |
| 5370 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5371 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 5372 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 5373 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5374 | if (ret) |
| 5375 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 5376 | kvm->arch.tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5377 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5378 | } |
| 5379 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5380 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5381 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5382 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5383 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 5384 | /* |
| 5385 | * Update instruction length as we may reinject the exception |
| 5386 | * from user space while in guest debugging mode. |
| 5387 | */ |
| 5388 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 5389 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5390 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5391 | return false; |
| 5392 | /* fall through */ |
| 5393 | case DB_VECTOR: |
| 5394 | if (vcpu->guest_debug & |
| 5395 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 5396 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5397 | /* fall through */ |
| 5398 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5399 | case OF_VECTOR: |
| 5400 | case BR_VECTOR: |
| 5401 | case UD_VECTOR: |
| 5402 | case DF_VECTOR: |
| 5403 | case SS_VECTOR: |
| 5404 | case GP_VECTOR: |
| 5405 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5406 | return true; |
| 5407 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5408 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5409 | return false; |
| 5410 | } |
| 5411 | |
| 5412 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 5413 | int vec, u32 err_code) |
| 5414 | { |
| 5415 | /* |
| 5416 | * Instruction with address size override prefix opcode 0x67 |
| 5417 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 5418 | */ |
| 5419 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
| 5420 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
| 5421 | if (vcpu->arch.halt_request) { |
| 5422 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 5423 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5424 | } |
| 5425 | return 1; |
| 5426 | } |
| 5427 | return 0; |
| 5428 | } |
| 5429 | |
| 5430 | /* |
| 5431 | * Forward all other exceptions that are valid in real mode. |
| 5432 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 5433 | * the required debugging infrastructure rework. |
| 5434 | */ |
| 5435 | kvm_queue_exception(vcpu, vec); |
| 5436 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5437 | } |
| 5438 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5439 | /* |
| 5440 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 5441 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 5442 | * We pass a fake environment to the machine check handler because we want |
| 5443 | * the guest to be always treated like user space, no matter what context |
| 5444 | * it used internally. |
| 5445 | */ |
| 5446 | static void kvm_machine_check(void) |
| 5447 | { |
| 5448 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 5449 | struct pt_regs regs = { |
| 5450 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 5451 | .flags = X86_EFLAGS_IF, |
| 5452 | }; |
| 5453 | |
| 5454 | do_machine_check(®s, 0); |
| 5455 | #endif |
| 5456 | } |
| 5457 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5458 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5459 | { |
| 5460 | /* already handled by vcpu_run */ |
| 5461 | return 1; |
| 5462 | } |
| 5463 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5464 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5465 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5466 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5467 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5468 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5469 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5470 | u32 vect_info; |
| 5471 | enum emulation_result er; |
| 5472 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5473 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 5474 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5475 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5476 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5477 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5478 | |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 5479 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 5480 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 5481 | |
| 5482 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 5483 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 5484 | return 1; |
| 5485 | } |
| 5486 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5487 | if (is_invalid_opcode(intr_info)) { |
Jan Kiszka | ae1f576 | 2015-03-09 20:56:43 +0100 | [diff] [blame] | 5488 | if (is_guest_mode(vcpu)) { |
| 5489 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5490 | return 1; |
| 5491 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5492 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5493 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 5494 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5495 | return 1; |
| 5496 | } |
| 5497 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5498 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 5499 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5500 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5501 | |
| 5502 | /* |
| 5503 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 5504 | * MMIO, it is better to report an internal error. |
| 5505 | * See the comments in vmx_handle_exit. |
| 5506 | */ |
| 5507 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 5508 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 5509 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 5510 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 5511 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5512 | vcpu->run->internal.data[0] = vect_info; |
| 5513 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 5514 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5515 | return 0; |
| 5516 | } |
| 5517 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5518 | if (is_page_fault(intr_info)) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5519 | /* EPT won't cause page fault directly */ |
Julia Lawall | cf3ace7 | 2011-08-02 12:34:57 +0200 | [diff] [blame] | 5520 | BUG_ON(enable_ept); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5521 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5522 | trace_kvm_page_fault(cr2, error_code); |
| 5523 | |
Gleb Natapov | 3298b75 | 2009-05-11 13:35:46 +0300 | [diff] [blame] | 5524 | if (kvm_event_needs_reinjection(vcpu)) |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 5525 | kvm_mmu_unprotect_page_virt(vcpu, cr2); |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 5526 | return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5527 | } |
| 5528 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5529 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5530 | |
| 5531 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 5532 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 5533 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5534 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 5535 | case AC_VECTOR: |
| 5536 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 5537 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5538 | case DB_VECTOR: |
| 5539 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 5540 | if (!(vcpu->guest_debug & |
| 5541 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 5542 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 5543 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 5544 | if (!(dr6 & ~DR6_RESERVED)) /* icebp */ |
| 5545 | skip_emulated_instruction(vcpu); |
| 5546 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5547 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 5548 | return 1; |
| 5549 | } |
| 5550 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 5551 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 5552 | /* fall through */ |
| 5553 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 5554 | /* |
| 5555 | * Update instruction length as we may reinject #BP from |
| 5556 | * user space while in guest debugging mode. Reading it for |
| 5557 | * #DB as well causes no harm, it is not used in that case. |
| 5558 | */ |
| 5559 | vmx->vcpu.arch.event_exit_inst_len = |
| 5560 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5561 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 5562 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5563 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 5564 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5565 | break; |
| 5566 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5567 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 5568 | kvm_run->ex.exception = ex_no; |
| 5569 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5570 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5571 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5572 | return 0; |
| 5573 | } |
| 5574 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5575 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5576 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 5577 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5578 | return 1; |
| 5579 | } |
| 5580 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5581 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5582 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5583 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5584 | return 0; |
| 5585 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5586 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5587 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5588 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5589 | unsigned long exit_qualification; |
Jan Kiszka | 34c33d1 | 2009-02-08 13:28:15 +0100 | [diff] [blame] | 5590 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 5591 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5592 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5593 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 5594 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 5595 | in = (exit_qualification & 8) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 5596 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5597 | ++vcpu->stat.io_exits; |
| 5598 | |
| 5599 | if (string || in) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5600 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5601 | |
| 5602 | port = exit_qualification >> 16; |
| 5603 | size = (exit_qualification & 7) + 1; |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 5604 | skip_emulated_instruction(vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5605 | |
| 5606 | return kvm_fast_pio_out(vcpu, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5607 | } |
| 5608 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5609 | static void |
| 5610 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 5611 | { |
| 5612 | /* |
| 5613 | * Patch in the VMCALL instruction: |
| 5614 | */ |
| 5615 | hypercall[0] = 0x0f; |
| 5616 | hypercall[1] = 0x01; |
| 5617 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5618 | } |
| 5619 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5620 | static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5621 | { |
| 5622 | unsigned long always_on = VMXON_CR0_ALWAYSON; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5623 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5624 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5625 | if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high & |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5626 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
| 5627 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 5628 | always_on &= ~(X86_CR0_PE | X86_CR0_PG); |
| 5629 | return (val & always_on) == always_on; |
| 5630 | } |
| 5631 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 5632 | /* 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] | 5633 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 5634 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5635 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5636 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5637 | unsigned long orig_val = val; |
| 5638 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5639 | /* |
| 5640 | * We get here when L2 changed cr0 in a way that did not change |
| 5641 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5642 | * but did change L0 shadowed bits. So we first calculate the |
| 5643 | * effective cr0 value that L1 would like to write into the |
| 5644 | * hardware. It consists of the L2-owned bits from the new |
| 5645 | * 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] | 5646 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5647 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 5648 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 5649 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5650 | if (!nested_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5651 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5652 | |
| 5653 | if (kvm_set_cr0(vcpu, val)) |
| 5654 | return 1; |
| 5655 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5656 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5657 | } else { |
| 5658 | if (to_vmx(vcpu)->nested.vmxon && |
| 5659 | ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON)) |
| 5660 | return 1; |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5661 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5662 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5663 | } |
| 5664 | |
| 5665 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 5666 | { |
| 5667 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5668 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5669 | unsigned long orig_val = val; |
| 5670 | |
| 5671 | /* analogously to handle_set_cr0 */ |
| 5672 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 5673 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 5674 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5675 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5676 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5677 | return 0; |
| 5678 | } else |
| 5679 | return kvm_set_cr4(vcpu, val); |
| 5680 | } |
| 5681 | |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 5682 | /* called to set cr0 as appropriate for clts instruction exit. */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5683 | static void handle_clts(struct kvm_vcpu *vcpu) |
| 5684 | { |
| 5685 | if (is_guest_mode(vcpu)) { |
| 5686 | /* |
| 5687 | * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS |
| 5688 | * but we did (!fpu_active). We need to keep GUEST_CR0.TS on, |
| 5689 | * just pretend it's off (also in arch.cr0 for fpu_activate). |
| 5690 | */ |
| 5691 | vmcs_writel(CR0_READ_SHADOW, |
| 5692 | vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS); |
| 5693 | vcpu->arch.cr0 &= ~X86_CR0_TS; |
| 5694 | } else |
| 5695 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
| 5696 | } |
| 5697 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5698 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5699 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5700 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5701 | int cr; |
| 5702 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 5703 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5704 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5705 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5706 | cr = exit_qualification & 15; |
| 5707 | reg = (exit_qualification >> 8) & 15; |
| 5708 | switch ((exit_qualification >> 4) & 3) { |
| 5709 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 5710 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5711 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5712 | switch (cr) { |
| 5713 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5714 | err = handle_set_cr0(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5715 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5716 | return 1; |
| 5717 | case 3: |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 5718 | err = kvm_set_cr3(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5719 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5720 | return 1; |
| 5721 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5722 | err = handle_set_cr4(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5723 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5724 | return 1; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5725 | case 8: { |
| 5726 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 5727 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 5728 | err = kvm_set_cr8(vcpu, cr8); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5729 | kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5730 | if (lapic_in_kernel(vcpu)) |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5731 | return 1; |
| 5732 | if (cr8_prev <= cr8) |
| 5733 | return 1; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5734 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5735 | return 0; |
| 5736 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 5737 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5738 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5739 | case 2: /* clts */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5740 | handle_clts(vcpu); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5741 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5742 | skip_emulated_instruction(vcpu); |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 5743 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5744 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5745 | case 1: /*mov from cr*/ |
| 5746 | switch (cr) { |
| 5747 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 5748 | val = kvm_read_cr3(vcpu); |
| 5749 | kvm_register_write(vcpu, reg, val); |
| 5750 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5751 | skip_emulated_instruction(vcpu); |
| 5752 | return 1; |
| 5753 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5754 | val = kvm_get_cr8(vcpu); |
| 5755 | kvm_register_write(vcpu, reg, val); |
| 5756 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5757 | skip_emulated_instruction(vcpu); |
| 5758 | return 1; |
| 5759 | } |
| 5760 | break; |
| 5761 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 5762 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5763 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 5764 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5765 | |
| 5766 | skip_emulated_instruction(vcpu); |
| 5767 | return 1; |
| 5768 | default: |
| 5769 | break; |
| 5770 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5771 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 5772 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5773 | (int)(exit_qualification >> 4) & 3, cr); |
| 5774 | return 0; |
| 5775 | } |
| 5776 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5777 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5778 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5779 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 5780 | int dr, dr7, reg; |
| 5781 | |
| 5782 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5783 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 5784 | |
| 5785 | /* First, if DR does not exist, trigger UD */ |
| 5786 | if (!kvm_require_dr(vcpu, dr)) |
| 5787 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5788 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 5789 | /* 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] | 5790 | if (!kvm_require_cpl(vcpu, 0)) |
| 5791 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 5792 | dr7 = vmcs_readl(GUEST_DR7); |
| 5793 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5794 | /* |
| 5795 | * As the vm-exit takes precedence over the debug trap, we |
| 5796 | * need to emulate the latter, either for the host or the |
| 5797 | * guest debugging itself. |
| 5798 | */ |
| 5799 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5800 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 5801 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 5802 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5803 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 5804 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5805 | return 0; |
| 5806 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 5807 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 5808 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5809 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 5810 | return 1; |
| 5811 | } |
| 5812 | } |
| 5813 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 5814 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 5815 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 5816 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 5817 | |
| 5818 | /* |
| 5819 | * No more DR vmexits; force a reload of the debug registers |
| 5820 | * and reenter on this instruction. The next vmexit will |
| 5821 | * retrieve the full state of the debug registers. |
| 5822 | */ |
| 5823 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 5824 | return 1; |
| 5825 | } |
| 5826 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5827 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 5828 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 5829 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 5830 | |
| 5831 | if (kvm_get_dr(vcpu, dr, &val)) |
| 5832 | return 1; |
| 5833 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 5834 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 5835 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 5836 | return 1; |
| 5837 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5838 | skip_emulated_instruction(vcpu); |
| 5839 | return 1; |
| 5840 | } |
| 5841 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 5842 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 5843 | { |
| 5844 | return vcpu->arch.dr6; |
| 5845 | } |
| 5846 | |
| 5847 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 5848 | { |
| 5849 | } |
| 5850 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 5851 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 5852 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 5853 | get_debugreg(vcpu->arch.db[0], 0); |
| 5854 | get_debugreg(vcpu->arch.db[1], 1); |
| 5855 | get_debugreg(vcpu->arch.db[2], 2); |
| 5856 | get_debugreg(vcpu->arch.db[3], 3); |
| 5857 | get_debugreg(vcpu->arch.dr6, 6); |
| 5858 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 5859 | |
| 5860 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 5861 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 5862 | } |
| 5863 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 5864 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 5865 | { |
| 5866 | vmcs_writel(GUEST_DR7, val); |
| 5867 | } |
| 5868 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5869 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5870 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 5871 | kvm_emulate_cpuid(vcpu); |
| 5872 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5873 | } |
| 5874 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5875 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5876 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5877 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 5878 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5879 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 5880 | msr_info.index = ecx; |
| 5881 | msr_info.host_initiated = false; |
| 5882 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 5883 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 5884 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5885 | return 1; |
| 5886 | } |
| 5887 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 5888 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 5889 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5890 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 5891 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 5892 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5893 | skip_emulated_instruction(vcpu); |
| 5894 | return 1; |
| 5895 | } |
| 5896 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5897 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5898 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 5899 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5900 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 5901 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 5902 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5903 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 5904 | msr.data = data; |
| 5905 | msr.index = ecx; |
| 5906 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 5907 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 5908 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 5909 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5910 | return 1; |
| 5911 | } |
| 5912 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 5913 | trace_kvm_msr_write(ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5914 | skip_emulated_instruction(vcpu); |
| 5915 | return 1; |
| 5916 | } |
| 5917 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5918 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5919 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5920 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5921 | return 1; |
| 5922 | } |
| 5923 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5924 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5925 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5926 | u32 cpu_based_vm_exec_control; |
| 5927 | |
| 5928 | /* clear pending irq */ |
| 5929 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5930 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 5931 | 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] | 5932 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5933 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 5934 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 5935 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5936 | return 1; |
| 5937 | } |
| 5938 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5939 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5940 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 5941 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5942 | } |
| 5943 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5944 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5945 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 5946 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5947 | } |
| 5948 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 5949 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 5950 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5951 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 5952 | } |
| 5953 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5954 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5955 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 5956 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5957 | |
| 5958 | kvm_mmu_invlpg(vcpu, exit_qualification); |
| 5959 | skip_emulated_instruction(vcpu); |
| 5960 | return 1; |
| 5961 | } |
| 5962 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 5963 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 5964 | { |
| 5965 | int err; |
| 5966 | |
| 5967 | err = kvm_rdpmc(vcpu); |
| 5968 | kvm_complete_insn_gp(vcpu, err); |
| 5969 | |
| 5970 | return 1; |
| 5971 | } |
| 5972 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5973 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 5974 | { |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 5975 | kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 5976 | return 1; |
| 5977 | } |
| 5978 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 5979 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 5980 | { |
| 5981 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 5982 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 5983 | |
| 5984 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
| 5985 | skip_emulated_instruction(vcpu); |
| 5986 | return 1; |
| 5987 | } |
| 5988 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5989 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 5990 | { |
| 5991 | skip_emulated_instruction(vcpu); |
| 5992 | WARN(1, "this should never happen\n"); |
| 5993 | return 1; |
| 5994 | } |
| 5995 | |
| 5996 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 5997 | { |
| 5998 | skip_emulated_instruction(vcpu); |
| 5999 | WARN(1, "this should never happen\n"); |
| 6000 | return 1; |
| 6001 | } |
| 6002 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6003 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6004 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 6005 | if (likely(fasteoi)) { |
| 6006 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6007 | int access_type, offset; |
| 6008 | |
| 6009 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 6010 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 6011 | /* |
| 6012 | * Sane guest uses MOV to write EOI, with written value |
| 6013 | * not cared. So make a short-circuit here by avoiding |
| 6014 | * heavy instruction emulation. |
| 6015 | */ |
| 6016 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 6017 | (offset == APIC_EOI)) { |
| 6018 | kvm_lapic_set_eoi(vcpu); |
| 6019 | skip_emulated_instruction(vcpu); |
| 6020 | return 1; |
| 6021 | } |
| 6022 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6023 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6024 | } |
| 6025 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6026 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 6027 | { |
| 6028 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6029 | int vector = exit_qualification & 0xff; |
| 6030 | |
| 6031 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 6032 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 6033 | return 1; |
| 6034 | } |
| 6035 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 6036 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 6037 | { |
| 6038 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6039 | u32 offset = exit_qualification & 0xfff; |
| 6040 | |
| 6041 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 6042 | kvm_apic_write_nodecode(vcpu, offset); |
| 6043 | return 1; |
| 6044 | } |
| 6045 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6046 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6047 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 6048 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6049 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 6050 | bool has_error_code = false; |
| 6051 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6052 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6053 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6054 | |
| 6055 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6056 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6057 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6058 | |
| 6059 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6060 | |
| 6061 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6062 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 6063 | switch (type) { |
| 6064 | case INTR_TYPE_NMI_INTR: |
| 6065 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 6066 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6067 | break; |
| 6068 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6069 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6070 | kvm_clear_interrupt_queue(vcpu); |
| 6071 | break; |
| 6072 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 6073 | if (vmx->idt_vectoring_info & |
| 6074 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 6075 | has_error_code = true; |
| 6076 | error_code = |
| 6077 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 6078 | } |
| 6079 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6080 | case INTR_TYPE_SOFT_EXCEPTION: |
| 6081 | kvm_clear_exception_queue(vcpu); |
| 6082 | break; |
| 6083 | default: |
| 6084 | break; |
| 6085 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 6086 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6087 | tss_selector = exit_qualification; |
| 6088 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6089 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 6090 | type != INTR_TYPE_EXT_INTR && |
| 6091 | type != INTR_TYPE_NMI_INTR)) |
| 6092 | skip_emulated_instruction(vcpu); |
| 6093 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6094 | if (kvm_task_switch(vcpu, tss_selector, |
| 6095 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 6096 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 6097 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6098 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 6099 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6100 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 6101 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6102 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6103 | /* |
| 6104 | * TODO: What about debug traps on tss switch? |
| 6105 | * Are we supposed to inject them and update dr6? |
| 6106 | */ |
| 6107 | |
| 6108 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6109 | } |
| 6110 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6111 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6112 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6113 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6114 | gpa_t gpa; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6115 | u32 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6116 | int gla_validity; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6117 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6118 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6119 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6120 | gla_validity = (exit_qualification >> 7) & 0x3; |
Liang Li | 72e0ae5 | 2016-08-18 15:49:19 +0800 | [diff] [blame] | 6121 | if (gla_validity == 0x2) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6122 | printk(KERN_ERR "EPT: Handling EPT violation failed!\n"); |
| 6123 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 6124 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6125 | vmcs_readl(GUEST_LINEAR_ADDRESS)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6126 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 6127 | (long unsigned int)exit_qualification); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6128 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 6129 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION; |
Avi Kivity | 596ae89 | 2009-06-03 14:12:10 +0300 | [diff] [blame] | 6130 | return 0; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6131 | } |
| 6132 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 6133 | /* |
| 6134 | * EPT violation happened while executing iret from NMI, |
| 6135 | * "blocked by NMI" bit has to be set before next VM entry. |
| 6136 | * There are errata that may cause this bit to not be set: |
| 6137 | * AAK134, BY25. |
| 6138 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 6139 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
| 6140 | cpu_has_virtual_nmis() && |
| 6141 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 6142 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 6143 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6144 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6145 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6146 | |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6147 | /* it is a read fault? */ |
| 6148 | error_code = (exit_qualification << 2) & PFERR_USER_MASK; |
| 6149 | /* it is a write fault? */ |
| 6150 | error_code |= exit_qualification & PFERR_WRITE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 6151 | /* It is a fetch fault? */ |
Tiejun Chen | 81ed33e | 2014-11-18 17:12:56 +0800 | [diff] [blame] | 6152 | error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6153 | /* ept page table is present? */ |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6154 | error_code |= (exit_qualification & 0x38) != 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6155 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 6156 | vcpu->arch.exit_qualification = exit_qualification; |
| 6157 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6158 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6159 | } |
| 6160 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6161 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6162 | { |
Xiao Guangrong | f735d4a | 2015-08-05 12:04:27 +0800 | [diff] [blame] | 6163 | int ret; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6164 | gpa_t gpa; |
| 6165 | |
| 6166 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 6167 | if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 6168 | skip_emulated_instruction(vcpu); |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 6169 | trace_kvm_fast_mmio(gpa); |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 6170 | return 1; |
| 6171 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6172 | |
Paolo Bonzini | 450869d | 2015-11-04 13:41:21 +0100 | [diff] [blame] | 6173 | ret = handle_mmio_page_fault(vcpu, gpa, true); |
Xiao Guangrong | b37fbea | 2013-06-07 16:51:25 +0800 | [diff] [blame] | 6174 | if (likely(ret == RET_MMIO_PF_EMULATE)) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6175 | return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) == |
| 6176 | EMULATE_DONE; |
Xiao Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 6177 | |
| 6178 | if (unlikely(ret == RET_MMIO_PF_INVALID)) |
| 6179 | return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0); |
| 6180 | |
Xiao Guangrong | b37fbea | 2013-06-07 16:51:25 +0800 | [diff] [blame] | 6181 | if (unlikely(ret == RET_MMIO_PF_RETRY)) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6182 | return 1; |
| 6183 | |
| 6184 | /* It is the real ept misconfig */ |
Xiao Guangrong | f735d4a | 2015-08-05 12:04:27 +0800 | [diff] [blame] | 6185 | WARN_ON(1); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6186 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6187 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 6188 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6189 | |
| 6190 | return 0; |
| 6191 | } |
| 6192 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6193 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6194 | { |
| 6195 | u32 cpu_based_vm_exec_control; |
| 6196 | |
| 6197 | /* clear pending NMI */ |
| 6198 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 6199 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 6200 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 6201 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6202 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6203 | |
| 6204 | return 1; |
| 6205 | } |
| 6206 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6207 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6208 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 6209 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6210 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6211 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6212 | u32 cpu_exec_ctrl; |
| 6213 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 6214 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6215 | |
| 6216 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 6217 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6218 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 6219 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 6220 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6221 | return handle_interrupt_window(&vmx->vcpu); |
| 6222 | |
Avi Kivity | de87dcd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 6223 | if (test_bit(KVM_REQ_EVENT, &vcpu->requests)) |
| 6224 | return 1; |
| 6225 | |
Gleb Natapov | 991eebf | 2013-04-11 12:10:51 +0300 | [diff] [blame] | 6226 | err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6227 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 6228 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 6229 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6230 | ret = 0; |
| 6231 | goto out; |
| 6232 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 6233 | |
Avi Kivity | de5f70e | 2012-06-12 20:22:28 +0300 | [diff] [blame] | 6234 | if (err != EMULATE_DONE) { |
| 6235 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6236 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 6237 | vcpu->run->internal.ndata = 0; |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 6238 | return 0; |
Avi Kivity | de5f70e | 2012-06-12 20:22:28 +0300 | [diff] [blame] | 6239 | } |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6240 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 6241 | if (vcpu->arch.halt_request) { |
| 6242 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6243 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 6244 | goto out; |
| 6245 | } |
| 6246 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6247 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6248 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6249 | if (need_resched()) |
| 6250 | schedule(); |
| 6251 | } |
| 6252 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6253 | out: |
| 6254 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6255 | } |
| 6256 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 6257 | static int __grow_ple_window(int val) |
| 6258 | { |
| 6259 | if (ple_window_grow < 1) |
| 6260 | return ple_window; |
| 6261 | |
| 6262 | val = min(val, ple_window_actual_max); |
| 6263 | |
| 6264 | if (ple_window_grow < ple_window) |
| 6265 | val *= ple_window_grow; |
| 6266 | else |
| 6267 | val += ple_window_grow; |
| 6268 | |
| 6269 | return val; |
| 6270 | } |
| 6271 | |
| 6272 | static int __shrink_ple_window(int val, int modifier, int minimum) |
| 6273 | { |
| 6274 | if (modifier < 1) |
| 6275 | return ple_window; |
| 6276 | |
| 6277 | if (modifier < ple_window) |
| 6278 | val /= modifier; |
| 6279 | else |
| 6280 | val -= modifier; |
| 6281 | |
| 6282 | return max(val, minimum); |
| 6283 | } |
| 6284 | |
| 6285 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 6286 | { |
| 6287 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6288 | int old = vmx->ple_window; |
| 6289 | |
| 6290 | vmx->ple_window = __grow_ple_window(old); |
| 6291 | |
| 6292 | if (vmx->ple_window != old) |
| 6293 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 6294 | |
| 6295 | 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] | 6296 | } |
| 6297 | |
| 6298 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 6299 | { |
| 6300 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6301 | int old = vmx->ple_window; |
| 6302 | |
| 6303 | vmx->ple_window = __shrink_ple_window(old, |
| 6304 | ple_window_shrink, ple_window); |
| 6305 | |
| 6306 | if (vmx->ple_window != old) |
| 6307 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 6308 | |
| 6309 | 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] | 6310 | } |
| 6311 | |
| 6312 | /* |
| 6313 | * ple_window_actual_max is computed to be one grow_ple_window() below |
| 6314 | * ple_window_max. (See __grow_ple_window for the reason.) |
| 6315 | * This prevents overflows, because ple_window_max is int. |
| 6316 | * ple_window_max effectively rounded down to a multiple of ple_window_grow in |
| 6317 | * this process. |
| 6318 | * ple_window_max is also prevented from setting vmx->ple_window < ple_window. |
| 6319 | */ |
| 6320 | static void update_ple_window_actual_max(void) |
| 6321 | { |
| 6322 | ple_window_actual_max = |
| 6323 | __shrink_ple_window(max(ple_window_max, ple_window), |
| 6324 | ple_window_grow, INT_MIN); |
| 6325 | } |
| 6326 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6327 | /* |
| 6328 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 6329 | */ |
| 6330 | static void wakeup_handler(void) |
| 6331 | { |
| 6332 | struct kvm_vcpu *vcpu; |
| 6333 | int cpu = smp_processor_id(); |
| 6334 | |
| 6335 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 6336 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 6337 | blocked_vcpu_list) { |
| 6338 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 6339 | |
| 6340 | if (pi_test_on(pi_desc) == 1) |
| 6341 | kvm_vcpu_kick(vcpu); |
| 6342 | } |
| 6343 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 6344 | } |
| 6345 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6346 | static __init int hardware_setup(void) |
| 6347 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6348 | int r = -ENOMEM, i, msr; |
| 6349 | |
| 6350 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 6351 | |
| 6352 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 6353 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 6354 | |
| 6355 | vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6356 | if (!vmx_io_bitmap_a) |
| 6357 | return r; |
| 6358 | |
| 6359 | vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6360 | if (!vmx_io_bitmap_b) |
| 6361 | goto out; |
| 6362 | |
| 6363 | vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6364 | if (!vmx_msr_bitmap_legacy) |
| 6365 | goto out1; |
| 6366 | |
| 6367 | vmx_msr_bitmap_legacy_x2apic = |
| 6368 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6369 | if (!vmx_msr_bitmap_legacy_x2apic) |
| 6370 | goto out2; |
| 6371 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6372 | vmx_msr_bitmap_legacy_x2apic_apicv_inactive = |
| 6373 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6374 | if (!vmx_msr_bitmap_legacy_x2apic_apicv_inactive) |
| 6375 | goto out3; |
| 6376 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6377 | vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6378 | if (!vmx_msr_bitmap_longmode) |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6379 | goto out4; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6380 | |
| 6381 | vmx_msr_bitmap_longmode_x2apic = |
| 6382 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6383 | if (!vmx_msr_bitmap_longmode_x2apic) |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6384 | goto out5; |
| 6385 | |
| 6386 | vmx_msr_bitmap_longmode_x2apic_apicv_inactive = |
| 6387 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6388 | if (!vmx_msr_bitmap_longmode_x2apic_apicv_inactive) |
| 6389 | goto out6; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 6390 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6391 | vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6392 | if (!vmx_vmread_bitmap) |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6393 | goto out7; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6394 | |
| 6395 | vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6396 | if (!vmx_vmwrite_bitmap) |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6397 | goto out8; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6398 | |
| 6399 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 6400 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 6401 | |
| 6402 | /* |
| 6403 | * Allow direct access to the PC debug port (it is often used for I/O |
| 6404 | * delays, but the vmexits simply slow things down). |
| 6405 | */ |
| 6406 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); |
| 6407 | clear_bit(0x80, vmx_io_bitmap_a); |
| 6408 | |
| 6409 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); |
| 6410 | |
| 6411 | memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE); |
| 6412 | memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE); |
| 6413 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6414 | if (setup_vmcs_config(&vmcs_config) < 0) { |
| 6415 | r = -EIO; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6416 | goto out9; |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6417 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6418 | |
| 6419 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 6420 | kvm_enable_efer_bits(EFER_NX); |
| 6421 | |
| 6422 | if (!cpu_has_vmx_vpid()) |
| 6423 | enable_vpid = 0; |
| 6424 | if (!cpu_has_vmx_shadow_vmcs()) |
| 6425 | enable_shadow_vmcs = 0; |
| 6426 | if (enable_shadow_vmcs) |
| 6427 | init_vmcs_shadow_fields(); |
| 6428 | |
| 6429 | if (!cpu_has_vmx_ept() || |
| 6430 | !cpu_has_vmx_ept_4levels()) { |
| 6431 | enable_ept = 0; |
| 6432 | enable_unrestricted_guest = 0; |
| 6433 | enable_ept_ad_bits = 0; |
| 6434 | } |
| 6435 | |
| 6436 | if (!cpu_has_vmx_ept_ad_bits()) |
| 6437 | enable_ept_ad_bits = 0; |
| 6438 | |
| 6439 | if (!cpu_has_vmx_unrestricted_guest()) |
| 6440 | enable_unrestricted_guest = 0; |
| 6441 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 6442 | if (!cpu_has_vmx_flexpriority()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6443 | flexpriority_enabled = 0; |
| 6444 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 6445 | /* |
| 6446 | * set_apic_access_page_addr() is used to reload apic access |
| 6447 | * page upon invalidation. No need to do anything if not |
| 6448 | * using the APIC_ACCESS_ADDR VMCS field. |
| 6449 | */ |
| 6450 | if (!flexpriority_enabled) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6451 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6452 | |
| 6453 | if (!cpu_has_vmx_tpr_shadow()) |
| 6454 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 6455 | |
| 6456 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 6457 | kvm_disable_largepages(); |
| 6458 | |
| 6459 | if (!cpu_has_vmx_ple()) |
| 6460 | ple_gap = 0; |
| 6461 | |
| 6462 | if (!cpu_has_vmx_apicv()) |
| 6463 | enable_apicv = 0; |
| 6464 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 6465 | if (cpu_has_vmx_tsc_scaling()) { |
| 6466 | kvm_has_tsc_control = true; |
| 6467 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 6468 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 6469 | } |
| 6470 | |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6471 | vmx_disable_intercept_for_msr(MSR_FS_BASE, false); |
| 6472 | vmx_disable_intercept_for_msr(MSR_GS_BASE, false); |
| 6473 | vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true); |
| 6474 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false); |
| 6475 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false); |
| 6476 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false); |
| 6477 | vmx_disable_intercept_for_msr(MSR_IA32_BNDCFGS, true); |
| 6478 | |
| 6479 | memcpy(vmx_msr_bitmap_legacy_x2apic, |
| 6480 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
| 6481 | memcpy(vmx_msr_bitmap_longmode_x2apic, |
| 6482 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6483 | memcpy(vmx_msr_bitmap_legacy_x2apic_apicv_inactive, |
| 6484 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
| 6485 | memcpy(vmx_msr_bitmap_longmode_x2apic_apicv_inactive, |
| 6486 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6487 | |
Wanpeng Li | 04bb92e | 2015-09-16 19:31:11 +0800 | [diff] [blame] | 6488 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 6489 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6490 | /* |
| 6491 | * enable_apicv && kvm_vcpu_apicv_active() |
| 6492 | */ |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 6493 | for (msr = 0x800; msr <= 0x8ff; msr++) |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6494 | vmx_disable_intercept_msr_read_x2apic(msr, true); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6495 | |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 6496 | /* TMCCT */ |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6497 | vmx_enable_intercept_msr_read_x2apic(0x839, true); |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 6498 | /* TPR */ |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6499 | vmx_disable_intercept_msr_write_x2apic(0x808, true); |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 6500 | /* EOI */ |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6501 | vmx_disable_intercept_msr_write_x2apic(0x80b, true); |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 6502 | /* SELF-IPI */ |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6503 | vmx_disable_intercept_msr_write_x2apic(0x83f, true); |
| 6504 | |
| 6505 | /* |
| 6506 | * (enable_apicv && !kvm_vcpu_apicv_active()) || |
| 6507 | * !enable_apicv |
| 6508 | */ |
| 6509 | /* TPR */ |
| 6510 | vmx_disable_intercept_msr_read_x2apic(0x808, false); |
| 6511 | vmx_disable_intercept_msr_write_x2apic(0x808, false); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6512 | |
| 6513 | if (enable_ept) { |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6514 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6515 | (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull, |
| 6516 | (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull, |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6517 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 6518 | cpu_has_vmx_ept_execute_only() ? |
| 6519 | 0ull : VMX_EPT_READABLE_MASK); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6520 | ept_set_mmio_spte_mask(); |
| 6521 | kvm_enable_tdp(); |
| 6522 | } else |
| 6523 | kvm_disable_tdp(); |
| 6524 | |
| 6525 | update_ple_window_actual_max(); |
| 6526 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6527 | /* |
| 6528 | * Only enable PML when hardware supports PML feature, and both EPT |
| 6529 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 6530 | */ |
| 6531 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 6532 | enable_pml = 0; |
| 6533 | |
| 6534 | if (!enable_pml) { |
| 6535 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 6536 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 6537 | kvm_x86_ops->flush_log_dirty = NULL; |
| 6538 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 6539 | } |
| 6540 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6541 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 6542 | u64 vmx_msr; |
| 6543 | |
| 6544 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 6545 | cpu_preemption_timer_multi = |
| 6546 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 6547 | } else { |
| 6548 | kvm_x86_ops->set_hv_timer = NULL; |
| 6549 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 6550 | } |
| 6551 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6552 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
| 6553 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 6554 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 6555 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6556 | return alloc_kvm_area(); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6557 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6558 | out9: |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6559 | free_page((unsigned long)vmx_vmwrite_bitmap); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6560 | out8: |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6561 | free_page((unsigned long)vmx_vmread_bitmap); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6562 | out7: |
| 6563 | free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic_apicv_inactive); |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 6564 | out6: |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6565 | free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6566 | out5: |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6567 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6568 | out4: |
| 6569 | free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic_apicv_inactive); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6570 | out3: |
| 6571 | free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic); |
| 6572 | out2: |
| 6573 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
| 6574 | out1: |
| 6575 | free_page((unsigned long)vmx_io_bitmap_b); |
| 6576 | out: |
| 6577 | free_page((unsigned long)vmx_io_bitmap_a); |
| 6578 | |
| 6579 | return r; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6580 | } |
| 6581 | |
| 6582 | static __exit void hardware_unsetup(void) |
| 6583 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6584 | free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6585 | free_page((unsigned long)vmx_msr_bitmap_legacy_x2apic_apicv_inactive); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6586 | free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6587 | free_page((unsigned long)vmx_msr_bitmap_longmode_x2apic_apicv_inactive); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6588 | free_page((unsigned long)vmx_msr_bitmap_legacy); |
| 6589 | free_page((unsigned long)vmx_msr_bitmap_longmode); |
| 6590 | free_page((unsigned long)vmx_io_bitmap_b); |
| 6591 | free_page((unsigned long)vmx_io_bitmap_a); |
| 6592 | free_page((unsigned long)vmx_vmwrite_bitmap); |
| 6593 | free_page((unsigned long)vmx_vmread_bitmap); |
| 6594 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6595 | free_kvm_area(); |
| 6596 | } |
| 6597 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6598 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6599 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 6600 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 6601 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 6602 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6603 | { |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 6604 | if (ple_gap) |
| 6605 | grow_ple_window(vcpu); |
| 6606 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6607 | skip_emulated_instruction(vcpu); |
| 6608 | kvm_vcpu_on_spin(vcpu); |
| 6609 | |
| 6610 | return 1; |
| 6611 | } |
| 6612 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6613 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 6614 | { |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6615 | skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 6616 | return 1; |
| 6617 | } |
| 6618 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6619 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 6620 | { |
| 6621 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 6622 | return handle_nop(vcpu); |
| 6623 | } |
| 6624 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 6625 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 6626 | { |
| 6627 | return 1; |
| 6628 | } |
| 6629 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6630 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 6631 | { |
| 6632 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 6633 | return handle_nop(vcpu); |
| 6634 | } |
| 6635 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6636 | /* |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6637 | * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12. |
| 6638 | * We could reuse a single VMCS for all the L2 guests, but we also want the |
| 6639 | * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this |
| 6640 | * allows keeping them loaded on the processor, and in the future will allow |
| 6641 | * optimizations where prepare_vmcs02 doesn't need to set all the fields on |
| 6642 | * every entry if they never change. |
| 6643 | * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE |
| 6644 | * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first. |
| 6645 | * |
| 6646 | * The following functions allocate and free a vmcs02 in this pool. |
| 6647 | */ |
| 6648 | |
| 6649 | /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */ |
| 6650 | static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx) |
| 6651 | { |
| 6652 | struct vmcs02_list *item; |
| 6653 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 6654 | if (item->vmptr == vmx->nested.current_vmptr) { |
| 6655 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 6656 | return &item->vmcs02; |
| 6657 | } |
| 6658 | |
| 6659 | if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) { |
| 6660 | /* Recycle the least recently used VMCS. */ |
Geliang Tang | d74c0e6 | 2016-01-01 19:47:14 +0800 | [diff] [blame] | 6661 | item = list_last_entry(&vmx->nested.vmcs02_pool, |
| 6662 | struct vmcs02_list, list); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6663 | item->vmptr = vmx->nested.current_vmptr; |
| 6664 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 6665 | return &item->vmcs02; |
| 6666 | } |
| 6667 | |
| 6668 | /* Create a new VMCS */ |
Ioan Orghici | 0fa24ce | 2013-03-10 15:46:00 +0200 | [diff] [blame] | 6669 | item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6670 | if (!item) |
| 6671 | return NULL; |
| 6672 | item->vmcs02.vmcs = alloc_vmcs(); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 6673 | item->vmcs02.shadow_vmcs = NULL; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6674 | if (!item->vmcs02.vmcs) { |
| 6675 | kfree(item); |
| 6676 | return NULL; |
| 6677 | } |
| 6678 | loaded_vmcs_init(&item->vmcs02); |
| 6679 | item->vmptr = vmx->nested.current_vmptr; |
| 6680 | list_add(&(item->list), &(vmx->nested.vmcs02_pool)); |
| 6681 | vmx->nested.vmcs02_num++; |
| 6682 | return &item->vmcs02; |
| 6683 | } |
| 6684 | |
| 6685 | /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */ |
| 6686 | static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 6687 | { |
| 6688 | struct vmcs02_list *item; |
| 6689 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 6690 | if (item->vmptr == vmptr) { |
| 6691 | free_loaded_vmcs(&item->vmcs02); |
| 6692 | list_del(&item->list); |
| 6693 | kfree(item); |
| 6694 | vmx->nested.vmcs02_num--; |
| 6695 | return; |
| 6696 | } |
| 6697 | } |
| 6698 | |
| 6699 | /* |
| 6700 | * Free all VMCSs saved for this vcpu, except the one pointed by |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6701 | * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs |
| 6702 | * must be &vmx->vmcs01. |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6703 | */ |
| 6704 | static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx) |
| 6705 | { |
| 6706 | struct vmcs02_list *item, *n; |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6707 | |
| 6708 | WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6709 | list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) { |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6710 | /* |
| 6711 | * Something will leak if the above WARN triggers. Better than |
| 6712 | * a use-after-free. |
| 6713 | */ |
| 6714 | if (vmx->loaded_vmcs == &item->vmcs02) |
| 6715 | continue; |
| 6716 | |
| 6717 | free_loaded_vmcs(&item->vmcs02); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6718 | list_del(&item->list); |
| 6719 | kfree(item); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6720 | vmx->nested.vmcs02_num--; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6721 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6722 | } |
| 6723 | |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 6724 | /* |
| 6725 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 6726 | * set the success or error code of an emulated VMX instruction, as specified |
| 6727 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 6728 | */ |
| 6729 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 6730 | { |
| 6731 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 6732 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 6733 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 6734 | } |
| 6735 | |
| 6736 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 6737 | { |
| 6738 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 6739 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 6740 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 6741 | | X86_EFLAGS_CF); |
| 6742 | } |
| 6743 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 6744 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 6745 | u32 vm_instruction_error) |
| 6746 | { |
| 6747 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 6748 | /* |
| 6749 | * failValid writes the error number to the current VMCS, which |
| 6750 | * can't be done there isn't a current VMCS. |
| 6751 | */ |
| 6752 | nested_vmx_failInvalid(vcpu); |
| 6753 | return; |
| 6754 | } |
| 6755 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 6756 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 6757 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 6758 | | X86_EFLAGS_ZF); |
| 6759 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 6760 | /* |
| 6761 | * We don't need to force a shadow sync because |
| 6762 | * VM_INSTRUCTION_ERROR is not shadowed |
| 6763 | */ |
| 6764 | } |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 6765 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 6766 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 6767 | { |
| 6768 | /* TODO: not to reset guest simply here. */ |
| 6769 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 6770 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 6771 | } |
| 6772 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 6773 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 6774 | { |
| 6775 | struct vcpu_vmx *vmx = |
| 6776 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 6777 | |
| 6778 | vmx->nested.preemption_timer_expired = true; |
| 6779 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 6780 | kvm_vcpu_kick(&vmx->vcpu); |
| 6781 | |
| 6782 | return HRTIMER_NORESTART; |
| 6783 | } |
| 6784 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6785 | /* |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6786 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 6787 | * exit caused by such an instruction (run by a guest hypervisor). |
| 6788 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 6789 | * #UD or #GP. |
| 6790 | */ |
| 6791 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 6792 | unsigned long exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6793 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6794 | { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6795 | gva_t off; |
| 6796 | bool exn; |
| 6797 | struct kvm_segment s; |
| 6798 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6799 | /* |
| 6800 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 6801 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 6802 | * addressing components of the operand. Only the displacement part |
| 6803 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 6804 | * For how an actual address is calculated from all these components, |
| 6805 | * refer to Vol. 1, "Operand Addressing". |
| 6806 | */ |
| 6807 | int scaling = vmx_instruction_info & 3; |
| 6808 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 6809 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 6810 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 6811 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 6812 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 6813 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 6814 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 6815 | |
| 6816 | if (is_reg) { |
| 6817 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6818 | return 1; |
| 6819 | } |
| 6820 | |
| 6821 | /* Addr = segment_base + offset */ |
| 6822 | /* offset = base + [index * scale] + displacement */ |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6823 | off = exit_qualification; /* holds the displacement */ |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6824 | if (base_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6825 | off += kvm_register_read(vcpu, base_reg); |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6826 | if (index_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6827 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
| 6828 | vmx_get_segment(vcpu, &s, seg_reg); |
| 6829 | *ret = s.base + off; |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6830 | |
| 6831 | if (addr_size == 1) /* 32 bit */ |
| 6832 | *ret &= 0xffffffff; |
| 6833 | |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6834 | /* Checks for #GP/#SS exceptions. */ |
| 6835 | exn = false; |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 6836 | if (is_long_mode(vcpu)) { |
| 6837 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 6838 | * non-canonical form. This is the only check on the memory |
| 6839 | * destination for long mode! |
| 6840 | */ |
| 6841 | exn = is_noncanonical_address(*ret); |
| 6842 | } else if (is_protmode(vcpu)) { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6843 | /* Protected mode: apply checks for segment validity in the |
| 6844 | * following order: |
| 6845 | * - segment type check (#GP(0) may be thrown) |
| 6846 | * - usability check (#GP(0)/#SS(0)) |
| 6847 | * - limit check (#GP(0)/#SS(0)) |
| 6848 | */ |
| 6849 | if (wr) |
| 6850 | /* #GP(0) if the destination operand is located in a |
| 6851 | * read-only data segment or any code segment. |
| 6852 | */ |
| 6853 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 6854 | else |
| 6855 | /* #GP(0) if the source operand is located in an |
| 6856 | * execute-only code segment |
| 6857 | */ |
| 6858 | exn = ((s.type & 0xa) == 8); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 6859 | if (exn) { |
| 6860 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 6861 | return 1; |
| 6862 | } |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6863 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 6864 | */ |
| 6865 | exn = (s.unusable != 0); |
| 6866 | /* Protected mode: #GP(0)/#SS(0) if the memory |
| 6867 | * operand is outside the segment limit. |
| 6868 | */ |
| 6869 | exn = exn || (off + sizeof(u64) > s.limit); |
| 6870 | } |
| 6871 | if (exn) { |
| 6872 | kvm_queue_exception_e(vcpu, |
| 6873 | seg_reg == VCPU_SREG_SS ? |
| 6874 | SS_VECTOR : GP_VECTOR, |
| 6875 | 0); |
| 6876 | return 1; |
| 6877 | } |
| 6878 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6879 | return 0; |
| 6880 | } |
| 6881 | |
| 6882 | /* |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6883 | * This function performs the various checks including |
| 6884 | * - if it's 4KB aligned |
| 6885 | * - No bits beyond the physical address width are set |
| 6886 | * - Returns 0 on success or else 1 |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6887 | * (Intel SDM Section 30.3) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6888 | */ |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6889 | static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason, |
| 6890 | gpa_t *vmpointer) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6891 | { |
| 6892 | gva_t gva; |
| 6893 | gpa_t vmptr; |
| 6894 | struct x86_exception e; |
| 6895 | struct page *page; |
| 6896 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6897 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 6898 | |
| 6899 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6900 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6901 | return 1; |
| 6902 | |
| 6903 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vmptr, |
| 6904 | sizeof(vmptr), &e)) { |
| 6905 | kvm_inject_page_fault(vcpu, &e); |
| 6906 | return 1; |
| 6907 | } |
| 6908 | |
| 6909 | switch (exit_reason) { |
| 6910 | case EXIT_REASON_VMON: |
| 6911 | /* |
| 6912 | * SDM 3: 24.11.5 |
| 6913 | * The first 4 bytes of VMXON region contain the supported |
| 6914 | * VMCS revision identifier |
| 6915 | * |
| 6916 | * Note - IA32_VMX_BASIC[48] will never be 1 |
| 6917 | * for the nested case; |
| 6918 | * which replaces physical address width with 32 |
| 6919 | * |
| 6920 | */ |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 6921 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6922 | nested_vmx_failInvalid(vcpu); |
| 6923 | skip_emulated_instruction(vcpu); |
| 6924 | return 1; |
| 6925 | } |
| 6926 | |
| 6927 | page = nested_get_page(vcpu, vmptr); |
| 6928 | if (page == NULL || |
| 6929 | *(u32 *)kmap(page) != VMCS12_REVISION) { |
| 6930 | nested_vmx_failInvalid(vcpu); |
| 6931 | kunmap(page); |
| 6932 | skip_emulated_instruction(vcpu); |
| 6933 | return 1; |
| 6934 | } |
| 6935 | kunmap(page); |
| 6936 | vmx->nested.vmxon_ptr = vmptr; |
| 6937 | break; |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6938 | case EXIT_REASON_VMCLEAR: |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 6939 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6940 | nested_vmx_failValid(vcpu, |
| 6941 | VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 6942 | skip_emulated_instruction(vcpu); |
| 6943 | return 1; |
| 6944 | } |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6945 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6946 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 6947 | nested_vmx_failValid(vcpu, |
| 6948 | VMXERR_VMCLEAR_VMXON_POINTER); |
| 6949 | skip_emulated_instruction(vcpu); |
| 6950 | return 1; |
| 6951 | } |
| 6952 | break; |
| 6953 | case EXIT_REASON_VMPTRLD: |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 6954 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6955 | nested_vmx_failValid(vcpu, |
| 6956 | VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 6957 | skip_emulated_instruction(vcpu); |
| 6958 | return 1; |
| 6959 | } |
| 6960 | |
| 6961 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 6962 | nested_vmx_failValid(vcpu, |
| 6963 | VMXERR_VMCLEAR_VMXON_POINTER); |
| 6964 | skip_emulated_instruction(vcpu); |
| 6965 | return 1; |
| 6966 | } |
| 6967 | break; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6968 | default: |
| 6969 | return 1; /* shouldn't happen */ |
| 6970 | } |
| 6971 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6972 | if (vmpointer) |
| 6973 | *vmpointer = vmptr; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6974 | return 0; |
| 6975 | } |
| 6976 | |
| 6977 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6978 | * Emulate the VMXON instruction. |
| 6979 | * Currently, we just remember that VMX is active, and do not save or even |
| 6980 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 6981 | * do not currently need to store anything in that guest-allocated memory |
| 6982 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 6983 | * argument is different from the VMXON pointer (which the spec says they do). |
| 6984 | */ |
| 6985 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 6986 | { |
| 6987 | struct kvm_segment cs; |
| 6988 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 6989 | struct vmcs *shadow_vmcs; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 6990 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 6991 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 6992 | |
| 6993 | /* The Intel VMX Instruction Reference lists a bunch of bits that |
| 6994 | * are prerequisite to running VMXON, most notably cr4.VMXE must be |
| 6995 | * set to 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 6996 | * Otherwise, we should fail with #UD. We test these now: |
| 6997 | */ |
| 6998 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) || |
| 6999 | !kvm_read_cr0_bits(vcpu, X86_CR0_PE) || |
| 7000 | (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
| 7001 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7002 | return 1; |
| 7003 | } |
| 7004 | |
| 7005 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 7006 | if (is_long_mode(vcpu) && !cs.l) { |
| 7007 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7008 | return 1; |
| 7009 | } |
| 7010 | |
| 7011 | if (vmx_get_cpl(vcpu)) { |
| 7012 | kvm_inject_gp(vcpu, 0); |
| 7013 | return 1; |
| 7014 | } |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7015 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7016 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7017 | return 1; |
| 7018 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7019 | if (vmx->nested.vmxon) { |
| 7020 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
| 7021 | skip_emulated_instruction(vcpu); |
| 7022 | return 1; |
| 7023 | } |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7024 | |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 7025 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7026 | != VMXON_NEEDED_FEATURES) { |
| 7027 | kvm_inject_gp(vcpu, 0); |
| 7028 | return 1; |
| 7029 | } |
| 7030 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7031 | if (cpu_has_vmx_msr_bitmap()) { |
| 7032 | vmx->nested.msr_bitmap = |
| 7033 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7034 | if (!vmx->nested.msr_bitmap) |
| 7035 | goto out_msr_bitmap; |
| 7036 | } |
| 7037 | |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7038 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 7039 | if (!vmx->nested.cached_vmcs12) |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7040 | goto out_cached_vmcs12; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7041 | |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7042 | if (enable_shadow_vmcs) { |
| 7043 | shadow_vmcs = alloc_vmcs(); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7044 | if (!shadow_vmcs) |
| 7045 | goto out_shadow_vmcs; |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7046 | /* mark vmcs as shadow */ |
| 7047 | shadow_vmcs->revision_id |= (1u << 31); |
| 7048 | /* init shadow vmcs */ |
| 7049 | vmcs_clear(shadow_vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7050 | vmx->vmcs01.shadow_vmcs = shadow_vmcs; |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7051 | } |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7052 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7053 | INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool)); |
| 7054 | vmx->nested.vmcs02_num = 0; |
| 7055 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7056 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
Wanpeng Li | f15a75e | 2016-08-30 16:14:01 +0800 | [diff] [blame] | 7057 | HRTIMER_MODE_REL_PINNED); |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7058 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 7059 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7060 | vmx->nested.vmxon = true; |
| 7061 | |
| 7062 | skip_emulated_instruction(vcpu); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 7063 | nested_vmx_succeed(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7064 | return 1; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7065 | |
| 7066 | out_shadow_vmcs: |
| 7067 | kfree(vmx->nested.cached_vmcs12); |
| 7068 | |
| 7069 | out_cached_vmcs12: |
| 7070 | free_page((unsigned long)vmx->nested.msr_bitmap); |
| 7071 | |
| 7072 | out_msr_bitmap: |
| 7073 | return -ENOMEM; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7074 | } |
| 7075 | |
| 7076 | /* |
| 7077 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 7078 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 7079 | * slightly different). It also specifies what exception to inject otherwise. |
| 7080 | */ |
| 7081 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 7082 | { |
| 7083 | struct kvm_segment cs; |
| 7084 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7085 | |
| 7086 | if (!vmx->nested.vmxon) { |
| 7087 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7088 | return 0; |
| 7089 | } |
| 7090 | |
| 7091 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 7092 | if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) || |
| 7093 | (is_long_mode(vcpu) && !cs.l)) { |
| 7094 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7095 | return 0; |
| 7096 | } |
| 7097 | |
| 7098 | if (vmx_get_cpl(vcpu)) { |
| 7099 | kvm_inject_gp(vcpu, 0); |
| 7100 | return 0; |
| 7101 | } |
| 7102 | |
| 7103 | return 1; |
| 7104 | } |
| 7105 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7106 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
| 7107 | { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7108 | if (vmx->nested.current_vmptr == -1ull) |
| 7109 | return; |
| 7110 | |
| 7111 | /* current_vmptr and current_vmcs12 are always set/reset together */ |
| 7112 | if (WARN_ON(vmx->nested.current_vmcs12 == NULL)) |
| 7113 | return; |
| 7114 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7115 | if (enable_shadow_vmcs) { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7116 | /* copy to memory all shadowed fields in case |
| 7117 | they were modified */ |
| 7118 | copy_shadow_to_vmcs12(vmx); |
| 7119 | vmx->nested.sync_shadow_vmcs = false; |
Xiao Guangrong | 7ec3629 | 2015-09-09 14:05:56 +0800 | [diff] [blame] | 7120 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 7121 | SECONDARY_EXEC_SHADOW_VMCS); |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7122 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7123 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7124 | vmx->nested.posted_intr_nv = -1; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7125 | |
| 7126 | /* Flush VMCS12 to guest memory */ |
| 7127 | memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12, |
| 7128 | VMCS12_SIZE); |
| 7129 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7130 | kunmap(vmx->nested.current_vmcs12_page); |
| 7131 | nested_release_page(vmx->nested.current_vmcs12_page); |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7132 | vmx->nested.current_vmptr = -1ull; |
| 7133 | vmx->nested.current_vmcs12 = NULL; |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7134 | } |
| 7135 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7136 | /* |
| 7137 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 7138 | * just stops using VMX. |
| 7139 | */ |
| 7140 | static void free_nested(struct vcpu_vmx *vmx) |
| 7141 | { |
| 7142 | if (!vmx->nested.vmxon) |
| 7143 | return; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7144 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7145 | vmx->nested.vmxon = false; |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 7146 | free_vpid(vmx->nested.vpid02); |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7147 | nested_release_vmcs12(vmx); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7148 | if (vmx->nested.msr_bitmap) { |
| 7149 | free_page((unsigned long)vmx->nested.msr_bitmap); |
| 7150 | vmx->nested.msr_bitmap = NULL; |
| 7151 | } |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7152 | if (enable_shadow_vmcs) { |
| 7153 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 7154 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 7155 | vmx->vmcs01.shadow_vmcs = NULL; |
| 7156 | } |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7157 | kfree(vmx->nested.cached_vmcs12); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 7158 | /* Unpin physical memory we referred to in current vmcs02 */ |
| 7159 | if (vmx->nested.apic_access_page) { |
| 7160 | nested_release_page(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 7161 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 7162 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7163 | if (vmx->nested.virtual_apic_page) { |
| 7164 | nested_release_page(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 7165 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7166 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7167 | if (vmx->nested.pi_desc_page) { |
| 7168 | kunmap(vmx->nested.pi_desc_page); |
| 7169 | nested_release_page(vmx->nested.pi_desc_page); |
| 7170 | vmx->nested.pi_desc_page = NULL; |
| 7171 | vmx->nested.pi_desc = NULL; |
| 7172 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7173 | |
| 7174 | nested_free_all_saved_vmcss(vmx); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7175 | } |
| 7176 | |
| 7177 | /* Emulate the VMXOFF instruction */ |
| 7178 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 7179 | { |
| 7180 | if (!nested_vmx_check_permission(vcpu)) |
| 7181 | return 1; |
| 7182 | free_nested(to_vmx(vcpu)); |
| 7183 | skip_emulated_instruction(vcpu); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 7184 | nested_vmx_succeed(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7185 | return 1; |
| 7186 | } |
| 7187 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7188 | /* Emulate the VMCLEAR instruction */ |
| 7189 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 7190 | { |
| 7191 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7192 | gpa_t vmptr; |
| 7193 | struct vmcs12 *vmcs12; |
| 7194 | struct page *page; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7195 | |
| 7196 | if (!nested_vmx_check_permission(vcpu)) |
| 7197 | return 1; |
| 7198 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7199 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr)) |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7200 | return 1; |
| 7201 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7202 | if (vmptr == vmx->nested.current_vmptr) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7203 | nested_release_vmcs12(vmx); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7204 | |
| 7205 | page = nested_get_page(vcpu, vmptr); |
| 7206 | if (page == NULL) { |
| 7207 | /* |
| 7208 | * For accurate processor emulation, VMCLEAR beyond available |
| 7209 | * physical memory should do nothing at all. However, it is |
| 7210 | * possible that a nested vmx bug, not a guest hypervisor bug, |
| 7211 | * resulted in this case, so let's shut down before doing any |
| 7212 | * more damage: |
| 7213 | */ |
| 7214 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
| 7215 | return 1; |
| 7216 | } |
| 7217 | vmcs12 = kmap(page); |
| 7218 | vmcs12->launch_state = 0; |
| 7219 | kunmap(page); |
| 7220 | nested_release_page(page); |
| 7221 | |
| 7222 | nested_free_vmcs02(vmx, vmptr); |
| 7223 | |
| 7224 | skip_emulated_instruction(vcpu); |
| 7225 | nested_vmx_succeed(vcpu); |
| 7226 | return 1; |
| 7227 | } |
| 7228 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7229 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 7230 | |
| 7231 | /* Emulate the VMLAUNCH instruction */ |
| 7232 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 7233 | { |
| 7234 | return nested_vmx_run(vcpu, true); |
| 7235 | } |
| 7236 | |
| 7237 | /* Emulate the VMRESUME instruction */ |
| 7238 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 7239 | { |
| 7240 | |
| 7241 | return nested_vmx_run(vcpu, false); |
| 7242 | } |
| 7243 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7244 | enum vmcs_field_type { |
| 7245 | VMCS_FIELD_TYPE_U16 = 0, |
| 7246 | VMCS_FIELD_TYPE_U64 = 1, |
| 7247 | VMCS_FIELD_TYPE_U32 = 2, |
| 7248 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 |
| 7249 | }; |
| 7250 | |
| 7251 | static inline int vmcs_field_type(unsigned long field) |
| 7252 | { |
| 7253 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
| 7254 | return VMCS_FIELD_TYPE_U32; |
| 7255 | return (field >> 13) & 0x3 ; |
| 7256 | } |
| 7257 | |
| 7258 | static inline int vmcs_field_readonly(unsigned long field) |
| 7259 | { |
| 7260 | return (((field >> 10) & 0x3) == 1); |
| 7261 | } |
| 7262 | |
| 7263 | /* |
| 7264 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 7265 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 7266 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 7267 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 7268 | * 64-bit fields are to be returned). |
| 7269 | */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7270 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
| 7271 | unsigned long field, u64 *ret) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7272 | { |
| 7273 | short offset = vmcs_field_to_offset(field); |
| 7274 | char *p; |
| 7275 | |
| 7276 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7277 | return offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7278 | |
| 7279 | p = ((char *)(get_vmcs12(vcpu))) + offset; |
| 7280 | |
| 7281 | switch (vmcs_field_type(field)) { |
| 7282 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7283 | *ret = *((natural_width *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7284 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7285 | case VMCS_FIELD_TYPE_U16: |
| 7286 | *ret = *((u16 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7287 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7288 | case VMCS_FIELD_TYPE_U32: |
| 7289 | *ret = *((u32 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7290 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7291 | case VMCS_FIELD_TYPE_U64: |
| 7292 | *ret = *((u64 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7293 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7294 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7295 | WARN_ON(1); |
| 7296 | return -ENOENT; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7297 | } |
| 7298 | } |
| 7299 | |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7300 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7301 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
| 7302 | unsigned long field, u64 field_value){ |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7303 | short offset = vmcs_field_to_offset(field); |
| 7304 | char *p = ((char *) get_vmcs12(vcpu)) + offset; |
| 7305 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7306 | return offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7307 | |
| 7308 | switch (vmcs_field_type(field)) { |
| 7309 | case VMCS_FIELD_TYPE_U16: |
| 7310 | *(u16 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7311 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7312 | case VMCS_FIELD_TYPE_U32: |
| 7313 | *(u32 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7314 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7315 | case VMCS_FIELD_TYPE_U64: |
| 7316 | *(u64 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7317 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7318 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7319 | *(natural_width *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7320 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7321 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7322 | WARN_ON(1); |
| 7323 | return -ENOENT; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7324 | } |
| 7325 | |
| 7326 | } |
| 7327 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7328 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 7329 | { |
| 7330 | int i; |
| 7331 | unsigned long field; |
| 7332 | u64 field_value; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7333 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7334 | const unsigned long *fields = shadow_read_write_fields; |
| 7335 | const int num_fields = max_shadow_read_write_fields; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7336 | |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 7337 | preempt_disable(); |
| 7338 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7339 | vmcs_load(shadow_vmcs); |
| 7340 | |
| 7341 | for (i = 0; i < num_fields; i++) { |
| 7342 | field = fields[i]; |
| 7343 | switch (vmcs_field_type(field)) { |
| 7344 | case VMCS_FIELD_TYPE_U16: |
| 7345 | field_value = vmcs_read16(field); |
| 7346 | break; |
| 7347 | case VMCS_FIELD_TYPE_U32: |
| 7348 | field_value = vmcs_read32(field); |
| 7349 | break; |
| 7350 | case VMCS_FIELD_TYPE_U64: |
| 7351 | field_value = vmcs_read64(field); |
| 7352 | break; |
| 7353 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7354 | field_value = vmcs_readl(field); |
| 7355 | break; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7356 | default: |
| 7357 | WARN_ON(1); |
| 7358 | continue; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7359 | } |
| 7360 | vmcs12_write_any(&vmx->vcpu, field, field_value); |
| 7361 | } |
| 7362 | |
| 7363 | vmcs_clear(shadow_vmcs); |
| 7364 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 7365 | |
| 7366 | preempt_enable(); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7367 | } |
| 7368 | |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7369 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 7370 | { |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7371 | const unsigned long *fields[] = { |
| 7372 | shadow_read_write_fields, |
| 7373 | shadow_read_only_fields |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7374 | }; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7375 | const int max_fields[] = { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7376 | max_shadow_read_write_fields, |
| 7377 | max_shadow_read_only_fields |
| 7378 | }; |
| 7379 | int i, q; |
| 7380 | unsigned long field; |
| 7381 | u64 field_value = 0; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7382 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7383 | |
| 7384 | vmcs_load(shadow_vmcs); |
| 7385 | |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7386 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7387 | for (i = 0; i < max_fields[q]; i++) { |
| 7388 | field = fields[q][i]; |
| 7389 | vmcs12_read_any(&vmx->vcpu, field, &field_value); |
| 7390 | |
| 7391 | switch (vmcs_field_type(field)) { |
| 7392 | case VMCS_FIELD_TYPE_U16: |
| 7393 | vmcs_write16(field, (u16)field_value); |
| 7394 | break; |
| 7395 | case VMCS_FIELD_TYPE_U32: |
| 7396 | vmcs_write32(field, (u32)field_value); |
| 7397 | break; |
| 7398 | case VMCS_FIELD_TYPE_U64: |
| 7399 | vmcs_write64(field, (u64)field_value); |
| 7400 | break; |
| 7401 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7402 | vmcs_writel(field, (long)field_value); |
| 7403 | break; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7404 | default: |
| 7405 | WARN_ON(1); |
| 7406 | break; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7407 | } |
| 7408 | } |
| 7409 | } |
| 7410 | |
| 7411 | vmcs_clear(shadow_vmcs); |
| 7412 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 7413 | } |
| 7414 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7415 | /* |
| 7416 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 7417 | * used before) all generate the same failure when it is missing. |
| 7418 | */ |
| 7419 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 7420 | { |
| 7421 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7422 | if (vmx->nested.current_vmptr == -1ull) { |
| 7423 | nested_vmx_failInvalid(vcpu); |
| 7424 | skip_emulated_instruction(vcpu); |
| 7425 | return 0; |
| 7426 | } |
| 7427 | return 1; |
| 7428 | } |
| 7429 | |
| 7430 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 7431 | { |
| 7432 | unsigned long field; |
| 7433 | u64 field_value; |
| 7434 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7435 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7436 | gva_t gva = 0; |
| 7437 | |
| 7438 | if (!nested_vmx_check_permission(vcpu) || |
| 7439 | !nested_vmx_check_vmcs12(vcpu)) |
| 7440 | return 1; |
| 7441 | |
| 7442 | /* Decode instruction info and find the field to read */ |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7443 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7444 | /* Read the field, zero-extended to a u64 field_value */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7445 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7446 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 7447 | skip_emulated_instruction(vcpu); |
| 7448 | return 1; |
| 7449 | } |
| 7450 | /* |
| 7451 | * Now copy part of this value to register or memory, as requested. |
| 7452 | * Note that the number of bits actually copied is 32 or 64 depending |
| 7453 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 7454 | */ |
| 7455 | if (vmx_instruction_info & (1u << 10)) { |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7456 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7457 | field_value); |
| 7458 | } else { |
| 7459 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7460 | vmx_instruction_info, true, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7461 | return 1; |
| 7462 | /* _system ok, as nested_vmx_check_permission verified cpl=0 */ |
| 7463 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
| 7464 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); |
| 7465 | } |
| 7466 | |
| 7467 | nested_vmx_succeed(vcpu); |
| 7468 | skip_emulated_instruction(vcpu); |
| 7469 | return 1; |
| 7470 | } |
| 7471 | |
| 7472 | |
| 7473 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 7474 | { |
| 7475 | unsigned long field; |
| 7476 | gva_t gva; |
| 7477 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7478 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7479 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 7480 | * mode, and eventually we need to write that into a field of several |
| 7481 | * possible lengths. The code below first zero-extends the value to 64 |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 7482 | * bit (field_value), and then copies only the appropriate number of |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7483 | * bits into the vmcs12 field. |
| 7484 | */ |
| 7485 | u64 field_value = 0; |
| 7486 | struct x86_exception e; |
| 7487 | |
| 7488 | if (!nested_vmx_check_permission(vcpu) || |
| 7489 | !nested_vmx_check_vmcs12(vcpu)) |
| 7490 | return 1; |
| 7491 | |
| 7492 | if (vmx_instruction_info & (1u << 10)) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7493 | field_value = kvm_register_readl(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7494 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 7495 | else { |
| 7496 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7497 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7498 | return 1; |
| 7499 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7500 | &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7501 | kvm_inject_page_fault(vcpu, &e); |
| 7502 | return 1; |
| 7503 | } |
| 7504 | } |
| 7505 | |
| 7506 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7507 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7508 | if (vmcs_field_readonly(field)) { |
| 7509 | nested_vmx_failValid(vcpu, |
| 7510 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
| 7511 | skip_emulated_instruction(vcpu); |
| 7512 | return 1; |
| 7513 | } |
| 7514 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7515 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7516 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 7517 | skip_emulated_instruction(vcpu); |
| 7518 | return 1; |
| 7519 | } |
| 7520 | |
| 7521 | nested_vmx_succeed(vcpu); |
| 7522 | skip_emulated_instruction(vcpu); |
| 7523 | return 1; |
| 7524 | } |
| 7525 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7526 | /* Emulate the VMPTRLD instruction */ |
| 7527 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 7528 | { |
| 7529 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7530 | gpa_t vmptr; |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7531 | |
| 7532 | if (!nested_vmx_check_permission(vcpu)) |
| 7533 | return 1; |
| 7534 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7535 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr)) |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7536 | return 1; |
| 7537 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7538 | if (vmx->nested.current_vmptr != vmptr) { |
| 7539 | struct vmcs12 *new_vmcs12; |
| 7540 | struct page *page; |
| 7541 | page = nested_get_page(vcpu, vmptr); |
| 7542 | if (page == NULL) { |
| 7543 | nested_vmx_failInvalid(vcpu); |
| 7544 | skip_emulated_instruction(vcpu); |
| 7545 | return 1; |
| 7546 | } |
| 7547 | new_vmcs12 = kmap(page); |
| 7548 | if (new_vmcs12->revision_id != VMCS12_REVISION) { |
| 7549 | kunmap(page); |
| 7550 | nested_release_page_clean(page); |
| 7551 | nested_vmx_failValid(vcpu, |
| 7552 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 7553 | skip_emulated_instruction(vcpu); |
| 7554 | return 1; |
| 7555 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7556 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7557 | nested_release_vmcs12(vmx); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7558 | vmx->nested.current_vmptr = vmptr; |
| 7559 | vmx->nested.current_vmcs12 = new_vmcs12; |
| 7560 | vmx->nested.current_vmcs12_page = page; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7561 | /* |
| 7562 | * Load VMCS12 from guest memory since it is not already |
| 7563 | * cached. |
| 7564 | */ |
| 7565 | memcpy(vmx->nested.cached_vmcs12, |
| 7566 | vmx->nested.current_vmcs12, VMCS12_SIZE); |
| 7567 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7568 | if (enable_shadow_vmcs) { |
Xiao Guangrong | 7ec3629 | 2015-09-09 14:05:56 +0800 | [diff] [blame] | 7569 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 7570 | SECONDARY_EXEC_SHADOW_VMCS); |
Abel Gordon | 8a1b9dd | 2013-04-18 14:39:55 +0300 | [diff] [blame] | 7571 | vmcs_write64(VMCS_LINK_POINTER, |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7572 | __pa(vmx->vmcs01.shadow_vmcs)); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7573 | vmx->nested.sync_shadow_vmcs = true; |
| 7574 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7575 | } |
| 7576 | |
| 7577 | nested_vmx_succeed(vcpu); |
| 7578 | skip_emulated_instruction(vcpu); |
| 7579 | return 1; |
| 7580 | } |
| 7581 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7582 | /* Emulate the VMPTRST instruction */ |
| 7583 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 7584 | { |
| 7585 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7586 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7587 | gva_t vmcs_gva; |
| 7588 | struct x86_exception e; |
| 7589 | |
| 7590 | if (!nested_vmx_check_permission(vcpu)) |
| 7591 | return 1; |
| 7592 | |
| 7593 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7594 | vmx_instruction_info, true, &vmcs_gva)) |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7595 | return 1; |
| 7596 | /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */ |
| 7597 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
| 7598 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 7599 | sizeof(u64), &e)) { |
| 7600 | kvm_inject_page_fault(vcpu, &e); |
| 7601 | return 1; |
| 7602 | } |
| 7603 | nested_vmx_succeed(vcpu); |
| 7604 | skip_emulated_instruction(vcpu); |
| 7605 | return 1; |
| 7606 | } |
| 7607 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7608 | /* Emulate the INVEPT instruction */ |
| 7609 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 7610 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7611 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7612 | u32 vmx_instruction_info, types; |
| 7613 | unsigned long type; |
| 7614 | gva_t gva; |
| 7615 | struct x86_exception e; |
| 7616 | struct { |
| 7617 | u64 eptp, gpa; |
| 7618 | } operand; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7619 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7620 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
| 7621 | SECONDARY_EXEC_ENABLE_EPT) || |
| 7622 | !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7623 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7624 | return 1; |
| 7625 | } |
| 7626 | |
| 7627 | if (!nested_vmx_check_permission(vcpu)) |
| 7628 | return 1; |
| 7629 | |
| 7630 | if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) { |
| 7631 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7632 | return 1; |
| 7633 | } |
| 7634 | |
| 7635 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7636 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7637 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7638 | types = (vmx->nested.nested_vmx_ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7639 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 7640 | if (type >= 32 || !(types & (1 << type))) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7641 | nested_vmx_failValid(vcpu, |
| 7642 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Paolo Bonzini | 2849eb4 | 2016-03-18 16:53:29 +0100 | [diff] [blame] | 7643 | skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7644 | return 1; |
| 7645 | } |
| 7646 | |
| 7647 | /* According to the Intel VMX instruction reference, the memory |
| 7648 | * operand is read even if it isn't needed (e.g., for type==global) |
| 7649 | */ |
| 7650 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7651 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7652 | return 1; |
| 7653 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, |
| 7654 | sizeof(operand), &e)) { |
| 7655 | kvm_inject_page_fault(vcpu, &e); |
| 7656 | return 1; |
| 7657 | } |
| 7658 | |
| 7659 | switch (type) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7660 | case VMX_EPT_EXTENT_GLOBAL: |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 7661 | /* |
| 7662 | * TODO: track mappings and invalidate |
| 7663 | * single context requests appropriately |
| 7664 | */ |
| 7665 | case VMX_EPT_EXTENT_CONTEXT: |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7666 | kvm_mmu_sync_roots(vcpu); |
Liang Chen | 77c3913 | 2014-09-18 12:38:37 -0400 | [diff] [blame] | 7667 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7668 | nested_vmx_succeed(vcpu); |
| 7669 | break; |
| 7670 | default: |
| 7671 | BUG_ON(1); |
| 7672 | break; |
| 7673 | } |
| 7674 | |
| 7675 | skip_emulated_instruction(vcpu); |
| 7676 | return 1; |
| 7677 | } |
| 7678 | |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7679 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 7680 | { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7681 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7682 | u32 vmx_instruction_info; |
| 7683 | unsigned long type, types; |
| 7684 | gva_t gva; |
| 7685 | struct x86_exception e; |
| 7686 | int vpid; |
| 7687 | |
| 7688 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
| 7689 | SECONDARY_EXEC_ENABLE_VPID) || |
| 7690 | !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) { |
| 7691 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7692 | return 1; |
| 7693 | } |
| 7694 | |
| 7695 | if (!nested_vmx_check_permission(vcpu)) |
| 7696 | return 1; |
| 7697 | |
| 7698 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7699 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 7700 | |
| 7701 | types = (vmx->nested.nested_vmx_vpid_caps >> 8) & 0x7; |
| 7702 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 7703 | if (type >= 32 || !(types & (1 << type))) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7704 | nested_vmx_failValid(vcpu, |
| 7705 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Paolo Bonzini | f6870ee | 2016-03-18 16:53:42 +0100 | [diff] [blame] | 7706 | skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7707 | return 1; |
| 7708 | } |
| 7709 | |
| 7710 | /* according to the intel vmx instruction reference, the memory |
| 7711 | * operand is read even if it isn't needed (e.g., for type==global) |
| 7712 | */ |
| 7713 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 7714 | vmx_instruction_info, false, &gva)) |
| 7715 | return 1; |
| 7716 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &vpid, |
| 7717 | sizeof(u32), &e)) { |
| 7718 | kvm_inject_page_fault(vcpu, &e); |
| 7719 | return 1; |
| 7720 | } |
| 7721 | |
| 7722 | switch (type) { |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 7723 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
| 7724 | /* |
| 7725 | * Old versions of KVM use the single-context version so we |
| 7726 | * have to support it; just treat it the same as all-context. |
| 7727 | */ |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7728 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 7729 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7730 | nested_vmx_succeed(vcpu); |
| 7731 | break; |
| 7732 | default: |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 7733 | /* Trap individual address invalidation invvpid calls */ |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7734 | BUG_ON(1); |
| 7735 | break; |
| 7736 | } |
| 7737 | |
| 7738 | skip_emulated_instruction(vcpu); |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7739 | return 1; |
| 7740 | } |
| 7741 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7742 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 7743 | { |
| 7744 | unsigned long exit_qualification; |
| 7745 | |
| 7746 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 7747 | |
| 7748 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7749 | |
| 7750 | /* |
| 7751 | * PML buffer FULL happened while executing iret from NMI, |
| 7752 | * "blocked by NMI" bit has to be set before next VM entry. |
| 7753 | */ |
| 7754 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
| 7755 | cpu_has_virtual_nmis() && |
| 7756 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 7757 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 7758 | GUEST_INTR_STATE_NMI); |
| 7759 | |
| 7760 | /* |
| 7761 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 7762 | * here.., and there's no userspace involvement needed for PML. |
| 7763 | */ |
| 7764 | return 1; |
| 7765 | } |
| 7766 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7767 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 7768 | { |
| 7769 | kvm_lapic_expired_hv_timer(vcpu); |
| 7770 | return 1; |
| 7771 | } |
| 7772 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 7773 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7774 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 7775 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 7776 | * to be done to userspace and return 0. |
| 7777 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 7778 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7779 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 7780 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 7781 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7782 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7783 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7784 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 7785 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 7786 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 7787 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 7788 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 7789 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 7790 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7791 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7792 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7793 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7794 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7795 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7796 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7797 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7798 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7799 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7800 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7801 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7802 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 7803 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7804 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 7805 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 7806 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7807 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7808 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7809 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7810 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 7811 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7812 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 7813 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7814 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7815 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 7816 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7817 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7818 | [EXIT_REASON_INVEPT] = handle_invept, |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7819 | [EXIT_REASON_INVVPID] = handle_invvpid, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7820 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 7821 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7822 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7823 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7824 | }; |
| 7825 | |
| 7826 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 7827 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7828 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7829 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 7830 | struct vmcs12 *vmcs12) |
| 7831 | { |
| 7832 | unsigned long exit_qualification; |
| 7833 | gpa_t bitmap, last_bitmap; |
| 7834 | unsigned int port; |
| 7835 | int size; |
| 7836 | u8 b; |
| 7837 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7838 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
Zhihui Zhang | 2f0a639 | 2013-12-30 15:56:29 -0500 | [diff] [blame] | 7839 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7840 | |
| 7841 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7842 | |
| 7843 | port = exit_qualification >> 16; |
| 7844 | size = (exit_qualification & 7) + 1; |
| 7845 | |
| 7846 | last_bitmap = (gpa_t)-1; |
| 7847 | b = -1; |
| 7848 | |
| 7849 | while (size > 0) { |
| 7850 | if (port < 0x8000) |
| 7851 | bitmap = vmcs12->io_bitmap_a; |
| 7852 | else if (port < 0x10000) |
| 7853 | bitmap = vmcs12->io_bitmap_b; |
| 7854 | else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7855 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7856 | bitmap += (port & 0x7fff) / 8; |
| 7857 | |
| 7858 | if (last_bitmap != bitmap) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 7859 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7860 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7861 | if (b & (1 << (port & 7))) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7862 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7863 | |
| 7864 | port++; |
| 7865 | size--; |
| 7866 | last_bitmap = bitmap; |
| 7867 | } |
| 7868 | |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7869 | return false; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7870 | } |
| 7871 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7872 | /* |
| 7873 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 7874 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 7875 | * disinterest in the current event (read or write a specific MSR) by using an |
| 7876 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 7877 | */ |
| 7878 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 7879 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 7880 | { |
| 7881 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 7882 | gpa_t bitmap; |
| 7883 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 7884 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7885 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7886 | |
| 7887 | /* |
| 7888 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 7889 | * for the four combinations of read/write and low/high MSR numbers. |
| 7890 | * First we need to figure out which of the four to use: |
| 7891 | */ |
| 7892 | bitmap = vmcs12->msr_bitmap; |
| 7893 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 7894 | bitmap += 2048; |
| 7895 | if (msr_index >= 0xc0000000) { |
| 7896 | msr_index -= 0xc0000000; |
| 7897 | bitmap += 1024; |
| 7898 | } |
| 7899 | |
| 7900 | /* Then read the msr_index'th bit from this bitmap: */ |
| 7901 | if (msr_index < 1024*8) { |
| 7902 | unsigned char b; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 7903 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7904 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7905 | return 1 & (b >> (msr_index & 7)); |
| 7906 | } else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7907 | return true; /* let L1 handle the wrong parameter */ |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7908 | } |
| 7909 | |
| 7910 | /* |
| 7911 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 7912 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 7913 | * intercept (via guest_host_mask etc.) the current event. |
| 7914 | */ |
| 7915 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 7916 | struct vmcs12 *vmcs12) |
| 7917 | { |
| 7918 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7919 | int cr = exit_qualification & 15; |
| 7920 | int reg = (exit_qualification >> 8) & 15; |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 7921 | unsigned long val = kvm_register_readl(vcpu, reg); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7922 | |
| 7923 | switch ((exit_qualification >> 4) & 3) { |
| 7924 | case 0: /* mov to cr */ |
| 7925 | switch (cr) { |
| 7926 | case 0: |
| 7927 | if (vmcs12->cr0_guest_host_mask & |
| 7928 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7929 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7930 | break; |
| 7931 | case 3: |
| 7932 | if ((vmcs12->cr3_target_count >= 1 && |
| 7933 | vmcs12->cr3_target_value0 == val) || |
| 7934 | (vmcs12->cr3_target_count >= 2 && |
| 7935 | vmcs12->cr3_target_value1 == val) || |
| 7936 | (vmcs12->cr3_target_count >= 3 && |
| 7937 | vmcs12->cr3_target_value2 == val) || |
| 7938 | (vmcs12->cr3_target_count >= 4 && |
| 7939 | vmcs12->cr3_target_value3 == val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7940 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7941 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7942 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7943 | break; |
| 7944 | case 4: |
| 7945 | if (vmcs12->cr4_guest_host_mask & |
| 7946 | (vmcs12->cr4_read_shadow ^ val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7947 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7948 | break; |
| 7949 | case 8: |
| 7950 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7951 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7952 | break; |
| 7953 | } |
| 7954 | break; |
| 7955 | case 2: /* clts */ |
| 7956 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 7957 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7958 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7959 | break; |
| 7960 | case 1: /* mov from cr */ |
| 7961 | switch (cr) { |
| 7962 | case 3: |
| 7963 | if (vmcs12->cpu_based_vm_exec_control & |
| 7964 | CPU_BASED_CR3_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7965 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7966 | break; |
| 7967 | case 8: |
| 7968 | if (vmcs12->cpu_based_vm_exec_control & |
| 7969 | CPU_BASED_CR8_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7970 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7971 | break; |
| 7972 | } |
| 7973 | break; |
| 7974 | case 3: /* lmsw */ |
| 7975 | /* |
| 7976 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 7977 | * cr0. Other attempted changes are ignored, with no exit. |
| 7978 | */ |
| 7979 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 7980 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7981 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7982 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 7983 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 7984 | (val & 0x1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7985 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7986 | break; |
| 7987 | } |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7988 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7989 | } |
| 7990 | |
| 7991 | /* |
| 7992 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 7993 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 7994 | * when in is_guest_mode (L2). |
| 7995 | */ |
| 7996 | static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu) |
| 7997 | { |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7998 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 7999 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8000 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Jan Kiszka | 957c897 | 2013-02-24 14:11:34 +0100 | [diff] [blame] | 8001 | u32 exit_reason = vmx->exit_reason; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8002 | |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 8003 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 8004 | vmcs_readl(EXIT_QUALIFICATION), |
| 8005 | vmx->idt_vectoring_info, |
| 8006 | intr_info, |
| 8007 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 8008 | KVM_ISA_VMX); |
| 8009 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8010 | if (vmx->nested.nested_run_pending) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8011 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8012 | |
| 8013 | if (unlikely(vmx->fail)) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 8014 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 8015 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8016 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8017 | } |
| 8018 | |
| 8019 | switch (exit_reason) { |
| 8020 | case EXIT_REASON_EXCEPTION_NMI: |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 8021 | if (is_nmi(intr_info)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8022 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8023 | else if (is_page_fault(intr_info)) |
| 8024 | return enable_ept; |
Anthoine Bourgeois | e504c90 | 2013-11-13 11:45:37 +0100 | [diff] [blame] | 8025 | else if (is_no_device(intr_info) && |
Paolo Bonzini | ccf9844 | 2014-02-27 22:54:11 +0100 | [diff] [blame] | 8026 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8027 | return false; |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 8028 | else if (is_debug(intr_info) && |
| 8029 | vcpu->guest_debug & |
| 8030 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 8031 | return false; |
| 8032 | else if (is_breakpoint(intr_info) && |
| 8033 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 8034 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8035 | return vmcs12->exception_bitmap & |
| 8036 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 8037 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8038 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8039 | case EXIT_REASON_TRIPLE_FAULT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8040 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8041 | case EXIT_REASON_PENDING_INTERRUPT: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 8042 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8043 | case EXIT_REASON_NMI_WINDOW: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 8044 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8045 | case EXIT_REASON_TASK_SWITCH: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8046 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8047 | case EXIT_REASON_CPUID: |
Marcelo Tosatti | bc61349 | 2014-09-18 18:24:57 -0300 | [diff] [blame] | 8048 | if (kvm_register_read(vcpu, VCPU_REGS_RAX) == 0xa) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8049 | return false; |
| 8050 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8051 | case EXIT_REASON_HLT: |
| 8052 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 8053 | case EXIT_REASON_INVD: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8054 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8055 | case EXIT_REASON_INVLPG: |
| 8056 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 8057 | case EXIT_REASON_RDPMC: |
| 8058 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 8059 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8060 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 8061 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 8062 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 8063 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: |
| 8064 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: |
| 8065 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8066 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8067 | /* |
| 8068 | * VMX instructions trap unconditionally. This allows L1 to |
| 8069 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 8070 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8071 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8072 | case EXIT_REASON_CR_ACCESS: |
| 8073 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 8074 | case EXIT_REASON_DR_ACCESS: |
| 8075 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 8076 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8077 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8078 | case EXIT_REASON_MSR_READ: |
| 8079 | case EXIT_REASON_MSR_WRITE: |
| 8080 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 8081 | case EXIT_REASON_INVALID_STATE: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8082 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8083 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 8084 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 8085 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
| 8086 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8087 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 8088 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 8089 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 8090 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 8091 | nested_cpu_has2(vmcs12, |
| 8092 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 8093 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8094 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8095 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8096 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8097 | case EXIT_REASON_APIC_ACCESS: |
| 8098 | return nested_cpu_has2(vmcs12, |
| 8099 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 8100 | case EXIT_REASON_APIC_WRITE: |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 8101 | case EXIT_REASON_EOI_INDUCED: |
| 8102 | /* apic_write and eoi_induced should exit unconditionally. */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8103 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8104 | case EXIT_REASON_EPT_VIOLATION: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 8105 | /* |
| 8106 | * L0 always deals with the EPT violation. If nested EPT is |
| 8107 | * used, and the nested mmu code discovers that the address is |
| 8108 | * missing in the guest EPT table (EPT12), the EPT violation |
| 8109 | * will be injected with nested_ept_inject_page_fault() |
| 8110 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8111 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8112 | case EXIT_REASON_EPT_MISCONFIG: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 8113 | /* |
| 8114 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 8115 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 8116 | * (EPT on EPT). So any problems with the structure of the |
| 8117 | * table is L0's fault. |
| 8118 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8119 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8120 | case EXIT_REASON_WBINVD: |
| 8121 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 8122 | case EXIT_REASON_XSETBV: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8123 | return true; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 8124 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 8125 | /* |
| 8126 | * This should never happen, since it is not possible to |
| 8127 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 8128 | * If if it were, XSS would have to be checked against |
| 8129 | * the XSS exit bitmap in vmcs12. |
| 8130 | */ |
| 8131 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 55123e3 | 2016-07-06 18:29:58 +0800 | [diff] [blame] | 8132 | case EXIT_REASON_PREEMPTION_TIMER: |
| 8133 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8134 | default: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8135 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8136 | } |
| 8137 | } |
| 8138 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 8139 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 8140 | { |
| 8141 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 8142 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8143 | } |
| 8144 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 8145 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8146 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 8147 | if (vmx->pml_pg) { |
| 8148 | __free_page(vmx->pml_pg); |
| 8149 | vmx->pml_pg = NULL; |
| 8150 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8151 | } |
| 8152 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8153 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8154 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8155 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8156 | u64 *pml_buf; |
| 8157 | u16 pml_idx; |
| 8158 | |
| 8159 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 8160 | |
| 8161 | /* Do nothing if PML buffer is empty */ |
| 8162 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 8163 | return; |
| 8164 | |
| 8165 | /* PML index always points to next available PML buffer entity */ |
| 8166 | if (pml_idx >= PML_ENTITY_NUM) |
| 8167 | pml_idx = 0; |
| 8168 | else |
| 8169 | pml_idx++; |
| 8170 | |
| 8171 | pml_buf = page_address(vmx->pml_pg); |
| 8172 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 8173 | u64 gpa; |
| 8174 | |
| 8175 | gpa = pml_buf[pml_idx]; |
| 8176 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8177 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8178 | } |
| 8179 | |
| 8180 | /* reset PML index */ |
| 8181 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 8182 | } |
| 8183 | |
| 8184 | /* |
| 8185 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 8186 | * Called before reporting dirty_bitmap to userspace. |
| 8187 | */ |
| 8188 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 8189 | { |
| 8190 | int i; |
| 8191 | struct kvm_vcpu *vcpu; |
| 8192 | /* |
| 8193 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 8194 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 8195 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 8196 | * buffer. |
| 8197 | */ |
| 8198 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 8199 | kvm_vcpu_kick(vcpu); |
| 8200 | } |
| 8201 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8202 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 8203 | { |
| 8204 | pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n", |
Chao Peng | 7c3bab1 | 2017-02-21 03:50:01 -0500 | [diff] [blame^] | 8205 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8206 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 8207 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 8208 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 8209 | } |
| 8210 | |
| 8211 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 8212 | { |
| 8213 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 8214 | name, vmcs_read32(limit), |
| 8215 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 8216 | } |
| 8217 | |
| 8218 | static void dump_vmcs(void) |
| 8219 | { |
| 8220 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 8221 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 8222 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 8223 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 8224 | u32 secondary_exec_control = 0; |
| 8225 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 8226 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8227 | int i, n; |
| 8228 | |
| 8229 | if (cpu_has_secondary_exec_ctrls()) |
| 8230 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 8231 | |
| 8232 | pr_err("*** Guest State ***\n"); |
| 8233 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 8234 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 8235 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 8236 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 8237 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 8238 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 8239 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 8240 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 8241 | { |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8242 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 8243 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 8244 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 8245 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8246 | } |
| 8247 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 8248 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 8249 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 8250 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 8251 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 8252 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 8253 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 8254 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 8255 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 8256 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 8257 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 8258 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 8259 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 8260 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 8261 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 8262 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 8263 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 8264 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 8265 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8266 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 8267 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 8268 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 8269 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8270 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
| 8271 | if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8272 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 8273 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8274 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8275 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8276 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 8277 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 8278 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 8279 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 8280 | pr_err("InterruptStatus = %04x\n", |
| 8281 | vmcs_read16(GUEST_INTR_STATUS)); |
| 8282 | |
| 8283 | pr_err("*** Host State ***\n"); |
| 8284 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 8285 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 8286 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 8287 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 8288 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 8289 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 8290 | vmcs_read16(HOST_TR_SELECTOR)); |
| 8291 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 8292 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 8293 | vmcs_readl(HOST_TR_BASE)); |
| 8294 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 8295 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 8296 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 8297 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 8298 | vmcs_readl(HOST_CR4)); |
| 8299 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 8300 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 8301 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 8302 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 8303 | if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER)) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8304 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 8305 | vmcs_read64(HOST_IA32_EFER), |
| 8306 | vmcs_read64(HOST_IA32_PAT)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8307 | if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8308 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 8309 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8310 | |
| 8311 | pr_err("*** Control State ***\n"); |
| 8312 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 8313 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 8314 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 8315 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 8316 | vmcs_read32(EXCEPTION_BITMAP), |
| 8317 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 8318 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 8319 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 8320 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 8321 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 8322 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 8323 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 8324 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8325 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 8326 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 8327 | pr_err(" reason=%08x qualification=%016lx\n", |
| 8328 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 8329 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 8330 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 8331 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8332 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 8333 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8334 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 8335 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8336 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 8337 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 8338 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 8339 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 8340 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8341 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8342 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 8343 | for (i = 0; i + 1 < n; i += 4) |
| 8344 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 8345 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 8346 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 8347 | if (i < n) |
| 8348 | pr_err("CR3 target%u=%016lx\n", |
| 8349 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 8350 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 8351 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 8352 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 8353 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 8354 | pr_err("Virtual processor ID = 0x%04x\n", |
| 8355 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 8356 | } |
| 8357 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8358 | /* |
| 8359 | * The guest has exited. See if we can fix it or if we need userspace |
| 8360 | * assistance. |
| 8361 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8362 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8363 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8364 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8365 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 8366 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8367 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 8368 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
| 8369 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8370 | /* |
| 8371 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 8372 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 8373 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 8374 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 8375 | * flushed already. |
| 8376 | */ |
| 8377 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8378 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8379 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 8380 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 8381 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 8382 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 8383 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8384 | if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 8385 | nested_vmx_vmexit(vcpu, exit_reason, |
| 8386 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8387 | vmcs_readl(EXIT_QUALIFICATION)); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8388 | return 1; |
| 8389 | } |
| 8390 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 8391 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8392 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 8393 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 8394 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 8395 | = exit_reason; |
| 8396 | return 0; |
| 8397 | } |
| 8398 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8399 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8400 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 8401 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8402 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 8403 | return 0; |
| 8404 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8405 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 8406 | /* |
| 8407 | * Note: |
| 8408 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 8409 | * delivery event since it indicates guest is accessing MMIO. |
| 8410 | * The vm-exit can be triggered again after return to guest that |
| 8411 | * will cause infinite loop. |
| 8412 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 8413 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 8414 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 8415 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 8416 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 8417 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 8418 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 8419 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
| 8420 | vcpu->run->internal.ndata = 2; |
| 8421 | vcpu->run->internal.data[0] = vectoring_info; |
| 8422 | vcpu->run->internal.data[1] = exit_reason; |
| 8423 | return 0; |
| 8424 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8425 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8426 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked && |
| 8427 | !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis( |
Nadav Har'El | f5c4368 | 2013-08-05 11:07:20 +0300 | [diff] [blame] | 8428 | get_vmcs12(vcpu))))) { |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 8429 | if (vmx_interrupt_allowed(vcpu)) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8430 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8431 | } else if (vmx->vnmi_blocked_time > 1000000000LL && |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 8432 | vcpu->arch.nmi_pending) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8433 | /* |
| 8434 | * This CPU don't support us in finding the end of an |
| 8435 | * NMI-blocked window if the guest runs with IRQs |
| 8436 | * disabled. So we pull the trigger after 1 s of |
| 8437 | * futile waiting, but inform the user about this. |
| 8438 | */ |
| 8439 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 8440 | "state on VCPU %d after 1 s timeout\n", |
| 8441 | __func__, vcpu->vcpu_id); |
| 8442 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8443 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8444 | } |
| 8445 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8446 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 8447 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8448 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8449 | else { |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 8450 | WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason); |
| 8451 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8452 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8453 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8454 | } |
| 8455 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8456 | 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] | 8457 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8458 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 8459 | |
| 8460 | if (is_guest_mode(vcpu) && |
| 8461 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 8462 | return; |
| 8463 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8464 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 8465 | vmcs_write32(TPR_THRESHOLD, 0); |
| 8466 | return; |
| 8467 | } |
| 8468 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8469 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 8470 | } |
| 8471 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8472 | static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
| 8473 | { |
| 8474 | u32 sec_exec_control; |
| 8475 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 8476 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 8477 | if (is_guest_mode(vcpu)) { |
| 8478 | to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true; |
| 8479 | return; |
| 8480 | } |
| 8481 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 8482 | if (!cpu_has_vmx_virtualize_x2apic_mode()) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8483 | return; |
| 8484 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 8485 | if (!cpu_need_tpr_shadow(vcpu)) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8486 | return; |
| 8487 | |
| 8488 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 8489 | |
| 8490 | if (set) { |
| 8491 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 8492 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 8493 | } else { |
| 8494 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 8495 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 8496 | } |
| 8497 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 8498 | |
| 8499 | vmx_set_msr_bitmap(vcpu); |
| 8500 | } |
| 8501 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8502 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 8503 | { |
| 8504 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8505 | |
| 8506 | /* |
| 8507 | * Currently we do not handle the nested case where L2 has an |
| 8508 | * APIC access page of its own; that page is still pinned. |
| 8509 | * Hence, we skip the case where the VCPU is in guest mode _and_ |
| 8510 | * L1 prepared an APIC access page for L2. |
| 8511 | * |
| 8512 | * For the case where L1 and L2 share the same APIC access page |
| 8513 | * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear |
| 8514 | * in the vmcs12), this function will only update either the vmcs01 |
| 8515 | * or the vmcs02. If the former, the vmcs02 will be updated by |
| 8516 | * prepare_vmcs02. If the latter, the vmcs01 will be updated in |
| 8517 | * the next L2->L1 exit. |
| 8518 | */ |
| 8519 | if (!is_guest_mode(vcpu) || |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8520 | !nested_cpu_has2(get_vmcs12(&vmx->vcpu), |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8521 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 8522 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 8523 | } |
| 8524 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8525 | static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8526 | { |
| 8527 | u16 status; |
| 8528 | u8 old; |
| 8529 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8530 | if (max_isr == -1) |
| 8531 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8532 | |
| 8533 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 8534 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8535 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8536 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8537 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8538 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 8539 | } |
| 8540 | } |
| 8541 | |
| 8542 | static void vmx_set_rvi(int vector) |
| 8543 | { |
| 8544 | u16 status; |
| 8545 | u8 old; |
| 8546 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8547 | if (vector == -1) |
| 8548 | vector = 0; |
| 8549 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8550 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 8551 | old = (u8)status & 0xff; |
| 8552 | if ((u8)vector != old) { |
| 8553 | status &= ~0xff; |
| 8554 | status |= (u8)vector; |
| 8555 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 8556 | } |
| 8557 | } |
| 8558 | |
| 8559 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 8560 | { |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8561 | if (!is_guest_mode(vcpu)) { |
| 8562 | vmx_set_rvi(max_irr); |
| 8563 | return; |
| 8564 | } |
| 8565 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8566 | if (max_irr == -1) |
| 8567 | return; |
| 8568 | |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8569 | /* |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8570 | * In guest mode. If a vmexit is needed, vmx_check_nested_events |
| 8571 | * handles it. |
| 8572 | */ |
| 8573 | if (nested_exit_on_intr(vcpu)) |
| 8574 | return; |
| 8575 | |
| 8576 | /* |
| 8577 | * Else, fall back to pre-APICv interrupt injection since L2 |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8578 | * is run without virtual interrupt delivery. |
| 8579 | */ |
| 8580 | if (!kvm_event_needs_reinjection(vcpu) && |
| 8581 | vmx_interrupt_allowed(vcpu)) { |
| 8582 | kvm_queue_interrupt(vcpu, max_irr, false); |
| 8583 | vmx_inject_irq(vcpu); |
| 8584 | } |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8585 | } |
| 8586 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 8587 | static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8588 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 8589 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 8590 | return; |
| 8591 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8592 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 8593 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 8594 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 8595 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 8596 | } |
| 8597 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8598 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8599 | { |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8600 | u32 exit_intr_info; |
| 8601 | |
| 8602 | if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 8603 | || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
| 8604 | return; |
| 8605 | |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 8606 | vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8607 | exit_intr_info = vmx->exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8608 | |
| 8609 | /* Handle machine checks before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8610 | if (is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8611 | kvm_machine_check(); |
| 8612 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8613 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 8614 | if (is_nmi(exit_intr_info)) { |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 8615 | kvm_before_handle_nmi(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8616 | asm("int $2"); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 8617 | kvm_after_handle_nmi(&vmx->vcpu); |
| 8618 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8619 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8620 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8621 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 8622 | { |
| 8623 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8624 | register void *__sp asm(_ASM_SP); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8625 | |
| 8626 | /* |
| 8627 | * If external interrupt exists, IF bit is set in rflags/eflags on the |
| 8628 | * interrupt stack frame, and interrupt will be enabled on a return |
| 8629 | * from interrupt handler. |
| 8630 | */ |
| 8631 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 8632 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 8633 | unsigned int vector; |
| 8634 | unsigned long entry; |
| 8635 | gate_desc *desc; |
| 8636 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8637 | #ifdef CONFIG_X86_64 |
| 8638 | unsigned long tmp; |
| 8639 | #endif |
| 8640 | |
| 8641 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 8642 | desc = (gate_desc *)vmx->host_idt_base + vector; |
| 8643 | entry = gate_offset(*desc); |
| 8644 | asm volatile( |
| 8645 | #ifdef CONFIG_X86_64 |
| 8646 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 8647 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 8648 | "push $%c[ss]\n\t" |
| 8649 | "push %[sp]\n\t" |
| 8650 | #endif |
| 8651 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8652 | __ASM_SIZE(push) " $%c[cs]\n\t" |
| 8653 | "call *%[entry]\n\t" |
| 8654 | : |
| 8655 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8656 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8657 | #endif |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8658 | "+r"(__sp) |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8659 | : |
| 8660 | [entry]"r"(entry), |
| 8661 | [ss]"i"(__KERNEL_DS), |
| 8662 | [cs]"i"(__KERNEL_CS) |
| 8663 | ); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 8664 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8665 | } |
| 8666 | |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 8667 | static bool vmx_has_high_real_mode_segbase(void) |
| 8668 | { |
| 8669 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 8670 | } |
| 8671 | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 8672 | static bool vmx_mpx_supported(void) |
| 8673 | { |
| 8674 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && |
| 8675 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); |
| 8676 | } |
| 8677 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 8678 | static bool vmx_xsaves_supported(void) |
| 8679 | { |
| 8680 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 8681 | SECONDARY_EXEC_XSAVES; |
| 8682 | } |
| 8683 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8684 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 8685 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 8686 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8687 | bool unblock_nmi; |
| 8688 | u8 vector; |
| 8689 | bool idtv_info_valid; |
| 8690 | |
| 8691 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8692 | |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8693 | if (cpu_has_virtual_nmis()) { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 8694 | if (vmx->nmi_known_unmasked) |
| 8695 | return; |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 8696 | /* |
| 8697 | * Can't use vmx->exit_intr_info since we're not sure what |
| 8698 | * the exit reason is. |
| 8699 | */ |
| 8700 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8701 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 8702 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 8703 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8704 | * SDM 3: 27.7.1.2 (September 2008) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8705 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 8706 | * a guest IRET fault. |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8707 | * SDM 3: 23.2.2 (September 2008) |
| 8708 | * Bit 12 is undefined in any of the following cases: |
| 8709 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 8710 | * information field. |
| 8711 | * If the VM exit is due to a double fault. |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8712 | */ |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8713 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 8714 | vector != DF_VECTOR && !idtv_info_valid) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8715 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 8716 | GUEST_INTR_STATE_NMI); |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 8717 | else |
| 8718 | vmx->nmi_known_unmasked = |
| 8719 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 8720 | & GUEST_INTR_STATE_NMI); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8721 | } else if (unlikely(vmx->soft_vnmi_blocked)) |
| 8722 | vmx->vnmi_blocked_time += |
| 8723 | ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8724 | } |
| 8725 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8726 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8727 | u32 idt_vectoring_info, |
| 8728 | int instr_len_field, |
| 8729 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8730 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8731 | u8 vector; |
| 8732 | int type; |
| 8733 | bool idtv_info_valid; |
| 8734 | |
| 8735 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8736 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8737 | vcpu->arch.nmi_injected = false; |
| 8738 | kvm_clear_exception_queue(vcpu); |
| 8739 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8740 | |
| 8741 | if (!idtv_info_valid) |
| 8742 | return; |
| 8743 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8744 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 8745 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8746 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 8747 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8748 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 8749 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8750 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8751 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8752 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8753 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8754 | * Clear bit "block by NMI" before VM entry if a NMI |
| 8755 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8756 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8757 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8758 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8759 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8760 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8761 | /* fall through */ |
| 8762 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 8763 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8764 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 8765 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 8766 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 8767 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8768 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8769 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8770 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8771 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8772 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8773 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8774 | break; |
| 8775 | default: |
| 8776 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 8777 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8778 | } |
| 8779 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8780 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 8781 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8782 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8783 | VM_EXIT_INSTRUCTION_LEN, |
| 8784 | IDT_VECTORING_ERROR_CODE); |
| 8785 | } |
| 8786 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 8787 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 8788 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8789 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 8790 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 8791 | VM_ENTRY_INSTRUCTION_LEN, |
| 8792 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 8793 | |
| 8794 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 8795 | } |
| 8796 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 8797 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 8798 | { |
| 8799 | int i, nr_msrs; |
| 8800 | struct perf_guest_switch_msr *msrs; |
| 8801 | |
| 8802 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 8803 | |
| 8804 | if (!msrs) |
| 8805 | return; |
| 8806 | |
| 8807 | for (i = 0; i < nr_msrs; i++) |
| 8808 | if (msrs[i].host == msrs[i].guest) |
| 8809 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 8810 | else |
| 8811 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
| 8812 | msrs[i].host); |
| 8813 | } |
| 8814 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8815 | void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
| 8816 | { |
| 8817 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8818 | u64 tscl; |
| 8819 | u32 delta_tsc; |
| 8820 | |
| 8821 | if (vmx->hv_deadline_tsc == -1) |
| 8822 | return; |
| 8823 | |
| 8824 | tscl = rdtsc(); |
| 8825 | if (vmx->hv_deadline_tsc > tscl) |
| 8826 | /* sure to be 32 bit only because checked on set_hv_timer */ |
| 8827 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 8828 | cpu_preemption_timer_multi); |
| 8829 | else |
| 8830 | delta_tsc = 0; |
| 8831 | |
| 8832 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); |
| 8833 | } |
| 8834 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 8835 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8836 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 8837 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 8838 | unsigned long debugctlmsr, cr4; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8839 | |
| 8840 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
| 8841 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) |
| 8842 | vmx->entry_time = ktime_get(); |
| 8843 | |
| 8844 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 8845 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 8846 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8847 | return; |
| 8848 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 8849 | if (vmx->ple_window_dirty) { |
| 8850 | vmx->ple_window_dirty = false; |
| 8851 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 8852 | } |
| 8853 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8854 | if (vmx->nested.sync_shadow_vmcs) { |
| 8855 | copy_vmcs12_to_shadow(vmx); |
| 8856 | vmx->nested.sync_shadow_vmcs = false; |
| 8857 | } |
| 8858 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8859 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 8860 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 8861 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 8862 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 8863 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 8864 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 8865 | if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) { |
| 8866 | vmcs_writel(HOST_CR4, cr4); |
| 8867 | vmx->host_state.vmcs_host_cr4 = cr4; |
| 8868 | } |
| 8869 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8870 | /* When single-stepping over STI and MOV SS, we must clear the |
| 8871 | * corresponding interruptibility bits in the guest state. Otherwise |
| 8872 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 8873 | * exceptions being set, but that's not correct for the guest debugging |
| 8874 | * case. */ |
| 8875 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 8876 | vmx_set_interrupt_shadow(vcpu, 0); |
| 8877 | |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 8878 | if (vmx->guest_pkru_valid) |
| 8879 | __write_pkru(vmx->guest_pkru); |
| 8880 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 8881 | atomic_switch_perf_msrs(vmx); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 8882 | debugctlmsr = get_debugctlmsr(); |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 8883 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8884 | vmx_arm_hv_timer(vcpu); |
| 8885 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 8886 | vmx->__launched = vmx->loaded_vmcs->launched; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 8887 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8888 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8889 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 8890 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 8891 | "push %%" _ASM_CX " \n\t" |
| 8892 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 8893 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8894 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 8895 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 8896 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 8897 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8898 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 8899 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 8900 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 8901 | "je 2f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8902 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 8903 | "2: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8904 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8905 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8906 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8907 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 8908 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 8909 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 8910 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 8911 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 8912 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 8913 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8914 | "mov %c[r8](%0), %%r8 \n\t" |
| 8915 | "mov %c[r9](%0), %%r9 \n\t" |
| 8916 | "mov %c[r10](%0), %%r10 \n\t" |
| 8917 | "mov %c[r11](%0), %%r11 \n\t" |
| 8918 | "mov %c[r12](%0), %%r12 \n\t" |
| 8919 | "mov %c[r13](%0), %%r13 \n\t" |
| 8920 | "mov %c[r14](%0), %%r14 \n\t" |
| 8921 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8922 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8923 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 8924 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8925 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 8926 | "jne 1f \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 8927 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 8928 | "jmp 2f \n\t" |
| 8929 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" |
| 8930 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8931 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8932 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 8933 | "pop %0 \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8934 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 8935 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 8936 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 8937 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 8938 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 8939 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 8940 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 8941 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8942 | "mov %%r8, %c[r8](%0) \n\t" |
| 8943 | "mov %%r9, %c[r9](%0) \n\t" |
| 8944 | "mov %%r10, %c[r10](%0) \n\t" |
| 8945 | "mov %%r11, %c[r11](%0) \n\t" |
| 8946 | "mov %%r12, %c[r12](%0) \n\t" |
| 8947 | "mov %%r13, %c[r13](%0) \n\t" |
| 8948 | "mov %%r14, %c[r14](%0) \n\t" |
| 8949 | "mov %%r15, %c[r15](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8950 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8951 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 8952 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 8953 | |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8954 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8955 | "setbe %c[fail](%0) \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 8956 | ".pushsection .rodata \n\t" |
| 8957 | ".global vmx_return \n\t" |
| 8958 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 8959 | ".popsection" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8960 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 8961 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 8962 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 8963 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 8964 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 8965 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 8966 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 8967 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 8968 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 8969 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 8970 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 8971 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 8972 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 8973 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 8974 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 8975 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 8976 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 8977 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 8978 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 8979 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8980 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 8981 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 8982 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 8983 | : "cc", "memory" |
| 8984 | #ifdef CONFIG_X86_64 |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8985 | , "rax", "rbx", "rdi", "rsi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 8986 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 8987 | #else |
| 8988 | , "eax", "ebx", "edi", "esi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 8989 | #endif |
| 8990 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8991 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 8992 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
| 8993 | if (debugctlmsr) |
| 8994 | update_debugctlmsr(debugctlmsr); |
| 8995 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 8996 | #ifndef CONFIG_X86_64 |
| 8997 | /* |
| 8998 | * The sysexit path does not restore ds/es, so we must set them to |
| 8999 | * a reasonable value ourselves. |
| 9000 | * |
| 9001 | * We can't defer this to vmx_load_host_state() since that function |
| 9002 | * may be executed in interrupt context, which saves and restore segments |
| 9003 | * around it, nullifying its effect. |
| 9004 | */ |
| 9005 | loadsegment(ds, __USER_DS); |
| 9006 | loadsegment(es, __USER_DS); |
| 9007 | #endif |
| 9008 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 9009 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 9010 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 9011 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 9012 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 9013 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 9014 | vcpu->arch.regs_dirty = 0; |
| 9015 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 9016 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 9017 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9018 | vmx->loaded_vmcs->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 9019 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9020 | vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9021 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 9022 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 9023 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 9024 | * back on host, so it is safe to read guest PKRU from current |
| 9025 | * XSAVE. |
| 9026 | */ |
| 9027 | if (boot_cpu_has(X86_FEATURE_OSPKE)) { |
| 9028 | vmx->guest_pkru = __read_pkru(); |
| 9029 | if (vmx->guest_pkru != vmx->host_pkru) { |
| 9030 | vmx->guest_pkru_valid = true; |
| 9031 | __write_pkru(vmx->host_pkru); |
| 9032 | } else |
| 9033 | vmx->guest_pkru_valid = false; |
| 9034 | } |
| 9035 | |
| 9036 | /* |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 9037 | * the KVM_REQ_EVENT optimization bit is only on for one entry, and if |
| 9038 | * we did not inject a still-pending event to L1 now because of |
| 9039 | * nested_run_pending, we need to re-enable this bit. |
| 9040 | */ |
| 9041 | if (vmx->nested.nested_run_pending) |
| 9042 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9043 | |
| 9044 | vmx->nested.nested_run_pending = 0; |
| 9045 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9046 | vmx_complete_atomic_exit(vmx); |
| 9047 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 9048 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9049 | } |
| 9050 | |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9051 | static void vmx_load_vmcs01(struct kvm_vcpu *vcpu) |
| 9052 | { |
| 9053 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9054 | int cpu; |
| 9055 | |
| 9056 | if (vmx->loaded_vmcs == &vmx->vmcs01) |
| 9057 | return; |
| 9058 | |
| 9059 | cpu = get_cpu(); |
| 9060 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 9061 | vmx_vcpu_put(vcpu); |
| 9062 | vmx_vcpu_load(vcpu, cpu); |
| 9063 | vcpu->cpu = cpu; |
| 9064 | put_cpu(); |
| 9065 | } |
| 9066 | |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9067 | /* |
| 9068 | * Ensure that the current vmcs of the logical processor is the |
| 9069 | * vmcs01 of the vcpu before calling free_nested(). |
| 9070 | */ |
| 9071 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) |
| 9072 | { |
| 9073 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9074 | int r; |
| 9075 | |
| 9076 | r = vcpu_load(vcpu); |
| 9077 | BUG_ON(r); |
| 9078 | vmx_load_vmcs01(vcpu); |
| 9079 | free_nested(vmx); |
| 9080 | vcpu_put(vcpu); |
| 9081 | } |
| 9082 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9083 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 9084 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9085 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9086 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9087 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9088 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9089 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9090 | leave_guest_mode(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9091 | vmx_free_vcpu_nested(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9092 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9093 | kfree(vmx->guest_msrs); |
| 9094 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 9095 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9096 | } |
| 9097 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9098 | 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] | 9099 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9100 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 9101 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9102 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9103 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9104 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9105 | return ERR_PTR(-ENOMEM); |
| 9106 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9107 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 9108 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9109 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 9110 | if (err) |
| 9111 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9112 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9113 | err = -ENOMEM; |
| 9114 | |
| 9115 | /* |
| 9116 | * If PML is turned on, failure on enabling PML just results in failure |
| 9117 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 9118 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 9119 | * for the guest, etc. |
| 9120 | */ |
| 9121 | if (enable_pml) { |
| 9122 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 9123 | if (!vmx->pml_pg) |
| 9124 | goto uninit_vcpu; |
| 9125 | } |
| 9126 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9127 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 9128 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 9129 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 9130 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9131 | if (!vmx->guest_msrs) |
| 9132 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9133 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9134 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 9135 | vmx->loaded_vmcs->vmcs = alloc_vmcs(); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 9136 | vmx->loaded_vmcs->shadow_vmcs = NULL; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9137 | if (!vmx->loaded_vmcs->vmcs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9138 | goto free_msrs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9139 | if (!vmm_exclusive) |
| 9140 | kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id()))); |
| 9141 | loaded_vmcs_init(vmx->loaded_vmcs); |
| 9142 | if (!vmm_exclusive) |
| 9143 | kvm_cpu_vmxoff(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9144 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9145 | cpu = get_cpu(); |
| 9146 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 9147 | vmx->vcpu.cpu = cpu; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 9148 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9149 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9150 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9151 | if (err) |
| 9152 | goto free_vmcs; |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 9153 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 9154 | err = alloc_apic_access_page(kvm); |
| 9155 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 9156 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 9157 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9158 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 9159 | if (enable_ept) { |
| 9160 | if (!kvm->arch.ept_identity_map_addr) |
| 9161 | kvm->arch.ept_identity_map_addr = |
| 9162 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 9163 | err = init_rmode_identity_map(kvm); |
| 9164 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 9165 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 9166 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 9167 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 9168 | if (nested) { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9169 | nested_vmx_setup_ctls_msrs(vmx); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 9170 | vmx->nested.vpid02 = allocate_vpid(); |
| 9171 | } |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9172 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9173 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 9174 | vmx->nested.current_vmptr = -1ull; |
| 9175 | vmx->nested.current_vmcs12 = NULL; |
| 9176 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 9177 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 9178 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9179 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9180 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9181 | free_vmcs: |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 9182 | free_vpid(vmx->nested.vpid02); |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 9183 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9184 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9185 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9186 | free_pml: |
| 9187 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9188 | uninit_vcpu: |
| 9189 | kvm_vcpu_uninit(&vmx->vcpu); |
| 9190 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9191 | free_vpid(vmx->vpid); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 9192 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9193 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9194 | } |
| 9195 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 9196 | static void __init vmx_check_processor_compat(void *rtn) |
| 9197 | { |
| 9198 | struct vmcs_config vmcs_conf; |
| 9199 | |
| 9200 | *(int *)rtn = 0; |
| 9201 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 9202 | *(int *)rtn = -EIO; |
| 9203 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 9204 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 9205 | smp_processor_id()); |
| 9206 | *(int *)rtn = -EIO; |
| 9207 | } |
| 9208 | } |
| 9209 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 9210 | static int get_ept_level(void) |
| 9211 | { |
| 9212 | return VMX_EPT_DEFAULT_GAW + 1; |
| 9213 | } |
| 9214 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 9215 | 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] | 9216 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9217 | u8 cache; |
| 9218 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 9219 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9220 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9221 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9222 | * 2. EPT with VT-d: |
| 9223 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9224 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9225 | * b. VT-d with snooping control feature: snooping control feature of |
| 9226 | * VT-d engine can guarantee the cache correctness. Just set it |
| 9227 | * 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] | 9228 | * 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] | 9229 | * consistent with host MTRR |
| 9230 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9231 | if (is_mmio) { |
| 9232 | cache = MTRR_TYPE_UNCACHABLE; |
| 9233 | goto exit; |
| 9234 | } |
| 9235 | |
| 9236 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9237 | ipat = VMX_EPT_IPAT_BIT; |
| 9238 | cache = MTRR_TYPE_WRBACK; |
| 9239 | goto exit; |
| 9240 | } |
| 9241 | |
| 9242 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 9243 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 9244 | if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) |
Xiao Guangrong | fb279950 | 2015-07-16 03:25:56 +0800 | [diff] [blame] | 9245 | cache = MTRR_TYPE_WRBACK; |
| 9246 | else |
| 9247 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9248 | goto exit; |
| 9249 | } |
| 9250 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 9251 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9252 | |
| 9253 | exit: |
| 9254 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 9255 | } |
| 9256 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 9257 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 9258 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 9259 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 9260 | return PT_DIRECTORY_LEVEL; |
| 9261 | else |
| 9262 | /* For shadow and EPT supported 1GB page */ |
| 9263 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 9264 | } |
| 9265 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9266 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 9267 | { |
| 9268 | /* |
| 9269 | * These bits in the secondary execution controls field |
| 9270 | * are dynamic, the others are mostly based on the hypervisor |
| 9271 | * architecture and the guest's CPUID. Do not touch the |
| 9272 | * dynamic bits. |
| 9273 | */ |
| 9274 | u32 mask = |
| 9275 | SECONDARY_EXEC_SHADOW_VMCS | |
| 9276 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 9277 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 9278 | |
| 9279 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 9280 | |
| 9281 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 9282 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 9283 | } |
| 9284 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 9285 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 9286 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9287 | struct kvm_cpuid_entry2 *best; |
| 9288 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9289 | u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9290 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9291 | if (vmx_rdtscp_supported()) { |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 9292 | bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu); |
| 9293 | if (!rdtscp_enabled) |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9294 | secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP; |
Xiao Guangrong | f36201e | 2015-09-09 14:05:53 +0800 | [diff] [blame] | 9295 | |
Paolo Bonzini | 8b97265 | 2015-09-15 17:34:42 +0200 | [diff] [blame] | 9296 | if (nested) { |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 9297 | if (rdtscp_enabled) |
Paolo Bonzini | 8b97265 | 2015-09-15 17:34:42 +0200 | [diff] [blame] | 9298 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 9299 | SECONDARY_EXEC_RDTSCP; |
| 9300 | else |
| 9301 | vmx->nested.nested_vmx_secondary_ctls_high &= |
| 9302 | ~SECONDARY_EXEC_RDTSCP; |
| 9303 | } |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9304 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9305 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9306 | /* Exposing INVPCID only when PCID is exposed */ |
| 9307 | best = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 9308 | if (vmx_invpcid_supported() && |
Xiao Guangrong | 29541bb | 2015-09-09 14:05:54 +0800 | [diff] [blame] | 9309 | (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) || |
| 9310 | !guest_cpuid_has_pcid(vcpu))) { |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9311 | secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Xiao Guangrong | 29541bb | 2015-09-09 14:05:54 +0800 | [diff] [blame] | 9312 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9313 | if (best) |
Ren, Yongjie | 4f97704 | 2012-09-07 07:36:59 +0000 | [diff] [blame] | 9314 | best->ebx &= ~bit(X86_FEATURE_INVPCID); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9315 | } |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 9316 | |
Huaitong Han | 45bdbcf | 2016-01-12 16:04:20 +0800 | [diff] [blame] | 9317 | if (cpu_has_secondary_exec_ctrls()) |
| 9318 | vmcs_set_secondary_exec_control(secondary_exec_ctl); |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9319 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 9320 | if (nested_vmx_allowed(vcpu)) |
| 9321 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 9322 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 9323 | else |
| 9324 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 9325 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 9326 | } |
| 9327 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 9328 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 9329 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 9330 | if (func == 1 && nested) |
| 9331 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 9332 | } |
| 9333 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9334 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 9335 | struct x86_exception *fault) |
| 9336 | { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9337 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9338 | u32 exit_reason; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9339 | |
| 9340 | if (fault->error_code & PFERR_RSVD_MASK) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9341 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9342 | else |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9343 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
| 9344 | nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9345 | vmcs12->guest_physical_address = fault->address; |
| 9346 | } |
| 9347 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9348 | /* Callbacks for nested_ept_init_mmu_context: */ |
| 9349 | |
| 9350 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) |
| 9351 | { |
| 9352 | /* return the page table to be shadowed - in our case, EPT12 */ |
| 9353 | return get_vmcs12(vcpu)->ept_pointer; |
| 9354 | } |
| 9355 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 9356 | static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9357 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 9358 | WARN_ON(mmu_is_nested(vcpu)); |
| 9359 | kvm_init_shadow_ept_mmu(vcpu, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9360 | to_vmx(vcpu)->nested.nested_vmx_ept_caps & |
| 9361 | VMX_EPT_EXECUTE_ONLY_BIT); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9362 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
| 9363 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; |
| 9364 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; |
| 9365 | |
| 9366 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9367 | } |
| 9368 | |
| 9369 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 9370 | { |
| 9371 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 9372 | } |
| 9373 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 9374 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 9375 | u16 error_code) |
| 9376 | { |
| 9377 | bool inequality, bit; |
| 9378 | |
| 9379 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 9380 | inequality = |
| 9381 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 9382 | vmcs12->page_fault_error_code_match; |
| 9383 | return inequality ^ bit; |
| 9384 | } |
| 9385 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 9386 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 9387 | struct x86_exception *fault) |
| 9388 | { |
| 9389 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9390 | |
| 9391 | WARN_ON(!is_guest_mode(vcpu)); |
| 9392 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 9393 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code)) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9394 | nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason, |
| 9395 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 9396 | vmcs_readl(EXIT_QUALIFICATION)); |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 9397 | else |
| 9398 | kvm_inject_page_fault(vcpu, fault); |
| 9399 | } |
| 9400 | |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9401 | static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, |
| 9402 | struct vmcs12 *vmcs12) |
| 9403 | { |
| 9404 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9405 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9406 | |
| 9407 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9408 | if (!PAGE_ALIGNED(vmcs12->apic_access_addr) || |
| 9409 | vmcs12->apic_access_addr >> maxphyaddr) |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9410 | return false; |
| 9411 | |
| 9412 | /* |
| 9413 | * Translate L1 physical address to host physical |
| 9414 | * address for vmcs02. Keep the page pinned, so this |
| 9415 | * physical address remains valid. We keep a reference |
| 9416 | * to it so we can release it later. |
| 9417 | */ |
| 9418 | if (vmx->nested.apic_access_page) /* shouldn't happen */ |
| 9419 | nested_release_page(vmx->nested.apic_access_page); |
| 9420 | vmx->nested.apic_access_page = |
| 9421 | nested_get_page(vcpu, vmcs12->apic_access_addr); |
| 9422 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9423 | |
| 9424 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9425 | if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) || |
| 9426 | vmcs12->virtual_apic_page_addr >> maxphyaddr) |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9427 | return false; |
| 9428 | |
| 9429 | if (vmx->nested.virtual_apic_page) /* shouldn't happen */ |
| 9430 | nested_release_page(vmx->nested.virtual_apic_page); |
| 9431 | vmx->nested.virtual_apic_page = |
| 9432 | nested_get_page(vcpu, vmcs12->virtual_apic_page_addr); |
| 9433 | |
| 9434 | /* |
| 9435 | * Failing the vm entry is _not_ what the processor does |
| 9436 | * but it's basically the only possibility we have. |
| 9437 | * We could still enter the guest if CR8 load exits are |
| 9438 | * enabled, CR8 store exits are enabled, and virtualize APIC |
| 9439 | * access is disabled; in this case the processor would never |
| 9440 | * use the TPR shadow and we could simply clear the bit from |
| 9441 | * the execution control. But such a configuration is useless, |
| 9442 | * so let's keep the code simple. |
| 9443 | */ |
| 9444 | if (!vmx->nested.virtual_apic_page) |
| 9445 | return false; |
| 9446 | } |
| 9447 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9448 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9449 | if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) || |
| 9450 | vmcs12->posted_intr_desc_addr >> maxphyaddr) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9451 | return false; |
| 9452 | |
| 9453 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
| 9454 | kunmap(vmx->nested.pi_desc_page); |
| 9455 | nested_release_page(vmx->nested.pi_desc_page); |
| 9456 | } |
| 9457 | vmx->nested.pi_desc_page = |
| 9458 | nested_get_page(vcpu, vmcs12->posted_intr_desc_addr); |
| 9459 | if (!vmx->nested.pi_desc_page) |
| 9460 | return false; |
| 9461 | |
| 9462 | vmx->nested.pi_desc = |
| 9463 | (struct pi_desc *)kmap(vmx->nested.pi_desc_page); |
| 9464 | if (!vmx->nested.pi_desc) { |
| 9465 | nested_release_page_clean(vmx->nested.pi_desc_page); |
| 9466 | return false; |
| 9467 | } |
| 9468 | vmx->nested.pi_desc = |
| 9469 | (struct pi_desc *)((void *)vmx->nested.pi_desc + |
| 9470 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 9471 | (PAGE_SIZE - 1))); |
| 9472 | } |
| 9473 | |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9474 | return true; |
| 9475 | } |
| 9476 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9477 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 9478 | { |
| 9479 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 9480 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9481 | |
| 9482 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 9483 | return; |
| 9484 | |
| 9485 | /* Make sure short timeouts reliably trigger an immediate vmexit. |
| 9486 | * hrtimer_start does not guarantee this. */ |
| 9487 | if (preemption_timeout <= 1) { |
| 9488 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 9489 | return; |
| 9490 | } |
| 9491 | |
| 9492 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 9493 | preemption_timeout *= 1000000; |
| 9494 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 9495 | hrtimer_start(&vmx->nested.preemption_timer, |
| 9496 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 9497 | } |
| 9498 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9499 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 9500 | struct vmcs12 *vmcs12) |
| 9501 | { |
| 9502 | int maxphyaddr; |
| 9503 | u64 addr; |
| 9504 | |
| 9505 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 9506 | return 0; |
| 9507 | |
| 9508 | if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) { |
| 9509 | WARN_ON(1); |
| 9510 | return -EINVAL; |
| 9511 | } |
| 9512 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 9513 | |
| 9514 | if (!PAGE_ALIGNED(vmcs12->msr_bitmap) || |
| 9515 | ((addr + PAGE_SIZE) >> maxphyaddr)) |
| 9516 | return -EINVAL; |
| 9517 | |
| 9518 | return 0; |
| 9519 | } |
| 9520 | |
| 9521 | /* |
| 9522 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 9523 | * we do not use the hardware. |
| 9524 | */ |
| 9525 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, |
| 9526 | struct vmcs12 *vmcs12) |
| 9527 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9528 | int msr; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9529 | struct page *page; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9530 | unsigned long *msr_bitmap_l1; |
| 9531 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9532 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9533 | /* This shortcut is ok because we support only x2APIC MSRs so far. */ |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9534 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12)) |
| 9535 | return false; |
| 9536 | |
| 9537 | page = nested_get_page(vcpu, vmcs12->msr_bitmap); |
| 9538 | if (!page) { |
| 9539 | WARN_ON(1); |
| 9540 | return false; |
| 9541 | } |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9542 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
| 9543 | if (!msr_bitmap_l1) { |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9544 | nested_release_page_clean(page); |
| 9545 | WARN_ON(1); |
| 9546 | return false; |
| 9547 | } |
| 9548 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9549 | memset(msr_bitmap_l0, 0xff, PAGE_SIZE); |
| 9550 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9551 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9552 | if (nested_cpu_has_apic_reg_virt(vmcs12)) |
| 9553 | for (msr = 0x800; msr <= 0x8ff; msr++) |
| 9554 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9555 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9556 | msr, MSR_TYPE_R); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9557 | |
| 9558 | nested_vmx_disable_intercept_for_msr( |
| 9559 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9560 | APIC_BASE_MSR + (APIC_TASKPRI >> 4), |
| 9561 | MSR_TYPE_R | MSR_TYPE_W); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9562 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9563 | if (nested_cpu_has_vid(vmcs12)) { |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9564 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9565 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9566 | APIC_BASE_MSR + (APIC_EOI >> 4), |
| 9567 | MSR_TYPE_W); |
| 9568 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9569 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9570 | APIC_BASE_MSR + (APIC_SELF_IPI >> 4), |
| 9571 | MSR_TYPE_W); |
| 9572 | } |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9573 | } |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9574 | kunmap(page); |
| 9575 | nested_release_page_clean(page); |
| 9576 | |
| 9577 | return true; |
| 9578 | } |
| 9579 | |
| 9580 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 9581 | struct vmcs12 *vmcs12) |
| 9582 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9583 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9584 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9585 | !nested_cpu_has_vid(vmcs12) && |
| 9586 | !nested_cpu_has_posted_intr(vmcs12)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9587 | return 0; |
| 9588 | |
| 9589 | /* |
| 9590 | * If virtualize x2apic mode is enabled, |
| 9591 | * virtualize apic access must be disabled. |
| 9592 | */ |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9593 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 9594 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9595 | return -EINVAL; |
| 9596 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9597 | /* |
| 9598 | * If virtual interrupt delivery is enabled, |
| 9599 | * we must exit on external interrupts. |
| 9600 | */ |
| 9601 | if (nested_cpu_has_vid(vmcs12) && |
| 9602 | !nested_exit_on_intr(vcpu)) |
| 9603 | return -EINVAL; |
| 9604 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9605 | /* |
| 9606 | * bits 15:8 should be zero in posted_intr_nv, |
| 9607 | * the descriptor address has been already checked |
| 9608 | * in nested_get_vmcs12_pages. |
| 9609 | */ |
| 9610 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 9611 | (!nested_cpu_has_vid(vmcs12) || |
| 9612 | !nested_exit_intr_ack_set(vcpu) || |
| 9613 | vmcs12->posted_intr_nv & 0xff00)) |
| 9614 | return -EINVAL; |
| 9615 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9616 | /* tpr shadow is needed by all apicv features. */ |
| 9617 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 9618 | return -EINVAL; |
| 9619 | |
| 9620 | return 0; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9621 | } |
| 9622 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9623 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
| 9624 | unsigned long count_field, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9625 | unsigned long addr_field) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9626 | { |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9627 | int maxphyaddr; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9628 | u64 count, addr; |
| 9629 | |
| 9630 | if (vmcs12_read_any(vcpu, count_field, &count) || |
| 9631 | vmcs12_read_any(vcpu, addr_field, &addr)) { |
| 9632 | WARN_ON(1); |
| 9633 | return -EINVAL; |
| 9634 | } |
| 9635 | if (count == 0) |
| 9636 | return 0; |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9637 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9638 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
| 9639 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9640 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9641 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
| 9642 | addr_field, maxphyaddr, count, addr); |
| 9643 | return -EINVAL; |
| 9644 | } |
| 9645 | return 0; |
| 9646 | } |
| 9647 | |
| 9648 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 9649 | struct vmcs12 *vmcs12) |
| 9650 | { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9651 | if (vmcs12->vm_exit_msr_load_count == 0 && |
| 9652 | vmcs12->vm_exit_msr_store_count == 0 && |
| 9653 | vmcs12->vm_entry_msr_load_count == 0) |
| 9654 | return 0; /* Fast path */ |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9655 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9656 | VM_EXIT_MSR_LOAD_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9657 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9658 | VM_EXIT_MSR_STORE_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9659 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9660 | VM_ENTRY_MSR_LOAD_ADDR)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9661 | return -EINVAL; |
| 9662 | return 0; |
| 9663 | } |
| 9664 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9665 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 9666 | struct vmx_msr_entry *e) |
| 9667 | { |
| 9668 | /* x2APIC MSR accesses are not allowed */ |
Jan Kiszka | 8a9781f | 2015-05-04 08:32:32 +0200 | [diff] [blame] | 9669 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9670 | return -EINVAL; |
| 9671 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 9672 | e->index == MSR_IA32_UCODE_REV) |
| 9673 | return -EINVAL; |
| 9674 | if (e->reserved != 0) |
| 9675 | return -EINVAL; |
| 9676 | return 0; |
| 9677 | } |
| 9678 | |
| 9679 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 9680 | struct vmx_msr_entry *e) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9681 | { |
| 9682 | if (e->index == MSR_FS_BASE || |
| 9683 | e->index == MSR_GS_BASE || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9684 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 9685 | nested_vmx_msr_check_common(vcpu, e)) |
| 9686 | return -EINVAL; |
| 9687 | return 0; |
| 9688 | } |
| 9689 | |
| 9690 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 9691 | struct vmx_msr_entry *e) |
| 9692 | { |
| 9693 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 9694 | nested_vmx_msr_check_common(vcpu, e)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9695 | return -EINVAL; |
| 9696 | return 0; |
| 9697 | } |
| 9698 | |
| 9699 | /* |
| 9700 | * Load guest's/host's msr at nested entry/exit. |
| 9701 | * return 0 for success, entry index for failure. |
| 9702 | */ |
| 9703 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 9704 | { |
| 9705 | u32 i; |
| 9706 | struct vmx_msr_entry e; |
| 9707 | struct msr_data msr; |
| 9708 | |
| 9709 | msr.host_initiated = false; |
| 9710 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9711 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 9712 | &e, sizeof(e))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9713 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9714 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 9715 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9716 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9717 | } |
| 9718 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9719 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9720 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 9721 | __func__, i, e.index, e.reserved); |
| 9722 | goto fail; |
| 9723 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9724 | msr.index = e.index; |
| 9725 | msr.data = e.value; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9726 | if (kvm_set_msr(vcpu, &msr)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9727 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9728 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 9729 | __func__, i, e.index, e.value); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9730 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9731 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9732 | } |
| 9733 | return 0; |
| 9734 | fail: |
| 9735 | return i + 1; |
| 9736 | } |
| 9737 | |
| 9738 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 9739 | { |
| 9740 | u32 i; |
| 9741 | struct vmx_msr_entry e; |
| 9742 | |
| 9743 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 9744 | struct msr_data msr_info; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9745 | if (kvm_vcpu_read_guest(vcpu, |
| 9746 | gpa + i * sizeof(e), |
| 9747 | &e, 2 * sizeof(u32))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9748 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9749 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 9750 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9751 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9752 | } |
| 9753 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9754 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9755 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 9756 | __func__, i, e.index, e.reserved); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9757 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9758 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 9759 | msr_info.host_initiated = false; |
| 9760 | msr_info.index = e.index; |
| 9761 | if (kvm_get_msr(vcpu, &msr_info)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9762 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9763 | "%s cannot read MSR (%u, 0x%x)\n", |
| 9764 | __func__, i, e.index); |
| 9765 | return -EINVAL; |
| 9766 | } |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9767 | if (kvm_vcpu_write_guest(vcpu, |
| 9768 | gpa + i * sizeof(e) + |
| 9769 | offsetof(struct vmx_msr_entry, value), |
| 9770 | &msr_info.data, sizeof(msr_info.data))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9771 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9772 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 9773 | __func__, i, e.index, msr_info.data); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9774 | return -EINVAL; |
| 9775 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9776 | } |
| 9777 | return 0; |
| 9778 | } |
| 9779 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9780 | /* |
| 9781 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 9782 | * 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] | 9783 | * 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] | 9784 | * guest in a way that will both be appropriate to L1's requests, and our |
| 9785 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 9786 | * function also has additional necessary side-effects, like setting various |
| 9787 | * vcpu->arch fields. |
| 9788 | */ |
| 9789 | static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 9790 | { |
| 9791 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9792 | u32 exec_control; |
| 9793 | |
| 9794 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 9795 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 9796 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 9797 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 9798 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 9799 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 9800 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 9801 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 9802 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 9803 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 9804 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 9805 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 9806 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 9807 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 9808 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 9809 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 9810 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 9811 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 9812 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 9813 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 9814 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 9815 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 9816 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 9817 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 9818 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 9819 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 9820 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 9821 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 9822 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 9823 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 9824 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 9825 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 9826 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 9827 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 9828 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 9829 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
| 9830 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 9831 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 9832 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 9833 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 9834 | } else { |
| 9835 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 9836 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 9837 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9838 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 9839 | vmcs12->vm_entry_intr_info_field); |
| 9840 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 9841 | vmcs12->vm_entry_exception_error_code); |
| 9842 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 9843 | vmcs12->vm_entry_instruction_len); |
| 9844 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 9845 | vmcs12->guest_interruptibility_info); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9846 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
Gleb Natapov | 63fbf59 | 2013-07-28 18:31:06 +0300 | [diff] [blame] | 9847 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9848 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 9849 | vmcs12->guest_pending_dbg_exceptions); |
| 9850 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 9851 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 9852 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 9853 | if (nested_cpu_has_xsaves(vmcs12)) |
| 9854 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9855 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 9856 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9857 | exec_control = vmcs12->pin_based_vm_exec_control; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9858 | |
Paolo Bonzini | 9314006 | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 9859 | /* Preemption timer setting is only taken from vmcs01. */ |
| 9860 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 9861 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
| 9862 | if (vmx->hv_deadline_tsc == -1) |
| 9863 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 9864 | |
| 9865 | /* Posted interrupts setting is only taken from vmcs12. */ |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9866 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 9867 | /* |
| 9868 | * Note that we use L0's vector here and in |
| 9869 | * vmx_deliver_nested_posted_interrupt. |
| 9870 | */ |
| 9871 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 9872 | vmx->nested.pi_pending = false; |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 9873 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9874 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 9875 | page_to_phys(vmx->nested.pi_desc_page) + |
| 9876 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 9877 | (PAGE_SIZE - 1))); |
| 9878 | } else |
| 9879 | exec_control &= ~PIN_BASED_POSTED_INTR; |
| 9880 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9881 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9882 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9883 | vmx->nested.preemption_timer_expired = false; |
| 9884 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 9885 | vmx_start_preemption_timer(vcpu); |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 9886 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9887 | /* |
| 9888 | * Whether page-faults are trapped is determined by a combination of |
| 9889 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 9890 | * If enable_ept, L0 doesn't care about page faults and we should |
| 9891 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 9892 | * care about (at least some) page faults, and because it is not easy |
| 9893 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 9894 | * to exit on each and every L2 page fault. This is done by setting |
| 9895 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 9896 | * Note that below we don't need special code to set EB.PF beyond the |
| 9897 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 9898 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 9899 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 9900 | * |
| 9901 | * A problem with this approach (when !enable_ept) is that L1 may be |
| 9902 | * injected with more page faults than it asked for. This could have |
| 9903 | * caused problems, but in practice existing hypervisors don't care. |
| 9904 | * To fix this, we will need to emulate the PFEC checking (on the L1 |
| 9905 | * page tables), using walk_addr(), when injecting PFs to L1. |
| 9906 | */ |
| 9907 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 9908 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 9909 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 9910 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 9911 | |
| 9912 | if (cpu_has_secondary_exec_ctrls()) { |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9913 | exec_control = vmx_secondary_exec_control(vmx); |
Xiao Guangrong | e282162 | 2015-09-09 14:05:52 +0800 | [diff] [blame] | 9914 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9915 | /* Take the following fields only from vmcs12 */ |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 9916 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 9917 | SECONDARY_EXEC_RDTSCP | |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 9918 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 9919 | SECONDARY_EXEC_APIC_REGISTER_VIRT); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9920 | if (nested_cpu_has(vmcs12, |
| 9921 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 9922 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 9923 | |
| 9924 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) { |
| 9925 | /* |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9926 | * If translation failed, no matter: This feature asks |
| 9927 | * to exit when accessing the given address, and if it |
| 9928 | * can never be accessed, this feature won't do |
| 9929 | * anything anyway. |
| 9930 | */ |
| 9931 | if (!vmx->nested.apic_access_page) |
| 9932 | exec_control &= |
| 9933 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 9934 | else |
| 9935 | vmcs_write64(APIC_ACCESS_ADDR, |
| 9936 | page_to_phys(vmx->nested.apic_access_page)); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9937 | } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) && |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 9938 | cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | ca3f257 | 2013-12-16 12:55:46 +0100 | [diff] [blame] | 9939 | exec_control |= |
| 9940 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9941 | kvm_vcpu_reload_apic_access_page(vcpu); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9942 | } |
| 9943 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9944 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) { |
| 9945 | vmcs_write64(EOI_EXIT_BITMAP0, |
| 9946 | vmcs12->eoi_exit_bitmap0); |
| 9947 | vmcs_write64(EOI_EXIT_BITMAP1, |
| 9948 | vmcs12->eoi_exit_bitmap1); |
| 9949 | vmcs_write64(EOI_EXIT_BITMAP2, |
| 9950 | vmcs12->eoi_exit_bitmap2); |
| 9951 | vmcs_write64(EOI_EXIT_BITMAP3, |
| 9952 | vmcs12->eoi_exit_bitmap3); |
| 9953 | vmcs_write16(GUEST_INTR_STATUS, |
| 9954 | vmcs12->guest_intr_status); |
| 9955 | } |
| 9956 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9957 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 9958 | } |
| 9959 | |
| 9960 | |
| 9961 | /* |
| 9962 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 9963 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 9964 | * Other fields are different per CPU, and will be set later when |
| 9965 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 9966 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9967 | vmx_set_constant_host_state(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9968 | |
| 9969 | /* |
| 9970 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 9971 | * entry, but only if the current (host) sp changed from the value |
| 9972 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 9973 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 9974 | * here we just force the write to happen on entry. |
| 9975 | */ |
| 9976 | vmx->host_rsp = 0; |
| 9977 | |
| 9978 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 9979 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 9980 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 9981 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 9982 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9983 | |
| 9984 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
| 9985 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
| 9986 | page_to_phys(vmx->nested.virtual_apic_page)); |
| 9987 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
| 9988 | } |
| 9989 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9990 | if (cpu_has_vmx_msr_bitmap() && |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9991 | exec_control & CPU_BASED_USE_MSR_BITMAPS && |
| 9992 | nested_vmx_merge_msr_bitmap(vcpu, vmcs12)) |
| 9993 | ; /* MSR_BITMAP will be set by following vmx_set_efer. */ |
| 9994 | else |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9995 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 9996 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9997 | /* |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9998 | * Merging of IO bitmap not currently supported. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 9999 | * Rather, exit every time. |
| 10000 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10001 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 10002 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 10003 | |
| 10004 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 10005 | |
| 10006 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 10007 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 10008 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 10009 | */ |
| 10010 | update_exception_bitmap(vcpu); |
| 10011 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 10012 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 10013 | |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10014 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 10015 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 10016 | * bits are further modified by vmx_set_efer() below. |
| 10017 | */ |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10018 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10019 | |
| 10020 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are |
| 10021 | * emulated by vmx_set_efer(), below. |
| 10022 | */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 10023 | vm_entry_controls_init(vmx, |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10024 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
| 10025 | ~VM_ENTRY_IA32E_MODE) | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10026 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 10027 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10028 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10029 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10030 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
| 10031 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10032 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 10033 | |
| 10034 | |
| 10035 | set_cr4_guest_host_mask(vmx); |
| 10036 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10037 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) |
| 10038 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 10039 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 10040 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 10041 | vmcs_write64(TSC_OFFSET, |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 10042 | vcpu->arch.tsc_offset + vmcs12->tsc_offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 10043 | else |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 10044 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 10045 | if (kvm_has_tsc_control) |
| 10046 | decache_tsc_multiplier(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10047 | |
| 10048 | if (enable_vpid) { |
| 10049 | /* |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10050 | * There is no direct mapping between vpid02 and vpid12, the |
| 10051 | * vpid02 is per-vCPU for L0 and reused while the value of |
| 10052 | * vpid12 is changed w/ one invvpid during nested vmentry. |
| 10053 | * The vpid12 is allocated by L1 for L2, so it will not |
| 10054 | * influence global bitmap(for vpid01 and vpid02 allocation) |
| 10055 | * even if spawn a lot of nested vCPUs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10056 | */ |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10057 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
| 10058 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 10059 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 10060 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
| 10061 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02); |
| 10062 | } |
| 10063 | } else { |
| 10064 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 10065 | vmx_flush_tlb(vcpu); |
| 10066 | } |
| 10067 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10068 | } |
| 10069 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10070 | if (nested_cpu_has_ept(vmcs12)) { |
| 10071 | kvm_mmu_unload(vcpu); |
| 10072 | nested_ept_init_mmu_context(vcpu); |
| 10073 | } |
| 10074 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10075 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) |
| 10076 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 10077 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10078 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 10079 | else |
| 10080 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 10081 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 10082 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 10083 | |
| 10084 | /* |
| 10085 | * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified |
| 10086 | * TS bit (for lazy fpu) and bits which we consider mandatory enabled. |
| 10087 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 10088 | * the specifications by L1; It's not enough to take |
| 10089 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 10090 | * have more bits than L1 expected. |
| 10091 | */ |
| 10092 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 10093 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 10094 | |
| 10095 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 10096 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 10097 | |
| 10098 | /* shadow page tables on either EPT or shadow page tables */ |
| 10099 | kvm_set_cr3(vcpu, vmcs12->guest_cr3); |
| 10100 | kvm_mmu_reset_context(vcpu); |
| 10101 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10102 | if (!enable_ept) |
| 10103 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 10104 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10105 | /* |
| 10106 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 |
| 10107 | */ |
| 10108 | if (enable_ept) { |
| 10109 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 10110 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 10111 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 10112 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 10113 | } |
| 10114 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10115 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 10116 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
| 10117 | } |
| 10118 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10119 | /* |
| 10120 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 10121 | * for running an L2 nested guest. |
| 10122 | */ |
| 10123 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 10124 | { |
| 10125 | struct vmcs12 *vmcs12; |
| 10126 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10127 | int cpu; |
| 10128 | struct loaded_vmcs *vmcs02; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 10129 | bool ia32e; |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10130 | u32 msr_entry_idx; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10131 | |
| 10132 | if (!nested_vmx_check_permission(vcpu) || |
| 10133 | !nested_vmx_check_vmcs12(vcpu)) |
| 10134 | return 1; |
| 10135 | |
| 10136 | skip_emulated_instruction(vcpu); |
| 10137 | vmcs12 = get_vmcs12(vcpu); |
| 10138 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10139 | if (enable_shadow_vmcs) |
| 10140 | copy_shadow_to_vmcs12(vmx); |
| 10141 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10142 | /* |
| 10143 | * The nested entry process starts with enforcing various prerequisites |
| 10144 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 10145 | * they fail: As the SDM explains, some conditions should cause the |
| 10146 | * instruction to fail, while others will cause the instruction to seem |
| 10147 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 10148 | * To speed up the normal (success) code path, we should avoid checking |
| 10149 | * for misconfigurations which will anyway be caught by the processor |
| 10150 | * when using the merged vmcs02. |
| 10151 | */ |
| 10152 | if (vmcs12->launch_state == launch) { |
| 10153 | nested_vmx_failValid(vcpu, |
| 10154 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 10155 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 10156 | return 1; |
| 10157 | } |
| 10158 | |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10159 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 10160 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) { |
Paolo Bonzini | 26539bd | 2013-04-15 15:00:27 +0200 | [diff] [blame] | 10161 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10162 | return 1; |
| 10163 | } |
| 10164 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10165 | if (!nested_get_vmcs12_pages(vcpu, vmcs12)) { |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10166 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10167 | return 1; |
| 10168 | } |
| 10169 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10170 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) { |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10171 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10172 | return 1; |
| 10173 | } |
| 10174 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10175 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) { |
| 10176 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10177 | return 1; |
| 10178 | } |
| 10179 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10180 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) { |
| 10181 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10182 | return 1; |
| 10183 | } |
| 10184 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10185 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10186 | vmx->nested.nested_vmx_true_procbased_ctls_low, |
| 10187 | vmx->nested.nested_vmx_procbased_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10188 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10189 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 10190 | vmx->nested.nested_vmx_secondary_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10191 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10192 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 10193 | vmx->nested.nested_vmx_pinbased_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10194 | !vmx_control_verify(vmcs12->vm_exit_controls, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10195 | vmx->nested.nested_vmx_true_exit_ctls_low, |
| 10196 | vmx->nested.nested_vmx_exit_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10197 | !vmx_control_verify(vmcs12->vm_entry_controls, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10198 | vmx->nested.nested_vmx_true_entry_ctls_low, |
| 10199 | vmx->nested.nested_vmx_entry_ctls_high)) |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10200 | { |
| 10201 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10202 | return 1; |
| 10203 | } |
| 10204 | |
| 10205 | if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) || |
| 10206 | ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 10207 | nested_vmx_failValid(vcpu, |
| 10208 | VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
| 10209 | return 1; |
| 10210 | } |
| 10211 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10212 | if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10213 | ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 10214 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10215 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 10216 | return 1; |
| 10217 | } |
| 10218 | if (vmcs12->vmcs_link_pointer != -1ull) { |
| 10219 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10220 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR); |
| 10221 | return 1; |
| 10222 | } |
| 10223 | |
| 10224 | /* |
Jan Kiszka | cb0c8cda | 2013-04-27 12:58:00 +0200 | [diff] [blame] | 10225 | * 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] | 10226 | * are performed on the field for the IA32_EFER MSR: |
| 10227 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 10228 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 10229 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 10230 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 10231 | * CR0.PG) is 1. |
| 10232 | */ |
| 10233 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) { |
| 10234 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 10235 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 10236 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 10237 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 10238 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) { |
| 10239 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10240 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 10241 | return 1; |
| 10242 | } |
| 10243 | } |
| 10244 | |
| 10245 | /* |
| 10246 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 10247 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 10248 | * the values of the LMA and LME bits in the field must each be that of |
| 10249 | * the host address-space size VM-exit control. |
| 10250 | */ |
| 10251 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 10252 | ia32e = (vmcs12->vm_exit_controls & |
| 10253 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 10254 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 10255 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 10256 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) { |
| 10257 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10258 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 10259 | return 1; |
| 10260 | } |
| 10261 | } |
| 10262 | |
| 10263 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10264 | * We're finally done with prerequisite checking, and can start with |
| 10265 | * the nested entry. |
| 10266 | */ |
| 10267 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10268 | vmcs02 = nested_get_current_vmcs02(vmx); |
| 10269 | if (!vmcs02) |
| 10270 | return -ENOMEM; |
| 10271 | |
| 10272 | enter_guest_mode(vcpu); |
| 10273 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 10274 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 10275 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 10276 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10277 | cpu = get_cpu(); |
| 10278 | vmx->loaded_vmcs = vmcs02; |
| 10279 | vmx_vcpu_put(vcpu); |
| 10280 | vmx_vcpu_load(vcpu, cpu); |
| 10281 | vcpu->cpu = cpu; |
| 10282 | put_cpu(); |
| 10283 | |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 10284 | vmx_segment_cache_clear(vmx); |
| 10285 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10286 | prepare_vmcs02(vcpu, vmcs12); |
| 10287 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10288 | msr_entry_idx = nested_vmx_load_msr(vcpu, |
| 10289 | vmcs12->vm_entry_msr_load_addr, |
| 10290 | vmcs12->vm_entry_msr_load_count); |
| 10291 | if (msr_entry_idx) { |
| 10292 | leave_guest_mode(vcpu); |
| 10293 | vmx_load_vmcs01(vcpu); |
| 10294 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10295 | EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx); |
| 10296 | return 1; |
| 10297 | } |
| 10298 | |
| 10299 | vmcs12->launch_state = 1; |
| 10300 | |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10301 | if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 10302 | return kvm_vcpu_halt(vcpu); |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10303 | |
Jan Kiszka | 7af40ad3 | 2014-01-04 18:47:23 +0100 | [diff] [blame] | 10304 | vmx->nested.nested_run_pending = 1; |
| 10305 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10306 | /* |
| 10307 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 10308 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 10309 | * returned as far as L1 is concerned. It will only return (and set |
| 10310 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 10311 | */ |
| 10312 | return 1; |
| 10313 | } |
| 10314 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10315 | /* |
| 10316 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 10317 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 10318 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 10319 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 10320 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 10321 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 10322 | * didn't trap the bit, because if L1 did, so would L0). |
| 10323 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 10324 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 10325 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 10326 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 10327 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 10328 | * changed these bits, and therefore they need to be updated, but L0 |
| 10329 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 10330 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 10331 | */ |
| 10332 | static inline unsigned long |
| 10333 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 10334 | { |
| 10335 | return |
| 10336 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 10337 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 10338 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 10339 | vcpu->arch.cr0_guest_owned_bits)); |
| 10340 | } |
| 10341 | |
| 10342 | static inline unsigned long |
| 10343 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 10344 | { |
| 10345 | return |
| 10346 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 10347 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 10348 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 10349 | vcpu->arch.cr4_guest_owned_bits)); |
| 10350 | } |
| 10351 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10352 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 10353 | struct vmcs12 *vmcs12) |
| 10354 | { |
| 10355 | u32 idt_vectoring; |
| 10356 | unsigned int nr; |
| 10357 | |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 10358 | if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10359 | nr = vcpu->arch.exception.nr; |
| 10360 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 10361 | |
| 10362 | if (kvm_exception_is_soft(nr)) { |
| 10363 | vmcs12->vm_exit_instruction_len = |
| 10364 | vcpu->arch.event_exit_inst_len; |
| 10365 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 10366 | } else |
| 10367 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 10368 | |
| 10369 | if (vcpu->arch.exception.has_error_code) { |
| 10370 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 10371 | vmcs12->idt_vectoring_error_code = |
| 10372 | vcpu->arch.exception.error_code; |
| 10373 | } |
| 10374 | |
| 10375 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
Jan Kiszka | cd2633c | 2013-10-23 17:42:15 +0100 | [diff] [blame] | 10376 | } else if (vcpu->arch.nmi_injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10377 | vmcs12->idt_vectoring_info_field = |
| 10378 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 10379 | } else if (vcpu->arch.interrupt.pending) { |
| 10380 | nr = vcpu->arch.interrupt.nr; |
| 10381 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 10382 | |
| 10383 | if (vcpu->arch.interrupt.soft) { |
| 10384 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 10385 | vmcs12->vm_entry_instruction_len = |
| 10386 | vcpu->arch.event_exit_inst_len; |
| 10387 | } else |
| 10388 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 10389 | |
| 10390 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 10391 | } |
| 10392 | } |
| 10393 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10394 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 10395 | { |
| 10396 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10397 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10398 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 10399 | vmx->nested.preemption_timer_expired) { |
| 10400 | if (vmx->nested.nested_run_pending) |
| 10401 | return -EBUSY; |
| 10402 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 10403 | return 0; |
| 10404 | } |
| 10405 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10406 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
Jan Kiszka | 220c567 | 2014-03-07 20:03:14 +0100 | [diff] [blame] | 10407 | if (vmx->nested.nested_run_pending || |
| 10408 | vcpu->arch.interrupt.pending) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10409 | return -EBUSY; |
| 10410 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 10411 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 10412 | INTR_INFO_VALID_MASK, 0); |
| 10413 | /* |
| 10414 | * The NMI-triggered VM exit counts as injection: |
| 10415 | * clear this one and block further NMIs. |
| 10416 | */ |
| 10417 | vcpu->arch.nmi_pending = 0; |
| 10418 | vmx_set_nmi_mask(vcpu, true); |
| 10419 | return 0; |
| 10420 | } |
| 10421 | |
| 10422 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 10423 | nested_exit_on_intr(vcpu)) { |
| 10424 | if (vmx->nested.nested_run_pending) |
| 10425 | return -EBUSY; |
| 10426 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10427 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10428 | } |
| 10429 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10430 | return vmx_complete_nested_posted_interrupt(vcpu); |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10431 | } |
| 10432 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10433 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 10434 | { |
| 10435 | ktime_t remaining = |
| 10436 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 10437 | u64 value; |
| 10438 | |
| 10439 | if (ktime_to_ns(remaining) <= 0) |
| 10440 | return 0; |
| 10441 | |
| 10442 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 10443 | do_div(value, 1000000); |
| 10444 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 10445 | } |
| 10446 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10447 | /* |
| 10448 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 10449 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 10450 | * and this function updates it to reflect the changes to the guest state while |
| 10451 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 10452 | * without going back to L1), and to reflect the exit reason. |
| 10453 | * Note that we do not have to copy here all VMCS fields, just those that |
| 10454 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 10455 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 10456 | * which already writes to vmcs12 directly. |
| 10457 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10458 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 10459 | u32 exit_reason, u32 exit_intr_info, |
| 10460 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10461 | { |
| 10462 | /* update guest state fields: */ |
| 10463 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 10464 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 10465 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10466 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 10467 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 10468 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 10469 | |
| 10470 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 10471 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 10472 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 10473 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 10474 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 10475 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 10476 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 10477 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 10478 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 10479 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 10480 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 10481 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 10482 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 10483 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 10484 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 10485 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 10486 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 10487 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 10488 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 10489 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 10490 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 10491 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 10492 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 10493 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 10494 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 10495 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 10496 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 10497 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 10498 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 10499 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 10500 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 10501 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 10502 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 10503 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 10504 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 10505 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 10506 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10507 | vmcs12->guest_interruptibility_info = |
| 10508 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 10509 | vmcs12->guest_pending_dbg_exceptions = |
| 10510 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
Jan Kiszka | 3edf1e6 | 2014-01-04 18:47:24 +0100 | [diff] [blame] | 10511 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 10512 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 10513 | else |
| 10514 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10515 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10516 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 10517 | if (vmcs12->vm_exit_controls & |
| 10518 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 10519 | vmcs12->vmx_preemption_timer_value = |
| 10520 | vmx_get_preemption_timer_value(vcpu); |
| 10521 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 10522 | } |
Arthur Chunqi Li | 7854cbc | 2013-09-16 16:11:44 +0800 | [diff] [blame] | 10523 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10524 | /* |
| 10525 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 10526 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 10527 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 10528 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 10529 | * |
| 10530 | * Additionally, restore L2's PDPTR to vmcs12. |
| 10531 | */ |
| 10532 | if (enable_ept) { |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 10533 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10534 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 10535 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 10536 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 10537 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 10538 | } |
| 10539 | |
Jan Dakinevich | 119a9c0 | 2016-09-04 21:22:47 +0300 | [diff] [blame] | 10540 | if (nested_cpu_has_ept(vmcs12)) |
| 10541 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
| 10542 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10543 | if (nested_cpu_has_vid(vmcs12)) |
| 10544 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 10545 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 10546 | vmcs12->vm_entry_controls = |
| 10547 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 10548 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 10549 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 10550 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 10551 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 10552 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 10553 | } |
| 10554 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10555 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 10556 | * the relevant bit asks not to trap the change */ |
Jan Kiszka | b8c07d5 | 2013-04-06 13:51:21 +0200 | [diff] [blame] | 10557 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10558 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
Jan Kiszka | 10ba54a | 2013-08-08 16:26:31 +0200 | [diff] [blame] | 10559 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 10560 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10561 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 10562 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 10563 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 10564 | if (kvm_mpx_supported()) |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10565 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 10566 | if (nested_cpu_has_xsaves(vmcs12)) |
| 10567 | vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10568 | |
| 10569 | /* update exit information fields: */ |
| 10570 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10571 | vmcs12->vm_exit_reason = exit_reason; |
| 10572 | vmcs12->exit_qualification = exit_qualification; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10573 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10574 | vmcs12->vm_exit_intr_info = exit_intr_info; |
Jan Kiszka | c0d1c77 | 2013-04-14 12:12:50 +0200 | [diff] [blame] | 10575 | if ((vmcs12->vm_exit_intr_info & |
| 10576 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == |
| 10577 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) |
| 10578 | vmcs12->vm_exit_intr_error_code = |
| 10579 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10580 | vmcs12->idt_vectoring_info_field = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10581 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 10582 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 10583 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10584 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
| 10585 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 10586 | * instead of reading the real value. */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10587 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10588 | |
| 10589 | /* |
| 10590 | * Transfer the event that L0 or L1 may wanted to inject into |
| 10591 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 10592 | */ |
| 10593 | vmcs12_save_pending_event(vcpu, vmcs12); |
| 10594 | } |
| 10595 | |
| 10596 | /* |
| 10597 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 10598 | * preserved above and would only end up incorrectly in L1. |
| 10599 | */ |
| 10600 | vcpu->arch.nmi_injected = false; |
| 10601 | kvm_clear_exception_queue(vcpu); |
| 10602 | kvm_clear_interrupt_queue(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10603 | } |
| 10604 | |
| 10605 | /* |
| 10606 | * A part of what we need to when the nested L2 guest exits and we want to |
| 10607 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 10608 | * in vmcs12. |
| 10609 | * This function is to be called not only on normal nested exit, but also on |
| 10610 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 10611 | * Failures During or After Loading Guest State"). |
| 10612 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 10613 | */ |
Jan Kiszka | 733568f | 2013-02-23 15:07:47 +0100 | [diff] [blame] | 10614 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 10615 | struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10616 | { |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 10617 | struct kvm_segment seg; |
| 10618 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10619 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 10620 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 10621 | 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] | 10622 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 10623 | else |
| 10624 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 10625 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 10626 | |
| 10627 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 10628 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
H. Peter Anvin | 1adfa76 | 2013-04-27 16:10:11 -0700 | [diff] [blame] | 10629 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10630 | /* |
| 10631 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 10632 | * actually changed, because it depends on the current state of |
| 10633 | * fpu_active (which may have changed). |
| 10634 | * Note that vmx_set_cr0 refers to efer set above. |
| 10635 | */ |
Jan Kiszka | 9e3e4db | 2013-09-03 21:11:45 +0200 | [diff] [blame] | 10636 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10637 | /* |
| 10638 | * If we did fpu_activate()/fpu_deactivate() during L2's run, we need |
| 10639 | * to apply the same changes to L1's vmcs. We just set cr0 correctly, |
| 10640 | * but we also need to update cr0_guest_host_mask and exception_bitmap. |
| 10641 | */ |
| 10642 | update_exception_bitmap(vcpu); |
| 10643 | vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0); |
| 10644 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 10645 | |
| 10646 | /* |
| 10647 | * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01 |
| 10648 | * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask(); |
| 10649 | */ |
| 10650 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 10651 | kvm_set_cr4(vcpu, vmcs12->host_cr4); |
| 10652 | |
Jan Kiszka | 29bf08f | 2013-12-28 16:31:52 +0100 | [diff] [blame] | 10653 | nested_ept_uninit_mmu_context(vcpu); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10654 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10655 | kvm_set_cr3(vcpu, vmcs12->host_cr3); |
| 10656 | kvm_mmu_reset_context(vcpu); |
| 10657 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10658 | if (!enable_ept) |
| 10659 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
| 10660 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10661 | if (enable_vpid) { |
| 10662 | /* |
| 10663 | * Trivially support vpid by letting L2s share their parent |
| 10664 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 10665 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 10666 | */ |
| 10667 | vmx_flush_tlb(vcpu); |
| 10668 | } |
| 10669 | |
| 10670 | |
| 10671 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 10672 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 10673 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 10674 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 10675 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10676 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10677 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 10678 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 10679 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 10680 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10681 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10682 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10683 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 10684 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10685 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 10686 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 10687 | vmcs12->host_ia32_perf_global_ctrl); |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 10688 | |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 10689 | /* Set L1 segment info according to Intel SDM |
| 10690 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 10691 | seg = (struct kvm_segment) { |
| 10692 | .base = 0, |
| 10693 | .limit = 0xFFFFFFFF, |
| 10694 | .selector = vmcs12->host_cs_selector, |
| 10695 | .type = 11, |
| 10696 | .present = 1, |
| 10697 | .s = 1, |
| 10698 | .g = 1 |
| 10699 | }; |
| 10700 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 10701 | seg.l = 1; |
| 10702 | else |
| 10703 | seg.db = 1; |
| 10704 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 10705 | seg = (struct kvm_segment) { |
| 10706 | .base = 0, |
| 10707 | .limit = 0xFFFFFFFF, |
| 10708 | .type = 3, |
| 10709 | .present = 1, |
| 10710 | .s = 1, |
| 10711 | .db = 1, |
| 10712 | .g = 1 |
| 10713 | }; |
| 10714 | seg.selector = vmcs12->host_ds_selector; |
| 10715 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 10716 | seg.selector = vmcs12->host_es_selector; |
| 10717 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 10718 | seg.selector = vmcs12->host_ss_selector; |
| 10719 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 10720 | seg.selector = vmcs12->host_fs_selector; |
| 10721 | seg.base = vmcs12->host_fs_base; |
| 10722 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 10723 | seg.selector = vmcs12->host_gs_selector; |
| 10724 | seg.base = vmcs12->host_gs_base; |
| 10725 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 10726 | seg = (struct kvm_segment) { |
Gleb Natapov | 205befd | 2013-08-04 15:08:06 +0300 | [diff] [blame] | 10727 | .base = vmcs12->host_tr_base, |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 10728 | .limit = 0x67, |
| 10729 | .selector = vmcs12->host_tr_selector, |
| 10730 | .type = 11, |
| 10731 | .present = 1 |
| 10732 | }; |
| 10733 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 10734 | |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 10735 | kvm_set_dr(vcpu, 7, 0x400); |
| 10736 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10737 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10738 | if (cpu_has_vmx_msr_bitmap()) |
| 10739 | vmx_set_msr_bitmap(vcpu); |
| 10740 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10741 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 10742 | vmcs12->vm_exit_msr_load_count)) |
| 10743 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10744 | } |
| 10745 | |
| 10746 | /* |
| 10747 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 10748 | * and modify vmcs12 to make it see what it would expect to see there if |
| 10749 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 10750 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10751 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 10752 | u32 exit_intr_info, |
| 10753 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10754 | { |
| 10755 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10756 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 10757 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10758 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 10759 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 10760 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10761 | leave_guest_mode(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10762 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 10763 | exit_qualification); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10764 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10765 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
| 10766 | vmcs12->vm_exit_msr_store_count)) |
| 10767 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
| 10768 | |
Wanpeng Li | f3380ca | 2014-08-05 12:42:23 +0800 | [diff] [blame] | 10769 | vmx_load_vmcs01(vcpu); |
| 10770 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 10771 | if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT) |
| 10772 | && nested_exit_intr_ack_set(vcpu)) { |
| 10773 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 10774 | WARN_ON(irq < 0); |
| 10775 | vmcs12->vm_exit_intr_info = irq | |
| 10776 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 10777 | } |
| 10778 | |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 10779 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 10780 | vmcs12->exit_qualification, |
| 10781 | vmcs12->idt_vectoring_info_field, |
| 10782 | vmcs12->vm_exit_intr_info, |
| 10783 | vmcs12->vm_exit_intr_error_code, |
| 10784 | KVM_ISA_VMX); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10785 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 10786 | vm_entry_controls_reset_shadow(vmx); |
| 10787 | vm_exit_controls_reset_shadow(vmx); |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 10788 | vmx_segment_cache_clear(vmx); |
| 10789 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10790 | /* if no vmcs02 cache requested, remove the one we used */ |
| 10791 | if (VMCS02_POOL_SIZE == 0) |
| 10792 | nested_free_vmcs02(vmx, vmx->nested.current_vmptr); |
| 10793 | |
| 10794 | load_vmcs12_host_state(vcpu, vmcs12); |
| 10795 | |
Paolo Bonzini | 9314006 | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 10796 | /* Update any VMCS fields that might have changed while L2 ran */ |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 10797 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Paolo Bonzini | 9314006 | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 10798 | if (vmx->hv_deadline_tsc == -1) |
| 10799 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 10800 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 10801 | else |
| 10802 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 10803 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 10804 | if (kvm_has_tsc_control) |
| 10805 | decache_tsc_multiplier(vmx); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10806 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 10807 | if (vmx->nested.change_vmcs01_virtual_x2apic_mode) { |
| 10808 | vmx->nested.change_vmcs01_virtual_x2apic_mode = false; |
| 10809 | vmx_set_virtual_x2apic_mode(vcpu, |
| 10810 | vcpu->arch.apic_base & X2APIC_ENABLE); |
| 10811 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10812 | |
| 10813 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 10814 | vmx->host_rsp = 0; |
| 10815 | |
| 10816 | /* Unpin physical memory we referred to in vmcs02 */ |
| 10817 | if (vmx->nested.apic_access_page) { |
| 10818 | nested_release_page(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 10819 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10820 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10821 | if (vmx->nested.virtual_apic_page) { |
| 10822 | nested_release_page(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 10823 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10824 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10825 | if (vmx->nested.pi_desc_page) { |
| 10826 | kunmap(vmx->nested.pi_desc_page); |
| 10827 | nested_release_page(vmx->nested.pi_desc_page); |
| 10828 | vmx->nested.pi_desc_page = NULL; |
| 10829 | vmx->nested.pi_desc = NULL; |
| 10830 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10831 | |
| 10832 | /* |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 10833 | * We are now running in L2, mmu_notifier will force to reload the |
| 10834 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 10835 | */ |
Wanpeng Li | c83b6d1 | 2016-09-06 17:20:33 +0800 | [diff] [blame] | 10836 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 10837 | |
| 10838 | /* |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10839 | * Exiting from L2 to L1, we're now back to L1 which thinks it just |
| 10840 | * finished a VMLAUNCH or VMRESUME instruction, so we need to set the |
| 10841 | * success or failure flag accordingly. |
| 10842 | */ |
| 10843 | if (unlikely(vmx->fail)) { |
| 10844 | vmx->fail = 0; |
| 10845 | nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 10846 | } else |
| 10847 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10848 | if (enable_shadow_vmcs) |
| 10849 | vmx->nested.sync_shadow_vmcs = true; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10850 | |
| 10851 | /* in case we halted in L2 */ |
| 10852 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10853 | } |
| 10854 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10855 | /* |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 10856 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 10857 | */ |
| 10858 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 10859 | { |
| 10860 | if (is_guest_mode(vcpu)) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10861 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 10862 | free_nested(to_vmx(vcpu)); |
| 10863 | } |
| 10864 | |
| 10865 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10866 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 10867 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 10868 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 10869 | * It should only be called before L2 actually succeeded to run, and when |
| 10870 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 10871 | */ |
| 10872 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 10873 | struct vmcs12 *vmcs12, |
| 10874 | u32 reason, unsigned long qualification) |
| 10875 | { |
| 10876 | load_vmcs12_host_state(vcpu, vmcs12); |
| 10877 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 10878 | vmcs12->exit_qualification = qualification; |
| 10879 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10880 | if (enable_shadow_vmcs) |
| 10881 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10882 | } |
| 10883 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 10884 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 10885 | struct x86_instruction_info *info, |
| 10886 | enum x86_intercept_stage stage) |
| 10887 | { |
| 10888 | return X86EMUL_CONTINUE; |
| 10889 | } |
| 10890 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10891 | #ifdef CONFIG_X86_64 |
| 10892 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 10893 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 10894 | u64 divisor, u64 *result) |
| 10895 | { |
| 10896 | u64 low = a << shift, high = a >> (64 - shift); |
| 10897 | |
| 10898 | /* To avoid the overflow on divq */ |
| 10899 | if (high >= divisor) |
| 10900 | return 1; |
| 10901 | |
| 10902 | /* Low hold the result, high hold rem which is discarded */ |
| 10903 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 10904 | "rm" (divisor), "0" (low), "1" (high)); |
| 10905 | *result = low; |
| 10906 | |
| 10907 | return 0; |
| 10908 | } |
| 10909 | |
| 10910 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 10911 | { |
| 10912 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 9175d2e | 2016-06-27 15:08:01 +0200 | [diff] [blame] | 10913 | u64 tscl = rdtsc(); |
| 10914 | u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 10915 | u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10916 | |
| 10917 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 10918 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 10919 | u64_shl_div_u64(delta_tsc, |
| 10920 | kvm_tsc_scaling_ratio_frac_bits, |
| 10921 | vcpu->arch.tsc_scaling_ratio, |
| 10922 | &delta_tsc)) |
| 10923 | return -ERANGE; |
| 10924 | |
| 10925 | /* |
| 10926 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 10927 | * we can't use the preemption timer. |
| 10928 | * It's possible that it fits on later vmentries, but checking |
| 10929 | * on every vmentry is costly so we just use an hrtimer. |
| 10930 | */ |
| 10931 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 10932 | return -ERANGE; |
| 10933 | |
| 10934 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
| 10935 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 10936 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 10937 | return 0; |
| 10938 | } |
| 10939 | |
| 10940 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 10941 | { |
| 10942 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10943 | vmx->hv_deadline_tsc = -1; |
| 10944 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 10945 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 10946 | } |
| 10947 | #endif |
| 10948 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 10949 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 10950 | { |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 10951 | if (ple_gap) |
| 10952 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 10953 | } |
| 10954 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10955 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 10956 | struct kvm_memory_slot *slot) |
| 10957 | { |
| 10958 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 10959 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 10960 | } |
| 10961 | |
| 10962 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 10963 | struct kvm_memory_slot *slot) |
| 10964 | { |
| 10965 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 10966 | } |
| 10967 | |
| 10968 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 10969 | { |
| 10970 | kvm_flush_pml_buffers(kvm); |
| 10971 | } |
| 10972 | |
| 10973 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 10974 | struct kvm_memory_slot *memslot, |
| 10975 | gfn_t offset, unsigned long mask) |
| 10976 | { |
| 10977 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 10978 | } |
| 10979 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 10980 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 10981 | * This routine does the following things for vCPU which is going |
| 10982 | * to be blocked if VT-d PI is enabled. |
| 10983 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 10984 | * we can find the right vCPU to wake up. |
| 10985 | * - Change the Posted-interrupt descriptor as below: |
| 10986 | * 'NDST' <-- vcpu->pre_pcpu |
| 10987 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 10988 | * - If 'ON' is set during this process, which means at least one |
| 10989 | * interrupt is posted for this vCPU, we cannot block it, in |
| 10990 | * this case, return 1, otherwise, return 0. |
| 10991 | * |
| 10992 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 10993 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 10994 | { |
| 10995 | unsigned long flags; |
| 10996 | unsigned int dest; |
| 10997 | struct pi_desc old, new; |
| 10998 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 10999 | |
| 11000 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 11001 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 11002 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11003 | return 0; |
| 11004 | |
| 11005 | vcpu->pre_pcpu = vcpu->cpu; |
| 11006 | spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock, |
| 11007 | vcpu->pre_pcpu), flags); |
| 11008 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 11009 | &per_cpu(blocked_vcpu_on_cpu, |
| 11010 | vcpu->pre_pcpu)); |
| 11011 | spin_unlock_irqrestore(&per_cpu(blocked_vcpu_on_cpu_lock, |
| 11012 | vcpu->pre_pcpu), flags); |
| 11013 | |
| 11014 | do { |
| 11015 | old.control = new.control = pi_desc->control; |
| 11016 | |
| 11017 | /* |
| 11018 | * We should not block the vCPU if |
| 11019 | * an interrupt is posted for it. |
| 11020 | */ |
| 11021 | if (pi_test_on(pi_desc) == 1) { |
| 11022 | spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock, |
| 11023 | vcpu->pre_pcpu), flags); |
| 11024 | list_del(&vcpu->blocked_vcpu_list); |
| 11025 | spin_unlock_irqrestore( |
| 11026 | &per_cpu(blocked_vcpu_on_cpu_lock, |
| 11027 | vcpu->pre_pcpu), flags); |
| 11028 | vcpu->pre_pcpu = -1; |
| 11029 | |
| 11030 | return 1; |
| 11031 | } |
| 11032 | |
| 11033 | WARN((pi_desc->sn == 1), |
| 11034 | "Warning: SN field of posted-interrupts " |
| 11035 | "is set before blocking\n"); |
| 11036 | |
| 11037 | /* |
| 11038 | * Since vCPU can be preempted during this process, |
| 11039 | * vcpu->cpu could be different with pre_pcpu, we |
| 11040 | * need to set pre_pcpu as the destination of wakeup |
| 11041 | * notification event, then we can find the right vCPU |
| 11042 | * to wakeup in wakeup handler if interrupts happen |
| 11043 | * when the vCPU is in blocked state. |
| 11044 | */ |
| 11045 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 11046 | |
| 11047 | if (x2apic_enabled()) |
| 11048 | new.ndst = dest; |
| 11049 | else |
| 11050 | new.ndst = (dest << 8) & 0xFF00; |
| 11051 | |
| 11052 | /* set 'NV' to 'wakeup vector' */ |
| 11053 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
| 11054 | } while (cmpxchg(&pi_desc->control, old.control, |
| 11055 | new.control) != old.control); |
| 11056 | |
| 11057 | return 0; |
| 11058 | } |
| 11059 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11060 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 11061 | { |
| 11062 | if (pi_pre_block(vcpu)) |
| 11063 | return 1; |
| 11064 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11065 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 11066 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 11067 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11068 | return 0; |
| 11069 | } |
| 11070 | |
| 11071 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11072 | { |
| 11073 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 11074 | struct pi_desc old, new; |
| 11075 | unsigned int dest; |
| 11076 | unsigned long flags; |
| 11077 | |
| 11078 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 11079 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 11080 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11081 | return; |
| 11082 | |
| 11083 | do { |
| 11084 | old.control = new.control = pi_desc->control; |
| 11085 | |
| 11086 | dest = cpu_physical_id(vcpu->cpu); |
| 11087 | |
| 11088 | if (x2apic_enabled()) |
| 11089 | new.ndst = dest; |
| 11090 | else |
| 11091 | new.ndst = (dest << 8) & 0xFF00; |
| 11092 | |
| 11093 | /* Allow posting non-urgent interrupts */ |
| 11094 | new.sn = 0; |
| 11095 | |
| 11096 | /* set 'NV' to 'notification vector' */ |
| 11097 | new.nv = POSTED_INTR_VECTOR; |
| 11098 | } while (cmpxchg(&pi_desc->control, old.control, |
| 11099 | new.control) != old.control); |
| 11100 | |
| 11101 | if(vcpu->pre_pcpu != -1) { |
| 11102 | spin_lock_irqsave( |
| 11103 | &per_cpu(blocked_vcpu_on_cpu_lock, |
| 11104 | vcpu->pre_pcpu), flags); |
| 11105 | list_del(&vcpu->blocked_vcpu_list); |
| 11106 | spin_unlock_irqrestore( |
| 11107 | &per_cpu(blocked_vcpu_on_cpu_lock, |
| 11108 | vcpu->pre_pcpu), flags); |
| 11109 | vcpu->pre_pcpu = -1; |
| 11110 | } |
| 11111 | } |
| 11112 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11113 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
| 11114 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11115 | if (kvm_x86_ops->set_hv_timer) |
| 11116 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 11117 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11118 | pi_post_block(vcpu); |
| 11119 | } |
| 11120 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11121 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11122 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 11123 | * |
| 11124 | * @kvm: kvm |
| 11125 | * @host_irq: host irq of the interrupt |
| 11126 | * @guest_irq: gsi of the interrupt |
| 11127 | * @set: set or unset PI |
| 11128 | * returns 0 on success, < 0 on failure |
| 11129 | */ |
| 11130 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 11131 | uint32_t guest_irq, bool set) |
| 11132 | { |
| 11133 | struct kvm_kernel_irq_routing_entry *e; |
| 11134 | struct kvm_irq_routing_table *irq_rt; |
| 11135 | struct kvm_lapic_irq irq; |
| 11136 | struct kvm_vcpu *vcpu; |
| 11137 | struct vcpu_data vcpu_info; |
| 11138 | int idx, ret = -EINVAL; |
| 11139 | |
| 11140 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 11141 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 11142 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11143 | return 0; |
| 11144 | |
| 11145 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 11146 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
| 11147 | BUG_ON(guest_irq >= irq_rt->nr_rt_entries); |
| 11148 | |
| 11149 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 11150 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 11151 | continue; |
| 11152 | /* |
| 11153 | * VT-d PI cannot support posting multicast/broadcast |
| 11154 | * interrupts to a vCPU, we still use interrupt remapping |
| 11155 | * for these kind of interrupts. |
| 11156 | * |
| 11157 | * For lowest-priority interrupts, we only support |
| 11158 | * those with single CPU as the destination, e.g. user |
| 11159 | * configures the interrupts via /proc/irq or uses |
| 11160 | * irqbalance to make the interrupts single-CPU. |
| 11161 | * |
| 11162 | * We will support full lowest-priority interrupt later. |
| 11163 | */ |
| 11164 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 11165 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 11166 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 11167 | /* |
| 11168 | * Make sure the IRTE is in remapped mode if |
| 11169 | * we don't handle it in posted mode. |
| 11170 | */ |
| 11171 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 11172 | if (ret < 0) { |
| 11173 | printk(KERN_INFO |
| 11174 | "failed to back to remapped mode, irq: %u\n", |
| 11175 | host_irq); |
| 11176 | goto out; |
| 11177 | } |
| 11178 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11179 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 11180 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11181 | |
| 11182 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 11183 | vcpu_info.vector = irq.vector; |
| 11184 | |
Feng Wu | b6ce978 | 2016-01-25 16:53:35 +0800 | [diff] [blame] | 11185 | trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11186 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 11187 | |
| 11188 | if (set) |
| 11189 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
| 11190 | else { |
| 11191 | /* suppress notification event before unposting */ |
| 11192 | pi_set_sn(vcpu_to_pi_desc(vcpu)); |
| 11193 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 11194 | pi_clear_sn(vcpu_to_pi_desc(vcpu)); |
| 11195 | } |
| 11196 | |
| 11197 | if (ret < 0) { |
| 11198 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 11199 | __func__); |
| 11200 | goto out; |
| 11201 | } |
| 11202 | } |
| 11203 | |
| 11204 | ret = 0; |
| 11205 | out: |
| 11206 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 11207 | return ret; |
| 11208 | } |
| 11209 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 11210 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 11211 | { |
| 11212 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 11213 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 11214 | FEATURE_CONTROL_LMCE; |
| 11215 | else |
| 11216 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 11217 | ~FEATURE_CONTROL_LMCE; |
| 11218 | } |
| 11219 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 11220 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11221 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 11222 | .disabled_by_bios = vmx_disabled_by_bios, |
| 11223 | .hardware_setup = hardware_setup, |
| 11224 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 11225 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11226 | .hardware_enable = hardware_enable, |
| 11227 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 11228 | .cpu_has_accelerated_tpr = report_flexpriority, |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 11229 | .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11230 | |
| 11231 | .vcpu_create = vmx_create_vcpu, |
| 11232 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 11233 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11234 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 11235 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11236 | .vcpu_load = vmx_vcpu_load, |
| 11237 | .vcpu_put = vmx_vcpu_put, |
| 11238 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 11239 | .update_bp_intercept = update_exception_bitmap, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11240 | .get_msr = vmx_get_msr, |
| 11241 | .set_msr = vmx_set_msr, |
| 11242 | .get_segment_base = vmx_get_segment_base, |
| 11243 | .get_segment = vmx_get_segment, |
| 11244 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 11245 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11246 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 11247 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 11248 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 11249 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11250 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11251 | .set_cr3 = vmx_set_cr3, |
| 11252 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11253 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11254 | .get_idt = vmx_get_idt, |
| 11255 | .set_idt = vmx_set_idt, |
| 11256 | .get_gdt = vmx_get_gdt, |
| 11257 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 11258 | .get_dr6 = vmx_get_dr6, |
| 11259 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 11260 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 11261 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 11262 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11263 | .get_rflags = vmx_get_rflags, |
| 11264 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 11265 | |
| 11266 | .get_pkru = vmx_get_pkru, |
| 11267 | |
Paolo Bonzini | 0fdd74f | 2015-05-20 11:33:43 +0200 | [diff] [blame] | 11268 | .fpu_activate = vmx_fpu_activate, |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 11269 | .fpu_deactivate = vmx_fpu_deactivate, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11270 | |
| 11271 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11272 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11273 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 11274 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11275 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 11276 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 11277 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 11278 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 11279 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11280 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 11281 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 11282 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 11283 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11284 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 11285 | .get_nmi_mask = vmx_get_nmi_mask, |
| 11286 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11287 | .enable_nmi_window = enable_nmi_window, |
| 11288 | .enable_irq_window = enable_irq_window, |
| 11289 | .update_cr8_intercept = update_cr8_intercept, |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 11290 | .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 11291 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 11292 | .get_enable_apicv = vmx_get_enable_apicv, |
| 11293 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 11294 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
| 11295 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 11296 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 11297 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 11298 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11299 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 11300 | .set_tss_addr = vmx_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 11301 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 11302 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 11303 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 11304 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 11305 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 11306 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 11307 | |
| 11308 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 11309 | |
| 11310 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 11311 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 11312 | |
| 11313 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 11314 | |
| 11315 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 11316 | |
| 11317 | .write_tsc_offset = vmx_write_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 11318 | |
| 11319 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 11320 | |
| 11321 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 11322 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 11323 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 11324 | .xsaves_supported = vmx_xsaves_supported, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11325 | |
| 11326 | .check_nested_events = vmx_check_nested_events, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 11327 | |
| 11328 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 11329 | |
| 11330 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 11331 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 11332 | .flush_log_dirty = vmx_flush_log_dirty, |
| 11333 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 11334 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11335 | .pre_block = vmx_pre_block, |
| 11336 | .post_block = vmx_post_block, |
| 11337 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 11338 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11339 | |
| 11340 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11341 | |
| 11342 | #ifdef CONFIG_X86_64 |
| 11343 | .set_hv_timer = vmx_set_hv_timer, |
| 11344 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 11345 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 11346 | |
| 11347 | .setup_mce = vmx_setup_mce, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11348 | }; |
| 11349 | |
| 11350 | static int __init vmx_init(void) |
| 11351 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 11352 | int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
| 11353 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 11354 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 11355 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 11356 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 11357 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 11358 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 11359 | crash_vmclear_local_loaded_vmcss); |
| 11360 | #endif |
| 11361 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 11362 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11363 | } |
| 11364 | |
| 11365 | static void __exit vmx_exit(void) |
| 11366 | { |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 11367 | #ifdef CONFIG_KEXEC_CORE |
Monam Agarwal | 3b63a43 | 2014-03-22 12:28:10 +0530 | [diff] [blame] | 11368 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 11369 | synchronize_rcu(); |
| 11370 | #endif |
| 11371 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 11372 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11373 | } |
| 11374 | |
| 11375 | module_init(vmx_init) |
| 11376 | module_exit(vmx_exit) |