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> |
Josh Poimboeuf | 935893a | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 36 | #include <linux/frame.h> |
Dan Williams | eb99bd6 | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 37 | #include <linux/nospec.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 38 | #include "kvm_cache_regs.h" |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 39 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 40 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 41 | #include <asm/cpu.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 42 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 43 | #include <asm/desc.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 44 | #include <asm/vmx.h> |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 45 | #include <asm/virtext.h> |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 46 | #include <asm/mce.h> |
Ingo Molnar | 952f07e | 2015-04-26 16:56:05 +0200 | [diff] [blame] | 47 | #include <asm/fpu/internal.h> |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 48 | #include <asm/perf_event.h> |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 49 | #include <asm/debugreg.h> |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 50 | #include <asm/kexec.h> |
Radim Krčmář | dab2087 | 2015-02-09 22:44:07 +0100 | [diff] [blame] | 51 | #include <asm/apic.h> |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 52 | #include <asm/irq_remapping.h> |
Paolo Bonzini | a175d51 | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 53 | #include <asm/microcode.h> |
Thomas Gleixner | 7a2d235 | 2018-04-29 15:01:37 +0200 | [diff] [blame] | 54 | #include <asm/spec-ctrl.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 55 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 56 | #include "trace.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 57 | #include "pmu.h" |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 58 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 59 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 60 | #define __ex_clear(x, reg) \ |
| 61 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 62 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 63 | MODULE_AUTHOR("Qumranet"); |
| 64 | MODULE_LICENSE("GPL"); |
| 65 | |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 66 | static const struct x86_cpu_id vmx_cpu_id[] = { |
| 67 | X86_FEATURE_MATCH(X86_FEATURE_VMX), |
| 68 | {} |
| 69 | }; |
| 70 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); |
| 71 | |
Konrad Rzeszutek Wilk | a0695af | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 72 | static bool __read_mostly nosmt; |
| 73 | module_param(nosmt, bool, S_IRUGO); |
| 74 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 75 | static bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 76 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 77 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 78 | static bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 79 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 80 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 81 | static bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 82 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 83 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 84 | static bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 85 | module_param_named(unrestricted_guest, |
| 86 | enable_unrestricted_guest, bool, S_IRUGO); |
| 87 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 88 | static bool __read_mostly enable_ept_ad_bits = 1; |
| 89 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 90 | |
Avi Kivity | a27685c | 2012-06-12 20:30:18 +0300 | [diff] [blame] | 91 | static bool __read_mostly emulate_invalid_guest_state = true; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 92 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 93 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 94 | static bool __read_mostly vmm_exclusive = 1; |
Dongxiao Xu | b923e62 | 2010-05-11 18:29:45 +0800 | [diff] [blame] | 95 | module_param(vmm_exclusive, bool, S_IRUGO); |
| 96 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 97 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 98 | module_param(fasteoi, bool, S_IRUGO); |
| 99 | |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 100 | static bool __read_mostly enable_apicv = 1; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 101 | module_param(enable_apicv, bool, S_IRUGO); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 102 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 103 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 104 | 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] | 105 | /* |
| 106 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 107 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 108 | * use VMX instructions. |
| 109 | */ |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 110 | static bool __read_mostly nested = 0; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 111 | module_param(nested, bool, S_IRUGO); |
| 112 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 113 | static u64 __read_mostly host_xss; |
| 114 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 115 | static bool __read_mostly enable_pml = 1; |
| 116 | module_param_named(pml, enable_pml, bool, S_IRUGO); |
| 117 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 118 | #define MSR_TYPE_R 1 |
| 119 | #define MSR_TYPE_W 2 |
| 120 | #define MSR_TYPE_RW 3 |
| 121 | |
| 122 | #define MSR_BITMAP_MODE_X2APIC 1 |
| 123 | #define MSR_BITMAP_MODE_X2APIC_APICV 2 |
| 124 | #define MSR_BITMAP_MODE_LM 4 |
| 125 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 126 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
| 127 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 128 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
| 129 | static int __read_mostly cpu_preemption_timer_multi; |
| 130 | static bool __read_mostly enable_preemption_timer = 1; |
| 131 | #ifdef CONFIG_X86_64 |
| 132 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); |
| 133 | #endif |
| 134 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 135 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
| 136 | #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] | 137 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 138 | (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] | 139 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 140 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Andy Lutomirski | 52ce3c2 | 2014-10-07 17:16:21 -0700 | [diff] [blame] | 141 | | X86_CR4_OSXMMEXCPT | X86_CR4_TSD) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 142 | |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 143 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 144 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 145 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 146 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 147 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 148 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 149 | |
Jan Dakinevich | 04c776e | 2018-02-23 11:42:18 +0100 | [diff] [blame] | 150 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ |
| 151 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ |
| 152 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ |
| 153 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ |
| 154 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) |
| 155 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 156 | /* |
| 157 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 158 | * ple_gap: upper bound on the amount of time between two successive |
| 159 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 160 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 161 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 162 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 163 | * less than 2^12 cycles |
| 164 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 165 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 166 | */ |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 167 | #define KVM_VMX_DEFAULT_PLE_GAP 128 |
| 168 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 |
| 169 | #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2 |
| 170 | #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0 |
| 171 | #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \ |
| 172 | INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW |
| 173 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 174 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
| 175 | module_param(ple_gap, int, S_IRUGO); |
| 176 | |
| 177 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 178 | module_param(ple_window, int, S_IRUGO); |
| 179 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 180 | /* Default doubles per-vcpu window every exit. */ |
| 181 | static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW; |
| 182 | module_param(ple_window_grow, int, S_IRUGO); |
| 183 | |
| 184 | /* Default resets per-vcpu window every exit to ple_window. */ |
| 185 | static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK; |
| 186 | module_param(ple_window_shrink, int, S_IRUGO); |
| 187 | |
| 188 | /* Default is to compute the maximum so we can never overflow. */ |
| 189 | static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 190 | static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 191 | module_param(ple_window_max, int, S_IRUGO); |
| 192 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 193 | extern const ulong vmx_return; |
| 194 | |
Konrad Rzeszutek Wilk | af6ce92 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 195 | static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush); |
| 196 | |
| 197 | /* These MUST be in sync with vmentry_l1d_param order. */ |
| 198 | enum vmx_l1d_flush_state { |
| 199 | VMENTER_L1D_FLUSH_NEVER, |
| 200 | VMENTER_L1D_FLUSH_COND, |
| 201 | VMENTER_L1D_FLUSH_ALWAYS, |
| 202 | }; |
| 203 | |
| 204 | static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush = VMENTER_L1D_FLUSH_COND; |
| 205 | |
| 206 | static const struct { |
| 207 | const char *option; |
| 208 | enum vmx_l1d_flush_state cmd; |
| 209 | } vmentry_l1d_param[] = { |
| 210 | {"never", VMENTER_L1D_FLUSH_NEVER}, |
| 211 | {"cond", VMENTER_L1D_FLUSH_COND}, |
| 212 | {"always", VMENTER_L1D_FLUSH_ALWAYS}, |
| 213 | }; |
| 214 | |
| 215 | static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp) |
| 216 | { |
| 217 | unsigned int i; |
| 218 | |
| 219 | if (!s) |
| 220 | return -EINVAL; |
| 221 | |
| 222 | for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) { |
| 223 | if (!strcmp(s, vmentry_l1d_param[i].option)) { |
| 224 | vmentry_l1d_flush = vmentry_l1d_param[i].cmd; |
| 225 | return 0; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
| 232 | static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp) |
| 233 | { |
| 234 | return sprintf(s, "%s\n", vmentry_l1d_param[vmentry_l1d_flush].option); |
| 235 | } |
| 236 | |
| 237 | static const struct kernel_param_ops vmentry_l1d_flush_ops = { |
| 238 | .set = vmentry_l1d_flush_set, |
| 239 | .get = vmentry_l1d_flush_get, |
| 240 | }; |
| 241 | module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, &vmentry_l1d_flush, S_IRUGO); |
| 242 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 243 | #define NR_AUTOLOAD_MSRS 8 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 244 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 245 | struct vmcs { |
| 246 | u32 revision_id; |
| 247 | u32 abort; |
| 248 | char data[0]; |
| 249 | }; |
| 250 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 251 | /* |
| 252 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 253 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 254 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 255 | */ |
| 256 | struct loaded_vmcs { |
| 257 | struct vmcs *vmcs; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 258 | struct vmcs *shadow_vmcs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 259 | int cpu; |
| 260 | int launched; |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 261 | unsigned long *msr_bitmap; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 262 | struct list_head loaded_vmcss_on_cpu_link; |
| 263 | }; |
| 264 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 265 | struct shared_msr_entry { |
| 266 | unsigned index; |
| 267 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 268 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 269 | }; |
| 270 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 271 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 272 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 273 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 274 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 275 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 276 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 277 | * More than one of these structures may exist, if L1 runs multiple L2 guests. |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 278 | * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 279 | * underlying hardware which will be used to run L2. |
| 280 | * This structure is packed to ensure that its layout is identical across |
| 281 | * machines (necessary for live migration). |
| 282 | * If there are changes in this struct, VMCS12_REVISION must be changed. |
| 283 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 284 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 285 | struct __packed vmcs12 { |
| 286 | /* According to the Intel spec, a VMCS region must start with the |
| 287 | * following two fields. Then follow implementation-specific data. |
| 288 | */ |
| 289 | u32 revision_id; |
| 290 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 291 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 292 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 293 | u32 padding[7]; /* room for future expansion */ |
| 294 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 295 | u64 io_bitmap_a; |
| 296 | u64 io_bitmap_b; |
| 297 | u64 msr_bitmap; |
| 298 | u64 vm_exit_msr_store_addr; |
| 299 | u64 vm_exit_msr_load_addr; |
| 300 | u64 vm_entry_msr_load_addr; |
| 301 | u64 tsc_offset; |
| 302 | u64 virtual_apic_page_addr; |
| 303 | u64 apic_access_addr; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 304 | u64 posted_intr_desc_addr; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 305 | u64 ept_pointer; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 306 | u64 eoi_exit_bitmap0; |
| 307 | u64 eoi_exit_bitmap1; |
| 308 | u64 eoi_exit_bitmap2; |
| 309 | u64 eoi_exit_bitmap3; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 310 | u64 xss_exit_bitmap; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 311 | u64 guest_physical_address; |
| 312 | u64 vmcs_link_pointer; |
| 313 | u64 guest_ia32_debugctl; |
| 314 | u64 guest_ia32_pat; |
| 315 | u64 guest_ia32_efer; |
| 316 | u64 guest_ia32_perf_global_ctrl; |
| 317 | u64 guest_pdptr0; |
| 318 | u64 guest_pdptr1; |
| 319 | u64 guest_pdptr2; |
| 320 | u64 guest_pdptr3; |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 321 | u64 guest_bndcfgs; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 322 | u64 host_ia32_pat; |
| 323 | u64 host_ia32_efer; |
| 324 | u64 host_ia32_perf_global_ctrl; |
| 325 | u64 padding64[8]; /* room for future expansion */ |
| 326 | /* |
| 327 | * To allow migration of L1 (complete with its L2 guests) between |
| 328 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 329 | * unsigned long fields with no explict size. We use u64 (aliased |
| 330 | * natural_width) instead. Luckily, x86 is little-endian. |
| 331 | */ |
| 332 | natural_width cr0_guest_host_mask; |
| 333 | natural_width cr4_guest_host_mask; |
| 334 | natural_width cr0_read_shadow; |
| 335 | natural_width cr4_read_shadow; |
| 336 | natural_width cr3_target_value0; |
| 337 | natural_width cr3_target_value1; |
| 338 | natural_width cr3_target_value2; |
| 339 | natural_width cr3_target_value3; |
| 340 | natural_width exit_qualification; |
| 341 | natural_width guest_linear_address; |
| 342 | natural_width guest_cr0; |
| 343 | natural_width guest_cr3; |
| 344 | natural_width guest_cr4; |
| 345 | natural_width guest_es_base; |
| 346 | natural_width guest_cs_base; |
| 347 | natural_width guest_ss_base; |
| 348 | natural_width guest_ds_base; |
| 349 | natural_width guest_fs_base; |
| 350 | natural_width guest_gs_base; |
| 351 | natural_width guest_ldtr_base; |
| 352 | natural_width guest_tr_base; |
| 353 | natural_width guest_gdtr_base; |
| 354 | natural_width guest_idtr_base; |
| 355 | natural_width guest_dr7; |
| 356 | natural_width guest_rsp; |
| 357 | natural_width guest_rip; |
| 358 | natural_width guest_rflags; |
| 359 | natural_width guest_pending_dbg_exceptions; |
| 360 | natural_width guest_sysenter_esp; |
| 361 | natural_width guest_sysenter_eip; |
| 362 | natural_width host_cr0; |
| 363 | natural_width host_cr3; |
| 364 | natural_width host_cr4; |
| 365 | natural_width host_fs_base; |
| 366 | natural_width host_gs_base; |
| 367 | natural_width host_tr_base; |
| 368 | natural_width host_gdtr_base; |
| 369 | natural_width host_idtr_base; |
| 370 | natural_width host_ia32_sysenter_esp; |
| 371 | natural_width host_ia32_sysenter_eip; |
| 372 | natural_width host_rsp; |
| 373 | natural_width host_rip; |
| 374 | natural_width paddingl[8]; /* room for future expansion */ |
| 375 | u32 pin_based_vm_exec_control; |
| 376 | u32 cpu_based_vm_exec_control; |
| 377 | u32 exception_bitmap; |
| 378 | u32 page_fault_error_code_mask; |
| 379 | u32 page_fault_error_code_match; |
| 380 | u32 cr3_target_count; |
| 381 | u32 vm_exit_controls; |
| 382 | u32 vm_exit_msr_store_count; |
| 383 | u32 vm_exit_msr_load_count; |
| 384 | u32 vm_entry_controls; |
| 385 | u32 vm_entry_msr_load_count; |
| 386 | u32 vm_entry_intr_info_field; |
| 387 | u32 vm_entry_exception_error_code; |
| 388 | u32 vm_entry_instruction_len; |
| 389 | u32 tpr_threshold; |
| 390 | u32 secondary_vm_exec_control; |
| 391 | u32 vm_instruction_error; |
| 392 | u32 vm_exit_reason; |
| 393 | u32 vm_exit_intr_info; |
| 394 | u32 vm_exit_intr_error_code; |
| 395 | u32 idt_vectoring_info_field; |
| 396 | u32 idt_vectoring_error_code; |
| 397 | u32 vm_exit_instruction_len; |
| 398 | u32 vmx_instruction_info; |
| 399 | u32 guest_es_limit; |
| 400 | u32 guest_cs_limit; |
| 401 | u32 guest_ss_limit; |
| 402 | u32 guest_ds_limit; |
| 403 | u32 guest_fs_limit; |
| 404 | u32 guest_gs_limit; |
| 405 | u32 guest_ldtr_limit; |
| 406 | u32 guest_tr_limit; |
| 407 | u32 guest_gdtr_limit; |
| 408 | u32 guest_idtr_limit; |
| 409 | u32 guest_es_ar_bytes; |
| 410 | u32 guest_cs_ar_bytes; |
| 411 | u32 guest_ss_ar_bytes; |
| 412 | u32 guest_ds_ar_bytes; |
| 413 | u32 guest_fs_ar_bytes; |
| 414 | u32 guest_gs_ar_bytes; |
| 415 | u32 guest_ldtr_ar_bytes; |
| 416 | u32 guest_tr_ar_bytes; |
| 417 | u32 guest_interruptibility_info; |
| 418 | u32 guest_activity_state; |
| 419 | u32 guest_sysenter_cs; |
| 420 | u32 host_ia32_sysenter_cs; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 421 | u32 vmx_preemption_timer_value; |
| 422 | u32 padding32[7]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 423 | u16 virtual_processor_id; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 424 | u16 posted_intr_nv; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 425 | u16 guest_es_selector; |
| 426 | u16 guest_cs_selector; |
| 427 | u16 guest_ss_selector; |
| 428 | u16 guest_ds_selector; |
| 429 | u16 guest_fs_selector; |
| 430 | u16 guest_gs_selector; |
| 431 | u16 guest_ldtr_selector; |
| 432 | u16 guest_tr_selector; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 433 | u16 guest_intr_status; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 434 | u16 host_es_selector; |
| 435 | u16 host_cs_selector; |
| 436 | u16 host_ss_selector; |
| 437 | u16 host_ds_selector; |
| 438 | u16 host_fs_selector; |
| 439 | u16 host_gs_selector; |
| 440 | u16 host_tr_selector; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 441 | }; |
| 442 | |
| 443 | /* |
| 444 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 445 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 446 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. |
| 447 | */ |
| 448 | #define VMCS12_REVISION 0x11e57ed0 |
| 449 | |
| 450 | /* |
| 451 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 452 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 453 | * current implementation, 4K are reserved to avoid future complications. |
| 454 | */ |
| 455 | #define VMCS12_SIZE 0x1000 |
| 456 | |
| 457 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 458 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 459 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 460 | */ |
| 461 | struct nested_vmx { |
| 462 | /* Has the level1 guest done vmxon? */ |
| 463 | bool vmxon; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 464 | gpa_t vmxon_ptr; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 465 | |
| 466 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 467 | gpa_t current_vmptr; |
| 468 | /* The host-usable pointer to the above */ |
| 469 | struct page *current_vmcs12_page; |
| 470 | struct vmcs12 *current_vmcs12; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 471 | /* |
| 472 | * Cache of the guest's VMCS, existing outside of guest memory. |
| 473 | * Loaded from guest memory during VMPTRLD. Flushed to guest |
| 474 | * memory during VMXOFF, VMCLEAR, VMPTRLD. |
| 475 | */ |
| 476 | struct vmcs12 *cached_vmcs12; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 477 | /* |
| 478 | * Indicates if the shadow vmcs must be updated with the |
| 479 | * data hold by vmcs12 |
| 480 | */ |
| 481 | bool sync_shadow_vmcs; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 482 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 483 | bool change_vmcs01_virtual_x2apic_mode; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 484 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 485 | bool nested_run_pending; |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 486 | |
| 487 | struct loaded_vmcs vmcs02; |
| 488 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 489 | /* |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 490 | * Guest pages referred to in the vmcs02 with host-physical |
| 491 | * pointers, so we must keep them pinned while L2 runs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 492 | */ |
| 493 | struct page *apic_access_page; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 494 | struct page *virtual_apic_page; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 495 | struct page *pi_desc_page; |
| 496 | struct pi_desc *pi_desc; |
| 497 | bool pi_pending; |
| 498 | u16 posted_intr_nv; |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 499 | |
| 500 | struct hrtimer preemption_timer; |
| 501 | bool preemption_timer_expired; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 502 | |
| 503 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ |
| 504 | u64 vmcs01_debugctl; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 505 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 506 | u16 vpid02; |
| 507 | u16 last_vpid; |
| 508 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 509 | u32 nested_vmx_procbased_ctls_low; |
| 510 | u32 nested_vmx_procbased_ctls_high; |
| 511 | u32 nested_vmx_true_procbased_ctls_low; |
| 512 | u32 nested_vmx_secondary_ctls_low; |
| 513 | u32 nested_vmx_secondary_ctls_high; |
| 514 | u32 nested_vmx_pinbased_ctls_low; |
| 515 | u32 nested_vmx_pinbased_ctls_high; |
| 516 | u32 nested_vmx_exit_ctls_low; |
| 517 | u32 nested_vmx_exit_ctls_high; |
| 518 | u32 nested_vmx_true_exit_ctls_low; |
| 519 | u32 nested_vmx_entry_ctls_low; |
| 520 | u32 nested_vmx_entry_ctls_high; |
| 521 | u32 nested_vmx_true_entry_ctls_low; |
| 522 | u32 nested_vmx_misc_low; |
| 523 | u32 nested_vmx_misc_high; |
| 524 | u32 nested_vmx_ept_caps; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 525 | u32 nested_vmx_vpid_caps; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 526 | }; |
| 527 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 528 | #define POSTED_INTR_ON 0 |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 529 | #define POSTED_INTR_SN 1 |
| 530 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 531 | /* Posted-Interrupt Descriptor */ |
| 532 | struct pi_desc { |
| 533 | u32 pir[8]; /* Posted interrupt requested */ |
Feng Wu | 6ef1522 | 2015-09-18 22:29:45 +0800 | [diff] [blame] | 534 | union { |
| 535 | struct { |
| 536 | /* bit 256 - Outstanding Notification */ |
| 537 | u16 on : 1, |
| 538 | /* bit 257 - Suppress Notification */ |
| 539 | sn : 1, |
| 540 | /* bit 271:258 - Reserved */ |
| 541 | rsvd_1 : 14; |
| 542 | /* bit 279:272 - Notification Vector */ |
| 543 | u8 nv; |
| 544 | /* bit 287:280 - Reserved */ |
| 545 | u8 rsvd_2; |
| 546 | /* bit 319:288 - Notification Destination */ |
| 547 | u32 ndst; |
| 548 | }; |
| 549 | u64 control; |
| 550 | }; |
| 551 | u32 rsvd[6]; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 552 | } __aligned(64); |
| 553 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 554 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
| 555 | { |
| 556 | return test_and_set_bit(POSTED_INTR_ON, |
| 557 | (unsigned long *)&pi_desc->control); |
| 558 | } |
| 559 | |
| 560 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) |
| 561 | { |
| 562 | return test_and_clear_bit(POSTED_INTR_ON, |
| 563 | (unsigned long *)&pi_desc->control); |
| 564 | } |
| 565 | |
| 566 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) |
| 567 | { |
| 568 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); |
| 569 | } |
| 570 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 571 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
| 572 | { |
| 573 | return clear_bit(POSTED_INTR_SN, |
| 574 | (unsigned long *)&pi_desc->control); |
| 575 | } |
| 576 | |
| 577 | static inline void pi_set_sn(struct pi_desc *pi_desc) |
| 578 | { |
| 579 | return set_bit(POSTED_INTR_SN, |
| 580 | (unsigned long *)&pi_desc->control); |
| 581 | } |
| 582 | |
| 583 | static inline int pi_test_on(struct pi_desc *pi_desc) |
| 584 | { |
| 585 | return test_bit(POSTED_INTR_ON, |
| 586 | (unsigned long *)&pi_desc->control); |
| 587 | } |
| 588 | |
| 589 | static inline int pi_test_sn(struct pi_desc *pi_desc) |
| 590 | { |
| 591 | return test_bit(POSTED_INTR_SN, |
| 592 | (unsigned long *)&pi_desc->control); |
| 593 | } |
| 594 | |
Konrad Rzeszutek Wilk | 57e3ada | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 595 | struct vmx_msrs { |
| 596 | unsigned int nr; |
| 597 | struct vmx_msr_entry val[NR_AUTOLOAD_MSRS]; |
| 598 | }; |
| 599 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 600 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 601 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 602 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 603 | u8 fail; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 604 | bool nmi_known_unmasked; |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 605 | u8 msr_bitmap_mode; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 606 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 607 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 608 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 609 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 610 | int nmsrs; |
| 611 | int save_nmsrs; |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 612 | unsigned long host_idt_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 613 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 614 | u64 msr_host_kernel_gs_base; |
| 615 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 616 | #endif |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 617 | |
KarimAllah Ahmed | 755502f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 618 | u64 arch_capabilities; |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 619 | u64 spec_ctrl; |
KarimAllah Ahmed | 755502f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 620 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 621 | u32 vm_entry_controls_shadow; |
| 622 | u32 vm_exit_controls_shadow; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 623 | /* |
| 624 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 625 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 626 | * guest (L2), it points to a different VMCS. |
| 627 | */ |
| 628 | struct loaded_vmcs vmcs01; |
| 629 | struct loaded_vmcs *loaded_vmcs; |
| 630 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 631 | struct msr_autoload { |
Konrad Rzeszutek Wilk | 57e3ada | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 632 | struct vmx_msrs guest; |
| 633 | struct vmx_msrs host; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 634 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 635 | struct { |
| 636 | int loaded; |
| 637 | u16 fs_sel, gs_sel, ldt_sel; |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 638 | #ifdef CONFIG_X86_64 |
| 639 | u16 ds_sel, es_sel; |
| 640 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 641 | int gs_ldt_reload_needed; |
| 642 | int fs_reload_needed; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 643 | u64 msr_host_bndcfgs; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 644 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 645 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 646 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 647 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 648 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 649 | struct kvm_segment segs[8]; |
| 650 | } rmode; |
| 651 | struct { |
| 652 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 653 | struct kvm_save_segment { |
| 654 | u16 selector; |
| 655 | unsigned long base; |
| 656 | u32 limit; |
| 657 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 658 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 659 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 660 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 661 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 662 | |
| 663 | /* Support for vnmi-less CPUs */ |
| 664 | int soft_vnmi_blocked; |
| 665 | ktime_t entry_time; |
| 666 | s64 vnmi_blocked_time; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 667 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 668 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 669 | /* Posted interrupt descriptor */ |
| 670 | struct pi_desc pi_desc; |
| 671 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 672 | /* Support for a guest hypervisor (nested VMX) */ |
| 673 | struct nested_vmx nested; |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 674 | |
| 675 | /* Dynamic PLE window. */ |
| 676 | int ple_window; |
| 677 | bool ple_window_dirty; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 678 | |
| 679 | /* Support for PML */ |
| 680 | #define PML_ENTITY_NUM 512 |
| 681 | struct page *pml_pg; |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 682 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 683 | /* apic deadline value in host tsc */ |
| 684 | u64 hv_deadline_tsc; |
| 685 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 686 | u64 current_tsc_ratio; |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 687 | |
| 688 | bool guest_pkru_valid; |
| 689 | u32 guest_pkru; |
| 690 | u32 host_pkru; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 691 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 692 | /* |
| 693 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in |
| 694 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included |
| 695 | * in msr_ia32_feature_control_valid_bits. |
| 696 | */ |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 697 | u64 msr_ia32_feature_control; |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 698 | u64 msr_ia32_feature_control_valid_bits; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 699 | }; |
| 700 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 701 | enum segment_cache_field { |
| 702 | SEG_FIELD_SEL = 0, |
| 703 | SEG_FIELD_BASE = 1, |
| 704 | SEG_FIELD_LIMIT = 2, |
| 705 | SEG_FIELD_AR = 3, |
| 706 | |
| 707 | SEG_FIELD_NR = 4 |
| 708 | }; |
| 709 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 710 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 711 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 712 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 713 | } |
| 714 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 715 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
| 716 | { |
| 717 | return &(to_vmx(vcpu)->pi_desc); |
| 718 | } |
| 719 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 720 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
| 721 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) |
| 722 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ |
| 723 | [number##_HIGH] = VMCS12_OFFSET(name)+4 |
| 724 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 725 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 726 | static unsigned long shadow_read_only_fields[] = { |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 727 | /* |
| 728 | * We do NOT shadow fields that are modified when L0 |
| 729 | * traps and emulates any vmx instruction (e.g. VMPTRLD, |
| 730 | * VMXON...) executed by L1. |
| 731 | * For example, VM_INSTRUCTION_ERROR is read |
| 732 | * by L1 if a vmx instruction fails (part of the error path). |
| 733 | * Note the code assumes this logic. If for some reason |
| 734 | * we start shadowing these fields then we need to |
| 735 | * force a shadow sync when L0 emulates vmx instructions |
| 736 | * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified |
| 737 | * by nested_vmx_failValid) |
| 738 | */ |
| 739 | VM_EXIT_REASON, |
| 740 | VM_EXIT_INTR_INFO, |
| 741 | VM_EXIT_INSTRUCTION_LEN, |
| 742 | IDT_VECTORING_INFO_FIELD, |
| 743 | IDT_VECTORING_ERROR_CODE, |
| 744 | VM_EXIT_INTR_ERROR_CODE, |
| 745 | EXIT_QUALIFICATION, |
| 746 | GUEST_LINEAR_ADDRESS, |
| 747 | GUEST_PHYSICAL_ADDRESS |
| 748 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 749 | static int max_shadow_read_only_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 750 | ARRAY_SIZE(shadow_read_only_fields); |
| 751 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 752 | static unsigned long shadow_read_write_fields[] = { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 753 | TPR_THRESHOLD, |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 754 | GUEST_RIP, |
| 755 | GUEST_RSP, |
| 756 | GUEST_CR0, |
| 757 | GUEST_CR3, |
| 758 | GUEST_CR4, |
| 759 | GUEST_INTERRUPTIBILITY_INFO, |
| 760 | GUEST_RFLAGS, |
| 761 | GUEST_CS_SELECTOR, |
| 762 | GUEST_CS_AR_BYTES, |
| 763 | GUEST_CS_LIMIT, |
| 764 | GUEST_CS_BASE, |
| 765 | GUEST_ES_BASE, |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 766 | GUEST_BNDCFGS, |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 767 | CR0_GUEST_HOST_MASK, |
| 768 | CR0_READ_SHADOW, |
| 769 | CR4_READ_SHADOW, |
| 770 | TSC_OFFSET, |
| 771 | EXCEPTION_BITMAP, |
| 772 | CPU_BASED_VM_EXEC_CONTROL, |
| 773 | VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 774 | VM_ENTRY_INTR_INFO_FIELD, |
| 775 | VM_ENTRY_INSTRUCTION_LEN, |
| 776 | VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 777 | HOST_FS_BASE, |
| 778 | HOST_GS_BASE, |
| 779 | HOST_FS_SELECTOR, |
| 780 | HOST_GS_SELECTOR |
| 781 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 782 | static int max_shadow_read_write_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 783 | ARRAY_SIZE(shadow_read_write_fields); |
| 784 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 785 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 786 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 787 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 788 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 789 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 790 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 791 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 792 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 793 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 794 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 795 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 796 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 797 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 798 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 799 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 800 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 801 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 802 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 803 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 804 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 805 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 806 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 807 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 808 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 809 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
| 810 | FIELD64(TSC_OFFSET, tsc_offset), |
| 811 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 812 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 813 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 814 | FIELD64(EPT_POINTER, ept_pointer), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 815 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
| 816 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), |
| 817 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), |
| 818 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 819 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 820 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 821 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 822 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 823 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 824 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 825 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 826 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 827 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 828 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 829 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 830 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 831 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 832 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 833 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 834 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 835 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 836 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 837 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 838 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 839 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 840 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 841 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 842 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 843 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 844 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 845 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 846 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 847 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 848 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 849 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 850 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 851 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 852 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 853 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 854 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 855 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 856 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 857 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 858 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 859 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 860 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 861 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 862 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 863 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 864 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 865 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 866 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 867 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 868 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 869 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 870 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 871 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 872 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 873 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 874 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 875 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 876 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 877 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 878 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 879 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 880 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 881 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 882 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 883 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 884 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 885 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 886 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 887 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 888 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 889 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 890 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 891 | FIELD(GUEST_CR0, guest_cr0), |
| 892 | FIELD(GUEST_CR3, guest_cr3), |
| 893 | FIELD(GUEST_CR4, guest_cr4), |
| 894 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 895 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 896 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 897 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 898 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 899 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 900 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 901 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 902 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 903 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 904 | FIELD(GUEST_DR7, guest_dr7), |
| 905 | FIELD(GUEST_RSP, guest_rsp), |
| 906 | FIELD(GUEST_RIP, guest_rip), |
| 907 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 908 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 909 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 910 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 911 | FIELD(HOST_CR0, host_cr0), |
| 912 | FIELD(HOST_CR3, host_cr3), |
| 913 | FIELD(HOST_CR4, host_cr4), |
| 914 | FIELD(HOST_FS_BASE, host_fs_base), |
| 915 | FIELD(HOST_GS_BASE, host_gs_base), |
| 916 | FIELD(HOST_TR_BASE, host_tr_base), |
| 917 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 918 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 919 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 920 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 921 | FIELD(HOST_RSP, host_rsp), |
| 922 | FIELD(HOST_RIP, host_rip), |
| 923 | }; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 924 | |
| 925 | static inline short vmcs_field_to_offset(unsigned long field) |
| 926 | { |
Dan Williams | eb99bd6 | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 927 | const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table); |
| 928 | unsigned short offset; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 929 | |
Dan Williams | eb99bd6 | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 930 | BUILD_BUG_ON(size > SHRT_MAX); |
| 931 | if (field >= size) |
Andrew Honig | 012df71 | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 932 | return -ENOENT; |
| 933 | |
Dan Williams | eb99bd6 | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 934 | field = array_index_nospec(field, size); |
| 935 | offset = vmcs_field_to_offset_table[field]; |
| 936 | if (offset == 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 937 | return -ENOENT; |
Dan Williams | eb99bd6 | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 938 | return offset; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 939 | } |
| 940 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 941 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 942 | { |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 943 | return to_vmx(vcpu)->nested.cached_vmcs12; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | static struct page *nested_get_page(struct kvm_vcpu *vcpu, gpa_t addr) |
| 947 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 948 | struct page *page = kvm_vcpu_gfn_to_page(vcpu, addr >> PAGE_SHIFT); |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 949 | if (is_error_page(page)) |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 950 | return NULL; |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 951 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 952 | return page; |
| 953 | } |
| 954 | |
| 955 | static void nested_release_page(struct page *page) |
| 956 | { |
| 957 | kvm_release_page_dirty(page); |
| 958 | } |
| 959 | |
| 960 | static void nested_release_page_clean(struct page *page) |
| 961 | { |
| 962 | kvm_release_page_clean(page); |
| 963 | } |
| 964 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 965 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 966 | static u64 construct_eptp(unsigned long root_hpa); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 967 | static void kvm_cpu_vmxon(u64 addr); |
| 968 | static void kvm_cpu_vmxoff(void); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 969 | static bool vmx_xsaves_supported(void); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 970 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 971 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 972 | struct kvm_segment *var, int seg); |
| 973 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 974 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 975 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 976 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 977 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 978 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 979 | static int alloc_identity_pagetable(struct kvm *kvm); |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 980 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu); |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 981 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 982 | u32 msr, int type); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 983 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 984 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 985 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 986 | /* |
| 987 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 988 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 989 | */ |
| 990 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 991 | static DEFINE_PER_CPU(struct desc_ptr, host_gdt); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 992 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 993 | /* |
| 994 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 995 | * can find which vCPU should be waken up. |
| 996 | */ |
| 997 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 998 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 999 | |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 1000 | static unsigned long *vmx_io_bitmap_a; |
| 1001 | static unsigned long *vmx_io_bitmap_b; |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 1002 | static unsigned long *vmx_vmread_bitmap; |
| 1003 | static unsigned long *vmx_vmwrite_bitmap; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 1004 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1005 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1006 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1007 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1008 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 1009 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 1010 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1011 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1012 | int size; |
| 1013 | int order; |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1014 | u32 basic_cap; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1015 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1016 | u32 pin_based_exec_ctrl; |
| 1017 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1018 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1019 | u32 vmexit_ctrl; |
| 1020 | u32 vmentry_ctrl; |
| 1021 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1022 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 1023 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1024 | u32 ept; |
| 1025 | u32 vpid; |
| 1026 | } vmx_capability; |
| 1027 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1028 | #define VMX_SEGMENT_FIELD(seg) \ |
| 1029 | [VCPU_SREG_##seg] = { \ |
| 1030 | .selector = GUEST_##seg##_SELECTOR, \ |
| 1031 | .base = GUEST_##seg##_BASE, \ |
| 1032 | .limit = GUEST_##seg##_LIMIT, \ |
| 1033 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 1034 | } |
| 1035 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 1036 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1037 | unsigned selector; |
| 1038 | unsigned base; |
| 1039 | unsigned limit; |
| 1040 | unsigned ar_bytes; |
| 1041 | } kvm_vmx_segment_fields[] = { |
| 1042 | VMX_SEGMENT_FIELD(CS), |
| 1043 | VMX_SEGMENT_FIELD(DS), |
| 1044 | VMX_SEGMENT_FIELD(ES), |
| 1045 | VMX_SEGMENT_FIELD(FS), |
| 1046 | VMX_SEGMENT_FIELD(GS), |
| 1047 | VMX_SEGMENT_FIELD(SS), |
| 1048 | VMX_SEGMENT_FIELD(TR), |
| 1049 | VMX_SEGMENT_FIELD(LDTR), |
| 1050 | }; |
| 1051 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1052 | static u64 host_efer; |
| 1053 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1054 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 1055 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1056 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1057 | * 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] | 1058 | * away by decrementing the array size. |
| 1059 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1060 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1061 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1062 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1063 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1064 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1065 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1066 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1067 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1068 | { |
| 1069 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1070 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1071 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
| 1072 | } |
| 1073 | |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 1074 | static inline bool is_debug(u32 intr_info) |
| 1075 | { |
| 1076 | return is_exception_n(intr_info, DB_VECTOR); |
| 1077 | } |
| 1078 | |
| 1079 | static inline bool is_breakpoint(u32 intr_info) |
| 1080 | { |
| 1081 | return is_exception_n(intr_info, BP_VECTOR); |
| 1082 | } |
| 1083 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1084 | static inline bool is_page_fault(u32 intr_info) |
| 1085 | { |
| 1086 | return is_exception_n(intr_info, PF_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1087 | } |
| 1088 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1089 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1090 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1091 | return is_exception_n(intr_info, NM_VECTOR); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1092 | } |
| 1093 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1094 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1095 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1096 | return is_exception_n(intr_info, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1097 | } |
| 1098 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1099 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1100 | { |
| 1101 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1102 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 1103 | } |
| 1104 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1105 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 1106 | { |
| 1107 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1108 | INTR_INFO_VALID_MASK)) == |
| 1109 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 1110 | } |
| 1111 | |
Linus Torvalds | 587da2b | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 1112 | /* Undocumented: icebp/int1 */ |
| 1113 | static inline bool is_icebp(u32 intr_info) |
| 1114 | { |
| 1115 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1116 | == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK); |
| 1117 | } |
| 1118 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1119 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1120 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1121 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1124 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1125 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1126 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1127 | } |
| 1128 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1129 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1130 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1131 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1132 | } |
| 1133 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1134 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1135 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1136 | return vmcs_config.cpu_based_exec_ctrl & |
| 1137 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1140 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1141 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1142 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1143 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1144 | } |
| 1145 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1146 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 1147 | { |
| 1148 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1149 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 1150 | } |
| 1151 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1152 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 1153 | { |
| 1154 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1155 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1156 | } |
| 1157 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1158 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 1159 | { |
| 1160 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1161 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1162 | } |
| 1163 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1164 | /* |
| 1165 | * Comment's format: document - errata name - stepping - processor name. |
| 1166 | * Refer from |
| 1167 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 1168 | */ |
| 1169 | static u32 vmx_preemption_cpu_tfms[] = { |
| 1170 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 1171 | 0x000206E6, |
| 1172 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 1173 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 1174 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1175 | 0x00020652, |
| 1176 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1177 | 0x00020655, |
| 1178 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 1179 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 1180 | /* |
| 1181 | * 320767.pdf - AAP86 - B1 - |
| 1182 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 1183 | */ |
| 1184 | 0x000106E5, |
| 1185 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 1186 | 0x000106A0, |
| 1187 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 1188 | 0x000106A1, |
| 1189 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 1190 | 0x000106A4, |
| 1191 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 1192 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 1193 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 1194 | 0x000106A5, |
| 1195 | }; |
| 1196 | |
| 1197 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 1198 | { |
| 1199 | u32 eax = cpuid_eax(0x00000001), i; |
| 1200 | |
| 1201 | /* Clear the reserved bits */ |
| 1202 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 1203 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1204 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 1205 | return true; |
| 1206 | |
| 1207 | return false; |
| 1208 | } |
| 1209 | |
| 1210 | static inline bool cpu_has_vmx_preemption_timer(void) |
| 1211 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1212 | return vmcs_config.pin_based_exec_ctrl & |
| 1213 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1214 | } |
| 1215 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1216 | static inline bool cpu_has_vmx_posted_intr(void) |
| 1217 | { |
Paolo Bonzini | d6a858d | 2015-09-28 11:58:14 +0200 | [diff] [blame] | 1218 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
| 1219 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | static inline bool cpu_has_vmx_apicv(void) |
| 1223 | { |
| 1224 | return cpu_has_vmx_apic_register_virt() && |
| 1225 | cpu_has_vmx_virtual_intr_delivery() && |
| 1226 | cpu_has_vmx_posted_intr(); |
| 1227 | } |
| 1228 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1229 | static inline bool cpu_has_vmx_flexpriority(void) |
| 1230 | { |
| 1231 | return cpu_has_vmx_tpr_shadow() && |
| 1232 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1233 | } |
| 1234 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1235 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 1236 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1237 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1238 | } |
| 1239 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1240 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 1241 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1242 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1243 | } |
| 1244 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1245 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 1246 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1247 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 1250 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 1251 | { |
| 1252 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 1253 | } |
| 1254 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 1255 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 1256 | { |
| 1257 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 1258 | } |
| 1259 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1260 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1261 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1262 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1263 | } |
| 1264 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1265 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1266 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1267 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1268 | } |
| 1269 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1270 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 1271 | { |
| 1272 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 1273 | } |
| 1274 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1275 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 1276 | { |
| 1277 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 1278 | } |
| 1279 | |
Wanpeng Li | 2df1969 | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 1280 | static inline bool cpu_has_vmx_invvpid(void) |
| 1281 | { |
| 1282 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; |
| 1283 | } |
| 1284 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1285 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1286 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1287 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1288 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1289 | } |
| 1290 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1291 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1292 | { |
| 1293 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1294 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 1295 | } |
| 1296 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1297 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1298 | { |
| 1299 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1300 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1301 | } |
| 1302 | |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1303 | static inline bool cpu_has_vmx_basic_inout(void) |
| 1304 | { |
| 1305 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); |
| 1306 | } |
| 1307 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1308 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1309 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1310 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1311 | } |
| 1312 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1313 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1314 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1315 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1316 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1317 | } |
| 1318 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1319 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1320 | { |
| 1321 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1322 | SECONDARY_EXEC_RDTSCP; |
| 1323 | } |
| 1324 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1325 | static inline bool cpu_has_vmx_invpcid(void) |
| 1326 | { |
| 1327 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1328 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 1329 | } |
| 1330 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1331 | static inline bool cpu_has_virtual_nmis(void) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 1332 | { |
| 1333 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 1334 | } |
| 1335 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1336 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 1337 | { |
| 1338 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1339 | SECONDARY_EXEC_WBINVD_EXITING; |
| 1340 | } |
| 1341 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1342 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
| 1343 | { |
| 1344 | u64 vmx_msr; |
| 1345 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 1346 | /* check if the cpu supports writing r/o exit information fields */ |
| 1347 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) |
| 1348 | return false; |
| 1349 | |
| 1350 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1351 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1352 | } |
| 1353 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1354 | static inline bool cpu_has_vmx_pml(void) |
| 1355 | { |
| 1356 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; |
| 1357 | } |
| 1358 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 1359 | static inline bool cpu_has_vmx_tsc_scaling(void) |
| 1360 | { |
| 1361 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1362 | SECONDARY_EXEC_TSC_SCALING; |
| 1363 | } |
| 1364 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1365 | static inline bool report_flexpriority(void) |
| 1366 | { |
| 1367 | return flexpriority_enabled; |
| 1368 | } |
| 1369 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1370 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 1371 | { |
| 1372 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 1373 | } |
| 1374 | |
| 1375 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 1376 | { |
| 1377 | return (vmcs12->cpu_based_vm_exec_control & |
| 1378 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 1379 | (vmcs12->secondary_vm_exec_control & bit); |
| 1380 | } |
| 1381 | |
Nadav Har'El | f5c4368 | 2013-08-05 11:07:20 +0300 | [diff] [blame] | 1382 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1383 | { |
| 1384 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 1385 | } |
| 1386 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 1387 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
| 1388 | { |
| 1389 | return vmcs12->pin_based_vm_exec_control & |
| 1390 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1391 | } |
| 1392 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 1393 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
| 1394 | { |
| 1395 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); |
| 1396 | } |
| 1397 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1398 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
| 1399 | { |
| 1400 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) && |
| 1401 | vmx_xsaves_supported(); |
| 1402 | } |
| 1403 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 1404 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
| 1405 | { |
| 1406 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
| 1407 | } |
| 1408 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 1409 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
| 1410 | { |
| 1411 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); |
| 1412 | } |
| 1413 | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 1414 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
| 1415 | { |
| 1416 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); |
| 1417 | } |
| 1418 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 1419 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
| 1420 | { |
| 1421 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 1422 | } |
| 1423 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 1424 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
| 1425 | { |
| 1426 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; |
| 1427 | } |
| 1428 | |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1429 | static inline bool is_nmi(u32 intr_info) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1430 | { |
| 1431 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1432 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1433 | } |
| 1434 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 1435 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 1436 | u32 exit_intr_info, |
| 1437 | unsigned long exit_qualification); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 1438 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 1439 | struct vmcs12 *vmcs12, |
| 1440 | u32 reason, unsigned long qualification); |
| 1441 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1442 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1443 | { |
| 1444 | int i; |
| 1445 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1446 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1447 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1448 | return i; |
| 1449 | return -1; |
| 1450 | } |
| 1451 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1452 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 1453 | { |
| 1454 | struct { |
| 1455 | u64 vpid : 16; |
| 1456 | u64 rsvd : 48; |
| 1457 | u64 gva; |
| 1458 | } operand = { vpid, 0, gva }; |
| 1459 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1460 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1461 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1462 | "; ja 1f ; ud2 ; 1:" |
| 1463 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 1464 | } |
| 1465 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1466 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 1467 | { |
| 1468 | struct { |
| 1469 | u64 eptp, gpa; |
| 1470 | } operand = {eptp, gpa}; |
| 1471 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1472 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1473 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1474 | "; ja 1f ; ud2 ; 1:\n" |
| 1475 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 1476 | } |
| 1477 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1478 | 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] | 1479 | { |
| 1480 | int i; |
| 1481 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1482 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1483 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1484 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1485 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1486 | } |
| 1487 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1488 | static void vmcs_clear(struct vmcs *vmcs) |
| 1489 | { |
| 1490 | u64 phys_addr = __pa(vmcs); |
| 1491 | u8 error; |
| 1492 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1493 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1494 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1495 | : "cc", "memory"); |
| 1496 | if (error) |
| 1497 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 1498 | vmcs, phys_addr); |
| 1499 | } |
| 1500 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1501 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 1502 | { |
| 1503 | vmcs_clear(loaded_vmcs->vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 1504 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 1505 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1506 | loaded_vmcs->cpu = -1; |
| 1507 | loaded_vmcs->launched = 0; |
| 1508 | } |
| 1509 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1510 | static void vmcs_load(struct vmcs *vmcs) |
| 1511 | { |
| 1512 | u64 phys_addr = __pa(vmcs); |
| 1513 | u8 error; |
| 1514 | |
| 1515 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1516 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1517 | : "cc", "memory"); |
| 1518 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 1519 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1520 | vmcs, phys_addr); |
| 1521 | } |
| 1522 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1523 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1524 | /* |
| 1525 | * This bitmap is used to indicate whether the vmclear |
| 1526 | * operation is enabled on all cpus. All disabled by |
| 1527 | * default. |
| 1528 | */ |
| 1529 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 1530 | |
| 1531 | static inline void crash_enable_local_vmclear(int cpu) |
| 1532 | { |
| 1533 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1534 | } |
| 1535 | |
| 1536 | static inline void crash_disable_local_vmclear(int cpu) |
| 1537 | { |
| 1538 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1539 | } |
| 1540 | |
| 1541 | static inline int crash_local_vmclear_enabled(int cpu) |
| 1542 | { |
| 1543 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1544 | } |
| 1545 | |
| 1546 | static void crash_vmclear_local_loaded_vmcss(void) |
| 1547 | { |
| 1548 | int cpu = raw_smp_processor_id(); |
| 1549 | struct loaded_vmcs *v; |
| 1550 | |
| 1551 | if (!crash_local_vmclear_enabled(cpu)) |
| 1552 | return; |
| 1553 | |
| 1554 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 1555 | loaded_vmcss_on_cpu_link) |
| 1556 | vmcs_clear(v->vmcs); |
| 1557 | } |
| 1558 | #else |
| 1559 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 1560 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1561 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1562 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1563 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1564 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1565 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1566 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1567 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1568 | if (loaded_vmcs->cpu != cpu) |
| 1569 | return; /* vcpu migration can race with cpu offline */ |
| 1570 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1571 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1572 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1573 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1574 | |
| 1575 | /* |
| 1576 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 1577 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 1578 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 1579 | * then adds the vmcs into percpu list before it is deleted. |
| 1580 | */ |
| 1581 | smp_wmb(); |
| 1582 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1583 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1584 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1587 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1588 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 1589 | int cpu = loaded_vmcs->cpu; |
| 1590 | |
| 1591 | if (cpu != -1) |
| 1592 | smp_call_function_single(cpu, |
| 1593 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1594 | } |
| 1595 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1596 | static inline void vpid_sync_vcpu_single(int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1597 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1598 | if (vpid == 0) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1599 | return; |
| 1600 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1601 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1602 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1603 | } |
| 1604 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1605 | static inline void vpid_sync_vcpu_global(void) |
| 1606 | { |
| 1607 | if (cpu_has_vmx_invvpid_global()) |
| 1608 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 1609 | } |
| 1610 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1611 | static inline void vpid_sync_context(int vpid) |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1612 | { |
| 1613 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1614 | vpid_sync_vcpu_single(vpid); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1615 | else |
| 1616 | vpid_sync_vcpu_global(); |
| 1617 | } |
| 1618 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1619 | static inline void ept_sync_global(void) |
| 1620 | { |
| 1621 | if (cpu_has_vmx_invept_global()) |
| 1622 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
| 1623 | } |
| 1624 | |
| 1625 | static inline void ept_sync_context(u64 eptp) |
| 1626 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1627 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1628 | if (cpu_has_vmx_invept_context()) |
| 1629 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 1630 | else |
| 1631 | ept_sync_global(); |
| 1632 | } |
| 1633 | } |
| 1634 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1635 | static __always_inline void vmcs_check16(unsigned long field) |
| 1636 | { |
| 1637 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 1638 | "16-bit accessor invalid for 64-bit field"); |
| 1639 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1640 | "16-bit accessor invalid for 64-bit high field"); |
| 1641 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1642 | "16-bit accessor invalid for 32-bit high field"); |
| 1643 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1644 | "16-bit accessor invalid for natural width field"); |
| 1645 | } |
| 1646 | |
| 1647 | static __always_inline void vmcs_check32(unsigned long field) |
| 1648 | { |
| 1649 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1650 | "32-bit accessor invalid for 16-bit field"); |
| 1651 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1652 | "32-bit accessor invalid for natural width field"); |
| 1653 | } |
| 1654 | |
| 1655 | static __always_inline void vmcs_check64(unsigned long field) |
| 1656 | { |
| 1657 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1658 | "64-bit accessor invalid for 16-bit field"); |
| 1659 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1660 | "64-bit accessor invalid for 64-bit high field"); |
| 1661 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1662 | "64-bit accessor invalid for 32-bit field"); |
| 1663 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1664 | "64-bit accessor invalid for natural width field"); |
| 1665 | } |
| 1666 | |
| 1667 | static __always_inline void vmcs_checkl(unsigned long field) |
| 1668 | { |
| 1669 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1670 | "Natural width accessor invalid for 16-bit field"); |
| 1671 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 1672 | "Natural width accessor invalid for 64-bit field"); |
| 1673 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1674 | "Natural width accessor invalid for 64-bit high field"); |
| 1675 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1676 | "Natural width accessor invalid for 32-bit field"); |
| 1677 | } |
| 1678 | |
| 1679 | static __always_inline unsigned long __vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1680 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1681 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1682 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1683 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 1684 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1685 | return value; |
| 1686 | } |
| 1687 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1688 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1689 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1690 | vmcs_check16(field); |
| 1691 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1692 | } |
| 1693 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1694 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1695 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1696 | vmcs_check32(field); |
| 1697 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1698 | } |
| 1699 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1700 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1701 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1702 | vmcs_check64(field); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1703 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1704 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1705 | #else |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1706 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1707 | #endif |
| 1708 | } |
| 1709 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1710 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
| 1711 | { |
| 1712 | vmcs_checkl(field); |
| 1713 | return __vmcs_readl(field); |
| 1714 | } |
| 1715 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1716 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 1717 | { |
| 1718 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 1719 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 1720 | dump_stack(); |
| 1721 | } |
| 1722 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1723 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1724 | { |
| 1725 | u8 error; |
| 1726 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1727 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1728 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1729 | if (unlikely(error)) |
| 1730 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1731 | } |
| 1732 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1733 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1734 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1735 | vmcs_check16(field); |
| 1736 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1737 | } |
| 1738 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1739 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1740 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1741 | vmcs_check32(field); |
| 1742 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1743 | } |
| 1744 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1745 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1746 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1747 | vmcs_check64(field); |
| 1748 | __vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 1749 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1750 | asm volatile (""); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1751 | __vmcs_writel(field+1, value >> 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1752 | #endif |
| 1753 | } |
| 1754 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1755 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1756 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1757 | vmcs_checkl(field); |
| 1758 | __vmcs_writel(field, value); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1759 | } |
| 1760 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1761 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1762 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1763 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 1764 | "vmcs_clear_bits does not support 64-bit fields"); |
| 1765 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); |
| 1766 | } |
| 1767 | |
| 1768 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
| 1769 | { |
| 1770 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 1771 | "vmcs_set_bits does not support 64-bit fields"); |
| 1772 | __vmcs_writel(field, __vmcs_readl(field) | mask); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1773 | } |
| 1774 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 1775 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 1776 | { |
| 1777 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); |
| 1778 | } |
| 1779 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1780 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1781 | { |
| 1782 | vmcs_write32(VM_ENTRY_CONTROLS, val); |
| 1783 | vmx->vm_entry_controls_shadow = val; |
| 1784 | } |
| 1785 | |
| 1786 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1787 | { |
| 1788 | if (vmx->vm_entry_controls_shadow != val) |
| 1789 | vm_entry_controls_init(vmx, val); |
| 1790 | } |
| 1791 | |
| 1792 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) |
| 1793 | { |
| 1794 | return vmx->vm_entry_controls_shadow; |
| 1795 | } |
| 1796 | |
| 1797 | |
| 1798 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 1799 | { |
| 1800 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); |
| 1801 | } |
| 1802 | |
| 1803 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 1804 | { |
| 1805 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); |
| 1806 | } |
| 1807 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 1808 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 1809 | { |
| 1810 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); |
| 1811 | } |
| 1812 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1813 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1814 | { |
| 1815 | vmcs_write32(VM_EXIT_CONTROLS, val); |
| 1816 | vmx->vm_exit_controls_shadow = val; |
| 1817 | } |
| 1818 | |
| 1819 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1820 | { |
| 1821 | if (vmx->vm_exit_controls_shadow != val) |
| 1822 | vm_exit_controls_init(vmx, val); |
| 1823 | } |
| 1824 | |
| 1825 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) |
| 1826 | { |
| 1827 | return vmx->vm_exit_controls_shadow; |
| 1828 | } |
| 1829 | |
| 1830 | |
| 1831 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 1832 | { |
| 1833 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); |
| 1834 | } |
| 1835 | |
| 1836 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 1837 | { |
| 1838 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); |
| 1839 | } |
| 1840 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1841 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 1842 | { |
| 1843 | vmx->segment_cache.bitmask = 0; |
| 1844 | } |
| 1845 | |
| 1846 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 1847 | unsigned field) |
| 1848 | { |
| 1849 | bool ret; |
| 1850 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 1851 | |
| 1852 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 1853 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 1854 | vmx->segment_cache.bitmask = 0; |
| 1855 | } |
| 1856 | ret = vmx->segment_cache.bitmask & mask; |
| 1857 | vmx->segment_cache.bitmask |= mask; |
| 1858 | return ret; |
| 1859 | } |
| 1860 | |
| 1861 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 1862 | { |
| 1863 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 1864 | |
| 1865 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 1866 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 1867 | return *p; |
| 1868 | } |
| 1869 | |
| 1870 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 1871 | { |
| 1872 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 1873 | |
| 1874 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 1875 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 1876 | return *p; |
| 1877 | } |
| 1878 | |
| 1879 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 1880 | { |
| 1881 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 1882 | |
| 1883 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 1884 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 1885 | return *p; |
| 1886 | } |
| 1887 | |
| 1888 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 1889 | { |
| 1890 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 1891 | |
| 1892 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 1893 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 1894 | return *p; |
| 1895 | } |
| 1896 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1897 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 1898 | { |
| 1899 | u32 eb; |
| 1900 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1901 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 1902 | (1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1903 | if ((vcpu->guest_debug & |
| 1904 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 1905 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 1906 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1907 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1908 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1909 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1910 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 1911 | if (vcpu->fpu_active) |
| 1912 | eb &= ~(1u << NM_VECTOR); |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1913 | |
| 1914 | /* When we are running a nested L2 guest and L1 specified for it a |
| 1915 | * certain exception bitmap, we must trap the same exceptions and pass |
| 1916 | * them to L1. When running L2, we will only handle the exceptions |
| 1917 | * specified above if L1 did not want them. |
| 1918 | */ |
| 1919 | if (is_guest_mode(vcpu)) |
| 1920 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 1921 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1922 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 1923 | } |
| 1924 | |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1925 | /* |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 1926 | * Check if MSR is intercepted for currently loaded MSR bitmap. |
| 1927 | */ |
| 1928 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) |
| 1929 | { |
| 1930 | unsigned long *msr_bitmap; |
| 1931 | int f = sizeof(unsigned long); |
| 1932 | |
| 1933 | if (!cpu_has_vmx_msr_bitmap()) |
| 1934 | return true; |
| 1935 | |
| 1936 | msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; |
| 1937 | |
| 1938 | if (msr <= 0x1fff) { |
| 1939 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 1940 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 1941 | msr &= 0x1fff; |
| 1942 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 1943 | } |
| 1944 | |
| 1945 | return true; |
| 1946 | } |
| 1947 | |
| 1948 | /* |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1949 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 1950 | */ |
| 1951 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 1952 | { |
| 1953 | unsigned long *msr_bitmap; |
| 1954 | int f = sizeof(unsigned long); |
| 1955 | |
| 1956 | if (!cpu_has_vmx_msr_bitmap()) |
| 1957 | return true; |
| 1958 | |
| 1959 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 1960 | |
| 1961 | if (msr <= 0x1fff) { |
| 1962 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 1963 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 1964 | msr &= 0x1fff; |
| 1965 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 1966 | } |
| 1967 | |
| 1968 | return true; |
| 1969 | } |
| 1970 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1971 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 1972 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1973 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1974 | vm_entry_controls_clearbit(vmx, entry); |
| 1975 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1976 | } |
| 1977 | |
Konrad Rzeszutek Wilk | 1555f9e | 2018-06-20 20:11:39 -0400 | [diff] [blame^] | 1978 | static int find_msr(struct vmx_msrs *m, unsigned int msr) |
| 1979 | { |
| 1980 | unsigned int i; |
| 1981 | |
| 1982 | for (i = 0; i < m->nr; ++i) { |
| 1983 | if (m->val[i].index == msr) |
| 1984 | return i; |
| 1985 | } |
| 1986 | return -ENOENT; |
| 1987 | } |
| 1988 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1989 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 1990 | { |
Konrad Rzeszutek Wilk | 1555f9e | 2018-06-20 20:11:39 -0400 | [diff] [blame^] | 1991 | int i; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1992 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1993 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1994 | switch (msr) { |
| 1995 | case MSR_EFER: |
| 1996 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1997 | clear_atomic_switch_msr_special(vmx, |
| 1998 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1999 | VM_EXIT_LOAD_IA32_EFER); |
| 2000 | return; |
| 2001 | } |
| 2002 | break; |
| 2003 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2004 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2005 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2006 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2007 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 2008 | return; |
| 2009 | } |
| 2010 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2011 | } |
Konrad Rzeszutek Wilk | 1555f9e | 2018-06-20 20:11:39 -0400 | [diff] [blame^] | 2012 | i = find_msr(&m->guest, msr); |
| 2013 | if (i < 0) |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2014 | return; |
Konrad Rzeszutek Wilk | 57e3ada | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2015 | --m->guest.nr; |
| 2016 | --m->host.nr; |
| 2017 | m->guest.val[i] = m->guest.val[m->guest.nr]; |
| 2018 | m->host.val[i] = m->host.val[m->host.nr]; |
| 2019 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); |
| 2020 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2021 | } |
| 2022 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2023 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2024 | unsigned long entry, unsigned long exit, |
| 2025 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 2026 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2027 | { |
| 2028 | vmcs_write64(guest_val_vmcs, guest_val); |
| 2029 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2030 | vm_entry_controls_setbit(vmx, entry); |
| 2031 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2032 | } |
| 2033 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2034 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 2035 | u64 guest_val, u64 host_val) |
| 2036 | { |
Konrad Rzeszutek Wilk | 1555f9e | 2018-06-20 20:11:39 -0400 | [diff] [blame^] | 2037 | int i; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2038 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2039 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2040 | switch (msr) { |
| 2041 | case MSR_EFER: |
| 2042 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2043 | add_atomic_switch_msr_special(vmx, |
| 2044 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2045 | VM_EXIT_LOAD_IA32_EFER, |
| 2046 | GUEST_IA32_EFER, |
| 2047 | HOST_IA32_EFER, |
| 2048 | guest_val, host_val); |
| 2049 | return; |
| 2050 | } |
| 2051 | break; |
| 2052 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2053 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2054 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2055 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2056 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2057 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 2058 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 2059 | guest_val, host_val); |
| 2060 | return; |
| 2061 | } |
| 2062 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 2063 | case MSR_IA32_PEBS_ENABLE: |
| 2064 | /* PEBS needs a quiescent period after being disabled (to write |
| 2065 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 2066 | * provide that period, so a CPU could write host's record into |
| 2067 | * guest's memory. |
| 2068 | */ |
| 2069 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2070 | } |
| 2071 | |
Konrad Rzeszutek Wilk | 1555f9e | 2018-06-20 20:11:39 -0400 | [diff] [blame^] | 2072 | i = find_msr(&m->guest, msr); |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2073 | if (i == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 2074 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2075 | "Can't add msr %x\n", msr); |
| 2076 | return; |
Konrad Rzeszutek Wilk | 1555f9e | 2018-06-20 20:11:39 -0400 | [diff] [blame^] | 2077 | } else if (i < 0) { |
| 2078 | i = m->guest.nr++; |
Konrad Rzeszutek Wilk | 57e3ada | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2079 | ++m->host.nr; |
| 2080 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); |
| 2081 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2082 | } |
| 2083 | |
Konrad Rzeszutek Wilk | 57e3ada | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2084 | m->guest.val[i].index = msr; |
| 2085 | m->guest.val[i].value = guest_val; |
| 2086 | m->host.val[i].index = msr; |
| 2087 | m->host.val[i].value = host_val; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2088 | } |
| 2089 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2090 | static void reload_tss(void) |
| 2091 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2092 | /* |
| 2093 | * VT restores TR but not its size. Useless. |
| 2094 | */ |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 2095 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 2096 | struct desc_struct *descs; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2097 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 2098 | descs = (void *)gdt->address; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2099 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 2100 | load_TR_desc(); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2101 | } |
| 2102 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2103 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2104 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2105 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 2106 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2107 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2108 | if (!enable_ept) { |
| 2109 | /* |
| 2110 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 2111 | * host CPUID is more efficient than testing guest CPUID |
| 2112 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 2113 | */ |
| 2114 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 2115 | guest_efer |= EFER_NX; |
| 2116 | else if (!(guest_efer & EFER_NX)) |
| 2117 | ignore_bits |= EFER_NX; |
| 2118 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 2119 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2120 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2121 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2122 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2123 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2124 | #ifdef CONFIG_X86_64 |
| 2125 | ignore_bits |= EFER_LMA | EFER_LME; |
| 2126 | /* SCE is meaningful only in long mode on Intel */ |
| 2127 | if (guest_efer & EFER_LMA) |
| 2128 | ignore_bits &= ~(u64)EFER_SCE; |
| 2129 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2130 | |
| 2131 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 2132 | |
| 2133 | /* |
| 2134 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 2135 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 2136 | * atomically, since it's faster than switching it manually. |
| 2137 | */ |
| 2138 | if (cpu_has_load_ia32_efer || |
| 2139 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2140 | if (!(guest_efer & EFER_LMA)) |
| 2141 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 2142 | if (guest_efer != host_efer) |
| 2143 | add_atomic_switch_msr(vmx, MSR_EFER, |
| 2144 | guest_efer, host_efer); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2145 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2146 | } else { |
| 2147 | guest_efer &= ~ignore_bits; |
| 2148 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2149 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2150 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 2151 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 2152 | |
| 2153 | return true; |
| 2154 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2155 | } |
| 2156 | |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2157 | static unsigned long segment_base(u16 selector) |
| 2158 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 2159 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2160 | struct desc_struct *d; |
| 2161 | unsigned long table_base; |
| 2162 | unsigned long v; |
| 2163 | |
| 2164 | if (!(selector & ~3)) |
| 2165 | return 0; |
| 2166 | |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 2167 | table_base = gdt->address; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2168 | |
| 2169 | if (selector & 4) { /* from ldt */ |
| 2170 | u16 ldt_selector = kvm_read_ldt(); |
| 2171 | |
| 2172 | if (!(ldt_selector & ~3)) |
| 2173 | return 0; |
| 2174 | |
| 2175 | table_base = segment_base(ldt_selector); |
| 2176 | } |
| 2177 | d = (struct desc_struct *)(table_base + (selector & ~7)); |
| 2178 | v = get_desc_base(d); |
| 2179 | #ifdef CONFIG_X86_64 |
| 2180 | if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11)) |
| 2181 | v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32; |
| 2182 | #endif |
| 2183 | return v; |
| 2184 | } |
| 2185 | |
| 2186 | static inline unsigned long kvm_read_tr_base(void) |
| 2187 | { |
| 2188 | u16 tr; |
| 2189 | asm("str %0" : "=g"(tr)); |
| 2190 | return segment_base(tr); |
| 2191 | } |
| 2192 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2193 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2194 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2195 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2196 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2197 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2198 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2199 | return; |
| 2200 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2201 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2202 | /* |
| 2203 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 2204 | * allow segment selectors with cpl > 0 or ti == 1. |
| 2205 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2206 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2207 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2208 | savesegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2209 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2210 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2211 | vmx->host_state.fs_reload_needed = 0; |
| 2212 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2213 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2214 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2215 | } |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2216 | savesegment(gs, vmx->host_state.gs_sel); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2217 | if (!(vmx->host_state.gs_sel & 7)) |
| 2218 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2219 | else { |
| 2220 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2221 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | #ifdef CONFIG_X86_64 |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2225 | savesegment(ds, vmx->host_state.ds_sel); |
| 2226 | savesegment(es, vmx->host_state.es_sel); |
| 2227 | #endif |
| 2228 | |
| 2229 | #ifdef CONFIG_X86_64 |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2230 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 2231 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 2232 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2233 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 2234 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2235 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2236 | |
| 2237 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2238 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
| 2239 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2240 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2241 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2242 | if (boot_cpu_has(X86_FEATURE_MPX)) |
| 2243 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2244 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 2245 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 2246 | vmx->guest_msrs[i].data, |
| 2247 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2248 | } |
| 2249 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2250 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2251 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2252 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2253 | return; |
| 2254 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2255 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2256 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2257 | #ifdef CONFIG_X86_64 |
| 2258 | if (is_long_mode(&vmx->vcpu)) |
| 2259 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 2260 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2261 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2262 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2263 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2264 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2265 | #else |
| 2266 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2267 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2268 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 2269 | if (vmx->host_state.fs_reload_needed) |
| 2270 | loadsegment(fs, vmx->host_state.fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2271 | #ifdef CONFIG_X86_64 |
| 2272 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { |
| 2273 | loadsegment(ds, vmx->host_state.ds_sel); |
| 2274 | loadsegment(es, vmx->host_state.es_sel); |
| 2275 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2276 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2277 | reload_tss(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2278 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2279 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2280 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2281 | if (vmx->host_state.msr_host_bndcfgs) |
| 2282 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 2283 | /* |
| 2284 | * If the FPU is not active (through the host task or |
| 2285 | * the guest vcpu), then restore the cr0.TS bit. |
| 2286 | */ |
Ingo Molnar | 3c6dffa | 2015-04-28 12:28:08 +0200 | [diff] [blame] | 2287 | if (!fpregs_active() && !vmx->vcpu.guest_fpu_loaded) |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 2288 | stts(); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 2289 | load_gdt(this_cpu_ptr(&host_gdt)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2290 | } |
| 2291 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2292 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 2293 | { |
| 2294 | preempt_disable(); |
| 2295 | __vmx_load_host_state(vmx); |
| 2296 | preempt_enable(); |
| 2297 | } |
| 2298 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2299 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 2300 | { |
| 2301 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2302 | struct pi_desc old, new; |
| 2303 | unsigned int dest; |
| 2304 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2305 | /* |
| 2306 | * In case of hot-plug or hot-unplug, we may have to undo |
| 2307 | * vmx_vcpu_pi_put even if there is no assigned device. And we |
| 2308 | * always keep PI.NDST up to date for simplicity: it makes the |
| 2309 | * code easier, and CPU migration is not a fast path. |
| 2310 | */ |
| 2311 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2312 | return; |
| 2313 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2314 | /* |
| 2315 | * First handle the simple case where no cmpxchg is necessary; just |
| 2316 | * allow posting non-urgent interrupts. |
| 2317 | * |
| 2318 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change |
| 2319 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler |
| 2320 | * expects the VCPU to be on the blocked_vcpu_list that matches |
| 2321 | * PI.NDST. |
| 2322 | */ |
| 2323 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || |
| 2324 | vcpu->cpu == cpu) { |
| 2325 | pi_clear_sn(pi_desc); |
| 2326 | return; |
| 2327 | } |
| 2328 | |
| 2329 | /* The full case. */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2330 | do { |
| 2331 | old.control = new.control = pi_desc->control; |
| 2332 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2333 | dest = cpu_physical_id(cpu); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2334 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2335 | if (x2apic_enabled()) |
| 2336 | new.ndst = dest; |
| 2337 | else |
| 2338 | new.ndst = (dest << 8) & 0xFF00; |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2339 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2340 | new.sn = 0; |
Paolo Bonzini | ea37f61 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 2341 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 2342 | new.control) != old.control); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2343 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2344 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2345 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
| 2346 | { |
| 2347 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; |
| 2348 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); |
| 2349 | } |
| 2350 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2351 | /* |
| 2352 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 2353 | * vcpu mutex is already taken. |
| 2354 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2355 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2356 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2357 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2358 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2359 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2360 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2361 | if (!vmm_exclusive) |
| 2362 | kvm_cpu_vmxon(phys_addr); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2363 | else if (!already_loaded) |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2364 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2365 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2366 | if (!already_loaded) { |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2367 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2368 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2369 | |
| 2370 | /* |
| 2371 | * Read loaded_vmcs->cpu should be before fetching |
| 2372 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 2373 | * See the comments in __loaded_vmcs_clear(). |
| 2374 | */ |
| 2375 | smp_rmb(); |
| 2376 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2377 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 2378 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2379 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2380 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 2384 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 2385 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2386 | indirect_branch_prediction_barrier(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | if (!already_loaded) { |
| 2390 | struct desc_ptr *gdt = this_cpu_ptr(&host_gdt); |
| 2391 | unsigned long sysenter_esp; |
| 2392 | |
| 2393 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2394 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2395 | /* |
| 2396 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 2397 | * processors. |
| 2398 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2399 | vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */ |
Avi Kivity | d359192 | 2010-07-26 18:32:39 +0300 | [diff] [blame] | 2400 | vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2401 | |
| 2402 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 2403 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 2404 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2405 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2406 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2407 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2408 | /* Setup TSC multiplier */ |
| 2409 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2410 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 2411 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2412 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2413 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2414 | vmx->host_pkru = read_pkru(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2415 | } |
| 2416 | |
| 2417 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 2418 | { |
| 2419 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2420 | |
| 2421 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 2422 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 2423 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2424 | return; |
| 2425 | |
| 2426 | /* Set SN when the vCPU is preempted */ |
| 2427 | if (vcpu->preempted) |
| 2428 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 2432 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2433 | vmx_vcpu_pi_put(vcpu); |
| 2434 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2435 | __vmx_load_host_state(to_vmx(vcpu)); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2436 | if (!vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2437 | __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs); |
| 2438 | vcpu->cpu = -1; |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 2439 | kvm_cpu_vmxoff(); |
| 2440 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2441 | } |
| 2442 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2443 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 2444 | { |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2445 | ulong cr0; |
| 2446 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2447 | if (vcpu->fpu_active) |
| 2448 | return; |
| 2449 | vcpu->fpu_active = 1; |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2450 | cr0 = vmcs_readl(GUEST_CR0); |
| 2451 | cr0 &= ~(X86_CR0_TS | X86_CR0_MP); |
| 2452 | cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP); |
| 2453 | vmcs_writel(GUEST_CR0, cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2454 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2455 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2456 | if (is_guest_mode(vcpu)) |
| 2457 | vcpu->arch.cr0_guest_owned_bits &= |
| 2458 | ~get_vmcs12(vcpu)->cr0_guest_host_mask; |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2459 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2460 | } |
| 2461 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2462 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 2463 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 2464 | /* |
| 2465 | * Return the cr0 value that a nested guest would read. This is a combination |
| 2466 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 2467 | * its hypervisor (cr0_read_shadow). |
| 2468 | */ |
| 2469 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 2470 | { |
| 2471 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 2472 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 2473 | } |
| 2474 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 2475 | { |
| 2476 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 2477 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 2478 | } |
| 2479 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2480 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 2481 | { |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2482 | /* Note that there is no vcpu->fpu_active = 0 here. The caller must |
| 2483 | * set this *before* calling this function. |
| 2484 | */ |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2485 | vmx_decache_cr0_guest_bits(vcpu); |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 2486 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2487 | update_exception_bitmap(vcpu); |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2488 | vcpu->arch.cr0_guest_owned_bits = 0; |
| 2489 | 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] | 2490 | if (is_guest_mode(vcpu)) { |
| 2491 | /* |
| 2492 | * L1's specified read shadow might not contain the TS bit, |
| 2493 | * so now that we turned on shadowing of this bit, we need to |
| 2494 | * set this bit of the shadow. Like in nested_vmx_run we need |
| 2495 | * nested_read_cr0(vmcs12), but vmcs12->guest_cr0 is not yet |
| 2496 | * up-to-date here because we just decached cr0.TS (and we'll |
| 2497 | * only update vmcs12->guest_cr0 on nested exit). |
| 2498 | */ |
| 2499 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2500 | vmcs12->guest_cr0 = (vmcs12->guest_cr0 & ~X86_CR0_TS) | |
| 2501 | (vcpu->arch.cr0 & X86_CR0_TS); |
| 2502 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 2503 | } else |
| 2504 | vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2505 | } |
| 2506 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2507 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 2508 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2509 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 2510 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2511 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 2512 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2513 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 2514 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2515 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2516 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 2517 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2518 | } |
| 2519 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2520 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2521 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2525 | { |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2526 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2527 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2528 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2529 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2530 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2531 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2532 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 2533 | } |
| 2534 | |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 2535 | static u32 vmx_get_pkru(struct kvm_vcpu *vcpu) |
| 2536 | { |
| 2537 | return to_vmx(vcpu)->guest_pkru; |
| 2538 | } |
| 2539 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2540 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2541 | { |
| 2542 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2543 | int ret = 0; |
| 2544 | |
| 2545 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2546 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2547 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2548 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2549 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2550 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 2554 | { |
| 2555 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2556 | u32 interruptibility = interruptibility_old; |
| 2557 | |
| 2558 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 2559 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2560 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2561 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2562 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2563 | interruptibility |= GUEST_INTR_STATE_STI; |
| 2564 | |
| 2565 | if ((interruptibility != interruptibility_old)) |
| 2566 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 2567 | } |
| 2568 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2569 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 2570 | { |
| 2571 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2572 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2573 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2574 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2575 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2576 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2577 | /* skipping an emulated instruction also counts */ |
| 2578 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2579 | } |
| 2580 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2581 | /* |
| 2582 | * KVM wants to inject page-faults which it got to the guest. This function |
| 2583 | * 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] | 2584 | */ |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2585 | 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] | 2586 | { |
| 2587 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2588 | |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2589 | if (!(vmcs12->exception_bitmap & (1u << nr))) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2590 | return 0; |
| 2591 | |
Wanpeng Li | a29fd27 | 2017-06-05 05:19:09 -0700 | [diff] [blame] | 2592 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 2593 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 2594 | vmcs_readl(EXIT_QUALIFICATION)); |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2595 | return 1; |
| 2596 | } |
| 2597 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2598 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
Joerg Roedel | ce7ddec | 2010-04-22 12:33:13 +0200 | [diff] [blame] | 2599 | bool has_error_code, u32 error_code, |
| 2600 | bool reinject) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2601 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2602 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2603 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2604 | |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2605 | if (!reinject && is_guest_mode(vcpu) && |
| 2606 | nested_vmx_check_exception(vcpu, nr)) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2607 | return; |
| 2608 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2609 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2610 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2611 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 2612 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2613 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2614 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 2615 | int inc_eip = 0; |
| 2616 | if (kvm_exception_is_soft(nr)) |
| 2617 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 2618 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 2619 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2620 | return; |
| 2621 | } |
| 2622 | |
Sean Christopherson | 94b4fed | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 2623 | WARN_ON_ONCE(vmx->emulation_required); |
| 2624 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2625 | if (kvm_exception_is_soft(nr)) { |
| 2626 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2627 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2628 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 2629 | } else |
| 2630 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 2631 | |
| 2632 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2633 | } |
| 2634 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2635 | static bool vmx_rdtscp_supported(void) |
| 2636 | { |
| 2637 | return cpu_has_vmx_rdtscp(); |
| 2638 | } |
| 2639 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 2640 | static bool vmx_invpcid_supported(void) |
| 2641 | { |
| 2642 | return cpu_has_vmx_invpcid() && enable_ept; |
| 2643 | } |
| 2644 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2645 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2646 | * Swap MSR entry in host/guest MSR entry array. |
| 2647 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2648 | 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] | 2649 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2650 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2651 | |
| 2652 | tmp = vmx->guest_msrs[to]; |
| 2653 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 2654 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2658 | * Set up the vmcs to automatically save and restore system |
| 2659 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 2660 | * mode, as fiddling with msrs is very expensive. |
| 2661 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2662 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2663 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2664 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2665 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2666 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2667 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2668 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2669 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2670 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2671 | move_msr_up(vmx, index, save_nmsrs++); |
| 2672 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2673 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2674 | move_msr_up(vmx, index, save_nmsrs++); |
| 2675 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2676 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2677 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2678 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 2679 | if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2680 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2681 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2682 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2683 | * if efer.sce is enabled. |
| 2684 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2685 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2686 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2687 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2688 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2689 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2690 | index = __find_msr_index(vmx, MSR_EFER); |
| 2691 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2692 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2693 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2694 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 2695 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2696 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 2697 | vmx_update_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2701 | * reads and returns guest's timestamp counter "register" |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2702 | * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset |
| 2703 | * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2704 | */ |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2705 | static u64 guest_read_tsc(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2706 | { |
| 2707 | u64 host_tsc, tsc_offset; |
| 2708 | |
Andy Lutomirski | 4ea1636 | 2015-06-25 18:44:07 +0200 | [diff] [blame] | 2709 | host_tsc = rdtsc(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2710 | tsc_offset = vmcs_read64(TSC_OFFSET); |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2711 | return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2715 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2716 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2717 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2718 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2719 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2720 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2721 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 2722 | * to the spec, this should set L1's TSC; The offset that L1 |
| 2723 | * set for L2 remains unchanged, and still needs to be added |
| 2724 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2725 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2726 | struct vmcs12 *vmcs12; |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2727 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 2728 | vmcs12 = get_vmcs12(vcpu); |
| 2729 | vmcs_write64(TSC_OFFSET, offset + |
| 2730 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 2731 | vmcs12->tsc_offset : 0)); |
| 2732 | } else { |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 2733 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 2734 | vmcs_read64(TSC_OFFSET), offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2735 | vmcs_write64(TSC_OFFSET, offset); |
| 2736 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2737 | } |
| 2738 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 2739 | static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu) |
| 2740 | { |
| 2741 | struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 2742 | return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31))); |
| 2743 | } |
| 2744 | |
| 2745 | /* |
| 2746 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 2747 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 2748 | * all guests if the "nested" module option is off, and can also be disabled |
| 2749 | * for a single guest by disabling its VMX cpuid bit. |
| 2750 | */ |
| 2751 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 2752 | { |
| 2753 | return nested && guest_cpuid_has_vmx(vcpu); |
| 2754 | } |
| 2755 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2756 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2757 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 2758 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 2759 | * The same values should also be used to verify that vmcs12 control fields are |
| 2760 | * valid during nested entry from L1 to L2. |
| 2761 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 2762 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 2763 | * bit in the high half is on if the corresponding bit in the control field |
| 2764 | * may be on. See also vmx_control_verify(). |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2765 | */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2766 | static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx) |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2767 | { |
| 2768 | /* |
| 2769 | * Note that as a general rule, the high half of the MSRs (bits in |
| 2770 | * the control fields which may be 1) should be initialized by the |
| 2771 | * intersection of the underlying hardware's MSR (i.e., features which |
| 2772 | * can be supported) and the list of features we want to expose - |
| 2773 | * because they are known to be properly supported in our code. |
| 2774 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 2775 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 2776 | * reason is that if one of these bits is necessary, it will appear |
| 2777 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 2778 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
| 2779 | * nested_vmx_exit_handled() will not pass related exits to L1. |
| 2780 | * These rules have exceptions below. |
| 2781 | */ |
| 2782 | |
| 2783 | /* pin-based controls */ |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 2784 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2785 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 2786 | vmx->nested.nested_vmx_pinbased_ctls_high); |
| 2787 | vmx->nested.nested_vmx_pinbased_ctls_low |= |
| 2788 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2789 | vmx->nested.nested_vmx_pinbased_ctls_high &= |
| 2790 | PIN_BASED_EXT_INTR_MASK | |
| 2791 | PIN_BASED_NMI_EXITING | |
| 2792 | PIN_BASED_VIRTUAL_NMIS; |
| 2793 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
| 2794 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 2795 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 2796 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 2797 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
| 2798 | PIN_BASED_POSTED_INTR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2799 | |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 2800 | /* exit controls */ |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2801 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2802 | vmx->nested.nested_vmx_exit_ctls_low, |
| 2803 | vmx->nested.nested_vmx_exit_ctls_high); |
| 2804 | vmx->nested.nested_vmx_exit_ctls_low = |
| 2805 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2806 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2807 | vmx->nested.nested_vmx_exit_ctls_high &= |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2808 | #ifdef CONFIG_X86_64 |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2809 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2810 | #endif |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2811 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2812 | vmx->nested.nested_vmx_exit_ctls_high |= |
| 2813 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2814 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2815 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 2816 | |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 2817 | if (kvm_mpx_supported()) |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2818 | vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2819 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2820 | /* We support free control of debug control saving. */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2821 | vmx->nested.nested_vmx_true_exit_ctls_low = |
| 2822 | vmx->nested.nested_vmx_exit_ctls_low & |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2823 | ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
| 2824 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2825 | /* entry controls */ |
| 2826 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2827 | vmx->nested.nested_vmx_entry_ctls_low, |
| 2828 | vmx->nested.nested_vmx_entry_ctls_high); |
| 2829 | vmx->nested.nested_vmx_entry_ctls_low = |
| 2830 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2831 | vmx->nested.nested_vmx_entry_ctls_high &= |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 2832 | #ifdef CONFIG_X86_64 |
| 2833 | VM_ENTRY_IA32E_MODE | |
| 2834 | #endif |
| 2835 | VM_ENTRY_LOAD_IA32_PAT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2836 | vmx->nested.nested_vmx_entry_ctls_high |= |
| 2837 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 2838 | if (kvm_mpx_supported()) |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2839 | vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 2840 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2841 | /* We support free control of debug control loading. */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2842 | vmx->nested.nested_vmx_true_entry_ctls_low = |
| 2843 | vmx->nested.nested_vmx_entry_ctls_low & |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2844 | ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
| 2845 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2846 | /* cpu-based controls */ |
| 2847 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2848 | vmx->nested.nested_vmx_procbased_ctls_low, |
| 2849 | vmx->nested.nested_vmx_procbased_ctls_high); |
| 2850 | vmx->nested.nested_vmx_procbased_ctls_low = |
| 2851 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2852 | vmx->nested.nested_vmx_procbased_ctls_high &= |
Jan Kiszka | a294c9b | 2013-10-23 17:43:09 +0100 | [diff] [blame] | 2853 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 2854 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2855 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 2856 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 2857 | CPU_BASED_CR3_STORE_EXITING | |
| 2858 | #ifdef CONFIG_X86_64 |
| 2859 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 2860 | #endif |
| 2861 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 2862 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 2863 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 2864 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 2865 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2866 | /* |
| 2867 | * We can allow some features even when not supported by the |
| 2868 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 2869 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 2870 | * without MSR bitmaps. |
| 2871 | */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2872 | vmx->nested.nested_vmx_procbased_ctls_high |= |
| 2873 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 2874 | CPU_BASED_USE_MSR_BITMAPS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2875 | |
Jan Kiszka | 3dcdf3e | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2876 | /* We support free control of CR3 access interception. */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2877 | vmx->nested.nested_vmx_true_procbased_ctls_low = |
| 2878 | vmx->nested.nested_vmx_procbased_ctls_low & |
Jan Kiszka | 3dcdf3e | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2879 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 2880 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2881 | /* secondary cpu-based controls */ |
| 2882 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2883 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 2884 | vmx->nested.nested_vmx_secondary_ctls_high); |
| 2885 | vmx->nested.nested_vmx_secondary_ctls_low = 0; |
| 2886 | vmx->nested.nested_vmx_secondary_ctls_high &= |
Jan Kiszka | d6851fb | 2013-02-23 22:34:39 +0100 | [diff] [blame] | 2887 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 2888 | SECONDARY_EXEC_RDTSCP | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 2889 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 2890 | SECONDARY_EXEC_ENABLE_VPID | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 2891 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 2892 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 2893 | SECONDARY_EXEC_WBINVD_EXITING | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 2894 | SECONDARY_EXEC_XSAVES; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2895 | |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2896 | if (enable_ept) { |
| 2897 | /* nested EPT: emulate EPT also to L1 */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2898 | vmx->nested.nested_vmx_secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 2899 | SECONDARY_EXEC_ENABLE_EPT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2900 | vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
Jan Kiszka | d3134db | 2013-10-23 14:40:31 +0100 | [diff] [blame] | 2901 | VMX_EPTP_WB_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 2902 | VMX_EPT_INVEPT_BIT; |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 2903 | if (cpu_has_vmx_ept_execute_only()) |
| 2904 | vmx->nested.nested_vmx_ept_caps |= |
| 2905 | VMX_EPT_EXECUTE_ONLY_BIT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2906 | vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept; |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 2907 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
| 2908 | VMX_EPT_EXTENT_CONTEXT_BIT; |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2909 | } else |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2910 | vmx->nested.nested_vmx_ept_caps = 0; |
Nadav Har'El | afa61f7 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2911 | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 2912 | /* |
| 2913 | * Old versions of KVM use the single-context version without |
| 2914 | * checking for support, so declare that it is supported even |
| 2915 | * though it is treated as global context. The alternative is |
| 2916 | * not failing the single-context invvpid, and it is worse. |
| 2917 | */ |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 2918 | if (enable_vpid) |
| 2919 | vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT | |
Jan Dakinevich | 04c776e | 2018-02-23 11:42:18 +0100 | [diff] [blame] | 2920 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 2921 | else |
| 2922 | vmx->nested.nested_vmx_vpid_caps = 0; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 2923 | |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 2924 | if (enable_unrestricted_guest) |
| 2925 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 2926 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 2927 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2928 | /* miscellaneous data */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2929 | rdmsr(MSR_IA32_VMX_MISC, |
| 2930 | vmx->nested.nested_vmx_misc_low, |
| 2931 | vmx->nested.nested_vmx_misc_high); |
| 2932 | vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 2933 | vmx->nested.nested_vmx_misc_low |= |
| 2934 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2935 | VMX_MISC_ACTIVITY_HLT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2936 | vmx->nested.nested_vmx_misc_high = 0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 2940 | { |
| 2941 | /* |
| 2942 | * Bits 0 in high must be 0, and bits 1 in low must be 1. |
| 2943 | */ |
| 2944 | return ((control & high) | low) == control; |
| 2945 | } |
| 2946 | |
| 2947 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 2948 | { |
| 2949 | return low | ((u64)high << 32); |
| 2950 | } |
| 2951 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 2952 | /* Returns 0 on success, non-0 otherwise. */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2953 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 2954 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2955 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2956 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2957 | switch (msr_index) { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2958 | case MSR_IA32_VMX_BASIC: |
| 2959 | /* |
| 2960 | * This MSR reports some information about VMX support. We |
| 2961 | * should return information about the VMX we emulate for the |
| 2962 | * guest, and the VMCS structure we give it - not about the |
| 2963 | * VMX support of the underlying hardware. |
| 2964 | */ |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 2965 | *pdata = VMCS12_REVISION | VMX_BASIC_TRUE_CTLS | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2966 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 2967 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 2968 | if (cpu_has_vmx_basic_inout()) |
| 2969 | *pdata |= VMX_BASIC_INOUT; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2970 | break; |
| 2971 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 2972 | case MSR_IA32_VMX_PINBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2973 | *pdata = vmx_control_msr( |
| 2974 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 2975 | vmx->nested.nested_vmx_pinbased_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2976 | break; |
| 2977 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2978 | *pdata = vmx_control_msr( |
| 2979 | vmx->nested.nested_vmx_true_procbased_ctls_low, |
| 2980 | vmx->nested.nested_vmx_procbased_ctls_high); |
Jan Kiszka | 3dcdf3e | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2981 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2982 | case MSR_IA32_VMX_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2983 | *pdata = vmx_control_msr( |
| 2984 | vmx->nested.nested_vmx_procbased_ctls_low, |
| 2985 | vmx->nested.nested_vmx_procbased_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2986 | break; |
| 2987 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2988 | *pdata = vmx_control_msr( |
| 2989 | vmx->nested.nested_vmx_true_exit_ctls_low, |
| 2990 | vmx->nested.nested_vmx_exit_ctls_high); |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2991 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2992 | case MSR_IA32_VMX_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2993 | *pdata = vmx_control_msr( |
| 2994 | vmx->nested.nested_vmx_exit_ctls_low, |
| 2995 | vmx->nested.nested_vmx_exit_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2996 | break; |
| 2997 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2998 | *pdata = vmx_control_msr( |
| 2999 | vmx->nested.nested_vmx_true_entry_ctls_low, |
| 3000 | vmx->nested.nested_vmx_entry_ctls_high); |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3001 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3002 | case MSR_IA32_VMX_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3003 | *pdata = vmx_control_msr( |
| 3004 | vmx->nested.nested_vmx_entry_ctls_low, |
| 3005 | vmx->nested.nested_vmx_entry_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3006 | break; |
| 3007 | case MSR_IA32_VMX_MISC: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3008 | *pdata = vmx_control_msr( |
| 3009 | vmx->nested.nested_vmx_misc_low, |
| 3010 | vmx->nested.nested_vmx_misc_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3011 | break; |
| 3012 | /* |
| 3013 | * These MSRs specify bits which the guest must keep fixed (on or off) |
| 3014 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 3015 | * We picked the standard core2 setting. |
| 3016 | */ |
| 3017 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 3018 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 3019 | case MSR_IA32_VMX_CR0_FIXED0: |
| 3020 | *pdata = VMXON_CR0_ALWAYSON; |
| 3021 | break; |
| 3022 | case MSR_IA32_VMX_CR0_FIXED1: |
| 3023 | *pdata = -1ULL; |
| 3024 | break; |
| 3025 | case MSR_IA32_VMX_CR4_FIXED0: |
| 3026 | *pdata = VMXON_CR4_ALWAYSON; |
| 3027 | break; |
| 3028 | case MSR_IA32_VMX_CR4_FIXED1: |
| 3029 | *pdata = -1ULL; |
| 3030 | break; |
| 3031 | case MSR_IA32_VMX_VMCS_ENUM: |
Jan Kiszka | 5381417 | 2014-06-16 13:59:44 +0200 | [diff] [blame] | 3032 | *pdata = 0x2e; /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3033 | break; |
| 3034 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3035 | *pdata = vmx_control_msr( |
| 3036 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 3037 | vmx->nested.nested_vmx_secondary_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3038 | break; |
| 3039 | case MSR_IA32_VMX_EPT_VPID_CAP: |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 3040 | *pdata = vmx->nested.nested_vmx_ept_caps | |
| 3041 | ((u64)vmx->nested.nested_vmx_vpid_caps << 32); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3042 | break; |
| 3043 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3044 | return 1; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 3045 | } |
| 3046 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3047 | return 0; |
| 3048 | } |
| 3049 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3050 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 3051 | uint64_t val) |
| 3052 | { |
| 3053 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 3054 | |
| 3055 | return !(val & ~valid_bits); |
| 3056 | } |
| 3057 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3058 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3059 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 3060 | * Returns 0 on success, non-0 otherwise. |
| 3061 | * Assumes vcpu_load() was already called. |
| 3062 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3063 | 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] | 3064 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3065 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3066 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3067 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3068 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3069 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3070 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3071 | break; |
| 3072 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3073 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3074 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3075 | case MSR_KERNEL_GS_BASE: |
| 3076 | vmx_load_host_state(to_vmx(vcpu)); |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3077 | msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3078 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3079 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3080 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3081 | return kvm_get_msr_common(vcpu, msr_info); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 3082 | case MSR_IA32_TSC: |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 3083 | msr_info->data = guest_read_tsc(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3084 | break; |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3085 | case MSR_IA32_SPEC_CTRL: |
| 3086 | if (!msr_info->host_initiated && |
Konrad Rzeszutek Wilk | 99318ec | 2018-04-25 22:04:25 -0400 | [diff] [blame] | 3087 | !guest_cpuid_has_spec_ctrl(vcpu)) |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3088 | return 1; |
| 3089 | |
| 3090 | msr_info->data = to_vmx(vcpu)->spec_ctrl; |
| 3091 | break; |
KarimAllah Ahmed | 755502f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3092 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3093 | if (!msr_info->host_initiated && |
| 3094 | !guest_cpuid_has_arch_capabilities(vcpu)) |
| 3095 | return 1; |
| 3096 | msr_info->data = to_vmx(vcpu)->arch_capabilities; |
| 3097 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3098 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3099 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3100 | break; |
| 3101 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3102 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3103 | break; |
| 3104 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3105 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3106 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3107 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | cce8d2e | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3108 | if (!kvm_mpx_supported() || |
| 3109 | (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3110 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3111 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3112 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3113 | case MSR_IA32_MCG_EXT_CTL: |
| 3114 | if (!msr_info->host_initiated && |
| 3115 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 3116 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3117 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3118 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 3119 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3120 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3121 | msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3122 | break; |
| 3123 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3124 | if (!nested_vmx_allowed(vcpu)) |
| 3125 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3126 | return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3127 | case MSR_IA32_XSS: |
| 3128 | if (!vmx_xsaves_supported()) |
| 3129 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3130 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3131 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3132 | case MSR_TSC_AUX: |
Haozhong Zhang | 81b1b9c | 2015-12-14 23:13:38 +0800 | [diff] [blame] | 3133 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3134 | return 1; |
| 3135 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3136 | default: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3137 | msr = find_msr_entry(to_vmx(vcpu), msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3138 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3139 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3140 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3141 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3142 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3143 | } |
| 3144 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3145 | return 0; |
| 3146 | } |
| 3147 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3148 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
| 3149 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3150 | /* |
| 3151 | * Writes msr value into into the appropriate "register". |
| 3152 | * Returns 0 on success, non-0 otherwise. |
| 3153 | * Assumes vcpu_load() was already called. |
| 3154 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3155 | 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] | 3156 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3157 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3158 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3159 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3160 | u32 msr_index = msr_info->index; |
| 3161 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3162 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3163 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3164 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3165 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3166 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 3167 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3168 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3169 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3170 | vmcs_writel(GUEST_FS_BASE, data); |
| 3171 | break; |
| 3172 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3173 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3174 | vmcs_writel(GUEST_GS_BASE, data); |
| 3175 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3176 | case MSR_KERNEL_GS_BASE: |
| 3177 | vmx_load_host_state(vmx); |
| 3178 | vmx->msr_guest_kernel_gs_base = data; |
| 3179 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3180 | #endif |
| 3181 | case MSR_IA32_SYSENTER_CS: |
| 3182 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 3183 | break; |
| 3184 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3185 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3186 | break; |
| 3187 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3188 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3189 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3190 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | cce8d2e | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3191 | if (!kvm_mpx_supported() || |
| 3192 | (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3193 | return 1; |
Jim Mattson | 07592d6 | 2017-05-23 11:52:54 -0700 | [diff] [blame] | 3194 | if (is_noncanonical_address(data & PAGE_MASK) || |
| 3195 | (data & MSR_IA32_BNDCFGS_RSVD)) |
| 3196 | return 1; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3197 | vmcs_write64(GUEST_BNDCFGS, data); |
| 3198 | break; |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 3199 | case MSR_IA32_TSC: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3200 | kvm_write_tsc(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3201 | break; |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3202 | case MSR_IA32_SPEC_CTRL: |
| 3203 | if (!msr_info->host_initiated && |
Konrad Rzeszutek Wilk | 99318ec | 2018-04-25 22:04:25 -0400 | [diff] [blame] | 3204 | !guest_cpuid_has_spec_ctrl(vcpu)) |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3205 | return 1; |
| 3206 | |
| 3207 | /* The STIBP bit doesn't fault even if it's not advertised */ |
Konrad Rzeszutek Wilk | bf3da84 | 2018-05-09 21:41:38 +0200 | [diff] [blame] | 3208 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD)) |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3209 | return 1; |
| 3210 | |
| 3211 | vmx->spec_ctrl = data; |
| 3212 | |
| 3213 | if (!data) |
| 3214 | break; |
| 3215 | |
| 3216 | /* |
| 3217 | * For non-nested: |
| 3218 | * When it's written (to non-zero) for the first time, pass |
| 3219 | * it through. |
| 3220 | * |
| 3221 | * For nested: |
| 3222 | * The handling of the MSR bitmap for L2 guests is done in |
| 3223 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 3224 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 3225 | * in the merging. We update the vmcs01 here for L1 as well |
| 3226 | * since it will end up touching the MSR anyway now. |
| 3227 | */ |
| 3228 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, |
| 3229 | MSR_IA32_SPEC_CTRL, |
| 3230 | MSR_TYPE_RW); |
| 3231 | break; |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 3232 | case MSR_IA32_PRED_CMD: |
| 3233 | if (!msr_info->host_initiated && |
| 3234 | !guest_cpuid_has_ibpb(vcpu)) |
| 3235 | return 1; |
| 3236 | |
| 3237 | if (data & ~PRED_CMD_IBPB) |
| 3238 | return 1; |
| 3239 | |
| 3240 | if (!data) |
| 3241 | break; |
| 3242 | |
| 3243 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 3244 | |
| 3245 | /* |
| 3246 | * For non-nested: |
| 3247 | * When it's written (to non-zero) for the first time, pass |
| 3248 | * it through. |
| 3249 | * |
| 3250 | * For nested: |
| 3251 | * The handling of the MSR bitmap for L2 guests is done in |
| 3252 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 3253 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 3254 | * in the merging. |
| 3255 | */ |
| 3256 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD, |
| 3257 | MSR_TYPE_W); |
| 3258 | break; |
KarimAllah Ahmed | 755502f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3259 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3260 | if (!msr_info->host_initiated) |
| 3261 | return 1; |
| 3262 | vmx->arch_capabilities = data; |
| 3263 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3264 | case MSR_IA32_CR_PAT: |
| 3265 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 3266 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 3267 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3268 | vmcs_write64(GUEST_IA32_PAT, data); |
| 3269 | vcpu->arch.pat = data; |
| 3270 | break; |
| 3271 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3272 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3273 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 3274 | case MSR_IA32_TSC_ADJUST: |
| 3275 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3276 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3277 | case MSR_IA32_MCG_EXT_CTL: |
| 3278 | if ((!msr_info->host_initiated && |
| 3279 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 3280 | FEATURE_CONTROL_LMCE)) || |
| 3281 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 3282 | return 1; |
| 3283 | vcpu->arch.mcg_ext_ctl = data; |
| 3284 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3285 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3286 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3287 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3288 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 3289 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3290 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3291 | if (msr_info->host_initiated && data == 0) |
| 3292 | vmx_leave_nested(vcpu); |
| 3293 | break; |
| 3294 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3295 | return 1; /* they are read-only */ |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3296 | case MSR_IA32_XSS: |
| 3297 | if (!vmx_xsaves_supported()) |
| 3298 | return 1; |
| 3299 | /* |
| 3300 | * The only supported bit as of Skylake is bit 8, but |
| 3301 | * it is not supported on KVM. |
| 3302 | */ |
| 3303 | if (data != 0) |
| 3304 | return 1; |
| 3305 | vcpu->arch.ia32_xss = data; |
| 3306 | if (vcpu->arch.ia32_xss != host_xss) |
| 3307 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
| 3308 | vcpu->arch.ia32_xss, host_xss); |
| 3309 | else |
| 3310 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 3311 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3312 | case MSR_TSC_AUX: |
Haozhong Zhang | 81b1b9c | 2015-12-14 23:13:38 +0800 | [diff] [blame] | 3313 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3314 | return 1; |
| 3315 | /* Check reserved bit, higher 32 bits should be zero */ |
| 3316 | if ((data >> 32) != 0) |
| 3317 | return 1; |
| 3318 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3319 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3320 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3321 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3322 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3323 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3324 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 3325 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3326 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 3327 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3328 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3329 | if (ret) |
| 3330 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3331 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3332 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3333 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3334 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3335 | } |
| 3336 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3337 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3338 | } |
| 3339 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3340 | 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] | 3341 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3342 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 3343 | switch (reg) { |
| 3344 | case VCPU_REGS_RSP: |
| 3345 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 3346 | break; |
| 3347 | case VCPU_REGS_RIP: |
| 3348 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 3349 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3350 | case VCPU_EXREG_PDPTR: |
| 3351 | if (enable_ept) |
| 3352 | ept_save_pdptrs(vcpu); |
| 3353 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3354 | default: |
| 3355 | break; |
| 3356 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3357 | } |
| 3358 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3359 | static __init int cpu_has_kvm_support(void) |
| 3360 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 3361 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | static __init int vmx_disabled_by_bios(void) |
| 3365 | { |
| 3366 | u64 msr; |
| 3367 | |
| 3368 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3369 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3370 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3371 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 3372 | && tboot_enabled()) |
| 3373 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3374 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3375 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3376 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 3377 | && !tboot_enabled()) { |
| 3378 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3379 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3380 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 3381 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3382 | /* launched w/o TXT and VMX disabled */ |
| 3383 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 3384 | && !tboot_enabled()) |
| 3385 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3389 | } |
| 3390 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 3391 | static void kvm_cpu_vmxon(u64 addr) |
| 3392 | { |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 3393 | intel_pt_handle_vmx(1); |
| 3394 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 3395 | asm volatile (ASM_VMX_VMXON_RAX |
| 3396 | : : "a"(&addr), "m"(addr) |
| 3397 | : "memory", "cc"); |
| 3398 | } |
| 3399 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 3400 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3401 | { |
| 3402 | int cpu = raw_smp_processor_id(); |
| 3403 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3404 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3405 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 3406 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3407 | return -EBUSY; |
| 3408 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3409 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 3410 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 3411 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 3412 | |
| 3413 | /* |
| 3414 | * Now we can enable the vmclear operation in kdump |
| 3415 | * since the loaded_vmcss_on_cpu list on this cpu |
| 3416 | * has been initialized. |
| 3417 | * |
| 3418 | * Though the cpu is not in VMX operation now, there |
| 3419 | * is no problem to enable the vmclear operation |
| 3420 | * for the loaded_vmcss_on_cpu list is empty! |
| 3421 | */ |
| 3422 | crash_enable_local_vmclear(cpu); |
| 3423 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3424 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3425 | |
| 3426 | test_bits = FEATURE_CONTROL_LOCKED; |
| 3427 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 3428 | if (tboot_enabled()) |
| 3429 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 3430 | |
| 3431 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3432 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3433 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 3434 | } |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 3435 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3436 | |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 3437 | if (vmm_exclusive) { |
| 3438 | kvm_cpu_vmxon(phys_addr); |
| 3439 | ept_sync_global(); |
| 3440 | } |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3441 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 3442 | native_store_gdt(this_cpu_ptr(&host_gdt)); |
Avi Kivity | 3444d7d | 2010-07-26 18:32:38 +0300 | [diff] [blame] | 3443 | |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3444 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3445 | } |
| 3446 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3447 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3448 | { |
| 3449 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3450 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3451 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3452 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 3453 | loaded_vmcss_on_cpu_link) |
| 3454 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3455 | } |
| 3456 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 3457 | |
| 3458 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 3459 | * tricks. |
| 3460 | */ |
| 3461 | static void kvm_cpu_vmxoff(void) |
| 3462 | { |
| 3463 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 3464 | |
| 3465 | intel_pt_handle_vmx(0); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 3466 | } |
| 3467 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 3468 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3469 | { |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 3470 | if (vmm_exclusive) { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3471 | vmclear_local_loaded_vmcss(); |
Dongxiao Xu | 4610c9c | 2010-05-11 18:29:48 +0800 | [diff] [blame] | 3472 | kvm_cpu_vmxoff(); |
| 3473 | } |
Andy Lutomirski | 375074c | 2014-10-24 15:58:07 -0700 | [diff] [blame] | 3474 | cr4_clear_bits(X86_CR4_VMXE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3475 | } |
| 3476 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3477 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3478 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3479 | { |
| 3480 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3481 | u32 ctl = ctl_min | ctl_opt; |
| 3482 | |
| 3483 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 3484 | |
| 3485 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 3486 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 3487 | |
| 3488 | /* Ensure minimum (required) set of control bits are supported. */ |
| 3489 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3490 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3491 | |
| 3492 | *result = ctl; |
| 3493 | return 0; |
| 3494 | } |
| 3495 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 3496 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 3497 | { |
| 3498 | u32 vmx_msr_low, vmx_msr_high; |
| 3499 | |
| 3500 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 3501 | return vmx_msr_high & ctl; |
| 3502 | } |
| 3503 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3504 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3505 | { |
| 3506 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3507 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3508 | u32 _pin_based_exec_control = 0; |
| 3509 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3510 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3511 | u32 _vmexit_control = 0; |
| 3512 | u32 _vmentry_control = 0; |
| 3513 | |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 3514 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3515 | #ifdef CONFIG_X86_64 |
| 3516 | CPU_BASED_CR8_LOAD_EXITING | |
| 3517 | CPU_BASED_CR8_STORE_EXITING | |
| 3518 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3519 | CPU_BASED_CR3_LOAD_EXITING | |
| 3520 | CPU_BASED_CR3_STORE_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3521 | CPU_BASED_USE_IO_BITMAPS | |
| 3522 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3523 | CPU_BASED_USE_TSC_OFFSETING | |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 3524 | CPU_BASED_MWAIT_EXITING | |
| 3525 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 3526 | CPU_BASED_INVLPG_EXITING | |
| 3527 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 3528 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3529 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3530 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3531 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3532 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 3533 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3534 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3535 | #ifdef CONFIG_X86_64 |
| 3536 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 3537 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 3538 | ~CPU_BASED_CR8_STORE_EXITING; |
| 3539 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3540 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3541 | min2 = 0; |
| 3542 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3543 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3544 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3545 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3546 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3547 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3548 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3549 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3550 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3551 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 3552 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3553 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 3554 | SECONDARY_EXEC_XSAVES | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 3555 | SECONDARY_EXEC_ENABLE_PML | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 3556 | SECONDARY_EXEC_TSC_SCALING; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3557 | if (adjust_vmx_controls(min2, opt2, |
| 3558 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3559 | &_cpu_based_2nd_exec_control) < 0) |
| 3560 | return -EIO; |
| 3561 | } |
| 3562 | #ifndef CONFIG_X86_64 |
| 3563 | if (!(_cpu_based_2nd_exec_control & |
| 3564 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 3565 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 3566 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3567 | |
| 3568 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 3569 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3570 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3571 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 3572 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3573 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3574 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3575 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 3576 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 3577 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 3578 | CPU_BASED_CR3_STORE_EXITING | |
| 3579 | CPU_BASED_INVLPG_EXITING); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3580 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
| 3581 | vmx_capability.ept, vmx_capability.vpid); |
| 3582 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3583 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3584 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3585 | #ifdef CONFIG_X86_64 |
| 3586 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 3587 | #endif |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 3588 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3589 | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3590 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 3591 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3592 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3593 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3594 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 3595 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | |
| 3596 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3597 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 3598 | &_pin_based_exec_control) < 0) |
| 3599 | return -EIO; |
| 3600 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 3601 | if (cpu_has_broken_vmx_preemption_timer()) |
| 3602 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3603 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3604 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3605 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 3606 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 3607 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 3608 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3609 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 3610 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3611 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3612 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 3613 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3614 | |
| 3615 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 3616 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3617 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3618 | |
| 3619 | #ifdef CONFIG_X86_64 |
| 3620 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 3621 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3622 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3623 | #endif |
| 3624 | |
| 3625 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 3626 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3627 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3628 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3629 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 3630 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 3631 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3632 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3633 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3634 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 3635 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3636 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3637 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 3638 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3639 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 3640 | cpu_has_load_ia32_efer = |
| 3641 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 3642 | VM_ENTRY_LOAD_IA32_EFER) |
| 3643 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 3644 | VM_EXIT_LOAD_IA32_EFER); |
| 3645 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 3646 | cpu_has_load_perf_global_ctrl = |
| 3647 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 3648 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 3649 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 3650 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 3651 | |
| 3652 | /* |
| 3653 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 3654 | * 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] | 3655 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 3656 | * |
| 3657 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 3658 | * |
| 3659 | * AAK155 (model 26) |
| 3660 | * AAP115 (model 30) |
| 3661 | * AAT100 (model 37) |
| 3662 | * BC86,AAY89,BD102 (model 44) |
| 3663 | * BA97 (model 46) |
| 3664 | * |
| 3665 | */ |
| 3666 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 3667 | switch (boot_cpu_data.x86_model) { |
| 3668 | case 26: |
| 3669 | case 30: |
| 3670 | case 37: |
| 3671 | case 44: |
| 3672 | case 46: |
| 3673 | cpu_has_load_perf_global_ctrl = false; |
| 3674 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 3675 | "does not work properly. Using workaround\n"); |
| 3676 | break; |
| 3677 | default: |
| 3678 | break; |
| 3679 | } |
| 3680 | } |
| 3681 | |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 3682 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3683 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 3684 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3685 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 3686 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3687 | |
| 3688 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 3689 | { |
| 3690 | int node = cpu_to_node(cpu); |
| 3691 | struct page *pages; |
| 3692 | struct vmcs *vmcs; |
| 3693 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 3694 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3695 | if (!pages) |
| 3696 | return NULL; |
| 3697 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3698 | memset(vmcs, 0, vmcs_config.size); |
| 3699 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3700 | return vmcs; |
| 3701 | } |
| 3702 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3703 | static void free_vmcs(struct vmcs *vmcs) |
| 3704 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3705 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3706 | } |
| 3707 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3708 | /* |
| 3709 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 3710 | */ |
| 3711 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 3712 | { |
| 3713 | if (!loaded_vmcs->vmcs) |
| 3714 | return; |
| 3715 | loaded_vmcs_clear(loaded_vmcs); |
| 3716 | free_vmcs(loaded_vmcs->vmcs); |
| 3717 | loaded_vmcs->vmcs = NULL; |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3718 | if (loaded_vmcs->msr_bitmap) |
| 3719 | free_page((unsigned long)loaded_vmcs->msr_bitmap); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 3720 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3721 | } |
| 3722 | |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 3723 | static struct vmcs *alloc_vmcs(void) |
| 3724 | { |
| 3725 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
| 3726 | } |
| 3727 | |
| 3728 | static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 3729 | { |
| 3730 | loaded_vmcs->vmcs = alloc_vmcs(); |
| 3731 | if (!loaded_vmcs->vmcs) |
| 3732 | return -ENOMEM; |
| 3733 | |
| 3734 | loaded_vmcs->shadow_vmcs = NULL; |
| 3735 | loaded_vmcs_init(loaded_vmcs); |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3736 | |
| 3737 | if (cpu_has_vmx_msr_bitmap()) { |
| 3738 | loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 3739 | if (!loaded_vmcs->msr_bitmap) |
| 3740 | goto out_vmcs; |
| 3741 | memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); |
| 3742 | } |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 3743 | return 0; |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3744 | |
| 3745 | out_vmcs: |
| 3746 | free_loaded_vmcs(loaded_vmcs); |
| 3747 | return -ENOMEM; |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 3748 | } |
| 3749 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 3750 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3751 | { |
| 3752 | int cpu; |
| 3753 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3754 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3755 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3756 | per_cpu(vmxarea, cpu) = NULL; |
| 3757 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3758 | } |
| 3759 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 3760 | static void init_vmcs_shadow_fields(void) |
| 3761 | { |
| 3762 | int i, j; |
| 3763 | |
| 3764 | /* No checks for read only fields yet */ |
| 3765 | |
| 3766 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
| 3767 | switch (shadow_read_write_fields[i]) { |
| 3768 | case GUEST_BNDCFGS: |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3769 | if (!kvm_mpx_supported()) |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 3770 | continue; |
| 3771 | break; |
| 3772 | default: |
| 3773 | break; |
| 3774 | } |
| 3775 | |
| 3776 | if (j < i) |
| 3777 | shadow_read_write_fields[j] = |
| 3778 | shadow_read_write_fields[i]; |
| 3779 | j++; |
| 3780 | } |
| 3781 | max_shadow_read_write_fields = j; |
| 3782 | |
| 3783 | /* shadowed fields guest access without vmexit */ |
| 3784 | for (i = 0; i < max_shadow_read_write_fields; i++) { |
| 3785 | clear_bit(shadow_read_write_fields[i], |
| 3786 | vmx_vmwrite_bitmap); |
| 3787 | clear_bit(shadow_read_write_fields[i], |
| 3788 | vmx_vmread_bitmap); |
| 3789 | } |
| 3790 | for (i = 0; i < max_shadow_read_only_fields; i++) |
| 3791 | clear_bit(shadow_read_only_fields[i], |
| 3792 | vmx_vmread_bitmap); |
| 3793 | } |
| 3794 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3795 | static __init int alloc_kvm_area(void) |
| 3796 | { |
| 3797 | int cpu; |
| 3798 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3799 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3800 | struct vmcs *vmcs; |
| 3801 | |
| 3802 | vmcs = alloc_vmcs_cpu(cpu); |
| 3803 | if (!vmcs) { |
| 3804 | free_kvm_area(); |
| 3805 | return -ENOMEM; |
| 3806 | } |
| 3807 | |
| 3808 | per_cpu(vmxarea, cpu) = vmcs; |
| 3809 | } |
| 3810 | return 0; |
| 3811 | } |
| 3812 | |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 3813 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 3814 | { |
| 3815 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 3816 | } |
| 3817 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 3818 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3819 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3820 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3821 | if (!emulate_invalid_guest_state) { |
| 3822 | /* |
| 3823 | * CS and SS RPL should be equal during guest entry according |
| 3824 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 3825 | * is in the middle of the transition from real mode to |
| 3826 | * protected mode it is safe to assume that RPL 0 is a good |
| 3827 | * default value. |
| 3828 | */ |
| 3829 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 3830 | save->selector &= ~SEGMENT_RPL_MASK; |
| 3831 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3832 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3833 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3834 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 3838 | { |
| 3839 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3840 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3841 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3842 | /* |
| 3843 | * Update real mode segment cache. It may be not up-to-date if sement |
| 3844 | * register was written while vcpu was in a guest mode. |
| 3845 | */ |
| 3846 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 3847 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 3848 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 3849 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 3850 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 3851 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 3852 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3853 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3854 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3855 | vmx_segment_cache_clear(vmx); |
| 3856 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3857 | 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] | 3858 | |
| 3859 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3860 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 3861 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3862 | vmcs_writel(GUEST_RFLAGS, flags); |
| 3863 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 3864 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 3865 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3866 | |
| 3867 | update_exception_bitmap(vcpu); |
| 3868 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 3869 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 3870 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 3871 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 3872 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 3873 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 3874 | 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] | 3875 | } |
| 3876 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3877 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3878 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 3879 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3880 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3881 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3882 | var.dpl = 0x3; |
| 3883 | if (seg == VCPU_SREG_CS) |
| 3884 | var.type = 0x3; |
| 3885 | |
| 3886 | if (!emulate_invalid_guest_state) { |
| 3887 | var.selector = var.base >> 4; |
| 3888 | var.base = var.base & 0xffff0; |
| 3889 | var.limit = 0xffff; |
| 3890 | var.g = 0; |
| 3891 | var.db = 0; |
| 3892 | var.present = 1; |
| 3893 | var.s = 1; |
| 3894 | var.l = 0; |
| 3895 | var.unusable = 0; |
| 3896 | var.type = 0x3; |
| 3897 | var.avl = 0; |
| 3898 | if (save->base & 0xf) |
| 3899 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 3900 | "paragraph aligned when entering " |
| 3901 | "protected mode (seg=%d)", seg); |
| 3902 | } |
| 3903 | |
| 3904 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 7c3bab1 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 3905 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3906 | vmcs_write32(sf->limit, var.limit); |
| 3907 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3908 | } |
| 3909 | |
| 3910 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 3911 | { |
| 3912 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3913 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3914 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3915 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 3916 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 3917 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 3918 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 3919 | 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] | 3920 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 3921 | 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] | 3922 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3923 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3924 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3925 | /* |
| 3926 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3927 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3928 | */ |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3929 | if (!vcpu->kvm->arch.tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3930 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 3931 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 3932 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3933 | vmx_segment_cache_clear(vmx); |
| 3934 | |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 3935 | vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3936 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3937 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 3938 | |
| 3939 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3940 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3941 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 3942 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3943 | |
| 3944 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 3945 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3946 | update_exception_bitmap(vcpu); |
| 3947 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3948 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 3949 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 3950 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 3951 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 3952 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 3953 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3954 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 3955 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3956 | } |
| 3957 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3958 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 3959 | { |
| 3960 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3961 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 3962 | |
| 3963 | if (!msr) |
| 3964 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3965 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3966 | /* |
| 3967 | * Force kernel_gs_base reloading before EFER changes, as control |
| 3968 | * of this msr depends on is_long_mode(). |
| 3969 | */ |
| 3970 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3971 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3972 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3973 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3974 | msr->data = efer; |
| 3975 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3976 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 3977 | |
| 3978 | msr->data = efer & ~EFER_LME; |
| 3979 | } |
| 3980 | setup_msrs(vmx); |
| 3981 | } |
| 3982 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3983 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3984 | |
| 3985 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 3986 | { |
| 3987 | u32 guest_tr_ar; |
| 3988 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3989 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 3990 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3991 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 3992 | 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] | 3993 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 3994 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3995 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 3996 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 3997 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3998 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 3999 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 4003 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4004 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4005 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4006 | } |
| 4007 | |
| 4008 | #endif |
| 4009 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4010 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4011 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4012 | vpid_sync_context(vpid); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4013 | if (enable_ept) { |
| 4014 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 4015 | return; |
Sheng Yang | 4e1096d | 2008-07-06 19:16:51 +0800 | [diff] [blame] | 4016 | ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa)); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4017 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4018 | } |
| 4019 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4020 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 4021 | { |
| 4022 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid); |
| 4023 | } |
| 4024 | |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 4025 | static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu) |
| 4026 | { |
| 4027 | if (enable_ept) |
| 4028 | vmx_flush_tlb(vcpu); |
| 4029 | } |
| 4030 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 4031 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 4032 | { |
| 4033 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 4034 | |
| 4035 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 4036 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 4037 | } |
| 4038 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4039 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 4040 | { |
| 4041 | if (enable_ept && is_paging(vcpu)) |
| 4042 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 4043 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 4044 | } |
| 4045 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4046 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4047 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4048 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 4049 | |
| 4050 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 4051 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4052 | } |
| 4053 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4054 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 4055 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4056 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4057 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4058 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 4059 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 4060 | return; |
| 4061 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4062 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4063 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 4064 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 4065 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 4066 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4067 | } |
| 4068 | } |
| 4069 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4070 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 4071 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4072 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4073 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4074 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4075 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 4076 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 4077 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 4078 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4079 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4080 | |
| 4081 | __set_bit(VCPU_EXREG_PDPTR, |
| 4082 | (unsigned long *)&vcpu->arch.regs_avail); |
| 4083 | __set_bit(VCPU_EXREG_PDPTR, |
| 4084 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4085 | } |
| 4086 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4087 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4088 | |
| 4089 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 4090 | unsigned long cr0, |
| 4091 | struct kvm_vcpu *vcpu) |
| 4092 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 4093 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 4094 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4095 | if (!(cr0 & X86_CR0_PG)) { |
| 4096 | /* From paging/starting to nonpaging */ |
| 4097 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4098 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4099 | (CPU_BASED_CR3_LOAD_EXITING | |
| 4100 | CPU_BASED_CR3_STORE_EXITING)); |
| 4101 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4102 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4103 | } else if (!is_paging(vcpu)) { |
| 4104 | /* From nonpaging to paging */ |
| 4105 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4106 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4107 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4108 | CPU_BASED_CR3_STORE_EXITING)); |
| 4109 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4110 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4111 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 4112 | |
| 4113 | if (!(cr0 & X86_CR0_WP)) |
| 4114 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4115 | } |
| 4116 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4117 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 4118 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4119 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4120 | unsigned long hw_cr0; |
| 4121 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4122 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4123 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4124 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4125 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4126 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4127 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4128 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 4129 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4130 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4131 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 4132 | enter_rmode(vcpu); |
| 4133 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4134 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4135 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4136 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4137 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4138 | enter_lmode(vcpu); |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4139 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4140 | exit_lmode(vcpu); |
| 4141 | } |
| 4142 | #endif |
| 4143 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4144 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4145 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 4146 | |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 4147 | if (!vcpu->fpu_active) |
Avi Kivity | 81231c6 | 2010-01-24 16:26:40 +0200 | [diff] [blame] | 4148 | hw_cr0 |= X86_CR0_TS | X86_CR0_MP; |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 4149 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4150 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4151 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4152 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 4153 | |
| 4154 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 4155 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4156 | } |
| 4157 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4158 | static u64 construct_eptp(unsigned long root_hpa) |
| 4159 | { |
| 4160 | u64 eptp; |
| 4161 | |
| 4162 | /* TODO write the value reading from MSR */ |
| 4163 | eptp = VMX_EPT_DEFAULT_MT | |
| 4164 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; |
Xudong Hao | b38f993 | 2012-05-28 19:33:36 +0800 | [diff] [blame] | 4165 | if (enable_ept_ad_bits) |
| 4166 | eptp |= VMX_EPT_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4167 | eptp |= (root_hpa & PAGE_MASK); |
| 4168 | |
| 4169 | return eptp; |
| 4170 | } |
| 4171 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4172 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 4173 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4174 | unsigned long guest_cr3; |
| 4175 | u64 eptp; |
| 4176 | |
| 4177 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4178 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4179 | eptp = construct_eptp(cr3); |
| 4180 | vmcs_write64(EPT_POINTER, eptp); |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 4181 | if (is_paging(vcpu) || is_guest_mode(vcpu)) |
| 4182 | guest_cr3 = kvm_read_cr3(vcpu); |
| 4183 | else |
| 4184 | guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr; |
Marcelo Tosatti | 7c93be4 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 4185 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4186 | } |
| 4187 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4188 | vmx_flush_tlb(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4189 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4190 | } |
| 4191 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4192 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4193 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 4194 | /* |
| 4195 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 4196 | * is in force while we are in guest mode. Do not let guests control |
| 4197 | * this bit, even if host CR4.MCE == 0. |
| 4198 | */ |
| 4199 | unsigned long hw_cr4 = |
| 4200 | (cr4_read_shadow() & X86_CR4_MCE) | |
| 4201 | (cr4 & ~X86_CR4_MCE) | |
| 4202 | (to_vmx(vcpu)->rmode.vm86_active ? |
| 4203 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4204 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4205 | if (cr4 & X86_CR4_VMXE) { |
| 4206 | /* |
| 4207 | * To use VMXON (and later other VMX instructions), a guest |
| 4208 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 4209 | * So basically the check on whether to allow nested VMX |
| 4210 | * is here. |
| 4211 | */ |
| 4212 | if (!nested_vmx_allowed(vcpu)) |
| 4213 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4214 | } |
| 4215 | if (to_vmx(vcpu)->nested.vmxon && |
| 4216 | ((cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4217 | return 1; |
| 4218 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4219 | vcpu->arch.cr4 = cr4; |
Avi Kivity | bc23008 | 2009-12-08 12:14:42 +0200 | [diff] [blame] | 4220 | if (enable_ept) { |
| 4221 | if (!is_paging(vcpu)) { |
| 4222 | hw_cr4 &= ~X86_CR4_PAE; |
| 4223 | hw_cr4 |= X86_CR4_PSE; |
| 4224 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 4225 | hw_cr4 &= ~X86_CR4_PAE; |
| 4226 | } |
| 4227 | } |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4228 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4229 | if (!enable_unrestricted_guest && !is_paging(vcpu)) |
| 4230 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 4231 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 4232 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 4233 | * to be manually disabled when guest switches to non-paging |
| 4234 | * mode. |
| 4235 | * |
| 4236 | * If !enable_unrestricted_guest, the CPU is always running |
| 4237 | * with CR0.PG=1 and CR4 needs to be modified. |
| 4238 | * If enable_unrestricted_guest, the CPU automatically |
| 4239 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4240 | */ |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 4241 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4242 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4243 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 4244 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4245 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4246 | } |
| 4247 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4248 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 4249 | struct kvm_segment *var, int seg) |
| 4250 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4251 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4252 | u32 ar; |
| 4253 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 4254 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4255 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4256 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4257 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4258 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 4259 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 4260 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 4261 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4262 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4263 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 4264 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 4265 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 4266 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 4267 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4268 | var->type = ar & 15; |
| 4269 | var->s = (ar >> 4) & 1; |
| 4270 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 4271 | /* |
| 4272 | * Some userspaces do not preserve unusable property. Since usable |
| 4273 | * segment has to be present according to VMX spec we can use present |
| 4274 | * property to amend userspace bug by making unusable segment always |
| 4275 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 4276 | * segment as unusable. |
| 4277 | */ |
| 4278 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4279 | var->avl = (ar >> 12) & 1; |
| 4280 | var->l = (ar >> 13) & 1; |
| 4281 | var->db = (ar >> 14) & 1; |
| 4282 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4283 | } |
| 4284 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4285 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 4286 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4287 | struct kvm_segment s; |
| 4288 | |
| 4289 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 4290 | vmx_get_segment(vcpu, &s, seg); |
| 4291 | return s.base; |
| 4292 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4293 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4294 | } |
| 4295 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 4296 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4297 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 4298 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4299 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 4300 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4301 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 4302 | else { |
| 4303 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4304 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 4305 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 4306 | } |
| 4307 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4308 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4309 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4310 | u32 ar; |
| 4311 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 4312 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4313 | ar = 1 << 16; |
| 4314 | else { |
| 4315 | ar = var->type & 15; |
| 4316 | ar |= (var->s & 1) << 4; |
| 4317 | ar |= (var->dpl & 3) << 5; |
| 4318 | ar |= (var->present & 1) << 7; |
| 4319 | ar |= (var->avl & 1) << 12; |
| 4320 | ar |= (var->l & 1) << 13; |
| 4321 | ar |= (var->db & 1) << 14; |
| 4322 | ar |= (var->g & 1) << 15; |
| 4323 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4324 | |
| 4325 | return ar; |
| 4326 | } |
| 4327 | |
| 4328 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 4329 | struct kvm_segment *var, int seg) |
| 4330 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4331 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4332 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4333 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4334 | vmx_segment_cache_clear(vmx); |
| 4335 | |
Gleb Natapov | 1ecd50a9 | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 4336 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 4337 | vmx->rmode.segs[seg] = *var; |
| 4338 | if (seg == VCPU_SREG_TR) |
| 4339 | vmcs_write16(sf->selector, var->selector); |
| 4340 | else if (var->s) |
| 4341 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4342 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4343 | } |
Gleb Natapov | 1ecd50a9 | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 4344 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4345 | vmcs_writel(sf->base, var->base); |
| 4346 | vmcs_write32(sf->limit, var->limit); |
| 4347 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4348 | |
| 4349 | /* |
| 4350 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 4351 | * qemu binaries. |
| 4352 | * IA32 arch specifies that at the time of processor reset the |
| 4353 | * "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] | 4354 | * 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] | 4355 | * state vmexit when "unrestricted guest" mode is turned on. |
| 4356 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 4357 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 4358 | * kvm hack. |
| 4359 | */ |
| 4360 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 4361 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4362 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 4363 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4364 | |
| 4365 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 4366 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4367 | } |
| 4368 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4369 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 4370 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4371 | 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] | 4372 | |
| 4373 | *db = (ar >> 14) & 1; |
| 4374 | *l = (ar >> 13) & 1; |
| 4375 | } |
| 4376 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4377 | 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] | 4378 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4379 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 4380 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4381 | } |
| 4382 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4383 | 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] | 4384 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4385 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 4386 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4387 | } |
| 4388 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4389 | 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] | 4390 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4391 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 4392 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4393 | } |
| 4394 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4395 | 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] | 4396 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4397 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 4398 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4399 | } |
| 4400 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4401 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 4402 | { |
| 4403 | struct kvm_segment var; |
| 4404 | u32 ar; |
| 4405 | |
| 4406 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 4407 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 4408 | if (seg == VCPU_SREG_CS) |
| 4409 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4410 | ar = vmx_segment_access_rights(&var); |
| 4411 | |
| 4412 | if (var.base != (var.selector << 4)) |
| 4413 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 4414 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4415 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 4416 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4417 | return false; |
| 4418 | |
| 4419 | return true; |
| 4420 | } |
| 4421 | |
| 4422 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 4423 | { |
| 4424 | struct kvm_segment cs; |
| 4425 | unsigned int cs_rpl; |
| 4426 | |
| 4427 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4428 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4429 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4430 | if (cs.unusable) |
| 4431 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4432 | 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] | 4433 | return false; |
| 4434 | if (!cs.s) |
| 4435 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4436 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4437 | if (cs.dpl > cs_rpl) |
| 4438 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4439 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4440 | if (cs.dpl != cs_rpl) |
| 4441 | return false; |
| 4442 | } |
| 4443 | if (!cs.present) |
| 4444 | return false; |
| 4445 | |
| 4446 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 4447 | return true; |
| 4448 | } |
| 4449 | |
| 4450 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 4451 | { |
| 4452 | struct kvm_segment ss; |
| 4453 | unsigned int ss_rpl; |
| 4454 | |
| 4455 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4456 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4457 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4458 | if (ss.unusable) |
| 4459 | return true; |
| 4460 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4461 | return false; |
| 4462 | if (!ss.s) |
| 4463 | return false; |
| 4464 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 4465 | return false; |
| 4466 | if (!ss.present) |
| 4467 | return false; |
| 4468 | |
| 4469 | return true; |
| 4470 | } |
| 4471 | |
| 4472 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 4473 | { |
| 4474 | struct kvm_segment var; |
| 4475 | unsigned int rpl; |
| 4476 | |
| 4477 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4478 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4479 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4480 | if (var.unusable) |
| 4481 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4482 | if (!var.s) |
| 4483 | return false; |
| 4484 | if (!var.present) |
| 4485 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4486 | 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] | 4487 | if (var.dpl < rpl) /* DPL < RPL */ |
| 4488 | return false; |
| 4489 | } |
| 4490 | |
| 4491 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 4492 | * rights flags |
| 4493 | */ |
| 4494 | return true; |
| 4495 | } |
| 4496 | |
| 4497 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 4498 | { |
| 4499 | struct kvm_segment tr; |
| 4500 | |
| 4501 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 4502 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4503 | if (tr.unusable) |
| 4504 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4505 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4506 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4507 | 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] | 4508 | return false; |
| 4509 | if (!tr.present) |
| 4510 | return false; |
| 4511 | |
| 4512 | return true; |
| 4513 | } |
| 4514 | |
| 4515 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 4516 | { |
| 4517 | struct kvm_segment ldtr; |
| 4518 | |
| 4519 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 4520 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4521 | if (ldtr.unusable) |
| 4522 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4523 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4524 | return false; |
| 4525 | if (ldtr.type != 2) |
| 4526 | return false; |
| 4527 | if (!ldtr.present) |
| 4528 | return false; |
| 4529 | |
| 4530 | return true; |
| 4531 | } |
| 4532 | |
| 4533 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 4534 | { |
| 4535 | struct kvm_segment cs, ss; |
| 4536 | |
| 4537 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 4538 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 4539 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4540 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 4541 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4542 | } |
| 4543 | |
| 4544 | /* |
| 4545 | * Check if guest state is valid. Returns true if valid, false if |
| 4546 | * not. |
| 4547 | * We assume that registers are always usable |
| 4548 | */ |
| 4549 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 4550 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 4551 | if (enable_unrestricted_guest) |
| 4552 | return true; |
| 4553 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4554 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 4555 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4556 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 4557 | return false; |
| 4558 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 4559 | return false; |
| 4560 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 4561 | return false; |
| 4562 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 4563 | return false; |
| 4564 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 4565 | return false; |
| 4566 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 4567 | return false; |
| 4568 | } else { |
| 4569 | /* protected mode guest state checks */ |
| 4570 | if (!cs_ss_rpl_check(vcpu)) |
| 4571 | return false; |
| 4572 | if (!code_segment_valid(vcpu)) |
| 4573 | return false; |
| 4574 | if (!stack_segment_valid(vcpu)) |
| 4575 | return false; |
| 4576 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 4577 | return false; |
| 4578 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 4579 | return false; |
| 4580 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 4581 | return false; |
| 4582 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 4583 | return false; |
| 4584 | if (!tr_valid(vcpu)) |
| 4585 | return false; |
| 4586 | if (!ldtr_valid(vcpu)) |
| 4587 | return false; |
| 4588 | } |
| 4589 | /* TODO: |
| 4590 | * - Add checks on RIP |
| 4591 | * - Add checks on RFLAGS |
| 4592 | */ |
| 4593 | |
| 4594 | return true; |
| 4595 | } |
| 4596 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 4597 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4598 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4599 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4600 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 4601 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4602 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4603 | idx = srcu_read_lock(&kvm->srcu); |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4604 | fn = kvm->arch.tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4605 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 4606 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4607 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4608 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 4609 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 4610 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4611 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4612 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4613 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 4614 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4615 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4616 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 4617 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4618 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4619 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4620 | r = kvm_write_guest_page(kvm, fn, &data, |
| 4621 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 4622 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4623 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4624 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 4625 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4626 | } |
| 4627 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4628 | static int init_rmode_identity_map(struct kvm *kvm) |
| 4629 | { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4630 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 4631 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4632 | u32 tmp; |
| 4633 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4634 | if (!enable_ept) |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4635 | return 0; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4636 | |
| 4637 | /* Protect kvm->arch.ept_identity_pagetable_done. */ |
| 4638 | mutex_lock(&kvm->slots_lock); |
| 4639 | |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4640 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4641 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4642 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4643 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4644 | |
| 4645 | r = alloc_identity_pagetable(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4646 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4647 | goto out2; |
| 4648 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4649 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4650 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 4651 | if (r < 0) |
| 4652 | goto out; |
| 4653 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 4654 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 4655 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 4656 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 4657 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 4658 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 4659 | if (r < 0) |
| 4660 | goto out; |
| 4661 | } |
| 4662 | kvm->arch.ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4663 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4664 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4665 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4666 | |
| 4667 | out2: |
| 4668 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4669 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4670 | } |
| 4671 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4672 | static void seg_setup(int seg) |
| 4673 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4674 | 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] | 4675 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4676 | |
| 4677 | vmcs_write16(sf->selector, 0); |
| 4678 | vmcs_writel(sf->base, 0); |
| 4679 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 4680 | ar = 0x93; |
| 4681 | if (seg == VCPU_SREG_CS) |
| 4682 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4683 | |
| 4684 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4685 | } |
| 4686 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4687 | static int alloc_apic_access_page(struct kvm *kvm) |
| 4688 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 4689 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4690 | int r = 0; |
| 4691 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4692 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 4693 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4694 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 4695 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 4696 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4697 | if (r) |
| 4698 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4699 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 4700 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 4701 | if (is_error_page(page)) { |
| 4702 | r = -EFAULT; |
| 4703 | goto out; |
| 4704 | } |
| 4705 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 4706 | /* |
| 4707 | * Do not pin the page in memory, so that memory hot-unplug |
| 4708 | * is able to migrate it. |
| 4709 | */ |
| 4710 | put_page(page); |
| 4711 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4712 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4713 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4714 | return r; |
| 4715 | } |
| 4716 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4717 | static int alloc_identity_pagetable(struct kvm *kvm) |
| 4718 | { |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4719 | /* Called with kvm->slots_lock held. */ |
| 4720 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4721 | int r = 0; |
| 4722 | |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4723 | BUG_ON(kvm->arch.ept_identity_pagetable_done); |
| 4724 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 4725 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
| 4726 | kvm->arch.ept_identity_map_addr, PAGE_SIZE); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4727 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4728 | return r; |
| 4729 | } |
| 4730 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4731 | static int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4732 | { |
| 4733 | int vpid; |
| 4734 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 4735 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4736 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4737 | spin_lock(&vmx_vpid_lock); |
| 4738 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4739 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4740 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4741 | else |
| 4742 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4743 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4744 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4745 | } |
| 4746 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4747 | static void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4748 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4749 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4750 | return; |
| 4751 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4752 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4753 | spin_unlock(&vmx_vpid_lock); |
| 4754 | } |
| 4755 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4756 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 4757 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4758 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4759 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4760 | |
| 4761 | if (!cpu_has_vmx_msr_bitmap()) |
| 4762 | return; |
| 4763 | |
| 4764 | /* |
| 4765 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4766 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4767 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4768 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4769 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4770 | if (type & MSR_TYPE_R) |
| 4771 | /* read-low */ |
| 4772 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 4773 | |
| 4774 | if (type & MSR_TYPE_W) |
| 4775 | /* write-low */ |
| 4776 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 4777 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4778 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4779 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4780 | if (type & MSR_TYPE_R) |
| 4781 | /* read-high */ |
| 4782 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 4783 | |
| 4784 | if (type & MSR_TYPE_W) |
| 4785 | /* write-high */ |
| 4786 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 4787 | |
| 4788 | } |
| 4789 | } |
| 4790 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4791 | static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
| 4792 | u32 msr, int type) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4793 | { |
| 4794 | int f = sizeof(unsigned long); |
| 4795 | |
| 4796 | if (!cpu_has_vmx_msr_bitmap()) |
| 4797 | return; |
| 4798 | |
| 4799 | /* |
| 4800 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4801 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4802 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4803 | */ |
| 4804 | if (msr <= 0x1fff) { |
| 4805 | if (type & MSR_TYPE_R) |
| 4806 | /* read-low */ |
| 4807 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 4808 | |
| 4809 | if (type & MSR_TYPE_W) |
| 4810 | /* write-low */ |
| 4811 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 4812 | |
| 4813 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4814 | msr &= 0x1fff; |
| 4815 | if (type & MSR_TYPE_R) |
| 4816 | /* read-high */ |
| 4817 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 4818 | |
| 4819 | if (type & MSR_TYPE_W) |
| 4820 | /* write-high */ |
| 4821 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 4822 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4823 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4824 | } |
| 4825 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4826 | static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap, |
| 4827 | u32 msr, int type, bool value) |
| 4828 | { |
| 4829 | if (value) |
| 4830 | vmx_enable_intercept_for_msr(msr_bitmap, msr, type); |
| 4831 | else |
| 4832 | vmx_disable_intercept_for_msr(msr_bitmap, msr, type); |
| 4833 | } |
| 4834 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 4835 | /* |
| 4836 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 4837 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 4838 | */ |
| 4839 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 4840 | unsigned long *msr_bitmap_nested, |
| 4841 | u32 msr, int type) |
| 4842 | { |
| 4843 | int f = sizeof(unsigned long); |
| 4844 | |
| 4845 | if (!cpu_has_vmx_msr_bitmap()) { |
| 4846 | WARN_ON(1); |
| 4847 | return; |
| 4848 | } |
| 4849 | |
| 4850 | /* |
| 4851 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4852 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4853 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4854 | */ |
| 4855 | if (msr <= 0x1fff) { |
| 4856 | if (type & MSR_TYPE_R && |
| 4857 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 4858 | /* read-low */ |
| 4859 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 4860 | |
| 4861 | if (type & MSR_TYPE_W && |
| 4862 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 4863 | /* write-low */ |
| 4864 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 4865 | |
| 4866 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4867 | msr &= 0x1fff; |
| 4868 | if (type & MSR_TYPE_R && |
| 4869 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 4870 | /* read-high */ |
| 4871 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 4872 | |
| 4873 | if (type & MSR_TYPE_W && |
| 4874 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 4875 | /* write-high */ |
| 4876 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 4877 | |
| 4878 | } |
| 4879 | } |
| 4880 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4881 | static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4882 | { |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4883 | u8 mode = 0; |
| 4884 | |
| 4885 | if (cpu_has_secondary_exec_ctrls() && |
| 4886 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 4887 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
| 4888 | mode |= MSR_BITMAP_MODE_X2APIC; |
| 4889 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) |
| 4890 | mode |= MSR_BITMAP_MODE_X2APIC_APICV; |
| 4891 | } |
| 4892 | |
| 4893 | if (is_long_mode(vcpu)) |
| 4894 | mode |= MSR_BITMAP_MODE_LM; |
| 4895 | |
| 4896 | return mode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4897 | } |
| 4898 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4899 | #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4)) |
| 4900 | |
| 4901 | static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap, |
| 4902 | u8 mode) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4903 | { |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4904 | int msr; |
| 4905 | |
| 4906 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 4907 | unsigned word = msr / BITS_PER_LONG; |
| 4908 | msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; |
| 4909 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
| 4910 | } |
| 4911 | |
| 4912 | if (mode & MSR_BITMAP_MODE_X2APIC) { |
| 4913 | /* |
| 4914 | * TPR reads and writes can be virtualized even if virtual interrupt |
| 4915 | * delivery is not in use. |
| 4916 | */ |
| 4917 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW); |
| 4918 | if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { |
| 4919 | vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R); |
| 4920 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); |
| 4921 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); |
| 4922 | } |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4923 | } |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4924 | } |
| 4925 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4926 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4927 | { |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4928 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4929 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 4930 | u8 mode = vmx_msr_bitmap_mode(vcpu); |
| 4931 | u8 changed = mode ^ vmx->msr_bitmap_mode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4932 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4933 | if (!changed) |
| 4934 | return; |
| 4935 | |
| 4936 | vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW, |
| 4937 | !(mode & MSR_BITMAP_MODE_LM)); |
| 4938 | |
| 4939 | if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) |
| 4940 | vmx_update_msr_bitmap_x2apic(msr_bitmap, mode); |
| 4941 | |
| 4942 | vmx->msr_bitmap_mode = mode; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4943 | } |
| 4944 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4945 | static bool vmx_get_enable_apicv(void) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 4946 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4947 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 4948 | } |
| 4949 | |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 4950 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
| 4951 | { |
| 4952 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4953 | gfn_t gfn; |
| 4954 | |
| 4955 | /* |
| 4956 | * Don't need to mark the APIC access page dirty; it is never |
| 4957 | * written to by the CPU during APIC virtualization. |
| 4958 | */ |
| 4959 | |
| 4960 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 4961 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 4962 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 4963 | } |
| 4964 | |
| 4965 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 4966 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 4967 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 4968 | } |
| 4969 | } |
| 4970 | |
| 4971 | |
David Hildenbrand | 1edccf2 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 4972 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4973 | { |
| 4974 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4975 | int max_irr; |
| 4976 | void *vapic_page; |
| 4977 | u16 status; |
| 4978 | |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 4979 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
| 4980 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4981 | |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 4982 | vmx->nested.pi_pending = false; |
| 4983 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 4984 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4985 | |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 4986 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 4987 | if (max_irr != 256) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4988 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 4989 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page); |
| 4990 | kunmap(vmx->nested.virtual_apic_page); |
| 4991 | |
| 4992 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 4993 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 4994 | status &= ~0xff; |
| 4995 | status |= (u8)max_irr; |
| 4996 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 4997 | } |
| 4998 | } |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 4999 | |
| 5000 | nested_mark_vmcs12_pages_dirty(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5001 | } |
| 5002 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5003 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu) |
| 5004 | { |
| 5005 | #ifdef CONFIG_SMP |
| 5006 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5007 | /* |
Haozhong Zhang | 3ffbe62 | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 5008 | * The vector of interrupt to be delivered to vcpu had |
| 5009 | * been set in PIR before this function. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5010 | * |
Haozhong Zhang | 3ffbe62 | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 5011 | * Following cases will be reached in this block, and |
| 5012 | * we always send a notification event in all cases as |
| 5013 | * explained below. |
| 5014 | * |
| 5015 | * Case 1: vcpu keeps in non-root mode. Sending a |
| 5016 | * notification event posts the interrupt to vcpu. |
| 5017 | * |
| 5018 | * Case 2: vcpu exits to root mode and is still |
| 5019 | * runnable. PIR will be synced to vIRR before the |
| 5020 | * next vcpu entry. Sending a notification event in |
| 5021 | * this case has no effect, as vcpu is not in root |
| 5022 | * mode. |
| 5023 | * |
| 5024 | * Case 3: vcpu exits to root mode and is blocked. |
| 5025 | * vcpu_block() has already synced PIR to vIRR and |
| 5026 | * never blocks vcpu if vIRR is not cleared. Therefore, |
| 5027 | * a blocked vcpu here does not wait for any requested |
| 5028 | * interrupts in PIR, and sending a notification event |
| 5029 | * which has no effect is safe here. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5030 | */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5031 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5032 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), |
| 5033 | POSTED_INTR_VECTOR); |
| 5034 | return true; |
| 5035 | } |
| 5036 | #endif |
| 5037 | return false; |
| 5038 | } |
| 5039 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5040 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5041 | int vector) |
| 5042 | { |
| 5043 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5044 | |
| 5045 | if (is_guest_mode(vcpu) && |
| 5046 | vector == vmx->nested.posted_intr_nv) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5047 | /* |
| 5048 | * If a posted intr is not recognized by hardware, |
| 5049 | * we will accomplish it in the next vmentry. |
| 5050 | */ |
| 5051 | vmx->nested.pi_pending = true; |
| 5052 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Liran Alon | ba88289 | 2017-11-09 20:27:20 +0200 | [diff] [blame] | 5053 | /* the PIR and ON have been set by L1. */ |
| 5054 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu)) |
| 5055 | kvm_vcpu_kick(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5056 | return 0; |
| 5057 | } |
| 5058 | return -1; |
| 5059 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5060 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5061 | * Send interrupt to vcpu via posted interrupt way. |
| 5062 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 5063 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 5064 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 5065 | * interrupt from PIR in next vmentry. |
| 5066 | */ |
| 5067 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 5068 | { |
| 5069 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5070 | int r; |
| 5071 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5072 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 5073 | if (!r) |
| 5074 | return; |
| 5075 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5076 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 5077 | return; |
| 5078 | |
| 5079 | r = pi_test_and_set_on(&vmx->pi_desc); |
| 5080 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5081 | if (r || !kvm_vcpu_trigger_posted_interrupt(vcpu)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5082 | kvm_vcpu_kick(vcpu); |
| 5083 | } |
| 5084 | |
| 5085 | static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
| 5086 | { |
| 5087 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5088 | |
| 5089 | if (!pi_test_and_clear_on(&vmx->pi_desc)) |
| 5090 | return; |
| 5091 | |
| 5092 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir); |
| 5093 | } |
| 5094 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5095 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5096 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 5097 | * will not change in the lifetime of the guest. |
| 5098 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 5099 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 5100 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5101 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5102 | { |
| 5103 | u32 low32, high32; |
| 5104 | unsigned long tmpl; |
| 5105 | struct desc_ptr dt; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5106 | unsigned long cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5107 | |
Suresh Siddha | b1a74bf | 2012-09-20 11:01:49 -0700 | [diff] [blame] | 5108 | 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] | 5109 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 5110 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5111 | /* 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] | 5112 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5113 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
| 5114 | vmx->host_state.vmcs_host_cr4 = cr4; |
| 5115 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5116 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5117 | #ifdef CONFIG_X86_64 |
| 5118 | /* |
| 5119 | * Load null selectors, so we can avoid reloading them in |
| 5120 | * __vmx_load_host_state(), in case userspace uses the null selectors |
| 5121 | * too (the expected case). |
| 5122 | */ |
| 5123 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 5124 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 5125 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5126 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5127 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5128 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5129 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5130 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 5131 | |
| 5132 | native_store_idt(&dt); |
| 5133 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5134 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5135 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 5136 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5137 | |
| 5138 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 5139 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 5140 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 5141 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 5142 | |
| 5143 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 5144 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 5145 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 5146 | } |
| 5147 | } |
| 5148 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5149 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 5150 | { |
| 5151 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 5152 | if (enable_ept) |
| 5153 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 5154 | if (is_guest_mode(&vmx->vcpu)) |
| 5155 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 5156 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5157 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 5158 | } |
| 5159 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5160 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 5161 | { |
| 5162 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 5163 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5164 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5165 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 5166 | /* Enable the preemption timer dynamically */ |
| 5167 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5168 | return pin_based_exec_ctrl; |
| 5169 | } |
| 5170 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5171 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 5172 | { |
| 5173 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5174 | |
| 5175 | 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] | 5176 | if (cpu_has_secondary_exec_ctrls()) { |
| 5177 | if (kvm_vcpu_apicv_active(vcpu)) |
| 5178 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5179 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5180 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 5181 | else |
| 5182 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5183 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5184 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 5185 | } |
| 5186 | |
| 5187 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5188 | vmx_update_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5189 | } |
| 5190 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5191 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 5192 | { |
| 5193 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
Paolo Bonzini | d16c293 | 2014-02-21 10:36:37 +0100 | [diff] [blame] | 5194 | |
| 5195 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 5196 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 5197 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5198 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5199 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 5200 | #ifdef CONFIG_X86_64 |
| 5201 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 5202 | CPU_BASED_CR8_LOAD_EXITING; |
| 5203 | #endif |
| 5204 | } |
| 5205 | if (!enable_ept) |
| 5206 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 5207 | CPU_BASED_CR3_LOAD_EXITING | |
| 5208 | CPU_BASED_INVLPG_EXITING; |
| 5209 | return exec_control; |
| 5210 | } |
| 5211 | |
| 5212 | static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) |
| 5213 | { |
| 5214 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5215 | if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5216 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 5217 | if (vmx->vpid == 0) |
| 5218 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 5219 | if (!enable_ept) { |
| 5220 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 5221 | enable_unrestricted_guest = 0; |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 5222 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
| 5223 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5224 | } |
| 5225 | if (!enable_unrestricted_guest) |
| 5226 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 5227 | if (!ple_gap) |
| 5228 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5229 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5230 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5231 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5232 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 5233 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 5234 | (handle_vmptrld). |
| 5235 | We can NOT enable shadow_vmcs here because we don't have yet |
| 5236 | a current VMCS12 |
| 5237 | */ |
| 5238 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 5239 | |
| 5240 | if (!enable_pml) |
| 5241 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5242 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5243 | return exec_control; |
| 5244 | } |
| 5245 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5246 | static void ept_set_mmio_spte_mask(void) |
| 5247 | { |
| 5248 | /* |
| 5249 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 5250 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | 885032b | 2013-06-07 16:51:23 +0800 | [diff] [blame] | 5251 | * Also, magic bits (0x3ull << 62) is set to quickly identify mmio |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5252 | * spte. |
| 5253 | */ |
Xiao Guangrong | 885032b | 2013-06-07 16:51:23 +0800 | [diff] [blame] | 5254 | kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5255 | } |
| 5256 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5257 | #define VMX_XSS_EXIT_BITMAP 0 |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5258 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5259 | * Sets up the vmcs for emulated real mode. |
| 5260 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 5261 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5262 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 5263 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5264 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 5265 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5266 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5267 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5268 | /* I/O */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 5269 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
| 5270 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5271 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 5272 | if (enable_shadow_vmcs) { |
| 5273 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
| 5274 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 5275 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5276 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5277 | vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5278 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5279 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 5280 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5281 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5282 | 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] | 5283 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5284 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5285 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5286 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 5287 | if (cpu_has_secondary_exec_ctrls()) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5288 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 5289 | vmx_secondary_exec_control(vmx)); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 5290 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5291 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5292 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5293 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 5294 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 5295 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 5296 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 5297 | |
| 5298 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5299 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 5300 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5301 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5302 | } |
| 5303 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5304 | if (ple_gap) { |
| 5305 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 5306 | vmx->ple_window = ple_window; |
| 5307 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5308 | } |
| 5309 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 5310 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 5311 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5312 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 5313 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 5314 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 5315 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5316 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5317 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5318 | rdmsrl(MSR_FS_BASE, a); |
| 5319 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 5320 | rdmsrl(MSR_GS_BASE, a); |
| 5321 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 5322 | #else |
| 5323 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 5324 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 5325 | #endif |
| 5326 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 5327 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 5328 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Konrad Rzeszutek Wilk | 57e3ada | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 5329 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 5330 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Konrad Rzeszutek Wilk | 57e3ada | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 5331 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5332 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 5333 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 5334 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 5335 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 5336 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5337 | u32 index = vmx_msr_index[i]; |
| 5338 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5339 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5340 | |
| 5341 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 5342 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 5343 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 5344 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 5345 | vmx->guest_msrs[j].index = i; |
| 5346 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 5347 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5348 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5349 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5350 | |
KarimAllah Ahmed | 755502f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 5351 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
| 5352 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 5353 | |
| 5354 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5355 | |
| 5356 | /* 22.2.1, 20.8.1 */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 5357 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 5358 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5359 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5360 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5361 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5362 | if (vmx_xsaves_supported()) |
| 5363 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 5364 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 5365 | if (enable_pml) { |
| 5366 | ASSERT(vmx->pml_pg); |
| 5367 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 5368 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 5369 | } |
| 5370 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5371 | return 0; |
| 5372 | } |
| 5373 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5374 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5375 | { |
| 5376 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 5377 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5378 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5379 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5380 | vmx->rmode.vm86_active = 0; |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 5381 | vmx->spec_ctrl = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5382 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5383 | vmx->soft_vnmi_blocked = 0; |
| 5384 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5385 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5386 | kvm_set_cr8(vcpu, 0); |
| 5387 | |
| 5388 | if (!init_event) { |
| 5389 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 5390 | MSR_IA32_APICBASE_ENABLE; |
| 5391 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 5392 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 5393 | apic_base_msr.host_initiated = true; |
| 5394 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 5395 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5396 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5397 | vmx_segment_cache_clear(vmx); |
| 5398 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 5399 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 5400 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 5401 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5402 | |
| 5403 | seg_setup(VCPU_SREG_DS); |
| 5404 | seg_setup(VCPU_SREG_ES); |
| 5405 | seg_setup(VCPU_SREG_FS); |
| 5406 | seg_setup(VCPU_SREG_GS); |
| 5407 | seg_setup(VCPU_SREG_SS); |
| 5408 | |
| 5409 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 5410 | vmcs_writel(GUEST_TR_BASE, 0); |
| 5411 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 5412 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 5413 | |
| 5414 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 5415 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 5416 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 5417 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 5418 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5419 | if (!init_event) { |
| 5420 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 5421 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 5422 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 5423 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 5424 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5425 | |
Wanpeng Li | 5c0b19b | 2017-11-20 14:52:21 -0800 | [diff] [blame] | 5426 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 5427 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5428 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5429 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 5430 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 5431 | |
| 5432 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 5433 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 5434 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 5435 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5436 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 5437 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5438 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5439 | setup_msrs(vmx); |
| 5440 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5441 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 5442 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5443 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5444 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5445 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5446 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5447 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5448 | vmcs_write32(TPR_THRESHOLD, 0); |
| 5449 | } |
| 5450 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 5451 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5452 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5453 | if (kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5454 | memset(&vmx->pi_desc, 0, sizeof(struct pi_desc)); |
| 5455 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5456 | if (vmx->vpid != 0) |
| 5457 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 5458 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5459 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5460 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 5461 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5462 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 5463 | vmx_set_efer(vcpu, 0); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5464 | vmx_fpu_activate(vcpu); |
| 5465 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5466 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 5467 | vpid_sync_context(vmx->vpid); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5468 | } |
| 5469 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 5470 | /* |
| 5471 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 5472 | * For most existing hypervisors, this will always return true. |
| 5473 | */ |
| 5474 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 5475 | { |
| 5476 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 5477 | PIN_BASED_EXT_INTR_MASK; |
| 5478 | } |
| 5479 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 5480 | /* |
| 5481 | * In nested virtualization, check if L1 has set |
| 5482 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 5483 | */ |
| 5484 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 5485 | { |
| 5486 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 5487 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 5488 | } |
| 5489 | |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 5490 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 5491 | { |
| 5492 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 5493 | PIN_BASED_NMI_EXITING; |
| 5494 | } |
| 5495 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5496 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5497 | { |
| 5498 | u32 cpu_based_vm_exec_control; |
Jan Kiszka | 730dca4 | 2013-04-28 10:50:52 +0200 | [diff] [blame] | 5499 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5500 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5501 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 5502 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 5503 | } |
| 5504 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5505 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5506 | { |
| 5507 | u32 cpu_based_vm_exec_control; |
| 5508 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5509 | if (!cpu_has_virtual_nmis() || |
| 5510 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
| 5511 | enable_irq_window(vcpu); |
| 5512 | return; |
| 5513 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 5514 | |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5515 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5516 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING; |
| 5517 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 5518 | } |
| 5519 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5520 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5521 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 5522 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5523 | uint32_t intr; |
| 5524 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 5525 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5526 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 5527 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 5528 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5529 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 5530 | int inc_eip = 0; |
| 5531 | if (vcpu->arch.interrupt.soft) |
| 5532 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 5533 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 5534 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5535 | return; |
| 5536 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5537 | intr = irq | INTR_INFO_VALID_MASK; |
| 5538 | if (vcpu->arch.interrupt.soft) { |
| 5539 | intr |= INTR_TYPE_SOFT_INTR; |
| 5540 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 5541 | vmx->vcpu.arch.event_exit_inst_len); |
| 5542 | } else |
| 5543 | intr |= INTR_TYPE_EXT_INTR; |
| 5544 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5545 | } |
| 5546 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5547 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 5548 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 5549 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5550 | |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 5551 | if (!is_guest_mode(vcpu)) { |
| 5552 | if (!cpu_has_virtual_nmis()) { |
| 5553 | /* |
| 5554 | * Tracking the NMI-blocked state in software is built upon |
| 5555 | * finding the next open IRQ window. This, in turn, depends on |
| 5556 | * well-behaving guests: They have to keep IRQs disabled at |
| 5557 | * least as long as the NMI handler runs. Otherwise we may |
| 5558 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 5559 | * highly unlikely, we can live with the residual risk. |
| 5560 | */ |
| 5561 | vmx->soft_vnmi_blocked = 1; |
| 5562 | vmx->vnmi_blocked_time = 0; |
| 5563 | } |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 5564 | |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 5565 | ++vcpu->stat.nmi_injections; |
| 5566 | vmx->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5567 | } |
| 5568 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5569 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 5570 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 5571 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 5572 | return; |
| 5573 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 5574 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5575 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 5576 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5577 | } |
| 5578 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5579 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 5580 | { |
| 5581 | if (!cpu_has_virtual_nmis()) |
| 5582 | return to_vmx(vcpu)->soft_vnmi_blocked; |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 5583 | if (to_vmx(vcpu)->nmi_known_unmasked) |
| 5584 | return false; |
Avi Kivity | c332c83 | 2010-05-04 12:24:12 +0300 | [diff] [blame] | 5585 | return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5586 | } |
| 5587 | |
| 5588 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 5589 | { |
| 5590 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5591 | |
| 5592 | if (!cpu_has_virtual_nmis()) { |
| 5593 | if (vmx->soft_vnmi_blocked != masked) { |
| 5594 | vmx->soft_vnmi_blocked = masked; |
| 5595 | vmx->vnmi_blocked_time = 0; |
| 5596 | } |
| 5597 | } else { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 5598 | vmx->nmi_known_unmasked = !masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5599 | if (masked) |
| 5600 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5601 | GUEST_INTR_STATE_NMI); |
| 5602 | else |
| 5603 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5604 | GUEST_INTR_STATE_NMI); |
| 5605 | } |
| 5606 | } |
| 5607 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 5608 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 5609 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 5610 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 5611 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 5612 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 5613 | if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked) |
| 5614 | return 0; |
| 5615 | |
| 5616 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 5617 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 5618 | | GUEST_INTR_STATE_NMI)); |
| 5619 | } |
| 5620 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5621 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 5622 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 5623 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 5624 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 5625 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 5626 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5627 | } |
| 5628 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5629 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 5630 | { |
| 5631 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5632 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 5633 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 5634 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5635 | if (ret) |
| 5636 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 5637 | kvm->arch.tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5638 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5639 | } |
| 5640 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5641 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5642 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5643 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5644 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 5645 | /* |
| 5646 | * Update instruction length as we may reinject the exception |
| 5647 | * from user space while in guest debugging mode. |
| 5648 | */ |
| 5649 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 5650 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5651 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5652 | return false; |
| 5653 | /* fall through */ |
| 5654 | case DB_VECTOR: |
| 5655 | if (vcpu->guest_debug & |
| 5656 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 5657 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5658 | /* fall through */ |
| 5659 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5660 | case OF_VECTOR: |
| 5661 | case BR_VECTOR: |
| 5662 | case UD_VECTOR: |
| 5663 | case DF_VECTOR: |
| 5664 | case SS_VECTOR: |
| 5665 | case GP_VECTOR: |
| 5666 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5667 | return true; |
| 5668 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5669 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5670 | return false; |
| 5671 | } |
| 5672 | |
| 5673 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 5674 | int vec, u32 err_code) |
| 5675 | { |
| 5676 | /* |
| 5677 | * Instruction with address size override prefix opcode 0x67 |
| 5678 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 5679 | */ |
| 5680 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
| 5681 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
| 5682 | if (vcpu->arch.halt_request) { |
| 5683 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 5684 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5685 | } |
| 5686 | return 1; |
| 5687 | } |
| 5688 | return 0; |
| 5689 | } |
| 5690 | |
| 5691 | /* |
| 5692 | * Forward all other exceptions that are valid in real mode. |
| 5693 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 5694 | * the required debugging infrastructure rework. |
| 5695 | */ |
| 5696 | kvm_queue_exception(vcpu, vec); |
| 5697 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5698 | } |
| 5699 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5700 | /* |
| 5701 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 5702 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 5703 | * We pass a fake environment to the machine check handler because we want |
| 5704 | * the guest to be always treated like user space, no matter what context |
| 5705 | * it used internally. |
| 5706 | */ |
| 5707 | static void kvm_machine_check(void) |
| 5708 | { |
| 5709 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 5710 | struct pt_regs regs = { |
| 5711 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 5712 | .flags = X86_EFLAGS_IF, |
| 5713 | }; |
| 5714 | |
| 5715 | do_machine_check(®s, 0); |
| 5716 | #endif |
| 5717 | } |
| 5718 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5719 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5720 | { |
| 5721 | /* already handled by vcpu_run */ |
| 5722 | return 1; |
| 5723 | } |
| 5724 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5725 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5726 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5727 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5728 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5729 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5730 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5731 | u32 vect_info; |
| 5732 | enum emulation_result er; |
| 5733 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5734 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 5735 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5736 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5737 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5738 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5739 | |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 5740 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 5741 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 5742 | |
| 5743 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 5744 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 5745 | return 1; |
| 5746 | } |
| 5747 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5748 | if (is_invalid_opcode(intr_info)) { |
Jan Kiszka | ae1f576 | 2015-03-09 20:56:43 +0100 | [diff] [blame] | 5749 | if (is_guest_mode(vcpu)) { |
| 5750 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5751 | return 1; |
| 5752 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5753 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
Liran Alon | c0a4c22 | 2017-11-05 16:56:32 +0200 | [diff] [blame] | 5754 | if (er == EMULATE_USER_EXIT) |
| 5755 | return 0; |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5756 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 5757 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5758 | return 1; |
| 5759 | } |
| 5760 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5761 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 5762 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5763 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5764 | |
| 5765 | /* |
| 5766 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 5767 | * MMIO, it is better to report an internal error. |
| 5768 | * See the comments in vmx_handle_exit. |
| 5769 | */ |
| 5770 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 5771 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 5772 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 5773 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 5774 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5775 | vcpu->run->internal.data[0] = vect_info; |
| 5776 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 5777 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5778 | return 0; |
| 5779 | } |
| 5780 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5781 | if (is_page_fault(intr_info)) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5782 | /* EPT won't cause page fault directly */ |
Julia Lawall | cf3ace7 | 2011-08-02 12:34:57 +0200 | [diff] [blame] | 5783 | BUG_ON(enable_ept); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5784 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5785 | trace_kvm_page_fault(cr2, error_code); |
Paolo Bonzini | b3dc63c | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 5786 | vcpu->arch.l1tf_flush_l1d = true; |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5787 | |
Gleb Natapov | 3298b75 | 2009-05-11 13:35:46 +0300 | [diff] [blame] | 5788 | if (kvm_event_needs_reinjection(vcpu)) |
Avi Kivity | 577bdc4 | 2008-07-19 08:57:05 +0300 | [diff] [blame] | 5789 | kvm_mmu_unprotect_page_virt(vcpu, cr2); |
Andre Przywara | dc25e89 | 2010-12-21 11:12:07 +0100 | [diff] [blame] | 5790 | return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5791 | } |
| 5792 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5793 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5794 | |
| 5795 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 5796 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 5797 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5798 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 5799 | case AC_VECTOR: |
| 5800 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 5801 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5802 | case DB_VECTOR: |
| 5803 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 5804 | if (!(vcpu->guest_debug & |
| 5805 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 5806 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 5807 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Linus Torvalds | 587da2b | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 5808 | if (is_icebp(intr_info)) |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 5809 | skip_emulated_instruction(vcpu); |
| 5810 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5811 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 5812 | return 1; |
| 5813 | } |
| 5814 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 5815 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 5816 | /* fall through */ |
| 5817 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 5818 | /* |
| 5819 | * Update instruction length as we may reinject #BP from |
| 5820 | * user space while in guest debugging mode. Reading it for |
| 5821 | * #DB as well causes no harm, it is not used in that case. |
| 5822 | */ |
| 5823 | vmx->vcpu.arch.event_exit_inst_len = |
| 5824 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5825 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 5826 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5827 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 5828 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5829 | break; |
| 5830 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5831 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 5832 | kvm_run->ex.exception = ex_no; |
| 5833 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5834 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5835 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5836 | return 0; |
| 5837 | } |
| 5838 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5839 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5840 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 5841 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5842 | return 1; |
| 5843 | } |
| 5844 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5845 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5846 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5847 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5848 | return 0; |
| 5849 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5850 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5851 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5852 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5853 | unsigned long exit_qualification; |
Jan Kiszka | 34c33d1 | 2009-02-08 13:28:15 +0100 | [diff] [blame] | 5854 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 5855 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5856 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5857 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 5858 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 5859 | in = (exit_qualification & 8) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 5860 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5861 | ++vcpu->stat.io_exits; |
| 5862 | |
| 5863 | if (string || in) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5864 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5865 | |
| 5866 | port = exit_qualification >> 16; |
| 5867 | size = (exit_qualification & 7) + 1; |
Guillaume Thouvenin | e93f36b | 2008-10-28 10:51:30 +0100 | [diff] [blame] | 5868 | skip_emulated_instruction(vcpu); |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5869 | |
| 5870 | return kvm_fast_pio_out(vcpu, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5871 | } |
| 5872 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5873 | static void |
| 5874 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 5875 | { |
| 5876 | /* |
| 5877 | * Patch in the VMCALL instruction: |
| 5878 | */ |
| 5879 | hypercall[0] = 0x0f; |
| 5880 | hypercall[1] = 0x01; |
| 5881 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5882 | } |
| 5883 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5884 | static bool nested_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5885 | { |
| 5886 | unsigned long always_on = VMXON_CR0_ALWAYSON; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5887 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5888 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5889 | if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high & |
Jan Kiszka | 92fbc7b | 2013-08-08 16:26:33 +0200 | [diff] [blame] | 5890 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
| 5891 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 5892 | always_on &= ~(X86_CR0_PE | X86_CR0_PG); |
| 5893 | return (val & always_on) == always_on; |
| 5894 | } |
| 5895 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 5896 | /* 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] | 5897 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 5898 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5899 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5900 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5901 | unsigned long orig_val = val; |
| 5902 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5903 | /* |
| 5904 | * We get here when L2 changed cr0 in a way that did not change |
| 5905 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5906 | * but did change L0 shadowed bits. So we first calculate the |
| 5907 | * effective cr0 value that L1 would like to write into the |
| 5908 | * hardware. It consists of the L2-owned bits from the new |
| 5909 | * 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] | 5910 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5911 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 5912 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 5913 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 5914 | if (!nested_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5915 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5916 | |
| 5917 | if (kvm_set_cr0(vcpu, val)) |
| 5918 | return 1; |
| 5919 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5920 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5921 | } else { |
| 5922 | if (to_vmx(vcpu)->nested.vmxon && |
| 5923 | ((val & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON)) |
| 5924 | return 1; |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5925 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5926 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5927 | } |
| 5928 | |
| 5929 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 5930 | { |
| 5931 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5932 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5933 | unsigned long orig_val = val; |
| 5934 | |
| 5935 | /* analogously to handle_set_cr0 */ |
| 5936 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 5937 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 5938 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5939 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5940 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5941 | return 0; |
| 5942 | } else |
| 5943 | return kvm_set_cr4(vcpu, val); |
| 5944 | } |
| 5945 | |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 5946 | /* called to set cr0 as appropriate for clts instruction exit. */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5947 | static void handle_clts(struct kvm_vcpu *vcpu) |
| 5948 | { |
| 5949 | if (is_guest_mode(vcpu)) { |
| 5950 | /* |
| 5951 | * We get here when L2 did CLTS, and L1 didn't shadow CR0.TS |
| 5952 | * but we did (!fpu_active). We need to keep GUEST_CR0.TS on, |
| 5953 | * just pretend it's off (also in arch.cr0 for fpu_activate). |
| 5954 | */ |
| 5955 | vmcs_writel(CR0_READ_SHADOW, |
| 5956 | vmcs_readl(CR0_READ_SHADOW) & ~X86_CR0_TS); |
| 5957 | vcpu->arch.cr0 &= ~X86_CR0_TS; |
| 5958 | } else |
| 5959 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
| 5960 | } |
| 5961 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5962 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5963 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5964 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5965 | int cr; |
| 5966 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 5967 | int err; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5968 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5969 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5970 | cr = exit_qualification & 15; |
| 5971 | reg = (exit_qualification >> 8) & 15; |
| 5972 | switch ((exit_qualification >> 4) & 3) { |
| 5973 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 5974 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5975 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5976 | switch (cr) { |
| 5977 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5978 | err = handle_set_cr0(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5979 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5980 | return 1; |
| 5981 | case 3: |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 5982 | err = kvm_set_cr3(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5983 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5984 | return 1; |
| 5985 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5986 | err = handle_set_cr4(vcpu, val); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5987 | kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5988 | return 1; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5989 | case 8: { |
| 5990 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 5991 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 5992 | err = kvm_set_cr8(vcpu, cr8); |
Andre Przywara | db8fcef | 2010-12-21 11:12:01 +0100 | [diff] [blame] | 5993 | kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5994 | if (lapic_in_kernel(vcpu)) |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5995 | return 1; |
| 5996 | if (cr8_prev <= cr8) |
| 5997 | return 1; |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5998 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5999 | return 0; |
| 6000 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 6001 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6002 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 6003 | case 2: /* clts */ |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6004 | handle_clts(vcpu); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 6005 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 6006 | skip_emulated_instruction(vcpu); |
Avi Kivity | 6b52d18 | 2010-01-21 15:31:47 +0200 | [diff] [blame] | 6007 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 6008 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6009 | case 1: /*mov from cr*/ |
| 6010 | switch (cr) { |
| 6011 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 6012 | val = kvm_read_cr3(vcpu); |
| 6013 | kvm_register_write(vcpu, reg, val); |
| 6014 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6015 | skip_emulated_instruction(vcpu); |
| 6016 | return 1; |
| 6017 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6018 | val = kvm_get_cr8(vcpu); |
| 6019 | kvm_register_write(vcpu, reg, val); |
| 6020 | trace_kvm_cr_read(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6021 | skip_emulated_instruction(vcpu); |
| 6022 | return 1; |
| 6023 | } |
| 6024 | break; |
| 6025 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 6026 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 6027 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 6028 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6029 | |
| 6030 | skip_emulated_instruction(vcpu); |
| 6031 | return 1; |
| 6032 | default: |
| 6033 | break; |
| 6034 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6035 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 6036 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6037 | (int)(exit_qualification >> 4) & 3, cr); |
| 6038 | return 0; |
| 6039 | } |
| 6040 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6041 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6042 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6043 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 6044 | int dr, dr7, reg; |
| 6045 | |
| 6046 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6047 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 6048 | |
| 6049 | /* First, if DR does not exist, trigger UD */ |
| 6050 | if (!kvm_require_dr(vcpu, dr)) |
| 6051 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6052 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 6053 | /* 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] | 6054 | if (!kvm_require_cpl(vcpu, 0)) |
| 6055 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 6056 | dr7 = vmcs_readl(GUEST_DR7); |
| 6057 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6058 | /* |
| 6059 | * As the vm-exit takes precedence over the debug trap, we |
| 6060 | * need to emulate the latter, either for the host or the |
| 6061 | * guest debugging itself. |
| 6062 | */ |
| 6063 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6064 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 6065 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 6066 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6067 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 6068 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6069 | return 0; |
| 6070 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 6071 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 6072 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6073 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 6074 | return 1; |
| 6075 | } |
| 6076 | } |
| 6077 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6078 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 6079 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6080 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6081 | |
| 6082 | /* |
| 6083 | * No more DR vmexits; force a reload of the debug registers |
| 6084 | * and reenter on this instruction. The next vmexit will |
| 6085 | * retrieve the full state of the debug registers. |
| 6086 | */ |
| 6087 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 6088 | return 1; |
| 6089 | } |
| 6090 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6091 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 6092 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6093 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 6094 | |
| 6095 | if (kvm_get_dr(vcpu, dr, &val)) |
| 6096 | return 1; |
| 6097 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6098 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 6099 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 6100 | return 1; |
| 6101 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6102 | skip_emulated_instruction(vcpu); |
| 6103 | return 1; |
| 6104 | } |
| 6105 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 6106 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 6107 | { |
| 6108 | return vcpu->arch.dr6; |
| 6109 | } |
| 6110 | |
| 6111 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 6112 | { |
| 6113 | } |
| 6114 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6115 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 6116 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6117 | get_debugreg(vcpu->arch.db[0], 0); |
| 6118 | get_debugreg(vcpu->arch.db[1], 1); |
| 6119 | get_debugreg(vcpu->arch.db[2], 2); |
| 6120 | get_debugreg(vcpu->arch.db[3], 3); |
| 6121 | get_debugreg(vcpu->arch.dr6, 6); |
| 6122 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 6123 | |
| 6124 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 6125 | 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] | 6126 | } |
| 6127 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6128 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 6129 | { |
| 6130 | vmcs_writel(GUEST_DR7, val); |
| 6131 | } |
| 6132 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6133 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6134 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 6135 | kvm_emulate_cpuid(vcpu); |
| 6136 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6137 | } |
| 6138 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6139 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6140 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6141 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6142 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6143 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6144 | msr_info.index = ecx; |
| 6145 | msr_info.host_initiated = false; |
| 6146 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 6147 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 6148 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6149 | return 1; |
| 6150 | } |
| 6151 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6152 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6153 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6154 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6155 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 6156 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6157 | skip_emulated_instruction(vcpu); |
| 6158 | return 1; |
| 6159 | } |
| 6160 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6161 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6162 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 6163 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6164 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 6165 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 6166 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6167 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 6168 | msr.data = data; |
| 6169 | msr.index = ecx; |
| 6170 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 6171 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 6172 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 6173 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6174 | return 1; |
| 6175 | } |
| 6176 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 6177 | trace_kvm_msr_write(ecx, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6178 | skip_emulated_instruction(vcpu); |
| 6179 | return 1; |
| 6180 | } |
| 6181 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6182 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6183 | { |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6184 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6185 | return 1; |
| 6186 | } |
| 6187 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6188 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6189 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6190 | u32 cpu_based_vm_exec_control; |
| 6191 | |
| 6192 | /* clear pending irq */ |
| 6193 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 6194 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 6195 | 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] | 6196 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6197 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 6198 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 6199 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6200 | return 1; |
| 6201 | } |
| 6202 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6203 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6204 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 6205 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6206 | } |
| 6207 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6208 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6209 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 6210 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6211 | } |
| 6212 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 6213 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 6214 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6215 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 6216 | } |
| 6217 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6218 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 6219 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6220 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 6221 | |
| 6222 | kvm_mmu_invlpg(vcpu, exit_qualification); |
| 6223 | skip_emulated_instruction(vcpu); |
| 6224 | return 1; |
| 6225 | } |
| 6226 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 6227 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 6228 | { |
| 6229 | int err; |
| 6230 | |
| 6231 | err = kvm_rdpmc(vcpu); |
| 6232 | kvm_complete_insn_gp(vcpu, err); |
| 6233 | |
| 6234 | return 1; |
| 6235 | } |
| 6236 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6237 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 6238 | { |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 6239 | kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 6240 | return 1; |
| 6241 | } |
| 6242 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 6243 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 6244 | { |
| 6245 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 6246 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 6247 | |
| 6248 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
| 6249 | skip_emulated_instruction(vcpu); |
| 6250 | return 1; |
| 6251 | } |
| 6252 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6253 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 6254 | { |
| 6255 | skip_emulated_instruction(vcpu); |
| 6256 | WARN(1, "this should never happen\n"); |
| 6257 | return 1; |
| 6258 | } |
| 6259 | |
| 6260 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 6261 | { |
| 6262 | skip_emulated_instruction(vcpu); |
| 6263 | WARN(1, "this should never happen\n"); |
| 6264 | return 1; |
| 6265 | } |
| 6266 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6267 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6268 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 6269 | if (likely(fasteoi)) { |
| 6270 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6271 | int access_type, offset; |
| 6272 | |
| 6273 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 6274 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 6275 | /* |
| 6276 | * Sane guest uses MOV to write EOI, with written value |
| 6277 | * not cared. So make a short-circuit here by avoiding |
| 6278 | * heavy instruction emulation. |
| 6279 | */ |
| 6280 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 6281 | (offset == APIC_EOI)) { |
| 6282 | kvm_lapic_set_eoi(vcpu); |
| 6283 | skip_emulated_instruction(vcpu); |
| 6284 | return 1; |
| 6285 | } |
| 6286 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6287 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6288 | } |
| 6289 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6290 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 6291 | { |
| 6292 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6293 | int vector = exit_qualification & 0xff; |
| 6294 | |
| 6295 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 6296 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 6297 | return 1; |
| 6298 | } |
| 6299 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 6300 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 6301 | { |
| 6302 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6303 | u32 offset = exit_qualification & 0xfff; |
| 6304 | |
| 6305 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 6306 | kvm_apic_write_nodecode(vcpu, offset); |
| 6307 | return 1; |
| 6308 | } |
| 6309 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6310 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6311 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 6312 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6313 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 6314 | bool has_error_code = false; |
| 6315 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6316 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6317 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6318 | |
| 6319 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6320 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6321 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6322 | |
| 6323 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6324 | |
| 6325 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6326 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 6327 | switch (type) { |
| 6328 | case INTR_TYPE_NMI_INTR: |
| 6329 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 6330 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6331 | break; |
| 6332 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6333 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6334 | kvm_clear_interrupt_queue(vcpu); |
| 6335 | break; |
| 6336 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 6337 | if (vmx->idt_vectoring_info & |
| 6338 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 6339 | has_error_code = true; |
| 6340 | error_code = |
| 6341 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 6342 | } |
| 6343 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6344 | case INTR_TYPE_SOFT_EXCEPTION: |
| 6345 | kvm_clear_exception_queue(vcpu); |
| 6346 | break; |
| 6347 | default: |
| 6348 | break; |
| 6349 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 6350 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6351 | tss_selector = exit_qualification; |
| 6352 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6353 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 6354 | type != INTR_TYPE_EXT_INTR && |
| 6355 | type != INTR_TYPE_NMI_INTR)) |
| 6356 | skip_emulated_instruction(vcpu); |
| 6357 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6358 | if (kvm_task_switch(vcpu, tss_selector, |
| 6359 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 6360 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 6361 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6362 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 6363 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6364 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 6365 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6366 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6367 | /* |
| 6368 | * TODO: What about debug traps on tss switch? |
| 6369 | * Are we supposed to inject them and update dr6? |
| 6370 | */ |
| 6371 | |
| 6372 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6373 | } |
| 6374 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6375 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6376 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6377 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6378 | gpa_t gpa; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6379 | u32 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6380 | int gla_validity; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6381 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6382 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6383 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6384 | gla_validity = (exit_qualification >> 7) & 0x3; |
Liang Li | 72e0ae5 | 2016-08-18 15:49:19 +0800 | [diff] [blame] | 6385 | if (gla_validity == 0x2) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6386 | printk(KERN_ERR "EPT: Handling EPT violation failed!\n"); |
| 6387 | printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n", |
| 6388 | (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS), |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6389 | vmcs_readl(GUEST_LINEAR_ADDRESS)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6390 | printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n", |
| 6391 | (long unsigned int)exit_qualification); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6392 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 6393 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION; |
Avi Kivity | 596ae89 | 2009-06-03 14:12:10 +0300 | [diff] [blame] | 6394 | return 0; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6395 | } |
| 6396 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 6397 | /* |
| 6398 | * EPT violation happened while executing iret from NMI, |
| 6399 | * "blocked by NMI" bit has to be set before next VM entry. |
| 6400 | * There are errata that may cause this bit to not be set: |
| 6401 | * AAK134, BY25. |
| 6402 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 6403 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
| 6404 | cpu_has_virtual_nmis() && |
| 6405 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 6406 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 6407 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6408 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6409 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6410 | |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6411 | /* it is a read fault? */ |
| 6412 | error_code = (exit_qualification << 2) & PFERR_USER_MASK; |
| 6413 | /* it is a write fault? */ |
| 6414 | error_code |= exit_qualification & PFERR_WRITE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 6415 | /* It is a fetch fault? */ |
Tiejun Chen | 81ed33e | 2014-11-18 17:12:56 +0800 | [diff] [blame] | 6416 | error_code |= (exit_qualification << 2) & PFERR_FETCH_MASK; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6417 | /* ept page table is present? */ |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6418 | error_code |= (exit_qualification & 0x38) != 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6419 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 6420 | vcpu->arch.exit_qualification = exit_qualification; |
| 6421 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6422 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6423 | } |
| 6424 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6425 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6426 | { |
Xiao Guangrong | f735d4a | 2015-08-05 12:04:27 +0800 | [diff] [blame] | 6427 | int ret; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6428 | gpa_t gpa; |
| 6429 | |
| 6430 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 6431 | 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] | 6432 | skip_emulated_instruction(vcpu); |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 6433 | trace_kvm_fast_mmio(gpa); |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 6434 | return 1; |
| 6435 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6436 | |
Paolo Bonzini | 450869d | 2015-11-04 13:41:21 +0100 | [diff] [blame] | 6437 | ret = handle_mmio_page_fault(vcpu, gpa, true); |
Xiao Guangrong | b37fbea | 2013-06-07 16:51:25 +0800 | [diff] [blame] | 6438 | if (likely(ret == RET_MMIO_PF_EMULATE)) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6439 | return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) == |
| 6440 | EMULATE_DONE; |
Xiao Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 6441 | |
| 6442 | if (unlikely(ret == RET_MMIO_PF_INVALID)) |
| 6443 | return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0); |
| 6444 | |
Xiao Guangrong | b37fbea | 2013-06-07 16:51:25 +0800 | [diff] [blame] | 6445 | if (unlikely(ret == RET_MMIO_PF_RETRY)) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6446 | return 1; |
| 6447 | |
| 6448 | /* It is the real ept misconfig */ |
Xiao Guangrong | f735d4a | 2015-08-05 12:04:27 +0800 | [diff] [blame] | 6449 | WARN_ON(1); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6450 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6451 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 6452 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6453 | |
| 6454 | return 0; |
| 6455 | } |
| 6456 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6457 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6458 | { |
| 6459 | u32 cpu_based_vm_exec_control; |
| 6460 | |
| 6461 | /* clear pending NMI */ |
| 6462 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 6463 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 6464 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 6465 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6466 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6467 | |
| 6468 | return 1; |
| 6469 | } |
| 6470 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6471 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6472 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 6473 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6474 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6475 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6476 | u32 cpu_exec_ctrl; |
| 6477 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 6478 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6479 | |
| 6480 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 6481 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6482 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 6483 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 6484 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6485 | return handle_interrupt_window(&vmx->vcpu); |
| 6486 | |
Avi Kivity | de87dcd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 6487 | if (test_bit(KVM_REQ_EVENT, &vcpu->requests)) |
| 6488 | return 1; |
| 6489 | |
Liran Alon | 114de9b | 2017-11-05 16:56:34 +0200 | [diff] [blame] | 6490 | err = emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6491 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 6492 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 6493 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6494 | ret = 0; |
| 6495 | goto out; |
| 6496 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 6497 | |
Sean Christopherson | 94b4fed | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 6498 | if (err != EMULATE_DONE) |
| 6499 | goto emulation_error; |
| 6500 | |
| 6501 | if (vmx->emulation_required && !vmx->rmode.vm86_active && |
| 6502 | vcpu->arch.exception.pending) |
| 6503 | goto emulation_error; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6504 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 6505 | if (vcpu->arch.halt_request) { |
| 6506 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6507 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 6508 | goto out; |
| 6509 | } |
| 6510 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6511 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6512 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6513 | if (need_resched()) |
| 6514 | schedule(); |
| 6515 | } |
| 6516 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6517 | out: |
| 6518 | return ret; |
Sean Christopherson | 94b4fed | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 6519 | |
| 6520 | emulation_error: |
| 6521 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6522 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 6523 | vcpu->run->internal.ndata = 0; |
| 6524 | return 0; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6525 | } |
| 6526 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 6527 | static int __grow_ple_window(int val) |
| 6528 | { |
| 6529 | if (ple_window_grow < 1) |
| 6530 | return ple_window; |
| 6531 | |
| 6532 | val = min(val, ple_window_actual_max); |
| 6533 | |
| 6534 | if (ple_window_grow < ple_window) |
| 6535 | val *= ple_window_grow; |
| 6536 | else |
| 6537 | val += ple_window_grow; |
| 6538 | |
| 6539 | return val; |
| 6540 | } |
| 6541 | |
| 6542 | static int __shrink_ple_window(int val, int modifier, int minimum) |
| 6543 | { |
| 6544 | if (modifier < 1) |
| 6545 | return ple_window; |
| 6546 | |
| 6547 | if (modifier < ple_window) |
| 6548 | val /= modifier; |
| 6549 | else |
| 6550 | val -= modifier; |
| 6551 | |
| 6552 | return max(val, minimum); |
| 6553 | } |
| 6554 | |
| 6555 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 6556 | { |
| 6557 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6558 | int old = vmx->ple_window; |
| 6559 | |
| 6560 | vmx->ple_window = __grow_ple_window(old); |
| 6561 | |
| 6562 | if (vmx->ple_window != old) |
| 6563 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 6564 | |
| 6565 | 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] | 6566 | } |
| 6567 | |
| 6568 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 6569 | { |
| 6570 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6571 | int old = vmx->ple_window; |
| 6572 | |
| 6573 | vmx->ple_window = __shrink_ple_window(old, |
| 6574 | ple_window_shrink, ple_window); |
| 6575 | |
| 6576 | if (vmx->ple_window != old) |
| 6577 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 6578 | |
| 6579 | 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] | 6580 | } |
| 6581 | |
| 6582 | /* |
| 6583 | * ple_window_actual_max is computed to be one grow_ple_window() below |
| 6584 | * ple_window_max. (See __grow_ple_window for the reason.) |
| 6585 | * This prevents overflows, because ple_window_max is int. |
| 6586 | * ple_window_max effectively rounded down to a multiple of ple_window_grow in |
| 6587 | * this process. |
| 6588 | * ple_window_max is also prevented from setting vmx->ple_window < ple_window. |
| 6589 | */ |
| 6590 | static void update_ple_window_actual_max(void) |
| 6591 | { |
| 6592 | ple_window_actual_max = |
| 6593 | __shrink_ple_window(max(ple_window_max, ple_window), |
| 6594 | ple_window_grow, INT_MIN); |
| 6595 | } |
| 6596 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6597 | /* |
| 6598 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 6599 | */ |
| 6600 | static void wakeup_handler(void) |
| 6601 | { |
| 6602 | struct kvm_vcpu *vcpu; |
| 6603 | int cpu = smp_processor_id(); |
| 6604 | |
| 6605 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 6606 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 6607 | blocked_vcpu_list) { |
| 6608 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 6609 | |
| 6610 | if (pi_test_on(pi_desc) == 1) |
| 6611 | kvm_vcpu_kick(vcpu); |
| 6612 | } |
| 6613 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 6614 | } |
| 6615 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6616 | static __init int hardware_setup(void) |
| 6617 | { |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6618 | int r = -ENOMEM, i; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6619 | |
| 6620 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 6621 | |
| 6622 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 6623 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 6624 | |
| 6625 | vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6626 | if (!vmx_io_bitmap_a) |
| 6627 | return r; |
| 6628 | |
| 6629 | vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6630 | if (!vmx_io_bitmap_b) |
| 6631 | goto out; |
| 6632 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6633 | vmx_vmread_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6634 | if (!vmx_vmread_bitmap) |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6635 | goto out1; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6636 | |
| 6637 | vmx_vmwrite_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6638 | if (!vmx_vmwrite_bitmap) |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6639 | goto out2; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6640 | |
| 6641 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 6642 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 6643 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6644 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6645 | |
| 6646 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); |
| 6647 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6648 | if (setup_vmcs_config(&vmcs_config) < 0) { |
| 6649 | r = -EIO; |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6650 | goto out3; |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6651 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6652 | |
| 6653 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 6654 | kvm_enable_efer_bits(EFER_NX); |
| 6655 | |
Wanpeng Li | 2df1969 | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 6656 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
| 6657 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6658 | enable_vpid = 0; |
Wanpeng Li | 2df1969 | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 6659 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6660 | if (!cpu_has_vmx_shadow_vmcs()) |
| 6661 | enable_shadow_vmcs = 0; |
| 6662 | if (enable_shadow_vmcs) |
| 6663 | init_vmcs_shadow_fields(); |
| 6664 | |
| 6665 | if (!cpu_has_vmx_ept() || |
| 6666 | !cpu_has_vmx_ept_4levels()) { |
| 6667 | enable_ept = 0; |
| 6668 | enable_unrestricted_guest = 0; |
| 6669 | enable_ept_ad_bits = 0; |
| 6670 | } |
| 6671 | |
| 6672 | if (!cpu_has_vmx_ept_ad_bits()) |
| 6673 | enable_ept_ad_bits = 0; |
| 6674 | |
| 6675 | if (!cpu_has_vmx_unrestricted_guest()) |
| 6676 | enable_unrestricted_guest = 0; |
| 6677 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 6678 | if (!cpu_has_vmx_flexpriority()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6679 | flexpriority_enabled = 0; |
| 6680 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 6681 | /* |
| 6682 | * set_apic_access_page_addr() is used to reload apic access |
| 6683 | * page upon invalidation. No need to do anything if not |
| 6684 | * using the APIC_ACCESS_ADDR VMCS field. |
| 6685 | */ |
| 6686 | if (!flexpriority_enabled) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6687 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6688 | |
| 6689 | if (!cpu_has_vmx_tpr_shadow()) |
| 6690 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 6691 | |
| 6692 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 6693 | kvm_disable_largepages(); |
| 6694 | |
| 6695 | if (!cpu_has_vmx_ple()) |
| 6696 | ple_gap = 0; |
| 6697 | |
| 6698 | if (!cpu_has_vmx_apicv()) |
| 6699 | enable_apicv = 0; |
| 6700 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 6701 | if (cpu_has_vmx_tsc_scaling()) { |
| 6702 | kvm_has_tsc_control = true; |
| 6703 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 6704 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 6705 | } |
| 6706 | |
Wanpeng Li | 04bb92e | 2015-09-16 19:31:11 +0800 | [diff] [blame] | 6707 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 6708 | |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6709 | if (enable_ept) { |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6710 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6711 | (enable_ept_ad_bits) ? VMX_EPT_ACCESS_BIT : 0ull, |
| 6712 | (enable_ept_ad_bits) ? VMX_EPT_DIRTY_BIT : 0ull, |
Bandan Das | d95c556 | 2016-07-12 18:18:51 -0400 | [diff] [blame] | 6713 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 6714 | cpu_has_vmx_ept_execute_only() ? |
| 6715 | 0ull : VMX_EPT_READABLE_MASK); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6716 | ept_set_mmio_spte_mask(); |
| 6717 | kvm_enable_tdp(); |
| 6718 | } else |
| 6719 | kvm_disable_tdp(); |
| 6720 | |
| 6721 | update_ple_window_actual_max(); |
| 6722 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6723 | /* |
| 6724 | * Only enable PML when hardware supports PML feature, and both EPT |
| 6725 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 6726 | */ |
| 6727 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 6728 | enable_pml = 0; |
| 6729 | |
| 6730 | if (!enable_pml) { |
| 6731 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 6732 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 6733 | kvm_x86_ops->flush_log_dirty = NULL; |
| 6734 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 6735 | } |
| 6736 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6737 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 6738 | u64 vmx_msr; |
| 6739 | |
| 6740 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 6741 | cpu_preemption_timer_multi = |
| 6742 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 6743 | } else { |
| 6744 | kvm_x86_ops->set_hv_timer = NULL; |
| 6745 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 6746 | } |
| 6747 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6748 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
| 6749 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 6750 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 6751 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6752 | return alloc_kvm_area(); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6753 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6754 | out3: |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6755 | free_page((unsigned long)vmx_vmwrite_bitmap); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6756 | out2: |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6757 | free_page((unsigned long)vmx_vmread_bitmap); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6758 | out1: |
| 6759 | free_page((unsigned long)vmx_io_bitmap_b); |
| 6760 | out: |
| 6761 | free_page((unsigned long)vmx_io_bitmap_a); |
| 6762 | |
| 6763 | return r; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6764 | } |
| 6765 | |
| 6766 | static __exit void hardware_unsetup(void) |
| 6767 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6768 | free_page((unsigned long)vmx_io_bitmap_b); |
| 6769 | free_page((unsigned long)vmx_io_bitmap_a); |
| 6770 | free_page((unsigned long)vmx_vmwrite_bitmap); |
| 6771 | free_page((unsigned long)vmx_vmread_bitmap); |
| 6772 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6773 | free_kvm_area(); |
| 6774 | } |
| 6775 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6776 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6777 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 6778 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 6779 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 6780 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6781 | { |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 6782 | if (ple_gap) |
| 6783 | grow_ple_window(vcpu); |
| 6784 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6785 | skip_emulated_instruction(vcpu); |
| 6786 | kvm_vcpu_on_spin(vcpu); |
| 6787 | |
| 6788 | return 1; |
| 6789 | } |
| 6790 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6791 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 6792 | { |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6793 | skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 6794 | return 1; |
| 6795 | } |
| 6796 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6797 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 6798 | { |
| 6799 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 6800 | return handle_nop(vcpu); |
| 6801 | } |
| 6802 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 6803 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 6804 | { |
| 6805 | return 1; |
| 6806 | } |
| 6807 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6808 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 6809 | { |
| 6810 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 6811 | return handle_nop(vcpu); |
| 6812 | } |
| 6813 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6814 | /* |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 6815 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 6816 | * set the success or error code of an emulated VMX instruction, as specified |
| 6817 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 6818 | */ |
| 6819 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 6820 | { |
| 6821 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 6822 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 6823 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 6824 | } |
| 6825 | |
| 6826 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 6827 | { |
| 6828 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 6829 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 6830 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 6831 | | X86_EFLAGS_CF); |
| 6832 | } |
| 6833 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 6834 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 6835 | u32 vm_instruction_error) |
| 6836 | { |
| 6837 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 6838 | /* |
| 6839 | * failValid writes the error number to the current VMCS, which |
| 6840 | * can't be done there isn't a current VMCS. |
| 6841 | */ |
| 6842 | nested_vmx_failInvalid(vcpu); |
| 6843 | return; |
| 6844 | } |
| 6845 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 6846 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 6847 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 6848 | | X86_EFLAGS_ZF); |
| 6849 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 6850 | /* |
| 6851 | * We don't need to force a shadow sync because |
| 6852 | * VM_INSTRUCTION_ERROR is not shadowed |
| 6853 | */ |
| 6854 | } |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 6855 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 6856 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 6857 | { |
| 6858 | /* TODO: not to reset guest simply here. */ |
| 6859 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 6860 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 6861 | } |
| 6862 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 6863 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 6864 | { |
| 6865 | struct vcpu_vmx *vmx = |
| 6866 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 6867 | |
| 6868 | vmx->nested.preemption_timer_expired = true; |
| 6869 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 6870 | kvm_vcpu_kick(&vmx->vcpu); |
| 6871 | |
| 6872 | return HRTIMER_NORESTART; |
| 6873 | } |
| 6874 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6875 | /* |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6876 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 6877 | * exit caused by such an instruction (run by a guest hypervisor). |
| 6878 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 6879 | * #UD or #GP. |
| 6880 | */ |
| 6881 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 6882 | unsigned long exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6883 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6884 | { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6885 | gva_t off; |
| 6886 | bool exn; |
| 6887 | struct kvm_segment s; |
| 6888 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6889 | /* |
| 6890 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 6891 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 6892 | * addressing components of the operand. Only the displacement part |
| 6893 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 6894 | * For how an actual address is calculated from all these components, |
| 6895 | * refer to Vol. 1, "Operand Addressing". |
| 6896 | */ |
| 6897 | int scaling = vmx_instruction_info & 3; |
| 6898 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 6899 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 6900 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 6901 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 6902 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 6903 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 6904 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 6905 | |
| 6906 | if (is_reg) { |
| 6907 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6908 | return 1; |
| 6909 | } |
| 6910 | |
| 6911 | /* Addr = segment_base + offset */ |
| 6912 | /* offset = base + [index * scale] + displacement */ |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6913 | off = exit_qualification; /* holds the displacement */ |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6914 | if (base_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6915 | off += kvm_register_read(vcpu, base_reg); |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6916 | if (index_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6917 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
| 6918 | vmx_get_segment(vcpu, &s, seg_reg); |
| 6919 | *ret = s.base + off; |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6920 | |
| 6921 | if (addr_size == 1) /* 32 bit */ |
| 6922 | *ret &= 0xffffffff; |
| 6923 | |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6924 | /* Checks for #GP/#SS exceptions. */ |
| 6925 | exn = false; |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 6926 | if (is_long_mode(vcpu)) { |
| 6927 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 6928 | * non-canonical form. This is the only check on the memory |
| 6929 | * destination for long mode! |
| 6930 | */ |
| 6931 | exn = is_noncanonical_address(*ret); |
| 6932 | } else if (is_protmode(vcpu)) { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6933 | /* Protected mode: apply checks for segment validity in the |
| 6934 | * following order: |
| 6935 | * - segment type check (#GP(0) may be thrown) |
| 6936 | * - usability check (#GP(0)/#SS(0)) |
| 6937 | * - limit check (#GP(0)/#SS(0)) |
| 6938 | */ |
| 6939 | if (wr) |
| 6940 | /* #GP(0) if the destination operand is located in a |
| 6941 | * read-only data segment or any code segment. |
| 6942 | */ |
| 6943 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 6944 | else |
| 6945 | /* #GP(0) if the source operand is located in an |
| 6946 | * execute-only code segment |
| 6947 | */ |
| 6948 | exn = ((s.type & 0xa) == 8); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 6949 | if (exn) { |
| 6950 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 6951 | return 1; |
| 6952 | } |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6953 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 6954 | */ |
| 6955 | exn = (s.unusable != 0); |
| 6956 | /* Protected mode: #GP(0)/#SS(0) if the memory |
| 6957 | * operand is outside the segment limit. |
| 6958 | */ |
| 6959 | exn = exn || (off + sizeof(u64) > s.limit); |
| 6960 | } |
| 6961 | if (exn) { |
| 6962 | kvm_queue_exception_e(vcpu, |
| 6963 | seg_reg == VCPU_SREG_SS ? |
| 6964 | SS_VECTOR : GP_VECTOR, |
| 6965 | 0); |
| 6966 | return 1; |
| 6967 | } |
| 6968 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6969 | return 0; |
| 6970 | } |
| 6971 | |
| 6972 | /* |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6973 | * This function performs the various checks including |
| 6974 | * - if it's 4KB aligned |
| 6975 | * - No bits beyond the physical address width are set |
| 6976 | * - Returns 0 on success or else 1 |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6977 | * (Intel SDM Section 30.3) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6978 | */ |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 6979 | static int nested_vmx_check_vmptr(struct kvm_vcpu *vcpu, int exit_reason, |
| 6980 | gpa_t *vmpointer) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6981 | { |
| 6982 | gva_t gva; |
| 6983 | gpa_t vmptr; |
| 6984 | struct x86_exception e; |
| 6985 | struct page *page; |
| 6986 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6987 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 6988 | |
| 6989 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6990 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6991 | return 1; |
| 6992 | |
Paolo Bonzini | 838b0e9 | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 6993 | if (kvm_read_guest_virt(vcpu, gva, &vmptr, sizeof(vmptr), &e)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 6994 | kvm_inject_page_fault(vcpu, &e); |
| 6995 | return 1; |
| 6996 | } |
| 6997 | |
| 6998 | switch (exit_reason) { |
| 6999 | case EXIT_REASON_VMON: |
| 7000 | /* |
| 7001 | * SDM 3: 24.11.5 |
| 7002 | * The first 4 bytes of VMXON region contain the supported |
| 7003 | * VMCS revision identifier |
| 7004 | * |
| 7005 | * Note - IA32_VMX_BASIC[48] will never be 1 |
| 7006 | * for the nested case; |
| 7007 | * which replaces physical address width with 32 |
| 7008 | * |
| 7009 | */ |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 7010 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7011 | nested_vmx_failInvalid(vcpu); |
| 7012 | skip_emulated_instruction(vcpu); |
| 7013 | return 1; |
| 7014 | } |
| 7015 | |
| 7016 | page = nested_get_page(vcpu, vmptr); |
Paolo Bonzini | 75465e7 | 2017-01-24 11:56:21 +0100 | [diff] [blame] | 7017 | if (page == NULL) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7018 | nested_vmx_failInvalid(vcpu); |
Paolo Bonzini | 75465e7 | 2017-01-24 11:56:21 +0100 | [diff] [blame] | 7019 | skip_emulated_instruction(vcpu); |
| 7020 | return 1; |
| 7021 | } |
| 7022 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7023 | kunmap(page); |
Paolo Bonzini | 75465e7 | 2017-01-24 11:56:21 +0100 | [diff] [blame] | 7024 | nested_release_page_clean(page); |
| 7025 | nested_vmx_failInvalid(vcpu); |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7026 | skip_emulated_instruction(vcpu); |
| 7027 | return 1; |
| 7028 | } |
| 7029 | kunmap(page); |
Paolo Bonzini | 75465e7 | 2017-01-24 11:56:21 +0100 | [diff] [blame] | 7030 | nested_release_page_clean(page); |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7031 | vmx->nested.vmxon_ptr = vmptr; |
| 7032 | break; |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7033 | case EXIT_REASON_VMCLEAR: |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 7034 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7035 | nested_vmx_failValid(vcpu, |
| 7036 | VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 7037 | skip_emulated_instruction(vcpu); |
| 7038 | return 1; |
| 7039 | } |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7040 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7041 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 7042 | nested_vmx_failValid(vcpu, |
| 7043 | VMXERR_VMCLEAR_VMXON_POINTER); |
| 7044 | skip_emulated_instruction(vcpu); |
| 7045 | return 1; |
| 7046 | } |
| 7047 | break; |
| 7048 | case EXIT_REASON_VMPTRLD: |
Fabian Frederick | bc39c4d | 2014-06-14 23:44:29 +0200 | [diff] [blame] | 7049 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> maxphyaddr)) { |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7050 | nested_vmx_failValid(vcpu, |
| 7051 | VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 7052 | skip_emulated_instruction(vcpu); |
| 7053 | return 1; |
| 7054 | } |
| 7055 | |
| 7056 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 7057 | nested_vmx_failValid(vcpu, |
| 7058 | VMXERR_VMCLEAR_VMXON_POINTER); |
| 7059 | skip_emulated_instruction(vcpu); |
| 7060 | return 1; |
| 7061 | } |
| 7062 | break; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7063 | default: |
| 7064 | return 1; /* shouldn't happen */ |
| 7065 | } |
| 7066 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7067 | if (vmpointer) |
| 7068 | *vmpointer = vmptr; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7069 | return 0; |
| 7070 | } |
| 7071 | |
| 7072 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7073 | * Emulate the VMXON instruction. |
| 7074 | * Currently, we just remember that VMX is active, and do not save or even |
| 7075 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 7076 | * do not currently need to store anything in that guest-allocated memory |
| 7077 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 7078 | * argument is different from the VMXON pointer (which the spec says they do). |
| 7079 | */ |
| 7080 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 7081 | { |
| 7082 | struct kvm_segment cs; |
| 7083 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7084 | struct vmcs *shadow_vmcs; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7085 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 7086 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 7087 | int r; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7088 | |
| 7089 | /* The Intel VMX Instruction Reference lists a bunch of bits that |
| 7090 | * are prerequisite to running VMXON, most notably cr4.VMXE must be |
| 7091 | * set to 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 7092 | * Otherwise, we should fail with #UD. We test these now: |
| 7093 | */ |
| 7094 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE) || |
| 7095 | !kvm_read_cr0_bits(vcpu, X86_CR0_PE) || |
| 7096 | (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
| 7097 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7098 | return 1; |
| 7099 | } |
| 7100 | |
| 7101 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 7102 | if (is_long_mode(vcpu) && !cs.l) { |
| 7103 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7104 | return 1; |
| 7105 | } |
| 7106 | |
| 7107 | if (vmx_get_cpl(vcpu)) { |
| 7108 | kvm_inject_gp(vcpu, 0); |
| 7109 | return 1; |
| 7110 | } |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7111 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7112 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMON, NULL)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7113 | return 1; |
| 7114 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7115 | if (vmx->nested.vmxon) { |
| 7116 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
| 7117 | skip_emulated_instruction(vcpu); |
| 7118 | return 1; |
| 7119 | } |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7120 | |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 7121 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7122 | != VMXON_NEEDED_FEATURES) { |
| 7123 | kvm_inject_gp(vcpu, 0); |
| 7124 | return 1; |
| 7125 | } |
| 7126 | |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 7127 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 7128 | if (r < 0) |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7129 | goto out_vmcs02; |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7130 | |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7131 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 7132 | if (!vmx->nested.cached_vmcs12) |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7133 | goto out_cached_vmcs12; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7134 | |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7135 | if (enable_shadow_vmcs) { |
| 7136 | shadow_vmcs = alloc_vmcs(); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7137 | if (!shadow_vmcs) |
| 7138 | goto out_shadow_vmcs; |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7139 | /* mark vmcs as shadow */ |
| 7140 | shadow_vmcs->revision_id |= (1u << 31); |
| 7141 | /* init shadow vmcs */ |
| 7142 | vmcs_clear(shadow_vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7143 | vmx->vmcs01.shadow_vmcs = shadow_vmcs; |
Abel Gordon | 8de4883 | 2013-04-18 14:37:25 +0300 | [diff] [blame] | 7144 | } |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7145 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7146 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
Wanpeng Li | f15a75e | 2016-08-30 16:14:01 +0800 | [diff] [blame] | 7147 | HRTIMER_MODE_REL_PINNED); |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7148 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 7149 | |
Roman Kagan | 020a90f | 2018-07-19 21:59:07 +0300 | [diff] [blame] | 7150 | vmx->nested.vpid02 = allocate_vpid(); |
| 7151 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7152 | vmx->nested.vmxon = true; |
| 7153 | |
| 7154 | skip_emulated_instruction(vcpu); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 7155 | nested_vmx_succeed(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7156 | return 1; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7157 | |
| 7158 | out_shadow_vmcs: |
| 7159 | kfree(vmx->nested.cached_vmcs12); |
| 7160 | |
| 7161 | out_cached_vmcs12: |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7162 | free_loaded_vmcs(&vmx->nested.vmcs02); |
| 7163 | |
| 7164 | out_vmcs02: |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7165 | return -ENOMEM; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7166 | } |
| 7167 | |
| 7168 | /* |
| 7169 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 7170 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 7171 | * slightly different). It also specifies what exception to inject otherwise. |
| 7172 | */ |
| 7173 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 7174 | { |
| 7175 | struct kvm_segment cs; |
| 7176 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7177 | |
| 7178 | if (!vmx->nested.vmxon) { |
| 7179 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7180 | return 0; |
| 7181 | } |
| 7182 | |
| 7183 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 7184 | if ((vmx_get_rflags(vcpu) & X86_EFLAGS_VM) || |
| 7185 | (is_long_mode(vcpu) && !cs.l)) { |
| 7186 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7187 | return 0; |
| 7188 | } |
| 7189 | |
| 7190 | if (vmx_get_cpl(vcpu)) { |
| 7191 | kvm_inject_gp(vcpu, 0); |
| 7192 | return 0; |
| 7193 | } |
| 7194 | |
| 7195 | return 1; |
| 7196 | } |
| 7197 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7198 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
| 7199 | { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7200 | if (vmx->nested.current_vmptr == -1ull) |
| 7201 | return; |
| 7202 | |
| 7203 | /* current_vmptr and current_vmcs12 are always set/reset together */ |
| 7204 | if (WARN_ON(vmx->nested.current_vmcs12 == NULL)) |
| 7205 | return; |
| 7206 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7207 | if (enable_shadow_vmcs) { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7208 | /* copy to memory all shadowed fields in case |
| 7209 | they were modified */ |
| 7210 | copy_shadow_to_vmcs12(vmx); |
| 7211 | vmx->nested.sync_shadow_vmcs = false; |
Xiao Guangrong | 7ec3629 | 2015-09-09 14:05:56 +0800 | [diff] [blame] | 7212 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 7213 | SECONDARY_EXEC_SHADOW_VMCS); |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7214 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7215 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7216 | vmx->nested.posted_intr_nv = -1; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7217 | |
| 7218 | /* Flush VMCS12 to guest memory */ |
| 7219 | memcpy(vmx->nested.current_vmcs12, vmx->nested.cached_vmcs12, |
| 7220 | VMCS12_SIZE); |
| 7221 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7222 | kunmap(vmx->nested.current_vmcs12_page); |
| 7223 | nested_release_page(vmx->nested.current_vmcs12_page); |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7224 | vmx->nested.current_vmptr = -1ull; |
| 7225 | vmx->nested.current_vmcs12 = NULL; |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7226 | } |
| 7227 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7228 | /* |
| 7229 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 7230 | * just stops using VMX. |
| 7231 | */ |
| 7232 | static void free_nested(struct vcpu_vmx *vmx) |
| 7233 | { |
| 7234 | if (!vmx->nested.vmxon) |
| 7235 | return; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7236 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7237 | vmx->nested.vmxon = false; |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 7238 | free_vpid(vmx->nested.vpid02); |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7239 | nested_release_vmcs12(vmx); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7240 | if (enable_shadow_vmcs) { |
| 7241 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 7242 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 7243 | vmx->vmcs01.shadow_vmcs = NULL; |
| 7244 | } |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7245 | kfree(vmx->nested.cached_vmcs12); |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7246 | /* Unpin physical memory we referred to in the vmcs02 */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 7247 | if (vmx->nested.apic_access_page) { |
| 7248 | nested_release_page(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 7249 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 7250 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7251 | if (vmx->nested.virtual_apic_page) { |
| 7252 | nested_release_page(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 7253 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7254 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7255 | if (vmx->nested.pi_desc_page) { |
| 7256 | kunmap(vmx->nested.pi_desc_page); |
| 7257 | nested_release_page(vmx->nested.pi_desc_page); |
| 7258 | vmx->nested.pi_desc_page = NULL; |
| 7259 | vmx->nested.pi_desc = NULL; |
| 7260 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7261 | |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7262 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7263 | } |
| 7264 | |
| 7265 | /* Emulate the VMXOFF instruction */ |
| 7266 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 7267 | { |
| 7268 | if (!nested_vmx_check_permission(vcpu)) |
| 7269 | return 1; |
| 7270 | free_nested(to_vmx(vcpu)); |
| 7271 | skip_emulated_instruction(vcpu); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 7272 | nested_vmx_succeed(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7273 | return 1; |
| 7274 | } |
| 7275 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7276 | /* Emulate the VMCLEAR instruction */ |
| 7277 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 7278 | { |
| 7279 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 29deec4 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 7280 | u32 zero = 0; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7281 | gpa_t vmptr; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7282 | |
| 7283 | if (!nested_vmx_check_permission(vcpu)) |
| 7284 | return 1; |
| 7285 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7286 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMCLEAR, &vmptr)) |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7287 | return 1; |
| 7288 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7289 | if (vmptr == vmx->nested.current_vmptr) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7290 | nested_release_vmcs12(vmx); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7291 | |
Jim Mattson | 29deec4 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 7292 | kvm_vcpu_write_guest(vcpu, |
| 7293 | vmptr + offsetof(struct vmcs12, launch_state), |
| 7294 | &zero, sizeof(zero)); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7295 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7296 | skip_emulated_instruction(vcpu); |
| 7297 | nested_vmx_succeed(vcpu); |
| 7298 | return 1; |
| 7299 | } |
| 7300 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7301 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 7302 | |
| 7303 | /* Emulate the VMLAUNCH instruction */ |
| 7304 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 7305 | { |
| 7306 | return nested_vmx_run(vcpu, true); |
| 7307 | } |
| 7308 | |
| 7309 | /* Emulate the VMRESUME instruction */ |
| 7310 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 7311 | { |
| 7312 | |
| 7313 | return nested_vmx_run(vcpu, false); |
| 7314 | } |
| 7315 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7316 | enum vmcs_field_type { |
| 7317 | VMCS_FIELD_TYPE_U16 = 0, |
| 7318 | VMCS_FIELD_TYPE_U64 = 1, |
| 7319 | VMCS_FIELD_TYPE_U32 = 2, |
| 7320 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 |
| 7321 | }; |
| 7322 | |
| 7323 | static inline int vmcs_field_type(unsigned long field) |
| 7324 | { |
| 7325 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
| 7326 | return VMCS_FIELD_TYPE_U32; |
| 7327 | return (field >> 13) & 0x3 ; |
| 7328 | } |
| 7329 | |
| 7330 | static inline int vmcs_field_readonly(unsigned long field) |
| 7331 | { |
| 7332 | return (((field >> 10) & 0x3) == 1); |
| 7333 | } |
| 7334 | |
| 7335 | /* |
| 7336 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 7337 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 7338 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 7339 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 7340 | * 64-bit fields are to be returned). |
| 7341 | */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7342 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
| 7343 | unsigned long field, u64 *ret) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7344 | { |
| 7345 | short offset = vmcs_field_to_offset(field); |
| 7346 | char *p; |
| 7347 | |
| 7348 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7349 | return offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7350 | |
| 7351 | p = ((char *)(get_vmcs12(vcpu))) + offset; |
| 7352 | |
| 7353 | switch (vmcs_field_type(field)) { |
| 7354 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7355 | *ret = *((natural_width *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7356 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7357 | case VMCS_FIELD_TYPE_U16: |
| 7358 | *ret = *((u16 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7359 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7360 | case VMCS_FIELD_TYPE_U32: |
| 7361 | *ret = *((u32 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7362 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7363 | case VMCS_FIELD_TYPE_U64: |
| 7364 | *ret = *((u64 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7365 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7366 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7367 | WARN_ON(1); |
| 7368 | return -ENOENT; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7369 | } |
| 7370 | } |
| 7371 | |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7372 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7373 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
| 7374 | unsigned long field, u64 field_value){ |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7375 | short offset = vmcs_field_to_offset(field); |
| 7376 | char *p = ((char *) get_vmcs12(vcpu)) + offset; |
| 7377 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7378 | return offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7379 | |
| 7380 | switch (vmcs_field_type(field)) { |
| 7381 | case VMCS_FIELD_TYPE_U16: |
| 7382 | *(u16 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7383 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7384 | case VMCS_FIELD_TYPE_U32: |
| 7385 | *(u32 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7386 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7387 | case VMCS_FIELD_TYPE_U64: |
| 7388 | *(u64 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7389 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7390 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7391 | *(natural_width *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7392 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7393 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7394 | WARN_ON(1); |
| 7395 | return -ENOENT; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7396 | } |
| 7397 | |
| 7398 | } |
| 7399 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7400 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 7401 | { |
| 7402 | int i; |
| 7403 | unsigned long field; |
| 7404 | u64 field_value; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7405 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7406 | const unsigned long *fields = shadow_read_write_fields; |
| 7407 | const int num_fields = max_shadow_read_write_fields; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7408 | |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 7409 | preempt_disable(); |
| 7410 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7411 | vmcs_load(shadow_vmcs); |
| 7412 | |
| 7413 | for (i = 0; i < num_fields; i++) { |
| 7414 | field = fields[i]; |
| 7415 | switch (vmcs_field_type(field)) { |
| 7416 | case VMCS_FIELD_TYPE_U16: |
| 7417 | field_value = vmcs_read16(field); |
| 7418 | break; |
| 7419 | case VMCS_FIELD_TYPE_U32: |
| 7420 | field_value = vmcs_read32(field); |
| 7421 | break; |
| 7422 | case VMCS_FIELD_TYPE_U64: |
| 7423 | field_value = vmcs_read64(field); |
| 7424 | break; |
| 7425 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7426 | field_value = vmcs_readl(field); |
| 7427 | break; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7428 | default: |
| 7429 | WARN_ON(1); |
| 7430 | continue; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7431 | } |
| 7432 | vmcs12_write_any(&vmx->vcpu, field, field_value); |
| 7433 | } |
| 7434 | |
| 7435 | vmcs_clear(shadow_vmcs); |
| 7436 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 7437 | |
| 7438 | preempt_enable(); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7439 | } |
| 7440 | |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7441 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 7442 | { |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7443 | const unsigned long *fields[] = { |
| 7444 | shadow_read_write_fields, |
| 7445 | shadow_read_only_fields |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7446 | }; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7447 | const int max_fields[] = { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7448 | max_shadow_read_write_fields, |
| 7449 | max_shadow_read_only_fields |
| 7450 | }; |
| 7451 | int i, q; |
| 7452 | unsigned long field; |
| 7453 | u64 field_value = 0; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7454 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7455 | |
| 7456 | vmcs_load(shadow_vmcs); |
| 7457 | |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7458 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7459 | for (i = 0; i < max_fields[q]; i++) { |
| 7460 | field = fields[q][i]; |
| 7461 | vmcs12_read_any(&vmx->vcpu, field, &field_value); |
| 7462 | |
| 7463 | switch (vmcs_field_type(field)) { |
| 7464 | case VMCS_FIELD_TYPE_U16: |
| 7465 | vmcs_write16(field, (u16)field_value); |
| 7466 | break; |
| 7467 | case VMCS_FIELD_TYPE_U32: |
| 7468 | vmcs_write32(field, (u32)field_value); |
| 7469 | break; |
| 7470 | case VMCS_FIELD_TYPE_U64: |
| 7471 | vmcs_write64(field, (u64)field_value); |
| 7472 | break; |
| 7473 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7474 | vmcs_writel(field, (long)field_value); |
| 7475 | break; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7476 | default: |
| 7477 | WARN_ON(1); |
| 7478 | break; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7479 | } |
| 7480 | } |
| 7481 | } |
| 7482 | |
| 7483 | vmcs_clear(shadow_vmcs); |
| 7484 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 7485 | } |
| 7486 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7487 | /* |
| 7488 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 7489 | * used before) all generate the same failure when it is missing. |
| 7490 | */ |
| 7491 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 7492 | { |
| 7493 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7494 | if (vmx->nested.current_vmptr == -1ull) { |
| 7495 | nested_vmx_failInvalid(vcpu); |
| 7496 | skip_emulated_instruction(vcpu); |
| 7497 | return 0; |
| 7498 | } |
| 7499 | return 1; |
| 7500 | } |
| 7501 | |
| 7502 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 7503 | { |
| 7504 | unsigned long field; |
| 7505 | u64 field_value; |
| 7506 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7507 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7508 | gva_t gva = 0; |
| 7509 | |
| 7510 | if (!nested_vmx_check_permission(vcpu) || |
| 7511 | !nested_vmx_check_vmcs12(vcpu)) |
| 7512 | return 1; |
| 7513 | |
| 7514 | /* Decode instruction info and find the field to read */ |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7515 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7516 | /* Read the field, zero-extended to a u64 field_value */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7517 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7518 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 7519 | skip_emulated_instruction(vcpu); |
| 7520 | return 1; |
| 7521 | } |
| 7522 | /* |
| 7523 | * Now copy part of this value to register or memory, as requested. |
| 7524 | * Note that the number of bits actually copied is 32 or 64 depending |
| 7525 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 7526 | */ |
| 7527 | if (vmx_instruction_info & (1u << 10)) { |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7528 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7529 | field_value); |
| 7530 | } else { |
| 7531 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7532 | vmx_instruction_info, true, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7533 | return 1; |
| 7534 | /* _system ok, as nested_vmx_check_permission verified cpl=0 */ |
Paolo Bonzini | 838b0e9 | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 7535 | kvm_write_guest_virt_system(vcpu, gva, &field_value, |
| 7536 | (is_long_mode(vcpu) ? 8 : 4), NULL); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7537 | } |
| 7538 | |
| 7539 | nested_vmx_succeed(vcpu); |
| 7540 | skip_emulated_instruction(vcpu); |
| 7541 | return 1; |
| 7542 | } |
| 7543 | |
| 7544 | |
| 7545 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 7546 | { |
| 7547 | unsigned long field; |
| 7548 | gva_t gva; |
| 7549 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7550 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7551 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 7552 | * mode, and eventually we need to write that into a field of several |
| 7553 | * possible lengths. The code below first zero-extends the value to 64 |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 7554 | * bit (field_value), and then copies only the appropriate number of |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7555 | * bits into the vmcs12 field. |
| 7556 | */ |
| 7557 | u64 field_value = 0; |
| 7558 | struct x86_exception e; |
| 7559 | |
| 7560 | if (!nested_vmx_check_permission(vcpu) || |
| 7561 | !nested_vmx_check_vmcs12(vcpu)) |
| 7562 | return 1; |
| 7563 | |
| 7564 | if (vmx_instruction_info & (1u << 10)) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7565 | field_value = kvm_register_readl(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7566 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 7567 | else { |
| 7568 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7569 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7570 | return 1; |
Paolo Bonzini | 838b0e9 | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 7571 | if (kvm_read_guest_virt(vcpu, gva, &field_value, |
| 7572 | (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7573 | kvm_inject_page_fault(vcpu, &e); |
| 7574 | return 1; |
| 7575 | } |
| 7576 | } |
| 7577 | |
| 7578 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7579 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7580 | if (vmcs_field_readonly(field)) { |
| 7581 | nested_vmx_failValid(vcpu, |
| 7582 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
| 7583 | skip_emulated_instruction(vcpu); |
| 7584 | return 1; |
| 7585 | } |
| 7586 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7587 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7588 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 7589 | skip_emulated_instruction(vcpu); |
| 7590 | return 1; |
| 7591 | } |
| 7592 | |
| 7593 | nested_vmx_succeed(vcpu); |
| 7594 | skip_emulated_instruction(vcpu); |
| 7595 | return 1; |
| 7596 | } |
| 7597 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7598 | /* Emulate the VMPTRLD instruction */ |
| 7599 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 7600 | { |
| 7601 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7602 | gpa_t vmptr; |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7603 | |
| 7604 | if (!nested_vmx_check_permission(vcpu)) |
| 7605 | return 1; |
| 7606 | |
Bandan Das | 4291b58 | 2014-05-06 02:19:18 -0400 | [diff] [blame] | 7607 | if (nested_vmx_check_vmptr(vcpu, EXIT_REASON_VMPTRLD, &vmptr)) |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7608 | return 1; |
| 7609 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7610 | if (vmx->nested.current_vmptr != vmptr) { |
| 7611 | struct vmcs12 *new_vmcs12; |
| 7612 | struct page *page; |
| 7613 | page = nested_get_page(vcpu, vmptr); |
| 7614 | if (page == NULL) { |
| 7615 | nested_vmx_failInvalid(vcpu); |
| 7616 | skip_emulated_instruction(vcpu); |
| 7617 | return 1; |
| 7618 | } |
| 7619 | new_vmcs12 = kmap(page); |
| 7620 | if (new_vmcs12->revision_id != VMCS12_REVISION) { |
| 7621 | kunmap(page); |
| 7622 | nested_release_page_clean(page); |
| 7623 | nested_vmx_failValid(vcpu, |
| 7624 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 7625 | skip_emulated_instruction(vcpu); |
| 7626 | return 1; |
| 7627 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7628 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7629 | nested_release_vmcs12(vmx); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7630 | vmx->nested.current_vmptr = vmptr; |
| 7631 | vmx->nested.current_vmcs12 = new_vmcs12; |
| 7632 | vmx->nested.current_vmcs12_page = page; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7633 | /* |
| 7634 | * Load VMCS12 from guest memory since it is not already |
| 7635 | * cached. |
| 7636 | */ |
| 7637 | memcpy(vmx->nested.cached_vmcs12, |
| 7638 | vmx->nested.current_vmcs12, VMCS12_SIZE); |
| 7639 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7640 | if (enable_shadow_vmcs) { |
Xiao Guangrong | 7ec3629 | 2015-09-09 14:05:56 +0800 | [diff] [blame] | 7641 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 7642 | SECONDARY_EXEC_SHADOW_VMCS); |
Abel Gordon | 8a1b9dd | 2013-04-18 14:39:55 +0300 | [diff] [blame] | 7643 | vmcs_write64(VMCS_LINK_POINTER, |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7644 | __pa(vmx->vmcs01.shadow_vmcs)); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7645 | vmx->nested.sync_shadow_vmcs = true; |
| 7646 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7647 | } |
| 7648 | |
| 7649 | nested_vmx_succeed(vcpu); |
| 7650 | skip_emulated_instruction(vcpu); |
| 7651 | return 1; |
| 7652 | } |
| 7653 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7654 | /* Emulate the VMPTRST instruction */ |
| 7655 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 7656 | { |
| 7657 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7658 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7659 | gva_t vmcs_gva; |
| 7660 | struct x86_exception e; |
| 7661 | |
| 7662 | if (!nested_vmx_check_permission(vcpu)) |
| 7663 | return 1; |
| 7664 | |
| 7665 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7666 | vmx_instruction_info, true, &vmcs_gva)) |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7667 | return 1; |
| 7668 | /* ok to use *_system, as nested_vmx_check_permission verified cpl=0 */ |
Paolo Bonzini | 838b0e9 | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 7669 | if (kvm_write_guest_virt_system(vcpu, vmcs_gva, |
| 7670 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 7671 | sizeof(u64), &e)) { |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7672 | kvm_inject_page_fault(vcpu, &e); |
| 7673 | return 1; |
| 7674 | } |
| 7675 | nested_vmx_succeed(vcpu); |
| 7676 | skip_emulated_instruction(vcpu); |
| 7677 | return 1; |
| 7678 | } |
| 7679 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7680 | /* Emulate the INVEPT instruction */ |
| 7681 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 7682 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7683 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7684 | u32 vmx_instruction_info, types; |
| 7685 | unsigned long type; |
| 7686 | gva_t gva; |
| 7687 | struct x86_exception e; |
| 7688 | struct { |
| 7689 | u64 eptp, gpa; |
| 7690 | } operand; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7691 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7692 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
| 7693 | SECONDARY_EXEC_ENABLE_EPT) || |
| 7694 | !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7695 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7696 | return 1; |
| 7697 | } |
| 7698 | |
| 7699 | if (!nested_vmx_check_permission(vcpu)) |
| 7700 | return 1; |
| 7701 | |
| 7702 | if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) { |
| 7703 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7704 | return 1; |
| 7705 | } |
| 7706 | |
| 7707 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7708 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7709 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7710 | 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] | 7711 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 7712 | if (type >= 32 || !(types & (1 << type))) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7713 | nested_vmx_failValid(vcpu, |
| 7714 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Paolo Bonzini | 2849eb4 | 2016-03-18 16:53:29 +0100 | [diff] [blame] | 7715 | skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7716 | return 1; |
| 7717 | } |
| 7718 | |
| 7719 | /* According to the Intel VMX instruction reference, the memory |
| 7720 | * operand is read even if it isn't needed (e.g., for type==global) |
| 7721 | */ |
| 7722 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7723 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7724 | return 1; |
Paolo Bonzini | 838b0e9 | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 7725 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7726 | kvm_inject_page_fault(vcpu, &e); |
| 7727 | return 1; |
| 7728 | } |
| 7729 | |
| 7730 | switch (type) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7731 | case VMX_EPT_EXTENT_GLOBAL: |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 7732 | /* |
| 7733 | * TODO: track mappings and invalidate |
| 7734 | * single context requests appropriately |
| 7735 | */ |
| 7736 | case VMX_EPT_EXTENT_CONTEXT: |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7737 | kvm_mmu_sync_roots(vcpu); |
Liang Chen | 77c3913 | 2014-09-18 12:38:37 -0400 | [diff] [blame] | 7738 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7739 | nested_vmx_succeed(vcpu); |
| 7740 | break; |
| 7741 | default: |
| 7742 | BUG_ON(1); |
| 7743 | break; |
| 7744 | } |
| 7745 | |
| 7746 | skip_emulated_instruction(vcpu); |
| 7747 | return 1; |
| 7748 | } |
| 7749 | |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7750 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 7751 | { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7752 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7753 | u32 vmx_instruction_info; |
| 7754 | unsigned long type, types; |
| 7755 | gva_t gva; |
| 7756 | struct x86_exception e; |
| 7757 | int vpid; |
| 7758 | |
| 7759 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
| 7760 | SECONDARY_EXEC_ENABLE_VPID) || |
| 7761 | !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) { |
| 7762 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7763 | return 1; |
| 7764 | } |
| 7765 | |
| 7766 | if (!nested_vmx_check_permission(vcpu)) |
| 7767 | return 1; |
| 7768 | |
| 7769 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7770 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 7771 | |
Jan Dakinevich | 04c776e | 2018-02-23 11:42:18 +0100 | [diff] [blame] | 7772 | types = (vmx->nested.nested_vmx_vpid_caps & |
| 7773 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7774 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 7775 | if (type >= 32 || !(types & (1 << type))) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7776 | nested_vmx_failValid(vcpu, |
| 7777 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Paolo Bonzini | f6870ee | 2016-03-18 16:53:42 +0100 | [diff] [blame] | 7778 | skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7779 | return 1; |
| 7780 | } |
| 7781 | |
| 7782 | /* according to the intel vmx instruction reference, the memory |
| 7783 | * operand is read even if it isn't needed (e.g., for type==global) |
| 7784 | */ |
| 7785 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 7786 | vmx_instruction_info, false, &gva)) |
| 7787 | return 1; |
Paolo Bonzini | 838b0e9 | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 7788 | if (kvm_read_guest_virt(vcpu, gva, &vpid, sizeof(u32), &e)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7789 | kvm_inject_page_fault(vcpu, &e); |
| 7790 | return 1; |
| 7791 | } |
| 7792 | |
| 7793 | switch (type) { |
Jan Dakinevich | 04c776e | 2018-02-23 11:42:18 +0100 | [diff] [blame] | 7794 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 7795 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
Jan Dakinevich | 04c776e | 2018-02-23 11:42:18 +0100 | [diff] [blame] | 7796 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
| 7797 | if (!vpid) { |
| 7798 | nested_vmx_failValid(vcpu, |
| 7799 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 7800 | skip_emulated_instruction(vcpu); |
| 7801 | return 1; |
| 7802 | } |
| 7803 | break; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7804 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7805 | break; |
| 7806 | default: |
Jan Dakinevich | 04c776e | 2018-02-23 11:42:18 +0100 | [diff] [blame] | 7807 | WARN_ON_ONCE(1); |
| 7808 | skip_emulated_instruction(vcpu); |
| 7809 | return 1; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7810 | } |
| 7811 | |
Jan Dakinevich | 04c776e | 2018-02-23 11:42:18 +0100 | [diff] [blame] | 7812 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02); |
| 7813 | nested_vmx_succeed(vcpu); |
| 7814 | |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7815 | skip_emulated_instruction(vcpu); |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7816 | return 1; |
| 7817 | } |
| 7818 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7819 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 7820 | { |
| 7821 | unsigned long exit_qualification; |
| 7822 | |
| 7823 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 7824 | |
| 7825 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7826 | |
| 7827 | /* |
| 7828 | * PML buffer FULL happened while executing iret from NMI, |
| 7829 | * "blocked by NMI" bit has to be set before next VM entry. |
| 7830 | */ |
| 7831 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
| 7832 | cpu_has_virtual_nmis() && |
| 7833 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 7834 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 7835 | GUEST_INTR_STATE_NMI); |
| 7836 | |
| 7837 | /* |
| 7838 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 7839 | * here.., and there's no userspace involvement needed for PML. |
| 7840 | */ |
| 7841 | return 1; |
| 7842 | } |
| 7843 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7844 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 7845 | { |
| 7846 | kvm_lapic_expired_hv_timer(vcpu); |
| 7847 | return 1; |
| 7848 | } |
| 7849 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 7850 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7851 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 7852 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 7853 | * to be done to userspace and return 0. |
| 7854 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 7855 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7856 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 7857 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 7858 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7859 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7860 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7861 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 7862 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 7863 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 7864 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 7865 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 7866 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 7867 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7868 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7869 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7870 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7871 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7872 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7873 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7874 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7875 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7876 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7877 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7878 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7879 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 7880 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7881 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 7882 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 7883 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7884 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7885 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7886 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7887 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 7888 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7889 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 7890 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7891 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7892 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 7893 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7894 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7895 | [EXIT_REASON_INVEPT] = handle_invept, |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7896 | [EXIT_REASON_INVVPID] = handle_invvpid, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7897 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 7898 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7899 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7900 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7901 | }; |
| 7902 | |
| 7903 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 7904 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7905 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7906 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 7907 | struct vmcs12 *vmcs12) |
| 7908 | { |
| 7909 | unsigned long exit_qualification; |
| 7910 | gpa_t bitmap, last_bitmap; |
| 7911 | unsigned int port; |
| 7912 | int size; |
| 7913 | u8 b; |
| 7914 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7915 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
Zhihui Zhang | 2f0a639 | 2013-12-30 15:56:29 -0500 | [diff] [blame] | 7916 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7917 | |
| 7918 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7919 | |
| 7920 | port = exit_qualification >> 16; |
| 7921 | size = (exit_qualification & 7) + 1; |
| 7922 | |
| 7923 | last_bitmap = (gpa_t)-1; |
| 7924 | b = -1; |
| 7925 | |
| 7926 | while (size > 0) { |
| 7927 | if (port < 0x8000) |
| 7928 | bitmap = vmcs12->io_bitmap_a; |
| 7929 | else if (port < 0x10000) |
| 7930 | bitmap = vmcs12->io_bitmap_b; |
| 7931 | else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7932 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7933 | bitmap += (port & 0x7fff) / 8; |
| 7934 | |
| 7935 | if (last_bitmap != bitmap) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 7936 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7937 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7938 | if (b & (1 << (port & 7))) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7939 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7940 | |
| 7941 | port++; |
| 7942 | size--; |
| 7943 | last_bitmap = bitmap; |
| 7944 | } |
| 7945 | |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7946 | return false; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7947 | } |
| 7948 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7949 | /* |
| 7950 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 7951 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 7952 | * disinterest in the current event (read or write a specific MSR) by using an |
| 7953 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 7954 | */ |
| 7955 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 7956 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 7957 | { |
| 7958 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 7959 | gpa_t bitmap; |
| 7960 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 7961 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7962 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7963 | |
| 7964 | /* |
| 7965 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 7966 | * for the four combinations of read/write and low/high MSR numbers. |
| 7967 | * First we need to figure out which of the four to use: |
| 7968 | */ |
| 7969 | bitmap = vmcs12->msr_bitmap; |
| 7970 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 7971 | bitmap += 2048; |
| 7972 | if (msr_index >= 0xc0000000) { |
| 7973 | msr_index -= 0xc0000000; |
| 7974 | bitmap += 1024; |
| 7975 | } |
| 7976 | |
| 7977 | /* Then read the msr_index'th bit from this bitmap: */ |
| 7978 | if (msr_index < 1024*8) { |
| 7979 | unsigned char b; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 7980 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
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 | return 1 & (b >> (msr_index & 7)); |
| 7983 | } else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7984 | return true; /* let L1 handle the wrong parameter */ |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7985 | } |
| 7986 | |
| 7987 | /* |
| 7988 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 7989 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 7990 | * intercept (via guest_host_mask etc.) the current event. |
| 7991 | */ |
| 7992 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 7993 | struct vmcs12 *vmcs12) |
| 7994 | { |
| 7995 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7996 | int cr = exit_qualification & 15; |
Jan H. Schönherr | a74bec4 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 7997 | int reg; |
| 7998 | unsigned long val; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7999 | |
| 8000 | switch ((exit_qualification >> 4) & 3) { |
| 8001 | case 0: /* mov to cr */ |
Jan H. Schönherr | a74bec4 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 8002 | reg = (exit_qualification >> 8) & 15; |
| 8003 | val = kvm_register_readl(vcpu, reg); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8004 | switch (cr) { |
| 8005 | case 0: |
| 8006 | if (vmcs12->cr0_guest_host_mask & |
| 8007 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8008 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8009 | break; |
| 8010 | case 3: |
| 8011 | if ((vmcs12->cr3_target_count >= 1 && |
| 8012 | vmcs12->cr3_target_value0 == val) || |
| 8013 | (vmcs12->cr3_target_count >= 2 && |
| 8014 | vmcs12->cr3_target_value1 == val) || |
| 8015 | (vmcs12->cr3_target_count >= 3 && |
| 8016 | vmcs12->cr3_target_value2 == val) || |
| 8017 | (vmcs12->cr3_target_count >= 4 && |
| 8018 | vmcs12->cr3_target_value3 == val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8019 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8020 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8021 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8022 | break; |
| 8023 | case 4: |
| 8024 | if (vmcs12->cr4_guest_host_mask & |
| 8025 | (vmcs12->cr4_read_shadow ^ val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8026 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8027 | break; |
| 8028 | case 8: |
| 8029 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8030 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8031 | break; |
| 8032 | } |
| 8033 | break; |
| 8034 | case 2: /* clts */ |
| 8035 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 8036 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8037 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8038 | break; |
| 8039 | case 1: /* mov from cr */ |
| 8040 | switch (cr) { |
| 8041 | case 3: |
| 8042 | if (vmcs12->cpu_based_vm_exec_control & |
| 8043 | CPU_BASED_CR3_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8044 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8045 | break; |
| 8046 | case 8: |
| 8047 | if (vmcs12->cpu_based_vm_exec_control & |
| 8048 | CPU_BASED_CR8_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8049 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8050 | break; |
| 8051 | } |
| 8052 | break; |
| 8053 | case 3: /* lmsw */ |
| 8054 | /* |
| 8055 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 8056 | * cr0. Other attempted changes are ignored, with no exit. |
| 8057 | */ |
Jan H. Schönherr | a74bec4 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 8058 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8059 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 8060 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8061 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8062 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 8063 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 8064 | (val & 0x1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8065 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8066 | break; |
| 8067 | } |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8068 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8069 | } |
| 8070 | |
| 8071 | /* |
| 8072 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 8073 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 8074 | * when in is_guest_mode (L2). |
| 8075 | */ |
| 8076 | static bool nested_vmx_exit_handled(struct kvm_vcpu *vcpu) |
| 8077 | { |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8078 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8079 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8080 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Jan Kiszka | 957c897 | 2013-02-24 14:11:34 +0100 | [diff] [blame] | 8081 | u32 exit_reason = vmx->exit_reason; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8082 | |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 8083 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 8084 | vmcs_readl(EXIT_QUALIFICATION), |
| 8085 | vmx->idt_vectoring_info, |
| 8086 | intr_info, |
| 8087 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 8088 | KVM_ISA_VMX); |
| 8089 | |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 8090 | /* |
| 8091 | * The host physical addresses of some pages of guest memory |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8092 | * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC |
| 8093 | * Page). The CPU may write to these pages via their host |
| 8094 | * physical address while L2 is running, bypassing any |
| 8095 | * address-translation-based dirty tracking (e.g. EPT write |
| 8096 | * protection). |
David Matlack | b7649e1 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 8097 | * |
| 8098 | * Mark them dirty on every exit from L2 to prevent them from |
| 8099 | * getting out of sync with dirty tracking. |
| 8100 | */ |
| 8101 | nested_mark_vmcs12_pages_dirty(vcpu); |
| 8102 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8103 | if (vmx->nested.nested_run_pending) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8104 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8105 | |
| 8106 | if (unlikely(vmx->fail)) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 8107 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 8108 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8109 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8110 | } |
| 8111 | |
| 8112 | switch (exit_reason) { |
| 8113 | case EXIT_REASON_EXCEPTION_NMI: |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 8114 | if (is_nmi(intr_info)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8115 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8116 | else if (is_page_fault(intr_info)) |
| 8117 | return enable_ept; |
Anthoine Bourgeois | e504c90 | 2013-11-13 11:45:37 +0100 | [diff] [blame] | 8118 | else if (is_no_device(intr_info) && |
Paolo Bonzini | ccf9844 | 2014-02-27 22:54:11 +0100 | [diff] [blame] | 8119 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8120 | return false; |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 8121 | else if (is_debug(intr_info) && |
| 8122 | vcpu->guest_debug & |
| 8123 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 8124 | return false; |
| 8125 | else if (is_breakpoint(intr_info) && |
| 8126 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 8127 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8128 | return vmcs12->exception_bitmap & |
| 8129 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 8130 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8131 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8132 | case EXIT_REASON_TRIPLE_FAULT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8133 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8134 | case EXIT_REASON_PENDING_INTERRUPT: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 8135 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8136 | case EXIT_REASON_NMI_WINDOW: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 8137 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8138 | case EXIT_REASON_TASK_SWITCH: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8139 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8140 | case EXIT_REASON_CPUID: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8141 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8142 | case EXIT_REASON_HLT: |
| 8143 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 8144 | case EXIT_REASON_INVD: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8145 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8146 | case EXIT_REASON_INVLPG: |
| 8147 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 8148 | case EXIT_REASON_RDPMC: |
| 8149 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 8150 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8151 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 8152 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 8153 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 8154 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: |
| 8155 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: |
| 8156 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8157 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8158 | /* |
| 8159 | * VMX instructions trap unconditionally. This allows L1 to |
| 8160 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 8161 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8162 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8163 | case EXIT_REASON_CR_ACCESS: |
| 8164 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 8165 | case EXIT_REASON_DR_ACCESS: |
| 8166 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 8167 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8168 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8169 | case EXIT_REASON_MSR_READ: |
| 8170 | case EXIT_REASON_MSR_WRITE: |
| 8171 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 8172 | case EXIT_REASON_INVALID_STATE: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8173 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8174 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 8175 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 8176 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
| 8177 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8178 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 8179 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 8180 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 8181 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 8182 | nested_cpu_has2(vmcs12, |
| 8183 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 8184 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8185 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8186 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8187 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8188 | case EXIT_REASON_APIC_ACCESS: |
| 8189 | return nested_cpu_has2(vmcs12, |
| 8190 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 8191 | case EXIT_REASON_APIC_WRITE: |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 8192 | case EXIT_REASON_EOI_INDUCED: |
| 8193 | /* apic_write and eoi_induced should exit unconditionally. */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8194 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8195 | case EXIT_REASON_EPT_VIOLATION: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 8196 | /* |
| 8197 | * L0 always deals with the EPT violation. If nested EPT is |
| 8198 | * used, and the nested mmu code discovers that the address is |
| 8199 | * missing in the guest EPT table (EPT12), the EPT violation |
| 8200 | * will be injected with nested_ept_inject_page_fault() |
| 8201 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8202 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8203 | case EXIT_REASON_EPT_MISCONFIG: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 8204 | /* |
| 8205 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 8206 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 8207 | * (EPT on EPT). So any problems with the structure of the |
| 8208 | * table is L0's fault. |
| 8209 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8210 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8211 | case EXIT_REASON_WBINVD: |
| 8212 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 8213 | case EXIT_REASON_XSETBV: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8214 | return true; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 8215 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 8216 | /* |
| 8217 | * This should never happen, since it is not possible to |
| 8218 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 8219 | * If if it were, XSS would have to be checked against |
| 8220 | * the XSS exit bitmap in vmcs12. |
| 8221 | */ |
| 8222 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 55123e3 | 2016-07-06 18:29:58 +0800 | [diff] [blame] | 8223 | case EXIT_REASON_PREEMPTION_TIMER: |
| 8224 | return false; |
Ladi Prosek | d0ee363 | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 8225 | case EXIT_REASON_PML_FULL: |
| 8226 | /* We don't expose PML support to L1. */ |
| 8227 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8228 | default: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8229 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8230 | } |
| 8231 | } |
| 8232 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 8233 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 8234 | { |
| 8235 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 8236 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8237 | } |
| 8238 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 8239 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8240 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 8241 | if (vmx->pml_pg) { |
| 8242 | __free_page(vmx->pml_pg); |
| 8243 | vmx->pml_pg = NULL; |
| 8244 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8245 | } |
| 8246 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8247 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8248 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8249 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8250 | u64 *pml_buf; |
| 8251 | u16 pml_idx; |
| 8252 | |
| 8253 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 8254 | |
| 8255 | /* Do nothing if PML buffer is empty */ |
| 8256 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 8257 | return; |
| 8258 | |
| 8259 | /* PML index always points to next available PML buffer entity */ |
| 8260 | if (pml_idx >= PML_ENTITY_NUM) |
| 8261 | pml_idx = 0; |
| 8262 | else |
| 8263 | pml_idx++; |
| 8264 | |
| 8265 | pml_buf = page_address(vmx->pml_pg); |
| 8266 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 8267 | u64 gpa; |
| 8268 | |
| 8269 | gpa = pml_buf[pml_idx]; |
| 8270 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8271 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8272 | } |
| 8273 | |
| 8274 | /* reset PML index */ |
| 8275 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 8276 | } |
| 8277 | |
| 8278 | /* |
| 8279 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 8280 | * Called before reporting dirty_bitmap to userspace. |
| 8281 | */ |
| 8282 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 8283 | { |
| 8284 | int i; |
| 8285 | struct kvm_vcpu *vcpu; |
| 8286 | /* |
| 8287 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 8288 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 8289 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 8290 | * buffer. |
| 8291 | */ |
| 8292 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 8293 | kvm_vcpu_kick(vcpu); |
| 8294 | } |
| 8295 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8296 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 8297 | { |
| 8298 | 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] | 8299 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8300 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 8301 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 8302 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 8303 | } |
| 8304 | |
| 8305 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 8306 | { |
| 8307 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 8308 | name, vmcs_read32(limit), |
| 8309 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 8310 | } |
| 8311 | |
| 8312 | static void dump_vmcs(void) |
| 8313 | { |
| 8314 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 8315 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 8316 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 8317 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 8318 | u32 secondary_exec_control = 0; |
| 8319 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 8320 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8321 | int i, n; |
| 8322 | |
| 8323 | if (cpu_has_secondary_exec_ctrls()) |
| 8324 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 8325 | |
| 8326 | pr_err("*** Guest State ***\n"); |
| 8327 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 8328 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 8329 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 8330 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 8331 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 8332 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 8333 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 8334 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 8335 | { |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8336 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 8337 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 8338 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 8339 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8340 | } |
| 8341 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 8342 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 8343 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 8344 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 8345 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 8346 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 8347 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 8348 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 8349 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 8350 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 8351 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 8352 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 8353 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 8354 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 8355 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 8356 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 8357 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 8358 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 8359 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8360 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 8361 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 8362 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 8363 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8364 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
| 8365 | if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8366 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 8367 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8368 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8369 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8370 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 8371 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 8372 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 8373 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 8374 | pr_err("InterruptStatus = %04x\n", |
| 8375 | vmcs_read16(GUEST_INTR_STATUS)); |
| 8376 | |
| 8377 | pr_err("*** Host State ***\n"); |
| 8378 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 8379 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 8380 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 8381 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 8382 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 8383 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 8384 | vmcs_read16(HOST_TR_SELECTOR)); |
| 8385 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 8386 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 8387 | vmcs_readl(HOST_TR_BASE)); |
| 8388 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 8389 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 8390 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 8391 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 8392 | vmcs_readl(HOST_CR4)); |
| 8393 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 8394 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 8395 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 8396 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 8397 | 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] | 8398 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 8399 | vmcs_read64(HOST_IA32_EFER), |
| 8400 | vmcs_read64(HOST_IA32_PAT)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8401 | if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8402 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 8403 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8404 | |
| 8405 | pr_err("*** Control State ***\n"); |
| 8406 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 8407 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 8408 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 8409 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 8410 | vmcs_read32(EXCEPTION_BITMAP), |
| 8411 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 8412 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 8413 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 8414 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 8415 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 8416 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 8417 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 8418 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8419 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 8420 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 8421 | pr_err(" reason=%08x qualification=%016lx\n", |
| 8422 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 8423 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 8424 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 8425 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8426 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 8427 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8428 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 8429 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8430 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 8431 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 8432 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 8433 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 8434 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b4 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8435 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8436 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 8437 | for (i = 0; i + 1 < n; i += 4) |
| 8438 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 8439 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 8440 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 8441 | if (i < n) |
| 8442 | pr_err("CR3 target%u=%016lx\n", |
| 8443 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 8444 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 8445 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 8446 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 8447 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 8448 | pr_err("Virtual processor ID = 0x%04x\n", |
| 8449 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 8450 | } |
| 8451 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8452 | /* |
| 8453 | * The guest has exited. See if we can fix it or if we need userspace |
| 8454 | * assistance. |
| 8455 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8456 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8457 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8458 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8459 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 8460 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8461 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 8462 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
| 8463 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8464 | /* |
| 8465 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 8466 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 8467 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 8468 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 8469 | * flushed already. |
| 8470 | */ |
| 8471 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8472 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8473 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 8474 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 8475 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 8476 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 8477 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8478 | if (is_guest_mode(vcpu) && nested_vmx_exit_handled(vcpu)) { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 8479 | nested_vmx_vmexit(vcpu, exit_reason, |
| 8480 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8481 | vmcs_readl(EXIT_QUALIFICATION)); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8482 | return 1; |
| 8483 | } |
| 8484 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 8485 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8486 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 8487 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 8488 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 8489 | = exit_reason; |
| 8490 | return 0; |
| 8491 | } |
| 8492 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8493 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8494 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 8495 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8496 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 8497 | return 0; |
| 8498 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8499 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 8500 | /* |
| 8501 | * Note: |
| 8502 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 8503 | * delivery event since it indicates guest is accessing MMIO. |
| 8504 | * The vm-exit can be triggered again after return to guest that |
| 8505 | * will cause infinite loop. |
| 8506 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 8507 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 8508 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 8509 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 8510 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 8511 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 8512 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 8513 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
| 8514 | vcpu->run->internal.ndata = 2; |
| 8515 | vcpu->run->internal.data[0] = vectoring_info; |
| 8516 | vcpu->run->internal.data[1] = exit_reason; |
| 8517 | return 0; |
| 8518 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8519 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8520 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked && |
| 8521 | !(is_guest_mode(vcpu) && nested_cpu_has_virtual_nmis( |
Nadav Har'El | f5c4368 | 2013-08-05 11:07:20 +0300 | [diff] [blame] | 8522 | get_vmcs12(vcpu))))) { |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 8523 | if (vmx_interrupt_allowed(vcpu)) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8524 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8525 | } else if (vmx->vnmi_blocked_time > 1000000000LL && |
Jan Kiszka | 4531220 | 2008-12-11 16:54:54 +0100 | [diff] [blame] | 8526 | vcpu->arch.nmi_pending) { |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8527 | /* |
| 8528 | * This CPU don't support us in finding the end of an |
| 8529 | * NMI-blocked window if the guest runs with IRQs |
| 8530 | * disabled. So we pull the trigger after 1 s of |
| 8531 | * futile waiting, but inform the user about this. |
| 8532 | */ |
| 8533 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 8534 | "state on VCPU %d after 1 s timeout\n", |
| 8535 | __func__, vcpu->vcpu_id); |
| 8536 | vmx->soft_vnmi_blocked = 0; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8537 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8538 | } |
| 8539 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8540 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 8541 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8542 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8543 | else { |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 8544 | WARN_ONCE(1, "vmx: unexpected exit reason 0x%x\n", exit_reason); |
| 8545 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8546 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8547 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8548 | } |
| 8549 | |
Paolo Bonzini | b3d648a | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 8550 | /* |
| 8551 | * Software based L1D cache flush which is used when microcode providing |
| 8552 | * the cache control MSR is not loaded. |
| 8553 | * |
| 8554 | * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to |
| 8555 | * flush it is required to read in 64 KiB because the replacement algorithm |
| 8556 | * is not exactly LRU. This could be sized at runtime via topology |
| 8557 | * information but as all relevant affected CPUs have 32KiB L1D cache size |
| 8558 | * there is no point in doing so. |
| 8559 | */ |
| 8560 | #define L1D_CACHE_ORDER 4 |
| 8561 | static void *vmx_l1d_flush_pages; |
| 8562 | |
Paolo Bonzini | b3dc63c | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 8563 | static void vmx_l1d_flush(struct kvm_vcpu *vcpu) |
Paolo Bonzini | b3d648a | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 8564 | { |
| 8565 | int size = PAGE_SIZE << L1D_CACHE_ORDER; |
Paolo Bonzini | b3dc63c | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 8566 | bool always; |
| 8567 | |
| 8568 | /* |
| 8569 | * If the mitigation mode is 'flush always', keep the flush bit |
| 8570 | * set, otherwise clear it. It gets set again either from |
| 8571 | * vcpu_run() or from one of the unsafe VMEXIT handlers. |
| 8572 | */ |
| 8573 | always = vmentry_l1d_flush == VMENTER_L1D_FLUSH_ALWAYS; |
| 8574 | vcpu->arch.l1tf_flush_l1d = always; |
| 8575 | |
| 8576 | vcpu->stat.l1d_flush++; |
Paolo Bonzini | b3d648a | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 8577 | |
Paolo Bonzini | acca8a7 | 2018-07-02 13:03:48 +0200 | [diff] [blame] | 8578 | if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) { |
| 8579 | wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH); |
| 8580 | return; |
| 8581 | } |
| 8582 | |
Paolo Bonzini | b3d648a | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 8583 | asm volatile( |
| 8584 | /* First ensure the pages are in the TLB */ |
| 8585 | "xorl %%eax, %%eax\n" |
| 8586 | ".Lpopulate_tlb:\n\t" |
| 8587 | "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t" |
| 8588 | "addl $4096, %%eax\n\t" |
| 8589 | "cmpl %%eax, %[size]\n\t" |
| 8590 | "jne .Lpopulate_tlb\n\t" |
| 8591 | "xorl %%eax, %%eax\n\t" |
| 8592 | "cpuid\n\t" |
| 8593 | /* Now fill the cache */ |
| 8594 | "xorl %%eax, %%eax\n" |
| 8595 | ".Lfill_cache:\n" |
| 8596 | "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t" |
| 8597 | "addl $64, %%eax\n\t" |
| 8598 | "cmpl %%eax, %[size]\n\t" |
| 8599 | "jne .Lfill_cache\n\t" |
| 8600 | "lfence\n" |
| 8601 | :: [empty_zp] "r" (vmx_l1d_flush_pages), |
| 8602 | [size] "r" (size) |
| 8603 | : "eax", "ebx", "ecx", "edx"); |
| 8604 | } |
| 8605 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8606 | 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] | 8607 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8608 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 8609 | |
| 8610 | if (is_guest_mode(vcpu) && |
| 8611 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 8612 | return; |
| 8613 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8614 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 8615 | vmcs_write32(TPR_THRESHOLD, 0); |
| 8616 | return; |
| 8617 | } |
| 8618 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8619 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 8620 | } |
| 8621 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8622 | static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
| 8623 | { |
| 8624 | u32 sec_exec_control; |
| 8625 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 8626 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 8627 | if (is_guest_mode(vcpu)) { |
| 8628 | to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true; |
| 8629 | return; |
| 8630 | } |
| 8631 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 8632 | if (!cpu_has_vmx_virtualize_x2apic_mode()) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8633 | return; |
| 8634 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 8635 | if (!cpu_need_tpr_shadow(vcpu)) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8636 | return; |
| 8637 | |
| 8638 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 8639 | |
| 8640 | if (set) { |
| 8641 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 8642 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 8643 | } else { |
| 8644 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 8645 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 8646 | vmx_flush_tlb_ept_only(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8647 | } |
| 8648 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 8649 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 8650 | vmx_update_msr_bitmap(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8651 | } |
| 8652 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8653 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 8654 | { |
| 8655 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8656 | |
| 8657 | /* |
| 8658 | * Currently we do not handle the nested case where L2 has an |
| 8659 | * APIC access page of its own; that page is still pinned. |
| 8660 | * Hence, we skip the case where the VCPU is in guest mode _and_ |
| 8661 | * L1 prepared an APIC access page for L2. |
| 8662 | * |
| 8663 | * For the case where L1 and L2 share the same APIC access page |
| 8664 | * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear |
| 8665 | * in the vmcs12), this function will only update either the vmcs01 |
| 8666 | * or the vmcs02. If the former, the vmcs02 will be updated by |
| 8667 | * prepare_vmcs02. If the latter, the vmcs01 will be updated in |
| 8668 | * the next L2->L1 exit. |
| 8669 | */ |
| 8670 | if (!is_guest_mode(vcpu) || |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8671 | !nested_cpu_has2(get_vmcs12(&vmx->vcpu), |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 8672 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8673 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 8674 | vmx_flush_tlb_ept_only(vcpu); |
| 8675 | } |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8676 | } |
| 8677 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8678 | 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] | 8679 | { |
| 8680 | u16 status; |
| 8681 | u8 old; |
| 8682 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8683 | if (max_isr == -1) |
| 8684 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8685 | |
| 8686 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 8687 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8688 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8689 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8690 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8691 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 8692 | } |
| 8693 | } |
| 8694 | |
| 8695 | static void vmx_set_rvi(int vector) |
| 8696 | { |
| 8697 | u16 status; |
| 8698 | u8 old; |
| 8699 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8700 | if (vector == -1) |
| 8701 | vector = 0; |
| 8702 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8703 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 8704 | old = (u8)status & 0xff; |
| 8705 | if ((u8)vector != old) { |
| 8706 | status &= ~0xff; |
| 8707 | status |= (u8)vector; |
| 8708 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 8709 | } |
| 8710 | } |
| 8711 | |
| 8712 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 8713 | { |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8714 | if (!is_guest_mode(vcpu)) { |
| 8715 | vmx_set_rvi(max_irr); |
| 8716 | return; |
| 8717 | } |
| 8718 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8719 | if (max_irr == -1) |
| 8720 | return; |
| 8721 | |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8722 | /* |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8723 | * In guest mode. If a vmexit is needed, vmx_check_nested_events |
| 8724 | * handles it. |
| 8725 | */ |
| 8726 | if (nested_exit_on_intr(vcpu)) |
| 8727 | return; |
| 8728 | |
| 8729 | /* |
| 8730 | * Else, fall back to pre-APICv interrupt injection since L2 |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8731 | * is run without virtual interrupt delivery. |
| 8732 | */ |
| 8733 | if (!kvm_event_needs_reinjection(vcpu) && |
| 8734 | vmx_interrupt_allowed(vcpu)) { |
| 8735 | kvm_queue_interrupt(vcpu, max_irr, false); |
| 8736 | vmx_inject_irq(vcpu); |
| 8737 | } |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8738 | } |
| 8739 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 8740 | 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] | 8741 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 8742 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 8743 | return; |
| 8744 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8745 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 8746 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 8747 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 8748 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 8749 | } |
| 8750 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8751 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8752 | { |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8753 | u32 exit_intr_info; |
| 8754 | |
| 8755 | if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 8756 | || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
| 8757 | return; |
| 8758 | |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 8759 | vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8760 | exit_intr_info = vmx->exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8761 | |
| 8762 | /* Handle machine checks before interrupts are enabled */ |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8763 | if (is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8764 | kvm_machine_check(); |
| 8765 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8766 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | 3f618a0 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 8767 | if (is_nmi(exit_intr_info)) { |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 8768 | kvm_before_handle_nmi(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8769 | asm("int $2"); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 8770 | kvm_after_handle_nmi(&vmx->vcpu); |
| 8771 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8772 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8773 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8774 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 8775 | { |
| 8776 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8777 | register void *__sp asm(_ASM_SP); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8778 | |
| 8779 | /* |
| 8780 | * If external interrupt exists, IF bit is set in rflags/eflags on the |
| 8781 | * interrupt stack frame, and interrupt will be enabled on a return |
| 8782 | * from interrupt handler. |
| 8783 | */ |
| 8784 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 8785 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 8786 | unsigned int vector; |
| 8787 | unsigned long entry; |
| 8788 | gate_desc *desc; |
| 8789 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8790 | #ifdef CONFIG_X86_64 |
| 8791 | unsigned long tmp; |
| 8792 | #endif |
| 8793 | |
| 8794 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 8795 | desc = (gate_desc *)vmx->host_idt_base + vector; |
| 8796 | entry = gate_offset(*desc); |
| 8797 | asm volatile( |
| 8798 | #ifdef CONFIG_X86_64 |
| 8799 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 8800 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 8801 | "push $%c[ss]\n\t" |
| 8802 | "push %[sp]\n\t" |
| 8803 | #endif |
| 8804 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8805 | __ASM_SIZE(push) " $%c[cs]\n\t" |
Peter Zijlstra | ec86a1d | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 8806 | CALL_NOSPEC |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8807 | : |
| 8808 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8809 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8810 | #endif |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8811 | "+r"(__sp) |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8812 | : |
Peter Zijlstra | ec86a1d | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 8813 | THUNK_TARGET(entry), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8814 | [ss]"i"(__KERNEL_DS), |
| 8815 | [cs]"i"(__KERNEL_CS) |
| 8816 | ); |
Paolo Bonzini | b3dc63c | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 8817 | vcpu->arch.l1tf_flush_l1d = true; |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 8818 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8819 | } |
Josh Poimboeuf | 935893a | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 8820 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8821 | |
Tom Lendacky | b965592 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 8822 | static bool vmx_has_emulated_msr(int index) |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 8823 | { |
Tom Lendacky | b965592 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 8824 | switch (index) { |
| 8825 | case MSR_IA32_SMBASE: |
| 8826 | /* |
| 8827 | * We cannot do SMM unless we can run the guest in big |
| 8828 | * real mode. |
| 8829 | */ |
| 8830 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 8831 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 8832 | /* This is AMD only. */ |
| 8833 | return false; |
| 8834 | default: |
| 8835 | return true; |
| 8836 | } |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 8837 | } |
| 8838 | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 8839 | static bool vmx_mpx_supported(void) |
| 8840 | { |
| 8841 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && |
| 8842 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); |
| 8843 | } |
| 8844 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 8845 | static bool vmx_xsaves_supported(void) |
| 8846 | { |
| 8847 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 8848 | SECONDARY_EXEC_XSAVES; |
| 8849 | } |
| 8850 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8851 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 8852 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 8853 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8854 | bool unblock_nmi; |
| 8855 | u8 vector; |
| 8856 | bool idtv_info_valid; |
| 8857 | |
| 8858 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8859 | |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8860 | if (cpu_has_virtual_nmis()) { |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 8861 | if (vmx->nmi_known_unmasked) |
| 8862 | return; |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 8863 | /* |
| 8864 | * Can't use vmx->exit_intr_info since we're not sure what |
| 8865 | * the exit reason is. |
| 8866 | */ |
| 8867 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8868 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 8869 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 8870 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8871 | * SDM 3: 27.7.1.2 (September 2008) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8872 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 8873 | * a guest IRET fault. |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8874 | * SDM 3: 23.2.2 (September 2008) |
| 8875 | * Bit 12 is undefined in any of the following cases: |
| 8876 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 8877 | * information field. |
| 8878 | * If the VM exit is due to a double fault. |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8879 | */ |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8880 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 8881 | vector != DF_VECTOR && !idtv_info_valid) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8882 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 8883 | GUEST_INTR_STATE_NMI); |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 8884 | else |
| 8885 | vmx->nmi_known_unmasked = |
| 8886 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 8887 | & GUEST_INTR_STATE_NMI); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8888 | } else if (unlikely(vmx->soft_vnmi_blocked)) |
| 8889 | vmx->vnmi_blocked_time += |
| 8890 | ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8891 | } |
| 8892 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8893 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8894 | u32 idt_vectoring_info, |
| 8895 | int instr_len_field, |
| 8896 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8897 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8898 | u8 vector; |
| 8899 | int type; |
| 8900 | bool idtv_info_valid; |
| 8901 | |
| 8902 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8903 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8904 | vcpu->arch.nmi_injected = false; |
| 8905 | kvm_clear_exception_queue(vcpu); |
| 8906 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8907 | |
| 8908 | if (!idtv_info_valid) |
| 8909 | return; |
| 8910 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8911 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 8912 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8913 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 8914 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8915 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 8916 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8917 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8918 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8919 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8920 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8921 | * Clear bit "block by NMI" before VM entry if a NMI |
| 8922 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8923 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8924 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8925 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8926 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8927 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8928 | /* fall through */ |
| 8929 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 8930 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8931 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 8932 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 8933 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 8934 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8935 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8936 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8937 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8938 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8939 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8940 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8941 | break; |
| 8942 | default: |
| 8943 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 8944 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8945 | } |
| 8946 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8947 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 8948 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8949 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8950 | VM_EXIT_INSTRUCTION_LEN, |
| 8951 | IDT_VECTORING_ERROR_CODE); |
| 8952 | } |
| 8953 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 8954 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 8955 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8956 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 8957 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 8958 | VM_ENTRY_INSTRUCTION_LEN, |
| 8959 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 8960 | |
| 8961 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 8962 | } |
| 8963 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 8964 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 8965 | { |
| 8966 | int i, nr_msrs; |
| 8967 | struct perf_guest_switch_msr *msrs; |
| 8968 | |
| 8969 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 8970 | |
| 8971 | if (!msrs) |
| 8972 | return; |
| 8973 | |
| 8974 | for (i = 0; i < nr_msrs; i++) |
| 8975 | if (msrs[i].host == msrs[i].guest) |
| 8976 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 8977 | else |
| 8978 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
| 8979 | msrs[i].host); |
| 8980 | } |
| 8981 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8982 | void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
| 8983 | { |
| 8984 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8985 | u64 tscl; |
| 8986 | u32 delta_tsc; |
| 8987 | |
| 8988 | if (vmx->hv_deadline_tsc == -1) |
| 8989 | return; |
| 8990 | |
| 8991 | tscl = rdtsc(); |
| 8992 | if (vmx->hv_deadline_tsc > tscl) |
| 8993 | /* sure to be 32 bit only because checked on set_hv_timer */ |
| 8994 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 8995 | cpu_preemption_timer_multi); |
| 8996 | else |
| 8997 | delta_tsc = 0; |
| 8998 | |
| 8999 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); |
| 9000 | } |
| 9001 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 9002 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9003 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9004 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 9005 | unsigned long debugctlmsr, cr4; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9006 | |
| 9007 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
| 9008 | if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) |
| 9009 | vmx->entry_time = ktime_get(); |
| 9010 | |
| 9011 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 9012 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 9013 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9014 | return; |
| 9015 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 9016 | if (vmx->ple_window_dirty) { |
| 9017 | vmx->ple_window_dirty = false; |
| 9018 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 9019 | } |
| 9020 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 9021 | if (vmx->nested.sync_shadow_vmcs) { |
| 9022 | copy_vmcs12_to_shadow(vmx); |
| 9023 | vmx->nested.sync_shadow_vmcs = false; |
| 9024 | } |
| 9025 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9026 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 9027 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 9028 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 9029 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 9030 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 9031 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 9032 | if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) { |
| 9033 | vmcs_writel(HOST_CR4, cr4); |
| 9034 | vmx->host_state.vmcs_host_cr4 = cr4; |
| 9035 | } |
| 9036 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9037 | /* When single-stepping over STI and MOV SS, we must clear the |
| 9038 | * corresponding interruptibility bits in the guest state. Otherwise |
| 9039 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 9040 | * exceptions being set, but that's not correct for the guest debugging |
| 9041 | * case. */ |
| 9042 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 9043 | vmx_set_interrupt_shadow(vcpu, 0); |
| 9044 | |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 9045 | if (vmx->guest_pkru_valid) |
| 9046 | __write_pkru(vmx->guest_pkru); |
| 9047 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 9048 | atomic_switch_perf_msrs(vmx); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 9049 | debugctlmsr = get_debugctlmsr(); |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 9050 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 9051 | vmx_arm_hv_timer(vcpu); |
| 9052 | |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9053 | /* |
| 9054 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 9055 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 9056 | * is no need to worry about the conditional branch over the wrmsr |
| 9057 | * being speculatively taken. |
| 9058 | */ |
Thomas Gleixner | 1189cbf | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 9059 | x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9060 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9061 | vmx->__launched = vmx->loaded_vmcs->launched; |
Thomas Gleixner | 1189cbf | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 9062 | |
Paolo Bonzini | b3dc63c | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 9063 | if (static_branch_unlikely(&vmx_l1d_should_flush)) { |
| 9064 | if (vcpu->arch.l1tf_flush_l1d) |
| 9065 | vmx_l1d_flush(vcpu); |
| 9066 | } |
| 9067 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9068 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9069 | /* Store host registers */ |
Avi Kivity | b188c81 | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9070 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 9071 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 9072 | "push %%" _ASM_CX " \n\t" |
| 9073 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 9074 | "je 1f \n\t" |
Avi Kivity | b188c81 | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9075 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 9076 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 9077 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 9078 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81 | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9079 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 9080 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 9081 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 9082 | "je 2f \n\t" |
Avi Kivity | b188c81 | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9083 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 9084 | "2: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9085 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9086 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9087 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81 | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9088 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 9089 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 9090 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 9091 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 9092 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 9093 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 9094 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9095 | "mov %c[r8](%0), %%r8 \n\t" |
| 9096 | "mov %c[r9](%0), %%r9 \n\t" |
| 9097 | "mov %c[r10](%0), %%r10 \n\t" |
| 9098 | "mov %c[r11](%0), %%r11 \n\t" |
| 9099 | "mov %c[r12](%0), %%r12 \n\t" |
| 9100 | "mov %c[r13](%0), %%r13 \n\t" |
| 9101 | "mov %c[r14](%0), %%r14 \n\t" |
| 9102 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9103 | #endif |
Avi Kivity | b188c81 | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9104 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 9105 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9106 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 9107 | "jne 1f \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 9108 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 9109 | "jmp 2f \n\t" |
| 9110 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" |
| 9111 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9112 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81 | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9113 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 9114 | "pop %0 \n\t" |
Jim Mattson | 491c0ca | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 9115 | "setbe %c[fail](%0)\n\t" |
Avi Kivity | b188c81 | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9116 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 9117 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 9118 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 9119 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 9120 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 9121 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 9122 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 9123 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9124 | "mov %%r8, %c[r8](%0) \n\t" |
| 9125 | "mov %%r9, %c[r9](%0) \n\t" |
| 9126 | "mov %%r10, %c[r10](%0) \n\t" |
| 9127 | "mov %%r11, %c[r11](%0) \n\t" |
| 9128 | "mov %%r12, %c[r12](%0) \n\t" |
| 9129 | "mov %%r13, %c[r13](%0) \n\t" |
| 9130 | "mov %%r14, %c[r14](%0) \n\t" |
| 9131 | "mov %%r15, %c[r15](%0) \n\t" |
Jim Mattson | 491c0ca | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 9132 | "xor %%r8d, %%r8d \n\t" |
| 9133 | "xor %%r9d, %%r9d \n\t" |
| 9134 | "xor %%r10d, %%r10d \n\t" |
| 9135 | "xor %%r11d, %%r11d \n\t" |
| 9136 | "xor %%r12d, %%r12d \n\t" |
| 9137 | "xor %%r13d, %%r13d \n\t" |
| 9138 | "xor %%r14d, %%r14d \n\t" |
| 9139 | "xor %%r15d, %%r15d \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9140 | #endif |
Avi Kivity | b188c81 | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9141 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 9142 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 9143 | |
Jim Mattson | 491c0ca | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 9144 | "xor %%eax, %%eax \n\t" |
| 9145 | "xor %%ebx, %%ebx \n\t" |
| 9146 | "xor %%esi, %%esi \n\t" |
| 9147 | "xor %%edi, %%edi \n\t" |
Avi Kivity | b188c81 | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9148 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 9149 | ".pushsection .rodata \n\t" |
| 9150 | ".global vmx_return \n\t" |
| 9151 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 9152 | ".popsection" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9153 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9154 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9155 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 9156 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 9157 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 9158 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 9159 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 9160 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 9161 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 9162 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 9163 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 9164 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 9165 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 9166 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 9167 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 9168 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 9169 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 9170 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 9171 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 9172 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9173 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 9174 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 9175 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9176 | : "cc", "memory" |
| 9177 | #ifdef CONFIG_X86_64 |
Avi Kivity | b188c81 | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9178 | , "rax", "rbx", "rdi", "rsi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9179 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81 | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9180 | #else |
| 9181 | , "eax", "ebx", "edi", "esi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9182 | #endif |
| 9183 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9184 | |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9185 | /* |
| 9186 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 9187 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 9188 | * turn it off. This is much more efficient than blindly adding |
| 9189 | * it to the atomic save/restore list. Especially as the former |
| 9190 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 9191 | * |
| 9192 | * For non-nested case: |
| 9193 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 9194 | * save it. |
| 9195 | * |
| 9196 | * For nested case: |
| 9197 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 9198 | * save it. |
| 9199 | */ |
Paolo Bonzini | f750e15 | 2018-02-22 16:43:18 +0100 | [diff] [blame] | 9200 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
Paolo Bonzini | a175d51 | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 9201 | vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9202 | |
Thomas Gleixner | 1189cbf | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 9203 | x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9204 | |
David Woodhouse | c1ddd99 | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 9205 | /* Eliminate branch target predictions from guest mode */ |
| 9206 | vmexit_fill_RSB(); |
| 9207 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 9208 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
| 9209 | if (debugctlmsr) |
| 9210 | update_debugctlmsr(debugctlmsr); |
| 9211 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 9212 | #ifndef CONFIG_X86_64 |
| 9213 | /* |
| 9214 | * The sysexit path does not restore ds/es, so we must set them to |
| 9215 | * a reasonable value ourselves. |
| 9216 | * |
| 9217 | * We can't defer this to vmx_load_host_state() since that function |
| 9218 | * may be executed in interrupt context, which saves and restore segments |
| 9219 | * around it, nullifying its effect. |
| 9220 | */ |
| 9221 | loadsegment(ds, __USER_DS); |
| 9222 | loadsegment(es, __USER_DS); |
| 9223 | #endif |
| 9224 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 9225 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 9226 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 9227 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 9228 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 9229 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 9230 | vcpu->arch.regs_dirty = 0; |
| 9231 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 9232 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 9233 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9234 | vmx->loaded_vmcs->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 9235 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9236 | vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9237 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 9238 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 9239 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 9240 | * back on host, so it is safe to read guest PKRU from current |
| 9241 | * XSAVE. |
| 9242 | */ |
| 9243 | if (boot_cpu_has(X86_FEATURE_OSPKE)) { |
| 9244 | vmx->guest_pkru = __read_pkru(); |
| 9245 | if (vmx->guest_pkru != vmx->host_pkru) { |
| 9246 | vmx->guest_pkru_valid = true; |
| 9247 | __write_pkru(vmx->host_pkru); |
| 9248 | } else |
| 9249 | vmx->guest_pkru_valid = false; |
| 9250 | } |
| 9251 | |
| 9252 | /* |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 9253 | * the KVM_REQ_EVENT optimization bit is only on for one entry, and if |
| 9254 | * we did not inject a still-pending event to L1 now because of |
| 9255 | * nested_run_pending, we need to re-enable this bit. |
| 9256 | */ |
| 9257 | if (vmx->nested.nested_run_pending) |
| 9258 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9259 | |
| 9260 | vmx->nested.nested_run_pending = 0; |
| 9261 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9262 | vmx_complete_atomic_exit(vmx); |
| 9263 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 9264 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9265 | } |
Josh Poimboeuf | 935893a | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 9266 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9267 | |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9268 | static void vmx_load_vmcs01(struct kvm_vcpu *vcpu) |
| 9269 | { |
| 9270 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9271 | int cpu; |
| 9272 | |
| 9273 | if (vmx->loaded_vmcs == &vmx->vmcs01) |
| 9274 | return; |
| 9275 | |
| 9276 | cpu = get_cpu(); |
| 9277 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 9278 | vmx_vcpu_put(vcpu); |
| 9279 | vmx_vcpu_load(vcpu, cpu); |
| 9280 | vcpu->cpu = cpu; |
| 9281 | put_cpu(); |
| 9282 | } |
| 9283 | |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9284 | /* |
| 9285 | * Ensure that the current vmcs of the logical processor is the |
| 9286 | * vmcs01 of the vcpu before calling free_nested(). |
| 9287 | */ |
| 9288 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) |
| 9289 | { |
| 9290 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9291 | int r; |
| 9292 | |
| 9293 | r = vcpu_load(vcpu); |
| 9294 | BUG_ON(r); |
| 9295 | vmx_load_vmcs01(vcpu); |
| 9296 | free_nested(vmx); |
| 9297 | vcpu_put(vcpu); |
| 9298 | } |
| 9299 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9300 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 9301 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9302 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9303 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9304 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9305 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9306 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9307 | leave_guest_mode(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9308 | vmx_free_vcpu_nested(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9309 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9310 | kfree(vmx->guest_msrs); |
| 9311 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 9312 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9313 | } |
| 9314 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9315 | 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] | 9316 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9317 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 9318 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 9319 | unsigned long *msr_bitmap; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9320 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9321 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9322 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9323 | return ERR_PTR(-ENOMEM); |
| 9324 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9325 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 9326 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9327 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 9328 | if (err) |
| 9329 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9330 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9331 | err = -ENOMEM; |
| 9332 | |
| 9333 | /* |
| 9334 | * If PML is turned on, failure on enabling PML just results in failure |
| 9335 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 9336 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 9337 | * for the guest, etc. |
| 9338 | */ |
| 9339 | if (enable_pml) { |
| 9340 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 9341 | if (!vmx->pml_pg) |
| 9342 | goto uninit_vcpu; |
| 9343 | } |
| 9344 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9345 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 9346 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 9347 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 9348 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9349 | if (!vmx->guest_msrs) |
| 9350 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9351 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9352 | if (!vmm_exclusive) |
| 9353 | kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id()))); |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 9354 | err = alloc_loaded_vmcs(&vmx->vmcs01); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9355 | if (!vmm_exclusive) |
| 9356 | kvm_cpu_vmxoff(); |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 9357 | if (err < 0) |
| 9358 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9359 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 9360 | msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 9361 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW); |
| 9362 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW); |
| 9363 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 9364 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); |
| 9365 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); |
| 9366 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); |
| 9367 | vmx->msr_bitmap_mode = 0; |
| 9368 | |
Paolo Bonzini | ff546f9 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 9369 | vmx->loaded_vmcs = &vmx->vmcs01; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9370 | cpu = get_cpu(); |
| 9371 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 9372 | vmx->vcpu.cpu = cpu; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 9373 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9374 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9375 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9376 | if (err) |
| 9377 | goto free_vmcs; |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 9378 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 9379 | err = alloc_apic_access_page(kvm); |
| 9380 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 9381 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 9382 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9383 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 9384 | if (enable_ept) { |
| 9385 | if (!kvm->arch.ept_identity_map_addr) |
| 9386 | kvm->arch.ept_identity_map_addr = |
| 9387 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 9388 | err = init_rmode_identity_map(kvm); |
| 9389 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 9390 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 9391 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 9392 | |
Roman Kagan | 020a90f | 2018-07-19 21:59:07 +0300 | [diff] [blame] | 9393 | if (nested) |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9394 | nested_vmx_setup_ctls_msrs(vmx); |
| 9395 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9396 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 9397 | vmx->nested.current_vmptr = -1ull; |
| 9398 | vmx->nested.current_vmcs12 = NULL; |
| 9399 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 9400 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 9401 | |
Paolo Bonzini | 58d2fb1 | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 9402 | /* |
| 9403 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR |
| 9404 | * or POSTED_INTR_WAKEUP_VECTOR. |
| 9405 | */ |
| 9406 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; |
| 9407 | vmx->pi_desc.sn = 1; |
| 9408 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9409 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9410 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9411 | free_vmcs: |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 9412 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9413 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9414 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9415 | free_pml: |
| 9416 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9417 | uninit_vcpu: |
| 9418 | kvm_vcpu_uninit(&vmx->vcpu); |
| 9419 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9420 | free_vpid(vmx->vpid); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 9421 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9422 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9423 | } |
| 9424 | |
Konrad Rzeszutek Wilk | a0695af | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 9425 | #define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n" |
| 9426 | |
| 9427 | static int vmx_vm_init(struct kvm *kvm) |
| 9428 | { |
| 9429 | if (boot_cpu_has(X86_BUG_L1TF) && cpu_smt_control == CPU_SMT_ENABLED) { |
| 9430 | if (nosmt) { |
| 9431 | pr_err(L1TF_MSG); |
| 9432 | return -EOPNOTSUPP; |
| 9433 | } |
| 9434 | pr_warn(L1TF_MSG); |
| 9435 | } |
| 9436 | return 0; |
| 9437 | } |
| 9438 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 9439 | static void __init vmx_check_processor_compat(void *rtn) |
| 9440 | { |
| 9441 | struct vmcs_config vmcs_conf; |
| 9442 | |
| 9443 | *(int *)rtn = 0; |
| 9444 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 9445 | *(int *)rtn = -EIO; |
| 9446 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 9447 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 9448 | smp_processor_id()); |
| 9449 | *(int *)rtn = -EIO; |
| 9450 | } |
| 9451 | } |
| 9452 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 9453 | static int get_ept_level(void) |
| 9454 | { |
| 9455 | return VMX_EPT_DEFAULT_GAW + 1; |
| 9456 | } |
| 9457 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 9458 | 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] | 9459 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9460 | u8 cache; |
| 9461 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 9462 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9463 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9464 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9465 | * 2. EPT with VT-d: |
| 9466 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9467 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9468 | * b. VT-d with snooping control feature: snooping control feature of |
| 9469 | * VT-d engine can guarantee the cache correctness. Just set it |
| 9470 | * 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] | 9471 | * 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] | 9472 | * consistent with host MTRR |
| 9473 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9474 | if (is_mmio) { |
| 9475 | cache = MTRR_TYPE_UNCACHABLE; |
| 9476 | goto exit; |
| 9477 | } |
| 9478 | |
| 9479 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9480 | ipat = VMX_EPT_IPAT_BIT; |
| 9481 | cache = MTRR_TYPE_WRBACK; |
| 9482 | goto exit; |
| 9483 | } |
| 9484 | |
| 9485 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 9486 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 9487 | 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] | 9488 | cache = MTRR_TYPE_WRBACK; |
| 9489 | else |
| 9490 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9491 | goto exit; |
| 9492 | } |
| 9493 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 9494 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9495 | |
| 9496 | exit: |
| 9497 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 9498 | } |
| 9499 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 9500 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 9501 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 9502 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 9503 | return PT_DIRECTORY_LEVEL; |
| 9504 | else |
| 9505 | /* For shadow and EPT supported 1GB page */ |
| 9506 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 9507 | } |
| 9508 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9509 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 9510 | { |
| 9511 | /* |
| 9512 | * These bits in the secondary execution controls field |
| 9513 | * are dynamic, the others are mostly based on the hypervisor |
| 9514 | * architecture and the guest's CPUID. Do not touch the |
| 9515 | * dynamic bits. |
| 9516 | */ |
| 9517 | u32 mask = |
| 9518 | SECONDARY_EXEC_SHADOW_VMCS | |
| 9519 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 9520 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 9521 | |
| 9522 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 9523 | |
| 9524 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 9525 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 9526 | } |
| 9527 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 9528 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 9529 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9530 | struct kvm_cpuid_entry2 *best; |
| 9531 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9532 | u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9533 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9534 | if (vmx_rdtscp_supported()) { |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 9535 | bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu); |
| 9536 | if (!rdtscp_enabled) |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9537 | secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP; |
Xiao Guangrong | f36201e | 2015-09-09 14:05:53 +0800 | [diff] [blame] | 9538 | |
Paolo Bonzini | 8b97265 | 2015-09-15 17:34:42 +0200 | [diff] [blame] | 9539 | if (nested) { |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 9540 | if (rdtscp_enabled) |
Paolo Bonzini | 8b97265 | 2015-09-15 17:34:42 +0200 | [diff] [blame] | 9541 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 9542 | SECONDARY_EXEC_RDTSCP; |
| 9543 | else |
| 9544 | vmx->nested.nested_vmx_secondary_ctls_high &= |
| 9545 | ~SECONDARY_EXEC_RDTSCP; |
| 9546 | } |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9547 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9548 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9549 | /* Exposing INVPCID only when PCID is exposed */ |
| 9550 | best = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 9551 | if (vmx_invpcid_supported() && |
Xiao Guangrong | 29541bb | 2015-09-09 14:05:54 +0800 | [diff] [blame] | 9552 | (!best || !(best->ebx & bit(X86_FEATURE_INVPCID)) || |
| 9553 | !guest_cpuid_has_pcid(vcpu))) { |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9554 | secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Xiao Guangrong | 29541bb | 2015-09-09 14:05:54 +0800 | [diff] [blame] | 9555 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9556 | if (best) |
Ren, Yongjie | 4f97704 | 2012-09-07 07:36:59 +0000 | [diff] [blame] | 9557 | best->ebx &= ~bit(X86_FEATURE_INVPCID); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9558 | } |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 9559 | |
Huaitong Han | 45bdbcf | 2016-01-12 16:04:20 +0800 | [diff] [blame] | 9560 | if (cpu_has_secondary_exec_ctrls()) |
| 9561 | vmcs_set_secondary_exec_control(secondary_exec_ctl); |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9562 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 9563 | if (nested_vmx_allowed(vcpu)) |
| 9564 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 9565 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 9566 | else |
| 9567 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 9568 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 9569 | } |
| 9570 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 9571 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 9572 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 9573 | if (func == 1 && nested) |
| 9574 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 9575 | } |
| 9576 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9577 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 9578 | struct x86_exception *fault) |
| 9579 | { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9580 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9581 | u32 exit_reason; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9582 | |
| 9583 | if (fault->error_code & PFERR_RSVD_MASK) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9584 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9585 | else |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9586 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
| 9587 | nested_vmx_vmexit(vcpu, exit_reason, 0, vcpu->arch.exit_qualification); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9588 | vmcs12->guest_physical_address = fault->address; |
| 9589 | } |
| 9590 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9591 | /* Callbacks for nested_ept_init_mmu_context: */ |
| 9592 | |
| 9593 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) |
| 9594 | { |
| 9595 | /* return the page table to be shadowed - in our case, EPT12 */ |
| 9596 | return get_vmcs12(vcpu)->ept_pointer; |
| 9597 | } |
| 9598 | |
Paolo Bonzini | 8a3c1a33 | 2013-10-02 16:56:13 +0200 | [diff] [blame] | 9599 | static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9600 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 9601 | WARN_ON(mmu_is_nested(vcpu)); |
| 9602 | kvm_init_shadow_ept_mmu(vcpu, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9603 | to_vmx(vcpu)->nested.nested_vmx_ept_caps & |
| 9604 | VMX_EPT_EXECUTE_ONLY_BIT); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9605 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
| 9606 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; |
| 9607 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; |
| 9608 | |
| 9609 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9610 | } |
| 9611 | |
| 9612 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 9613 | { |
| 9614 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 9615 | } |
| 9616 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 9617 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 9618 | u16 error_code) |
| 9619 | { |
| 9620 | bool inequality, bit; |
| 9621 | |
| 9622 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 9623 | inequality = |
| 9624 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 9625 | vmcs12->page_fault_error_code_match; |
| 9626 | return inequality ^ bit; |
| 9627 | } |
| 9628 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 9629 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 9630 | struct x86_exception *fault) |
| 9631 | { |
| 9632 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9633 | |
| 9634 | WARN_ON(!is_guest_mode(vcpu)); |
| 9635 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 9636 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code)) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9637 | nested_vmx_vmexit(vcpu, to_vmx(vcpu)->exit_reason, |
| 9638 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 9639 | vmcs_readl(EXIT_QUALIFICATION)); |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 9640 | else |
| 9641 | kvm_inject_page_fault(vcpu, fault); |
| 9642 | } |
| 9643 | |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9644 | static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, |
| 9645 | struct vmcs12 *vmcs12) |
| 9646 | { |
| 9647 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9648 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9649 | |
| 9650 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9651 | if (!PAGE_ALIGNED(vmcs12->apic_access_addr) || |
| 9652 | vmcs12->apic_access_addr >> maxphyaddr) |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9653 | return false; |
| 9654 | |
| 9655 | /* |
| 9656 | * Translate L1 physical address to host physical |
| 9657 | * address for vmcs02. Keep the page pinned, so this |
| 9658 | * physical address remains valid. We keep a reference |
| 9659 | * to it so we can release it later. |
| 9660 | */ |
| 9661 | if (vmx->nested.apic_access_page) /* shouldn't happen */ |
| 9662 | nested_release_page(vmx->nested.apic_access_page); |
| 9663 | vmx->nested.apic_access_page = |
| 9664 | nested_get_page(vcpu, vmcs12->apic_access_addr); |
| 9665 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9666 | |
| 9667 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9668 | if (!PAGE_ALIGNED(vmcs12->virtual_apic_page_addr) || |
| 9669 | vmcs12->virtual_apic_page_addr >> maxphyaddr) |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9670 | return false; |
| 9671 | |
| 9672 | if (vmx->nested.virtual_apic_page) /* shouldn't happen */ |
| 9673 | nested_release_page(vmx->nested.virtual_apic_page); |
| 9674 | vmx->nested.virtual_apic_page = |
| 9675 | nested_get_page(vcpu, vmcs12->virtual_apic_page_addr); |
| 9676 | |
| 9677 | /* |
| 9678 | * Failing the vm entry is _not_ what the processor does |
| 9679 | * but it's basically the only possibility we have. |
| 9680 | * We could still enter the guest if CR8 load exits are |
| 9681 | * enabled, CR8 store exits are enabled, and virtualize APIC |
| 9682 | * access is disabled; in this case the processor would never |
| 9683 | * use the TPR shadow and we could simply clear the bit from |
| 9684 | * the execution control. But such a configuration is useless, |
| 9685 | * so let's keep the code simple. |
| 9686 | */ |
| 9687 | if (!vmx->nested.virtual_apic_page) |
| 9688 | return false; |
| 9689 | } |
| 9690 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9691 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Eugene Korenevsky | 9090422 | 2015-03-29 23:56:27 +0300 | [diff] [blame] | 9692 | if (!IS_ALIGNED(vmcs12->posted_intr_desc_addr, 64) || |
| 9693 | vmcs12->posted_intr_desc_addr >> maxphyaddr) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9694 | return false; |
| 9695 | |
| 9696 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
| 9697 | kunmap(vmx->nested.pi_desc_page); |
| 9698 | nested_release_page(vmx->nested.pi_desc_page); |
| 9699 | } |
| 9700 | vmx->nested.pi_desc_page = |
| 9701 | nested_get_page(vcpu, vmcs12->posted_intr_desc_addr); |
| 9702 | if (!vmx->nested.pi_desc_page) |
| 9703 | return false; |
| 9704 | |
| 9705 | vmx->nested.pi_desc = |
| 9706 | (struct pi_desc *)kmap(vmx->nested.pi_desc_page); |
| 9707 | if (!vmx->nested.pi_desc) { |
| 9708 | nested_release_page_clean(vmx->nested.pi_desc_page); |
| 9709 | return false; |
| 9710 | } |
| 9711 | vmx->nested.pi_desc = |
| 9712 | (struct pi_desc *)((void *)vmx->nested.pi_desc + |
| 9713 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 9714 | (PAGE_SIZE - 1))); |
| 9715 | } |
| 9716 | |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9717 | return true; |
| 9718 | } |
| 9719 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9720 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 9721 | { |
| 9722 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 9723 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9724 | |
| 9725 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 9726 | return; |
| 9727 | |
| 9728 | /* Make sure short timeouts reliably trigger an immediate vmexit. |
| 9729 | * hrtimer_start does not guarantee this. */ |
| 9730 | if (preemption_timeout <= 1) { |
| 9731 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 9732 | return; |
| 9733 | } |
| 9734 | |
| 9735 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 9736 | preemption_timeout *= 1000000; |
| 9737 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 9738 | hrtimer_start(&vmx->nested.preemption_timer, |
| 9739 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 9740 | } |
| 9741 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9742 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 9743 | struct vmcs12 *vmcs12) |
| 9744 | { |
| 9745 | int maxphyaddr; |
| 9746 | u64 addr; |
| 9747 | |
| 9748 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 9749 | return 0; |
| 9750 | |
| 9751 | if (vmcs12_read_any(vcpu, MSR_BITMAP, &addr)) { |
| 9752 | WARN_ON(1); |
| 9753 | return -EINVAL; |
| 9754 | } |
| 9755 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 9756 | |
| 9757 | if (!PAGE_ALIGNED(vmcs12->msr_bitmap) || |
| 9758 | ((addr + PAGE_SIZE) >> maxphyaddr)) |
| 9759 | return -EINVAL; |
| 9760 | |
| 9761 | return 0; |
| 9762 | } |
| 9763 | |
| 9764 | /* |
| 9765 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 9766 | * we do not use the hardware. |
| 9767 | */ |
| 9768 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, |
| 9769 | struct vmcs12 *vmcs12) |
| 9770 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9771 | int msr; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9772 | struct page *page; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9773 | unsigned long *msr_bitmap_l1; |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 9774 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 9775 | /* |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9776 | * pred_cmd & spec_ctrl are trying to verify two things: |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 9777 | * |
| 9778 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 9779 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 9780 | * from the L12 MSR bitmap that is too permissive. |
| 9781 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 9782 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 9783 | * works properly because we only update the L01 MSR bitmap lazily. |
| 9784 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 9785 | * updated to reflect this when L1 (or its L2s) actually write to |
| 9786 | * the MSR. |
| 9787 | */ |
KarimAllah Ahmed | 9665296 | 2018-02-10 23:39:25 +0000 | [diff] [blame] | 9788 | bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD); |
| 9789 | bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9790 | |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 9791 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9792 | !pred_cmd && !spec_ctrl) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9793 | return false; |
| 9794 | |
| 9795 | page = nested_get_page(vcpu, vmcs12->msr_bitmap); |
Radim Krčmář | 215df1f | 2017-03-07 17:51:49 +0100 | [diff] [blame] | 9796 | if (!page) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9797 | return false; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9798 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9799 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9800 | memset(msr_bitmap_l0, 0xff, PAGE_SIZE); |
| 9801 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9802 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9803 | if (nested_cpu_has_apic_reg_virt(vmcs12)) |
| 9804 | for (msr = 0x800; msr <= 0x8ff; msr++) |
| 9805 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9806 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9807 | msr, MSR_TYPE_R); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9808 | |
| 9809 | nested_vmx_disable_intercept_for_msr( |
| 9810 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9811 | APIC_BASE_MSR + (APIC_TASKPRI >> 4), |
| 9812 | MSR_TYPE_R | MSR_TYPE_W); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9813 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9814 | if (nested_cpu_has_vid(vmcs12)) { |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9815 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9816 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9817 | APIC_BASE_MSR + (APIC_EOI >> 4), |
| 9818 | MSR_TYPE_W); |
| 9819 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9820 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9821 | APIC_BASE_MSR + (APIC_SELF_IPI >> 4), |
| 9822 | MSR_TYPE_W); |
| 9823 | } |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9824 | } |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 9825 | |
KarimAllah Ahmed | e5a8341 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9826 | if (spec_ctrl) |
| 9827 | nested_vmx_disable_intercept_for_msr( |
| 9828 | msr_bitmap_l1, msr_bitmap_l0, |
| 9829 | MSR_IA32_SPEC_CTRL, |
| 9830 | MSR_TYPE_R | MSR_TYPE_W); |
| 9831 | |
Ashok Raj | 7013129 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 9832 | if (pred_cmd) |
| 9833 | nested_vmx_disable_intercept_for_msr( |
| 9834 | msr_bitmap_l1, msr_bitmap_l0, |
| 9835 | MSR_IA32_PRED_CMD, |
| 9836 | MSR_TYPE_W); |
| 9837 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9838 | kunmap(page); |
| 9839 | nested_release_page_clean(page); |
| 9840 | |
| 9841 | return true; |
| 9842 | } |
| 9843 | |
| 9844 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 9845 | struct vmcs12 *vmcs12) |
| 9846 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9847 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9848 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9849 | !nested_cpu_has_vid(vmcs12) && |
| 9850 | !nested_cpu_has_posted_intr(vmcs12)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9851 | return 0; |
| 9852 | |
| 9853 | /* |
| 9854 | * If virtualize x2apic mode is enabled, |
| 9855 | * virtualize apic access must be disabled. |
| 9856 | */ |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9857 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 9858 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9859 | return -EINVAL; |
| 9860 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9861 | /* |
| 9862 | * If virtual interrupt delivery is enabled, |
| 9863 | * we must exit on external interrupts. |
| 9864 | */ |
| 9865 | if (nested_cpu_has_vid(vmcs12) && |
| 9866 | !nested_exit_on_intr(vcpu)) |
| 9867 | return -EINVAL; |
| 9868 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9869 | /* |
| 9870 | * bits 15:8 should be zero in posted_intr_nv, |
| 9871 | * the descriptor address has been already checked |
| 9872 | * in nested_get_vmcs12_pages. |
| 9873 | */ |
| 9874 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 9875 | (!nested_cpu_has_vid(vmcs12) || |
| 9876 | !nested_exit_intr_ack_set(vcpu) || |
| 9877 | vmcs12->posted_intr_nv & 0xff00)) |
| 9878 | return -EINVAL; |
| 9879 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9880 | /* tpr shadow is needed by all apicv features. */ |
| 9881 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 9882 | return -EINVAL; |
| 9883 | |
| 9884 | return 0; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9885 | } |
| 9886 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9887 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
| 9888 | unsigned long count_field, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9889 | unsigned long addr_field) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9890 | { |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9891 | int maxphyaddr; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9892 | u64 count, addr; |
| 9893 | |
| 9894 | if (vmcs12_read_any(vcpu, count_field, &count) || |
| 9895 | vmcs12_read_any(vcpu, addr_field, &addr)) { |
| 9896 | WARN_ON(1); |
| 9897 | return -EINVAL; |
| 9898 | } |
| 9899 | if (count == 0) |
| 9900 | return 0; |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9901 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9902 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
| 9903 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9904 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9905 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
| 9906 | addr_field, maxphyaddr, count, addr); |
| 9907 | return -EINVAL; |
| 9908 | } |
| 9909 | return 0; |
| 9910 | } |
| 9911 | |
| 9912 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 9913 | struct vmcs12 *vmcs12) |
| 9914 | { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9915 | if (vmcs12->vm_exit_msr_load_count == 0 && |
| 9916 | vmcs12->vm_exit_msr_store_count == 0 && |
| 9917 | vmcs12->vm_entry_msr_load_count == 0) |
| 9918 | return 0; /* Fast path */ |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9919 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9920 | VM_EXIT_MSR_LOAD_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9921 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9922 | VM_EXIT_MSR_STORE_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9923 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9924 | VM_ENTRY_MSR_LOAD_ADDR)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9925 | return -EINVAL; |
| 9926 | return 0; |
| 9927 | } |
| 9928 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9929 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 9930 | struct vmx_msr_entry *e) |
| 9931 | { |
| 9932 | /* x2APIC MSR accesses are not allowed */ |
Jan Kiszka | 8a9781f | 2015-05-04 08:32:32 +0200 | [diff] [blame] | 9933 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9934 | return -EINVAL; |
| 9935 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 9936 | e->index == MSR_IA32_UCODE_REV) |
| 9937 | return -EINVAL; |
| 9938 | if (e->reserved != 0) |
| 9939 | return -EINVAL; |
| 9940 | return 0; |
| 9941 | } |
| 9942 | |
| 9943 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 9944 | struct vmx_msr_entry *e) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9945 | { |
| 9946 | if (e->index == MSR_FS_BASE || |
| 9947 | e->index == MSR_GS_BASE || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9948 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 9949 | nested_vmx_msr_check_common(vcpu, e)) |
| 9950 | return -EINVAL; |
| 9951 | return 0; |
| 9952 | } |
| 9953 | |
| 9954 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 9955 | struct vmx_msr_entry *e) |
| 9956 | { |
| 9957 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 9958 | nested_vmx_msr_check_common(vcpu, e)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9959 | return -EINVAL; |
| 9960 | return 0; |
| 9961 | } |
| 9962 | |
| 9963 | /* |
| 9964 | * Load guest's/host's msr at nested entry/exit. |
| 9965 | * return 0 for success, entry index for failure. |
| 9966 | */ |
| 9967 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 9968 | { |
| 9969 | u32 i; |
| 9970 | struct vmx_msr_entry e; |
| 9971 | struct msr_data msr; |
| 9972 | |
| 9973 | msr.host_initiated = false; |
| 9974 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9975 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 9976 | &e, sizeof(e))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9977 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9978 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 9979 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9980 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9981 | } |
| 9982 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9983 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9984 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 9985 | __func__, i, e.index, e.reserved); |
| 9986 | goto fail; |
| 9987 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9988 | msr.index = e.index; |
| 9989 | msr.data = e.value; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9990 | if (kvm_set_msr(vcpu, &msr)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9991 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9992 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 9993 | __func__, i, e.index, e.value); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9994 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9995 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9996 | } |
| 9997 | return 0; |
| 9998 | fail: |
| 9999 | return i + 1; |
| 10000 | } |
| 10001 | |
| 10002 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 10003 | { |
| 10004 | u32 i; |
| 10005 | struct vmx_msr_entry e; |
| 10006 | |
| 10007 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 10008 | struct msr_data msr_info; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 10009 | if (kvm_vcpu_read_guest(vcpu, |
| 10010 | gpa + i * sizeof(e), |
| 10011 | &e, 2 * sizeof(u32))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10012 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10013 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 10014 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10015 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10016 | } |
| 10017 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10018 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10019 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 10020 | __func__, i, e.index, e.reserved); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10021 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10022 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 10023 | msr_info.host_initiated = false; |
| 10024 | msr_info.index = e.index; |
| 10025 | if (kvm_get_msr(vcpu, &msr_info)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10026 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10027 | "%s cannot read MSR (%u, 0x%x)\n", |
| 10028 | __func__, i, e.index); |
| 10029 | return -EINVAL; |
| 10030 | } |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 10031 | if (kvm_vcpu_write_guest(vcpu, |
| 10032 | gpa + i * sizeof(e) + |
| 10033 | offsetof(struct vmx_msr_entry, value), |
| 10034 | &msr_info.data, sizeof(msr_info.data))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10035 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10036 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 10037 | __func__, i, e.index, msr_info.data); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10038 | return -EINVAL; |
| 10039 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10040 | } |
| 10041 | return 0; |
| 10042 | } |
| 10043 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10044 | /* |
| 10045 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 10046 | * 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] | 10047 | * 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] | 10048 | * guest in a way that will both be appropriate to L1's requests, and our |
| 10049 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 10050 | * function also has additional necessary side-effects, like setting various |
| 10051 | * vcpu->arch fields. |
| 10052 | */ |
| 10053 | static void prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 10054 | { |
| 10055 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10056 | u32 exec_control; |
| 10057 | |
| 10058 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 10059 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 10060 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 10061 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 10062 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 10063 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 10064 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 10065 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 10066 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 10067 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 10068 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 10069 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 10070 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 10071 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 10072 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 10073 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 10074 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 10075 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 10076 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 10077 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 10078 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 10079 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 10080 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 10081 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 10082 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 10083 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 10084 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 10085 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 10086 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 10087 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 10088 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 10089 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 10090 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 10091 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 10092 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 10093 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
| 10094 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 10095 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 10096 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 10097 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 10098 | } else { |
| 10099 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 10100 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 10101 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10102 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 10103 | vmcs12->vm_entry_intr_info_field); |
| 10104 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 10105 | vmcs12->vm_entry_exception_error_code); |
| 10106 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 10107 | vmcs12->vm_entry_instruction_len); |
| 10108 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 10109 | vmcs12->guest_interruptibility_info); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10110 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
Gleb Natapov | 63fbf59 | 2013-07-28 18:31:06 +0300 | [diff] [blame] | 10111 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10112 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 10113 | vmcs12->guest_pending_dbg_exceptions); |
| 10114 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 10115 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 10116 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 10117 | if (nested_cpu_has_xsaves(vmcs12)) |
| 10118 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10119 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 10120 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10121 | exec_control = vmcs12->pin_based_vm_exec_control; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10122 | |
Paolo Bonzini | 9314006 | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 10123 | /* Preemption timer setting is only taken from vmcs01. */ |
| 10124 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 10125 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
| 10126 | if (vmx->hv_deadline_tsc == -1) |
| 10127 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 10128 | |
| 10129 | /* Posted interrupts setting is only taken from vmcs12. */ |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10130 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 10131 | /* |
| 10132 | * Note that we use L0's vector here and in |
| 10133 | * vmx_deliver_nested_posted_interrupt. |
| 10134 | */ |
| 10135 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 10136 | vmx->nested.pi_pending = false; |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 10137 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10138 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 10139 | page_to_phys(vmx->nested.pi_desc_page) + |
| 10140 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 10141 | (PAGE_SIZE - 1))); |
| 10142 | } else |
| 10143 | exec_control &= ~PIN_BASED_POSTED_INTR; |
| 10144 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10145 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10146 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10147 | vmx->nested.preemption_timer_expired = false; |
| 10148 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 10149 | vmx_start_preemption_timer(vcpu); |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 10150 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10151 | /* |
| 10152 | * Whether page-faults are trapped is determined by a combination of |
| 10153 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 10154 | * If enable_ept, L0 doesn't care about page faults and we should |
| 10155 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 10156 | * care about (at least some) page faults, and because it is not easy |
| 10157 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 10158 | * to exit on each and every L2 page fault. This is done by setting |
| 10159 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 10160 | * Note that below we don't need special code to set EB.PF beyond the |
| 10161 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 10162 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 10163 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 10164 | * |
| 10165 | * A problem with this approach (when !enable_ept) is that L1 may be |
| 10166 | * injected with more page faults than it asked for. This could have |
| 10167 | * caused problems, but in practice existing hypervisors don't care. |
| 10168 | * To fix this, we will need to emulate the PFEC checking (on the L1 |
| 10169 | * page tables), using walk_addr(), when injecting PFs to L1. |
| 10170 | */ |
| 10171 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 10172 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 10173 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 10174 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 10175 | |
| 10176 | if (cpu_has_secondary_exec_ctrls()) { |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10177 | exec_control = vmx_secondary_exec_control(vmx); |
Xiao Guangrong | e282162 | 2015-09-09 14:05:52 +0800 | [diff] [blame] | 10178 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10179 | /* Take the following fields only from vmcs12 */ |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 10180 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 10181 | SECONDARY_EXEC_RDTSCP | |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 10182 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 10183 | SECONDARY_EXEC_APIC_REGISTER_VIRT); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10184 | if (nested_cpu_has(vmcs12, |
| 10185 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) |
| 10186 | exec_control |= vmcs12->secondary_vm_exec_control; |
| 10187 | |
| 10188 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) { |
| 10189 | /* |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10190 | * If translation failed, no matter: This feature asks |
| 10191 | * to exit when accessing the given address, and if it |
| 10192 | * can never be accessed, this feature won't do |
| 10193 | * anything anyway. |
| 10194 | */ |
| 10195 | if (!vmx->nested.apic_access_page) |
| 10196 | exec_control &= |
| 10197 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 10198 | else |
| 10199 | vmcs_write64(APIC_ACCESS_ADDR, |
| 10200 | page_to_phys(vmx->nested.apic_access_page)); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10201 | } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) && |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 10202 | cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | ca3f257 | 2013-12-16 12:55:46 +0100 | [diff] [blame] | 10203 | exec_control |= |
| 10204 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 10205 | kvm_vcpu_reload_apic_access_page(vcpu); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10206 | } |
| 10207 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10208 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) { |
| 10209 | vmcs_write64(EOI_EXIT_BITMAP0, |
| 10210 | vmcs12->eoi_exit_bitmap0); |
| 10211 | vmcs_write64(EOI_EXIT_BITMAP1, |
| 10212 | vmcs12->eoi_exit_bitmap1); |
| 10213 | vmcs_write64(EOI_EXIT_BITMAP2, |
| 10214 | vmcs12->eoi_exit_bitmap2); |
| 10215 | vmcs_write64(EOI_EXIT_BITMAP3, |
| 10216 | vmcs12->eoi_exit_bitmap3); |
| 10217 | vmcs_write16(GUEST_INTR_STATUS, |
| 10218 | vmcs12->guest_intr_status); |
| 10219 | } |
| 10220 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10221 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 10222 | } |
| 10223 | |
| 10224 | |
| 10225 | /* |
| 10226 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 10227 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 10228 | * Other fields are different per CPU, and will be set later when |
| 10229 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 10230 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10231 | vmx_set_constant_host_state(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10232 | |
| 10233 | /* |
Jim Mattson | 69c2525 | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 10234 | * Set the MSR load/store lists to match L0's settings. |
| 10235 | */ |
| 10236 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
Konrad Rzeszutek Wilk | 57e3ada | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 10237 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 10238 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
| 10239 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 10240 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
Jim Mattson | 69c2525 | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 10241 | |
| 10242 | /* |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10243 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 10244 | * entry, but only if the current (host) sp changed from the value |
| 10245 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 10246 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 10247 | * here we just force the write to happen on entry. |
| 10248 | */ |
| 10249 | vmx->host_rsp = 0; |
| 10250 | |
| 10251 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 10252 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 10253 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 10254 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 10255 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10256 | |
| 10257 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
| 10258 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
| 10259 | page_to_phys(vmx->nested.virtual_apic_page)); |
| 10260 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Jim Mattson | 86ef97b | 2017-09-12 13:02:54 -0700 | [diff] [blame] | 10261 | } else { |
| 10262 | #ifdef CONFIG_X86_64 |
| 10263 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 10264 | CPU_BASED_CR8_STORE_EXITING; |
| 10265 | #endif |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10266 | } |
| 10267 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10268 | if (cpu_has_vmx_msr_bitmap() && |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 10269 | exec_control & CPU_BASED_USE_MSR_BITMAPS && |
| 10270 | nested_vmx_merge_msr_bitmap(vcpu, vmcs12)) |
| 10271 | ; /* MSR_BITMAP will be set by following vmx_set_efer. */ |
| 10272 | else |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10273 | exec_control &= ~CPU_BASED_USE_MSR_BITMAPS; |
| 10274 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10275 | /* |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10276 | * Merging of IO bitmap not currently supported. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10277 | * Rather, exit every time. |
| 10278 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10279 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 10280 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 10281 | |
| 10282 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 10283 | |
| 10284 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 10285 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 10286 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 10287 | */ |
| 10288 | update_exception_bitmap(vcpu); |
| 10289 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 10290 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 10291 | |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10292 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 10293 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 10294 | * bits are further modified by vmx_set_efer() below. |
| 10295 | */ |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10296 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10297 | |
| 10298 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are |
| 10299 | * emulated by vmx_set_efer(), below. |
| 10300 | */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 10301 | vm_entry_controls_init(vmx, |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10302 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
| 10303 | ~VM_ENTRY_IA32E_MODE) | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10304 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 10305 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10306 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10307 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10308 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
| 10309 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10310 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
| 10311 | |
| 10312 | |
| 10313 | set_cr4_guest_host_mask(vmx); |
| 10314 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10315 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) |
| 10316 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 10317 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 10318 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 10319 | vmcs_write64(TSC_OFFSET, |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 10320 | vcpu->arch.tsc_offset + vmcs12->tsc_offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 10321 | else |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 10322 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 10323 | if (kvm_has_tsc_control) |
| 10324 | decache_tsc_multiplier(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10325 | |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10326 | if (cpu_has_vmx_msr_bitmap()) |
| 10327 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
| 10328 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10329 | if (enable_vpid) { |
| 10330 | /* |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10331 | * There is no direct mapping between vpid02 and vpid12, the |
| 10332 | * vpid02 is per-vCPU for L0 and reused while the value of |
| 10333 | * vpid12 is changed w/ one invvpid during nested vmentry. |
| 10334 | * The vpid12 is allocated by L1 for L2, so it will not |
| 10335 | * influence global bitmap(for vpid01 and vpid02 allocation) |
| 10336 | * even if spawn a lot of nested vCPUs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10337 | */ |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10338 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
| 10339 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 10340 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 10341 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
| 10342 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02); |
| 10343 | } |
| 10344 | } else { |
| 10345 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 10346 | vmx_flush_tlb(vcpu); |
| 10347 | } |
| 10348 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10349 | } |
| 10350 | |
Ladi Prosek | 560a979 | 2017-04-04 14:18:53 +0200 | [diff] [blame] | 10351 | if (enable_pml) { |
| 10352 | /* |
| 10353 | * Conceptually we want to copy the PML address and index from |
| 10354 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, |
| 10355 | * since we always flush the log on each vmexit, this happens |
| 10356 | * to be equivalent to simply resetting the fields in vmcs02. |
| 10357 | */ |
| 10358 | ASSERT(vmx->pml_pg); |
| 10359 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 10360 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 10361 | } |
| 10362 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10363 | if (nested_cpu_has_ept(vmcs12)) { |
| 10364 | kvm_mmu_unload(vcpu); |
| 10365 | nested_ept_init_mmu_context(vcpu); |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 10366 | } else if (nested_cpu_has2(vmcs12, |
| 10367 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 10368 | vmx_flush_tlb_ept_only(vcpu); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10369 | } |
| 10370 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10371 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) |
| 10372 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 10373 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10374 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 10375 | else |
| 10376 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 10377 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 10378 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 10379 | |
| 10380 | /* |
| 10381 | * This sets GUEST_CR0 to vmcs12->guest_cr0, with possibly a modified |
| 10382 | * TS bit (for lazy fpu) and bits which we consider mandatory enabled. |
| 10383 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 10384 | * the specifications by L1; It's not enough to take |
| 10385 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 10386 | * have more bits than L1 expected. |
| 10387 | */ |
| 10388 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 10389 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 10390 | |
| 10391 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 10392 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 10393 | |
| 10394 | /* shadow page tables on either EPT or shadow page tables */ |
| 10395 | kvm_set_cr3(vcpu, vmcs12->guest_cr3); |
| 10396 | kvm_mmu_reset_context(vcpu); |
| 10397 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10398 | if (!enable_ept) |
| 10399 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 10400 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10401 | /* |
| 10402 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 |
| 10403 | */ |
| 10404 | if (enable_ept) { |
| 10405 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 10406 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 10407 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 10408 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 10409 | } |
| 10410 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10411 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 10412 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
| 10413 | } |
| 10414 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10415 | /* |
| 10416 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 10417 | * for running an L2 nested guest. |
| 10418 | */ |
| 10419 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 10420 | { |
| 10421 | struct vmcs12 *vmcs12; |
| 10422 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10423 | int cpu; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 10424 | bool ia32e; |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10425 | u32 msr_entry_idx; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10426 | |
| 10427 | if (!nested_vmx_check_permission(vcpu) || |
| 10428 | !nested_vmx_check_vmcs12(vcpu)) |
| 10429 | return 1; |
| 10430 | |
| 10431 | skip_emulated_instruction(vcpu); |
| 10432 | vmcs12 = get_vmcs12(vcpu); |
| 10433 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10434 | if (enable_shadow_vmcs) |
| 10435 | copy_shadow_to_vmcs12(vmx); |
| 10436 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10437 | /* |
| 10438 | * The nested entry process starts with enforcing various prerequisites |
| 10439 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 10440 | * they fail: As the SDM explains, some conditions should cause the |
| 10441 | * instruction to fail, while others will cause the instruction to seem |
| 10442 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 10443 | * To speed up the normal (success) code path, we should avoid checking |
| 10444 | * for misconfigurations which will anyway be caught by the processor |
| 10445 | * when using the merged vmcs02. |
| 10446 | */ |
| 10447 | if (vmcs12->launch_state == launch) { |
| 10448 | nested_vmx_failValid(vcpu, |
| 10449 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 10450 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
| 10451 | return 1; |
| 10452 | } |
| 10453 | |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10454 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 10455 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) { |
Paolo Bonzini | 26539bd | 2013-04-15 15:00:27 +0200 | [diff] [blame] | 10456 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10457 | return 1; |
| 10458 | } |
| 10459 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10460 | if (!nested_get_vmcs12_pages(vcpu, vmcs12)) { |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10461 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10462 | return 1; |
| 10463 | } |
| 10464 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10465 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) { |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10466 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10467 | return 1; |
| 10468 | } |
| 10469 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10470 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) { |
| 10471 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10472 | return 1; |
| 10473 | } |
| 10474 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10475 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) { |
| 10476 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10477 | return 1; |
| 10478 | } |
| 10479 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10480 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10481 | vmx->nested.nested_vmx_true_procbased_ctls_low, |
| 10482 | vmx->nested.nested_vmx_procbased_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10483 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10484 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 10485 | vmx->nested.nested_vmx_secondary_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10486 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10487 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 10488 | vmx->nested.nested_vmx_pinbased_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10489 | !vmx_control_verify(vmcs12->vm_exit_controls, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10490 | vmx->nested.nested_vmx_true_exit_ctls_low, |
| 10491 | vmx->nested.nested_vmx_exit_ctls_high) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10492 | !vmx_control_verify(vmcs12->vm_entry_controls, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10493 | vmx->nested.nested_vmx_true_entry_ctls_low, |
| 10494 | vmx->nested.nested_vmx_entry_ctls_high)) |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10495 | { |
| 10496 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 10497 | return 1; |
| 10498 | } |
| 10499 | |
| 10500 | if (((vmcs12->host_cr0 & VMXON_CR0_ALWAYSON) != VMXON_CR0_ALWAYSON) || |
| 10501 | ((vmcs12->host_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 10502 | nested_vmx_failValid(vcpu, |
| 10503 | VMXERR_ENTRY_INVALID_HOST_STATE_FIELD); |
| 10504 | return 1; |
| 10505 | } |
| 10506 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10507 | if (!nested_cr0_valid(vcpu, vmcs12->guest_cr0) || |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10508 | ((vmcs12->guest_cr4 & VMXON_CR4_ALWAYSON) != VMXON_CR4_ALWAYSON)) { |
| 10509 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10510 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 10511 | return 1; |
| 10512 | } |
| 10513 | if (vmcs12->vmcs_link_pointer != -1ull) { |
| 10514 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10515 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_VMCS_LINK_PTR); |
| 10516 | return 1; |
| 10517 | } |
| 10518 | |
| 10519 | /* |
Jan Kiszka | cb0c8cda | 2013-04-27 12:58:00 +0200 | [diff] [blame] | 10520 | * 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] | 10521 | * are performed on the field for the IA32_EFER MSR: |
| 10522 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 10523 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 10524 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 10525 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 10526 | * CR0.PG) is 1. |
| 10527 | */ |
| 10528 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER) { |
| 10529 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 10530 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 10531 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 10532 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 10533 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) { |
| 10534 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10535 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 10536 | return 1; |
| 10537 | } |
| 10538 | } |
| 10539 | |
| 10540 | /* |
| 10541 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 10542 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 10543 | * the values of the LMA and LME bits in the field must each be that of |
| 10544 | * the host address-space size VM-exit control. |
| 10545 | */ |
| 10546 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 10547 | ia32e = (vmcs12->vm_exit_controls & |
| 10548 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 10549 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 10550 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 10551 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) { |
| 10552 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10553 | EXIT_REASON_INVALID_STATE, ENTRY_FAIL_DEFAULT); |
| 10554 | return 1; |
| 10555 | } |
| 10556 | } |
| 10557 | |
| 10558 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10559 | * We're finally done with prerequisite checking, and can start with |
| 10560 | * the nested entry. |
| 10561 | */ |
| 10562 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10563 | enter_guest_mode(vcpu); |
| 10564 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 10565 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 10566 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 10567 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10568 | cpu = get_cpu(); |
Jim Mattson | 46e24df | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 10569 | vmx->loaded_vmcs = &vmx->nested.vmcs02; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10570 | vmx_vcpu_put(vcpu); |
| 10571 | vmx_vcpu_load(vcpu, cpu); |
| 10572 | vcpu->cpu = cpu; |
| 10573 | put_cpu(); |
| 10574 | |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 10575 | vmx_segment_cache_clear(vmx); |
| 10576 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10577 | prepare_vmcs02(vcpu, vmcs12); |
| 10578 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10579 | msr_entry_idx = nested_vmx_load_msr(vcpu, |
| 10580 | vmcs12->vm_entry_msr_load_addr, |
| 10581 | vmcs12->vm_entry_msr_load_count); |
| 10582 | if (msr_entry_idx) { |
| 10583 | leave_guest_mode(vcpu); |
| 10584 | vmx_load_vmcs01(vcpu); |
| 10585 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10586 | EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx); |
| 10587 | return 1; |
| 10588 | } |
| 10589 | |
| 10590 | vmcs12->launch_state = 1; |
| 10591 | |
Paolo Bonzini | b3dc63c | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 10592 | /* Hide L1D cache contents from the nested guest. */ |
| 10593 | vmx->vcpu.arch.l1tf_flush_l1d = true; |
| 10594 | |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10595 | if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 10596 | return kvm_vcpu_halt(vcpu); |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10597 | |
Jan Kiszka | 7af40ad3 | 2014-01-04 18:47:23 +0100 | [diff] [blame] | 10598 | vmx->nested.nested_run_pending = 1; |
| 10599 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10600 | /* |
| 10601 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 10602 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 10603 | * returned as far as L1 is concerned. It will only return (and set |
| 10604 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 10605 | */ |
| 10606 | return 1; |
| 10607 | } |
| 10608 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10609 | /* |
| 10610 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 10611 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 10612 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 10613 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 10614 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 10615 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 10616 | * didn't trap the bit, because if L1 did, so would L0). |
| 10617 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 10618 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 10619 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 10620 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 10621 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 10622 | * changed these bits, and therefore they need to be updated, but L0 |
| 10623 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 10624 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 10625 | */ |
| 10626 | static inline unsigned long |
| 10627 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 10628 | { |
| 10629 | return |
| 10630 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 10631 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 10632 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 10633 | vcpu->arch.cr0_guest_owned_bits)); |
| 10634 | } |
| 10635 | |
| 10636 | static inline unsigned long |
| 10637 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 10638 | { |
| 10639 | return |
| 10640 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 10641 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 10642 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 10643 | vcpu->arch.cr4_guest_owned_bits)); |
| 10644 | } |
| 10645 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10646 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 10647 | struct vmcs12 *vmcs12) |
| 10648 | { |
| 10649 | u32 idt_vectoring; |
| 10650 | unsigned int nr; |
| 10651 | |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 10652 | if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10653 | nr = vcpu->arch.exception.nr; |
| 10654 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 10655 | |
| 10656 | if (kvm_exception_is_soft(nr)) { |
| 10657 | vmcs12->vm_exit_instruction_len = |
| 10658 | vcpu->arch.event_exit_inst_len; |
| 10659 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 10660 | } else |
| 10661 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 10662 | |
| 10663 | if (vcpu->arch.exception.has_error_code) { |
| 10664 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 10665 | vmcs12->idt_vectoring_error_code = |
| 10666 | vcpu->arch.exception.error_code; |
| 10667 | } |
| 10668 | |
| 10669 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
Jan Kiszka | cd2633c | 2013-10-23 17:42:15 +0100 | [diff] [blame] | 10670 | } else if (vcpu->arch.nmi_injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10671 | vmcs12->idt_vectoring_info_field = |
| 10672 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 10673 | } else if (vcpu->arch.interrupt.pending) { |
| 10674 | nr = vcpu->arch.interrupt.nr; |
| 10675 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 10676 | |
| 10677 | if (vcpu->arch.interrupt.soft) { |
| 10678 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 10679 | vmcs12->vm_entry_instruction_len = |
| 10680 | vcpu->arch.event_exit_inst_len; |
| 10681 | } else |
| 10682 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 10683 | |
| 10684 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 10685 | } |
| 10686 | } |
| 10687 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10688 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 10689 | { |
| 10690 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10691 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10692 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 10693 | vmx->nested.preemption_timer_expired) { |
| 10694 | if (vmx->nested.nested_run_pending) |
| 10695 | return -EBUSY; |
| 10696 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 10697 | return 0; |
| 10698 | } |
| 10699 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10700 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
Jan Kiszka | 220c567 | 2014-03-07 20:03:14 +0100 | [diff] [blame] | 10701 | if (vmx->nested.nested_run_pending || |
| 10702 | vcpu->arch.interrupt.pending) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10703 | return -EBUSY; |
| 10704 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 10705 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 10706 | INTR_INFO_VALID_MASK, 0); |
| 10707 | /* |
| 10708 | * The NMI-triggered VM exit counts as injection: |
| 10709 | * clear this one and block further NMIs. |
| 10710 | */ |
| 10711 | vcpu->arch.nmi_pending = 0; |
| 10712 | vmx_set_nmi_mask(vcpu, true); |
| 10713 | return 0; |
| 10714 | } |
| 10715 | |
| 10716 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 10717 | nested_exit_on_intr(vcpu)) { |
| 10718 | if (vmx->nested.nested_run_pending) |
| 10719 | return -EBUSY; |
| 10720 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10721 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10722 | } |
| 10723 | |
David Hildenbrand | 1edccf2 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 10724 | vmx_complete_nested_posted_interrupt(vcpu); |
| 10725 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10726 | } |
| 10727 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10728 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 10729 | { |
| 10730 | ktime_t remaining = |
| 10731 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 10732 | u64 value; |
| 10733 | |
| 10734 | if (ktime_to_ns(remaining) <= 0) |
| 10735 | return 0; |
| 10736 | |
| 10737 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 10738 | do_div(value, 1000000); |
| 10739 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 10740 | } |
| 10741 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10742 | /* |
| 10743 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 10744 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 10745 | * and this function updates it to reflect the changes to the guest state while |
| 10746 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 10747 | * without going back to L1), and to reflect the exit reason. |
| 10748 | * Note that we do not have to copy here all VMCS fields, just those that |
| 10749 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 10750 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 10751 | * which already writes to vmcs12 directly. |
| 10752 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10753 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 10754 | u32 exit_reason, u32 exit_intr_info, |
| 10755 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10756 | { |
| 10757 | /* update guest state fields: */ |
| 10758 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 10759 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 10760 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10761 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 10762 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 10763 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 10764 | |
| 10765 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 10766 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 10767 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 10768 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 10769 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 10770 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 10771 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 10772 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 10773 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 10774 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 10775 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 10776 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 10777 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 10778 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 10779 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 10780 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 10781 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 10782 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 10783 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 10784 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 10785 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 10786 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 10787 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 10788 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 10789 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 10790 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 10791 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 10792 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 10793 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 10794 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 10795 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 10796 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 10797 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 10798 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 10799 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 10800 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 10801 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10802 | vmcs12->guest_interruptibility_info = |
| 10803 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 10804 | vmcs12->guest_pending_dbg_exceptions = |
| 10805 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
Jan Kiszka | 3edf1e6 | 2014-01-04 18:47:24 +0100 | [diff] [blame] | 10806 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 10807 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 10808 | else |
| 10809 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10810 | |
Jan Kiszka | f412450 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10811 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 10812 | if (vmcs12->vm_exit_controls & |
| 10813 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 10814 | vmcs12->vmx_preemption_timer_value = |
| 10815 | vmx_get_preemption_timer_value(vcpu); |
| 10816 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 10817 | } |
Arthur Chunqi Li | 7854cbc | 2013-09-16 16:11:44 +0800 | [diff] [blame] | 10818 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10819 | /* |
| 10820 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 10821 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 10822 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 10823 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 10824 | * |
| 10825 | * Additionally, restore L2's PDPTR to vmcs12. |
| 10826 | */ |
| 10827 | if (enable_ept) { |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 10828 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10829 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 10830 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 10831 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 10832 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 10833 | } |
| 10834 | |
Jim Mattson | 4933e9f | 2017-06-01 12:44:46 -0700 | [diff] [blame] | 10835 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
Jan Dakinevich | 119a9c0 | 2016-09-04 21:22:47 +0300 | [diff] [blame] | 10836 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10837 | if (nested_cpu_has_vid(vmcs12)) |
| 10838 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 10839 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 10840 | vmcs12->vm_entry_controls = |
| 10841 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 10842 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 10843 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 10844 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 10845 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 10846 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 10847 | } |
| 10848 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10849 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 10850 | * the relevant bit asks not to trap the change */ |
Jan Kiszka | b8c07d5 | 2013-04-06 13:51:21 +0200 | [diff] [blame] | 10851 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10852 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
Jan Kiszka | 10ba54a | 2013-08-08 16:26:31 +0200 | [diff] [blame] | 10853 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 10854 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10855 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 10856 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 10857 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 10858 | if (kvm_mpx_supported()) |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10859 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 10860 | if (nested_cpu_has_xsaves(vmcs12)) |
| 10861 | vmcs12->xss_exit_bitmap = vmcs_read64(XSS_EXIT_BITMAP); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10862 | |
| 10863 | /* update exit information fields: */ |
| 10864 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10865 | vmcs12->vm_exit_reason = exit_reason; |
| 10866 | vmcs12->exit_qualification = exit_qualification; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10867 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10868 | vmcs12->vm_exit_intr_info = exit_intr_info; |
Jan Kiszka | c0d1c77 | 2013-04-14 12:12:50 +0200 | [diff] [blame] | 10869 | if ((vmcs12->vm_exit_intr_info & |
| 10870 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == |
| 10871 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) |
| 10872 | vmcs12->vm_exit_intr_error_code = |
| 10873 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10874 | vmcs12->idt_vectoring_info_field = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10875 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 10876 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 10877 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10878 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
| 10879 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 10880 | * instead of reading the real value. */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10881 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10882 | |
| 10883 | /* |
| 10884 | * Transfer the event that L0 or L1 may wanted to inject into |
| 10885 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 10886 | */ |
| 10887 | vmcs12_save_pending_event(vcpu, vmcs12); |
| 10888 | } |
| 10889 | |
| 10890 | /* |
| 10891 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 10892 | * preserved above and would only end up incorrectly in L1. |
| 10893 | */ |
| 10894 | vcpu->arch.nmi_injected = false; |
| 10895 | kvm_clear_exception_queue(vcpu); |
| 10896 | kvm_clear_interrupt_queue(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10897 | } |
| 10898 | |
| 10899 | /* |
| 10900 | * A part of what we need to when the nested L2 guest exits and we want to |
| 10901 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 10902 | * in vmcs12. |
| 10903 | * This function is to be called not only on normal nested exit, but also on |
| 10904 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 10905 | * Failures During or After Loading Guest State"). |
| 10906 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 10907 | */ |
Jan Kiszka | 733568f | 2013-02-23 15:07:47 +0100 | [diff] [blame] | 10908 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 10909 | struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10910 | { |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 10911 | struct kvm_segment seg; |
| 10912 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10913 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 10914 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 10915 | 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] | 10916 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 10917 | else |
| 10918 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 10919 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 10920 | |
| 10921 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 10922 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
H. Peter Anvin | 1adfa76 | 2013-04-27 16:10:11 -0700 | [diff] [blame] | 10923 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10924 | /* |
| 10925 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
| 10926 | * actually changed, because it depends on the current state of |
| 10927 | * fpu_active (which may have changed). |
| 10928 | * Note that vmx_set_cr0 refers to efer set above. |
| 10929 | */ |
Jan Kiszka | 9e3e4db | 2013-09-03 21:11:45 +0200 | [diff] [blame] | 10930 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10931 | /* |
| 10932 | * If we did fpu_activate()/fpu_deactivate() during L2's run, we need |
| 10933 | * to apply the same changes to L1's vmcs. We just set cr0 correctly, |
| 10934 | * but we also need to update cr0_guest_host_mask and exception_bitmap. |
| 10935 | */ |
| 10936 | update_exception_bitmap(vcpu); |
| 10937 | vcpu->arch.cr0_guest_owned_bits = (vcpu->fpu_active ? X86_CR0_TS : 0); |
| 10938 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 10939 | |
| 10940 | /* |
| 10941 | * Note that CR4_GUEST_HOST_MASK is already set in the original vmcs01 |
| 10942 | * (KVM doesn't change it)- no reason to call set_cr4_guest_host_mask(); |
| 10943 | */ |
| 10944 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
Haozhong Zhang | 08e1674 | 2017-10-10 15:01:22 +0800 | [diff] [blame] | 10945 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10946 | |
Jan Kiszka | 29bf08f | 2013-12-28 16:31:52 +0100 | [diff] [blame] | 10947 | nested_ept_uninit_mmu_context(vcpu); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10948 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10949 | kvm_set_cr3(vcpu, vmcs12->host_cr3); |
| 10950 | kvm_mmu_reset_context(vcpu); |
| 10951 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10952 | if (!enable_ept) |
| 10953 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
| 10954 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10955 | if (enable_vpid) { |
| 10956 | /* |
| 10957 | * Trivially support vpid by letting L2s share their parent |
| 10958 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 10959 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 10960 | */ |
| 10961 | vmx_flush_tlb(vcpu); |
| 10962 | } |
| 10963 | |
| 10964 | |
| 10965 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 10966 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 10967 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 10968 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 10969 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
Ladi Prosek | 1be0c0e | 2017-10-11 16:54:42 +0200 | [diff] [blame] | 10970 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 10971 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10972 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10973 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 10974 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 10975 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 10976 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10977 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10978 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10979 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 10980 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10981 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 10982 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 10983 | vmcs12->host_ia32_perf_global_ctrl); |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 10984 | |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 10985 | /* Set L1 segment info according to Intel SDM |
| 10986 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 10987 | seg = (struct kvm_segment) { |
| 10988 | .base = 0, |
| 10989 | .limit = 0xFFFFFFFF, |
| 10990 | .selector = vmcs12->host_cs_selector, |
| 10991 | .type = 11, |
| 10992 | .present = 1, |
| 10993 | .s = 1, |
| 10994 | .g = 1 |
| 10995 | }; |
| 10996 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 10997 | seg.l = 1; |
| 10998 | else |
| 10999 | seg.db = 1; |
| 11000 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 11001 | seg = (struct kvm_segment) { |
| 11002 | .base = 0, |
| 11003 | .limit = 0xFFFFFFFF, |
| 11004 | .type = 3, |
| 11005 | .present = 1, |
| 11006 | .s = 1, |
| 11007 | .db = 1, |
| 11008 | .g = 1 |
| 11009 | }; |
| 11010 | seg.selector = vmcs12->host_ds_selector; |
| 11011 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 11012 | seg.selector = vmcs12->host_es_selector; |
| 11013 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 11014 | seg.selector = vmcs12->host_ss_selector; |
| 11015 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 11016 | seg.selector = vmcs12->host_fs_selector; |
| 11017 | seg.base = vmcs12->host_fs_base; |
| 11018 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 11019 | seg.selector = vmcs12->host_gs_selector; |
| 11020 | seg.base = vmcs12->host_gs_base; |
| 11021 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 11022 | seg = (struct kvm_segment) { |
Gleb Natapov | 205befd | 2013-08-04 15:08:06 +0300 | [diff] [blame] | 11023 | .base = vmcs12->host_tr_base, |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 11024 | .limit = 0x67, |
| 11025 | .selector = vmcs12->host_tr_selector, |
| 11026 | .type = 11, |
| 11027 | .present = 1 |
| 11028 | }; |
| 11029 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 11030 | |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 11031 | kvm_set_dr(vcpu, 7, 0x400); |
| 11032 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11033 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11034 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 6236b78 | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 11035 | vmx_update_msr_bitmap(vcpu); |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11036 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11037 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 11038 | vmcs12->vm_exit_msr_load_count)) |
| 11039 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11040 | } |
| 11041 | |
| 11042 | /* |
| 11043 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 11044 | * and modify vmcs12 to make it see what it would expect to see there if |
| 11045 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 11046 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11047 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 11048 | u32 exit_intr_info, |
| 11049 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11050 | { |
| 11051 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11052 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 11053 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11054 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 11055 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 11056 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11057 | leave_guest_mode(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11058 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 11059 | exit_qualification); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11060 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11061 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
| 11062 | vmcs12->vm_exit_msr_store_count)) |
| 11063 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
| 11064 | |
Wanpeng Li | f3380ca | 2014-08-05 12:42:23 +0800 | [diff] [blame] | 11065 | vmx_load_vmcs01(vcpu); |
| 11066 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 11067 | if ((exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT) |
| 11068 | && nested_exit_intr_ack_set(vcpu)) { |
| 11069 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 11070 | WARN_ON(irq < 0); |
| 11071 | vmcs12->vm_exit_intr_info = irq | |
| 11072 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 11073 | } |
| 11074 | |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 11075 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 11076 | vmcs12->exit_qualification, |
| 11077 | vmcs12->idt_vectoring_info_field, |
| 11078 | vmcs12->vm_exit_intr_info, |
| 11079 | vmcs12->vm_exit_intr_error_code, |
| 11080 | KVM_ISA_VMX); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11081 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 11082 | vm_entry_controls_reset_shadow(vmx); |
| 11083 | vm_exit_controls_reset_shadow(vmx); |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 11084 | vmx_segment_cache_clear(vmx); |
| 11085 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11086 | load_vmcs12_host_state(vcpu, vmcs12); |
| 11087 | |
Paolo Bonzini | 9314006 | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 11088 | /* Update any VMCS fields that might have changed while L2 ran */ |
Konrad Rzeszutek Wilk | 57e3ada | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 11089 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 11090 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 11091 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Paolo Bonzini | 9314006 | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 11092 | if (vmx->hv_deadline_tsc == -1) |
| 11093 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11094 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 11095 | else |
| 11096 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11097 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 11098 | if (kvm_has_tsc_control) |
| 11099 | decache_tsc_multiplier(vmx); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11100 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 11101 | if (vmx->nested.change_vmcs01_virtual_x2apic_mode) { |
| 11102 | vmx->nested.change_vmcs01_virtual_x2apic_mode = false; |
| 11103 | vmx_set_virtual_x2apic_mode(vcpu, |
| 11104 | vcpu->arch.apic_base & X2APIC_ENABLE); |
Jim Mattson | 8386ff5 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 11105 | } else if (!nested_cpu_has_ept(vmcs12) && |
| 11106 | nested_cpu_has2(vmcs12, |
| 11107 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 11108 | vmx_flush_tlb_ept_only(vcpu); |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 11109 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11110 | |
| 11111 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 11112 | vmx->host_rsp = 0; |
| 11113 | |
| 11114 | /* Unpin physical memory we referred to in vmcs02 */ |
| 11115 | if (vmx->nested.apic_access_page) { |
| 11116 | nested_release_page(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 11117 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11118 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11119 | if (vmx->nested.virtual_apic_page) { |
| 11120 | nested_release_page(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 11121 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11122 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11123 | if (vmx->nested.pi_desc_page) { |
| 11124 | kunmap(vmx->nested.pi_desc_page); |
| 11125 | nested_release_page(vmx->nested.pi_desc_page); |
| 11126 | vmx->nested.pi_desc_page = NULL; |
| 11127 | vmx->nested.pi_desc = NULL; |
| 11128 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11129 | |
| 11130 | /* |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 11131 | * We are now running in L2, mmu_notifier will force to reload the |
| 11132 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 11133 | */ |
Wanpeng Li | c83b6d1 | 2016-09-06 17:20:33 +0800 | [diff] [blame] | 11134 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 11135 | |
| 11136 | /* |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11137 | * Exiting from L2 to L1, we're now back to L1 which thinks it just |
| 11138 | * finished a VMLAUNCH or VMRESUME instruction, so we need to set the |
| 11139 | * success or failure flag accordingly. |
| 11140 | */ |
| 11141 | if (unlikely(vmx->fail)) { |
| 11142 | vmx->fail = 0; |
| 11143 | nested_vmx_failValid(vcpu, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 11144 | } else |
| 11145 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 11146 | if (enable_shadow_vmcs) |
| 11147 | vmx->nested.sync_shadow_vmcs = true; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11148 | |
| 11149 | /* in case we halted in L2 */ |
| 11150 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11151 | } |
| 11152 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11153 | /* |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 11154 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 11155 | */ |
| 11156 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 11157 | { |
Wanpeng Li | c886f28 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 11158 | if (is_guest_mode(vcpu)) { |
| 11159 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11160 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
Wanpeng Li | c886f28 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 11161 | } |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 11162 | free_nested(to_vmx(vcpu)); |
| 11163 | } |
| 11164 | |
| 11165 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11166 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 11167 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 11168 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 11169 | * It should only be called before L2 actually succeeded to run, and when |
| 11170 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 11171 | */ |
| 11172 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 11173 | struct vmcs12 *vmcs12, |
| 11174 | u32 reason, unsigned long qualification) |
| 11175 | { |
| 11176 | load_vmcs12_host_state(vcpu, vmcs12); |
| 11177 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 11178 | vmcs12->exit_qualification = qualification; |
| 11179 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 11180 | if (enable_shadow_vmcs) |
| 11181 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11182 | } |
| 11183 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 11184 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 11185 | struct x86_instruction_info *info, |
| 11186 | enum x86_intercept_stage stage) |
| 11187 | { |
| 11188 | return X86EMUL_CONTINUE; |
| 11189 | } |
| 11190 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11191 | #ifdef CONFIG_X86_64 |
| 11192 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 11193 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 11194 | u64 divisor, u64 *result) |
| 11195 | { |
| 11196 | u64 low = a << shift, high = a >> (64 - shift); |
| 11197 | |
| 11198 | /* To avoid the overflow on divq */ |
| 11199 | if (high >= divisor) |
| 11200 | return 1; |
| 11201 | |
| 11202 | /* Low hold the result, high hold rem which is discarded */ |
| 11203 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 11204 | "rm" (divisor), "0" (low), "1" (high)); |
| 11205 | *result = low; |
| 11206 | |
| 11207 | return 0; |
| 11208 | } |
| 11209 | |
| 11210 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 11211 | { |
| 11212 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 9175d2e | 2016-06-27 15:08:01 +0200 | [diff] [blame] | 11213 | u64 tscl = rdtsc(); |
| 11214 | u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 11215 | u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11216 | |
| 11217 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 11218 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 11219 | u64_shl_div_u64(delta_tsc, |
| 11220 | kvm_tsc_scaling_ratio_frac_bits, |
| 11221 | vcpu->arch.tsc_scaling_ratio, |
| 11222 | &delta_tsc)) |
| 11223 | return -ERANGE; |
| 11224 | |
| 11225 | /* |
| 11226 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 11227 | * we can't use the preemption timer. |
| 11228 | * It's possible that it fits on later vmentries, but checking |
| 11229 | * on every vmentry is costly so we just use an hrtimer. |
| 11230 | */ |
| 11231 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 11232 | return -ERANGE; |
| 11233 | |
| 11234 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
| 11235 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11236 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 11237 | return 0; |
| 11238 | } |
| 11239 | |
| 11240 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 11241 | { |
| 11242 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11243 | vmx->hv_deadline_tsc = -1; |
| 11244 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11245 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 11246 | } |
| 11247 | #endif |
| 11248 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 11249 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 11250 | { |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 11251 | if (ple_gap) |
| 11252 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 11253 | } |
| 11254 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 11255 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 11256 | struct kvm_memory_slot *slot) |
| 11257 | { |
| 11258 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 11259 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 11260 | } |
| 11261 | |
| 11262 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 11263 | struct kvm_memory_slot *slot) |
| 11264 | { |
| 11265 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 11266 | } |
| 11267 | |
| 11268 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 11269 | { |
| 11270 | kvm_flush_pml_buffers(kvm); |
| 11271 | } |
| 11272 | |
| 11273 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 11274 | struct kvm_memory_slot *memslot, |
| 11275 | gfn_t offset, unsigned long mask) |
| 11276 | { |
| 11277 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 11278 | } |
| 11279 | |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11280 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
| 11281 | { |
| 11282 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 11283 | struct pi_desc old, new; |
| 11284 | unsigned int dest; |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11285 | |
| 11286 | do { |
| 11287 | old.control = new.control = pi_desc->control; |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11288 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
| 11289 | "Wakeup handler not enabled while the VCPU is blocked\n"); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11290 | |
| 11291 | dest = cpu_physical_id(vcpu->cpu); |
| 11292 | |
| 11293 | if (x2apic_enabled()) |
| 11294 | new.ndst = dest; |
| 11295 | else |
| 11296 | new.ndst = (dest << 8) & 0xFF00; |
| 11297 | |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11298 | /* set 'NV' to 'notification vector' */ |
| 11299 | new.nv = POSTED_INTR_VECTOR; |
Paolo Bonzini | ea37f61 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 11300 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 11301 | new.control) != old.control); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11302 | |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11303 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
| 11304 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11305 | list_del(&vcpu->blocked_vcpu_list); |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11306 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11307 | vcpu->pre_pcpu = -1; |
| 11308 | } |
| 11309 | } |
| 11310 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11311 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11312 | * This routine does the following things for vCPU which is going |
| 11313 | * to be blocked if VT-d PI is enabled. |
| 11314 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 11315 | * we can find the right vCPU to wake up. |
| 11316 | * - Change the Posted-interrupt descriptor as below: |
| 11317 | * 'NDST' <-- vcpu->pre_pcpu |
| 11318 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 11319 | * - If 'ON' is set during this process, which means at least one |
| 11320 | * interrupt is posted for this vCPU, we cannot block it, in |
| 11321 | * this case, return 1, otherwise, return 0. |
| 11322 | * |
| 11323 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11324 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11325 | { |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11326 | unsigned int dest; |
| 11327 | struct pi_desc old, new; |
| 11328 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 11329 | |
| 11330 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 11331 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 11332 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11333 | return 0; |
| 11334 | |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11335 | WARN_ON(irqs_disabled()); |
| 11336 | local_irq_disable(); |
| 11337 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { |
| 11338 | vcpu->pre_pcpu = vcpu->cpu; |
| 11339 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 11340 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 11341 | &per_cpu(blocked_vcpu_on_cpu, |
| 11342 | vcpu->pre_pcpu)); |
| 11343 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 11344 | } |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11345 | |
| 11346 | do { |
| 11347 | old.control = new.control = pi_desc->control; |
| 11348 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11349 | WARN((pi_desc->sn == 1), |
| 11350 | "Warning: SN field of posted-interrupts " |
| 11351 | "is set before blocking\n"); |
| 11352 | |
| 11353 | /* |
| 11354 | * Since vCPU can be preempted during this process, |
| 11355 | * vcpu->cpu could be different with pre_pcpu, we |
| 11356 | * need to set pre_pcpu as the destination of wakeup |
| 11357 | * notification event, then we can find the right vCPU |
| 11358 | * to wakeup in wakeup handler if interrupts happen |
| 11359 | * when the vCPU is in blocked state. |
| 11360 | */ |
| 11361 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 11362 | |
| 11363 | if (x2apic_enabled()) |
| 11364 | new.ndst = dest; |
| 11365 | else |
| 11366 | new.ndst = (dest << 8) & 0xFF00; |
| 11367 | |
| 11368 | /* set 'NV' to 'wakeup vector' */ |
| 11369 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
Paolo Bonzini | ea37f61 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 11370 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 11371 | new.control) != old.control); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11372 | |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11373 | /* We should not block the vCPU if an interrupt is posted for it. */ |
| 11374 | if (pi_test_on(pi_desc) == 1) |
| 11375 | __pi_post_block(vcpu); |
| 11376 | |
| 11377 | local_irq_enable(); |
| 11378 | return (vcpu->pre_pcpu == -1); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11379 | } |
| 11380 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11381 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 11382 | { |
| 11383 | if (pi_pre_block(vcpu)) |
| 11384 | return 1; |
| 11385 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11386 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 11387 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 11388 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11389 | return 0; |
| 11390 | } |
| 11391 | |
| 11392 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11393 | { |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11394 | if (vcpu->pre_pcpu == -1) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11395 | return; |
| 11396 | |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11397 | WARN_ON(irqs_disabled()); |
| 11398 | local_irq_disable(); |
Paolo Bonzini | 01c58b0 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 11399 | __pi_post_block(vcpu); |
Paolo Bonzini | ff5eb8f | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 11400 | local_irq_enable(); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11401 | } |
| 11402 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11403 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
| 11404 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11405 | if (kvm_x86_ops->set_hv_timer) |
| 11406 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 11407 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11408 | pi_post_block(vcpu); |
| 11409 | } |
| 11410 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11411 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11412 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 11413 | * |
| 11414 | * @kvm: kvm |
| 11415 | * @host_irq: host irq of the interrupt |
| 11416 | * @guest_irq: gsi of the interrupt |
| 11417 | * @set: set or unset PI |
| 11418 | * returns 0 on success, < 0 on failure |
| 11419 | */ |
| 11420 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 11421 | uint32_t guest_irq, bool set) |
| 11422 | { |
| 11423 | struct kvm_kernel_irq_routing_entry *e; |
| 11424 | struct kvm_irq_routing_table *irq_rt; |
| 11425 | struct kvm_lapic_irq irq; |
| 11426 | struct kvm_vcpu *vcpu; |
| 11427 | struct vcpu_data vcpu_info; |
Jan H. Schönherr | 3d4213f | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 11428 | int idx, ret = 0; |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11429 | |
| 11430 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 11431 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 11432 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11433 | return 0; |
| 11434 | |
| 11435 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 11436 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
Jan H. Schönherr | 3d4213f | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 11437 | if (guest_irq >= irq_rt->nr_rt_entries || |
| 11438 | hlist_empty(&irq_rt->map[guest_irq])) { |
| 11439 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", |
| 11440 | guest_irq, irq_rt->nr_rt_entries); |
| 11441 | goto out; |
| 11442 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11443 | |
| 11444 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 11445 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 11446 | continue; |
| 11447 | /* |
| 11448 | * VT-d PI cannot support posting multicast/broadcast |
| 11449 | * interrupts to a vCPU, we still use interrupt remapping |
| 11450 | * for these kind of interrupts. |
| 11451 | * |
| 11452 | * For lowest-priority interrupts, we only support |
| 11453 | * those with single CPU as the destination, e.g. user |
| 11454 | * configures the interrupts via /proc/irq or uses |
| 11455 | * irqbalance to make the interrupts single-CPU. |
| 11456 | * |
| 11457 | * We will support full lowest-priority interrupt later. |
| 11458 | */ |
| 11459 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 11460 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 11461 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 11462 | /* |
| 11463 | * Make sure the IRTE is in remapped mode if |
| 11464 | * we don't handle it in posted mode. |
| 11465 | */ |
| 11466 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 11467 | if (ret < 0) { |
| 11468 | printk(KERN_INFO |
| 11469 | "failed to back to remapped mode, irq: %u\n", |
| 11470 | host_irq); |
| 11471 | goto out; |
| 11472 | } |
| 11473 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11474 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 11475 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11476 | |
| 11477 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 11478 | vcpu_info.vector = irq.vector; |
| 11479 | |
Feng Wu | b6ce978 | 2016-01-25 16:53:35 +0800 | [diff] [blame] | 11480 | trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11481 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 11482 | |
| 11483 | if (set) |
| 11484 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
Haozhong Zhang | 0c4e39c | 2017-09-18 09:56:49 +0800 | [diff] [blame] | 11485 | else |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11486 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11487 | |
| 11488 | if (ret < 0) { |
| 11489 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 11490 | __func__); |
| 11491 | goto out; |
| 11492 | } |
| 11493 | } |
| 11494 | |
| 11495 | ret = 0; |
| 11496 | out: |
| 11497 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 11498 | return ret; |
| 11499 | } |
| 11500 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 11501 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 11502 | { |
| 11503 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 11504 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 11505 | FEATURE_CONTROL_LMCE; |
| 11506 | else |
| 11507 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 11508 | ~FEATURE_CONTROL_LMCE; |
| 11509 | } |
| 11510 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 11511 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11512 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 11513 | .disabled_by_bios = vmx_disabled_by_bios, |
| 11514 | .hardware_setup = hardware_setup, |
| 11515 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 11516 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11517 | .hardware_enable = hardware_enable, |
| 11518 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 11519 | .cpu_has_accelerated_tpr = report_flexpriority, |
Tom Lendacky | b965592 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 11520 | .has_emulated_msr = vmx_has_emulated_msr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11521 | |
Konrad Rzeszutek Wilk | a0695af | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 11522 | .vm_init = vmx_vm_init, |
| 11523 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11524 | .vcpu_create = vmx_create_vcpu, |
| 11525 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 11526 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11527 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 11528 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11529 | .vcpu_load = vmx_vcpu_load, |
| 11530 | .vcpu_put = vmx_vcpu_put, |
| 11531 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 11532 | .update_bp_intercept = update_exception_bitmap, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11533 | .get_msr = vmx_get_msr, |
| 11534 | .set_msr = vmx_set_msr, |
| 11535 | .get_segment_base = vmx_get_segment_base, |
| 11536 | .get_segment = vmx_get_segment, |
| 11537 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 11538 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11539 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 11540 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 11541 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 11542 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11543 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11544 | .set_cr3 = vmx_set_cr3, |
| 11545 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11546 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11547 | .get_idt = vmx_get_idt, |
| 11548 | .set_idt = vmx_set_idt, |
| 11549 | .get_gdt = vmx_get_gdt, |
| 11550 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 11551 | .get_dr6 = vmx_get_dr6, |
| 11552 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 11553 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 11554 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 11555 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11556 | .get_rflags = vmx_get_rflags, |
| 11557 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 11558 | |
| 11559 | .get_pkru = vmx_get_pkru, |
| 11560 | |
Paolo Bonzini | 0fdd74f | 2015-05-20 11:33:43 +0200 | [diff] [blame] | 11561 | .fpu_activate = vmx_fpu_activate, |
Avi Kivity | 02daab2 | 2009-12-30 12:40:26 +0200 | [diff] [blame] | 11562 | .fpu_deactivate = vmx_fpu_deactivate, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11563 | |
| 11564 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11565 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11566 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 11567 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11568 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 11569 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 11570 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 11571 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 11572 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11573 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 11574 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 11575 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 11576 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11577 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 11578 | .get_nmi_mask = vmx_get_nmi_mask, |
| 11579 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11580 | .enable_nmi_window = enable_nmi_window, |
| 11581 | .enable_irq_window = enable_irq_window, |
| 11582 | .update_cr8_intercept = update_cr8_intercept, |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 11583 | .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 11584 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 11585 | .get_enable_apicv = vmx_get_enable_apicv, |
| 11586 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 11587 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
| 11588 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 11589 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 11590 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 11591 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11592 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 11593 | .set_tss_addr = vmx_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 11594 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 11595 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 11596 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 11597 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 11598 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 11599 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 11600 | |
| 11601 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 11602 | |
| 11603 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 11604 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 11605 | |
| 11606 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 11607 | |
| 11608 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 11609 | |
| 11610 | .write_tsc_offset = vmx_write_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 11611 | |
| 11612 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 11613 | |
| 11614 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 11615 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 11616 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 11617 | .xsaves_supported = vmx_xsaves_supported, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11618 | |
| 11619 | .check_nested_events = vmx_check_nested_events, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 11620 | |
| 11621 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 11622 | |
| 11623 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 11624 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 11625 | .flush_log_dirty = vmx_flush_log_dirty, |
| 11626 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 11627 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11628 | .pre_block = vmx_pre_block, |
| 11629 | .post_block = vmx_post_block, |
| 11630 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 11631 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11632 | |
| 11633 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11634 | |
| 11635 | #ifdef CONFIG_X86_64 |
| 11636 | .set_hv_timer = vmx_set_hv_timer, |
| 11637 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 11638 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 11639 | |
| 11640 | .setup_mce = vmx_setup_mce, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11641 | }; |
| 11642 | |
Paolo Bonzini | b3d648a | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 11643 | static int __init vmx_setup_l1d_flush(void) |
Konrad Rzeszutek Wilk | af6ce92 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 11644 | { |
Paolo Bonzini | b3d648a | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 11645 | struct page *page; |
| 11646 | |
Konrad Rzeszutek Wilk | af6ce92 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 11647 | if (vmentry_l1d_flush == VMENTER_L1D_FLUSH_NEVER || |
| 11648 | !boot_cpu_has_bug(X86_BUG_L1TF)) |
Paolo Bonzini | b3d648a | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 11649 | return 0; |
Konrad Rzeszutek Wilk | af6ce92 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 11650 | |
Paolo Bonzini | acca8a7 | 2018-07-02 13:03:48 +0200 | [diff] [blame] | 11651 | if (!boot_cpu_has(X86_FEATURE_FLUSH_L1D)) { |
| 11652 | page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER); |
| 11653 | if (!page) |
| 11654 | return -ENOMEM; |
| 11655 | vmx_l1d_flush_pages = page_address(page); |
| 11656 | } |
Paolo Bonzini | b3d648a | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 11657 | |
Konrad Rzeszutek Wilk | af6ce92 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 11658 | static_branch_enable(&vmx_l1d_should_flush); |
Paolo Bonzini | b3d648a | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 11659 | return 0; |
| 11660 | } |
| 11661 | |
| 11662 | static void vmx_free_l1d_flush_pages(void) |
| 11663 | { |
| 11664 | if (vmx_l1d_flush_pages) { |
| 11665 | free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER); |
| 11666 | vmx_l1d_flush_pages = NULL; |
| 11667 | } |
Konrad Rzeszutek Wilk | af6ce92 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 11668 | } |
| 11669 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11670 | static int __init vmx_init(void) |
| 11671 | { |
Konrad Rzeszutek Wilk | af6ce92 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 11672 | int r; |
| 11673 | |
Paolo Bonzini | b3d648a | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 11674 | r = vmx_setup_l1d_flush(); |
| 11675 | if (r) |
| 11676 | return r; |
Konrad Rzeszutek Wilk | af6ce92 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 11677 | |
| 11678 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
| 11679 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
Paolo Bonzini | b3d648a | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 11680 | if (r) { |
| 11681 | vmx_free_l1d_flush_pages(); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 11682 | return r; |
Paolo Bonzini | b3d648a | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 11683 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 11684 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 11685 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 11686 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 11687 | crash_vmclear_local_loaded_vmcss); |
| 11688 | #endif |
| 11689 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 11690 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11691 | } |
| 11692 | |
| 11693 | static void __exit vmx_exit(void) |
| 11694 | { |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 11695 | #ifdef CONFIG_KEXEC_CORE |
Monam Agarwal | 3b63a43 | 2014-03-22 12:28:10 +0530 | [diff] [blame] | 11696 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 11697 | synchronize_rcu(); |
| 11698 | #endif |
| 11699 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 11700 | kvm_exit(); |
Paolo Bonzini | b3d648a | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 11701 | |
| 11702 | vmx_free_l1d_flush_pages(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11703 | } |
| 11704 | |
| 11705 | module_init(vmx_init) |
| 11706 | module_exit(vmx_exit) |